View source
<?php
namespace Drupal\Tests\captcha\Functional;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class CaptchaPersistenceTest extends CaptchaWebTestBase {
use StringTranslationTrait;
private function setUpPersistence($persistence) {
$this
->drupalLogin($this->adminUser);
$edit = [
'persistence' => $persistence,
];
$this
->drupalGet(self::CAPTCHA_ADMIN_PATH);
$this
->submitForm($edit, 'Save configuration');
$this
->drupalLogout();
captcha_set_form_id_setting('user_login_form', 'captcha/Test');
$this
->drupalGet('user');
$this
->assertCaptchaPresence(TRUE);
captcha_set_form_id_setting('user_register_form', 'captcha/Test');
$this
->drupalGet('user/register');
$this
->assertCaptchaPresence(TRUE);
}
protected function assertPreservedCsid($captcha_sid_initial) {
$captcha_sid = $this
->getCaptchaSidFromForm();
$this
->assertEquals($captcha_sid_initial, $captcha_sid, "CAPTCHA session ID should be preserved (expected: {$captcha_sid_initial}, found: {$captcha_sid}).");
}
protected function assertDifferentCsid($captcha_sid_initial) {
$captcha_sid = $this
->getCaptchaSidFromForm();
$this
->assertNotEquals($captcha_sid_initial, $captcha_sid, "CAPTCHA session ID should be different.");
}
public function testPersistenceAlways() {
$this
->setUpPersistence(CAPTCHA_PERSISTENCE_SHOW_ALWAYS);
$this
->drupalGet('<front>');
$this
->assertCaptchaPresence(TRUE);
$captcha_sid_initial = $this
->getCaptchaSidFromForm();
$edit = [
'name' => 'foobar',
'pass' => 'bazlaz',
'captcha_response' => 'Test 123',
];
$this
->submitForm($edit, $this
->t('Log in'), self::LOGIN_HTML_FORM_ID);
$this
->assertCaptchaResponseAccepted();
$this
->assertCaptchaPresence(TRUE);
$this
->assertPreservedCsid($captcha_sid_initial);
$this
->submitForm($edit, $this
->t('Log in'), self::LOGIN_HTML_FORM_ID);
$this
->assertCaptchaResponseAccepted();
$this
->assertPreservedCsid($captcha_sid_initial);
}
public function testPersistencePerFormInstance() {
$this
->setUpPersistence(CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_INSTANCE);
$this
->drupalGet('<front>');
$this
->assertCaptchaPresence(TRUE);
$captcha_sid_initial = $this
->getCaptchaSidFromForm();
$edit = [
'name' => 'foobar',
'pass' => 'bazlaz',
'captcha_response' => 'Test 123',
];
$this
->submitForm($edit, $this
->t('Log in'), self::LOGIN_HTML_FORM_ID);
$this
->assertCaptchaResponseAccepted();
$this
->assertCaptchaPresence(FALSE);
$this
->assertPreservedCsid($captcha_sid_initial);
$this
->drupalGet('node');
$this
->drupalGet('user');
$this
->assertCaptchaPresence(TRUE);
$this
->assertDifferentCsid($captcha_sid_initial);
$this
->drupalGet('user/register');
$this
->assertCaptchaPresence(TRUE);
$this
->assertDifferentCsid($captcha_sid_initial);
}
public function testPersistencePerFormType() {
$this
->setUpPersistence(CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL_PER_FORM_TYPE);
$this
->drupalGet('<front>');
$this
->assertCaptchaPresence(TRUE);
$captcha_sid_initial = $this
->getCaptchaSidFromForm();
$edit = [
'name' => 'foobar',
'pass' => 'bazlaz',
'captcha_response' => 'Test 123',
];
$this
->submitForm($edit, $this
->t('Log in'), self::LOGIN_HTML_FORM_ID);
$this
->assertCaptchaResponseAccepted();
$this
->assertCaptchaPresence(FALSE);
$this
->assertPreservedCsid($captcha_sid_initial);
$this
->drupalGet('node');
$this
->drupalGet('user');
$this
->assertCaptchaPresence(FALSE);
$this
->assertDifferentCsid($captcha_sid_initial);
$captcha_point = \Drupal::entityTypeManager()
->getStorage('captcha_point')
->load('user_register_form');
$captcha_point
->enable()
->save();
$this
->drupalGet('user/register');
$this
->assertCaptchaPresence(TRUE);
$this
->assertDifferentCsid($captcha_sid_initial);
}
public function testPersistenceOnlyOnce() {
$this
->setUpPersistence(CAPTCHA_PERSISTENCE_SKIP_ONCE_SUCCESSFUL);
$this
->drupalGet('<front>');
$this
->assertCaptchaPresence(TRUE);
$captcha_sid_initial = $this
->getCaptchaSidFromForm();
$edit = [
'name' => 'foobar',
'pass' => 'bazlaz',
'captcha_response' => 'Test 123',
];
$this
->submitForm($edit, $this
->t('Log in'), self::LOGIN_HTML_FORM_ID);
$this
->assertCaptchaResponseAccepted();
$this
->assertCaptchaPresence(FALSE);
$this
->assertPreservedCsid($captcha_sid_initial);
$this
->drupalGet('node');
$this
->drupalGet('user');
$this
->assertCaptchaPresence(FALSE);
$this
->assertDifferentCsid($captcha_sid_initial);
$this
->drupalGet('user/register');
$this
->assertCaptchaPresence(FALSE);
$this
->assertDifferentCsid($captcha_sid_initial);
}
}