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@0: $bundles = \Drupal::entityManager()->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 ? '