View source
<?php
namespace Drupal\Tests\captcha\Functional;
use Drupal\Core\Database\Database;
class CaptchaCacheTest extends CaptchaWebTestBase {
protected static $modules = [
'block',
'image_captcha',
'captcha_test',
];
public function setUp() : void {
parent::setUp();
$this
->drupalPlaceBlock('user_login_block', [
'id' => 'login',
]);
}
public function testCacheTags() {
global $base_path;
$this
->drupalGet('');
$this
->assertEquals($this
->getSession()
->getResponseHeader('x-drupal-cache'), 'MISS');
$this
->drupalGet('');
$this
->assertEquals($this
->getSession()
->getResponseHeader('x-drupal-cache'), 'HIT');
captcha_set_form_id_setting('user_login_form', 'captcha/Math');
$this
->drupalGet('');
$sid = $this
->getCaptchaSidFromForm();
$this
->assertNull($this
->getSession()
->getResponseHeader('x-drupal-cache'), 'Cache is disabled');
$this
->drupalGet('');
$this
->assertNotEquals($sid, $this
->getCaptchaSidFromForm());
captcha_set_form_id_setting('user_login_form', 'captcha/Test');
$this
->drupalGet('');
$sid = $this
->getCaptchaSidFromForm();
$this
->assertNull($this
->getSession()
->getResponseHeader('x-drupal-cache'), 'Cache is disabled');
$this
->drupalGet('');
$this
->assertNotEquals($sid, $this
->getCaptchaSidFromForm());
captcha_set_form_id_setting('user_login_form', 'image_captcha/Image');
$this
->drupalGet('');
$image_path = $this
->getSession()
->getPage()
->find('css', '.captcha img')
->getAttribute('src');
$this
->assertNull($this
->getSession()
->getResponseHeader('x-drupal-cache'), 'Cache disabled');
$this
->drupalGet('');
$this
->assertNotEquals($image_path, $this
->getSession()
->getPage()
->find('css', '.captcha img')
->getAttribute('src'));
$this
->drupalGet(substr($image_path, strlen($base_path)));
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet(substr($image_path, strlen($base_path)));
$this
->assertSession()
->statusCodeEquals(200);
}
public function testCacheableCaptcha() {
$web_assert = $this
->assertSession();
captcha_set_form_id_setting('user_login_form', 'captcha_test/TestCacheable');
$this
->drupalGet('');
$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());
$connection = Database::getConnection();
$connection
->delete('captcha_sessions')
->execute();
$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());
}
}