You are here

function CommentModuleTestCase::testAnonymous in SimpleTest 6

Test anonymous comment functionality.

File

tests/comment_module.test, line 103

Class

CommentModuleTestCase

Code

function testAnonymous() {
  $this
    ->drupalLoginUser($this->admin_user);

  // Enabled anonymous user comments.
  $this
    ->set_anonymous_user_comment(TRUE, TRUE);
  $this
    ->set_comment_anonymous('0');

  // Ensure that doesn't require contact info.
  $this
    ->drupalGet('logout');

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

  // Allow contact info.
  $this
    ->drupalLoginUser($this->admin_user);
  $this
    ->set_comment_anonymous('1');
  $this
    ->drupalGet('logout');

  // Post anonymous comment with contact info (optional).
  $this
    ->drupalGet('comment/reply/' . $this->node->nid);
  $this
    ->assertTrue($this
    ->comment_contact_info_available(), 'Contact information available.');
  $anonymous_comment2 = $this
    ->post_comment($this->node, $this
    ->randomName(), $this
    ->randomName());
  $this
    ->assertTrue($this
    ->comment_exists($anonymous_comment2), 'Anonymous comment with contact info (optional) found.');

  // Require contact info.
  $this
    ->drupalLoginUser($this->admin_user);
  $this
    ->set_comment_anonymous('2');
  $this
    ->drupalGet('logout');

  // Try to post comment with contact info (required).
  $this
    ->drupalGet('comment/reply/' . $this->node->nid);
  $this
    ->assertTrue($this
    ->comment_contact_info_available(), 'Contact information available.');
  $anonymous_comment3 = $this
    ->post_comment($this->node, $this
    ->randomName(), $this
    ->randomName(), TRUE, TRUE);
  $this
    ->assertText(t('E-mail field is required.'), 'E-mail required.');

  // Name should have 'Anonymous' for value by default.
  $this
    ->assertFalse($this
    ->comment_exists($anonymous_comment3), 'Anonymous comment with contact info (required) not found.');

  // Post comment with contact info (required).
  $anonymous_comment3 = $this
    ->post_comment($this->node, $this
    ->randomName(), $this
    ->randomName(), TRUE, array(
    'mail' => 'tester@simpletest.org',
  ));
  $this
    ->assertTrue($this
    ->comment_exists($anonymous_comment3), 'Anonymous comment with contact info (required) found.');

  // Unpublish comment.
  $this
    ->drupalLoginUser($this->admin_user);
  $this
    ->perform_comment_operation($anonymous_comment3, 'unpublish');
  $this
    ->drupalGet('admin/content/comment/approval');
  $this
    ->assertWantedRaw('comments[' . $anonymous_comment3->id . ']', 'Comment was unpublished.');

  // Publish comment.
  $this
    ->perform_comment_operation($anonymous_comment3, 'publish', TRUE);
  $this
    ->drupalGet('admin/content/comment');
  $this
    ->assertWantedRaw('comments[' . $anonymous_comment3->id . ']', 'Comment was published.');

  // Delete comment.
  $this
    ->perform_comment_operation($anonymous_comment3, 'delete');
  $this
    ->drupalGet('admin/content/comment');
  $this
    ->assertNoUnwantedRaw('comments[' . $anonymous_comment3->id . ']', 'Comment was deleted.');

  // Set anonymouse comments to require approval.
  $this
    ->set_anonymous_user_comment(TRUE, FALSE);
  $this
    ->set_comment_anonymous('0');

  // Ensure that doesn't require contact info.
  $this
    ->drupalGet('logout');

  // Post anonymous comment without contact info.
  $subject = $this
    ->randomName();
  $body = $this
    ->randomName();
  $this
    ->post_comment($this->node, $subject, $body, TRUE, TRUE);

  // Set $contact to true so that it won't check for id and message.
  $this
    ->assertText(t('Your comment has been queued for moderation by site administrators and will be published after approval.'), 'Comment requires approval.');

  // Get unaproved comment id.
  $this
    ->drupalLoginUser($this->admin_user);
  $anonymous_comment4 = $this
    ->get_unaproved_comment($subject);
  $anonymous_comment4 = (object) array(
    'id' => $anonymous_comment4,
    'subject' => $subject,
    'comment' => $body,
  );
  $this
    ->drupalGet('logout');
  $this
    ->assertFalse($this
    ->comment_exists($anonymous_comment4), 'Anonymous comment was not published.');

  // Approve comment.
  $this
    ->drupalLoginUser($this->admin_user);
  $this
    ->perform_comment_operation($anonymous_comment4, 'publish', TRUE);
  $this
    ->drupalGet('logout');
  $this
    ->drupalGet('node/' . $this->node->nid);
  $this
    ->assertTrue($this
    ->comment_exists($anonymous_comment4), 'Anonymous comment visible.');

  // Reset.
  $this
    ->drupalLoginUser($this->admin_user);
  $this
    ->set_anonymous_user_comment(FALSE, FALSE);
}