annotate core/modules/comment/tests/src/Functional/CommentBlockTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\comment\Functional;
Chris@0 4
Chris@17 5 use Drupal\Component\Render\FormattableMarkup;
Chris@0 6 use Drupal\user\RoleInterface;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Tests comment block functionality.
Chris@0 10 *
Chris@0 11 * @group comment
Chris@0 12 */
Chris@0 13 class CommentBlockTest extends CommentTestBase {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Modules to install.
Chris@0 17 *
Chris@0 18 * @var array
Chris@0 19 */
Chris@0 20 public static $modules = ['block', 'views'];
Chris@0 21
Chris@0 22 protected function setUp() {
Chris@0 23 parent::setUp();
Chris@0 24 // Update admin user to have the 'administer blocks' permission.
Chris@0 25 $this->adminUser = $this->drupalCreateUser([
Chris@0 26 'administer content types',
Chris@0 27 'administer comments',
Chris@0 28 'skip comment approval',
Chris@0 29 'post comments',
Chris@0 30 'access comments',
Chris@0 31 'access content',
Chris@0 32 'administer blocks',
Chris@0 33 ]);
Chris@0 34 }
Chris@0 35
Chris@0 36 /**
Chris@0 37 * Tests the recent comments block.
Chris@0 38 */
Chris@0 39 public function testRecentCommentBlock() {
Chris@0 40 $this->drupalLogin($this->adminUser);
Chris@0 41 $block = $this->drupalPlaceBlock('views_block:comments_recent-block_1');
Chris@0 42
Chris@0 43 // Add some test comments, with and without subjects. Because the 10 newest
Chris@0 44 // comments should be shown by the block, we create 11 to test that behavior
Chris@0 45 // below.
Chris@0 46 $timestamp = REQUEST_TIME;
Chris@0 47 for ($i = 0; $i < 11; ++$i) {
Chris@0 48 $subject = ($i % 2) ? $this->randomMachineName() : '';
Chris@0 49 $comments[$i] = $this->postComment($this->node, $this->randomMachineName(), $subject);
Chris@0 50 $comments[$i]->created->value = $timestamp--;
Chris@0 51 $comments[$i]->save();
Chris@0 52 }
Chris@0 53
Chris@0 54 // Test that a user without the 'access comments' permission cannot see the
Chris@0 55 // block.
Chris@0 56 $this->drupalLogout();
Chris@0 57 user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['access comments']);
Chris@0 58 $this->drupalGet('');
Chris@0 59 $this->assertNoText(t('Recent comments'));
Chris@0 60 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access comments']);
Chris@0 61
Chris@0 62 // Test that a user with the 'access comments' permission can see the
Chris@0 63 // block.
Chris@0 64 $this->drupalLogin($this->webUser);
Chris@0 65 $this->drupalGet('');
Chris@0 66 $this->assertText(t('Recent comments'));
Chris@0 67
Chris@0 68 // Test the only the 10 latest comments are shown and in the proper order.
Chris@0 69 $this->assertNoText($comments[10]->getSubject(), 'Comment 11 not found in block.');
Chris@0 70 for ($i = 0; $i < 10; $i++) {
Chris@17 71 $this->assertText($comments[$i]->getSubject(), new FormattableMarkup('Comment @number found in block.', ['@number' => 10 - $i]));
Chris@0 72 if ($i > 1) {
Chris@0 73 $previous_position = $position;
Chris@17 74 $position = strpos($this->getSession()->getPage()->getContent(), $comments[$i]->getSubject());
Chris@17 75 $this->assertTrue($position > $previous_position, new FormattableMarkup('Comment @a appears after comment @b', ['@a' => 10 - $i, '@b' => 11 - $i]));
Chris@0 76 }
Chris@17 77 $position = strpos($this->getSession()->getPage()->getContent(), $comments[$i]->getSubject());
Chris@0 78 }
Chris@0 79
Chris@0 80 // Test that links to comments work when comments are across pages.
Chris@0 81 $this->setCommentsPerPage(1);
Chris@0 82
Chris@0 83 for ($i = 0; $i < 10; $i++) {
Chris@0 84 $this->clickLink($comments[$i]->getSubject());
Chris@0 85 $this->assertText($comments[$i]->getSubject(), 'Comment link goes to correct page.');
Chris@0 86 $this->assertRaw('<link rel="canonical"', 'Canonical URL was found in the HTML head');
Chris@0 87 }
Chris@0 88 }
Chris@0 89
Chris@0 90 }