More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / modules / contrib / security_review / tests / src / Kernel / CheckTest.php
1 <?php
2
3 namespace Drupal\Tests\security_review\Kernel;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\security_review\CheckResult;
7
8 /**
9  * Contains tests for Checks.
10  *
11  * @group security_review
12  */
13 class CheckTest extends KernelTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['security_review', 'security_review_test'];
21
22   /**
23    * The security checks defined by Security Review and Security Review Test.
24    *
25    * @var \Drupal\security_review\Check[]
26    */
27   protected $checks;
28
29   /**
30    * The security checks defined by Security Review.
31    *
32    * @var \Drupal\security_review\Check[]
33    */
34   protected $realChecks;
35
36   /**
37    * The security checks defined by Security Review Test.
38    *
39    * @var \Drupal\security_review\Check[]
40    */
41   protected $testChecks;
42
43   /**
44    * Sets up the environment, populates the $checks variable.
45    */
46   protected function setUp() {
47     parent::setUp();
48     $this->realChecks = security_review_security_review_checks();
49     $this->testChecks = security_review_test_security_review_checks();
50     $this->checks = array_merge($this->realChecks, $this->testChecks);
51   }
52
53   /**
54    * Tests whether $checks is empty.
55    */
56   public function testChecksExist() {
57     $this->assertFalse(empty($this->checks), 'Checks found.');
58   }
59
60   /**
61    * Every check should be enabled by default.
62    */
63   public function testEnabledByDefault() {
64     foreach ($this->checks as $check) {
65       $this->assertFalse($check->isSkipped(), $check->getTitle() . ' is enabled by default.');
66     }
67   }
68
69   /**
70    * Tests some check's results on a clean install of Drupal.
71    */
72   public function testDefaultResults() {
73     $defaults = [
74       'security_review-field' => CheckResult::SUCCESS,
75     ];
76
77     foreach ($this->checks as $check) {
78       if (array_key_exists($check->id(), $defaults)) {
79         $result = $check->run();
80         $this->assertEquals($defaults[$check->id()], $result->result(), $check->getTitle() . ' produced the right result.');
81       }
82     }
83   }
84
85   /**
86    * Tests the storing of a check result on every test check.
87    */
88   public function testStoreResult() {
89     foreach ($this->testChecks as $check) {
90       // Run the check and store its result.
91       $result = $check->run();
92       $check->storeResult($result);
93
94       // Compare lastResult() with $result.
95       $last_result = $check->lastResult(TRUE);
96       $this->assertEquals($result->result(), $last_result->result(), 'Result stored.');
97       $this->assertEquals($result->time(), $last_result->time(), 'Time stored.');
98       if ($check->storesFindings()) {
99         // If storesFindings() is set to FALSE, then these could differ.
100         $this->assertEquals($result->findings(), $last_result->findings(), 'Findings stored.');
101       }
102     }
103   }
104
105   /**
106    * Tests stored result correction on lastResult() call.
107    *
108    * Tests the case when the check doesn't store its findings, and the new
109    * result that lastResult() returns overwrites the old one if the result
110    * integer is not the same.
111    */
112   public function testLastResultUpdate() {
113     foreach ($this->testChecks as $check) {
114       if (!$check->storesFindings()) {
115         // Get the real result.
116         $result = $check->run();
117
118         // Build the fake result.
119         $new_result_result = $result->result() == CheckResult::SUCCESS ? CheckResult::FAIL : CheckResult::SUCCESS;
120         $new_result = new CheckResult(
121           $check,
122           $new_result_result,
123           [],
124           TRUE
125         );
126
127         // Store it.
128         $check->storeResult($new_result);
129
130         // Check if lastResult()'s result integer is the same as $result's.
131         $last_result = $check->lastResult(TRUE);
132         $this->assertEquals($result->result(), $last_result->result(), 'Invalid result got updated.');
133       }
134     }
135   }
136
137 }