comparison core/modules/comment/tests/src/Functional/CommentAdminTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\comment\Functional;
4
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Component\Utility\Html;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\user\RoleInterface;
9 use Drupal\comment\Entity\Comment;
10
11 /**
12 * Tests comment approval functionality.
13 *
14 * @group comment
15 */
16 class CommentAdminTest extends CommentTestBase {
17
18 protected function setUp() {
19 parent::setUp();
20
21 $this->drupalPlaceBlock('page_title_block');
22 }
23
24 /**
25 * Test comment approval functionality through admin/content/comment.
26 */
27 public function testApprovalAdminInterface() {
28 // Set anonymous comments to require approval.
29 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
30 'access comments' => TRUE,
31 'post comments' => TRUE,
32 'skip comment approval' => FALSE,
33 ]);
34 $this->drupalLogin($this->adminUser);
35 // Ensure that doesn't require contact info.
36 $this->setCommentAnonymous('0');
37
38 // Test that the comments page loads correctly when there are no comments
39 $this->drupalGet('admin/content/comment');
40 $this->assertText(t('No comments available.'));
41
42 $this->drupalLogout();
43
44 // Post anonymous comment without contact info.
45 $subject = $this->randomMachineName();
46 $body = $this->randomMachineName();
47 // Set $contact to true so that it won't check for id and message.
48 $this->postComment($this->node, $body, $subject, TRUE);
49 $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
50
51 // Get unapproved comment id.
52 $this->drupalLogin($this->adminUser);
53 $anonymous_comment4 = $this->getUnapprovedComment($subject);
54 $anonymous_comment4 = Comment::create([
55 'cid' => $anonymous_comment4,
56 'subject' => $subject,
57 'comment_body' => $body,
58 'entity_id' => $this->node->id(),
59 'entity_type' => 'node',
60 'field_name' => 'comment'
61 ]);
62 $this->drupalLogout();
63
64 $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
65
66 // Approve comment.
67 $this->drupalLogin($this->adminUser);
68 $this->performCommentOperation($anonymous_comment4, 'publish', TRUE);
69 $this->drupalLogout();
70
71 $this->drupalGet('node/' . $this->node->id());
72 $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
73
74 // Post 2 anonymous comments without contact info.
75 $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
76 $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
77
78 // Publish multiple comments in one operation.
79 $this->drupalLogin($this->adminUser);
80 $this->drupalGet('admin/content/comment/approval');
81 $this->assertText(t('Unapproved comments (@count)', ['@count' => 2]), 'Two unapproved comments waiting for approval.');
82 $edit = [
83 "comments[{$comments[0]->id()}]" => 1,
84 "comments[{$comments[1]->id()}]" => 1,
85 ];
86 $this->drupalPostForm(NULL, $edit, t('Update'));
87 $this->assertText(t('Unapproved comments (@count)', ['@count' => 0]), 'All comments were approved.');
88
89 // Delete multiple comments in one operation.
90 $edit = [
91 'operation' => 'delete',
92 "comments[{$comments[0]->id()}]" => 1,
93 "comments[{$comments[1]->id()}]" => 1,
94 "comments[{$anonymous_comment4->id()}]" => 1,
95 ];
96 $this->drupalPostForm(NULL, $edit, t('Update'));
97 $this->assertText(t('Are you sure you want to delete these comments and all their children?'), 'Confirmation required.');
98 $this->drupalPostForm(NULL, [], t('Delete'));
99 $this->assertText(t('No comments available.'), 'All comments were deleted.');
100 // Test message when no comments selected.
101 $edit = [
102 'operation' => 'delete',
103 ];
104 $this->drupalPostForm(NULL, $edit, t('Update'));
105 $this->assertText(t('Select one or more comments to perform the update on.'));
106
107 // Make sure the label of unpublished node is not visible on listing page.
108 $this->drupalGet('admin/content/comment');
109 $this->postComment($this->node, $this->randomMachineName());
110 $this->drupalGet('admin/content/comment');
111 $this->assertText(Html::escape($this->node->label()));
112 $this->node->setUnpublished()->save();
113 $this->drupalGet('admin/content/comment');
114 $this->assertNoText(Html::escape($this->node->label()));
115 }
116
117 /**
118 * Tests comment approval functionality through the node interface.
119 */
120 public function testApprovalNodeInterface() {
121 // Set anonymous comments to require approval.
122 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
123 'access comments' => TRUE,
124 'post comments' => TRUE,
125 'skip comment approval' => FALSE,
126 ]);
127 $this->drupalLogin($this->adminUser);
128 // Ensure that doesn't require contact info.
129 $this->setCommentAnonymous('0');
130 $this->drupalLogout();
131
132 // Post anonymous comment without contact info.
133 $subject = $this->randomMachineName();
134 $body = $this->randomMachineName();
135 // Set $contact to true so that it won't check for id and message.
136 $this->postComment($this->node, $body, $subject, TRUE);
137 $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
138
139 // Get unapproved comment id.
140 $this->drupalLogin($this->adminUser);
141 $anonymous_comment4 = $this->getUnapprovedComment($subject);
142 $anonymous_comment4 = Comment::create([
143 'cid' => $anonymous_comment4,
144 'subject' => $subject,
145 'comment_body' => $body,
146 'entity_id' => $this->node->id(),
147 'entity_type' => 'node',
148 'field_name' => 'comment'
149 ]);
150 $this->drupalLogout();
151
152 $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
153
154 // Approve comment.
155 $this->drupalLogin($this->adminUser);
156 $this->drupalGet('comment/1/approve');
157 $this->assertResponse(403, 'Forged comment approval was denied.');
158 $this->drupalGet('comment/1/approve', ['query' => ['token' => 'forged']]);
159 $this->assertResponse(403, 'Forged comment approval was denied.');
160 $this->drupalGet('comment/1/edit');
161 $this->assertFieldChecked('edit-status-0');
162 $this->drupalGet('node/' . $this->node->id());
163 $this->clickLink(t('Approve'));
164 $this->drupalLogout();
165
166 $this->drupalGet('node/' . $this->node->id());
167 $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
168 }
169
170 /**
171 * Tests comment bundle admin.
172 */
173 public function testCommentAdmin() {
174 // Login.
175 $this->drupalLogin($this->adminUser);
176 // Browse to comment bundle overview.
177 $this->drupalGet('admin/structure/comment');
178 $this->assertResponse(200);
179 // Make sure titles visible.
180 $this->assertText('Comment type');
181 $this->assertText('Description');
182 // Make sure the description is present.
183 $this->assertText('Default comment field');
184 // Manage fields.
185 $this->clickLink('Manage fields');
186 $this->assertResponse(200);
187 // Make sure comment_body field is shown.
188 $this->assertText('comment_body');
189 // Rest from here on in is field_ui.
190 }
191
192 /**
193 * Tests editing a comment as an admin.
194 */
195 public function testEditComment() {
196 // Enable anonymous user comments.
197 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
198 'access comments',
199 'post comments',
200 'skip comment approval',
201 ]);
202
203 // Log in as a web user.
204 $this->drupalLogin($this->webUser);
205 // Post a comment.
206 $comment = $this->postComment($this->node, $this->randomMachineName());
207
208 $this->drupalLogout();
209
210 // Post anonymous comment.
211 $this->drupalLogin($this->adminUser);
212 // Ensure that we need email id before posting comment.
213 $this->setCommentAnonymous('2');
214 $this->drupalLogout();
215
216 // Post comment with contact info (required).
217 $author_name = $this->randomMachineName();
218 $author_mail = $this->randomMachineName() . '@example.com';
219 $anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), ['name' => $author_name, 'mail' => $author_mail]);
220
221 // Log in as an admin user.
222 $this->drupalLogin($this->adminUser);
223
224 // Make sure the comment field is not visible when
225 // the comment was posted by an authenticated user.
226 $this->drupalGet('comment/' . $comment->id() . '/edit');
227 $this->assertNoFieldById('edit-mail', $comment->getAuthorEmail());
228
229 // Make sure the comment field is visible when
230 // the comment was posted by an anonymous user.
231 $this->drupalGet('comment/' . $anonymous_comment->id() . '/edit');
232 $this->assertFieldById('edit-mail', $anonymous_comment->getAuthorEmail());
233 }
234
235 /**
236 * Tests commented translation deletion admin view.
237 */
238 public function testCommentedTranslationDeletion() {
239 \Drupal::service('module_installer')->install([
240 'language',
241 'locale',
242 ]);
243 \Drupal::service('router.builder')->rebuildIfNeeded();
244
245 ConfigurableLanguage::createFromLangcode('ur')->save();
246 // Rebuild the container to update the default language container variable.
247 $this->rebuildContainer();
248 // Ensure that doesn't require contact info.
249 $this->setCommentAnonymous('0');
250 $this->drupalLogin($this->webUser);
251 $count_query = \Drupal::entityTypeManager()
252 ->getStorage('comment')
253 ->getQuery()
254 ->count();
255 $before_count = $count_query->execute();
256 // Post 2 anonymous comments without contact info.
257 $comment1 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
258 $comment2 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
259
260 $comment1->addTranslation('ur', ['subject' => 'ur ' . $comment1->label()])
261 ->save();
262 $comment2->addTranslation('ur', ['subject' => 'ur ' . $comment1->label()])
263 ->save();
264 $this->drupalLogout();
265 $this->drupalLogin($this->adminUser);
266 // Delete multiple comments in one operation.
267 $edit = [
268 'operation' => 'delete',
269 "comments[{$comment1->id()}]" => 1,
270 "comments[{$comment2->id()}]" => 1,
271 ];
272 $this->drupalPostForm('admin/content/comment', $edit, t('Update'));
273 $this->assertRaw(new FormattableMarkup('@label (Original translation) - <em>The following comment translations will be deleted:</em>', ['@label' => $comment1->label()]));
274 $this->assertRaw(new FormattableMarkup('@label (Original translation) - <em>The following comment translations will be deleted:</em>', ['@label' => $comment2->label()]));
275 $this->assertText('English');
276 $this->assertText('Urdu');
277 $this->drupalPostForm(NULL, [], t('Delete'));
278 $after_count = $count_query->execute();
279 $this->assertEqual($after_count, $before_count, 'No comment or translation found.');
280 }
281
282 }