You are here

public function CaptchaCacheTest::testCacheableCaptcha in CAPTCHA 8

Tests a cacheable captcha type.

File

tests/src/Functional/CaptchaCacheTest.php, line 81

Class

CaptchaCacheTest
Tests CAPTCHA caching on various pages.

Namespace

Drupal\Tests\captcha\Functional

Code

public function testCacheableCaptcha() {
  $web_assert = $this
    ->assertSession();

  // Enable captcha on login block with a cacheable captcha.
  captcha_set_form_id_setting('user_login_form', 'captcha_test/TestCacheable');

  // Warm up the caches.
  $this
    ->drupalGet('');

  // Let's check if the page is cached.
  $this
    ->drupalGet('');
  static::assertSame('HIT', $this
    ->drupalGetHeader('X-Drupal-Cache'), 'Cache enabled');
  $edit = [
    'name' => $this->normalUser
      ->getDisplayName(),
    'pass' => $this->normalUser->pass_raw,
    'captcha_response' => 'Test 123',
  ];
  $this
    ->submitForm($edit, 'Log in');
  $web_assert
    ->addressEquals('user/' . $this->normalUser
    ->id());

  // Simulate a cron run that deletes the {captcha_session} data.
  $connection = Database::getConnection();
  $connection
    ->delete('captcha_sessions')
    ->execute();

  // Log out and reload the form. Because the captcha is cacheable, the form
  // is retrieved from the render cache, and contains the same CSID as
  // previously.
  $this
    ->drupalLogout();
  $this
    ->drupalGet('');
  static::assertSame('HIT', $this
    ->drupalGetHeader('X-Drupal-Cache'), 'Cache enabled');
  $edit = [
    'name' => $this->normalUser
      ->getDisplayName(),
    'pass' => $this->normalUser->pass_raw,
    'captcha_response' => 'Test 123',
  ];
  $this
    ->submitForm($edit, 'Log in');
  $web_assert
    ->addressEquals('user/' . $this->normalUser
    ->id());
}