3 namespace Drupal\Tests\comment\Functional;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\user\RoleInterface;
9 * Tests comment block functionality.
13 class CommentBlockTest extends CommentTestBase {
20 public static $modules = ['block', 'views'];
22 protected function setUp() {
24 // Update admin user to have the 'administer blocks' permission.
25 $this->adminUser = $this->drupalCreateUser([
26 'administer content types',
27 'administer comments',
28 'skip comment approval',
37 * Tests the recent comments block.
39 public function testRecentCommentBlock() {
40 $this->drupalLogin($this->adminUser);
41 $block = $this->drupalPlaceBlock('views_block:comments_recent-block_1');
43 // Add some test comments, with and without subjects. Because the 10 newest
44 // comments should be shown by the block, we create 11 to test that behavior
46 $timestamp = REQUEST_TIME;
47 for ($i = 0; $i < 11; ++$i) {
48 $subject = ($i % 2) ? $this->randomMachineName() : '';
49 $comments[$i] = $this->postComment($this->node, $this->randomMachineName(), $subject);
50 $comments[$i]->created->value = $timestamp--;
51 $comments[$i]->save();
54 // Test that a user without the 'access comments' permission cannot see the
56 $this->drupalLogout();
57 user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['access comments']);
59 $this->assertNoText(t('Recent comments'));
60 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access comments']);
62 // Test that a user with the 'access comments' permission can see the
64 $this->drupalLogin($this->webUser);
66 $this->assertText(t('Recent comments'));
68 // Test the only the 10 latest comments are shown and in the proper order.
69 $this->assertNoText($comments[10]->getSubject(), 'Comment 11 not found in block.');
70 for ($i = 0; $i < 10; $i++) {
71 $this->assertText($comments[$i]->getSubject(), new FormattableMarkup('Comment @number found in block.', ['@number' => 10 - $i]));
73 $previous_position = $position;
74 $position = strpos($this->getSession()->getPage()->getContent(), $comments[$i]->getSubject());
75 $this->assertTrue($position > $previous_position, new FormattableMarkup('Comment @a appears after comment @b', ['@a' => 10 - $i, '@b' => 11 - $i]));
77 $position = strpos($this->getSession()->getPage()->getContent(), $comments[$i]->getSubject());
80 // Test that links to comments work when comments are across pages.
81 $this->setCommentsPerPage(1);
83 for ($i = 0; $i < 10; $i++) {
84 $this->clickLink($comments[$i]->getSubject());
85 $this->assertText($comments[$i]->getSubject(), 'Comment link goes to correct page.');
86 $this->assertRaw('<link rel="canonical"', 'Canonical URL was found in the HTML head');