Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / comment / tests / src / Functional / CommentBlockTest.php
1 <?php
2
3 namespace Drupal\Tests\comment\Functional;
4
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\user\RoleInterface;
7
8 /**
9  * Tests comment block functionality.
10  *
11  * @group comment
12  */
13 class CommentBlockTest extends CommentTestBase {
14
15   /**
16    * Modules to install.
17    *
18    * @var array
19    */
20   public static $modules = ['block', 'views'];
21
22   protected function setUp() {
23     parent::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',
29       'post comments',
30       'access comments',
31       'access content',
32       'administer blocks',
33      ]);
34   }
35
36   /**
37    * Tests the recent comments block.
38    */
39   public function testRecentCommentBlock() {
40     $this->drupalLogin($this->adminUser);
41     $block = $this->drupalPlaceBlock('views_block:comments_recent-block_1');
42
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
45     // below.
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();
52     }
53
54     // Test that a user without the 'access comments' permission cannot see the
55     // block.
56     $this->drupalLogout();
57     user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['access comments']);
58     $this->drupalGet('');
59     $this->assertNoText(t('Recent comments'));
60     user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access comments']);
61
62     // Test that a user with the 'access comments' permission can see the
63     // block.
64     $this->drupalLogin($this->webUser);
65     $this->drupalGet('');
66     $this->assertText(t('Recent comments'));
67
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]));
72       if ($i > 1) {
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]));
76       }
77       $position = strpos($this->getSession()->getPage()->getContent(), $comments[$i]->getSubject());
78     }
79
80     // Test that links to comments work when comments are across pages.
81     $this->setCommentsPerPage(1);
82
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');
87     }
88   }
89
90 }