You are here

public function ContactPersonalTest::testPersonalContactFlood in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/contact/tests/src/Functional/ContactPersonalTest.php \Drupal\Tests\contact\Functional\ContactPersonalTest::testPersonalContactFlood()

Tests the personal contact form flood protection.

File

core/modules/contact/tests/src/Functional/ContactPersonalTest.php, line 260

Class

ContactPersonalTest
Tests personal contact form functionality.

Namespace

Drupal\Tests\contact\Functional

Code

public function testPersonalContactFlood() {
  $flood_limit = 3;
  $this
    ->config('contact.settings')
    ->set('flood.limit', $flood_limit)
    ->save();
  $this
    ->drupalLogin($this->webUser);

  // Submit contact form with correct values and check flood interval.
  for ($i = 0; $i < $flood_limit; $i++) {
    $this
      ->submitPersonalContact($this->contactUser);
    $this
      ->assertSession()
      ->pageTextContains('Your message has been sent.');
  }

  // Submit contact form one over limit.
  $this
    ->submitPersonalContact($this->contactUser);

  // Normal user should be denied access to flooded contact form.
  $interval = \Drupal::service('date.formatter')
    ->formatInterval($this
    ->config('contact.settings')
    ->get('flood.interval'));
  $this
    ->assertSession()
    ->pageTextContains("You cannot send more than 3 messages in {$interval}. Try again later.");

  // Test that the admin user can still access the contact form even though
  // the flood limit was reached.
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->assertSession()
    ->pageTextNotContains('Try again later.');
}