3 namespace Drupal\comment\Tests;
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\entity_test\Entity\EntityTest;
10 use Drupal\field\Entity\FieldConfig;
11 use Drupal\field\Entity\FieldStorageConfig;
12 use Drupal\field_ui\Tests\FieldUiTestTrait;
13 use Drupal\simpletest\WebTestBase;
14 use Drupal\Core\Entity\EntityInterface;
15 use Drupal\user\RoleInterface;
18 * Tests commenting on a test entity.
22 class CommentNonNodeTest extends WebTestBase {
27 public static $modules = ['comment', 'user', 'field_ui', 'entity_test', 'block'];
30 * An administrative user with permission to configure comment settings.
32 * @var \Drupal\user\UserInterface
37 * The entity to use within tests.
39 * @var \Drupal\entity_test\Entity\EntityTest
46 protected function setUp() {
48 $this->drupalPlaceBlock('system_breadcrumb_block');
49 $this->drupalPlaceBlock('page_title_block');
51 // Create a bundle for entity_test.
52 entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test');
55 'label' => 'Comment settings',
56 'description' => 'Comment settings',
57 'target_entity_type_id' => 'entity_test',
59 // Create comment field on entity_test bundle.
60 $this->addDefaultCommentField('entity_test', 'entity_test');
62 // Verify that bundles are defined correctly.
63 $bundles = \Drupal::entityManager()->getBundleInfo('comment');
64 $this->assertEqual($bundles['comment']['label'], 'Comment settings');
67 $this->adminUser = $this->drupalCreateUser([
68 'administer comments',
69 'skip comment approval',
73 'administer entity_test content',
76 // Enable anonymous and authenticated user comments.
77 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
80 'skip comment approval',
82 user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [
85 'skip comment approval',
88 // Create a test entity.
89 $random_label = $this->randomMachineName();
90 $data = ['type' => 'entity_test', 'name' => $random_label];
91 $this->entity = EntityTest::create($data);
92 $this->entity->save();
98 * @param \Drupal\Core\Entity\EntityInterface|null $entity
99 * Entity to post comment on or NULL to post to the previously loaded page.
100 * @param string $comment
102 * @param string $subject
104 * @param mixed $contact
105 * Set to NULL for no contact info, TRUE to ignore success checking, and
106 * array of values to set contact info.
108 * @return \Drupal\comment\CommentInterface
109 * The new comment entity.
111 public function postComment(EntityInterface $entity, $comment, $subject = '', $contact = NULL) {
113 $edit['comment_body[0][value]'] = $comment;
115 $field = FieldConfig::loadByName('entity_test', 'entity_test', 'comment');
116 $preview_mode = $field->getSetting('preview');
118 // Must get the page before we test for fields.
119 if ($entity !== NULL) {
120 $this->drupalGet('comment/reply/entity_test/' . $entity->id() . '/comment');
123 // Determine the visibility of subject form field.
124 if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) {
125 // Subject input allowed.
126 $edit['subject[0][value]'] = $subject;
129 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.');
132 if ($contact !== NULL && is_array($contact)) {
135 switch ($preview_mode) {
136 case DRUPAL_REQUIRED:
137 // Preview required so no save button should be found.
138 $this->assertNoFieldByName('op', t('Save'), 'Save button not found.');
139 $this->drupalPostForm(NULL, $edit, t('Preview'));
140 // Don't break here so that we can test post-preview field presence and
142 case DRUPAL_OPTIONAL:
143 $this->assertFieldByName('op', t('Preview'), 'Preview button found.');
144 $this->assertFieldByName('op', t('Save'), 'Save button found.');
145 $this->drupalPostForm(NULL, $edit, t('Save'));
148 case DRUPAL_DISABLED:
149 $this->assertNoFieldByName('op', t('Preview'), 'Preview button not found.');
150 $this->assertFieldByName('op', t('Save'), 'Save button found.');
151 $this->drupalPostForm(NULL, $edit, t('Save'));
156 preg_match('/#comment-([0-9]+)/', $this->getURL(), $match);
159 if ($contact !== TRUE) { // If true then attempting to find error message.
161 $this->assertText($subject, 'Comment subject posted.');
163 $this->assertText($comment, 'Comment body posted.');
164 $this->assertTrue((!empty($match) && !empty($match[1])), 'Comment ID found.');
167 if (isset($match[1])) {
168 return Comment::load($match[1]);
173 * Checks current page for specified comment.
175 * @param \Drupal\comment\CommentInterface $comment
176 * The comment object.
178 * Boolean indicating whether the comment is a reply to another comment.
181 * Boolean indicating whether the comment was found.
183 public function commentExists(CommentInterface $comment = NULL, $reply = FALSE) {
185 $regex = '/' . ($reply ? '<div class="indented">(.*?)' : '');
186 $regex .= '<a id="comment-' . $comment->id() . '"(.*?)';
187 $regex .= $comment->getSubject() . '(.*?)';
188 $regex .= $comment->comment_body->value . '(.*?)';
191 return (boolean) preg_match($regex, $this->getRawContent());
199 * Checks whether the commenter's contact information is displayed.
202 * Contact info is available.
204 public function commentContactInfoAvailable() {
205 return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->getRawContent());
209 * Performs the specified operation on the specified comment.
211 * @param object $comment
212 * Comment to perform operation on.
213 * @param string $operation
214 * Operation to perform.
215 * @param bool $approval
216 * Operation is found on approval page.
218 public function performCommentOperation($comment, $operation, $approval = FALSE) {
220 $edit['operation'] = $operation;
221 $edit['comments[' . $comment->id() . ']'] = TRUE;
222 $this->drupalPostForm('admin/content/comment' . ($approval ? '/approval' : ''), $edit, t('Update'));
224 if ($operation == 'delete') {
225 $this->drupalPostForm(NULL, [], t('Delete comments'));
226 $this->assertRaw(\Drupal::translation()->formatPlural(1, 'Deleted 1 comment.', 'Deleted @count comments.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation]));
229 $this->assertText(t('The update has been performed.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation]));
234 * Gets the comment ID for an unapproved comment.
236 * @param string $subject
237 * Comment subject to find.
242 public function getUnapprovedComment($subject) {
243 $this->drupalGet('admin/content/comment/approval');
244 preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this->getRawContent(), $match);
250 * Tests anonymous comment functionality.
252 public function testCommentFunctionality() {
253 $limited_user = $this->drupalCreateUser([
254 'administer entity_test fields'
256 $this->drupalLogin($limited_user);
257 // Test that default field exists.
258 $this->drupalGet('entity_test/structure/entity_test/fields');
259 $this->assertText(t('Comments'));
260 $this->assertLinkByHref('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
261 // Test widget hidden option is not visible when there's no comments.
262 $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
263 $this->assertResponse(200);
264 $this->assertNoField('edit-default-value-input-comment-und-0-status-0');
265 // Test that field to change cardinality is not available.
266 $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment/storage');
267 $this->assertResponse(200);
268 $this->assertNoField('cardinality_number');
269 $this->assertNoField('cardinality');
271 $this->drupalLogin($this->adminUser);
273 // Test breadcrumb on comment add page.
274 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
275 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
276 $this->assertEqual(current($this->xpath($xpath)), $this->entity->label(), 'Last breadcrumb item is equal to node title on comment reply page.');
279 /** @var \Drupal\comment\CommentInterface $comment1 */
280 $comment1 = $this->postComment($this->entity, $this->randomMachineName(), $this->randomMachineName());
281 $this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.');
283 // Test breadcrumb on comment reply page.
284 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id());
285 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
286 $this->assertEqual(current($this->xpath($xpath)), $comment1->getSubject(), 'Last breadcrumb item is equal to comment title on comment reply page.');
288 // Test breadcrumb on comment edit page.
289 $this->drupalGet('comment/' . $comment1->id() . '/edit');
290 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
291 $this->assertEqual(current($this->xpath($xpath)), $comment1->getSubject(), 'Last breadcrumb item is equal to comment subject on edit page.');
293 // Test breadcrumb on comment delete page.
294 $this->drupalGet('comment/' . $comment1->id() . '/delete');
295 $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
296 $this->assertEqual(current($this->xpath($xpath)), $comment1->getSubject(), 'Last breadcrumb item is equal to comment subject on delete confirm page.');
298 // Unpublish the comment.
299 $this->performCommentOperation($comment1, 'unpublish');
300 $this->drupalGet('admin/content/comment/approval');
301 $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was unpublished.');
303 // Publish the comment.
304 $this->performCommentOperation($comment1, 'publish', TRUE);
305 $this->drupalGet('admin/content/comment');
306 $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was published.');
308 // Delete the comment.
309 $this->performCommentOperation($comment1, 'delete');
310 $this->drupalGet('admin/content/comment');
311 $this->assertNoRaw('comments[' . $comment1->id() . ']', 'Comment was deleted.');
313 // Post another comment.
314 $comment1 = $this->postComment($this->entity, $this->randomMachineName(), $this->randomMachineName());
315 $this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.');
317 // Check that the comment was found.
318 $this->drupalGet('admin/content/comment');
319 $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was published.');
321 // Check that entity access applies to administrative page.
322 $this->assertText($this->entity->label(), 'Name of commented account found.');
323 $limited_user = $this->drupalCreateUser([
324 'administer comments',
326 $this->drupalLogin($limited_user);
327 $this->drupalGet('admin/content/comment');
328 $this->assertNoText($this->entity->label(), 'No commented account name found.');
330 $this->drupalLogout();
332 // Deny anonymous users access to comments.
333 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
334 'access comments' => FALSE,
335 'post comments' => FALSE,
336 'skip comment approval' => FALSE,
337 'view test entity' => TRUE,
340 // Attempt to view comments while disallowed.
341 $this->drupalGet('entity-test/' . $this->entity->id());
342 $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
343 $this->assertNoLink('Add new comment', 'Link to add comment was found.');
345 // Attempt to view test entity comment form while disallowed.
346 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
347 $this->assertResponse(403);
348 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.');
349 $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field not found.');
351 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
352 'access comments' => TRUE,
353 'post comments' => FALSE,
354 'view test entity' => TRUE,
355 'skip comment approval' => FALSE,
357 $this->drupalGet('entity_test/' . $this->entity->id());
358 $this->assertPattern('@<h2[^>]*>Comments</h2>@', 'Comments were displayed.');
359 $this->assertLink('Log in', 0, 'Link to login was found.');
360 $this->assertLink('register', 0, 'Link to register was found.');
361 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.');
362 $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field not found.');
364 // Test the combination of anonymous users being able to post, but not view
365 // comments, to ensure that access to post comments doesn't grant access to
367 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
368 'access comments' => FALSE,
369 'post comments' => TRUE,
370 'skip comment approval' => TRUE,
371 'view test entity' => TRUE,
373 $this->drupalGet('entity_test/' . $this->entity->id());
374 $this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
375 $this->assertFieldByName('subject[0][value]', '', 'Subject field found.');
376 $this->assertFieldByName('comment_body[0][value]', '', 'Comment field found.');
378 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id());
379 $this->assertResponse(403);
380 $this->assertNoText($comment1->getSubject(), 'Comment not displayed.');
382 // Test comment field widget changes.
383 $limited_user = $this->drupalCreateUser([
384 'administer entity_test fields',
386 'administer entity_test content',
387 'administer comments',
389 $this->drupalLogin($limited_user);
390 $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
391 $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-0');
392 $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-1');
393 $this->assertFieldChecked('edit-default-value-input-comment-0-status-2');
394 // Test comment option change in field settings.
396 'default_value_input[comment][0][status]' => CommentItemInterface::CLOSED,
397 'settings[anonymous]' => COMMENT_ANONYMOUS_MAY_CONTACT,
399 $this->drupalPostForm(NULL, $edit, t('Save settings'));
400 $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
401 $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-0');
402 $this->assertFieldChecked('edit-default-value-input-comment-0-status-1');
403 $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-2');
404 $this->assertFieldByName('settings[anonymous]', COMMENT_ANONYMOUS_MAY_CONTACT);
406 // Add a new comment-type.
407 $bundle = CommentType::create([
411 'target_entity_type_id' => 'entity_test',
415 // Add a new comment field.
417 'settings[comment_type]' => 'foobar',
419 $this->fieldUIAddNewField('entity_test/structure/entity_test', 'foobar', 'Foobar', 'comment', $storage_edit);
421 // Add a third comment field.
422 $this->fieldUIAddNewField('entity_test/structure/entity_test', 'barfoo', 'BarFoo', 'comment', $storage_edit);
424 // Check the field contains the correct comment type.
425 $field_storage = FieldStorageConfig::load('entity_test.field_barfoo');
426 $this->assertTrue($field_storage);
427 $this->assertEqual($field_storage->getSetting('comment_type'), 'foobar');
428 $this->assertEqual($field_storage->getCardinality(), 1);
430 // Test the new entity commenting inherits default.
431 $random_label = $this->randomMachineName();
432 $data = ['bundle' => 'entity_test', 'name' => $random_label];
433 $new_entity = EntityTest::create($data);
435 $this->drupalGet('entity_test/manage/' . $new_entity->id() . '/edit');
436 $this->assertNoFieldChecked('edit-field-foobar-0-status-1');
437 $this->assertFieldChecked('edit-field-foobar-0-status-2');
438 $this->assertNoField('edit-field-foobar-0-status-0');
440 // @todo Check proper url and form https://www.drupal.org/node/2458323
441 $this->drupalGet('comment/reply/entity_test/comment/' . $new_entity->id());
442 $this->assertNoFieldByName('subject[0][value]', '', 'Subject field found.');
443 $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field found.');
445 // Test removal of comment_body field.
446 $limited_user = $this->drupalCreateUser([
447 'administer entity_test fields',
449 'administer comment fields',
450 'administer comment types',
452 $this->drupalLogin($limited_user);
454 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
455 $this->assertFieldByName('comment_body[0][value]', '', 'Comment body field found.');
456 $this->fieldUIDeleteField('admin/structure/comment/manage/comment', 'comment.comment.comment_body', 'Comment', 'Comment settings');
457 $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
458 $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment body field not found.');
459 // Set subject field to autogenerate it.
460 $edit = ['subject[0][value]' => ''];
461 $this->drupalPostForm(NULL, $edit, t('Save'));
465 * Tests comment fields cannot be added to entity types without integer IDs.
467 public function testsNonIntegerIdEntities() {
468 // Create a bundle for entity_test_string_id.
469 entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test_string_id');
470 $limited_user = $this->drupalCreateUser([
471 'administer entity_test_string_id fields',
473 $this->drupalLogin($limited_user);
474 // Visit the Field UI field add page.
475 $this->drupalGet('entity_test_string_id/structure/entity_test/fields/add-field');
476 // Ensure field isn't shown for string IDs.
477 $this->assertNoOption('edit-new-storage-type', 'comment');
478 // Ensure a core field type shown.
479 $this->assertOption('edit-new-storage-type', 'boolean');
481 // Create a bundle for entity_test_no_id.
482 entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test_no_id');
483 $this->drupalLogin($this->drupalCreateUser([
484 'administer entity_test_no_id fields',
486 // Visit the Field UI field add page.
487 $this->drupalGet('entity_test_no_id/structure/entity_test/fields/add-field');
488 // Ensure field isn't shown for empty IDs.
489 $this->assertNoOption('edit-new-storage-type', 'comment');
490 // Ensure a core field type shown.
491 $this->assertOption('edit-new-storage-type', 'boolean');