Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / src / Kernel / System / FloodTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Kernel\System;
4
5 use Drupal\Core\Flood\DatabaseBackend;
6 use Drupal\Core\Flood\MemoryBackend;
7 use Drupal\KernelTests\KernelTestBase;
8
9 /**
10  * Functional tests for the flood control mechanism.
11  *
12  * @group system
13  */
14 class FloodTest extends KernelTestBase {
15
16   /**
17    * {@inheritdoc}
18    */
19   protected static $modules = ['system'];
20
21   /**
22    * Test flood control mechanism clean-up.
23    */
24   public function testCleanUp() {
25     $threshold = 1;
26     $window_expired = -1;
27     $name = 'flood_test_cleanup';
28     $cron = $this->container->get('cron');
29
30     $flood = \Drupal::flood();
31     $this->assertTrue($flood->isAllowed($name, $threshold));
32     // Register expired event.
33     $flood->register($name, $window_expired);
34     // Verify event is not allowed.
35     $this->assertFalse($flood->isAllowed($name, $threshold));
36     // Run cron and verify event is now allowed.
37     $cron->run();
38     $this->assertTrue($flood->isAllowed($name, $threshold));
39
40     // Register unexpired event.
41     $flood->register($name);
42     // Verify event is not allowed.
43     $this->assertFalse($flood->isAllowed($name, $threshold));
44     // Run cron and verify event is still not allowed.
45     $cron->run();
46     $this->assertFalse($flood->isAllowed($name, $threshold));
47   }
48
49   /**
50    * Test flood control memory backend.
51    */
52   public function testMemoryBackend() {
53     $threshold = 1;
54     $window_expired = -1;
55     $name = 'flood_test_cleanup';
56
57     $request_stack = \Drupal::service('request_stack');
58     $flood = new MemoryBackend($request_stack);
59     $this->assertTrue($flood->isAllowed($name, $threshold));
60     // Register expired event.
61     $flood->register($name, $window_expired);
62     // Verify event is not allowed.
63     $this->assertFalse($flood->isAllowed($name, $threshold));
64     // Run cron and verify event is now allowed.
65     $flood->garbageCollection();
66     $this->assertTrue($flood->isAllowed($name, $threshold));
67
68     // Register unexpired event.
69     $flood->register($name);
70     // Verify event is not allowed.
71     $this->assertFalse($flood->isAllowed($name, $threshold));
72     // Run cron and verify event is still not allowed.
73     $flood->garbageCollection();
74     $this->assertFalse($flood->isAllowed($name, $threshold));
75   }
76
77   /**
78    * Test flood control database backend.
79    */
80   public function testDatabaseBackend() {
81     $threshold = 1;
82     $window_expired = -1;
83     $name = 'flood_test_cleanup';
84
85     $connection = \Drupal::service('database');
86     $request_stack = \Drupal::service('request_stack');
87     $flood = new DatabaseBackend($connection, $request_stack);
88     $this->assertTrue($flood->isAllowed($name, $threshold));
89     // Register expired event.
90     $flood->register($name, $window_expired);
91     // Verify event is not allowed.
92     $this->assertFalse($flood->isAllowed($name, $threshold));
93     // Run cron and verify event is now allowed.
94     $flood->garbageCollection();
95     $this->assertTrue($flood->isAllowed($name, $threshold));
96
97     // Register unexpired event.
98     $flood->register($name);
99     // Verify event is not allowed.
100     $this->assertFalse($flood->isAllowed($name, $threshold));
101     // Run cron and verify event is still not allowed.
102     $flood->garbageCollection();
103     $this->assertFalse($flood->isAllowed($name, $threshold));
104   }
105
106 }