annotate 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
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\system\Tests\System;
Chris@0 4
Chris@0 5 use Drupal\Core\Flood\DatabaseBackend;
Chris@0 6 use Drupal\Core\Flood\MemoryBackend;
Chris@0 7 use Drupal\simpletest\WebTestBase;
Chris@0 8 use Symfony\Component\HttpFoundation\Request;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Functional tests for the flood control mechanism.
Chris@0 12 *
Chris@0 13 * @group system
Chris@0 14 */
Chris@0 15 class FloodTest extends WebTestBase {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * {@inheritdoc}
Chris@0 19 */
Chris@0 20 protected function setUp() {
Chris@0 21 parent::setUp();
Chris@0 22
Chris@0 23 // Flood backends need a request object. Create a dummy one and insert it
Chris@0 24 // to the container.
Chris@0 25 $request = Request::createFromGlobals();
Chris@0 26 $this->container->get('request_stack')->push($request);
Chris@0 27 }
Chris@0 28
Chris@0 29 /**
Chris@0 30 * Test flood control mechanism clean-up.
Chris@0 31 */
Chris@0 32 public function testCleanUp() {
Chris@0 33 $threshold = 1;
Chris@0 34 $window_expired = -1;
Chris@0 35 $name = 'flood_test_cleanup';
Chris@0 36
Chris@0 37 $flood = \Drupal::flood();
Chris@0 38 $this->assertTrue($flood->isAllowed($name, $threshold));
Chris@0 39 // Register expired event.
Chris@0 40 $flood->register($name, $window_expired);
Chris@0 41 // Verify event is not allowed.
Chris@0 42 $this->assertFalse($flood->isAllowed($name, $threshold));
Chris@0 43 // Run cron and verify event is now allowed.
Chris@0 44 $this->cronRun();
Chris@0 45 $this->assertTrue($flood->isAllowed($name, $threshold));
Chris@0 46
Chris@0 47 // Register unexpired event.
Chris@0 48 $flood->register($name);
Chris@0 49 // Verify event is not allowed.
Chris@0 50 $this->assertFalse($flood->isAllowed($name, $threshold));
Chris@0 51 // Run cron and verify event is still not allowed.
Chris@0 52 $this->cronRun();
Chris@0 53 $this->assertFalse($flood->isAllowed($name, $threshold));
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * Test flood control memory backend.
Chris@0 58 */
Chris@0 59 public function testMemoryBackend() {
Chris@0 60 $threshold = 1;
Chris@0 61 $window_expired = -1;
Chris@0 62 $name = 'flood_test_cleanup';
Chris@0 63
Chris@0 64 $request_stack = \Drupal::service('request_stack');
Chris@0 65 $flood = new MemoryBackend($request_stack);
Chris@0 66 $this->assertTrue($flood->isAllowed($name, $threshold));
Chris@0 67 // Register expired event.
Chris@0 68 $flood->register($name, $window_expired);
Chris@0 69 // Verify event is not allowed.
Chris@0 70 $this->assertFalse($flood->isAllowed($name, $threshold));
Chris@0 71 // Run cron and verify event is now allowed.
Chris@0 72 $flood->garbageCollection();
Chris@0 73 $this->assertTrue($flood->isAllowed($name, $threshold));
Chris@0 74
Chris@0 75 // Register unexpired event.
Chris@0 76 $flood->register($name);
Chris@0 77 // Verify event is not allowed.
Chris@0 78 $this->assertFalse($flood->isAllowed($name, $threshold));
Chris@0 79 // Run cron and verify event is still not allowed.
Chris@0 80 $flood->garbageCollection();
Chris@0 81 $this->assertFalse($flood->isAllowed($name, $threshold));
Chris@0 82 }
Chris@0 83
Chris@0 84 /**
Chris@0 85 * Test flood control database backend.
Chris@0 86 */
Chris@0 87 public function testDatabaseBackend() {
Chris@0 88 $threshold = 1;
Chris@0 89 $window_expired = -1;
Chris@0 90 $name = 'flood_test_cleanup';
Chris@0 91
Chris@0 92 $connection = \Drupal::service('database');
Chris@0 93 $request_stack = \Drupal::service('request_stack');
Chris@0 94 $flood = new DatabaseBackend($connection, $request_stack);
Chris@0 95 $this->assertTrue($flood->isAllowed($name, $threshold));
Chris@0 96 // Register expired event.
Chris@0 97 $flood->register($name, $window_expired);
Chris@0 98 // Verify event is not allowed.
Chris@0 99 $this->assertFalse($flood->isAllowed($name, $threshold));
Chris@0 100 // Run cron and verify event is now allowed.
Chris@0 101 $flood->garbageCollection();
Chris@0 102 $this->assertTrue($flood->isAllowed($name, $threshold));
Chris@0 103
Chris@0 104 // Register unexpired event.
Chris@0 105 $flood->register($name);
Chris@0 106 // Verify event is not allowed.
Chris@0 107 $this->assertFalse($flood->isAllowed($name, $threshold));
Chris@0 108 // Run cron and verify event is still not allowed.
Chris@0 109 $flood->garbageCollection();
Chris@0 110 $this->assertFalse($flood->isAllowed($name, $threshold));
Chris@0 111 }
Chris@0 112
Chris@0 113 }