View source
<?php
namespace Drupal\css_captcha\Tests;
use Drupal\captcha\Tests\CaptchaBaseWebTestCase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
class CSSCaptchaTestCase extends CaptchaBaseWebTestCase {
public static $modules = [
'block',
];
public function testCssCaptchaOnLoginForm() {
$user = $this
->drupalCreateUser();
$this
->drupalLogin($user);
$this
->drupalLogout();
$captcha_point = \Drupal::entityTypeManager()
->getStorage('captcha_point')
->load('user_login_form');
$captcha_point
->setCaptchaType('css_captcha/CSS CAPTCHA');
$captcha_point
->enable()
->save();
$this
->drupalGet('');
$this
->assertCaptchaPresence(TRUE);
$edit = [
'name' => $user
->getUsername(),
'pass' => $user->pass_raw,
'captcha_response' => '?',
];
$this
->drupalPostForm(NULL, $edit, t('Log in'), [], [], self::LOGIN_HTML_FORM_ID);
$this
->assertText(self::CAPTCHA_WRONG_RESPONSE_ERROR_MESSAGE, 'CSS CAPTCHA should block user login form', 'CAPTCHA');
$this
->drupalGet('user');
$this
->assertField('name', t('Username field found.'), 'CAPTCHA');
$this
->assertField('pass', t('Password field found.'), '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
->drupalPostForm('comment/reply/node/' . $node
->id() . '/comment', $edit, t('Save'), [], [], 'comment-form');
if ($should_pass) {
$this
->assertCaptchaResponseAccepted();
$this
->drupalGet('node/' . $node
->id());
$this
->assertText($comment_subject, $message . ' Comment should show up on node page.', 'CAPTCHA');
$this
->assertText($comment_body, $message . ' Comment should show up on node page.', 'CAPTCHA');
}
else {
$this
->assertText(self::CAPTCHA_WRONG_RESPONSE_ERROR_MESSAGE, $message . ' Comment submission should be blocked.', 'CAPTCHA');
$this
->drupalGet('node/' . $node
->id());
$this
->assertNoText($comment_subject, $message . ' Comment should not show up on node page.', 'CAPTCHA');
$this
->assertNoText($comment_body, $message . ' Comment should not show up on node page.', 'CAPTCHA');
}
}
public function testCssCaseInsensitiveValidation() {
$config = $this
->config('captcha.settings');
captcha_set_form_id_setting(self::COMMENT_FORM_ID, 'css_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 testCssCaptchaDescriptionAfterCommentPreview() {
captcha_set_form_id_setting(self::COMMENT_FORM_ID, 'css_captcha/Test');
$this
->drupalLogin($this->normalUser);
$node = $this
->drupalCreateNode();
$edit = $this
->getCommentFormValues();
$edit['captcha_response'] = 'Test 123';
$this
->drupalPostForm('comment/reply/node/' . $node
->id() . '/comment', $edit, t('Preview'));
$this
->assertCaptchaPresence(FALSE);
}
public function testCssCaptchaSessionReuseOnNodeForms() {
captcha_set_form_id_setting('node_page_form', 'css_captcha/Test');
$this
->drupalLogin($this->normalUser);
$edit = $this
->getNodeFormValues();
$edit['captcha_response'] = 'Test 123';
$this
->drupalGet('node/add/page');
$this
->drupalPostForm(NULL, $edit, t('Preview'));
$this
->assertCaptchaPresence(FALSE);
}
public function testCssCaptchaOnLoginBlockOnAdminPagesIssue893810() {
$captcha_point = \Drupal::entityTypeManager()
->getStorage('captcha_point')
->load('user_login_form');
$captcha_point
->setCaptchaType('css_captcha/CSS CAPTCHA');
$captcha_point
->enable()
->save();
$this
->drupalPlaceBlock('user_login_block', [
'id' => 'login',
]);
$this
->drupalGet('');
$this
->assertCaptchaPresence(TRUE);
$this
->drupalGet('admin');
$this
->assertCaptchaPresence(TRUE);
}
public function testAjaxFormRebuild() {
\Drupal::entityManager()
->getStorage('captcha_point')
->create([
'id' => 'user_form',
'formId' => 'user_form',
'status' => TRUE,
'captchaType' => 'css_captcha/CSS CAPTCHA',
])
->save();
$field_storage_config = FieldStorageConfig::create([
'field_name' => 'field_texts',
'type' => 'string',
'entity_type' => 'user',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
]);
$field_storage_config
->save();
FieldConfig::create([
'field_storage' => $field_storage_config,
'bundle' => 'user',
])
->save();
entity_get_form_display('user', 'user', 'default')
->setComponent('field_texts', [
'type' => 'string_textfield',
'weight' => 10,
])
->save();
$user = $this
->drupalCreateUser([]);
$this
->drupalLogin($user);
$this
->drupalGet("user/{$user->id()}/edit");
$this
->drupalPostAjaxForm(NULL, [], 'field_texts_add_more');
$this
->drupalPostForm(NULL, [
'captcha_response' => $this
->getMathCaptchaSolutionFromForm('user-form'),
], t('Save'));
$this
->assertText(t('The changes have been saved.'));
}
}