You are here

public function CaptchaAdminTest::testUntrustedUserPosting in CAPTCHA 8

Test untrusted user posting.

File

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

Class

CaptchaAdminTest
Tests CAPTCHA admin settings.

Namespace

Drupal\Tests\captcha\Functional

Code

public function testUntrustedUserPosting() {

  // Set CAPTCHA on comment form.
  captcha_set_form_id_setting(self::COMMENT_FORM_ID, 'captcha/Math');

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

  // Log in as normal (untrusted) user.
  $this
    ->drupalLogin($this->normalUser);

  // Go to node page and click the "add comment" link.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->clickLink($this
    ->t('Add new comment'));
  $add_comment_url = $this
    ->getUrl();

  // Check if CAPTCHA is visible on form.
  $this
    ->assertCaptchaPresence(TRUE);

  // Try to post a comment with wrong answer.
  $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');
}