3 namespace Drupal\Tests\security_review\Kernel;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\security_review\CheckResult;
9 * Contains tests for Checks.
11 * @group security_review
13 class CheckTest extends KernelTestBase {
20 public static $modules = ['security_review', 'security_review_test'];
23 * The security checks defined by Security Review and Security Review Test.
25 * @var \Drupal\security_review\Check[]
30 * The security checks defined by Security Review.
32 * @var \Drupal\security_review\Check[]
34 protected $realChecks;
37 * The security checks defined by Security Review Test.
39 * @var \Drupal\security_review\Check[]
41 protected $testChecks;
44 * Sets up the environment, populates the $checks variable.
46 protected function 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);
54 * Tests whether $checks is empty.
56 public function testChecksExist() {
57 $this->assertFalse(empty($this->checks), 'Checks found.');
61 * Every check should be enabled by default.
63 public function testEnabledByDefault() {
64 foreach ($this->checks as $check) {
65 $this->assertFalse($check->isSkipped(), $check->getTitle() . ' is enabled by default.');
70 * Tests some check's results on a clean install of Drupal.
72 public function testDefaultResults() {
74 'security_review-field' => CheckResult::SUCCESS,
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.');
86 * Tests the storing of a check result on every test check.
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);
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.');
106 * Tests stored result correction on lastResult() call.
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.
112 public function testLastResultUpdate() {
113 foreach ($this->testChecks as $check) {
114 if (!$check->storesFindings()) {
115 // Get the real result.
116 $result = $check->run();
118 // Build the fake result.
119 $new_result_result = $result->result() == CheckResult::SUCCESS ? CheckResult::FAIL : CheckResult::SUCCESS;
120 $new_result = new CheckResult(
128 $check->storeResult($new_result);
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.');