/** * Tests the Drupal 6 to Drupal 8 comment type migration. */ public function testCommentType() { $comment_type = CommentType::load('comment'); $this->assertIdentical('node', $comment_type->getTargetEntityTypeId()); $comment_type = CommentType::load('comment_no_subject'); $this->assertIdentical('node', $comment_type->getTargetEntityTypeId()); }
/** * Asserts a comment type entity. * * @param string $id * The entity ID. * @param string $label * The entity label. */ protected function assertEntity($id, $label) { $entity = CommentType::load($id); $this->assertTrue($entity instanceof CommentTypeInterface); /** @var \Drupal\comment\CommentTypeInterface $entity */ $this->assertIdentical($label, $entity->label()); $this->assertIdentical('node', $entity->getTargetEntityTypeId()); }
/** * Tests deleting a comment type that still has content. */ public function testCommentTypeDeletion() { // Create a comment type programmatically. $type = $this->createCommentType('foo'); $this->drupalCreateContentType(array('type' => 'page')); \Drupal::service('comment.manager')->addDefaultField('node', 'page', 'foo', CommentItemInterface::OPEN, 'foo'); $field_storage = FieldStorageConfig::loadByName('node', 'foo'); $this->drupalLogin($this->adminUser); // Create a node. $node = Node::create(array('type' => 'page', 'title' => 'foo')); $node->save(); // Add a new comment of this type. $comment = Comment::create(array('comment_type' => 'foo', 'entity_type' => 'node', 'field_name' => 'foo', 'entity_id' => $node->id())); $comment->save(); // Attempt to delete the comment type, which should not be allowed. $this->drupalGet('admin/structure/comment/manage/' . $type->id() . '/delete'); $this->assertRaw(t('%label is used by 1 comment on your site. You can not remove this comment type until you have removed all of the %label comments.', array('%label' => $type->label())), 'The comment type will not be deleted until all comments of that type are removed.'); $this->assertRaw(t('%label is used by the %field field on your site. You can not remove this comment type until you have removed the field.', array('%label' => 'foo', '%field' => 'node.foo')), 'The comment type will not be deleted until all fields of that type are removed.'); $this->assertNoText(t('This action cannot be undone.'), 'The comment type deletion confirmation form is not available.'); // Delete the comment and the field. $comment->delete(); $field_storage->delete(); // Attempt to delete the comment type, which should now be allowed. $this->drupalGet('admin/structure/comment/manage/' . $type->id() . '/delete'); $this->assertRaw(t('Are you sure you want to delete %type?', array('%type' => $type->id())), 'The comment type is available for deletion.'); $this->assertText(t('This action cannot be undone.'), 'The comment type deletion confirmation form is available.'); // Test exception thrown when re-using an existing comment type. try { \Drupal::service('comment.manager')->addDefaultField('comment', 'comment', 'bar'); $this->fail('Exception not thrown.'); } catch (\InvalidArgumentException $e) { $this->pass('Exception thrown if attempting to re-use comment-type from another entity type.'); } // Delete the comment type. $this->drupalPostForm('admin/structure/comment/manage/' . $type->id() . '/delete', array(), t('Delete')); $this->assertNull(CommentType::load($type->id()), 'Comment type deleted.'); $this->assertRaw(t('Comment type %label has been deleted.', array('%label' => $type->label()))); }
/** * {@inheritdoc} */ public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { if ($comment_type = CommentType::load($bundle)) { $fields['entity_id'] = clone $base_field_definitions['entity_id']; $fields['entity_id']->setSetting('target_type', $comment_type->getTargetEntityTypeId()); return $fields; } return array(); }