980ad30a2601dd92b8cbd72c3cdeed2a3123d4be
[yaffs-website] / web / core / modules / comment / src / Tests / CommentAdminTest.php
1 <?php
2
3 namespace Drupal\comment\Tests;
4
5 use Drupal\user\RoleInterface;
6 use Drupal\comment\Entity\Comment;
7
8 /**
9  * Tests comment approval functionality.
10  *
11  * @group comment
12  */
13 class CommentAdminTest extends CommentTestBase {
14
15   protected function setUp() {
16     parent::setUp();
17
18     $this->drupalPlaceBlock('page_title_block');
19   }
20
21   /**
22    * Test comment approval functionality through admin/content/comment.
23    */
24   public function testApprovalAdminInterface() {
25     // Set anonymous comments to require approval.
26     user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
27       'access comments' => TRUE,
28       'post comments' => TRUE,
29       'skip comment approval' => FALSE,
30     ]);
31     $this->drupalLogin($this->adminUser);
32     $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
33
34     // Test that the comments page loads correctly when there are no comments
35     $this->drupalGet('admin/content/comment');
36     $this->assertText(t('No comments available.'));
37
38     $this->drupalLogout();
39
40     // Post anonymous comment without contact info.
41     $subject = $this->randomMachineName();
42     $body = $this->randomMachineName();
43     $this->postComment($this->node, $body, $subject, TRUE); // Set $contact to true so that it won't check for id and message.
44     $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
45
46     // Get unapproved comment id.
47     $this->drupalLogin($this->adminUser);
48     $anonymous_comment4 = $this->getUnapprovedComment($subject);
49     $anonymous_comment4 = Comment::create([
50       'cid' => $anonymous_comment4,
51       'subject' => $subject,
52       'comment_body' => $body,
53       'entity_id' => $this->node->id(),
54       'entity_type' => 'node',
55       'field_name' => 'comment'
56     ]);
57     $this->drupalLogout();
58
59     $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
60
61     // Approve comment.
62     $this->drupalLogin($this->adminUser);
63     $this->performCommentOperation($anonymous_comment4, 'publish', TRUE);
64     $this->drupalLogout();
65
66     $this->drupalGet('node/' . $this->node->id());
67     $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
68
69     // Post 2 anonymous comments without contact info.
70     $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
71     $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
72
73     // Publish multiple comments in one operation.
74     $this->drupalLogin($this->adminUser);
75     $this->drupalGet('admin/content/comment/approval');
76     $this->assertText(t('Unapproved comments (@count)', ['@count' => 2]), 'Two unapproved comments waiting for approval.');
77     $edit = [
78       "comments[{$comments[0]->id()}]" => 1,
79       "comments[{$comments[1]->id()}]" => 1,
80     ];
81     $this->drupalPostForm(NULL, $edit, t('Update'));
82     $this->assertText(t('Unapproved comments (@count)', ['@count' => 0]), 'All comments were approved.');
83
84     // Delete multiple comments in one operation.
85     $edit = [
86       'operation' => 'delete',
87       "comments[{$comments[0]->id()}]" => 1,
88       "comments[{$comments[1]->id()}]" => 1,
89       "comments[{$anonymous_comment4->id()}]" => 1,
90     ];
91     $this->drupalPostForm(NULL, $edit, t('Update'));
92     $this->assertText(t('Are you sure you want to delete these comments and all their children?'), 'Confirmation required.');
93     $this->drupalPostForm(NULL, $edit, t('Delete comments'));
94     $this->assertText(t('No comments available.'), 'All comments were deleted.');
95     // Test message when no comments selected.
96     $edit = [
97       'operation' => 'delete',
98     ];
99     $this->drupalPostForm(NULL, $edit, t('Update'));
100     $this->assertText(t('Select one or more comments to perform the update on.'));
101   }
102
103   /**
104    * Tests comment approval functionality through the node interface.
105    */
106   public function testApprovalNodeInterface() {
107     // Set anonymous comments to require approval.
108     user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
109       'access comments' => TRUE,
110       'post comments' => TRUE,
111       'skip comment approval' => FALSE,
112     ]);
113     $this->drupalLogin($this->adminUser);
114     $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
115     $this->drupalLogout();
116
117     // Post anonymous comment without contact info.
118     $subject = $this->randomMachineName();
119     $body = $this->randomMachineName();
120     $this->postComment($this->node, $body, $subject, TRUE); // Set $contact to true so that it won't check for id and message.
121     $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
122
123     // Get unapproved comment id.
124     $this->drupalLogin($this->adminUser);
125     $anonymous_comment4 = $this->getUnapprovedComment($subject);
126     $anonymous_comment4 = Comment::create([
127       'cid' => $anonymous_comment4,
128       'subject' => $subject,
129       'comment_body' => $body,
130       'entity_id' => $this->node->id(),
131       'entity_type' => 'node',
132       'field_name' => 'comment'
133     ]);
134     $this->drupalLogout();
135
136     $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
137
138     // Approve comment.
139     $this->drupalLogin($this->adminUser);
140     $this->drupalGet('comment/1/approve');
141     $this->assertResponse(403, 'Forged comment approval was denied.');
142     $this->drupalGet('comment/1/approve', ['query' => ['token' => 'forged']]);
143     $this->assertResponse(403, 'Forged comment approval was denied.');
144     $this->drupalGet('comment/1/edit');
145     $this->assertFieldChecked('edit-status-0');
146     $this->drupalGet('node/' . $this->node->id());
147     $this->clickLink(t('Approve'));
148     $this->drupalLogout();
149
150     $this->drupalGet('node/' . $this->node->id());
151     $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
152   }
153
154   /**
155    * Tests comment bundle admin.
156    */
157   public function testCommentAdmin() {
158     // Login.
159     $this->drupalLogin($this->adminUser);
160     // Browse to comment bundle overview.
161     $this->drupalGet('admin/structure/comment');
162     $this->assertResponse(200);
163     // Make sure titles visible.
164     $this->assertText('Comment type');
165     $this->assertText('Description');
166     // Make sure the description is present.
167     $this->assertText('Default comment field');
168     // Manage fields.
169     $this->clickLink('Manage fields');
170     $this->assertResponse(200);
171     // Make sure comment_body field is shown.
172     $this->assertText('comment_body');
173     // Rest from here on in is field_ui.
174   }
175
176   /**
177    * Tests editing a comment as an admin.
178    */
179   public function testEditComment() {
180     // Enable anonymous user comments.
181     user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
182       'access comments',
183       'post comments',
184       'skip comment approval',
185     ]);
186
187     // Log in as a web user.
188     $this->drupalLogin($this->webUser);
189     // Post a comment.
190     $comment = $this->postComment($this->node, $this->randomMachineName());
191
192     $this->drupalLogout();
193
194     // Post anonymous comment.
195     $this->drupalLogin($this->adminUser);
196     $this->setCommentAnonymous('2'); // Ensure that we need email id before posting comment.
197     $this->drupalLogout();
198
199     // Post comment with contact info (required).
200     $author_name = $this->randomMachineName();
201     $author_mail = $this->randomMachineName() . '@example.com';
202     $anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), ['name' => $author_name, 'mail' => $author_mail]);
203
204     // Log in as an admin user.
205     $this->drupalLogin($this->adminUser);
206
207     // Make sure the comment field is not visible when
208     // the comment was posted by an authenticated user.
209     $this->drupalGet('comment/' . $comment->id() . '/edit');
210     $this->assertNoFieldById('edit-mail', $comment->getAuthorEmail());
211
212     // Make sure the comment field is visible when
213     // the comment was posted by an anonymous user.
214     $this->drupalGet('comment/' . $anonymous_comment->id() . '/edit');
215     $this->assertFieldById('edit-mail', $anonymous_comment->getAuthorEmail());
216   }
217
218 }