class FloodTest
Same name and namespace in other branches
- 11.x core/modules/system/tests/src/Kernel/System/FloodTest.php \Drupal\Tests\system\Kernel\System\FloodTest
- 10 core/modules/system/tests/src/Kernel/System/FloodTest.php \Drupal\Tests\system\Kernel\System\FloodTest
- 8.9.x core/modules/system/tests/src/Kernel/System/FloodTest.php \Drupal\Tests\system\Kernel\System\FloodTest
Functional tests for the flood control mechanism.
@group system
Hierarchy
- class \Drupal\KernelTests\KernelTestBase implements \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\system\Kernel\System\FloodTest extends \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of FloodTest
File
-
core/
modules/ system/ tests/ src/ Kernel/ System/ FloodTest.php, line 13
Namespace
Drupal\Tests\system\Kernel\SystemView source
class FloodTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
];
/**
* Tests flood control mechanism clean-up.
*/
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));
// Register expired event.
$flood->register($name, $window_expired);
// Verify event is not allowed.
$this->assertFalse($flood->isAllowed($name, $threshold));
// Run cron and verify event is now allowed.
$cron->run();
$this->assertTrue($flood->isAllowed($name, $threshold));
// Register unexpired event.
$flood->register($name);
// Verify event is not allowed.
$this->assertFalse($flood->isAllowed($name, $threshold));
// Run cron and verify event is still not allowed.
$cron->run();
$this->assertFalse($flood->isAllowed($name, $threshold));
}
/**
* Tests flood control database backend.
*/
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));
// Register expired event.
$flood->register($name, $window_expired);
// Verify event is not allowed.
$this->assertFalse($flood->isAllowed($name, $threshold));
// Run cron and verify event is now allowed.
$flood->garbageCollection();
$this->assertTrue($flood->isAllowed($name, $threshold));
// Register unexpired event.
$flood->register($name);
// Verify event is not allowed.
$this->assertFalse($flood->isAllowed($name, $threshold));
// Run cron and verify event is still not allowed.
$flood->garbageCollection();
$this->assertFalse($flood->isAllowed($name, $threshold));
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.