a3a336831484124816ff677d17bc953bc266e67a
[yaffs-website] / web / core / modules / system / src / Tests / System / FloodTest.php
1 <?php
2
3 namespace Drupal\system\Tests\System;
4
5 use Drupal\Core\Flood\DatabaseBackend;
6 use Drupal\Core\Flood\MemoryBackend;
7 use Drupal\simpletest\WebTestBase;
8 use Symfony\Component\HttpFoundation\Request;
9
10 /**
11  * Functional tests for the flood control mechanism.
12  *
13  * @group system
14  */
15 class FloodTest extends WebTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   protected function setUp() {
21     parent::setUp();
22
23     // Flood backends need a request object. Create a dummy one and insert it
24     // to the container.
25     $request = Request::createFromGlobals();
26     $this->container->get('request_stack')->push($request);
27   }
28
29   /**
30    * Test flood control mechanism clean-up.
31    */
32   public function testCleanUp() {
33     $threshold = 1;
34     $window_expired = -1;
35     $name = 'flood_test_cleanup';
36
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.
43     $this->cronRun();
44     $this->assertTrue($flood->isAllowed($name, $threshold));
45
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.
51     $this->cronRun();
52     $this->assertFalse($flood->isAllowed($name, $threshold));
53   }
54
55   /**
56    * Test flood control memory backend.
57    */
58   public function testMemoryBackend() {
59     $threshold = 1;
60     $window_expired = -1;
61     $name = 'flood_test_cleanup';
62
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));
72
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));
80   }
81
82   /**
83    * Test flood control database backend.
84    */
85   public function testDatabaseBackend() {
86     $threshold = 1;
87     $window_expired = -1;
88     $name = 'flood_test_cleanup';
89
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));
100
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));
108   }
109
110 }