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