3 namespace Drupal\system\Tests\System;
5 use Drupal\Core\Flood\DatabaseBackend;
6 use Drupal\Core\Flood\MemoryBackend;
7 use Drupal\simpletest\WebTestBase;
8 use Symfony\Component\HttpFoundation\Request;
11 * Functional tests for the flood control mechanism.
15 class FloodTest extends WebTestBase {
20 protected function setUp() {
23 // Flood backends need a request object. Create a dummy one and insert it
25 $request = Request::createFromGlobals();
26 $this->container->get('request_stack')->push($request);
30 * Test flood control mechanism clean-up.
32 public function testCleanUp() {
35 $name = 'flood_test_cleanup';
37 $flood = \Drupal::flood();
38 $this->assertTrue($flood->isAllowed($name, $threshold));
39 // Register expired event.
40 $flood->register($name, $window_expired);
41 // Verify event is not allowed.
42 $this->assertFalse($flood->isAllowed($name, $threshold));
43 // Run cron and verify event is now allowed.
45 $this->assertTrue($flood->isAllowed($name, $threshold));
47 // Register unexpired event.
48 $flood->register($name);
49 // Verify event is not allowed.
50 $this->assertFalse($flood->isAllowed($name, $threshold));
51 // Run cron and verify event is still not allowed.
53 $this->assertFalse($flood->isAllowed($name, $threshold));
57 * Test flood control memory backend.
59 public function testMemoryBackend() {
62 $name = 'flood_test_cleanup';
64 $request_stack = \Drupal::service('request_stack');
65 $flood = new MemoryBackend($request_stack);
66 $this->assertTrue($flood->isAllowed($name, $threshold));
67 // Register expired event.
68 $flood->register($name, $window_expired);
69 // Verify event is not allowed.
70 $this->assertFalse($flood->isAllowed($name, $threshold));
71 // Run cron and verify event is now allowed.
72 $flood->garbageCollection();
73 $this->assertTrue($flood->isAllowed($name, $threshold));
75 // Register unexpired event.
76 $flood->register($name);
77 // Verify event is not allowed.
78 $this->assertFalse($flood->isAllowed($name, $threshold));
79 // Run cron and verify event is still not allowed.
80 $flood->garbageCollection();
81 $this->assertFalse($flood->isAllowed($name, $threshold));
85 * Test flood control database backend.
87 public function testDatabaseBackend() {
90 $name = 'flood_test_cleanup';
92 $connection = \Drupal::service('database');
93 $request_stack = \Drupal::service('request_stack');
94 $flood = new DatabaseBackend($connection, $request_stack);
95 $this->assertTrue($flood->isAllowed($name, $threshold));
96 // Register expired event.
97 $flood->register($name, $window_expired);
98 // Verify event is not allowed.
99 $this->assertFalse($flood->isAllowed($name, $threshold));
100 // Run cron and verify event is now allowed.
101 $flood->garbageCollection();
102 $this->assertTrue($flood->isAllowed($name, $threshold));
104 // Register unexpired event.
105 $flood->register($name);
106 // Verify event is not allowed.
107 $this->assertFalse($flood->isAllowed($name, $threshold));
108 // Run cron and verify event is still not allowed.
109 $flood->garbageCollection();
110 $this->assertFalse($flood->isAllowed($name, $threshold));