Chris@0: drupalPlaceBlock('system_breadcrumb_block'); Chris@0: $this->drupalPlaceBlock('page_title_block'); Chris@0: Chris@0: // Create a bundle for entity_test. Chris@0: entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test'); Chris@0: CommentType::create([ Chris@0: 'id' => 'comment', Chris@0: 'label' => 'Comment settings', Chris@0: 'description' => 'Comment settings', Chris@0: 'target_entity_type_id' => 'entity_test', Chris@0: ])->save(); Chris@0: // Create comment field on entity_test bundle. Chris@0: $this->addDefaultCommentField('entity_test', 'entity_test'); Chris@0: Chris@0: // Verify that bundles are defined correctly. Chris@18: $bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('comment'); Chris@0: $this->assertEqual($bundles['comment']['label'], 'Comment settings'); Chris@0: Chris@0: // Create test user. Chris@0: $this->adminUser = $this->drupalCreateUser([ Chris@0: 'administer comments', Chris@0: 'skip comment approval', Chris@0: 'post comments', Chris@0: 'access comments', Chris@0: 'view test entity', Chris@0: 'administer entity_test content', Chris@0: ]); Chris@0: Chris@0: // Enable anonymous and authenticated user comments. Chris@0: user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [ Chris@0: 'access comments', Chris@0: 'post comments', Chris@0: 'skip comment approval', Chris@0: ]); Chris@0: user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [ Chris@0: 'access comments', Chris@0: 'post comments', Chris@0: 'skip comment approval', Chris@0: ]); Chris@0: Chris@0: // Create a test entity. Chris@0: $random_label = $this->randomMachineName(); Chris@0: $data = ['type' => 'entity_test', 'name' => $random_label]; Chris@0: $this->entity = EntityTest::create($data); Chris@0: $this->entity->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Posts a comment. Chris@0: * Chris@0: * @param \Drupal\Core\Entity\EntityInterface|null $entity Chris@0: * Entity to post comment on or NULL to post to the previously loaded page. Chris@0: * @param string $comment Chris@0: * Comment body. Chris@0: * @param string $subject Chris@0: * Comment subject. Chris@0: * @param mixed $contact Chris@0: * Set to NULL for no contact info, TRUE to ignore success checking, and Chris@0: * array of values to set contact info. Chris@0: * Chris@0: * @return \Drupal\comment\CommentInterface Chris@0: * The new comment entity. Chris@0: */ Chris@0: public function postComment(EntityInterface $entity, $comment, $subject = '', $contact = NULL) { Chris@0: $edit = []; Chris@0: $edit['comment_body[0][value]'] = $comment; Chris@0: Chris@0: $field = FieldConfig::loadByName('entity_test', 'entity_test', 'comment'); Chris@0: $preview_mode = $field->getSetting('preview'); Chris@0: Chris@0: // Must get the page before we test for fields. Chris@0: if ($entity !== NULL) { Chris@0: $this->drupalGet('comment/reply/entity_test/' . $entity->id() . '/comment'); Chris@0: } Chris@0: Chris@0: // Determine the visibility of subject form field. Chris@0: if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) { Chris@0: // Subject input allowed. Chris@0: $edit['subject[0][value]'] = $subject; Chris@0: } Chris@0: else { Chris@0: $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.'); Chris@0: } Chris@0: Chris@0: if ($contact !== NULL && is_array($contact)) { Chris@0: $edit += $contact; Chris@0: } Chris@0: switch ($preview_mode) { Chris@0: case DRUPAL_REQUIRED: Chris@0: // Preview required so no save button should be found. Chris@0: $this->assertNoFieldByName('op', t('Save'), 'Save button not found.'); Chris@0: $this->drupalPostForm(NULL, $edit, t('Preview')); Chris@0: // Don't break here so that we can test post-preview field presence and Chris@0: // function below. Chris@0: case DRUPAL_OPTIONAL: Chris@0: $this->assertFieldByName('op', t('Preview'), 'Preview button found.'); Chris@0: $this->assertFieldByName('op', t('Save'), 'Save button found.'); Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: break; Chris@0: Chris@0: case DRUPAL_DISABLED: Chris@0: $this->assertNoFieldByName('op', t('Preview'), 'Preview button not found.'); Chris@0: $this->assertFieldByName('op', t('Save'), 'Save button found.'); Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: break; Chris@0: } Chris@0: $match = []; Chris@0: // Get comment ID Chris@0: preg_match('/#comment-([0-9]+)/', $this->getURL(), $match); Chris@0: Chris@0: // Get comment. Chris@0: if ($contact !== TRUE) { Chris@0: // If true then attempting to find error message. Chris@0: if ($subject) { Chris@0: $this->assertText($subject, 'Comment subject posted.'); Chris@0: } Chris@0: $this->assertText($comment, 'Comment body posted.'); Chris@0: $this->assertTrue((!empty($match) && !empty($match[1])), 'Comment ID found.'); Chris@0: } Chris@0: Chris@0: if (isset($match[1])) { Chris@0: return Comment::load($match[1]); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks current page for specified comment. Chris@0: * Chris@0: * @param \Drupal\comment\CommentInterface $comment Chris@0: * The comment object. Chris@0: * @param bool $reply Chris@0: * Boolean indicating whether the comment is a reply to another comment. Chris@0: * Chris@0: * @return bool Chris@0: * Boolean indicating whether the comment was found. Chris@0: */ Chris@0: public function commentExists(CommentInterface $comment = NULL, $reply = FALSE) { Chris@0: if ($comment) { Chris@0: $regex = '/' . ($reply ? '
(.*?)' : ''); Chris@18: $regex .= 'id() . '"(.*?)'; Chris@0: $regex .= $comment->getSubject() . '(.*?)'; Chris@0: $regex .= $comment->comment_body->value . '(.*?)'; Chris@0: $regex .= '/s'; Chris@0: Chris@17: return (boolean) preg_match($regex, $this->getSession()->getPage()->getContent()); Chris@0: } Chris@0: else { Chris@0: return FALSE; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether the commenter's contact information is displayed. Chris@0: * Chris@0: * @return bool Chris@0: * Contact info is available. Chris@0: */ Chris@0: public function commentContactInfoAvailable() { Chris@17: return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->getSession()->getPage()->getContent()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Performs the specified operation on the specified comment. Chris@0: * Chris@0: * @param object $comment Chris@0: * Comment to perform operation on. Chris@0: * @param string $operation Chris@0: * Operation to perform. Chris@0: * @param bool $approval Chris@0: * Operation is found on approval page. Chris@0: */ Chris@0: public function performCommentOperation($comment, $operation, $approval = FALSE) { Chris@0: $edit = []; Chris@0: $edit['operation'] = $operation; Chris@0: $edit['comments[' . $comment->id() . ']'] = TRUE; Chris@0: $this->drupalPostForm('admin/content/comment' . ($approval ? '/approval' : ''), $edit, t('Update')); Chris@0: Chris@0: if ($operation == 'delete') { Chris@0: $this->drupalPostForm(NULL, [], t('Delete')); Chris@0: $this->assertRaw(\Drupal::translation()->formatPlural(1, 'Deleted 1 comment.', 'Deleted @count comments.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation])); Chris@0: } Chris@0: else { Chris@0: $this->assertText(t('The update has been performed.'), format_string('Operation "@operation" was performed on comment.', ['@operation' => $operation])); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the comment ID for an unapproved comment. Chris@0: * Chris@0: * @param string $subject Chris@0: * Comment subject to find. Chris@0: * Chris@0: * @return int Chris@0: * Comment ID. Chris@0: */ Chris@0: public function getUnapprovedComment($subject) { Chris@0: $this->drupalGet('admin/content/comment/approval'); Chris@17: preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this->getSession()->getPage()->getContent(), $match); Chris@0: Chris@0: return $match[2]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests anonymous comment functionality. Chris@0: */ Chris@0: public function testCommentFunctionality() { Chris@0: $limited_user = $this->drupalCreateUser([ Chris@17: 'administer entity_test fields', Chris@0: ]); Chris@0: $this->drupalLogin($limited_user); Chris@0: // Test that default field exists. Chris@0: $this->drupalGet('entity_test/structure/entity_test/fields'); Chris@0: $this->assertText(t('Comments')); Chris@0: $this->assertLinkByHref('entity_test/structure/entity_test/fields/entity_test.entity_test.comment'); Chris@0: // Test widget hidden option is not visible when there's no comments. Chris@0: $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertNoField('edit-default-value-input-comment-und-0-status-0'); Chris@0: // Test that field to change cardinality is not available. Chris@0: $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment/storage'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertNoField('cardinality_number'); Chris@0: $this->assertNoField('cardinality'); Chris@0: Chris@0: $this->drupalLogin($this->adminUser); Chris@0: Chris@0: // Test breadcrumb on comment add page. Chris@0: $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment'); Chris@0: $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; Chris@0: $this->assertEqual(current($this->xpath($xpath))->getText(), $this->entity->label(), 'Last breadcrumb item is equal to node title on comment reply page.'); Chris@0: Chris@0: // Post a comment. Chris@0: /** @var \Drupal\comment\CommentInterface $comment1 */ Chris@0: $comment1 = $this->postComment($this->entity, $this->randomMachineName(), $this->randomMachineName()); Chris@0: $this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.'); Chris@0: Chris@0: // Test breadcrumb on comment reply page. Chris@0: $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id()); Chris@0: $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; Chris@0: $this->assertEqual(current($this->xpath($xpath))->getText(), $comment1->getSubject(), 'Last breadcrumb item is equal to comment title on comment reply page.'); Chris@0: Chris@0: // Test breadcrumb on comment edit page. Chris@0: $this->drupalGet('comment/' . $comment1->id() . '/edit'); Chris@0: $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; Chris@0: $this->assertEqual(current($this->xpath($xpath))->getText(), $comment1->getSubject(), 'Last breadcrumb item is equal to comment subject on edit page.'); Chris@0: Chris@0: // Test breadcrumb on comment delete page. Chris@0: $this->drupalGet('comment/' . $comment1->id() . '/delete'); Chris@0: $xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a'; Chris@0: $this->assertEqual(current($this->xpath($xpath))->getText(), $comment1->getSubject(), 'Last breadcrumb item is equal to comment subject on delete confirm page.'); Chris@0: Chris@0: // Unpublish the comment. Chris@0: $this->performCommentOperation($comment1, 'unpublish'); Chris@0: $this->drupalGet('admin/content/comment/approval'); Chris@0: $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was unpublished.'); Chris@0: Chris@0: // Publish the comment. Chris@0: $this->performCommentOperation($comment1, 'publish', TRUE); Chris@0: $this->drupalGet('admin/content/comment'); Chris@0: $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was published.'); Chris@0: Chris@0: // Delete the comment. Chris@0: $this->performCommentOperation($comment1, 'delete'); Chris@0: $this->drupalGet('admin/content/comment'); Chris@0: $this->assertNoRaw('comments[' . $comment1->id() . ']', 'Comment was deleted.'); Chris@0: Chris@0: // Post another comment. Chris@0: $comment1 = $this->postComment($this->entity, $this->randomMachineName(), $this->randomMachineName()); Chris@0: $this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.'); Chris@0: Chris@0: // Check that the comment was found. Chris@0: $this->drupalGet('admin/content/comment'); Chris@0: $this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was published.'); Chris@0: Chris@0: // Check that entity access applies to administrative page. Chris@0: $this->assertText($this->entity->label(), 'Name of commented account found.'); Chris@0: $limited_user = $this->drupalCreateUser([ Chris@0: 'administer comments', Chris@0: ]); Chris@0: $this->drupalLogin($limited_user); Chris@0: $this->drupalGet('admin/content/comment'); Chris@0: $this->assertNoText($this->entity->label(), 'No commented account name found.'); Chris@0: Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Deny anonymous users access to comments. Chris@0: user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [ Chris@0: 'access comments' => FALSE, Chris@0: 'post comments' => FALSE, Chris@0: 'skip comment approval' => FALSE, Chris@0: 'view test entity' => TRUE, Chris@0: ]); Chris@0: Chris@0: // Attempt to view comments while disallowed. Chris@0: $this->drupalGet('entity-test/' . $this->entity->id()); Chris@18: $this->assertSession()->responseNotMatches('@]*>Comments@', 'Comments were not displayed.'); Chris@0: $this->assertNoLink('Add new comment', 'Link to add comment was found.'); Chris@0: Chris@0: // Attempt to view test entity comment form while disallowed. Chris@0: $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment'); Chris@0: $this->assertResponse(403); Chris@0: $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.'); Chris@0: $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field not found.'); Chris@0: Chris@0: user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [ Chris@0: 'access comments' => TRUE, Chris@0: 'post comments' => FALSE, Chris@0: 'view test entity' => TRUE, Chris@0: 'skip comment approval' => FALSE, Chris@0: ]); Chris@0: $this->drupalGet('entity_test/' . $this->entity->id()); Chris@0: $this->assertPattern('@]*>Comments@', 'Comments were displayed.'); Chris@0: $this->assertLink('Log in', 0, 'Link to login was found.'); Chris@0: $this->assertLink('register', 0, 'Link to register was found.'); Chris@0: $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.'); Chris@0: $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field not found.'); Chris@0: Chris@0: // Test the combination of anonymous users being able to post, but not view Chris@0: // comments, to ensure that access to post comments doesn't grant access to Chris@0: // view them. Chris@0: user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [ Chris@0: 'access comments' => FALSE, Chris@0: 'post comments' => TRUE, Chris@0: 'skip comment approval' => TRUE, Chris@0: 'view test entity' => TRUE, Chris@0: ]); Chris@0: $this->drupalGet('entity_test/' . $this->entity->id()); Chris@18: $this->assertSession()->responseNotMatches('@]*>Comments@', 'Comments were not displayed.'); Chris@0: $this->assertFieldByName('subject[0][value]', '', 'Subject field found.'); Chris@0: $this->assertFieldByName('comment_body[0][value]', '', 'Comment field found.'); Chris@0: Chris@0: $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id()); Chris@0: $this->assertResponse(403); Chris@0: $this->assertNoText($comment1->getSubject(), 'Comment not displayed.'); Chris@0: Chris@0: // Test comment field widget changes. Chris@0: $limited_user = $this->drupalCreateUser([ Chris@0: 'administer entity_test fields', Chris@0: 'view test entity', Chris@0: 'administer entity_test content', Chris@0: 'administer comments', Chris@0: ]); Chris@0: $this->drupalLogin($limited_user); Chris@0: $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment'); Chris@0: $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-0'); Chris@0: $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-1'); Chris@0: $this->assertFieldChecked('edit-default-value-input-comment-0-status-2'); Chris@0: // Test comment option change in field settings. Chris@0: $edit = [ Chris@0: 'default_value_input[comment][0][status]' => CommentItemInterface::CLOSED, Chris@18: 'settings[anonymous]' => CommentInterface::ANONYMOUS_MAY_CONTACT, Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save settings')); Chris@0: $this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment'); Chris@0: $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-0'); Chris@0: $this->assertFieldChecked('edit-default-value-input-comment-0-status-1'); Chris@0: $this->assertNoFieldChecked('edit-default-value-input-comment-0-status-2'); Chris@18: $this->assertFieldByName('settings[anonymous]', CommentInterface::ANONYMOUS_MAY_CONTACT); Chris@0: Chris@0: // Add a new comment-type. Chris@0: $bundle = CommentType::create([ Chris@0: 'id' => 'foobar', Chris@0: 'label' => 'Foobar', Chris@0: 'description' => '', Chris@0: 'target_entity_type_id' => 'entity_test', Chris@0: ]); Chris@0: $bundle->save(); Chris@0: Chris@0: // Add a new comment field. Chris@0: $storage_edit = [ Chris@0: 'settings[comment_type]' => 'foobar', Chris@0: ]; Chris@0: $this->fieldUIAddNewField('entity_test/structure/entity_test', 'foobar', 'Foobar', 'comment', $storage_edit); Chris@0: Chris@0: // Add a third comment field. Chris@0: $this->fieldUIAddNewField('entity_test/structure/entity_test', 'barfoo', 'BarFoo', 'comment', $storage_edit); Chris@0: Chris@0: // Check the field contains the correct comment type. Chris@0: $field_storage = FieldStorageConfig::load('entity_test.field_barfoo'); Chris@0: $this->assertTrue($field_storage); Chris@0: $this->assertEqual($field_storage->getSetting('comment_type'), 'foobar'); Chris@0: $this->assertEqual($field_storage->getCardinality(), 1); Chris@0: Chris@0: // Test the new entity commenting inherits default. Chris@0: $random_label = $this->randomMachineName(); Chris@0: $data = ['bundle' => 'entity_test', 'name' => $random_label]; Chris@0: $new_entity = EntityTest::create($data); Chris@0: $new_entity->save(); Chris@0: $this->drupalGet('entity_test/manage/' . $new_entity->id() . '/edit'); Chris@0: $this->assertNoFieldChecked('edit-field-foobar-0-status-1'); Chris@0: $this->assertFieldChecked('edit-field-foobar-0-status-2'); Chris@0: $this->assertNoField('edit-field-foobar-0-status-0'); Chris@0: Chris@0: // @todo Check proper url and form https://www.drupal.org/node/2458323 Chris@0: $this->drupalGet('comment/reply/entity_test/comment/' . $new_entity->id()); Chris@0: $this->assertNoFieldByName('subject[0][value]', '', 'Subject field found.'); Chris@0: $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field found.'); Chris@0: Chris@0: // Test removal of comment_body field. Chris@0: $limited_user = $this->drupalCreateUser([ Chris@0: 'administer entity_test fields', Chris@0: 'post comments', Chris@0: 'administer comment fields', Chris@0: 'administer comment types', Chris@12: 'view test entity', Chris@0: ]); Chris@0: $this->drupalLogin($limited_user); Chris@0: Chris@0: $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment'); Chris@0: $this->assertFieldByName('comment_body[0][value]', '', 'Comment body field found.'); Chris@0: $this->fieldUIDeleteField('admin/structure/comment/manage/comment', 'comment.comment.comment_body', 'Comment', 'Comment settings'); Chris@0: $this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment'); Chris@0: $this->assertNoFieldByName('comment_body[0][value]', '', 'Comment body field not found.'); Chris@0: // Set subject field to autogenerate it. Chris@0: $edit = ['subject[0][value]' => '']; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests comment fields cannot be added to entity types without integer IDs. Chris@0: */ Chris@0: public function testsNonIntegerIdEntities() { Chris@0: // Create a bundle for entity_test_string_id. Chris@0: entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test_string_id'); Chris@0: $limited_user = $this->drupalCreateUser([ Chris@0: 'administer entity_test_string_id fields', Chris@0: ]); Chris@0: $this->drupalLogin($limited_user); Chris@0: // Visit the Field UI field add page. Chris@0: $this->drupalGet('entity_test_string_id/structure/entity_test/fields/add-field'); Chris@0: // Ensure field isn't shown for string IDs. Chris@0: $this->assertNoOption('edit-new-storage-type', 'comment'); Chris@0: // Ensure a core field type shown. Chris@0: $this->assertOption('edit-new-storage-type', 'boolean'); Chris@0: Chris@0: // Create a bundle for entity_test_no_id. Chris@0: entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test_no_id'); Chris@0: $this->drupalLogin($this->drupalCreateUser([ Chris@0: 'administer entity_test_no_id fields', Chris@0: ])); Chris@0: // Visit the Field UI field add page. Chris@0: $this->drupalGet('entity_test_no_id/structure/entity_test/fields/add-field'); Chris@0: // Ensure field isn't shown for empty IDs. Chris@0: $this->assertNoOption('edit-new-storage-type', 'comment'); Chris@0: // Ensure a core field type shown. Chris@0: $this->assertOption('edit-new-storage-type', 'boolean'); Chris@0: } Chris@0: Chris@0: }