Chris@0: container->get('request_stack')->push($request); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test flood control mechanism clean-up. Chris@0: */ Chris@0: public function testCleanUp() { Chris@0: $threshold = 1; Chris@0: $window_expired = -1; Chris@0: $name = 'flood_test_cleanup'; Chris@0: Chris@0: $flood = \Drupal::flood(); Chris@0: $this->assertTrue($flood->isAllowed($name, $threshold)); Chris@0: // Register expired event. Chris@0: $flood->register($name, $window_expired); Chris@0: // Verify event is not allowed. Chris@0: $this->assertFalse($flood->isAllowed($name, $threshold)); Chris@0: // Run cron and verify event is now allowed. Chris@0: $this->cronRun(); Chris@0: $this->assertTrue($flood->isAllowed($name, $threshold)); Chris@0: Chris@0: // Register unexpired event. Chris@0: $flood->register($name); Chris@0: // Verify event is not allowed. Chris@0: $this->assertFalse($flood->isAllowed($name, $threshold)); Chris@0: // Run cron and verify event is still not allowed. Chris@0: $this->cronRun(); Chris@0: $this->assertFalse($flood->isAllowed($name, $threshold)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test flood control memory backend. Chris@0: */ Chris@0: public function testMemoryBackend() { Chris@0: $threshold = 1; Chris@0: $window_expired = -1; Chris@0: $name = 'flood_test_cleanup'; Chris@0: Chris@0: $request_stack = \Drupal::service('request_stack'); Chris@0: $flood = new MemoryBackend($request_stack); Chris@0: $this->assertTrue($flood->isAllowed($name, $threshold)); Chris@0: // Register expired event. Chris@0: $flood->register($name, $window_expired); Chris@0: // Verify event is not allowed. Chris@0: $this->assertFalse($flood->isAllowed($name, $threshold)); Chris@0: // Run cron and verify event is now allowed. Chris@0: $flood->garbageCollection(); Chris@0: $this->assertTrue($flood->isAllowed($name, $threshold)); Chris@0: Chris@0: // Register unexpired event. Chris@0: $flood->register($name); Chris@0: // Verify event is not allowed. Chris@0: $this->assertFalse($flood->isAllowed($name, $threshold)); Chris@0: // Run cron and verify event is still not allowed. Chris@0: $flood->garbageCollection(); Chris@0: $this->assertFalse($flood->isAllowed($name, $threshold)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test flood control database backend. Chris@0: */ Chris@0: public function testDatabaseBackend() { Chris@0: $threshold = 1; Chris@0: $window_expired = -1; Chris@0: $name = 'flood_test_cleanup'; Chris@0: Chris@0: $connection = \Drupal::service('database'); Chris@0: $request_stack = \Drupal::service('request_stack'); Chris@0: $flood = new DatabaseBackend($connection, $request_stack); Chris@0: $this->assertTrue($flood->isAllowed($name, $threshold)); Chris@0: // Register expired event. Chris@0: $flood->register($name, $window_expired); Chris@0: // Verify event is not allowed. Chris@0: $this->assertFalse($flood->isAllowed($name, $threshold)); Chris@0: // Run cron and verify event is now allowed. Chris@0: $flood->garbageCollection(); Chris@0: $this->assertTrue($flood->isAllowed($name, $threshold)); Chris@0: Chris@0: // Register unexpired event. Chris@0: $flood->register($name); Chris@0: // Verify event is not allowed. Chris@0: $this->assertFalse($flood->isAllowed($name, $threshold)); Chris@0: // Run cron and verify event is still not allowed. Chris@0: $flood->garbageCollection(); Chris@0: $this->assertFalse($flood->isAllowed($name, $threshold)); Chris@0: } Chris@0: Chris@0: }