You are here

public function CaptchaSessionReuseAttackTestCase::testMultipleCaptchaProtectedFormsOnOnePage in CAPTCHA 8

Test multiple captcha widgets on single page.

File

tests/src/Functional/CaptchaSessionReuseAttackTestCase.php, line 170

Class

CaptchaSessionReuseAttackTestCase
Tests CAPTCHA session reusing.

Namespace

Drupal\Tests\captcha\Functional

Code

public function testMultipleCaptchaProtectedFormsOnOnePage() {
  \Drupal::service('module_installer')
    ->install([
    'block',
  ]);
  $this
    ->drupalPlaceBlock('user_login_block');

  // Set Test CAPTCHA on comment form and login block.
  captcha_set_form_id_setting(self::COMMENT_FORM_ID, 'captcha/Test');
  captcha_set_form_id_setting('user_login_form', 'captcha/Test');
  $this
    ->allowCommentPostingForAnonymousVisitors();

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

  // Preview comment with correct CAPTCHA answer.
  $edit = $this
    ->getCommentFormValues();
  $comment_subject = $edit['subject[0][value]'];
  $edit['captcha_response'] = 'Test 123';
  $this
    ->drupalGet('comment/reply/node/' . $node
    ->id() . '/comment');
  $this
    ->submitForm($edit, 'Preview');

  // Post should be accepted: no warnings,
  // no CAPTCHA reuse detection (which could be used by user log in block).
  $this
    ->assertCaptchaResponseAccepted();
  $this
    ->assertSession()
    ->pageTextContains($comment_subject);
}