1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Testing\Feature\Concerns; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Concerns\HasPageMetadata; |
6
|
|
|
use Hyde\Framework\Contracts\AbstractPage; |
7
|
|
|
use Hyde\Framework\Contracts\RouteContract; |
8
|
|
|
use Hyde\Framework\Helpers\Meta; |
9
|
|
|
use Hyde\Framework\Models\Pages\MarkdownPage; |
10
|
|
|
use Hyde\Framework\Models\Route; |
11
|
|
|
use Hyde\Testing\TestCase; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @covers \Hyde\Framework\Concerns\HasPageMetadata |
15
|
|
|
* |
16
|
|
|
* @see \Hyde\Framework\Testing\Unit\HasPageMetadataRssFeedLinkTest |
17
|
|
|
*/ |
18
|
|
|
class HasPageMetadataTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
protected function setUp(): void |
21
|
|
|
{ |
22
|
|
|
parent::setUp(); |
23
|
|
|
|
24
|
|
|
config(['hyde.meta' => []]); |
25
|
|
|
config(['site.url' => null]); |
26
|
|
|
config(['site.pretty_urls' => false]); |
27
|
|
|
config(['site.generate_sitemap' => false]); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function makePage(): AbstractPage |
31
|
|
|
{ |
32
|
|
|
return new class extends AbstractPage |
33
|
|
|
{ |
34
|
|
|
use HasPageMetadata; |
|
|
|
|
35
|
|
|
|
36
|
|
|
public string $slug = 'foo'; |
37
|
|
|
public static string $outputDirectory = ''; |
38
|
|
|
}; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function test_get_canonical_url_returns_url_for_top_level_page() |
42
|
|
|
{ |
43
|
|
|
$page = $this->makePage(); |
44
|
|
|
config(['site.url' => 'https://example.com']); |
45
|
|
|
|
46
|
|
|
$this->assertEquals('https://example.com/foo.html', $page->getCanonicalUrl()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function test_get_canonical_url_returns_pretty_url_for_top_level_page() |
50
|
|
|
{ |
51
|
|
|
$page = $this->makePage(); |
52
|
|
|
config(['site.url' => 'https://example.com']); |
53
|
|
|
config(['site.pretty_urls' => true]); |
54
|
|
|
|
55
|
|
|
$this->assertEquals('https://example.com/foo', $page->getCanonicalUrl()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function test_get_canonical_url_returns_url_for_nested_page() |
59
|
|
|
{ |
60
|
|
|
$page = new class extends AbstractPage |
61
|
|
|
{ |
62
|
|
|
use HasPageMetadata; |
|
|
|
|
63
|
|
|
|
64
|
|
|
public string $slug = 'foo'; |
65
|
|
|
public static string $outputDirectory = ''; |
66
|
|
|
|
67
|
|
|
public function getCurrentPagePath(): string |
68
|
|
|
{ |
69
|
|
|
return 'bar/'.$this->slug; |
70
|
|
|
} |
71
|
|
|
}; |
72
|
|
|
config(['site.url' => 'https://example.com']); |
73
|
|
|
|
74
|
|
|
$this->assertEquals('https://example.com/bar/foo.html', $page->getCanonicalUrl()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function test_get_canonical_url_returns_url_for_deeply_nested_page() |
78
|
|
|
{ |
79
|
|
|
$page = new class extends AbstractPage |
80
|
|
|
{ |
81
|
|
|
use HasPageMetadata; |
|
|
|
|
82
|
|
|
|
83
|
|
|
public string $slug = 'foo'; |
84
|
|
|
public static string $outputDirectory = ''; |
85
|
|
|
|
86
|
|
|
public function getCurrentPagePath(): string |
87
|
|
|
{ |
88
|
|
|
return 'bar/baz/'.$this->slug; |
89
|
|
|
} |
90
|
|
|
}; |
91
|
|
|
config(['site.url' => 'https://example.com']); |
92
|
|
|
|
93
|
|
|
$this->assertEquals('https://example.com/bar/baz/foo.html', $page->getCanonicalUrl()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function test_can_use_canonical_url_returns_true_when_both_uri_path_and_slug_is_set() |
97
|
|
|
{ |
98
|
|
|
$page = $this->makePage(); |
99
|
|
|
config(['site.url' => 'https://example.com']); |
100
|
|
|
|
101
|
|
|
$this->assertTrue($page->canUseCanonicalUrl()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function test_can_use_canonical_url_returns_false_no_conditions_are_met() |
105
|
|
|
{ |
106
|
|
|
$page = new class extends AbstractPage |
107
|
|
|
{ |
108
|
|
|
use HasPageMetadata; |
|
|
|
|
109
|
|
|
|
110
|
|
|
public string $slug; |
111
|
|
|
public static string $outputDirectory = ''; |
112
|
|
|
}; |
113
|
|
|
|
114
|
|
|
$this->assertFalse($page->canUseCanonicalUrl()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function test_can_use_canonical_url_returns_false_when_only_one_condition_is_met() |
118
|
|
|
{ |
119
|
|
|
$page = new class extends AbstractPage |
120
|
|
|
{ |
121
|
|
|
use HasPageMetadata; |
|
|
|
|
122
|
|
|
|
123
|
|
|
public string $slug; |
124
|
|
|
public static string $outputDirectory = ''; |
125
|
|
|
}; |
126
|
|
|
config(['site.url' => 'https://example.com']); |
127
|
|
|
|
128
|
|
|
$this->assertFalse($page->canUseCanonicalUrl()); |
129
|
|
|
|
130
|
|
|
$page = new class extends AbstractPage |
131
|
|
|
{ |
132
|
|
|
use HasPageMetadata; |
|
|
|
|
133
|
|
|
|
134
|
|
|
public string $slug = 'foo'; |
135
|
|
|
}; |
136
|
|
|
config(['site.url' => null]); |
137
|
|
|
|
138
|
|
|
$this->assertFalse($page->canUseCanonicalUrl()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function test_render_page_metadata_returns_string_with_merged_metadata() |
142
|
|
|
{ |
143
|
|
|
$page = $this->makePage(); |
144
|
|
|
config(['site.url' => 'https://example.com']); |
145
|
|
|
|
146
|
|
|
config(['hyde.meta' => [ |
147
|
|
|
Meta::name('foo', 'bar'), |
148
|
|
|
]]); |
149
|
|
|
|
150
|
|
|
$this->assertEquals( |
151
|
|
|
'<meta name="foo" content="bar">'."\n". |
152
|
|
|
'<link rel="canonical" href="https://example.com/foo.html" />', |
153
|
|
|
$page->renderPageMetadata() |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function test_render_page_metadata_only_adds_canonical_if_conditions_are_met() |
158
|
|
|
{ |
159
|
|
|
$page = $this->makePage(); |
160
|
|
|
|
161
|
|
|
$this->assertEquals( |
162
|
|
|
'', |
163
|
|
|
$page->renderPageMetadata() |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function test_get_dynamic_metadata_only_adds_canonical_if_conditions_are_met() |
168
|
|
|
{ |
169
|
|
|
$page = $this->makePage(); |
170
|
|
|
|
171
|
|
|
$this->assertEquals( |
172
|
|
|
[], |
173
|
|
|
$page->getDynamicMetadata() |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function test_get_dynamic_metadata_adds_canonical_url_when_conditions_are_met() |
178
|
|
|
{ |
179
|
|
|
$page = $this->makePage(); |
180
|
|
|
config(['site.url' => 'https://example.com']); |
181
|
|
|
|
182
|
|
|
config(['hyde.meta' => [ |
183
|
|
|
Meta::name('foo', 'bar'), |
184
|
|
|
]]); |
185
|
|
|
|
186
|
|
|
$this->assertEquals(['<link rel="canonical" href="https://example.com/foo.html" />'], |
187
|
|
|
$page->getDynamicMetadata() |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function test_get_dynamic_metadata_adds_sitemap_link_when_conditions_are_met() |
192
|
|
|
{ |
193
|
|
|
$page = new class extends AbstractPage |
194
|
|
|
{ |
195
|
|
|
use HasPageMetadata; |
|
|
|
|
196
|
|
|
}; |
197
|
|
|
config(['site.url' => 'https://example.com']); |
198
|
|
|
config(['site.generate_sitemap' => true]); |
199
|
|
|
|
200
|
|
|
$this->assertEquals(['<link rel="sitemap" type="application/xml" title="Sitemap" href="https://example.com/sitemap.xml" />'], |
201
|
|
|
$page->getDynamicMetadata() |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function test_get_dynamic_metadata_does_not_add_sitemap_link_when_conditions_are_not_met() |
206
|
|
|
{ |
207
|
|
|
$page = new class extends AbstractPage |
208
|
|
|
{ |
209
|
|
|
use HasPageMetadata; |
|
|
|
|
210
|
|
|
}; |
211
|
|
|
config(['site.url' => 'https://example.com']); |
212
|
|
|
config(['site.generate_sitemap' => false]); |
213
|
|
|
|
214
|
|
|
$this->assertEquals([], |
215
|
|
|
$page->getDynamicMetadata() |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function test_has_twitter_title_in_config_returns_true_when_present_in_config() |
220
|
|
|
{ |
221
|
|
|
config(['hyde.meta' => [ |
222
|
|
|
Meta::name('twitter:title', 'foo'), |
223
|
|
|
]]); |
224
|
|
|
|
225
|
|
|
$page = $this->makeTestClass(); |
226
|
|
|
|
227
|
|
|
$this->assertTrue($page->hasTwitterTitleInConfig()); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function test_has_twitter_title_in_config_returns_false_when_not_present_in_config() |
231
|
|
|
{ |
232
|
|
|
config(['hyde.meta' => []]); |
233
|
|
|
|
234
|
|
|
$page = $this->makeTestClass(); |
235
|
|
|
|
236
|
|
|
$this->assertFalse($page->hasTwitterTitleInConfig()); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function test_has_open_graph_title_in_config_returns_true_when_present_in_config() |
240
|
|
|
{ |
241
|
|
|
config(['hyde.meta' => [ |
242
|
|
|
Meta::property('title', 'foo'), |
243
|
|
|
]]); |
244
|
|
|
|
245
|
|
|
$page = $this->makeTestClass(); |
246
|
|
|
|
247
|
|
|
$this->assertTrue($page->hasOpenGraphTitleInConfig()); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function test_has_open_graph_title_in_config_returns_false_when_not_present_in_config() |
251
|
|
|
{ |
252
|
|
|
config(['hyde.meta' => []]); |
253
|
|
|
|
254
|
|
|
$page = $this->makeTestClass(); |
255
|
|
|
|
256
|
|
|
$this->assertFalse($page->hasOpenGraphTitleInConfig()); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function test_get_dynamic_metadata_adds_twitter_and_open_graph_title_when_conditions_are_met() |
260
|
|
|
{ |
261
|
|
|
config(['hyde.meta' => [ |
262
|
|
|
Meta::name('twitter:title', 'foo'), |
263
|
|
|
Meta::property('title', 'foo'), |
264
|
|
|
]]); |
265
|
|
|
|
266
|
|
|
$page = new class extends AbstractPage |
267
|
|
|
{ |
268
|
|
|
use HasPageMetadata; |
269
|
|
|
|
270
|
|
|
public string $title = 'bar'; |
271
|
|
|
}; |
272
|
|
|
|
273
|
|
|
$this->assertEquals([ |
274
|
|
|
'<meta name="twitter:title" content="HydePHP - bar" />', |
275
|
|
|
'<meta property="og:title" content="HydePHP - bar" />', |
276
|
|
|
], |
277
|
|
|
$page->getDynamicMetadata() |
278
|
|
|
); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
protected function makeTestClass(): object |
282
|
|
|
{ |
283
|
|
|
return new class |
284
|
|
|
{ |
285
|
|
|
use HasPageMetadata; |
|
|
|
|
286
|
|
|
|
287
|
|
|
public function htmlTitle(?string $title = null): string |
288
|
|
|
{ |
289
|
|
|
return $title ?? ''; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
public function getRoute(): RouteContract |
293
|
|
|
{ |
294
|
|
|
return new Route(new MarkdownPage()); |
295
|
|
|
} |
296
|
|
|
}; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|