CheckTest.php in Security Review 8
File
tests/src/Kernel/CheckTest.php
View source
<?php
namespace Drupal\Tests\security_review\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\security_review\CheckResult;
class CheckTest extends KernelTestBase {
public static $modules = [
'security_review',
'security_review_test',
];
protected $checks;
protected $realChecks;
protected $testChecks;
protected function setUp() {
parent::setUp();
$this->realChecks = security_review_security_review_checks();
$this->testChecks = security_review_test_security_review_checks();
$this->checks = array_merge($this->realChecks, $this->testChecks);
}
public function testChecksExist() {
$this
->assertFalse(empty($this->checks), 'Checks found.');
}
public function testEnabledByDefault() {
foreach ($this->checks as $check) {
$this
->assertFalse($check
->isSkipped(), $check
->getTitle() . ' is enabled by default.');
}
}
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
->assertEquals($defaults[$check
->id()], $result
->result(), $check
->getTitle() . ' produced the right result.');
}
}
}
public function testStoreResult() {
foreach ($this->testChecks as $check) {
$result = $check
->run();
$check
->storeResult($result);
$last_result = $check
->lastResult(TRUE);
$this
->assertEquals($result
->result(), $last_result
->result(), 'Result stored.');
$this
->assertEquals($result
->time(), $last_result
->time(), 'Time stored.');
if ($check
->storesFindings()) {
$this
->assertEquals($result
->findings(), $last_result
->findings(), 'Findings stored.');
}
}
}
public function testLastResultUpdate() {
foreach ($this->testChecks as $check) {
if (!$check
->storesFindings()) {
$result = $check
->run();
$new_result_result = $result
->result() == CheckResult::SUCCESS ? CheckResult::FAIL : CheckResult::SUCCESS;
$new_result = new CheckResult($check, $new_result_result, [], TRUE);
$check
->storeResult($new_result);
$last_result = $check
->lastResult(TRUE);
$this
->assertEquals($result
->result(), $last_result
->result(), 'Invalid result got updated.');
}
}
}
}