drupalPlaceBlock('system_breadcrumb_block'); $this->drupalPlaceBlock('page_title_block'); // Create a bundle for entity_test. entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test'); CommentType::create([ 'id' => 'comment', 'label' => 'Comment settings', 'description' => 'Comment settings', 'target_entity_type_id' => 'entity_test', ])->save(); // Create comment field on entity_test bundle. $this->addDefaultCommentField('entity_test', 'entity_test'); // Verify that bundles are defined correctly. $bundles = \Drupal::entityManager()->getBundleInfo('comment'); $this->assertEqual($bundles['comment']['label'], 'Comment settings'); // Create test user. $this->adminUser = $this->drupalCreateUser([ 'administer comments', 'skip comment approval', 'post comments', 'access comments', 'view test entity', 'administer entity_test content', ]); // Enable anonymous and authenticated user comments. user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [ 'access comments', 'post comments', 'skip comment approval', ]); user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [ 'access comments', 'post comments', 'skip comment approval', ]); // Create a test entity. $random_label = $this->randomMachineName(); $data = ['type' => 'entity_test', 'name' => $random_label]; $this->entity = EntityTest::create($data); $this->entity->save(); } /** * Posts a comment. * * @param \Drupal\Core\Entity\EntityInterface|null $entity * Entity to post comment on or NULL to post to the previously loaded page. * @param string $comment * Comment body. * @param string $subject * Comment subject. * @param mixed $contact * Set to NULL for no contact info, TRUE to ignore success checking, and * array of values to set contact info. * * @return \Drupal\comment\CommentInterface * The new comment entity. */ public function postComment(EntityInterface $entity, $comment, $subject = '', $contact = NULL) { $edit = []; $edit['comment_body[0][value]'] = $comment; $field = FieldConfig::loadByName('entity_test', 'entity_test', 'comment'); $preview_mode = $field->getSetting('preview'); // Must get the page before we test for fields. if ($entity !== NULL) { $this->drupalGet('comment/reply/entity_test/' . $entity->id() . '/comment'); } // Determine the visibility of subject form field. if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) { // Subject input allowed. $edit['subject[0][value]'] = $subject; } else { $this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.'); } if ($contact !== NULL && is_array($contact)) { $edit += $contact; } switch ($preview_mode) { case DRUPAL_REQUIRED: // Preview required so no save button should be found. $this->assertNoFieldByName('op', t('Save'), 'Save button not found.'); $this->drupalPostForm(NULL, $edit, t('Preview')); // Don't break here so that we can test post-preview field presence and // function below. case DRUPAL_OPTIONAL: $this->assertFieldByName('op', t('Preview'), 'Preview button found.'); $this->assertFieldByName('op', t('Save'), 'Save button found.'); $this->drupalPostForm(NULL, $edit, t('Save')); break; case DRUPAL_DISABLED: $this->assertNoFieldByName('op', t('Preview'), 'Preview button not found.'); $this->assertFieldByName('op', t('Save'), 'Save button found.'); $this->drupalPostForm(NULL, $edit, t('Save')); break; } $match = []; // Get comment ID preg_match('/#comment-([0-9]+)/', $this->getURL(), $match); // Get comment. if ($contact !== TRUE) { // If true then attempting to find error message. if ($subject) { $this->assertText($subject, 'Comment subject posted.'); } $this->assertText($comment, 'Comment body posted.'); $this->assertTrue((!empty($match) && !empty($match[1])), 'Comment ID found.'); } if (isset($match[1])) { return Comment::load($match[1]); } } /** * Checks current page for specified comment. * * @param \Drupal\comment\CommentInterface $comment * The comment object. * @param bool $reply * Boolean indicating whether the comment is a reply to another comment. * * @return bool * Boolean indicating whether the comment was found. */ public function commentExists(CommentInterface $comment = NULL, $reply = FALSE) { if ($comment) { $regex = '/' . ($reply ? '