View source
<?php
namespace Drupal\Tests\captcha\Functional;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\BrowserTestBase;
abstract class CaptchaWebTestBase extends BrowserTestBase {
use CommentTestTrait;
const CAPTCHA_WRONG_RESPONSE_ERROR_MESSAGE = 'The answer you entered for the CAPTCHA was not correct.';
const CAPTCHA_UNKNOWN_CSID_ERROR_MESSAGE = 'CAPTCHA validation error: unknown CAPTCHA session ID. Contact the site administrator if this problem persists.';
const COMMENT_FORM_ID = 'comment_comment_form';
const LOGIN_HTML_FORM_ID = 'user-login-form';
const CAPTCHA_ADMIN_PATH = 'admin/config/people/captcha';
protected static $modules = [
'captcha',
'comment',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected $normalUser;
public function setUp() {
parent::setUp();
module_load_include('inc', 'captcha');
$this
->drupalCreateContentType([
'type' => 'page',
]);
$permissions = [
'access comments',
'post comments',
'skip comment approval',
'access content',
'create page content',
'edit own page content',
];
$this->normalUser = $this
->drupalCreateUser($permissions);
$permissions[] = 'administer CAPTCHA settings';
$permissions[] = 'skip CAPTCHA';
$permissions[] = 'administer permissions';
$permissions[] = 'administer content types';
$this->adminUser = $this
->drupalCreateUser($permissions);
$this
->addDefaultCommentField('node', 'page');
$comment_field = FieldConfig::loadByName('node', 'page', 'comment');
$comment_field
->setSetting('form_location', CommentItemInterface::FORM_SEPARATE_PAGE);
$comment_field
->save();
$captcha_point = \Drupal::entityTypeManager()
->getStorage('captcha_point')
->load('user_login_form');
$captcha_point
->enable()
->save();
$this
->config('captcha.settings')
->set('default_challenge', 'captcha/test')
->save();
}
protected function assertCaptchaResponseAccepted() {
$this
->assertSession()
->pageTextNotContains(self::CAPTCHA_UNKNOWN_CSID_ERROR_MESSAGE, 'CAPTCHA response should be accepted (known CSID).', 'CAPTCHA');
$this
->assertSession()
->pageTextNotContains(self::CAPTCHA_WRONG_RESPONSE_ERROR_MESSAGE, 'CAPTCHA response should be accepted (correct response).', 'CAPTCHA');
}
protected function assertCaptchaPresence($presence) {
if ($presence) {
$this
->assertSession()
->pageTextContains(_captcha_get_description(), 'There should be a CAPTCHA on the form.', 'CAPTCHA');
}
else {
$this
->assertSession()
->pageTextNotContains(_captcha_get_description(), 'There should be no CAPTCHA on the form.', 'CAPTCHA');
}
}
protected function getCommentFormValues() {
$edit = [
'subject[0][value]' => 'comment_subject ' . $this
->randomMachineName(32),
'comment_body[0][value]' => 'comment_body ' . $this
->randomMachineName(256),
];
return $edit;
}
protected function getNodeFormValues() {
$edit = [
'title[0][value]' => 'node_title ' . $this
->randomMachineName(32),
'body[0][value]' => 'node_body ' . $this
->randomMachineName(256),
];
return $edit;
}
protected function getCaptchaSidFromForm($form_html_id = NULL) {
if (!$form_html_id) {
$elements = $this
->xpath('//input[@name="captcha_sid"]');
}
else {
$elements = $this
->xpath('//form[@id="' . $form_html_id . '"]//input[@name="captcha_sid"]');
}
$element = current($elements);
$captcha_sid = (int) $element
->getValue();
return $captcha_sid;
}
protected function getCaptchaTokenFromForm($form_html_id = NULL) {
if (!$form_html_id) {
$elements = $this
->xpath('//input[@name="captcha_token"]');
}
else {
$elements = $this
->xpath('//form[@id="' . $form_html_id . '"]//input[@name="captcha_token"]');
}
$element = current($elements);
$captcha_token = (int) $element
->getValue();
return $captcha_token;
}
protected function getMathCaptchaSolutionFromForm($form_html_id = NULL) {
if (!$form_html_id) {
$elements = $this
->xpath('//div[contains(@class, "form-item-captcha-response")]/span[@class="field-prefix"]');
}
else {
$elements = $this
->xpath('//form[@id="' . $form_html_id . '"]//div[contains(@class, "form-item-captcha-response")]/span[@class="field-prefix"]');
}
$this
->assertTrue('pass', json_encode($elements));
$challenge = (string) $elements[0];
$this
->assertTrue('pass', $challenge);
$matches = [];
preg_match('/\\s*(\\d+)\\s*(-|\\+)\\s*(\\d+)\\s*=\\s*/', $challenge, $matches);
$a = (int) $matches[1];
$b = (int) $matches[3];
$solution = $matches[2] == '-' ? $a - $b : $a + $b;
return $solution;
}
protected function allowCommentPostingForAnonymousVisitors() {
user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, [
'access comments',
'post comments',
'skip comment approval',
]);
}
}