You are here

public function CaptchaAdminTest::testCaptchaAdminLinks in CAPTCHA 8

Testing of the CAPTCHA administration links.

File

tests/src/Functional/CaptchaAdminTest.php, line 108

Class

CaptchaAdminTest
Tests CAPTCHA admin settings.

Namespace

Drupal\Tests\captcha\Functional

Code

public function testCaptchaAdminLinks() {
  $this
    ->drupalLogin($this->adminUser);

  // Enable CAPTCHA administration links.
  $edit = [
    'administration_mode' => TRUE,
  ];
  $this
    ->drupalGet(self::CAPTCHA_ADMIN_PATH);
  $this
    ->submitForm($edit, $this
    ->t('Save configuration'));

  // Create a node with comments enabled.
  $node = $this
    ->drupalCreateNode();

  // Go to node page.
  $this
    ->drupalGet('node/' . $node
    ->id());

  // Click the add new comment link.
  $this
    ->clickLink($this
    ->t('Add new comment'));
  $add_comment_url = $this
    ->getUrl();

  // Remove fragment part from comment URL to avoid
  // problems with later asserts.
  $add_comment_url = strtok($add_comment_url, "#");

  // Click the CAPTCHA admin link to enable a challenge.
  $this
    ->clickLink($this
    ->t('Place a CAPTCHA here for untrusted users.'));

  // Enable Math CAPTCHA.
  $edit = [
    'captchaType' => 'captcha/Math',
  ];
  $this
    ->drupalGet($this
    ->getUrl());
  $this
    ->submitForm($edit, $this
    ->t('Save'));

  // Check if returned to original comment form.
  $this
    ->assertSession()
    ->addressEquals($add_comment_url, [], 'After setting CAPTCHA with CAPTCHA admin links: should return to original form.', 'CAPTCHA');

  // Check if CAPTCHA was successfully enabled
  // (on CAPTCHA admin links fieldset).
  $this
    ->assertSession()
    ->pageTextContains($this
    ->t('CAPTCHA: challenge "@type" enabled', [
    '@type' => $edit['captchaType'],
  ]), 'Enable a challenge through the CAPTCHA admin links', 'CAPTCHA');

  // Check if CAPTCHA was successfully enabled (through API).
  $this
    ->assertCaptchaSetting(self::COMMENT_FORM_ID, 'captcha/Math');

  // Edit challenge type through CAPTCHA admin links.
  $this
    ->clickLink($this
    ->t('change'));

  // Enable Math CAPTCHA.
  $edit = [
    'captchaType' => 'default',
  ];
  $this
    ->drupalGet($this
    ->getUrl());
  $this
    ->submitForm($edit, 'Save');

  // Check if returned to original comment form.
  $this
    ->assertEquals($add_comment_url, $this
    ->getUrl(), 'After editing challenge type CAPTCHA admin links: should return to original form.');

  // Check if CAPTCHA was successfully changed
  // (on CAPTCHA admin links fieldset).
  // This is actually the same as the previous setting because
  // the captcha/Math is the default for the default challenge.
  // TODO Make sure the edit is a real change.
  $this
    ->assertSession()
    ->pageTextContains($this
    ->t('CAPTCHA: challenge "@type" enabled', [
    '@type' => $edit['captchaType'],
  ]), 'Enable a challenge through the CAPTCHA admin links', 'CAPTCHA');

  // Check if CAPTCHA was successfully edited (through API).
  $this
    ->assertCaptchaSetting(self::COMMENT_FORM_ID, 'default');

  // Disable challenge through CAPTCHA admin links.
  $this
    ->drupalGet(Url::fromRoute('entity.captcha_point.disable', [
    'captcha_point' => self::COMMENT_FORM_ID,
  ]));
  $this
    ->submitForm([], $this
    ->t('Disable'));

  // Check if returned to captcha point list.
  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.');

  // Check if CAPTCHA was successfully disabled
  // (on CAPTCHA admin links fieldset).
  $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');
}