View source
<?php
namespace Drupal\Tests\captcha\Functional;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class CaptchaTest extends CaptchaWebTestBase {
use StringTranslationTrait;
protected static $modules = [
'block',
'captcha_long_form_id_test',
];
public function testCaptchaOnLoginForm() {
$user = $this
->drupalCreateUser();
$this
->drupalLogin($user);
$this
->drupalLogout();
$captcha_point = \Drupal::entityTypeManager()
->getStorage('captcha_point')
->load('user_login_form');
$captcha_point
->setCaptchaType('captcha/Math');
$captcha_point
->enable()
->save();
$this
->drupalGet('user');
$this
->assertCaptchaPresence(TRUE);
$edit = [
'name' => $user
->getDisplayName(),
'pass' => $user->pass_raw,
'captcha_response' => '?',
];
$this
->submitForm($edit, $this
->t('Log in'), self::LOGIN_HTML_FORM_ID);
$this
->assertSession()
->pageTextContains(self::CAPTCHA_WRONG_RESPONSE_ERROR_MESSAGE, 'CAPTCHA should block user login form', 'CAPTCHA');
$this
->drupalGet('user');
$this
->assertSession()
->fieldExists('name');
$this
->assertSession()
->fieldExists('pass');
}
public function testCaptchaResponseErrorMenssage() {
$this
->drupalLogin($this->adminUser);
$customized_menssage = 'The answer you entered is wrong.';
$edit = [
'wrong_captcha_response_message' => $customized_menssage,
];
$this
->drupalGet("admin/config/people/captcha");
$this
->submitForm($edit, $this
->t('Save configuration'));
$captcha_point = \Drupal::entityTypeManager()
->getStorage('captcha_point')
->load('user_login_form');
$captcha_point
->setCaptchaType('captcha/Math');
$captcha_point
->enable()
->save();
$this
->drupalLogout();
$this
->drupalGet('user');
$edit = [
'name' => $this->adminUser
->getDisplayName(),
'pass' => $this->adminUser->pass_raw,
'captcha_response' => '?',
];
$this
->submitForm($edit, $this
->t('Log in'), self::LOGIN_HTML_FORM_ID);
$this
->assertSession()
->pageTextContains($customized_menssage, 'CAPTCHA should block user login form', 'CAPTCHA');
}
protected function assertCommentPosting($captcha_response, $should_pass, $message) {
$this->container
->get('state')
->set('comment_preview_page', DRUPAL_OPTIONAL);
$node = $this
->drupalCreateNode();
$edit = $this
->getCommentFormValues();
$comment_subject = $edit['subject[0][value]'];
$comment_body = $edit['comment_body[0][value]'];
$edit['captcha_response'] = $captcha_response;
$this
->drupalGet('comment/reply/node/' . $node
->id() . '/comment');
$this
->submitForm($edit, $this
->t('Save'), 'comment-form');
if ($should_pass) {
$this
->assertCaptchaResponseAccepted();
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->pageTextContains($comment_subject, $message . ' Comment should show up on node page.', 'CAPTCHA');
$this
->assertSession()
->pageTextContains($comment_body, $message . ' Comment should show up on node page.', 'CAPTCHA');
}
else {
$this
->assertSession()
->pageTextContains(self::CAPTCHA_WRONG_RESPONSE_ERROR_MESSAGE, $message . ' Comment submission should be blocked.', 'CAPTCHA');
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->pageTextNotContains($comment_subject, $message . ' Comment should not show up on node page.', 'CAPTCHA');
$this
->assertSession()
->pageTextNotContains($comment_body, $message . ' Comment should not show up on node page.', 'CAPTCHA');
}
}
public function testCaseInsensitiveValidation() {
$config = $this
->config('captcha.settings');
captcha_set_form_id_setting(self::COMMENT_FORM_ID, 'captcha/Test');
$this
->drupalLogin($this->normalUser);
$config
->set('default_validation', CAPTCHA_DEFAULT_VALIDATION_CASE_SENSITIVE);
$config
->save();
$this
->assertCommentPosting('Test 123', TRUE, 'Case sensitive validation of right casing.');
$this
->assertCommentPosting('test 123', FALSE, 'Case sensitive validation of wrong casing.');
$this
->assertCommentPosting('TEST 123', FALSE, 'Case sensitive validation of wrong casing.');
$config
->set('default_validation', CAPTCHA_DEFAULT_VALIDATION_CASE_INSENSITIVE);
$config
->save();
$this
->assertCommentPosting('Test 123', TRUE, 'Case insensitive validation of right casing.');
$this
->assertCommentPosting('test 123', TRUE, 'Case insensitive validation of wrong casing.');
$this
->assertCommentPosting('TEST 123', TRUE, 'Case insensitive validation of wrong casing.');
}
public function testCaptchaDescriptionAfterCommentPreview() {
captcha_set_form_id_setting(self::COMMENT_FORM_ID, 'captcha/Test');
$this
->drupalLogin($this->normalUser);
$node = $this
->drupalCreateNode();
$edit = $this
->getCommentFormValues();
$edit['captcha_response'] = 'Test 123';
$this
->drupalGet('comment/reply/node/' . $node
->id() . '/comment');
$this
->submitForm($edit, $this
->t('Preview'));
$this
->assertCaptchaPresence(FALSE);
}
public function testCaptchaSessionReuseOnNodeForms() {
captcha_set_form_id_setting('node_page_form', 'captcha/Test');
$this
->drupalLogin($this->normalUser);
$edit = $this
->getNodeFormValues();
$edit['captcha_response'] = 'Test 123';
$this
->drupalGet('node/add/page');
$this
->submitForm($edit, $this
->t('Preview'));
$this
->assertCaptchaPresence(FALSE);
}
public function testCaptchaOnLoginBlockOnAdminPagesIssue893810() {
$captcha_point = \Drupal::entityTypeManager()
->getStorage('captcha_point')
->load('user_login_form');
$captcha_point
->setCaptchaType('captcha/Math');
$captcha_point
->enable()
->save();
$this
->drupalPlaceBlock('user_login_block', [
'id' => 'login',
]);
$this
->drupalGet('');
$this
->assertCaptchaPresence(TRUE);
$this
->drupalGet('admin');
$this
->assertCaptchaPresence(TRUE);
}
public function testLongFormId() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet(self::CAPTCHA_ADMIN_PATH);
$label = 'this_formid_is_intentionally_longer_than_64_characters_to_test_captcha';
$formId = substr($label, 0, 64);
$form_values = [
'label' => $label,
'formId' => $formId,
'captchaType' => 'captcha/Math',
];
$this
->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/add');
$this
->submitForm($form_values, $this
->t('Save'));
$this
->assertSession()
->responseContains($this
->t('Captcha Point for %label form was created.', [
'%label' => $formId,
]));
$this
->drupalLogout();
$this
->drupalGet('captcha/test_form/long_id');
$this
->assertCaptchaPresence(TRUE);
}
}