1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Testing\Feature\Concerns; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Concerns\HasArticleMetadata; |
6
|
|
|
use Hyde\Framework\Contracts\RouteContract; |
7
|
|
|
use Hyde\Framework\Models\Pages\MarkdownPost; |
8
|
|
|
use Hyde\Framework\Models\Route; |
9
|
|
|
use Hyde\Testing\TestCase; |
10
|
|
|
use Illuminate\Support\Facades\Config; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @covers \Hyde\Framework\Concerns\HasArticleMetadata |
14
|
|
|
* |
15
|
|
|
* @see \Hyde\Framework\Models\Metadata |
16
|
|
|
*/ |
17
|
|
|
class HasArticleMetadataTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
use HasArticleMetadata; |
20
|
|
|
|
21
|
|
|
public array $matter; |
22
|
|
|
protected string $slug; |
23
|
|
|
protected RouteContract $route; |
24
|
|
|
|
25
|
|
|
protected function setUp(): void |
26
|
|
|
{ |
27
|
|
|
parent::setUp(); |
28
|
|
|
|
29
|
|
|
Config::set('site.url', null); |
30
|
|
|
$this->route = new Route(new MarkdownPost()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function getRoute(): RouteContract |
34
|
|
|
{ |
35
|
|
|
return $this->route; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function tearDown(): void |
39
|
|
|
{ |
40
|
|
|
unset($this->metadata); |
41
|
|
|
unset($this->matter); |
42
|
|
|
|
43
|
|
|
parent::tearDown(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function test_get_metadata_returns_empty_array_when_uninitialized() |
47
|
|
|
{ |
48
|
|
|
$this->matter = ['description' => 'foo']; |
49
|
|
|
$this->assertEquals([], $this->getMetadata()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function test_get_metadata_returns_valid_array_when_initialized() |
53
|
|
|
{ |
54
|
|
|
$this->matter = [ |
55
|
|
|
'description' => 'foo', |
56
|
|
|
'author' => 'bar', |
57
|
|
|
'category' => 'cat', |
58
|
|
|
]; |
59
|
|
|
$this->constructMetadata(); |
60
|
|
|
$this->assertEquals([ |
61
|
|
|
'description' => 'foo', |
62
|
|
|
'author' => 'bar', |
63
|
|
|
'keywords' => 'cat', |
64
|
|
|
], $this->getMetadata()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function test_get_meta_properties_returns_empty_array_when_uninitialized() |
68
|
|
|
{ |
69
|
|
|
$this->assertEquals([], $this->getMetaProperties()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function test_get_meta_properties_returns_base_array_when_initialized_with_empty_front_matter() |
73
|
|
|
{ |
74
|
|
|
$this->constructMetadata(); |
75
|
|
|
|
76
|
|
|
$this->assertEquals(['og:type' => 'article'], $this->getMetaProperties()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Note that this currently assumes that the object using it is a Blog Post. |
80
|
|
|
public function test_get_meta_properties_contains_og_url_when_uri_path_set() |
81
|
|
|
{ |
82
|
|
|
Config::set('site.url', 'https://example.com/foo'); |
83
|
|
|
$this->route = new Route(new MarkdownPost(slug: 'bar')); |
84
|
|
|
$this->constructMetadata(); |
85
|
|
|
|
86
|
|
|
$this->assertEquals([ |
87
|
|
|
'og:type' => 'article', |
88
|
|
|
'og:url' => 'https://example.com/foo/posts/bar.html', |
89
|
|
|
], $this->getMetaProperties()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function test_get_meta_properties_contains_og_title_when_title_set() |
93
|
|
|
{ |
94
|
|
|
$this->matter = [ |
95
|
|
|
'title' => 'foo', |
96
|
|
|
]; |
97
|
|
|
$this->constructMetadata(); |
98
|
|
|
|
99
|
|
|
$this->assertEquals([ |
100
|
|
|
'og:type' => 'article', |
101
|
|
|
'og:title' => 'foo', |
102
|
|
|
], $this->getMetaProperties()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function test_get_meta_properties_contains_og_article_date_published_when_date_set() |
106
|
|
|
{ |
107
|
|
|
$this->matter = [ |
108
|
|
|
'date' => '2022-01-01 12:00', |
109
|
|
|
]; |
110
|
|
|
$this->constructMetadata(); |
111
|
|
|
|
112
|
|
|
$this->assertEquals([ |
113
|
|
|
'og:type' => 'article', |
114
|
|
|
'og:article:published_time' => '2022-01-01T12:00:00+00:00', |
115
|
|
|
], $this->getMetaProperties()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function test_get_meta_properties_contains_image_metadata_when_featured_image_set_to_string() |
119
|
|
|
{ |
120
|
|
|
$this->matter = [ |
121
|
|
|
'image' => 'foo.jpg', |
122
|
|
|
]; |
123
|
|
|
$this->constructMetadata(); |
124
|
|
|
|
125
|
|
|
$this->assertEquals([ |
126
|
|
|
'og:type' => 'article', |
127
|
|
|
'og:image' => 'foo.jpg', |
128
|
|
|
], $this->getMetaProperties()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function test_get_meta_properties_contains_image_metadata_when_featured_image_set_to_array_with_path() |
132
|
|
|
{ |
133
|
|
|
$this->matter = [ |
134
|
|
|
'image' => [ |
135
|
|
|
'path' => 'foo.jpg', |
136
|
|
|
], |
137
|
|
|
]; |
138
|
|
|
$this->constructMetadata(); |
139
|
|
|
|
140
|
|
|
$this->assertEquals([ |
141
|
|
|
'og:type' => 'article', |
142
|
|
|
'og:image' => 'foo.jpg', |
143
|
|
|
], $this->getMetaProperties()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function test_get_meta_properties_contains_image_metadata_when_featured_image_set_to_array_with_uri() |
147
|
|
|
{ |
148
|
|
|
$this->matter = [ |
149
|
|
|
'image' => [ |
150
|
|
|
'uri' => 'foo.jpg', |
151
|
|
|
], |
152
|
|
|
]; |
153
|
|
|
$this->constructMetadata(); |
154
|
|
|
|
155
|
|
|
$this->assertEquals([ |
156
|
|
|
'og:type' => 'article', |
157
|
|
|
'og:image' => 'foo.jpg', |
158
|
|
|
], $this->getMetaProperties()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function test_get_author_returns_author_name_when_author_set_to_array_using_username() |
162
|
|
|
{ |
163
|
|
|
$this->matter = [ |
164
|
|
|
'author' => [ |
165
|
|
|
'username' => 'foo', |
166
|
|
|
], |
167
|
|
|
]; |
168
|
|
|
$this->constructMetadata(); |
169
|
|
|
|
170
|
|
|
$this->assertEquals([ |
171
|
|
|
'author' => 'foo', |
172
|
|
|
], $this->getMetadata()); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function test_get_author_returns_author_name_when_author_set_to_array_using_name() |
176
|
|
|
{ |
177
|
|
|
$this->matter = [ |
178
|
|
|
'author' => [ |
179
|
|
|
'name' => 'foo', |
180
|
|
|
], |
181
|
|
|
]; |
182
|
|
|
$this->constructMetadata(); |
183
|
|
|
|
184
|
|
|
$this->assertEquals([ |
185
|
|
|
'author' => 'foo', |
186
|
|
|
], $this->getMetadata()); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function test_get_author_returns_guest_when_author_set_to_array_without_name_or_username() |
190
|
|
|
{ |
191
|
|
|
$this->matter = [ |
192
|
|
|
'author' => [], |
193
|
|
|
]; |
194
|
|
|
$this->constructMetadata(); |
195
|
|
|
|
196
|
|
|
$this->assertEquals([ |
197
|
|
|
'author' => 'Guest', |
198
|
|
|
], $this->getMetadata()); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|