public function CommentTypeTest::testCommentTypeDeletion in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/comment/src/Tests/CommentTypeTest.php \Drupal\comment\Tests\CommentTypeTest::testCommentTypeDeletion()
Tests deleting a comment type that still has content.
File
- core/
modules/ comment/ src/ Tests/ CommentTypeTest.php, line 129 - Contains \Drupal\comment\Tests\CommentTypeTest.
Class
- CommentTypeTest
- Ensures that comment type functions work correctly.
Namespace
Drupal\comment\TestsCode
public function testCommentTypeDeletion() {
// Create a comment type programmatically.
$type = $this
->createCommentType('foo');
$this
->drupalCreateContentType(array(
'type' => 'page',
));
$this
->addDefaultCommentField('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 the comment type %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 {
$this
->addDefaultCommentField('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('The comment type %label has been deleted.', array(
'%label' => $type
->label(),
)));
}