1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace MOrtola\BehatSEOContexts\Context; |
4
|
|
|
|
5
|
|
|
use Behat\Mink\Element\NodeElement; |
6
|
|
|
use Webmozart\Assert\Assert; |
7
|
|
|
|
8
|
|
|
class MetaContext extends BaseContext |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @Then the page canonical should be :expectedCanonicalUrl |
12
|
|
|
*/ |
13
|
|
|
public function thePageCanonicalShouldBe(string $expectedCanonicalUrl): void |
14
|
|
|
{ |
15
|
|
|
$this->assertCanonicalElementExists(); |
16
|
|
|
|
17
|
|
|
$canonicalElement = $this->getCanonicalElement(); |
18
|
|
|
|
19
|
|
|
Assert::notNull($canonicalElement); |
20
|
|
|
|
21
|
|
|
Assert::eq( |
22
|
|
|
$this->toAbsoluteUrl($expectedCanonicalUrl), |
23
|
|
|
$canonicalElement->getAttribute('href'), |
24
|
|
|
sprintf('Canonical url should be "%s"', $this->toAbsoluteUrl($expectedCanonicalUrl)) |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
private function assertCanonicalElementExists(): void |
29
|
|
|
{ |
30
|
|
|
Assert::notNull( |
31
|
|
|
$this->getCanonicalElement(), |
32
|
|
|
'Canonical element does not exist' |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
private function getCanonicalElement(): ?NodeElement |
37
|
|
|
{ |
38
|
|
|
return $this->getSession()->getPage()->find( |
39
|
|
|
'xpath', |
40
|
|
|
'//head/link[@rel="canonical"]' |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @Then the page canonical should not be empty |
46
|
|
|
*/ |
47
|
|
|
public function thePageCanonicalShouldNotBeEmpty(): void |
48
|
|
|
{ |
49
|
|
|
$this->assertCanonicalElementExists(); |
50
|
|
|
|
51
|
|
|
$canonicalElement = $this->getCanonicalElement(); |
52
|
|
|
|
53
|
|
|
Assert::notNull($canonicalElement); |
54
|
|
|
|
55
|
|
|
Assert::notEmpty( |
56
|
|
|
trim($canonicalElement->getAttribute('href') ?? ''), |
57
|
|
|
'Canonical url is empty' |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @Then the page meta robots should be noindex |
63
|
|
|
*/ |
64
|
|
|
public function thePageShouldBeNoindex(): void |
65
|
|
|
{ |
66
|
|
|
$metaRobotsElement = $this->getMetaRobotsElement(); |
67
|
|
|
|
68
|
|
|
Assert::notNull( |
69
|
|
|
$metaRobotsElement, |
70
|
|
|
'Meta robots does not exist.' |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
Assert::contains( |
74
|
|
|
strtolower($metaRobotsElement->getAttribute('content') ?? ''), |
75
|
|
|
'noindex', |
76
|
|
|
sprintf( |
77
|
|
|
'Url %s is not noindex: %s', |
78
|
|
|
$this->getCurrentUrl(), |
79
|
|
|
$metaRobotsElement->getHtml() |
80
|
|
|
) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function getMetaRobotsElement(): ?NodeElement |
85
|
|
|
{ |
86
|
|
|
return $this->getSession()->getPage()->find( |
87
|
|
|
'xpath', |
88
|
|
|
'//head/meta[@name="robots"]' |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @Then the page meta robots should not be noindex |
94
|
|
|
*/ |
95
|
|
|
public function thePageShouldNotBeNoindex(): void |
96
|
|
|
{ |
97
|
|
|
$this->assertInverse( |
98
|
|
|
[$this, 'thePageShouldBeNoindex'], |
99
|
|
|
'Page meta robots is noindex.' |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @Then /^the page title should be "(?P<expectedTitle>[^"]*)"$/ |
105
|
|
|
*/ |
106
|
|
|
public function thePageTitleShouldBe(string $expectedTitle): void |
107
|
|
|
{ |
108
|
|
|
$this->assertTitleElementExists(); |
109
|
|
|
|
110
|
|
|
$titleElement = $this->getTitleElement(); |
111
|
|
|
|
112
|
|
|
Assert::notNull($titleElement); |
113
|
|
|
|
114
|
|
|
Assert::eq( |
115
|
|
|
$expectedTitle, |
116
|
|
|
$titleElement->getText(), |
117
|
|
|
sprintf( |
118
|
|
|
'Title tag is not "%s"', |
119
|
|
|
$expectedTitle |
120
|
|
|
) |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function assertTitleElementExists(): void |
125
|
|
|
{ |
126
|
|
|
Assert::notNull( |
127
|
|
|
$this->getTitleElement(), |
128
|
|
|
'Title tag does not exist' |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function getTitleElement(): ?NodeElement |
133
|
|
|
{ |
134
|
|
|
return $this->getSession()->getPage()->find('css', 'title'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @Then the page title should be empty |
139
|
|
|
*/ |
140
|
|
|
public function thePageTitleShouldBeEmpty(): void |
141
|
|
|
{ |
142
|
|
|
$this->assertTitleElementExists(); |
143
|
|
|
|
144
|
|
|
$titleElement = $this->getTitleElement(); |
145
|
|
|
|
146
|
|
|
Assert::notNull($titleElement); |
147
|
|
|
|
148
|
|
|
Assert::isEmpty( |
149
|
|
|
trim($titleElement->getText()), |
150
|
|
|
'Title tag is not empty' |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @Then the page title should not be empty |
156
|
|
|
*/ |
157
|
|
|
public function thePageTitleShouldNotBeEmpty(): void |
158
|
|
|
{ |
159
|
|
|
$this->assertTitleElementExists(); |
160
|
|
|
|
161
|
|
|
$titleElement = $this->getTitleElement(); |
162
|
|
|
|
163
|
|
|
Assert::notNull($titleElement); |
164
|
|
|
|
165
|
|
|
Assert::notEmpty( |
166
|
|
|
trim($titleElement->getText()), |
167
|
|
|
'Title tag is empty' |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @Then /^the page meta description should be "(?P<expectedMetaDescription>[^"]*)"$/ |
173
|
|
|
*/ |
174
|
|
|
public function thePageMetaDescriptionShouldBe(string $expectedMetaDescription): void |
175
|
|
|
{ |
176
|
|
|
$this->assertPageMetaDescriptionElementExists(); |
177
|
|
|
|
178
|
|
|
$metaDescription = $this->getMetaDescriptionElement(); |
179
|
|
|
|
180
|
|
|
Assert::notNull($metaDescription); |
181
|
|
|
|
182
|
|
|
Assert::eq( |
183
|
|
|
$expectedMetaDescription, |
184
|
|
|
$metaDescription->getAttribute('content'), |
185
|
|
|
sprintf( |
186
|
|
|
'Meta description is not "%s"', |
187
|
|
|
$expectedMetaDescription |
188
|
|
|
) |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
private function assertPageMetaDescriptionElementExists(): void |
193
|
|
|
{ |
194
|
|
|
Assert::notNull( |
195
|
|
|
$this->getMetaDescriptionElement(), |
196
|
|
|
'Meta description does not exist' |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
private function getMetaDescriptionElement(): ?NodeElement |
201
|
|
|
{ |
202
|
|
|
return $this->getSession()->getPage()->find( |
203
|
|
|
'xpath', |
204
|
|
|
'//head/meta[@name="description"]' |
205
|
|
|
); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @Then the page meta description should be empty |
210
|
|
|
*/ |
211
|
|
|
public function thePageMetaDescriptionBeEmpty(): void |
212
|
|
|
{ |
213
|
|
|
$this->assertPageMetaDescriptionElementExists(); |
214
|
|
|
|
215
|
|
|
$metaDescription = $this->getMetaDescriptionElement(); |
216
|
|
|
|
217
|
|
|
Assert::notNull($metaDescription); |
218
|
|
|
|
219
|
|
|
Assert::isEmpty( |
220
|
|
|
trim($metaDescription->getAttribute('content') ?? ''), |
221
|
|
|
'Meta description is not empty' |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @Then the page meta description should not be empty |
227
|
|
|
*/ |
228
|
|
|
public function thePageMetaDescriptionNotBeEmpty(): void |
229
|
|
|
{ |
230
|
|
|
$this->assertPageMetaDescriptionElementExists(); |
231
|
|
|
|
232
|
|
|
$metaDescription = $this->getMetaDescriptionElement(); |
233
|
|
|
|
234
|
|
|
Assert::notNull($metaDescription); |
235
|
|
|
|
236
|
|
|
Assert::notEmpty( |
237
|
|
|
trim($metaDescription->getAttribute('content') ?? ''), |
238
|
|
|
'Meta description is empty' |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @Then the page canonical should not exist |
244
|
|
|
*/ |
245
|
|
|
public function thePageCanonicalShouldNotExist(): void |
246
|
|
|
{ |
247
|
|
|
Assert::null( |
248
|
|
|
$this->getCanonicalElement(), |
249
|
|
|
'Canonical does exist' |
250
|
|
|
); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @Then the page title should not exist |
255
|
|
|
*/ |
256
|
|
|
public function thePageTitleShouldNotExist(): void |
257
|
|
|
{ |
258
|
|
|
Assert::null( |
259
|
|
|
$this->getTitleElement(), |
260
|
|
|
'Title tag does exist.' |
261
|
|
|
); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @Then the page meta description should not exist |
266
|
|
|
*/ |
267
|
|
|
public function thePageMetaDescriptionShouldNotExist(): void |
268
|
|
|
{ |
269
|
|
|
Assert::null( |
270
|
|
|
$this->getMetaDescriptionElement(), |
271
|
|
|
'Meta description does exist' |
272
|
|
|
); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|