1 comment.test CommentAnonymous::testAnonymous()

Tests anonymous comment functionality.

File

core/modules/comment/tests/comment.test, line 1138
Tests for the Comment module.

Class

CommentAnonymous
Tests anonymous commenting.

Code

function testAnonymous() {
  $this->backdropLogin($this->admin_user);
  // Enabled anonymous user comments.
  user_role_change_permissions(BACKDROP_ANONYMOUS_ROLE, array(
    'access comments' => TRUE,
    'post comments' => TRUE,
    'skip comment approval' => TRUE,
  ));
  $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
  $this->backdropLogout();

  // Post anonymous comment without contact info.
  $anonymous_comment1 = $this->postComment($this->node, $this->randomName());
  $this->assertTrue($this->commentExists($anonymous_comment1), 'Anonymous comment without contact info found.');

  // Allow contact info.
  $this->backdropLogin($this->admin_user);
  $this->setCommentAnonymous('1');

  // Attempt to edit anonymous comment.
  $this->backdropGet('comment/' . $anonymous_comment1->id . '/edit');
  $edited_comment = $this->postComment(NULL, $this->randomName());
  $this->assertTrue($this->commentExists($edited_comment, FALSE), 'Modified reply found.');
  $this->backdropLogout();

  // Post anonymous comment with contact info (optional).
  $this->backdropGet('comment/reply/' . $this->node->nid);
  $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');

  $anonymous_comment2 = $this->postComment($this->node, $this->randomName());
  $this->assertTrue($this->commentExists($anonymous_comment2), 'Anonymous comment with contact info (optional) found.');

  // Ensure anonymous users cannot post in the name of registered users.
  $langcode = LANGUAGE_NONE;
  $edit = array(
    'name' => $this->admin_user->name,
    'mail' => $this->randomName() . '@example.com',
    'subject' => $this->randomName(),
    "comment_body[$langcode][0][value]" => $this->randomName(),
  );
  $this->setCommentSubject(TRUE);

  $this->backdropPost('comment/reply/' . $this->node->nid, $edit, t('Save'));
  $this->assertText(t('The name you used belongs to a registered user.'));

  // Require contact info.
  $this->backdropLogin($this->admin_user);
  $this->setCommentAnonymous('2');
  $this->backdropLogout();

  // Try to post comment with contact info (required).
  $this->backdropGet('comment/reply/' . $this->node->nid);
  $this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');

  $anonymous_comment3 = $this->postComment($this->node, $this->randomName(), '', TRUE);
  // Name should have 'Anonymous' for value by default.
  $this->assertText(t('E-mail field is required.'), 'E-mail required.');
  $this->assertFalse($this->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) not found.');

  // Post comment with contact info (required).
  $author_name = $this->randomName();
  $author_mail = $this->randomName() . '@example.com';
  $anonymous_comment3 = $this->postComment($this->node, $this->randomName(), '', array('name' => $author_name, 'mail' => $author_mail));
  $this->assertTrue($this->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) found.');

  // Make sure the user data appears correctly when editing the comment.
  $this->backdropLogin($this->admin_user);
  $this->backdropGet('comment/' . $anonymous_comment3->id . '/edit');
  $this->assertRaw($author_name, "The anonymous user's name is correct when editing the comment.");
  $this->assertRaw($author_mail, "The anonymous user's e-mail address is correct when editing the comment.");

  // Unpublish comment.
  $this->performCommentOperation($anonymous_comment3, 'unpublish');

  $this->backdropGet('admin/content/comment/approval');
  $this->assertRaw('comments[' . $anonymous_comment3->id . ']', 'Comment was unpublished.');

  // Publish comment.
  $this->performCommentOperation($anonymous_comment3, 'publish', TRUE);

  $this->backdropGet('admin/content/comment');
  $this->assertRaw('comments[' . $anonymous_comment3->id . ']', 'Comment was published.');

  // Delete comment.
  $this->performCommentOperation($anonymous_comment3, 'delete');

  $this->backdropGet('admin/content/comment');
  $this->assertNoRaw('comments[' . $anonymous_comment3->id . ']', 'Comment was deleted.');
  $this->backdropLogout();

  // Reset.
  user_role_change_permissions(BACKDROP_ANONYMOUS_ROLE, array(
    'access comments' => FALSE,
    'post comments' => FALSE,
    'skip comment approval' => FALSE,
  ));

  // Attempt to view comments while disallowed.
  // NOTE: if authenticated user has permission to post comments, then a
  // "Login or register to post comments" type link may be shown.
  $this->backdropGet('node/' . $this->node->nid);
  $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
  $this->assertNoLink('Add comment', 'Link to add comment was found.');

  // Attempt to view node-comment form while disallowed.
  $this->backdropGet('comment/reply/' . $this->node->nid);
  $this->assertText('You are not authorized to post comments', 'Error attempting to post comment.');
  $this->assertNoFieldByName('subject', '', 'Title field not found.');
  $this->assertNoFieldByName("comment_body[$langcode][0][value]", '', 'Comment field not found.');

  user_role_change_permissions(BACKDROP_ANONYMOUS_ROLE, array(
    'access comments' => TRUE,
    'post comments' => FALSE,
    'skip comment approval' => FALSE,
  ));
  $this->backdropGet('node/' . $this->node->nid);
  $this->assertPattern('@<h2[^>]*>Comments</h2>@', 'Comments were displayed.');
  $this->assertLink('Log in', 1, 'Link to log in was found.');
  $this->assertLink('register', 1, 'Link to register was found.');

  user_role_change_permissions(BACKDROP_ANONYMOUS_ROLE, array(
    'access comments' => FALSE,
    'post comments' => TRUE,
    'skip comment approval' => TRUE,
  ));
  $this->backdropGet('node/' . $this->node->nid);
  $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
  $this->assertFieldByName('subject', '', 'Title field found.');
  $this->assertFieldByName("comment_body[$langcode][0][value]", '', 'Comment field found.');

  $this->backdropGet('comment/reply/' . $this->node->nid . '/' . $anonymous_comment3->id);
  $this->assertText('You are not authorized to view comments', 'Error attempting to post reply.');
  $this->assertNoText($author_name, 'Comment not displayed.');
}