Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/comment/tests/src/Functional/CommentCSSTest.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\comment\Functional; | |
4 | |
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; | |
10 | |
11 /** | |
12 * Tests CSS classes on comments. | |
13 * | |
14 * @group comment | |
15 */ | |
16 class CommentCSSTest extends CommentTestBase { | |
17 | |
18 use GeneratePermutationsTrait; | |
19 | |
20 protected function setUp() { | |
21 parent::setUp(); | |
22 | |
23 // Allow anonymous users to see comments. | |
24 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [ | |
25 'access comments', | |
26 'access content' | |
27 ]); | |
28 } | |
29 | |
30 /** | |
31 * Tests CSS classes on comments. | |
32 */ | |
33 public function testCommentClasses() { | |
34 // Create all permutations for comments, users, and nodes. | |
35 $parameters = [ | |
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'], | |
40 ]; | |
41 $permutations = $this->generatePermutations($parameters); | |
42 | |
43 foreach ($permutations as $case) { | |
44 // Create a new node. | |
45 $node = $this->drupalCreateNode(['type' => 'article', 'uid' => $case['node_uid']]); | |
46 | |
47 // Add a comment. | |
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()]], | |
58 ]); | |
59 $comment->save(); | |
60 | |
61 // Adjust the current/viewing user. | |
62 switch ($case['user']) { | |
63 case 'anonymous': | |
64 if ($this->loggedInUser) { | |
65 $this->drupalLogout(); | |
66 } | |
67 $case['user_uid'] = 0; | |
68 break; | |
69 | |
70 case 'authenticated': | |
71 $this->drupalLogin($this->webUser); | |
72 $case['user_uid'] = $this->webUser->id(); | |
73 break; | |
74 | |
75 case 'admin': | |
76 $this->drupalLogin($this->adminUser); | |
77 $case['user_uid'] = $this->adminUser->id(); | |
78 break; | |
79 } | |
80 // Request the node with the comment. | |
81 $this->drupalGet('node/' . $node->id()); | |
82 $settings = $this->getDrupalSettings(); | |
83 | |
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.'); | |
87 | |
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.'); | |
94 } | |
95 else { | |
96 $this->assertFalse(count($comments), 'by-anonymous class not found.'); | |
97 } | |
98 | |
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.'); | |
103 } | |
104 else { | |
105 $this->assertFalse(count($comments), 'by-node-author class not found.'); | |
106 } | |
107 | |
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.'); | |
114 } | |
115 | |
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.'); | |
120 } | |
121 else { | |
122 $this->assertFalse(count($comments), 'unpublished class not found.'); | |
123 } | |
124 | |
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.'); | |
133 } | |
134 } | |
135 } | |
136 | |
137 } |