annotate core/modules/comment/tests/src/Functional/CommentCSSTest.php @ 19:fa3358dc1485 tip

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