db backup prior to drupal security update
[yaffs-website] / web / core / modules / comment / src / Tests / CommentCSSTest.php
1 <?php
2
3 namespace Drupal\comment\Tests;
4
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\comment\CommentInterface;
7 use Drupal\user\RoleInterface;
8 use Drupal\comment\Entity\Comment;
9
10 /**
11  * Tests CSS classes on comments.
12  *
13  * @group comment
14  */
15 class CommentCSSTest extends CommentTestBase {
16
17   protected function setUp() {
18     parent::setUp();
19
20     // Allow anonymous users to see comments.
21     user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
22       'access comments',
23       'access content'
24     ]);
25   }
26
27   /**
28    * Tests CSS classes on comments.
29    */
30   public function testCommentClasses() {
31     // Create all permutations for comments, users, and nodes.
32     $parameters = [
33       'node_uid' => [0, $this->webUser->id()],
34       'comment_uid' => [0, $this->webUser->id(), $this->adminUser->id()],
35       'comment_status' => [CommentInterface::PUBLISHED, CommentInterface::NOT_PUBLISHED],
36       'user' => ['anonymous', 'authenticated', 'admin'],
37     ];
38     $permutations = $this->generatePermutations($parameters);
39
40     foreach ($permutations as $case) {
41       // Create a new node.
42       $node = $this->drupalCreateNode(['type' => 'article', 'uid' => $case['node_uid']]);
43
44       // Add a comment.
45       /** @var \Drupal\comment\CommentInterface $comment */
46       $comment = Comment::create([
47         'entity_id' => $node->id(),
48         'entity_type' => 'node',
49         'field_name' => 'comment',
50         'uid' => $case['comment_uid'],
51         'status' => $case['comment_status'],
52         'subject' => $this->randomMachineName(),
53         'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
54         'comment_body' => [LanguageInterface::LANGCODE_NOT_SPECIFIED => [$this->randomMachineName()]],
55       ]);
56       $comment->save();
57
58       // Adjust the current/viewing user.
59       switch ($case['user']) {
60         case 'anonymous':
61           if ($this->loggedInUser) {
62             $this->drupalLogout();
63           }
64           $case['user_uid'] = 0;
65           break;
66
67         case 'authenticated':
68           $this->drupalLogin($this->webUser);
69           $case['user_uid'] = $this->webUser->id();
70           break;
71
72         case 'admin':
73           $this->drupalLogin($this->adminUser);
74           $case['user_uid'] = $this->adminUser->id();
75           break;
76       }
77       // Request the node with the comment.
78       $this->drupalGet('node/' . $node->id());
79       $settings = $this->getDrupalSettings();
80
81       // Verify the data-history-node-id attribute, which is necessary for the
82       // by-viewer class and the "new" indicator, see below.
83       $this->assertIdentical(1, count($this->xpath('//*[@data-history-node-id="' . $node->id() . '"]')), 'data-history-node-id attribute is set on node.');
84
85       // Verify classes if the comment is visible for the current user.
86       if ($case['comment_status'] == CommentInterface::PUBLISHED || $case['user'] == 'admin') {
87         // Verify the by-anonymous class.
88         $comments = $this->xpath('//*[contains(@class, "comment") and contains(@class, "by-anonymous")]');
89         if ($case['comment_uid'] == 0) {
90           $this->assertTrue(count($comments) == 1, 'by-anonymous class found.');
91         }
92         else {
93           $this->assertFalse(count($comments), 'by-anonymous class not found.');
94         }
95
96         // Verify the by-node-author class.
97         $comments = $this->xpath('//*[contains(@class, "comment") and contains(@class, "by-node-author")]');
98         if ($case['comment_uid'] > 0 && $case['comment_uid'] == $case['node_uid']) {
99           $this->assertTrue(count($comments) == 1, 'by-node-author class found.');
100         }
101         else {
102           $this->assertFalse(count($comments), 'by-node-author class not found.');
103         }
104
105         // Verify the data-comment-user-id attribute, which is used by the
106         // drupal.comment-by-viewer library to add a by-viewer when the current
107         // user (the viewer) was the author of the comment. We do this in Java-
108         // Script to prevent breaking the render cache.
109         $this->assertIdentical(1, count($this->xpath('//*[contains(@class, "comment") and @data-comment-user-id="' . $case['comment_uid'] . '"]')), 'data-comment-user-id attribute is set on comment.');
110         $this->assertRaw(drupal_get_path('module', 'comment') . '/js/comment-by-viewer.js', 'drupal.comment-by-viewer library is present.');
111       }
112
113       // Verify the unpublished class.
114       $comments = $this->xpath('//*[contains(@class, "comment") and contains(@class, "unpublished")]');
115       if ($case['comment_status'] == CommentInterface::NOT_PUBLISHED && $case['user'] == 'admin') {
116         $this->assertTrue(count($comments) == 1, 'unpublished class found.');
117       }
118       else {
119         $this->assertFalse(count($comments), 'unpublished class not found.');
120       }
121
122       // Verify the data-comment-timestamp attribute, which is used by the
123       // drupal.comment-new-indicator library to add a "new" indicator to each
124       // comment that was created or changed after the last time the current
125       // user read the corresponding node.
126       if ($case['comment_status'] == CommentInterface::PUBLISHED || $case['user'] == 'admin') {
127         $this->assertIdentical(1, count($this->xpath('//*[contains(@class, "comment")]/*[@data-comment-timestamp="' . $comment->getChangedTime() . '"]')), 'data-comment-timestamp attribute is set on comment');
128         $expectedJS = ($case['user'] !== 'anonymous');
129         $this->assertIdentical($expectedJS, isset($settings['ajaxPageState']['libraries']) && in_array('comment/drupal.comment-new-indicator', explode(',', $settings['ajaxPageState']['libraries'])), 'drupal.comment-new-indicator library is present.');
130       }
131     }
132   }
133
134 }