Mercurial > hg > isophonics-drupal-site
comparison core/modules/system/src/Tests/System/FloodTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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 $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. | |
44 $this->cronRun(); | |
45 $this->assertTrue($flood->isAllowed($name, $threshold)); | |
46 | |
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. | |
52 $this->cronRun(); | |
53 $this->assertFalse($flood->isAllowed($name, $threshold)); | |
54 } | |
55 | |
56 /** | |
57 * Test flood control memory backend. | |
58 */ | |
59 public function testMemoryBackend() { | |
60 $threshold = 1; | |
61 $window_expired = -1; | |
62 $name = 'flood_test_cleanup'; | |
63 | |
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)); | |
74 | |
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)); | |
82 } | |
83 | |
84 /** | |
85 * Test flood control database backend. | |
86 */ | |
87 public function testDatabaseBackend() { | |
88 $threshold = 1; | |
89 $window_expired = -1; | |
90 $name = 'flood_test_cleanup'; | |
91 | |
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)); | |
103 | |
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)); | |
111 } | |
112 | |
113 } |