Chris@0: adminUser = $this->drupalCreateUser([ Chris@0: 'administer content types', Chris@0: 'administer comments', Chris@0: 'skip comment approval', Chris@0: 'post comments', Chris@0: 'access comments', Chris@0: 'access content', Chris@0: 'administer blocks', Chris@0: ]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the recent comments block. Chris@0: */ Chris@0: public function testRecentCommentBlock() { Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $block = $this->drupalPlaceBlock('views_block:comments_recent-block_1'); Chris@0: Chris@0: // Add some test comments, with and without subjects. Because the 10 newest Chris@0: // comments should be shown by the block, we create 11 to test that behavior Chris@0: // below. Chris@0: $timestamp = REQUEST_TIME; Chris@0: for ($i = 0; $i < 11; ++$i) { Chris@0: $subject = ($i % 2) ? $this->randomMachineName() : ''; Chris@0: $comments[$i] = $this->postComment($this->node, $this->randomMachineName(), $subject); Chris@0: $comments[$i]->created->value = $timestamp--; Chris@0: $comments[$i]->save(); Chris@0: } Chris@0: Chris@0: // Test that a user without the 'access comments' permission cannot see the Chris@0: // block. Chris@0: $this->drupalLogout(); Chris@0: user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['access comments']); Chris@0: $this->drupalGet(''); Chris@0: $this->assertNoText(t('Recent comments')); Chris@0: user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access comments']); Chris@0: Chris@0: // Test that a user with the 'access comments' permission can see the Chris@0: // block. Chris@0: $this->drupalLogin($this->webUser); Chris@0: $this->drupalGet(''); Chris@0: $this->assertText(t('Recent comments')); Chris@0: Chris@0: // Test the only the 10 latest comments are shown and in the proper order. Chris@0: $this->assertNoText($comments[10]->getSubject(), 'Comment 11 not found in block.'); Chris@0: for ($i = 0; $i < 10; $i++) { Chris@17: $this->assertText($comments[$i]->getSubject(), new FormattableMarkup('Comment @number found in block.', ['@number' => 10 - $i])); Chris@0: if ($i > 1) { Chris@0: $previous_position = $position; Chris@17: $position = strpos($this->getSession()->getPage()->getContent(), $comments[$i]->getSubject()); Chris@17: $this->assertTrue($position > $previous_position, new FormattableMarkup('Comment @a appears after comment @b', ['@a' => 10 - $i, '@b' => 11 - $i])); Chris@0: } Chris@17: $position = strpos($this->getSession()->getPage()->getContent(), $comments[$i]->getSubject()); Chris@0: } Chris@0: Chris@0: // Test that links to comments work when comments are across pages. Chris@0: $this->setCommentsPerPage(1); Chris@0: Chris@0: for ($i = 0; $i < 10; $i++) { Chris@0: $this->clickLink($comments[$i]->getSubject()); Chris@0: $this->assertText($comments[$i]->getSubject(), 'Comment link goes to correct page.'); Chris@0: $this->assertRaw('