You are here

public function CaptchaPersistenceTest::testPersistenceAlways in CAPTCHA 8

Test persistence always.

File

tests/src/Functional/CaptchaPersistenceTest.php, line 70

Class

CaptchaPersistenceTest
Tests CAPTCHA Persistence.

Namespace

Drupal\Tests\captcha\Functional

Code

public function testPersistenceAlways() {

  // Set up of persistence and CAPTCHAs.
  $this
    ->setUpPersistence(CAPTCHA_PERSISTENCE_SHOW_ALWAYS);

  // Go to login form and check if there is a CAPTCHA
  // on the login form (look for the title).
  $this
    ->drupalGet('<front>');
  $this
    ->assertCaptchaPresence(TRUE);
  $captcha_sid_initial = $this
    ->getCaptchaSidFromForm();

  // Try to with wrong user name and password, but correct CAPTCHA.
  $edit = [
    'name' => 'foobar',
    'pass' => 'bazlaz',
    'captcha_response' => 'Test 123',
  ];
  $this
    ->submitForm($edit, $this
    ->t('Log in'), self::LOGIN_HTML_FORM_ID);

  // Check that there was no error message for the CAPTCHA.
  $this
    ->assertCaptchaResponseAccepted();

  // Name and password were wrong, we should get an updated
  // form with a fresh CAPTCHA.
  $this
    ->assertCaptchaPresence(TRUE);
  $this
    ->assertPreservedCsid($captcha_sid_initial);

  // Post from again.
  $this
    ->submitForm($edit, $this
    ->t('Log in'), self::LOGIN_HTML_FORM_ID);

  // Check that there was no error message for the CAPTCHA.
  $this
    ->assertCaptchaResponseAccepted();
  $this
    ->assertPreservedCsid($captcha_sid_initial);
}