View source
<?php
namespace Drupal\Tests\captcha\Functional;
use Drupal\captcha\Entity\CaptchaPoint;
use Drupal\Core\Url;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class CaptchaAdminTest extends CaptchaWebTestBase {
use StringTranslationTrait;
public function testAdminAccess() {
$this
->drupalLogin($this->normalUser);
$this
->drupalGet(self::CAPTCHA_ADMIN_PATH);
$this
->assertSession()
->pageTextContains($this
->t('Access denied'), 'Normal users should not be able to access the CAPTCHA admin pages', 'CAPTCHA');
$this
->drupalLogin($this->adminUser);
$this
->drupalGet(self::CAPTCHA_ADMIN_PATH);
$this
->assertSession()
->pageTextNotContains($this
->t('Access denied'), 'Admin users should be able to access the CAPTCHA admin pages', 'CAPTCHA');
}
public function testCaptchaPointSettingGetterAndSetter() {
$comment_form_id = self::COMMENT_FORM_ID;
captcha_set_form_id_setting($comment_form_id, 'test');
$result = captcha_get_form_id_setting($comment_form_id);
$this
->assertNotNull($result, 'CAPTCHA exists', 'CAPTCHA');
$this
->assertEquals($result
->getCaptchaType(), 'test', 'CAPTCHA type: default');
$result = captcha_get_form_id_setting($comment_form_id, TRUE);
$this
->assertNotNull($result, 'CAPTCHA exists', 'CAPTCHA');
$this
->assertEquals($result, 'test', 'Setting and symbolic getting CAPTCHA point: "test"');
captcha_set_form_id_setting($comment_form_id, 'default');
$this
->config('captcha.settings')
->set('default_challenge', 'foo/bar')
->save();
$result = captcha_get_form_id_setting($comment_form_id);
$this
->assertNotNull($result, 'CAPTCHA exists', 'CAPTCHA');
$this
->assertEquals($result
->getCaptchaType(), 'foo/bar', 'Setting and getting CAPTCHA point: default');
$result = captcha_get_form_id_setting($comment_form_id, TRUE);
$this
->assertNotNull($result, 'Setting and symbolic getting CAPTCHA point: "default"');
$this
->assertEquals($result, 'foo/bar', 'Setting and symbolic getting CAPTCHA point: default');
captcha_set_form_id_setting($comment_form_id, 'baz/boo');
$result = captcha_get_form_id_setting($comment_form_id);
$this
->assertNotNull($result, 'CAPTCHA exists', 'CAPTCHA');
$this
->assertEquals($result
->getCaptchaType(), 'baz/boo', 'Setting and getting CAPTCHA point: baz/boo');
$result = captcha_get_form_id_setting($comment_form_id, TRUE);
$this
->assertEquals($result, 'baz/boo', 'Setting and symbolic getting CAPTCHA point: "baz/boo"');
captcha_set_form_id_setting($comment_form_id, NULL);
$result = captcha_get_form_id_setting($comment_form_id);
$this
->assertNotNull($result, 'CAPTCHA exists', 'CAPTCHA');
$this
->assertEquals($result
->getCaptchaType(), 'foo/bar', 'Setting and getting CAPTCHA point: NULL');
$result = captcha_get_form_id_setting($comment_form_id, TRUE);
$this
->assertNotNull($result, 'CAPTCHA exists', 'CAPTCHA');
$captcha_type = 'baba/fofo';
captcha_set_form_id_setting($comment_form_id, $captcha_type);
$result = captcha_get_form_id_setting($comment_form_id);
$this
->assertNotNull($result, 'Setting and getting CAPTCHA point: baba/fofo', 'CAPTCHA');
$this
->assertEquals($result
->getCaptchaType(), 'baba/fofo', 'Setting and getting CAPTCHA point: baba/fofo');
$result = captcha_get_form_id_setting($comment_form_id, TRUE);
$this
->assertEquals($result, 'baba/fofo', 'Setting and symbolic getting CAPTCHA point: "baba/fofo"');
}
protected function assertCaptchaSetting($form_id, $challenge_type) {
$result = captcha_get_form_id_setting(self::COMMENT_FORM_ID, TRUE);
$this
->assertEquals($result, $challenge_type, $this
->t('Check CAPTCHA setting for form: expected: @expected, received: @received.', [
'@expected' => var_export($challenge_type, TRUE),
'@received' => var_export($result, TRUE),
]));
}
public function testCaptchaAdminLinks() {
$this
->drupalLogin($this->adminUser);
$edit = [
'administration_mode' => TRUE,
];
$this
->drupalGet(self::CAPTCHA_ADMIN_PATH);
$this
->submitForm($edit, $this
->t('Save configuration'));
$node = $this
->drupalCreateNode();
$this
->drupalGet('node/' . $node
->id());
$this
->clickLink($this
->t('Add new comment'));
$add_comment_url = $this
->getUrl();
$add_comment_url = strtok($add_comment_url, "#");
$this
->clickLink($this
->t('Place a CAPTCHA here for untrusted users.'));
$edit = [
'captchaType' => 'captcha/Math',
];
$this
->drupalGet($this
->getUrl());
$this
->submitForm($edit, $this
->t('Save'));
$this
->assertSession()
->addressEquals($add_comment_url, [], 'After setting CAPTCHA with CAPTCHA admin links: should return to original form.', 'CAPTCHA');
$this
->assertSession()
->pageTextContains($this
->t('CAPTCHA: challenge "@type" enabled', [
'@type' => $edit['captchaType'],
]), 'Enable a challenge through the CAPTCHA admin links', 'CAPTCHA');
$this
->assertCaptchaSetting(self::COMMENT_FORM_ID, 'captcha/Math');
$this
->clickLink($this
->t('change'));
$edit = [
'captchaType' => 'default',
];
$this
->drupalGet($this
->getUrl());
$this
->submitForm($edit, 'Save');
$this
->assertEquals($add_comment_url, $this
->getUrl(), 'After editing challenge type CAPTCHA admin links: should return to original form.');
$this
->assertSession()
->pageTextContains($this
->t('CAPTCHA: challenge "@type" enabled', [
'@type' => $edit['captchaType'],
]), 'Enable a challenge through the CAPTCHA admin links', 'CAPTCHA');
$this
->assertCaptchaSetting(self::COMMENT_FORM_ID, 'default');
$this
->drupalGet(Url::fromRoute('entity.captcha_point.disable', [
'captcha_point' => self::COMMENT_FORM_ID,
]));
$this
->submitForm([], $this
->t('Disable'));
global $base_url;
$this
->assertEquals($base_url . '/admin/config/people/captcha/captcha-points', $this
->getUrl(), 'After disabling challenge in CAPTCHA admin: should return to captcha point list.');
$this
->assertSession()
->responseContains($this
->t('Captcha point %form_id has been disabled.', [
'%form_id' => self::COMMENT_FORM_ID,
]), 'Disable challenge through the CAPTCHA admin links', 'CAPTCHA');
}
public function testUntrustedUserPosting() {
captcha_set_form_id_setting(self::COMMENT_FORM_ID, 'captcha/Math');
$node = $this
->drupalCreateNode();
$this
->drupalLogin($this->normalUser);
$this
->drupalGet('node/' . $node
->id());
$this
->clickLink($this
->t('Add new comment'));
$add_comment_url = $this
->getUrl();
$this
->assertCaptchaPresence(TRUE);
$edit = $this
->getCommentFormValues();
$edit['captcha_response'] = 'xx';
$this
->drupalGet($add_comment_url);
$this
->submitForm($edit, $this
->t('Preview'));
$this
->assertSession()
->pageTextContains(self::CAPTCHA_WRONG_RESPONSE_ERROR_MESSAGE, 'wrong CAPTCHA should block form submission.', 'CAPTCHA');
}
public function testXssOnCaptchaDescription() {
captcha_set_form_id_setting('user_register', 'captcha/Math');
$this
->drupalLogin($this->adminUser);
$xss = '<script type="text/javascript">alert("xss")</script>';
$edit = [
'description' => $xss,
];
$this
->drupalGet(self::CAPTCHA_ADMIN_PATH);
$this
->submitForm($edit, $this
->t('Save configuration'));
$this
->drupalLogout();
$this
->drupalGet('user/register');
$this
->assertSession()
->responseNotContains($xss, 'JavaScript should not be allowed in CAPTCHA description.', 'CAPTCHA');
}
public function testCaptchaPlacementCacheClearing() {
captcha_set_form_id_setting('user_register_form', 'captcha/Math');
$this
->drupalGet('user/register');
$placement_map = $this->container
->get('cache.default')
->get('captcha_placement_map_cache');
$this
->assertNotNull($placement_map, 'CAPTCHA placement cache should be set.');
$this
->drupalLogin($this->adminUser);
$this
->drupalGet(self::CAPTCHA_ADMIN_PATH);
$this
->submitForm([], $this
->t('Clear the CAPTCHA placement cache'));
$placement_map = $this->container
->get('cache.default')
->get('captcha_placement_map_cache');
$this
->assertFalse($placement_map, 'CAPTCHA placement cache should be unset after cache clear.');
}
protected function getCaptchaPointSettingFromDatabase($form_id) {
$ids = \Drupal::entityQuery('captcha_point')
->condition('formId', $form_id)
->execute();
return $ids ? CaptchaPoint::load(reset($ids)) : NULL;
}
public function testCaptchaPointAdministration() {
$captcha_point_form_id = 'form_' . strtolower($this
->randomMachineName(32));
$captcha_point_module = 'captcha';
$captcha_point_type = 'Math';
$this
->drupalLogin($this->adminUser);
$label = 'TEST';
$form_values = [
'formId' => $captcha_point_form_id,
'captchaType' => $captcha_point_module . '/' . $captcha_point_type,
];
$this
->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/add');
$this
->submitForm($form_values, 'Save');
$this
->assertSession()
->pageTextContains($this
->t('Form ID field is required.'));
$form_values['label'] = $label;
$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' => $captcha_point_form_id,
]));
$result = $this
->getCaptchaPointSettingFromDatabase($captcha_point_form_id);
$this
->assertEquals($result->captchaType, $captcha_point_module . '/' . $captcha_point_type, 'Enabled CAPTCHA point should have module and type set');
$this
->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id . '/disable');
$this
->submitForm([], $this
->t('Disable'));
$this
->assertSession()
->responseContains($this
->t('Captcha point %label has been disabled.', [
'%label' => $label,
]), 'Disabling of CAPTCHA point');
$result = $this
->getCaptchaPointSettingFromDatabase($captcha_point_form_id);
$this
->assertInstanceOf(CaptchaPoint::class, $result, 'Disabled CAPTCHA point should be in database');
$this
->assertFalse($result
->status());
$form_values = [
'captchaType' => $captcha_point_module . '/' . $captcha_point_type,
];
$this
->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id);
$this
->submitForm($form_values, $this
->t('Save'));
$this
->assertSession()
->responseContains($this
->t('Captcha Point for %form_id form was updated.', [
'%form_id' => $captcha_point_form_id,
]), 'Saving of CAPTCHA point settings');
$result = $this
->getCaptchaPointSettingFromDatabase($captcha_point_form_id);
$this
->assertEquals($result->captchaType, $captcha_point_module . '/' . $captcha_point_type, 'Enabled CAPTCHA point should have module and type set');
$this
->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id . '/delete');
$this
->submitForm([], $this
->t('Delete'));
$this
->assertSession()
->responseContains($this
->t('Captcha point %label has been deleted.', [
'%label' => $label,
]), 'Deleting of CAPTCHA point');
$result = $this
->getCaptchaPointSettingFromDatabase($captcha_point_form_id);
$this
->assertNull($result, 'Deleted CAPTCHA point should not be in database');
}
public function testCaptchaPointAdministrationByNonAdmin() {
$captcha_point_form_id = 'form_' . strtolower($this
->randomMachineName(32));
$captcha_point_module = 'captcha';
$captcha_point_type = 'Math';
$label = 'TEST_2';
$this
->drupalLogin($this->adminUser);
$form_values = [
'label' => $label,
'formId' => $captcha_point_form_id,
'captchaType' => $captcha_point_module . '/' . $captcha_point_type,
];
$this
->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/add');
$this
->submitForm($form_values, $this
->t('Save'));
$this
->assertSession()
->responseContains($this
->t('Captcha Point for %form_id form was created.', [
'%form_id' => $captcha_point_form_id,
]));
$this
->drupalLogin($this->normalUser);
$this
->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points');
$this
->assertSession()
->pageTextContains($this
->t('You are not authorized to access this page.'), 'Non admin should not be able to set a CAPTCHA point');
$this
->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id . '/disable');
$this
->assertSession()
->pageTextContains($this
->t('You are not authorized to access this page.'), 'Non admin should not be able to disable a CAPTCHA point');
$this
->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id . '/delete');
$this
->assertSession()
->pageTextContains($this
->t('You are not authorized to access this page.'), 'Non admin should not be able to delete a CAPTCHA point');
$this
->drupalLogin($this->adminUser);
$result = $this
->getCaptchaPointSettingFromDatabase($captcha_point_form_id);
$this
->assertEquals($result->captchaType, $captcha_point_module . '/' . $captcha_point_type, 'Enabled CAPTCHA point should have module and type set');
$this
->drupalGet(self::CAPTCHA_ADMIN_PATH . '/captcha-points/' . $captcha_point_form_id . '/delete');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->responseContains($this
->t('Captcha point %label has been deleted.', [
'%label' => $label,
]), 'Disabling of CAPTCHA point');
}
}