FloodTest.php in Drupal 8
File
core/modules/system/tests/src/Kernel/System/FloodTest.php
View source
<?php
namespace Drupal\Tests\system\Kernel\System;
use Drupal\Core\Flood\DatabaseBackend;
use Drupal\Core\Flood\MemoryBackend;
use Drupal\KernelTests\KernelTestBase;
class FloodTest extends KernelTestBase {
protected static $modules = [
'system',
];
public function testCleanUp() {
$threshold = 1;
$window_expired = -1;
$name = 'flood_test_cleanup';
$cron = $this->container
->get('cron');
$flood = \Drupal::flood();
$this
->assertTrue($flood
->isAllowed($name, $threshold));
$flood
->register($name, $window_expired);
$this
->assertFalse($flood
->isAllowed($name, $threshold));
$cron
->run();
$this
->assertTrue($flood
->isAllowed($name, $threshold));
$flood
->register($name);
$this
->assertFalse($flood
->isAllowed($name, $threshold));
$cron
->run();
$this
->assertFalse($flood
->isAllowed($name, $threshold));
}
public function testMemoryBackend() {
$threshold = 1;
$window_expired = -1;
$name = 'flood_test_cleanup';
$request_stack = \Drupal::service('request_stack');
$flood = new MemoryBackend($request_stack);
$this
->assertTrue($flood
->isAllowed($name, $threshold));
$flood
->register($name, $window_expired);
$this
->assertFalse($flood
->isAllowed($name, $threshold));
$flood
->garbageCollection();
$this
->assertTrue($flood
->isAllowed($name, $threshold));
$flood
->register($name);
$this
->assertFalse($flood
->isAllowed($name, $threshold));
$flood
->garbageCollection();
$this
->assertFalse($flood
->isAllowed($name, $threshold));
}
public function testDatabaseBackend() {
$threshold = 1;
$window_expired = -1;
$name = 'flood_test_cleanup';
$connection = \Drupal::service('database');
$request_stack = \Drupal::service('request_stack');
$flood = new DatabaseBackend($connection, $request_stack);
$this
->assertTrue($flood
->isAllowed($name, $threshold));
$flood
->register($name, $window_expired);
$this
->assertFalse($flood
->isAllowed($name, $threshold));
$flood
->garbageCollection();
$this
->assertTrue($flood
->isAllowed($name, $threshold));
$flood
->register($name);
$this
->assertFalse($flood
->isAllowed($name, $threshold));
$flood
->garbageCollection();
$this
->assertFalse($flood
->isAllowed($name, $threshold));
}
}
Classes
Name |
Description |
FloodTest |
Functional tests for the flood control mechanism. |