Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\comment\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\comment\Entity\CommentType;
|
Chris@0
|
6 use Drupal\comment\Entity\Comment;
|
Chris@0
|
7 use Drupal\comment\CommentInterface;
|
Chris@0
|
8 use Drupal\comment\Tests\CommentTestTrait;
|
Chris@0
|
9 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
10 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
Chris@0
|
11 use Drupal\node\Entity\NodeType;
|
Chris@0
|
12 use Drupal\Tests\BrowserTestBase;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Provides setup and helper methods for comment tests.
|
Chris@0
|
16 */
|
Chris@0
|
17 abstract class CommentTestBase extends BrowserTestBase {
|
Chris@0
|
18
|
Chris@0
|
19 use CommentTestTrait;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Modules to install.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @var array
|
Chris@0
|
25 */
|
Chris@0
|
26 public static $modules = ['block', 'comment', 'node', 'history', 'field_ui', 'datetime'];
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * An administrative user with permission to configure comment settings.
|
Chris@0
|
30 *
|
Chris@0
|
31 * @var \Drupal\user\UserInterface
|
Chris@0
|
32 */
|
Chris@0
|
33 protected $adminUser;
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * A normal user with permission to post comments.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @var \Drupal\user\UserInterface
|
Chris@0
|
39 */
|
Chris@0
|
40 protected $webUser;
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * A test node to which comments will be posted.
|
Chris@0
|
44 *
|
Chris@0
|
45 * @var \Drupal\node\NodeInterface
|
Chris@0
|
46 */
|
Chris@0
|
47 protected $node;
|
Chris@0
|
48
|
Chris@0
|
49 protected function setUp() {
|
Chris@0
|
50 parent::setUp();
|
Chris@0
|
51
|
Chris@0
|
52 // Create an article content type only if it does not yet exist, so that
|
Chris@0
|
53 // child classes may specify the standard profile.
|
Chris@0
|
54 $types = NodeType::loadMultiple();
|
Chris@0
|
55 if (empty($types['article'])) {
|
Chris@0
|
56 $this->drupalCreateContentType(['type' => 'article', 'name' => t('Article')]);
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 // Create two test users.
|
Chris@0
|
60 $this->adminUser = $this->drupalCreateUser([
|
Chris@0
|
61 'administer content types',
|
Chris@0
|
62 'administer comments',
|
Chris@0
|
63 'administer comment types',
|
Chris@0
|
64 'administer comment fields',
|
Chris@0
|
65 'administer comment display',
|
Chris@0
|
66 'skip comment approval',
|
Chris@0
|
67 'post comments',
|
Chris@0
|
68 'access comments',
|
Chris@0
|
69 // Usernames aren't shown in comment edit form autocomplete unless this
|
Chris@0
|
70 // permission is granted.
|
Chris@0
|
71 'access user profiles',
|
Chris@0
|
72 'access content',
|
Chris@0
|
73 ]);
|
Chris@0
|
74 $this->webUser = $this->drupalCreateUser([
|
Chris@0
|
75 'access comments',
|
Chris@0
|
76 'post comments',
|
Chris@0
|
77 'create article content',
|
Chris@0
|
78 'edit own comments',
|
Chris@0
|
79 'skip comment approval',
|
Chris@0
|
80 'access content',
|
Chris@0
|
81 ]);
|
Chris@0
|
82
|
Chris@0
|
83 // Create comment field on article.
|
Chris@0
|
84 $this->addDefaultCommentField('node', 'article');
|
Chris@0
|
85
|
Chris@0
|
86 // Create a test node authored by the web user.
|
Chris@0
|
87 $this->node = $this->drupalCreateNode(['type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()]);
|
Chris@0
|
88 $this->drupalPlaceBlock('local_tasks_block');
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * Posts a comment.
|
Chris@0
|
93 *
|
Chris@0
|
94 * @param \Drupal\Core\Entity\EntityInterface|null $entity
|
Chris@0
|
95 * Node to post comment on or NULL to post to the previously loaded page.
|
Chris@0
|
96 * @param string $comment
|
Chris@0
|
97 * Comment body.
|
Chris@0
|
98 * @param string $subject
|
Chris@0
|
99 * Comment subject.
|
Chris@0
|
100 * @param string $contact
|
Chris@0
|
101 * Set to NULL for no contact info, TRUE to ignore success checking, and
|
Chris@0
|
102 * array of values to set contact info.
|
Chris@0
|
103 * @param string $field_name
|
Chris@0
|
104 * (optional) Field name through which the comment should be posted.
|
Chris@0
|
105 * Defaults to 'comment'.
|
Chris@0
|
106 *
|
Chris@0
|
107 * @return \Drupal\comment\CommentInterface|null
|
Chris@0
|
108 * The posted comment or NULL when posted comment was not found.
|
Chris@0
|
109 */
|
Chris@0
|
110 public function postComment($entity, $comment, $subject = '', $contact = NULL, $field_name = 'comment') {
|
Chris@0
|
111 $edit = [];
|
Chris@0
|
112 $edit['comment_body[0][value]'] = $comment;
|
Chris@0
|
113
|
Chris@0
|
114 if ($entity !== NULL) {
|
Chris@0
|
115 $field = FieldConfig::loadByName($entity->getEntityTypeId(), $entity->bundle(), $field_name);
|
Chris@0
|
116 }
|
Chris@0
|
117 else {
|
Chris@0
|
118 $field = FieldConfig::loadByName('node', 'article', $field_name);
|
Chris@0
|
119 }
|
Chris@0
|
120 $preview_mode = $field->getSetting('preview');
|
Chris@0
|
121
|
Chris@0
|
122 // Must get the page before we test for fields.
|
Chris@0
|
123 if ($entity !== NULL) {
|
Chris@0
|
124 $this->drupalGet('comment/reply/' . $entity->getEntityTypeId() . '/' . $entity->id() . '/' . $field_name);
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 // Determine the visibility of subject form field.
|
Chris@0
|
128 if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) {
|
Chris@0
|
129 // Subject input allowed.
|
Chris@0
|
130 $edit['subject[0][value]'] = $subject;
|
Chris@0
|
131 }
|
Chris@0
|
132 else {
|
Chris@0
|
133 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.');
|
Chris@0
|
134 }
|
Chris@0
|
135
|
Chris@0
|
136 if ($contact !== NULL && is_array($contact)) {
|
Chris@0
|
137 $edit += $contact;
|
Chris@0
|
138 }
|
Chris@0
|
139 switch ($preview_mode) {
|
Chris@0
|
140 case DRUPAL_REQUIRED:
|
Chris@0
|
141 // Preview required so no save button should be found.
|
Chris@0
|
142 $this->assertNoFieldByName('op', t('Save'), 'Save button not found.');
|
Chris@0
|
143 $this->drupalPostForm(NULL, $edit, t('Preview'));
|
Chris@0
|
144 // Don't break here so that we can test post-preview field presence and
|
Chris@0
|
145 // function below.
|
Chris@0
|
146 case DRUPAL_OPTIONAL:
|
Chris@0
|
147 $this->assertFieldByName('op', t('Preview'), 'Preview button found.');
|
Chris@0
|
148 $this->assertFieldByName('op', t('Save'), 'Save button found.');
|
Chris@0
|
149 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
150 break;
|
Chris@0
|
151
|
Chris@0
|
152 case DRUPAL_DISABLED:
|
Chris@0
|
153 $this->assertNoFieldByName('op', t('Preview'), 'Preview button not found.');
|
Chris@0
|
154 $this->assertFieldByName('op', t('Save'), 'Save button found.');
|
Chris@0
|
155 $this->drupalPostForm(NULL, $edit, t('Save'));
|
Chris@0
|
156 break;
|
Chris@0
|
157 }
|
Chris@0
|
158 $match = [];
|
Chris@0
|
159 // Get comment ID
|
Chris@0
|
160 preg_match('/#comment-([0-9]+)/', $this->getURL(), $match);
|
Chris@0
|
161
|
Chris@0
|
162 // Get comment.
|
Chris@0
|
163 if ($contact !== TRUE) {
|
Chris@0
|
164 // If true then attempting to find error message.
|
Chris@0
|
165 if ($subject) {
|
Chris@0
|
166 $this->assertText($subject, 'Comment subject posted.');
|
Chris@0
|
167 }
|
Chris@0
|
168 $this->assertText($comment, 'Comment body posted.');
|
Chris@0
|
169 $this->assertTrue((!empty($match) && !empty($match[1])), 'Comment id found.');
|
Chris@0
|
170 }
|
Chris@0
|
171
|
Chris@0
|
172 if (isset($match[1])) {
|
Chris@0
|
173 \Drupal::entityManager()->getStorage('comment')->resetCache([$match[1]]);
|
Chris@0
|
174 return Comment::load($match[1]);
|
Chris@0
|
175 }
|
Chris@0
|
176 }
|
Chris@0
|
177
|
Chris@0
|
178 /**
|
Chris@0
|
179 * Checks current page for specified comment.
|
Chris@0
|
180 *
|
Chris@0
|
181 * @param \Drupal\comment\CommentInterface $comment
|
Chris@0
|
182 * The comment object.
|
Chris@0
|
183 * @param bool $reply
|
Chris@0
|
184 * Boolean indicating whether the comment is a reply to another comment.
|
Chris@0
|
185 *
|
Chris@0
|
186 * @return bool
|
Chris@0
|
187 * Boolean indicating whether the comment was found.
|
Chris@0
|
188 */
|
Chris@0
|
189 public function commentExists(CommentInterface $comment = NULL, $reply = FALSE) {
|
Chris@0
|
190 if ($comment) {
|
Chris@18
|
191 $comment_element = $this->cssSelect('.comment-wrapper ' . ($reply ? '.indented ' : '') . 'article#comment-' . $comment->id());
|
Chris@0
|
192 if (empty($comment_element)) {
|
Chris@0
|
193 return FALSE;
|
Chris@0
|
194 }
|
Chris@0
|
195
|
Chris@0
|
196 $comment_title = $comment_element[0]->find('xpath', 'div/h3/a');
|
Chris@0
|
197 if (empty($comment_title) || $comment_title->getText() !== $comment->getSubject()) {
|
Chris@0
|
198 return FALSE;
|
Chris@0
|
199 }
|
Chris@0
|
200
|
Chris@0
|
201 $comment_body = $comment_element[0]->find('xpath', 'div/div/p');
|
Chris@0
|
202 if (empty($comment_body) || $comment_body->getText() !== $comment->comment_body->value) {
|
Chris@0
|
203 return FALSE;
|
Chris@0
|
204 }
|
Chris@0
|
205
|
Chris@0
|
206 return TRUE;
|
Chris@0
|
207 }
|
Chris@0
|
208 else {
|
Chris@0
|
209 return FALSE;
|
Chris@0
|
210 }
|
Chris@0
|
211 }
|
Chris@0
|
212
|
Chris@0
|
213 /**
|
Chris@0
|
214 * Deletes a comment.
|
Chris@0
|
215 *
|
Chris@0
|
216 * @param \Drupal\comment\CommentInterface $comment
|
Chris@0
|
217 * Comment to delete.
|
Chris@0
|
218 */
|
Chris@0
|
219 public function deleteComment(CommentInterface $comment) {
|
Chris@0
|
220 $this->drupalPostForm('comment/' . $comment->id() . '/delete', [], t('Delete'));
|
Chris@0
|
221 $this->assertText(t('The comment and all its replies have been deleted.'), 'Comment deleted.');
|
Chris@0
|
222 }
|
Chris@0
|
223
|
Chris@0
|
224 /**
|
Chris@0
|
225 * Sets the value governing whether the subject field should be enabled.
|
Chris@0
|
226 *
|
Chris@0
|
227 * @param bool $enabled
|
Chris@0
|
228 * Boolean specifying whether the subject field should be enabled.
|
Chris@0
|
229 */
|
Chris@0
|
230 public function setCommentSubject($enabled) {
|
Chris@0
|
231 $form_display = entity_get_form_display('comment', 'comment', 'default');
|
Chris@0
|
232 if ($enabled) {
|
Chris@0
|
233 $form_display->setComponent('subject', [
|
Chris@0
|
234 'type' => 'string_textfield',
|
Chris@0
|
235 ]);
|
Chris@0
|
236 }
|
Chris@0
|
237 else {
|
Chris@0
|
238 $form_display->removeComponent('subject');
|
Chris@0
|
239 }
|
Chris@0
|
240 $form_display->save();
|
Chris@0
|
241 // Display status message.
|
Chris@0
|
242 $this->pass('Comment subject ' . ($enabled ? 'enabled' : 'disabled') . '.');
|
Chris@0
|
243 }
|
Chris@0
|
244
|
Chris@0
|
245 /**
|
Chris@0
|
246 * Sets the value governing the previewing mode for the comment form.
|
Chris@0
|
247 *
|
Chris@0
|
248 * @param int $mode
|
Chris@0
|
249 * The preview mode: DRUPAL_DISABLED, DRUPAL_OPTIONAL or DRUPAL_REQUIRED.
|
Chris@0
|
250 * @param string $field_name
|
Chris@0
|
251 * (optional) Field name through which the comment should be posted.
|
Chris@0
|
252 * Defaults to 'comment'.
|
Chris@0
|
253 */
|
Chris@0
|
254 public function setCommentPreview($mode, $field_name = 'comment') {
|
Chris@0
|
255 switch ($mode) {
|
Chris@0
|
256 case DRUPAL_DISABLED:
|
Chris@0
|
257 $mode_text = 'disabled';
|
Chris@0
|
258 break;
|
Chris@0
|
259
|
Chris@0
|
260 case DRUPAL_OPTIONAL:
|
Chris@0
|
261 $mode_text = 'optional';
|
Chris@0
|
262 break;
|
Chris@0
|
263
|
Chris@0
|
264 case DRUPAL_REQUIRED:
|
Chris@0
|
265 $mode_text = 'required';
|
Chris@0
|
266 break;
|
Chris@0
|
267 }
|
Chris@0
|
268 $this->setCommentSettings('preview', $mode, format_string('Comment preview @mode_text.', ['@mode_text' => $mode_text]), $field_name);
|
Chris@0
|
269 }
|
Chris@0
|
270
|
Chris@0
|
271 /**
|
Chris@0
|
272 * Sets the value governing whether the comment form is on its own page.
|
Chris@0
|
273 *
|
Chris@0
|
274 * @param bool $enabled
|
Chris@0
|
275 * TRUE if the comment form should be displayed on the same page as the
|
Chris@0
|
276 * comments; FALSE if it should be displayed on its own page.
|
Chris@0
|
277 * @param string $field_name
|
Chris@0
|
278 * (optional) Field name through which the comment should be posted.
|
Chris@0
|
279 * Defaults to 'comment'.
|
Chris@0
|
280 */
|
Chris@0
|
281 public function setCommentForm($enabled, $field_name = 'comment') {
|
Chris@0
|
282 $this->setCommentSettings('form_location', ($enabled ? CommentItemInterface::FORM_BELOW : CommentItemInterface::FORM_SEPARATE_PAGE), 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.', $field_name);
|
Chris@0
|
283 }
|
Chris@0
|
284
|
Chris@0
|
285 /**
|
Chris@0
|
286 * Sets the value governing restrictions on anonymous comments.
|
Chris@0
|
287 *
|
Chris@0
|
288 * @param int $level
|
Chris@0
|
289 * The level of the contact information allowed for anonymous comments:
|
Chris@0
|
290 * - 0: No contact information allowed.
|
Chris@0
|
291 * - 1: Contact information allowed but not required.
|
Chris@0
|
292 * - 2: Contact information required.
|
Chris@0
|
293 */
|
Chris@0
|
294 public function setCommentAnonymous($level) {
|
Chris@0
|
295 $this->setCommentSettings('anonymous', $level, format_string('Anonymous commenting set to level @level.', ['@level' => $level]));
|
Chris@0
|
296 }
|
Chris@0
|
297
|
Chris@0
|
298 /**
|
Chris@0
|
299 * Sets the value specifying the default number of comments per page.
|
Chris@0
|
300 *
|
Chris@0
|
301 * @param int $number
|
Chris@0
|
302 * Comments per page value.
|
Chris@0
|
303 * @param string $field_name
|
Chris@0
|
304 * (optional) Field name through which the comment should be posted.
|
Chris@0
|
305 * Defaults to 'comment'.
|
Chris@0
|
306 */
|
Chris@0
|
307 public function setCommentsPerPage($number, $field_name = 'comment') {
|
Chris@0
|
308 $this->setCommentSettings('per_page', $number, format_string('Number of comments per page set to @number.', ['@number' => $number]), $field_name);
|
Chris@0
|
309 }
|
Chris@0
|
310
|
Chris@0
|
311 /**
|
Chris@0
|
312 * Sets a comment settings variable for the article content type.
|
Chris@0
|
313 *
|
Chris@0
|
314 * @param string $name
|
Chris@0
|
315 * Name of variable.
|
Chris@0
|
316 * @param string $value
|
Chris@0
|
317 * Value of variable.
|
Chris@0
|
318 * @param string $message
|
Chris@0
|
319 * Status message to display.
|
Chris@0
|
320 * @param string $field_name
|
Chris@0
|
321 * (optional) Field name through which the comment should be posted.
|
Chris@0
|
322 * Defaults to 'comment'.
|
Chris@0
|
323 */
|
Chris@0
|
324 public function setCommentSettings($name, $value, $message, $field_name = 'comment') {
|
Chris@0
|
325 $field = FieldConfig::loadByName('node', 'article', $field_name);
|
Chris@0
|
326 $field->setSetting($name, $value);
|
Chris@0
|
327 $field->save();
|
Chris@0
|
328 // Display status message.
|
Chris@0
|
329 $this->pass($message);
|
Chris@0
|
330 }
|
Chris@0
|
331
|
Chris@0
|
332 /**
|
Chris@0
|
333 * Checks whether the commenter's contact information is displayed.
|
Chris@0
|
334 *
|
Chris@0
|
335 * @return bool
|
Chris@0
|
336 * Contact info is available.
|
Chris@0
|
337 */
|
Chris@0
|
338 public function commentContactInfoAvailable() {
|
Chris@17
|
339 return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->getSession()->getPage()->getContent());
|
Chris@0
|
340 }
|
Chris@0
|
341
|
Chris@0
|
342 /**
|
Chris@0
|
343 * Performs the specified operation on the specified comment.
|
Chris@0
|
344 *
|
Chris@0
|
345 * @param \Drupal\comment\CommentInterface $comment
|
Chris@0
|
346 * Comment to perform operation on.
|
Chris@0
|
347 * @param string $operation
|
Chris@0
|
348 * Operation to perform.
|
Chris@0
|
349 * @param bool $approval
|
Chris@0
|
350 * Operation is found on approval page.
|
Chris@0
|
351 */
|
Chris@0
|
352 public function performCommentOperation(CommentInterface $comment, $operation, $approval = FALSE) {
|
Chris@0
|
353 $edit = [];
|
Chris@0
|
354 $edit['operation'] = $operation;
|
Chris@0
|
355 $edit['comments[' . $comment->id() . ']'] = TRUE;
|
Chris@0
|
356 $this->drupalPostForm('admin/content/comment' . ($approval ? '/approval' : ''), $edit, t('Update'));
|
Chris@0
|
357
|
Chris@0
|
358 if ($operation == 'delete') {
|
Chris@0
|
359 $this->drupalPostForm(NULL, [], t('Delete'));
|
Chris@0
|
360 $this->assertRaw(\Drupal::translation()->formatPlural(1, 'Deleted 1 comment.', 'Deleted @count comments.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation]));
|
Chris@0
|
361 }
|
Chris@0
|
362 else {
|
Chris@0
|
363 $this->assertText(t('The update has been performed.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation]));
|
Chris@0
|
364 }
|
Chris@0
|
365 }
|
Chris@0
|
366
|
Chris@0
|
367 /**
|
Chris@0
|
368 * Gets the comment ID for an unapproved comment.
|
Chris@0
|
369 *
|
Chris@0
|
370 * @param string $subject
|
Chris@0
|
371 * Comment subject to find.
|
Chris@0
|
372 *
|
Chris@0
|
373 * @return int
|
Chris@0
|
374 * Comment id.
|
Chris@0
|
375 */
|
Chris@0
|
376 public function getUnapprovedComment($subject) {
|
Chris@0
|
377 $this->drupalGet('admin/content/comment/approval');
|
Chris@17
|
378 preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this->getSession()->getPage()->getContent(), $match);
|
Chris@0
|
379
|
Chris@0
|
380 return $match[2];
|
Chris@0
|
381 }
|
Chris@0
|
382
|
Chris@0
|
383 /**
|
Chris@0
|
384 * Creates a comment comment type (bundle).
|
Chris@0
|
385 *
|
Chris@0
|
386 * @param string $label
|
Chris@0
|
387 * The comment type label.
|
Chris@0
|
388 *
|
Chris@0
|
389 * @return \Drupal\comment\Entity\CommentType
|
Chris@0
|
390 * Created comment type.
|
Chris@0
|
391 */
|
Chris@0
|
392 protected function createCommentType($label) {
|
Chris@0
|
393 $bundle = CommentType::create([
|
Chris@0
|
394 'id' => $label,
|
Chris@0
|
395 'label' => $label,
|
Chris@0
|
396 'description' => '',
|
Chris@0
|
397 'target_entity_type_id' => 'node',
|
Chris@0
|
398 ]);
|
Chris@0
|
399 $bundle->save();
|
Chris@0
|
400 return $bundle;
|
Chris@0
|
401 }
|
Chris@0
|
402
|
Chris@0
|
403 }
|