1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace whm\Smoke\Rules\Html; |
4
|
|
|
|
5
|
|
|
use phm\HttpWebdriverClient\Http\Client\HeadlessChrome\HeadlessChromeResponse; |
6
|
|
|
use phm\HttpWebdriverClient\Http\Response\ResourcesAwareResponse; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use whm\Smoke\Rules\CheckResult; |
9
|
|
|
use whm\Smoke\Rules\StandardRule; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This rule checks if a scanned resource has 4xx or 5xx links |
13
|
|
|
*/ |
14
|
|
|
class BrokenLinkRule extends StandardRule |
15
|
|
|
{ |
16
|
|
|
protected $contentTypes = ['text/html']; |
17
|
|
|
|
18
|
|
|
private $excludedFiles = []; |
19
|
|
|
|
20
|
|
|
public function init($excludedFiles = array()) |
21
|
|
|
{ |
22
|
|
|
foreach ($excludedFiles as $fileName) { |
23
|
|
|
$this->excludedFiles[] = $fileName['filename']; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function doValidation(ResponseInterface $response) |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
if ($response instanceof ResourcesAwareResponse) { |
31
|
|
|
$resources = $response->getResources(); |
32
|
|
|
|
33
|
|
|
$errorList = []; |
34
|
|
|
|
35
|
|
|
foreach ($resources as $resource) { |
36
|
|
|
if ($resource['http_status'] >= 400) { |
37
|
|
|
$excluded = false; |
38
|
|
|
foreach ($this->excludedFiles as $excludedFile) { |
39
|
|
|
if (strpos($resource['name'], $excludedFile) !== false) { |
40
|
|
|
$excluded = true; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (!$excluded) { |
45
|
|
|
$errorList[] = $resource; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (count($errorList) > 0) { |
51
|
|
|
$count = count($errorList); |
52
|
|
|
$msg = 'Found ' . $count . ' resource(s) with status code 4xx or 5xx. <ul>'; |
53
|
|
|
foreach ($errorList as $error) { |
54
|
|
|
$msg .= '<li>' . $error['name'] . ' (http status: ' . $error['http_status'] . ')</li>'; |
55
|
|
|
} |
56
|
|
|
$msg .= '</ul>'; |
57
|
|
|
return new CheckResult(CheckResult::STATUS_FAILURE, $msg, $count); |
58
|
|
|
} else { |
59
|
|
|
return new CheckResult(CheckResult::STATUS_SUCCESS, 'No resources with status 4xx or 5xx found.', 0); |
60
|
|
|
} |
61
|
|
|
} else { |
62
|
|
|
return new CheckResult(CheckResult::STATUS_SKIPPED, 'Expected ResourcesAwareResponse. Skipped.'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|