Passed
Push — master ( 656f92...afef6b )
by Marc
02:43
created

MetaContext::thePageMetaDescriptionBeEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
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->getOuterHtml($this->getMetaRobotsElement())
0 ignored issues
show
Bug introduced by
It seems like $this->getMetaRobotsElement() can also be of type null; however, parameter $nodeElement of MOrtola\BehatSEOContexts...Context::getOuterHtml() does only seem to accept Behat\Mink\Element\NodeElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
                $this->getOuterHtml(/** @scrutinizer ignore-type */ $this->getMetaRobotsElement())
Loading history...
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 "(?P<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 be empty
129
     */
130
    public function thePageTitleShouldBeEmpty()
131
    {
132
        $this->assertTitleElementExists();
133
134
        Assert::assertEmpty(
135
            trim($this->getTitleElement()->getText()),
136
            'Title tag is not empty'
137
        );
138
    }
139
140
    /**
141
     * @throws \Exception
142
     *
143
     * @Then the page title should not be empty
144
     */
145
    public function thePageTitleShouldNotBeEmpty()
146
    {
147
        $this->assertTitleElementExists();
148
149
        Assert::assertNotEmpty(
150
            trim($this->getTitleElement()->getText()),
151
            'Title tag is empty'
152
        );
153
    }
154
155
    /**
156
     * @return NodeElement|null
157
     */
158
    private function getTitleElement()
159
    {
160
        return $this->getSession()->getPage()->find('css', 'title');
161
    }
162
163
    /**
164
     * @throws \Exception
165
     */
166
    private function assertTitleElementExists()
167
    {
168
        Assert::assertNotNull(
169
            $this->getTitleElement(),
170
            'Title tag does not exist'
171
        );
172
    }
173
174
    /**
175
     * @throws \Exception
176
     *
177
     * @Then /^the page meta description should be "(?P<expectedMetaDescription>[^"]*)"$/
178
     */
179
    public function thePageMetaDescriptionShouldBe(string $expectedMetaDescription)
180
    {
181
        $this->assertPageMetaDescriptionElementExists();
182
183
        Assert::assertEquals(
184
            $expectedMetaDescription,
185
            $this->getMetaDescriptionElement()->getAttribute('content'),
186
            sprintf(
187
                'Meta description is not "%s"',
188
                $expectedMetaDescription
189
            )
190
        );
191
    }
192
193
    /**
194
     * @throws \Exception
195
     *
196
     * @Then the page meta description should be empty
197
     */
198
    public function thePageMetaDescriptionBeEmpty()
199
    {
200
        $this->assertPageMetaDescriptionElementExists();
201
202
        Assert::assertEmpty(
203
            trim($this->getMetaDescriptionElement()->getAttribute('content')),
204
            'Meta description is not empty'
205
        );
206
    }
207
208
    /**
209
     * @throws \Exception
210
     *
211
     * @Then the page meta description should not be empty
212
     */
213
    public function thePageMetaDescriptionNotBeEmpty()
214
    {
215
        $this->assertPageMetaDescriptionElementExists();
216
217
        Assert::assertNotEmpty(
218
            trim($this->getMetaDescriptionElement()->getAttribute('content')),
219
            'Meta description is empty'
220
        );
221
    }
222
223
    /**
224
     * @return NodeElement|null
225
     */
226
    private function getMetaDescriptionElement()
227
    {
228
        return $this->getSession()->getPage()->find(
229
            'xpath',
230
            '//head/meta[@name="description"]'
231
        );
232
    }
233
234
    /**
235
     * @throws \Exception
236
     */
237
    private function assertPageMetaDescriptionElementExists()
238
    {
239
        Assert::assertNotNull(
240
            $this->getMetaDescriptionElement(),
241
            'Meta description does not exist'
242
        );
243
    }
244
245
    /**
246
     * @Then the page canonical should not exist
247
     */
248
    public function thePageCanonicalShouldNotExist()
249
    {
250
        Assert::assertNull(
251
            $this->getCanonicalElement(),
252
            'Canonical does exist'
253
        );
254
    }
255
256
    /**
257
     * @Then the page title should not exist
258
     */
259
    public function thePageTitleShouldNotExist()
260
    {
261
        Assert::assertNull(
262
            $this->getTitleElement(),
263
            'Title tag does exist.'
264
        );
265
    }
266
267
    /**
268
     * @Then the page meta description should not exist
269
     */
270
    public function thePageMetaDescriptionShouldNotExist()
271
    {
272
        Assert::assertNull(
273
            $this->getMetaDescriptionElement(),
274
            'Meta description does exist'
275
        );
276
    }
277
}
278