You are here

protected function CaptchaTest::assertCommentPosting in CAPTCHA 8

Assert function for testing if comment posting works as it should.

Creates node with comment writing enabled, tries to post comment with given CAPTCHA response (caller should enable the desired challenge on page node comment forms) and checks if the result is as expected.

Parameters

string $captcha_response: The response on the CAPTCHA.

bool $should_pass: Describing if the posting should pass or should be blocked.

string $message: To prefix to nested asserts.

1 call to CaptchaTest::assertCommentPosting()
CaptchaTest::testCaseInsensitiveValidation in tests/src/Functional/CaptchaTest.php
Testing the case sensitive/insensitive validation.

File

tests/src/Functional/CaptchaTest.php, line 112

Class

CaptchaTest
Tests CAPTCHA main test case sensitivity.

Namespace

Drupal\Tests\captcha\Functional

Code

protected function assertCommentPosting($captcha_response, $should_pass, $message) {

  // Make sure comments on pages can be saved directly without preview.
  $this->container
    ->get('state')
    ->set('comment_preview_page', DRUPAL_OPTIONAL);

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

  // Post comment on node.
  $edit = $this
    ->getCommentFormValues();
  $comment_subject = $edit['subject[0][value]'];
  $comment_body = $edit['comment_body[0][value]'];
  $edit['captcha_response'] = $captcha_response;
  $this
    ->drupalGet('comment/reply/node/' . $node
    ->id() . '/comment');
  $this
    ->submitForm($edit, $this
    ->t('Save'), 'comment-form');
  if ($should_pass) {

    // There should be no error message.
    $this
      ->assertCaptchaResponseAccepted();

    // Get node page and check that comment shows up.
    $this
      ->drupalGet('node/' . $node
      ->id());
    $this
      ->assertSession()
      ->pageTextContains($comment_subject, $message . ' Comment should show up on node page.', 'CAPTCHA');
    $this
      ->assertSession()
      ->pageTextContains($comment_body, $message . ' Comment should show up on node page.', 'CAPTCHA');
  }
  else {

    // Check for error message.
    $this
      ->assertSession()
      ->pageTextContains(self::CAPTCHA_WRONG_RESPONSE_ERROR_MESSAGE, $message . ' Comment submission should be blocked.', 'CAPTCHA');

    // Get node page and check that comment is not present.
    $this
      ->drupalGet('node/' . $node
      ->id());
    $this
      ->assertSession()
      ->pageTextNotContains($comment_subject, $message . ' Comment should not show up on node page.', 'CAPTCHA');
    $this
      ->assertSession()
      ->pageTextNotContains($comment_body, $message . ' Comment should not show up on node page.', 'CAPTCHA');
  }
}