Chris@0: [0, $this->webUser->id()], Chris@0: 'comment_uid' => [0, $this->webUser->id(), $this->adminUser->id()], Chris@0: 'comment_status' => [CommentInterface::PUBLISHED, CommentInterface::NOT_PUBLISHED], Chris@0: 'user' => ['anonymous', 'authenticated', 'admin'], Chris@0: ]; Chris@0: $permutations = $this->generatePermutations($parameters); Chris@0: Chris@0: foreach ($permutations as $case) { Chris@0: // Create a new node. Chris@0: $node = $this->drupalCreateNode(['type' => 'article', 'uid' => $case['node_uid']]); Chris@0: Chris@0: // Add a comment. Chris@0: /** @var \Drupal\comment\CommentInterface $comment */ Chris@0: $comment = Comment::create([ Chris@0: 'entity_id' => $node->id(), Chris@0: 'entity_type' => 'node', Chris@0: 'field_name' => 'comment', Chris@0: 'uid' => $case['comment_uid'], Chris@0: 'status' => $case['comment_status'], Chris@0: 'subject' => $this->randomMachineName(), Chris@0: 'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED, Chris@0: 'comment_body' => [LanguageInterface::LANGCODE_NOT_SPECIFIED => [$this->randomMachineName()]], Chris@0: ]); Chris@0: $comment->save(); Chris@0: Chris@0: // Adjust the current/viewing user. Chris@0: switch ($case['user']) { Chris@0: case 'anonymous': Chris@0: if ($this->loggedInUser) { Chris@0: $this->drupalLogout(); Chris@0: } Chris@0: $case['user_uid'] = 0; Chris@0: break; Chris@0: Chris@0: case 'authenticated': Chris@0: $this->drupalLogin($this->webUser); Chris@0: $case['user_uid'] = $this->webUser->id(); Chris@0: break; Chris@0: Chris@0: case 'admin': Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $case['user_uid'] = $this->adminUser->id(); Chris@0: break; Chris@0: } Chris@0: // Request the node with the comment. Chris@0: $this->drupalGet('node/' . $node->id()); Chris@0: $settings = $this->getDrupalSettings(); Chris@0: Chris@0: // Verify the data-history-node-id attribute, which is necessary for the Chris@0: // by-viewer class and the "new" indicator, see below. Chris@0: $this->assertIdentical(1, count($this->xpath('//*[@data-history-node-id="' . $node->id() . '"]')), 'data-history-node-id attribute is set on node.'); Chris@0: Chris@0: // Verify classes if the comment is visible for the current user. Chris@0: if ($case['comment_status'] == CommentInterface::PUBLISHED || $case['user'] == 'admin') { Chris@0: // Verify the by-anonymous class. Chris@0: $comments = $this->xpath('//*[contains(@class, "comment") and contains(@class, "by-anonymous")]'); Chris@0: if ($case['comment_uid'] == 0) { Chris@0: $this->assertTrue(count($comments) == 1, 'by-anonymous class found.'); Chris@0: } Chris@0: else { Chris@0: $this->assertFalse(count($comments), 'by-anonymous class not found.'); Chris@0: } Chris@0: Chris@0: // Verify the by-node-author class. Chris@0: $comments = $this->xpath('//*[contains(@class, "comment") and contains(@class, "by-node-author")]'); Chris@0: if ($case['comment_uid'] > 0 && $case['comment_uid'] == $case['node_uid']) { Chris@0: $this->assertTrue(count($comments) == 1, 'by-node-author class found.'); Chris@0: } Chris@0: else { Chris@0: $this->assertFalse(count($comments), 'by-node-author class not found.'); Chris@0: } Chris@0: Chris@0: // Verify the data-comment-user-id attribute, which is used by the Chris@0: // drupal.comment-by-viewer library to add a by-viewer when the current Chris@0: // user (the viewer) was the author of the comment. We do this in Java- Chris@0: // Script to prevent breaking the render cache. Chris@0: $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.'); Chris@0: $this->assertRaw(drupal_get_path('module', 'comment') . '/js/comment-by-viewer.js', 'drupal.comment-by-viewer library is present.'); Chris@0: } Chris@0: Chris@0: // Verify the unpublished class. Chris@0: $comments = $this->xpath('//*[contains(@class, "comment") and contains(@class, "unpublished")]'); Chris@0: if ($case['comment_status'] == CommentInterface::NOT_PUBLISHED && $case['user'] == 'admin') { Chris@0: $this->assertTrue(count($comments) == 1, 'unpublished class found.'); Chris@0: } Chris@0: else { Chris@0: $this->assertFalse(count($comments), 'unpublished class not found.'); Chris@0: } Chris@0: Chris@0: // Verify the data-comment-timestamp attribute, which is used by the Chris@0: // drupal.comment-new-indicator library to add a "new" indicator to each Chris@0: // comment that was created or changed after the last time the current Chris@0: // user read the corresponding node. Chris@0: if ($case['comment_status'] == CommentInterface::PUBLISHED || $case['user'] == 'admin') { Chris@0: $this->assertIdentical(1, count($this->xpath('//*[contains(@class, "comment")]/*[@data-comment-timestamp="' . $comment->getChangedTime() . '"]')), 'data-comment-timestamp attribute is set on comment'); Chris@0: $expectedJS = ($case['user'] !== 'anonymous'); Chris@0: $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.'); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: }