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