3 namespace Drupal\Tests\system\Kernel\System;
5 use Drupal\Core\Flood\DatabaseBackend;
6 use Drupal\Core\Flood\MemoryBackend;
7 use Drupal\KernelTests\KernelTestBase;
10 * Functional tests for the flood control mechanism.
14 class FloodTest extends KernelTestBase {
19 protected static $modules = ['system'];
22 * Test flood control mechanism clean-up.
24 public function testCleanUp() {
27 $name = 'flood_test_cleanup';
28 $cron = $this->container->get('cron');
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.
38 $this->assertTrue($flood->isAllowed($name, $threshold));
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.
46 $this->assertFalse($flood->isAllowed($name, $threshold));
50 * Test flood control memory backend.
52 public function testMemoryBackend() {
55 $name = 'flood_test_cleanup';
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));
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));
78 * Test flood control database backend.
80 public function testDatabaseBackend() {
83 $name = 'flood_test_cleanup';
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));
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));