You are here

public function ProtectedSubmissionsTest::testNodeCommentRejectedSubmissionPrintsCorrectMessage in Protected Submissions 8

File

tests/src/Functional/ProtectedSubmissionsTest.php, line 33

Class

ProtectedSubmissionsTest

Namespace

Drupal\Tests\protected_submissions\Functional

Code

public function testNodeCommentRejectedSubmissionPrintsCorrectMessage() {

  // Create an article content type (if it doesn't exist already).
  $types = NodeType::loadMultiple();
  if (empty($types['article'])) {
    $this
      ->drupalCreateContentType([
      'type' => 'article',
      'name' => $this
        ->t('Article'),
    ]);
  }

  // Create comment field on article.
  $this
    ->addDefaultCommentField('node', 'article');

  // Create an article node.
  $this
    ->drupalCreateNode([
    'title' => $this
      ->t('An Article'),
    'type' => 'article',
  ])
    ->save();

  // Allow anonymous users to post comments.
  $permissions = [
    'access content',
    'access comments',
    'post comments',
    'skip comment approval',
  ];
  user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, $permissions);

  // Add a custom string to the reject patterns list.
  $rejectPatterns = $this
    ->config('protected_submissions.settings')
    ->get('protected_submissions.reject_patterns');
  $inputString = ' monkey';
  $rejectPatterns .= ',' . $inputString;
  $this
    ->config('protected_submissions.settings')
    ->set('protected_submissions.reject_patterns', $rejectPatterns)
    ->save();

  // Attempt to post a spam comment.
  $input = [
    'edit-comment-body-0-value' => $inputString,
  ];
  $this
    ->drupalPostForm('node/1', $input, $this
    ->t('Save'));

  // Ensure we get a 200 response.
  $this
    ->assertResponse(Response::HTTP_OK);

  // Ensure the rejected message is displayed.
  $message = $this
    ->config('protected_submissions.settings')
    ->get('protected_submissions.reject_message');
  $this
    ->assertSession()
    ->responseContains($message);
  $this
    ->assertSession()
    ->responseNotContains('Your comment has been posted.');

  // Attempt to post a normal comment.
  $input = [
    'edit-comment-body-0-value' => 'a normal comment',
  ];
  $this
    ->drupalPostForm('node/1', $input, $this
    ->t('Save'));

  // Ensure we get a 200 response.
  $this
    ->assertResponse(Response::HTTP_OK);

  // Ensure our normal post was posted.
  $this
    ->assertSession()
    ->responseContains('Your comment has been posted.');
  $this
    ->assertSession()
    ->responseContains('a normal comment');
}