3 namespace Drupal\comment\Tests;
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\comment\CommentInterface;
7 use Drupal\user\RoleInterface;
8 use Drupal\comment\Entity\Comment;
11 * Tests CSS classes on comments.
15 class CommentCSSTest extends CommentTestBase {
17 protected function setUp() {
20 // Allow anonymous users to see comments.
21 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
28 * Tests CSS classes on comments.
30 public function testCommentClasses() {
31 // Create all permutations for comments, users, and nodes.
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'],
38 $permutations = $this->generatePermutations($parameters);
40 foreach ($permutations as $case) {
42 $node = $this->drupalCreateNode(['type' => 'article', 'uid' => $case['node_uid']]);
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()]],
58 // Adjust the current/viewing user.
59 switch ($case['user']) {
61 if ($this->loggedInUser) {
62 $this->drupalLogout();
64 $case['user_uid'] = 0;
68 $this->drupalLogin($this->webUser);
69 $case['user_uid'] = $this->webUser->id();
73 $this->drupalLogin($this->adminUser);
74 $case['user_uid'] = $this->adminUser->id();
77 // Request the node with the comment.
78 $this->drupalGet('node/' . $node->id());
79 $settings = $this->getDrupalSettings();
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.');
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.');
93 $this->assertFalse(count($comments), 'by-anonymous class not found.');
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.');
102 $this->assertFalse(count($comments), 'by-node-author class not found.');
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.');
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.');
119 $this->assertFalse(count($comments), 'unpublished class not found.');
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.');