Including security review as a submodule - with patched for Yaffs.
[yaffs-website] / web / modules / contrib / security_review / src / Tests / ChecklistTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\security_review\Tests\ChecklistTest.
6  */
7
8 namespace Drupal\security_review\Tests;
9
10 use Drupal\security_review\Checklist;
11 use Drupal\simpletest\KernelTestBase;
12
13 /**
14  * Contains test for Checklist.
15  *
16  * @group security_review
17  */
18 class ChecklistTest 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_review.checklist service.
29    *
30    * @var \Drupal\security_review\Checklist
31    */
32   protected $checklist;
33
34   /**
35    * The security checks defined by Security Review and Security Review Test.
36    *
37    * @var \Drupal\security_review\Check[]
38    */
39   protected $checks;
40
41   /**
42    * The security checks defined by Security Review.
43    *
44    * @var \Drupal\security_review\Check[]
45    */
46   protected $realChecks;
47
48   /**
49    * The security checks defined by Security Review Test.
50    *
51    * @var \Drupal\security_review\Check[]
52    */
53   protected $testChecks;
54
55   /**
56    * Array of the IDs of $checks.
57    *
58    * @var array
59    */
60   protected $checkIDs;
61
62   /**
63    * Sets up the environment, populates the $checks variable.
64    */
65   protected function setUp() {
66     parent::setUp();
67
68     $this->checklist = \Drupal::getContainer()
69       ->get('security_review.checklist');
70     $this->realChecks = security_review_security_review_checks();
71     $this->testChecks = security_review_test_security_review_checks();
72     $this->checks = array_merge($this->realChecks, $this->testChecks);
73
74     Checklist::clearCache();
75     $this->checkIDs = [];
76     foreach ($this->checks as $check) {
77       $this->checkIDs[] = $check->id();
78     }
79   }
80
81   /**
82    * Tests Checklist::getChecks().
83    *
84    * Tests whether getChecks() contains all the checks that
85    * security_review_security_review_checks() and
86    * security_review_test_security_review_checks() returns.
87    */
88   public function testChecksProvided() {
89     foreach ($this->checklist->getChecks() as $check) {
90       $this->assertTrue(in_array($check->id(), $this->checkIDs), $check->getTitle() . ' found.');
91     }
92   }
93
94   /**
95    * Tests whether checks returned by getEnabledChecks() are all enabled.
96    */
97   public function testEnabledChecks() {
98     foreach ($this->checklist->getEnabledChecks() as $check) {
99       $this->assertFalse($check->isSkipped(), $check->getTitle() . ' is enabled.');
100
101       // Disable check.
102       $check->skip();
103     }
104     Checklist::clearCache();
105     $this->assertEqual(count($this->checklist->getEnabledChecks()), 0, 'Disabled all checks.');
106   }
107
108   /**
109    * Tests Checklist's Check search functions.
110    *
111    * Tests the search functions of Checklist:
112    *   getCheck().
113    *   getCheckById().
114    */
115   public function testCheckSearch() {
116     foreach ($this->checklist->getChecks() as $check) {
117       // getCheck().
118       $found = $this->checklist->getCheck($check->getMachineNamespace(), $check->getMachineTitle());
119       $this->assertNotNull($found, 'Found a check.');
120       $this->assertEqual($check->id(), $found->id(), 'Found ' . $check->getTitle() . '.');
121
122       // getCheckById().
123       $found = $this->checklist->getCheckById($check->id());
124       $this->assertNotNull($found, 'Found a check.');
125       $this->assertEqual($check->id(), $found->id(), 'Found ' . $check->getTitle() . '.');
126     }
127   }
128
129 }