X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fsecurity_review%2Fsrc%2FTests%2FCheckTest.php;fp=web%2Fmodules%2Fcontrib%2Fsecurity_review%2Fsrc%2FTests%2FCheckTest.php;h=1ac1b1cf9275476e2ad53c75e4377c22abf9c763;hp=0000000000000000000000000000000000000000;hb=ba1b5c55c66590c41ccc9844d3e62391b0399abb;hpb=93ef30d42f68e55d11d97312531118bbcd4cf318 diff --git a/web/modules/contrib/security_review/src/Tests/CheckTest.php b/web/modules/contrib/security_review/src/Tests/CheckTest.php new file mode 100644 index 000000000..1ac1b1cf9 --- /dev/null +++ b/web/modules/contrib/security_review/src/Tests/CheckTest.php @@ -0,0 +1,142 @@ +realChecks = security_review_security_review_checks(); + $this->testChecks = security_review_test_security_review_checks(); + $this->checks = array_merge($this->realChecks, $this->testChecks); + } + + /** + * Tests whether $checks is empty. + */ + protected function testChecksExist() { + $this->assertFalse(empty($this->checks), 'Checks found.'); + } + + /** + * Every check should be enabled by default. + */ + public function testEnabledByDefault() { + foreach ($this->checks as $check) { + $this->assertFalse($check->isSkipped(), $check->getTitle() . ' is enabled by default.'); + } + } + + /** + * Tests some check's results on a clean install of Drupal. + */ + public function testDefaultResults() { + $defaults = [ + 'security_review-field' => CheckResult::SUCCESS, + ]; + + foreach ($this->checks as $check) { + if (array_key_exists($check->id(), $defaults)) { + $result = $check->run(); + $this->assertEqual($result->result(), $defaults[$check->id()], $check->getTitle() . ' produced the right result.'); + } + } + } + + /** + * Tests the storing of a check result on every test check. + */ + public function testStoreResult() { + foreach ($this->testChecks as $check) { + // Run the check and store its result. + $result = $check->run(); + $check->storeResult($result); + + // Compare lastResult() with $result. + $last_result = $check->lastResult(TRUE); + $this->assertEqual($result->result(), $last_result->result(), 'Result stored.'); + $this->assertEqual($result->time(), $last_result->time(), 'Time stored.'); + if ($check->storesFindings()) { + // If storesFindings() is set to FALSE, then these could differ. + $this->assertEqual($result->findings(), $last_result->findings(), 'Findings stored.'); + } + } + } + + /** + * Tests stored result correction on lastResult() call. + * + * Tests the case when the check doesn't store its findings, and the new + * result that lastResult() returns overwrites the old one if the result + * integer is not the same. + */ + public function testLastResultUpdate() { + foreach ($this->testChecks as $check) { + if (!$check->storesFindings()) { + // Get the real result. + $result = $check->run(); + + // Build the fake result. + $new_result_result = $result->result() == CheckResult::SUCCESS ? CheckResult::FAIL : CheckResult::SUCCESS; + $new_result = new CheckResult( + $check, + $new_result_result, + [], + TRUE + ); + + // Store it. + $check->storeResult($new_result); + + // Check if lastResult()'s result integer is the same as $result's. + $last_result = $check->lastResult(TRUE); + $this->assertEqual($last_result->result(), $result->result(), 'Invalid result got updated.'); + } + } + } + +}