@@ -15,27 +15,27 @@ |
||
15 | 15 | */ |
16 | 16 | class HasFunctionCheckTest extends SapphireTest |
17 | 17 | { |
18 | - public function testCheckReportsMissingFunctions() |
|
19 | - { |
|
20 | - $check = new HasFunctionCheck('foo'); |
|
18 | + public function testCheckReportsMissingFunctions() |
|
19 | + { |
|
20 | + $check = new HasFunctionCheck('foo'); |
|
21 | 21 | |
22 | - $expected = [ |
|
23 | - EnvironmentCheck::ERROR, |
|
24 | - 'foo() doesn\'t exist' |
|
25 | - ]; |
|
22 | + $expected = [ |
|
23 | + EnvironmentCheck::ERROR, |
|
24 | + 'foo() doesn\'t exist' |
|
25 | + ]; |
|
26 | 26 | |
27 | - $this->assertEquals($expected, $check->check()); |
|
28 | - } |
|
27 | + $this->assertEquals($expected, $check->check()); |
|
28 | + } |
|
29 | 29 | |
30 | - public function testCheckReportsFoundFunctions() |
|
31 | - { |
|
32 | - $check = new HasFunctionCheck('class_exists'); |
|
30 | + public function testCheckReportsFoundFunctions() |
|
31 | + { |
|
32 | + $check = new HasFunctionCheck('class_exists'); |
|
33 | 33 | |
34 | - $expected = [ |
|
35 | - EnvironmentCheck::OK, |
|
36 | - 'class_exists() exists' |
|
37 | - ]; |
|
34 | + $expected = [ |
|
35 | + EnvironmentCheck::OK, |
|
36 | + 'class_exists() exists' |
|
37 | + ]; |
|
38 | 38 | |
39 | - $this->assertEquals($expected, $check->check()); |
|
40 | - } |
|
39 | + $this->assertEquals($expected, $check->check()); |
|
40 | + } |
|
41 | 41 | } |
@@ -18,80 +18,80 @@ |
||
18 | 18 | */ |
19 | 19 | class EnvironmentCheckerTest extends SapphireTest |
20 | 20 | { |
21 | - protected $usesDatabase = true; |
|
21 | + protected $usesDatabase = true; |
|
22 | 22 | |
23 | - protected function tearDown() |
|
24 | - { |
|
25 | - EnvironmentCheckSuite::reset(); |
|
26 | - parent::tearDown(); |
|
27 | - } |
|
23 | + protected function tearDown() |
|
24 | + { |
|
25 | + EnvironmentCheckSuite::reset(); |
|
26 | + parent::tearDown(); |
|
27 | + } |
|
28 | 28 | |
29 | - public function testOnlyLogsWithErrors() |
|
30 | - { |
|
31 | - Config::modify()->set(EnvironmentChecker::class, 'log_results_warning', true); |
|
32 | - Config::modify()->set(EnvironmentChecker::class, 'log_results_error', true); |
|
29 | + public function testOnlyLogsWithErrors() |
|
30 | + { |
|
31 | + Config::modify()->set(EnvironmentChecker::class, 'log_results_warning', true); |
|
32 | + Config::modify()->set(EnvironmentChecker::class, 'log_results_error', true); |
|
33 | 33 | |
34 | - EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckNoErrors()); |
|
34 | + EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckNoErrors()); |
|
35 | 35 | |
36 | - $logger = $this->getMockBuilder(Logger::class) |
|
37 | - ->disableOriginalConstructor() |
|
38 | - ->setMethods(['log']) |
|
39 | - ->getMock(); |
|
36 | + $logger = $this->getMockBuilder(Logger::class) |
|
37 | + ->disableOriginalConstructor() |
|
38 | + ->setMethods(['log']) |
|
39 | + ->getMock(); |
|
40 | 40 | |
41 | - $logger->expects($this->never())->method('log'); |
|
41 | + $logger->expects($this->never())->method('log'); |
|
42 | 42 | |
43 | - Injector::inst()->registerService($logger, LoggerInterface::class); |
|
43 | + Injector::inst()->registerService($logger, LoggerInterface::class); |
|
44 | 44 | |
45 | - (new EnvironmentChecker('test suite', 'test'))->index(); |
|
46 | - } |
|
45 | + (new EnvironmentChecker('test suite', 'test'))->index(); |
|
46 | + } |
|
47 | 47 | |
48 | - public function testLogsWithWarnings() |
|
49 | - { |
|
50 | - Config::modify()->set(EnvironmentChecker::class, 'log_results_warning', true); |
|
51 | - Config::modify()->set(EnvironmentChecker::class, 'log_results_error', false); |
|
48 | + public function testLogsWithWarnings() |
|
49 | + { |
|
50 | + Config::modify()->set(EnvironmentChecker::class, 'log_results_warning', true); |
|
51 | + Config::modify()->set(EnvironmentChecker::class, 'log_results_error', false); |
|
52 | 52 | |
53 | - EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckWarnings()); |
|
54 | - EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckErrors()); |
|
53 | + EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckWarnings()); |
|
54 | + EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckErrors()); |
|
55 | 55 | |
56 | - $logger = $this->getMockBuilder(Logger::class) |
|
57 | - ->disableOriginalConstructor() |
|
58 | - ->setMethods(['log']) |
|
59 | - ->getMock(); |
|
56 | + $logger = $this->getMockBuilder(Logger::class) |
|
57 | + ->disableOriginalConstructor() |
|
58 | + ->setMethods(['log']) |
|
59 | + ->getMock(); |
|
60 | 60 | |
61 | - $logger->expects($this->once()) |
|
62 | - ->method('log') |
|
63 | - ->withConsecutive( |
|
64 | - $this->equalTo(LogLevel::WARNING), |
|
65 | - $this->anything() |
|
66 | - ); |
|
61 | + $logger->expects($this->once()) |
|
62 | + ->method('log') |
|
63 | + ->withConsecutive( |
|
64 | + $this->equalTo(LogLevel::WARNING), |
|
65 | + $this->anything() |
|
66 | + ); |
|
67 | 67 | |
68 | - Injector::inst()->registerService($logger, LoggerInterface::class); |
|
68 | + Injector::inst()->registerService($logger, LoggerInterface::class); |
|
69 | 69 | |
70 | - (new EnvironmentChecker('test suite', 'test'))->index(); |
|
71 | - } |
|
70 | + (new EnvironmentChecker('test suite', 'test'))->index(); |
|
71 | + } |
|
72 | 72 | |
73 | - public function testLogsWithErrors() |
|
74 | - { |
|
75 | - Config::modify()->set(EnvironmentChecker::class, 'log_results_error', false); |
|
76 | - Config::modify()->set(EnvironmentChecker::class, 'log_results_error', true); |
|
73 | + public function testLogsWithErrors() |
|
74 | + { |
|
75 | + Config::modify()->set(EnvironmentChecker::class, 'log_results_error', false); |
|
76 | + Config::modify()->set(EnvironmentChecker::class, 'log_results_error', true); |
|
77 | 77 | |
78 | - EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckWarnings()); |
|
79 | - EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckErrors()); |
|
78 | + EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckWarnings()); |
|
79 | + EnvironmentCheckSuite::register('test suite', new EnvironmentCheckerTest\CheckErrors()); |
|
80 | 80 | |
81 | - $logger = $this->getMockBuilder(Logger::class) |
|
82 | - ->disableOriginalConstructor() |
|
83 | - ->setMethods(['log']) |
|
84 | - ->getMock(); |
|
81 | + $logger = $this->getMockBuilder(Logger::class) |
|
82 | + ->disableOriginalConstructor() |
|
83 | + ->setMethods(['log']) |
|
84 | + ->getMock(); |
|
85 | 85 | |
86 | - $logger->expects($this->once()) |
|
87 | - ->method('log') |
|
88 | - ->withConsecutive( |
|
89 | - [$this->equalTo(LogLevel::ALERT), $this->anything()], |
|
90 | - [$this->equalTo(LogLevel::WARNING), $this->anything()] |
|
91 | - ); |
|
86 | + $logger->expects($this->once()) |
|
87 | + ->method('log') |
|
88 | + ->withConsecutive( |
|
89 | + [$this->equalTo(LogLevel::ALERT), $this->anything()], |
|
90 | + [$this->equalTo(LogLevel::WARNING), $this->anything()] |
|
91 | + ); |
|
92 | 92 | |
93 | - Injector::inst()->registerService($logger, LoggerInterface::class); |
|
93 | + Injector::inst()->registerService($logger, LoggerInterface::class); |
|
94 | 94 | |
95 | - (new EnvironmentChecker('test suite', 'test'))->index(); |
|
96 | - } |
|
95 | + (new EnvironmentChecker('test suite', 'test'))->index(); |
|
96 | + } |
|
97 | 97 | } |
@@ -7,8 +7,8 @@ |
||
7 | 7 | |
8 | 8 | class CheckNoErrors implements EnvironmentCheck, TestOnly |
9 | 9 | { |
10 | - public function check() |
|
11 | - { |
|
12 | - return [EnvironmentCheck::OK, '']; |
|
13 | - } |
|
10 | + public function check() |
|
11 | + { |
|
12 | + return [EnvironmentCheck::OK, '']; |
|
13 | + } |
|
14 | 14 | } |
@@ -7,8 +7,8 @@ |
||
7 | 7 | |
8 | 8 | class CheckErrors implements EnvironmentCheck, TestOnly |
9 | 9 | { |
10 | - public function check() |
|
11 | - { |
|
12 | - return [EnvironmentCheck::ERROR, 'test error']; |
|
13 | - } |
|
10 | + public function check() |
|
11 | + { |
|
12 | + return [EnvironmentCheck::ERROR, 'test error']; |
|
13 | + } |
|
14 | 14 | } |
@@ -7,8 +7,8 @@ |
||
7 | 7 | |
8 | 8 | class CheckWarnings implements EnvironmentCheck, TestOnly |
9 | 9 | { |
10 | - public function check() |
|
11 | - { |
|
12 | - return [EnvironmentCheck::WARNING, 'test warning']; |
|
13 | - } |
|
10 | + public function check() |
|
11 | + { |
|
12 | + return [EnvironmentCheck::WARNING, 'test warning']; |
|
13 | + } |
|
14 | 14 | } |
@@ -15,38 +15,38 @@ |
||
15 | 15 | */ |
16 | 16 | class SolrIndexCheck implements EnvironmentCheck |
17 | 17 | { |
18 | - /** |
|
19 | - * {@inheritDoc} |
|
20 | - * |
|
21 | - * @return array |
|
22 | - */ |
|
23 | - public function check() |
|
24 | - { |
|
25 | - $brokenCores = []; |
|
18 | + /** |
|
19 | + * {@inheritDoc} |
|
20 | + * |
|
21 | + * @return array |
|
22 | + */ |
|
23 | + public function check() |
|
24 | + { |
|
25 | + $brokenCores = []; |
|
26 | 26 | |
27 | - if (!class_exists(Solr::class)) { |
|
28 | - return [ |
|
29 | - EnvironmentCheck::ERROR, |
|
30 | - 'Class `' . Solr::class . '` not found. Is the fulltextsearch module installed?' |
|
31 | - ]; |
|
32 | - } |
|
27 | + if (!class_exists(Solr::class)) { |
|
28 | + return [ |
|
29 | + EnvironmentCheck::ERROR, |
|
30 | + 'Class `' . Solr::class . '` not found. Is the fulltextsearch module installed?' |
|
31 | + ]; |
|
32 | + } |
|
33 | 33 | |
34 | - $service = Solr::service(); |
|
35 | - foreach (Solr::get_indexes() as $index) { |
|
36 | - /** @var SolrIndex $core */ |
|
37 | - $core = $index->getIndexName(); |
|
38 | - if (!$service->coreIsActive($core)) { |
|
39 | - $brokenCores[] = $core; |
|
40 | - } |
|
41 | - } |
|
34 | + $service = Solr::service(); |
|
35 | + foreach (Solr::get_indexes() as $index) { |
|
36 | + /** @var SolrIndex $core */ |
|
37 | + $core = $index->getIndexName(); |
|
38 | + if (!$service->coreIsActive($core)) { |
|
39 | + $brokenCores[] = $core; |
|
40 | + } |
|
41 | + } |
|
42 | 42 | |
43 | - if (!empty($brokenCores)) { |
|
44 | - return [ |
|
45 | - EnvironmentCheck::ERROR, |
|
46 | - 'The following indexes are unavailable: ' . implode($brokenCores, ', ') |
|
47 | - ]; |
|
48 | - } |
|
43 | + if (!empty($brokenCores)) { |
|
44 | + return [ |
|
45 | + EnvironmentCheck::ERROR, |
|
46 | + 'The following indexes are unavailable: ' . implode($brokenCores, ', ') |
|
47 | + ]; |
|
48 | + } |
|
49 | 49 | |
50 | - return [EnvironmentCheck::OK, 'Expected indexes are available.']; |
|
51 | - } |
|
50 | + return [EnvironmentCheck::OK, 'Expected indexes are available.']; |
|
51 | + } |
|
52 | 52 | } |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | if (!class_exists(Solr::class)) { |
28 | 28 | return [ |
29 | 29 | EnvironmentCheck::ERROR, |
30 | - 'Class `' . Solr::class . '` not found. Is the fulltextsearch module installed?' |
|
30 | + 'Class `'.Solr::class.'` not found. Is the fulltextsearch module installed?' |
|
31 | 31 | ]; |
32 | 32 | } |
33 | 33 | |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | if (!empty($brokenCores)) { |
44 | 44 | return [ |
45 | 45 | EnvironmentCheck::ERROR, |
46 | - 'The following indexes are unavailable: ' . implode($brokenCores, ', ') |
|
46 | + 'The following indexes are unavailable: '.implode($brokenCores, ', ') |
|
47 | 47 | ]; |
48 | 48 | } |
49 | 49 |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | $goodTypes = [ValidationResult::TYPE_GOOD, ValidationResult::TYPE_INFO]; |
124 | 124 | $good = array_filter( |
125 | 125 | $this->result->getMessages(), |
126 | - function ($val, $key) use ($goodTypes) { |
|
126 | + function($val, $key) use ($goodTypes) { |
|
127 | 127 | if (in_array($val['messageType'], $goodTypes)) { |
128 | 128 | return true; |
129 | 129 | } |
@@ -132,14 +132,14 @@ discard block |
||
132 | 132 | ARRAY_FILTER_USE_BOTH |
133 | 133 | ); |
134 | 134 | if (!empty($good)) { |
135 | - $ret .= "GOOD: " . implode('; ', array_column($good, 'message')) . " "; |
|
135 | + $ret .= "GOOD: ".implode('; ', array_column($good, 'message'))." "; |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | // Filter bad messages |
139 | 139 | $badTypes = [ValidationResult::TYPE_ERROR, ValidationResult::TYPE_WARNING]; |
140 | 140 | $bad = array_filter( |
141 | 141 | $this->result->getMessages(), |
142 | - function ($val, $key) use ($badTypes) { |
|
142 | + function($val, $key) use ($badTypes) { |
|
143 | 143 | if (in_array($val['messageType'], $badTypes)) { |
144 | 144 | return true; |
145 | 145 | } |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | ARRAY_FILTER_USE_BOTH |
149 | 149 | ); |
150 | 150 | if (!empty($bad)) { |
151 | - $ret .= "BAD: " . implode('; ', array_column($bad, 'message')); |
|
151 | + $ret .= "BAD: ".implode('; ', array_column($bad, 'message')); |
|
152 | 152 | } |
153 | 153 | return $ret; |
154 | 154 | } |
@@ -20,188 +20,188 @@ |
||
20 | 20 | */ |
21 | 21 | class CacheHeadersCheck implements EnvironmentCheck |
22 | 22 | { |
23 | - use Fetcher; |
|
24 | - |
|
25 | - /** |
|
26 | - * Settings that must be included in the Cache-Control header |
|
27 | - * |
|
28 | - * @var array |
|
29 | - */ |
|
30 | - protected $mustInclude = []; |
|
31 | - |
|
32 | - /** |
|
33 | - * Settings that must be excluded in the Cache-Control header |
|
34 | - * |
|
35 | - * @var array |
|
36 | - */ |
|
37 | - protected $mustExclude = []; |
|
38 | - |
|
39 | - /** |
|
40 | - * Result to keep track of status and messages for all checks, reuses |
|
41 | - * ValidationResult for convenience. |
|
42 | - * |
|
43 | - * @var ValidationResult |
|
44 | - */ |
|
45 | - protected $result; |
|
46 | - |
|
47 | - /** |
|
48 | - * Set up with URL, arrays of header settings to check. |
|
49 | - * |
|
50 | - * @param string $url |
|
51 | - * @param array $mustInclude Settings that must be included in Cache-Control |
|
52 | - * @param array $mustExclude Settings that must be excluded in Cache-Control |
|
53 | - */ |
|
54 | - public function __construct($url = '', $mustInclude = [], $mustExclude = []) |
|
55 | - { |
|
56 | - $this->setURL($url); |
|
57 | - $this->mustInclude = $mustInclude; |
|
58 | - $this->mustExclude = $mustExclude; |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Check that correct caching headers are present. |
|
63 | - * |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - public function check() |
|
67 | - { |
|
68 | - // Using a validation result to capture messages |
|
69 | - $this->result = new ValidationResult(); |
|
70 | - |
|
71 | - $response = $this->client->get($this->getURL()); |
|
72 | - $fullURL = $this->getURL(); |
|
73 | - if ($response === null) { |
|
74 | - return [ |
|
75 | - EnvironmentCheck::ERROR, |
|
76 | - "Cache headers check request failed for $fullURL", |
|
77 | - ]; |
|
78 | - } |
|
79 | - |
|
80 | - //Check that Etag exists |
|
81 | - $this->checkEtag($response); |
|
82 | - |
|
83 | - // Check Cache-Control settings |
|
84 | - $this->checkCacheControl($response); |
|
85 | - |
|
86 | - if ($this->result->isValid()) { |
|
87 | - return [ |
|
88 | - EnvironmentCheck::OK, |
|
89 | - $this->getMessage(), |
|
90 | - ]; |
|
91 | - } else { |
|
92 | - // @todo Ability to return a warning |
|
93 | - return [ |
|
94 | - EnvironmentCheck::ERROR, |
|
95 | - $this->getMessage(), |
|
96 | - ]; |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Collate messages from ValidationResult so that it is clear which parts |
|
102 | - * of the check passed and which failed. |
|
103 | - * |
|
104 | - * @return string |
|
105 | - */ |
|
106 | - private function getMessage() |
|
107 | - { |
|
108 | - $ret = ''; |
|
109 | - // Filter good messages |
|
110 | - $goodTypes = [ValidationResult::TYPE_GOOD, ValidationResult::TYPE_INFO]; |
|
111 | - $good = array_filter( |
|
112 | - $this->result->getMessages(), |
|
113 | - function ($val, $key) use ($goodTypes) { |
|
114 | - if (in_array($val['messageType'], $goodTypes)) { |
|
115 | - return true; |
|
116 | - } |
|
117 | - return false; |
|
118 | - }, |
|
119 | - ARRAY_FILTER_USE_BOTH |
|
120 | - ); |
|
121 | - if (!empty($good)) { |
|
122 | - $ret .= "GOOD: " . implode('; ', array_column($good, 'message')) . " "; |
|
123 | - } |
|
124 | - |
|
125 | - // Filter bad messages |
|
126 | - $badTypes = [ValidationResult::TYPE_ERROR, ValidationResult::TYPE_WARNING]; |
|
127 | - $bad = array_filter( |
|
128 | - $this->result->getMessages(), |
|
129 | - function ($val, $key) use ($badTypes) { |
|
130 | - if (in_array($val['messageType'], $badTypes)) { |
|
131 | - return true; |
|
132 | - } |
|
133 | - return false; |
|
134 | - }, |
|
135 | - ARRAY_FILTER_USE_BOTH |
|
136 | - ); |
|
137 | - if (!empty($bad)) { |
|
138 | - $ret .= "BAD: " . implode('; ', array_column($bad, 'message')); |
|
139 | - } |
|
140 | - return $ret; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Check that ETag header exists |
|
145 | - * |
|
146 | - * @param ResponseInterface $response |
|
147 | - * @return void |
|
148 | - */ |
|
149 | - private function checkEtag(ResponseInterface $response) |
|
150 | - { |
|
151 | - $eTag = $response->getHeaderLine('ETag'); |
|
152 | - $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url); |
|
153 | - |
|
154 | - if ($eTag) { |
|
155 | - $this->result->addMessage( |
|
156 | - "$fullURL includes an Etag header in response", |
|
157 | - ValidationResult::TYPE_GOOD |
|
158 | - ); |
|
159 | - return; |
|
160 | - } |
|
161 | - $this->result->addError( |
|
162 | - "$fullURL is missing an Etag header", |
|
163 | - ValidationResult::TYPE_WARNING |
|
164 | - ); |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Check that the correct header settings are either included or excluded. |
|
169 | - * |
|
170 | - * @param ResponseInterface $response |
|
171 | - * @return void |
|
172 | - */ |
|
173 | - private function checkCacheControl(ResponseInterface $response) |
|
174 | - { |
|
175 | - $cacheControl = $response->getHeaderLine('Cache-Control'); |
|
176 | - $vals = array_map('trim', explode(',', $cacheControl)); |
|
177 | - $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url); |
|
178 | - |
|
179 | - // All entries from must contain should be present |
|
180 | - if ($this->mustInclude == array_intersect($this->mustInclude, $vals)) { |
|
181 | - $matched = implode(",", $this->mustInclude); |
|
182 | - $this->result->addMessage( |
|
183 | - "$fullURL includes all settings: {$matched}", |
|
184 | - ValidationResult::TYPE_GOOD |
|
185 | - ); |
|
186 | - } else { |
|
187 | - $missing = implode(",", array_diff($this->mustInclude, $vals)); |
|
188 | - $this->result->addError( |
|
189 | - "$fullURL is excluding some settings: {$missing}" |
|
190 | - ); |
|
191 | - } |
|
192 | - |
|
193 | - // All entries from must exclude should not be present |
|
194 | - if (empty(array_intersect($this->mustExclude, $vals))) { |
|
195 | - $missing = implode(",", $this->mustExclude); |
|
196 | - $this->result->addMessage( |
|
197 | - "$fullURL excludes all settings: {$missing}", |
|
198 | - ValidationResult::TYPE_GOOD |
|
199 | - ); |
|
200 | - } else { |
|
201 | - $matched = implode(",", array_intersect($this->mustExclude, $vals)); |
|
202 | - $this->result->addError( |
|
203 | - "$fullURL is including some settings: {$matched}" |
|
204 | - ); |
|
205 | - } |
|
206 | - } |
|
23 | + use Fetcher; |
|
24 | + |
|
25 | + /** |
|
26 | + * Settings that must be included in the Cache-Control header |
|
27 | + * |
|
28 | + * @var array |
|
29 | + */ |
|
30 | + protected $mustInclude = []; |
|
31 | + |
|
32 | + /** |
|
33 | + * Settings that must be excluded in the Cache-Control header |
|
34 | + * |
|
35 | + * @var array |
|
36 | + */ |
|
37 | + protected $mustExclude = []; |
|
38 | + |
|
39 | + /** |
|
40 | + * Result to keep track of status and messages for all checks, reuses |
|
41 | + * ValidationResult for convenience. |
|
42 | + * |
|
43 | + * @var ValidationResult |
|
44 | + */ |
|
45 | + protected $result; |
|
46 | + |
|
47 | + /** |
|
48 | + * Set up with URL, arrays of header settings to check. |
|
49 | + * |
|
50 | + * @param string $url |
|
51 | + * @param array $mustInclude Settings that must be included in Cache-Control |
|
52 | + * @param array $mustExclude Settings that must be excluded in Cache-Control |
|
53 | + */ |
|
54 | + public function __construct($url = '', $mustInclude = [], $mustExclude = []) |
|
55 | + { |
|
56 | + $this->setURL($url); |
|
57 | + $this->mustInclude = $mustInclude; |
|
58 | + $this->mustExclude = $mustExclude; |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Check that correct caching headers are present. |
|
63 | + * |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + public function check() |
|
67 | + { |
|
68 | + // Using a validation result to capture messages |
|
69 | + $this->result = new ValidationResult(); |
|
70 | + |
|
71 | + $response = $this->client->get($this->getURL()); |
|
72 | + $fullURL = $this->getURL(); |
|
73 | + if ($response === null) { |
|
74 | + return [ |
|
75 | + EnvironmentCheck::ERROR, |
|
76 | + "Cache headers check request failed for $fullURL", |
|
77 | + ]; |
|
78 | + } |
|
79 | + |
|
80 | + //Check that Etag exists |
|
81 | + $this->checkEtag($response); |
|
82 | + |
|
83 | + // Check Cache-Control settings |
|
84 | + $this->checkCacheControl($response); |
|
85 | + |
|
86 | + if ($this->result->isValid()) { |
|
87 | + return [ |
|
88 | + EnvironmentCheck::OK, |
|
89 | + $this->getMessage(), |
|
90 | + ]; |
|
91 | + } else { |
|
92 | + // @todo Ability to return a warning |
|
93 | + return [ |
|
94 | + EnvironmentCheck::ERROR, |
|
95 | + $this->getMessage(), |
|
96 | + ]; |
|
97 | + } |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Collate messages from ValidationResult so that it is clear which parts |
|
102 | + * of the check passed and which failed. |
|
103 | + * |
|
104 | + * @return string |
|
105 | + */ |
|
106 | + private function getMessage() |
|
107 | + { |
|
108 | + $ret = ''; |
|
109 | + // Filter good messages |
|
110 | + $goodTypes = [ValidationResult::TYPE_GOOD, ValidationResult::TYPE_INFO]; |
|
111 | + $good = array_filter( |
|
112 | + $this->result->getMessages(), |
|
113 | + function ($val, $key) use ($goodTypes) { |
|
114 | + if (in_array($val['messageType'], $goodTypes)) { |
|
115 | + return true; |
|
116 | + } |
|
117 | + return false; |
|
118 | + }, |
|
119 | + ARRAY_FILTER_USE_BOTH |
|
120 | + ); |
|
121 | + if (!empty($good)) { |
|
122 | + $ret .= "GOOD: " . implode('; ', array_column($good, 'message')) . " "; |
|
123 | + } |
|
124 | + |
|
125 | + // Filter bad messages |
|
126 | + $badTypes = [ValidationResult::TYPE_ERROR, ValidationResult::TYPE_WARNING]; |
|
127 | + $bad = array_filter( |
|
128 | + $this->result->getMessages(), |
|
129 | + function ($val, $key) use ($badTypes) { |
|
130 | + if (in_array($val['messageType'], $badTypes)) { |
|
131 | + return true; |
|
132 | + } |
|
133 | + return false; |
|
134 | + }, |
|
135 | + ARRAY_FILTER_USE_BOTH |
|
136 | + ); |
|
137 | + if (!empty($bad)) { |
|
138 | + $ret .= "BAD: " . implode('; ', array_column($bad, 'message')); |
|
139 | + } |
|
140 | + return $ret; |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * Check that ETag header exists |
|
145 | + * |
|
146 | + * @param ResponseInterface $response |
|
147 | + * @return void |
|
148 | + */ |
|
149 | + private function checkEtag(ResponseInterface $response) |
|
150 | + { |
|
151 | + $eTag = $response->getHeaderLine('ETag'); |
|
152 | + $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url); |
|
153 | + |
|
154 | + if ($eTag) { |
|
155 | + $this->result->addMessage( |
|
156 | + "$fullURL includes an Etag header in response", |
|
157 | + ValidationResult::TYPE_GOOD |
|
158 | + ); |
|
159 | + return; |
|
160 | + } |
|
161 | + $this->result->addError( |
|
162 | + "$fullURL is missing an Etag header", |
|
163 | + ValidationResult::TYPE_WARNING |
|
164 | + ); |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Check that the correct header settings are either included or excluded. |
|
169 | + * |
|
170 | + * @param ResponseInterface $response |
|
171 | + * @return void |
|
172 | + */ |
|
173 | + private function checkCacheControl(ResponseInterface $response) |
|
174 | + { |
|
175 | + $cacheControl = $response->getHeaderLine('Cache-Control'); |
|
176 | + $vals = array_map('trim', explode(',', $cacheControl)); |
|
177 | + $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url); |
|
178 | + |
|
179 | + // All entries from must contain should be present |
|
180 | + if ($this->mustInclude == array_intersect($this->mustInclude, $vals)) { |
|
181 | + $matched = implode(",", $this->mustInclude); |
|
182 | + $this->result->addMessage( |
|
183 | + "$fullURL includes all settings: {$matched}", |
|
184 | + ValidationResult::TYPE_GOOD |
|
185 | + ); |
|
186 | + } else { |
|
187 | + $missing = implode(",", array_diff($this->mustInclude, $vals)); |
|
188 | + $this->result->addError( |
|
189 | + "$fullURL is excluding some settings: {$missing}" |
|
190 | + ); |
|
191 | + } |
|
192 | + |
|
193 | + // All entries from must exclude should not be present |
|
194 | + if (empty(array_intersect($this->mustExclude, $vals))) { |
|
195 | + $missing = implode(",", $this->mustExclude); |
|
196 | + $this->result->addMessage( |
|
197 | + "$fullURL excludes all settings: {$missing}", |
|
198 | + ValidationResult::TYPE_GOOD |
|
199 | + ); |
|
200 | + } else { |
|
201 | + $matched = implode(",", array_intersect($this->mustExclude, $vals)); |
|
202 | + $this->result->addError( |
|
203 | + "$fullURL is including some settings: {$matched}" |
|
204 | + ); |
|
205 | + } |
|
206 | + } |
|
207 | 207 | } |
@@ -56,7 +56,7 @@ |
||
56 | 56 | if ($cookie) { |
57 | 57 | return [ |
58 | 58 | EnvironmentCheck::ERROR, |
59 | - "Sessions are being set for {$fullURL} : Set-Cookie => " . $cookie, |
|
59 | + "Sessions are being set for {$fullURL} : Set-Cookie => ".$cookie, |
|
60 | 60 | ]; |
61 | 61 | } |
62 | 62 | return [ |
@@ -14,58 +14,58 @@ |
||
14 | 14 | */ |
15 | 15 | class SessionCheck implements EnvironmentCheck |
16 | 16 | { |
17 | - use Fetcher; |
|
17 | + use Fetcher; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Set up check with URL |
|
21 | - * |
|
22 | - * @param string $url The route, excluding the domain |
|
23 | - * @inheritdoc |
|
24 | - */ |
|
25 | - public function __construct($url = '') |
|
26 | - { |
|
27 | - $this->setURL($url); |
|
28 | - } |
|
19 | + /** |
|
20 | + * Set up check with URL |
|
21 | + * |
|
22 | + * @param string $url The route, excluding the domain |
|
23 | + * @inheritdoc |
|
24 | + */ |
|
25 | + public function __construct($url = '') |
|
26 | + { |
|
27 | + $this->setURL($url); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * Check that the response for URL does not create a session |
|
32 | - * |
|
33 | - * @return array |
|
34 | - */ |
|
35 | - public function check() |
|
36 | - { |
|
37 | - $response = $this->client->get($this->getURL()); |
|
38 | - $cookie = $this->getCookie($response); |
|
39 | - $fullURL = $this->getURL(); |
|
30 | + /** |
|
31 | + * Check that the response for URL does not create a session |
|
32 | + * |
|
33 | + * @return array |
|
34 | + */ |
|
35 | + public function check() |
|
36 | + { |
|
37 | + $response = $this->client->get($this->getURL()); |
|
38 | + $cookie = $this->getCookie($response); |
|
39 | + $fullURL = $this->getURL(); |
|
40 | 40 | |
41 | - if ($cookie) { |
|
42 | - return [ |
|
43 | - EnvironmentCheck::ERROR, |
|
44 | - "Sessions are being set for {$fullURL} : Set-Cookie => " . $cookie, |
|
45 | - ]; |
|
46 | - } |
|
47 | - return [ |
|
48 | - EnvironmentCheck::OK, |
|
49 | - "Sessions are not being created for {$fullURL} |
@@ -13,28 +13,28 @@ |
||
13 | 13 | */ |
14 | 14 | class EnvTypeCheck implements EnvironmentCheck |
15 | 15 | { |
16 | - /** |
|
17 | - * Check the environment setting. |
|
18 | - * |
|
19 | - * @return array |
|
20 | - */ |
|
21 | - public function check() |
|
22 | - { |
|
23 | - $envSetting = Director::get_environment_type(); |
|
24 | - switch ($envSetting) { |
|
25 | - case 'live': |
|
26 | - return [ |
|
27 | - EnvironmentCheck::OK, |
|
28 | - "Env setting is 'live'", |
|
29 | - ]; |
|
30 | - // Fallthrough |
|
31 | - default: |
|
32 | - case 'dev': |
|
33 | - case 'test': |
|
34 | - return [ |
|
35 | - EnvironmentCheck::ERROR, |
|
36 | - "Env setting is '{$envSetting}' and may disclose information", |
|
37 | - ]; |
|
38 | - } |
|
39 | - } |
|
16 | + /** |
|
17 | + * Check the environment setting. |
|
18 | + * |
|
19 | + * @return array |
|
20 | + */ |
|
21 | + public function check() |
|
22 | + { |
|
23 | + $envSetting = Director::get_environment_type(); |
|
24 | + switch ($envSetting) { |
|
25 | + case 'live': |
|
26 | + return [ |
|
27 | + EnvironmentCheck::OK, |
|
28 | + "Env setting is 'live'", |
|
29 | + ]; |
|
30 | + // Fallthrough |
|
31 | + default: |
|
32 | + case 'dev': |
|
33 | + case 'test': |
|
34 | + return [ |
|
35 | + EnvironmentCheck::ERROR, |
|
36 | + "Env setting is '{$envSetting}' and may disclose information", |
|
37 | + ]; |
|
38 | + } |
|
39 | + } |
|
40 | 40 | } |