1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace MOrtola\BehatSEOContexts\Context; |
4
|
|
|
|
5
|
|
|
use Behat\Behat\Tester\Exception\PendingException; |
6
|
|
|
use HtmlValidator\Exception\ServerException; |
7
|
|
|
use HtmlValidator\Exception\UnknownParserException; |
8
|
|
|
use HtmlValidator\Validator; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use Webmozart\Assert\Assert; |
11
|
|
|
|
12
|
|
|
class HTMLContext extends BaseContext |
13
|
|
|
{ |
14
|
|
|
const VALIDATION_SERVICES = [ |
15
|
|
|
Validator::DEFAULT_VALIDATOR_URL, |
16
|
|
|
'https://validator.nu/', |
17
|
|
|
'https://validator.w3.org/nu/', |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @Then the page HTML markup should be valid |
22
|
|
|
*/ |
23
|
|
|
public function thePageHtmlMarkupShouldBeValid(): void |
24
|
|
|
{ |
25
|
|
|
$validated = false; |
26
|
|
|
$validationErrors = []; |
27
|
|
|
|
28
|
|
|
foreach (self::VALIDATION_SERVICES as $validatorService) { |
29
|
|
|
try { |
30
|
|
|
$validator = new Validator($validatorService); |
31
|
|
|
$validatorResult = $validator->validateDocument($this->getSession()->getPage()->getContent()); |
32
|
|
|
$validated = true; |
33
|
|
|
$validationErrors = $validatorResult->getErrors(); |
34
|
|
|
break; |
35
|
|
|
} catch (ServerException | UnknownParserException $e) { |
36
|
|
|
// @ignoreException |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (!$validated) { |
41
|
|
|
throw new PendingException('HTML validation services are not working'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (isset($validationErrors[0])) { |
45
|
|
|
throw new InvalidArgumentException( |
46
|
|
|
sprintf( |
47
|
|
|
'HTML markup validation error: Line %s: "%s" - %s in %s', |
48
|
|
|
$validationErrors[0]->getFirstLine(), |
49
|
|
|
$validationErrors[0]->getExtract(), |
50
|
|
|
$validationErrors[0]->getText(), |
51
|
|
|
$this->getCurrentUrl() |
52
|
|
|
) |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @Then the page HTML markup should not be valid |
59
|
|
|
*/ |
60
|
|
|
public function thePageHtmlMarkupShouldNotBeValid(): void |
61
|
|
|
{ |
62
|
|
|
$this->assertInverse( |
63
|
|
|
[$this, 'thePageHtmlMarkupShouldBeValid'], |
64
|
|
|
'HTML markup should not be valid.' |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @Then the page HTML5 doctype declaration should be valid |
70
|
|
|
*/ |
71
|
|
|
public function thePageHtml5DoctypeDeclarationShouldBeValid(): void |
72
|
|
|
{ |
73
|
|
|
$pageContent = preg_replace( |
74
|
|
|
'/<!--(.|\s)*?-->/', |
75
|
|
|
'', |
76
|
|
|
$this->getSession()->getPage()->getContent() |
77
|
|
|
) ?? ''; |
78
|
|
|
|
79
|
|
|
Assert::startsWith( |
80
|
|
|
strtolower(trim($pageContent)), |
81
|
|
|
'<!doctype html>' |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @Then the page HTML5 doctype declaration should not be valid |
87
|
|
|
*/ |
88
|
|
|
public function thePageHtml5DoctypeDeclarationShouldNotBeValid(): void |
89
|
|
|
{ |
90
|
|
|
$this->assertInverse( |
91
|
|
|
[$this, 'thePageHtml5DoctypeDeclarationShouldBeValid'], |
92
|
|
|
'The page HTML5 doctype declaration should not be valid.' |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|