You are here

public function FloodTest::testDatabaseBackend in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/System/FloodTest.php \Drupal\system\Tests\System\FloodTest::testDatabaseBackend()

Test flood control database backend.

File

core/modules/system/src/Tests/System/FloodTest.php, line 88
Contains \Drupal\system\Tests\System\FloodTest.

Class

FloodTest
Functional tests for the flood control mechanism.

Namespace

Drupal\system\Tests\System

Code

public function testDatabaseBackend() {
  $threshold = 1;
  $window_expired = -1;
  $name = 'flood_test_cleanup';
  $connection = \Drupal::service('database');
  $request_stack = \Drupal::service('request_stack');
  $flood = new \Drupal\Core\Flood\DatabaseBackend($connection, $request_stack);

  // 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));
}