5 * Contains \Drupal\security_review\Tests\CheckTest.
8 namespace Drupal\security_review\Tests;
10 use Drupal\security_review\CheckResult;
11 use Drupal\simpletest\KernelTestBase;
14 * Contains tests for Checks.
16 * @group security_review
18 class CheckTest extends KernelTestBase {
25 public static $modules = ['security_review', 'security_review_test'];
28 * The security checks defined by Security Review and Security Review Test.
30 * @var \Drupal\security_review\Check[]
35 * The security checks defined by Security Review.
37 * @var \Drupal\security_review\Check[]
39 protected $realChecks;
42 * The security checks defined by Security Review Test.
44 * @var \Drupal\security_review\Check[]
46 protected $testChecks;
49 * Sets up the environment, populates the $checks variable.
51 protected function setUp() {
53 $this->realChecks = security_review_security_review_checks();
54 $this->testChecks = security_review_test_security_review_checks();
55 $this->checks = array_merge($this->realChecks, $this->testChecks);
59 * Tests whether $checks is empty.
61 protected function testChecksExist() {
62 $this->assertFalse(empty($this->checks), 'Checks found.');
66 * Every check should be enabled by default.
68 public function testEnabledByDefault() {
69 foreach ($this->checks as $check) {
70 $this->assertFalse($check->isSkipped(), $check->getTitle() . ' is enabled by default.');
75 * Tests some check's results on a clean install of Drupal.
77 public function testDefaultResults() {
79 'security_review-field' => CheckResult::SUCCESS,
82 foreach ($this->checks as $check) {
83 if (array_key_exists($check->id(), $defaults)) {
84 $result = $check->run();
85 $this->assertEqual($result->result(), $defaults[$check->id()], $check->getTitle() . ' produced the right result.');
91 * Tests the storing of a check result on every test check.
93 public function testStoreResult() {
94 foreach ($this->testChecks as $check) {
95 // Run the check and store its result.
96 $result = $check->run();
97 $check->storeResult($result);
99 // Compare lastResult() with $result.
100 $last_result = $check->lastResult(TRUE);
101 $this->assertEqual($result->result(), $last_result->result(), 'Result stored.');
102 $this->assertEqual($result->time(), $last_result->time(), 'Time stored.');
103 if ($check->storesFindings()) {
104 // If storesFindings() is set to FALSE, then these could differ.
105 $this->assertEqual($result->findings(), $last_result->findings(), 'Findings stored.');
111 * Tests stored result correction on lastResult() call.
113 * Tests the case when the check doesn't store its findings, and the new
114 * result that lastResult() returns overwrites the old one if the result
115 * integer is not the same.
117 public function testLastResultUpdate() {
118 foreach ($this->testChecks as $check) {
119 if (!$check->storesFindings()) {
120 // Get the real result.
121 $result = $check->run();
123 // Build the fake result.
124 $new_result_result = $result->result() == CheckResult::SUCCESS ? CheckResult::FAIL : CheckResult::SUCCESS;
125 $new_result = new CheckResult(
133 $check->storeResult($new_result);
135 // Check if lastResult()'s result integer is the same as $result's.
136 $last_result = $check->lastResult(TRUE);
137 $this->assertEqual($last_result->result(), $result->result(), 'Invalid result got updated.');