function FloodTest::testMemoryBackend

Test flood control memory backend.

File

drupal/core/modules/system/lib/Drupal/system/Tests/System/FloodTest.php, line 52
Definition of Drupal\system\Tests\System\FloodTest.

Class

FloodTest
Functional tests for the flood control mechanism.

Namespace

Drupal\system\Tests\System

Code

function testMemoryBackend() {
  $threshold = 1;
  $window_expired = -1;
  $name = 'flood_test_cleanup';
  $flood = new \Drupal\Core\Flood\MemoryBackend();

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