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 // Register expired event.
38 $flood = \Drupal::flood();
39 $flood->register($name, $window_expired);
40 // Verify event is not allowed.
41 $this->assertFalse($flood->isAllowed($name, $threshold));
42 // Run cron and verify event is now allowed.
44 $this->assertTrue($flood->isAllowed($name, $threshold));
46 // Register unexpired event.
47 $flood->register($name);
48 // Verify event is not allowed.
49 $this->assertFalse($flood->isAllowed($name, $threshold));
50 // Run cron and verify event is still not allowed.
52 $this->assertFalse($flood->isAllowed($name, $threshold));
56 * Test flood control memory backend.
58 public function testMemoryBackend() {
61 $name = 'flood_test_cleanup';
63 $request_stack = \Drupal::service('request_stack');
64 $flood = new MemoryBackend($request_stack);
65 // Register expired event.
66 $flood->register($name, $window_expired);
67 // Verify event is not allowed.
68 $this->assertFalse($flood->isAllowed($name, $threshold));
69 // Run cron and verify event is now allowed.
70 $flood->garbageCollection();
71 $this->assertTrue($flood->isAllowed($name, $threshold));
73 // Register unexpired event.
74 $flood->register($name);
75 // Verify event is not allowed.
76 $this->assertFalse($flood->isAllowed($name, $threshold));
77 // Run cron and verify event is still not allowed.
78 $flood->garbageCollection();
79 $this->assertFalse($flood->isAllowed($name, $threshold));
83 * Test flood control database backend.
85 public function testDatabaseBackend() {
88 $name = 'flood_test_cleanup';
90 $connection = \Drupal::service('database');
91 $request_stack = \Drupal::service('request_stack');
92 $flood = new DatabaseBackend($connection, $request_stack);
93 // Register expired event.
94 $flood->register($name, $window_expired);
95 // Verify event is not allowed.
96 $this->assertFalse($flood->isAllowed($name, $threshold));
97 // Run cron and verify event is now allowed.
98 $flood->garbageCollection();
99 $this->assertTrue($flood->isAllowed($name, $threshold));
101 // Register unexpired event.
102 $flood->register($name);
103 // Verify event is not allowed.
104 $this->assertFalse($flood->isAllowed($name, $threshold));
105 // Run cron and verify event is still not allowed.
106 $flood->garbageCollection();
107 $this->assertFalse($flood->isAllowed($name, $threshold));