3 namespace Drupal\Tests\comment\Functional;
5 use Drupal\comment\CommentInterface;
6 use Drupal\comment\Entity\Comment;
7 use Drupal\comment\Entity\CommentType;
8 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
9 use Drupal\comment\Tests\CommentTestTrait;
10 use Drupal\entity_test\Entity\EntityTest;
11 use Drupal\field\Entity\FieldConfig;
12 use Drupal\field\Entity\FieldStorageConfig;
13 use Drupal\field_ui\Tests\FieldUiTestTrait;
14 use Drupal\Tests\BrowserTestBase;
15 use Drupal\Core\Entity\EntityInterface;
16 use Drupal\user\RoleInterface;
19 * Tests commenting on a test entity.
23 class CommentNonNodeTest extends BrowserTestBase {
28 public static $modules = ['comment', 'user', 'field_ui', 'entity_test', 'block'];
31 * An administrative user with permission to configure comment settings.
33 * @var \Drupal\user\UserInterface
38 * The entity to use within tests.
40 * @var \Drupal\entity_test\Entity\EntityTest
47 protected function setUp() {
49 $this->drupalPlaceBlock('system_breadcrumb_block');
50 $this->drupalPlaceBlock('page_title_block');
52 // Create a bundle for entity_test.
53 entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test');
56 'label' => 'Comment settings',
57 'description' => 'Comment settings',
58 'target_entity_type_id' => 'entity_test',
60 // Create comment field on entity_test bundle.
61 $this->addDefaultCommentField('entity_test', 'entity_test');
63 // Verify that bundles are defined correctly.
64 $bundles = \Drupal::entityManager()->getBundleInfo('comment');
65 $this->assertEqual($bundles['comment']['label'], 'Comment settings');
68 $this->adminUser = $this->drupalCreateUser([
69 'administer comments',
70 'skip comment approval',
74 'administer entity_test content',
77 // Enable anonymous and authenticated user comments.
78 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
81 'skip comment approval',
83 user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [
86 'skip comment approval',
89 // Create a test entity.
90 $random_label = $this->randomMachineName();
91 $data = ['type' => 'entity_test', 'name' => $random_label];
92 $this->entity = EntityTest::create($data);
93 $this->entity->save();
99 * @param \Drupal\Core\Entity\EntityInterface|null $entity
100 * Entity to post comment on or NULL to post to the previously loaded page.
101 * @param string $comment
103 * @param string $subject
105 * @param mixed $contact
106 * Set to NULL for no contact info, TRUE to ignore success checking, and
107 * array of values to set contact info.
109 * @return \Drupal\comment\CommentInterface
110 * The new comment entity.
112 public function postComment(EntityInterface $entity, $comment, $subject = '', $contact = NULL) {
114 $edit['comment_body[0][value]'] = $comment;
116 $field = FieldConfig::loadByName('entity_test', 'entity_test', 'comment');
117 $preview_mode = $field->getSetting('preview');
119 // Must get the page before we test for fields.
120 if ($entity !== NULL) {
121 $this->drupalGet('comment/reply/entity_test/' . $entity->id() . '/comment');
124 // Determine the visibility of subject form field.
125 if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) {
126 // Subject input allowed.
127 $edit['subject[0][value]'] = $subject;
130 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.');
133 if ($contact !== NULL && is_array($contact)) {
136 switch ($preview_mode) {
137 case DRUPAL_REQUIRED:
138 // Preview required so no save button should be found.
139 $this->assertNoFieldByName('op', t('Save'), 'Save button not found.');
140 $this->drupalPostForm(NULL, $edit, t('Preview'));
141 // Don't break here so that we can test post-preview field presence and
143 case DRUPAL_OPTIONAL:
144 $this->assertFieldByName('op', t('Preview'), 'Preview button found.');
145 $this->assertFieldByName('op', t('Save'), 'Save button found.');
146 $this->drupalPostForm(NULL, $edit, t('Save'));
149 case DRUPAL_DISABLED:
150 $this->assertNoFieldByName('op', t('Preview'), 'Preview button not found.');
151 $this->assertFieldByName('op', t('Save'), 'Save button found.');
152 $this->drupalPostForm(NULL, $edit, t('Save'));
157 preg_match('/#comment-([0-9]+)/', $this->getURL(), $match);
160 if ($contact !== TRUE) {
161 // If true then attempting to find error message.
163 $this->assertText($subject, 'Comment subject posted.');
165 $this->assertText($comment, 'Comment body posted.');
166 $this->assertTrue((!empty($match) && !empty($match[1])), 'Comment ID found.');
169 if (isset($match[1])) {
170 return Comment::load($match[1]);
175 * Checks current page for specified comment.
177 * @param \Drupal\comment\CommentInterface $comment
178 * The comment object.
180 * Boolean indicating whether the comment is a reply to another comment.
183 * Boolean indicating whether the comment was found.
185 public function commentExists(CommentInterface $comment = NULL, $reply = FALSE) {
187 $regex = '/' . ($reply ? '<div class="indented">(.*?)' : '');
188 $regex .= '<a id="comment-' . $comment->id() . '"(.*?)';
189 $regex .= $comment->getSubject() . '(.*?)';
190 $regex .= $comment->comment_body->value . '(.*?)';
193 return (boolean) preg_match($regex, $this->getRawContent());
201 * Checks whether the commenter's contact information is displayed.
204 * Contact info is available.
206 public function commentContactInfoAvailable() {
207 return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->getRawContent());
211 * Performs the specified operation on the specified comment.
213 * @param object $comment
214 * Comment to perform operation on.
215 * @param string $operation
216 * Operation to perform.
217 * @param bool $approval
218 * Operation is found on approval page.
220 public function performCommentOperation($comment, $operation, $approval = FALSE) {
222 $edit['operation'] = $operation;
223 $edit['comments[' . $comment->id() . ']'] = TRUE;
224 $this->drupalPostForm('admin/content/comment' . ($approval ? '/approval' : ''), $edit, t('Update'));
226 if ($operation == 'delete') {
227 $this->drupalPostForm(NULL, [], t('Delete'));
228 $this->assertRaw(\Drupal::translation()->formatPlural(1, 'Deleted 1 comment.', 'Deleted @count comments.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation]));
231 $this->assertText(t('The update has been performed.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation]));
236 * Gets the comment ID for an unapproved comment.
238 * @param string $subject
239 * Comment subject to find.
244 public function getUnapprovedComment($subject) {
245 $this->drupalGet('admin/content/comment/approval');
246 preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this->getRawContent(), $match);
252 * Tests anonymous comment functionality.
254 public function testCommentFunctionality() {
255 $limited_user = $this->drupalCreateUser([
256 'administer entity_test fields'
258 $this->drupalLogin($limited_user);
259 // Test that default field exists.
260 $this->drupalGet('entity_test/structure/entity_test/fields');
261 $this->assertText(t('Comments'));
262 $this->assertLinkByHref('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
263 // Test widget hidden option is not visible when there's no comments.
264 $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
265 $this->assertResponse(200);
266 $this->assertNoField('edit-default-value-input-comment-und-0-status-0');
267 // Test that field to change cardinality is not available.
268 $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment/storage');
269 $this->assertResponse(200);
270 $this->assertNoField('cardinality_number');
271 $this->assertNoField('cardinality');
273 $this->drupalLogin($this->adminUser);
275 // Test breadcrumb on comment add page.
276 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
277 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
278 $this->assertEqual(current($this->xpath($xpath))->getText(), $this->entity->label(), 'Last breadcrumb item is equal to node title on comment reply page.');
281 /** @var \Drupal\comment\CommentInterface $comment1 */
282 $comment1 = $this->postComment($this->entity, $this->randomMachineName(), $this->randomMachineName());
283 $this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.');
285 // Test breadcrumb on comment reply page.
286 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id());
287 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
288 $this->assertEqual(current($this->xpath($xpath))->getText(), $comment1->getSubject(), 'Last breadcrumb item is equal to comment title on comment reply page.');
290 // Test breadcrumb on comment edit page.
291 $this->drupalGet('comment/' . $comment1->id() . '/edit');
292 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
293 $this->assertEqual(current($this->xpath($xpath))->getText(), $comment1->getSubject(), 'Last breadcrumb item is equal to comment subject on edit page.');
295 // Test breadcrumb on comment delete page.
296 $this->drupalGet('comment/' . $comment1->id() . '/delete');
297 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
298 $this->assertEqual(current($this->xpath($xpath))->getText(), $comment1->getSubject(), 'Last breadcrumb item is equal to comment subject on delete confirm page.');
300 // Unpublish the comment.
301 $this->performCommentOperation($comment1, 'unpublish');
302 $this->drupalGet('admin/content/comment/approval');
303 $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was unpublished.');
305 // Publish the comment.
306 $this->performCommentOperation($comment1, 'publish', TRUE);
307 $this->drupalGet('admin/content/comment');
308 $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was published.');
310 // Delete the comment.
311 $this->performCommentOperation($comment1, 'delete');
312 $this->drupalGet('admin/content/comment');
313 $this->assertNoRaw('comments[' . $comment1->id() . ']', 'Comment was deleted.');
315 // Post another comment.
316 $comment1 = $this->postComment($this->entity, $this->randomMachineName(), $this->randomMachineName());
317 $this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.');
319 // Check that the comment was found.
320 $this->drupalGet('admin/content/comment');
321 $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was published.');
323 // Check that entity access applies to administrative page.
324 $this->assertText($this->entity->label(), 'Name of commented account found.');
325 $limited_user = $this->drupalCreateUser([
326 'administer comments',
328 $this->drupalLogin($limited_user);
329 $this->drupalGet('admin/content/comment');
330 $this->assertNoText($this->entity->label(), 'No commented account name found.');
332 $this->drupalLogout();
334 // Deny anonymous users access to comments.
335 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
336 'access comments' => FALSE,
337 'post comments' => FALSE,
338 'skip comment approval' => FALSE,
339 'view test entity' => TRUE,
342 // Attempt to view comments while disallowed.
343 $this->drupalGet('entity-test/' . $this->entity->id());
344 $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
345 $this->assertNoLink('Add new comment', 'Link to add comment was found.');
347 // Attempt to view test entity comment form while disallowed.
348 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
349 $this->assertResponse(403);
350 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.');
351 $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field not found.');
353 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
354 'access comments' => TRUE,
355 'post comments' => FALSE,
356 'view test entity' => TRUE,
357 'skip comment approval' => FALSE,
359 $this->drupalGet('entity_test/' . $this->entity->id());
360 $this->assertPattern('@<h2[^>]*>Comments</h2>@', 'Comments were displayed.');
361 $this->assertLink('Log in', 0, 'Link to login was found.');
362 $this->assertLink('register', 0, 'Link to register was found.');
363 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.');
364 $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field not found.');
366 // Test the combination of anonymous users being able to post, but not view
367 // comments, to ensure that access to post comments doesn't grant access to
369 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
370 'access comments' => FALSE,
371 'post comments' => TRUE,
372 'skip comment approval' => TRUE,
373 'view test entity' => TRUE,
375 $this->drupalGet('entity_test/' . $this->entity->id());
376 $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
377 $this->assertFieldByName('subject[0][value]', '', 'Subject field found.');
378 $this->assertFieldByName('comment_body[0][value]', '', 'Comment field found.');
380 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id());
381 $this->assertResponse(403);
382 $this->assertNoText($comment1->getSubject(), 'Comment not displayed.');
384 // Test comment field widget changes.
385 $limited_user = $this->drupalCreateUser([
386 'administer entity_test fields',
388 'administer entity_test content',
389 'administer comments',
391 $this->drupalLogin($limited_user);
392 $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
393 $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-0');
394 $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-1');
395 $this->assertFieldChecked('edit-default-value-input-comment-0-status-2');
396 // Test comment option change in field settings.
398 'default_value_input[comment][0][status]' => CommentItemInterface::CLOSED,
399 'settings[anonymous]' => COMMENT_ANONYMOUS_MAY_CONTACT,
401 $this->drupalPostForm(NULL, $edit, t('Save settings'));
402 $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
403 $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-0');
404 $this->assertFieldChecked('edit-default-value-input-comment-0-status-1');
405 $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-2');
406 $this->assertFieldByName('settings[anonymous]', COMMENT_ANONYMOUS_MAY_CONTACT);
408 // Add a new comment-type.
409 $bundle = CommentType::create([
413 'target_entity_type_id' => 'entity_test',
417 // Add a new comment field.
419 'settings[comment_type]' => 'foobar',
421 $this->fieldUIAddNewField('entity_test/structure/entity_test', 'foobar', 'Foobar', 'comment', $storage_edit);
423 // Add a third comment field.
424 $this->fieldUIAddNewField('entity_test/structure/entity_test', 'barfoo', 'BarFoo', 'comment', $storage_edit);
426 // Check the field contains the correct comment type.
427 $field_storage = FieldStorageConfig::load('entity_test.field_barfoo');
428 $this->assertTrue($field_storage);
429 $this->assertEqual($field_storage->getSetting('comment_type'), 'foobar');
430 $this->assertEqual($field_storage->getCardinality(), 1);
432 // Test the new entity commenting inherits default.
433 $random_label = $this->randomMachineName();
434 $data = ['bundle' => 'entity_test', 'name' => $random_label];
435 $new_entity = EntityTest::create($data);
437 $this->drupalGet('entity_test/manage/' . $new_entity->id() . '/edit');
438 $this->assertNoFieldChecked('edit-field-foobar-0-status-1');
439 $this->assertFieldChecked('edit-field-foobar-0-status-2');
440 $this->assertNoField('edit-field-foobar-0-status-0');
442 // @todo Check proper url and form https://www.drupal.org/node/2458323
443 $this->drupalGet('comment/reply/entity_test/comment/' . $new_entity->id());
444 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field found.');
445 $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field found.');
447 // Test removal of comment_body field.
448 $limited_user = $this->drupalCreateUser([
449 'administer entity_test fields',
451 'administer comment fields',
452 'administer comment types',
455 $this->drupalLogin($limited_user);
457 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
458 $this->assertFieldByName('comment_body[0][value]', '', 'Comment body field found.');
459 $this->fieldUIDeleteField('admin/structure/comment/manage/comment', 'comment.comment.comment_body', 'Comment', 'Comment settings');
460 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
461 $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment body field not found.');
462 // Set subject field to autogenerate it.
463 $edit = ['subject[0][value]' => ''];
464 $this->drupalPostForm(NULL, $edit, t('Save'));
468 * Tests comment fields cannot be added to entity types without integer IDs.
470 public function testsNonIntegerIdEntities() {
471 // Create a bundle for entity_test_string_id.
472 entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test_string_id');
473 $limited_user = $this->drupalCreateUser([
474 'administer entity_test_string_id fields',
476 $this->drupalLogin($limited_user);
477 // Visit the Field UI field add page.
478 $this->drupalGet('entity_test_string_id/structure/entity_test/fields/add-field');
479 // Ensure field isn't shown for string IDs.
480 $this->assertNoOption('edit-new-storage-type', 'comment');
481 // Ensure a core field type shown.
482 $this->assertOption('edit-new-storage-type', 'boolean');
484 // Create a bundle for entity_test_no_id.
485 entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test_no_id');
486 $this->drupalLogin($this->drupalCreateUser([
487 'administer entity_test_no_id fields',
489 // Visit the Field UI field add page.
490 $this->drupalGet('entity_test_no_id/structure/entity_test/fields/add-field');
491 // Ensure field isn't shown for empty IDs.
492 $this->assertNoOption('edit-new-storage-type', 'comment');
493 // Ensure a core field type shown.
494 $this->assertOption('edit-new-storage-type', 'boolean');