3 namespace Drupal\Tests\comment\Functional;
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\comment\CommentInterface;
7 use Drupal\user\RoleInterface;
8 use Drupal\comment\Entity\Comment;
9 use Drupal\Tests\Traits\Core\GeneratePermutationsTrait;
12 * Tests CSS classes on comments.
16 class CommentCSSTest extends CommentTestBase {
18 use GeneratePermutationsTrait;
20 protected function setUp() {
23 // Allow anonymous users to see comments.
24 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
31 * Tests CSS classes on comments.
33 public function testCommentClasses() {
34 // Create all permutations for comments, users, and nodes.
36 'node_uid' => [0, $this->webUser->id()],
37 'comment_uid' => [0, $this->webUser->id(), $this->adminUser->id()],
38 'comment_status' => [CommentInterface::PUBLISHED, CommentInterface::NOT_PUBLISHED],
39 'user' => ['anonymous', 'authenticated', 'admin'],
41 $permutations = $this->generatePermutations($parameters);
43 foreach ($permutations as $case) {
45 $node = $this->drupalCreateNode(['type' => 'article', 'uid' => $case['node_uid']]);
48 /** @var \Drupal\comment\CommentInterface $comment */
49 $comment = Comment::create([
50 'entity_id' => $node->id(),
51 'entity_type' => 'node',
52 'field_name' => 'comment',
53 'uid' => $case['comment_uid'],
54 'status' => $case['comment_status'],
55 'subject' => $this->randomMachineName(),
56 'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
57 'comment_body' => [LanguageInterface::LANGCODE_NOT_SPECIFIED => [$this->randomMachineName()]],
61 // Adjust the current/viewing user.
62 switch ($case['user']) {
64 if ($this->loggedInUser) {
65 $this->drupalLogout();
67 $case['user_uid'] = 0;
71 $this->drupalLogin($this->webUser);
72 $case['user_uid'] = $this->webUser->id();
76 $this->drupalLogin($this->adminUser);
77 $case['user_uid'] = $this->adminUser->id();
80 // Request the node with the comment.
81 $this->drupalGet('node/' . $node->id());
82 $settings = $this->getDrupalSettings();
84 // Verify the data-history-node-id attribute, which is necessary for the
85 // by-viewer class and the "new" indicator, see below.
86 $this->assertIdentical(1, count($this->xpath('//*[@data-history-node-id="' . $node->id() . '"]')), 'data-history-node-id attribute is set on node.');
88 // Verify classes if the comment is visible for the current user.
89 if ($case['comment_status'] == CommentInterface::PUBLISHED || $case['user'] == 'admin') {
90 // Verify the by-anonymous class.
91 $comments = $this->xpath('//*[contains(@class, "comment") and contains(@class, "by-anonymous")]');
92 if ($case['comment_uid'] == 0) {
93 $this->assertTrue(count($comments) == 1, 'by-anonymous class found.');
96 $this->assertFalse(count($comments), 'by-anonymous class not found.');
99 // Verify the by-node-author class.
100 $comments = $this->xpath('//*[contains(@class, "comment") and contains(@class, "by-node-author")]');
101 if ($case['comment_uid'] > 0 && $case['comment_uid'] == $case['node_uid']) {
102 $this->assertTrue(count($comments) == 1, 'by-node-author class found.');
105 $this->assertFalse(count($comments), 'by-node-author class not found.');
108 // Verify the data-comment-user-id attribute, which is used by the
109 // drupal.comment-by-viewer library to add a by-viewer when the current
110 // user (the viewer) was the author of the comment. We do this in Java-
111 // Script to prevent breaking the render cache.
112 $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.');
113 $this->assertRaw(drupal_get_path('module', 'comment') . '/js/comment-by-viewer.js', 'drupal.comment-by-viewer library is present.');
116 // Verify the unpublished class.
117 $comments = $this->xpath('//*[contains(@class, "comment") and contains(@class, "unpublished")]');
118 if ($case['comment_status'] == CommentInterface::NOT_PUBLISHED && $case['user'] == 'admin') {
119 $this->assertTrue(count($comments) == 1, 'unpublished class found.');
122 $this->assertFalse(count($comments), 'unpublished class not found.');
125 // Verify the data-comment-timestamp attribute, which is used by the
126 // drupal.comment-new-indicator library to add a "new" indicator to each
127 // comment that was created or changed after the last time the current
128 // user read the corresponding node.
129 if ($case['comment_status'] == CommentInterface::PUBLISHED || $case['user'] == 'admin') {
130 $this->assertIdentical(1, count($this->xpath('//*[contains(@class, "comment")]/*[@data-comment-timestamp="' . $comment->getChangedTime() . '"]')), 'data-comment-timestamp attribute is set on comment');
131 $expectedJS = ($case['user'] !== 'anonymous');
132 $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.');