Including security review as a submodule - with patched for Yaffs.
[yaffs-website] / web / modules / contrib / security_review / src / Tests / CheckTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\security_review\Tests\CheckTest.
6  */
7
8 namespace Drupal\security_review\Tests;
9
10 use Drupal\security_review\CheckResult;
11 use Drupal\simpletest\KernelTestBase;
12
13 /**
14  * Contains tests for Checks.
15  *
16  * @group security_review
17  */
18 class CheckTest extends KernelTestBase {
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = ['security_review', 'security_review_test'];
26
27   /**
28    * The security checks defined by Security Review and Security Review Test.
29    *
30    * @var \Drupal\security_review\Check[]
31    */
32   protected $checks;
33
34   /**
35    * The security checks defined by Security Review.
36    *
37    * @var \Drupal\security_review\Check[]
38    */
39   protected $realChecks;
40
41   /**
42    * The security checks defined by Security Review Test.
43    *
44    * @var \Drupal\security_review\Check[]
45    */
46   protected $testChecks;
47
48   /**
49    * Sets up the environment, populates the $checks variable.
50    */
51   protected function setUp() {
52     parent::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);
56   }
57
58   /**
59    * Tests whether $checks is empty.
60    */
61   protected function testChecksExist() {
62     $this->assertFalse(empty($this->checks), 'Checks found.');
63   }
64
65   /**
66    * Every check should be enabled by default.
67    */
68   public function testEnabledByDefault() {
69     foreach ($this->checks as $check) {
70       $this->assertFalse($check->isSkipped(), $check->getTitle() . ' is enabled by default.');
71     }
72   }
73
74   /**
75    * Tests some check's results on a clean install of Drupal.
76    */
77   public function testDefaultResults() {
78     $defaults = [
79       'security_review-field' => CheckResult::SUCCESS,
80     ];
81
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.');
86       }
87     }
88   }
89
90   /**
91    * Tests the storing of a check result on every test check.
92    */
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);
98
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.');
106       }
107     }
108   }
109
110   /**
111    * Tests stored result correction on lastResult() call.
112    *
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.
116    */
117   public function testLastResultUpdate() {
118     foreach ($this->testChecks as $check) {
119       if (!$check->storesFindings()) {
120         // Get the real result.
121         $result = $check->run();
122
123         // Build the fake result.
124         $new_result_result = $result->result() == CheckResult::SUCCESS ? CheckResult::FAIL : CheckResult::SUCCESS;
125         $new_result = new CheckResult(
126           $check,
127           $new_result_result,
128           [],
129           TRUE
130         );
131
132         // Store it.
133         $check->storeResult($new_result);
134
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.');
138       }
139     }
140   }
141
142 }