1 comment.test CommentBlockFunctionalTest::testRecentCommentBlock()

Tests the recent comments block.

File

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

Class

CommentBlockFunctionalTest
Tests the Comment module blocks.

Code

function testRecentCommentBlock() {
  $this->backdropLogin($this->admin_user);
  $this->setCommentSubject(TRUE);

  // Add the block to the sidebar region in the default layout.
  $layout_name = 'default';
  $region = 'sidebar';
  $block = array(
    'module' => 'comment',
    'delta' => 'recent',
  );
  $block_title = $this->randomName(16);
  $edit = array(
    'region' => $region,
    'title' => $block_title,
    'title_display' => LAYOUT_TITLE_CUSTOM,
    'block_settings[comment_count]' => 2,
  );
  $this->backdropPost('admin/structure/layouts/manage/' . $layout_name . '/add-block/editor/' . $region . '/' . $block['module'] . ':' . $block['delta'], $edit, t('Add block'));
  $this->backdropPost('admin/structure/layouts/manage/' . $layout_name, array(), t('Save layout'));

  // Add some test comments, one without a subject.
  $comment1 = $this->postComment($this->node, $this->randomName(), $this->randomName());
  $comment2 = $this->postComment($this->node, $this->randomName(), $this->randomName());
  $comment3 = $this->postComment($this->node, $this->randomName());

  // Test that a user without the 'access comments' permission cannot see the
  // block.
  $this->backdropLogout();
  user_role_revoke_permissions(BACKDROP_ANONYMOUS_ROLE, array('access comments'));
  // Clear the page cache.
  cache_clear_all();
  $this->backdropGet('');
  $this->assertNoText($block_title, 'Block was not found.');
  user_role_grant_permissions(BACKDROP_ANONYMOUS_ROLE, array('access comments'));

  // Test that a user with the 'access comments' permission can see the
  // block.
  $this->backdropLogin($this->web_user);
  $this->backdropGet('');
  $this->assertText($block_title, 'Block was found.');

  // Test the only the 2 latest comments are shown and in the proper order.
  $this->assertNoText($comment1->subject, 'Comment not found in block.');
  $this->assertText($comment2->subject, 'Comment found in block.');
  $this->assertText($comment3->comment, 'Comment found in block.');
  $this->assertTrue(strpos($this->backdropGetContent(), $comment3->comment) < strpos($this->backdropGetContent(), $comment2->subject), 'Comments were ordered correctly in block.');

  // Set the number of recent comments to show to 10.
  $this->backdropLogout();
  $this->backdropLogin($this->admin_user);
  $edit = array(
    'block_settings[comment_count]' => 10,
  );
  $this->backdropPost('admin/structure/layouts/manage/' . $layout_name . '/add-block/editor/' . $region . '/' . $block['module'] . ':' . $block['delta'], $edit, t('Add block'));
  $this->backdropPost('admin/structure/layouts/manage/' . $layout_name, array(), t('Save layout'));

  // Post an additional comment.
  $comment4 = $this->postComment($this->node, $this->randomName(), $this->randomName());

  // Test that all four comments are shown.
  $this->assertText($comment1->subject, 'Comment found in block.');
  $this->assertText($comment2->subject, 'Comment found in block.');
  $this->assertText($comment3->comment, 'Comment found in block.');
  $this->assertText($comment4->subject, 'Comment found in block.');

  // Test that links to comments work when comments are across pages.
  $this->setCommentsPerPage(1);
  $this->backdropGet('');
  $this->clickLink($comment1->subject);
  $this->assertText($comment1->subject, 'Comment link goes to correct page.');
  $this->backdropGet('');
  $this->clickLink($comment2->subject);
  $this->assertText($comment2->subject, 'Comment link goes to correct page.');
  $this->clickLink($comment4->subject);
  $this->assertText($comment4->subject, 'Comment link goes to correct page.');
  // Check that when viewing a comment page from a link to the comment, that
  // rel="canonical" is added to the head of the document.
  $this->assertRaw('<link rel="canonical"', 'Canonical URL was found in the HTML head');
}