forked from snipe/snipe-it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTMLValidator.php
133 lines (122 loc) · 3.88 KB
/
HTMLValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* A helper class for Codeception (http:https://codeception.com/) that allows automated HTML5 Validation
* using the Nu Html Checker (http:https://validator.github.io/validator/) during acceptance testing.
* It uses local binaries and can therefore be run offline.
*
*
* Requirements:
* =============
*
* - Codeception with WebDriver set up (PhpBrowser doesn't work)
* - java is installed locally
* - The vnu.jar is installed locally (download the .zip from https://github.com/validator/validator/releases,
* it contains the .jar file)
*
*
* Installation:
* =============
*
* - Copy this file to _support/Helper/ in the codeception directory
* - Merge the following configuration to acceptance.suite.yml:
*
* modules:
* enabled:
* - \Helper\HTMLValidator
* config:
* \Helper\HTMLValidator:
* javaPath: /usr/bin/java
* vnuPath: /usr/local/bin/vnu.jar
*
*
*
* Usage:
* ======
*
* Validate the HTML of the current page:
* $I->validateHTML();
*
* Validate the HTML of the current page, but ignore errors containing the string "Ignoreit":
* $I->validateHTML(["Ignoreme"]);
*
*
*
* @license http:https://www.opensource.org/licenses/mit-license.html MIT License
* @author Tobias Hößl <[email protected]>
*/
namespace Helper;
use Codeception\TestCase;
class HTMLValidator extends \Codeception\Module
{
/**
* @param string $html
* @return array
* @throws \Exception
*/
private function validateByVNU($html)
{
$javaPath = $this->_getConfig('javaPath');
if (! $javaPath) {
$javaPath = 'java';
}
$vnuPath = $this->_getConfig('vnuPath');
if (! $vnuPath) {
$vnuPath = '/usr/local/bin/vnu.jar';
}
$filename = DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.uniqid('html-validate').'.html';
file_put_contents($filename, $html);
exec($javaPath.' -Xss1024k -jar '.$vnuPath.' --format json '.$filename.' 2>&1', $return);
$data = json_decode($return[0], true);
if (! $data || ! isset($data['messages']) || ! is_array($data['messages'])) {
throw new \Exception('Invalid data returned from validation service: '.$return);
}
return $data['messages'];
}
/**
* @return string
* @throws \Codeception\Exception\ModuleException
* @throws \Exception
*/
private function getPageSource()
{
if (! $this->hasModule('WebDriver')) {
throw new \Exception('This validator needs WebDriver to work');
}
/** @var \Codeception\Module\WebDriver $webdriver */
$webdriver = $this->getModule('WebDriver');
return $webdriver->webDriver->getPageSource();
}
/**
* @param string[] $ignoreMessages
*/
public function validateHTML($ignoreMessages = [])
{
$source = $this->getPageSource();
try {
$messages = $this->validateByVNU($source);
} catch (\Exception $e) {
$this->fail($e->getMessage());
return;
}
$failMessages = [];
$lines = explode("\n", $source);
foreach ($messages as $message) {
if ($message['type'] == 'error') {
$formattedMsg = '- Line '.$message['lastLine'].', column '.$message['lastColumn'].': '.
$message['message']."\n > ".$lines[$message['lastLine'] - 1];
$ignoring = false;
foreach ($ignoreMessages as $ignoreMessage) {
if (mb_stripos($formattedMsg, $ignoreMessage) !== false) {
$ignoring = true;
}
}
if (! $ignoring) {
$failMessages[] = $formattedMsg;
}
}
}
if (count($failMessages) > 0) {
\PHPUnit_Framework_Assert::fail('Invalid HTML: '."\n".implode("\n", $failMessages));
}
}
}