function CommentTypeTest::testCommentTypeDeletion

Same name and namespace in other branches
  1. 8.9.x core/modules/comment/tests/src/Functional/CommentTypeTest.php \Drupal\Tests\comment\Functional\CommentTypeTest::testCommentTypeDeletion()
  2. 10 core/modules/comment/tests/src/Functional/CommentTypeTest.php \Drupal\Tests\comment\Functional\CommentTypeTest::testCommentTypeDeletion()
  3. 11.x core/modules/comment/tests/src/Functional/CommentTypeTest.php \Drupal\Tests\comment\Functional\CommentTypeTest::testCommentTypeDeletion()

Tests deleting a comment type that still has content.

File

core/modules/comment/tests/src/Functional/CommentTypeTest.php, line 137

Class

CommentTypeTest
Ensures that comment type functions work correctly.

Namespace

Drupal\Tests\comment\Functional

Code

public function testCommentTypeDeletion() {
    // Create a comment type programmatically.
    $type = $this->createCommentType('foo');
    $this->drupalCreateContentType([
        '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([
        'type' => 'page',
        'title' => 'foo',
    ]);
    $node->save();
    // Add a new comment of this type.
    $comment = Comment::create([
        '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->assertSession()
        ->pageTextContains($type->label() . ' is used by 1 comment on your site. You can not remove this comment type until you have removed all of the ' . $type->label() . ' comments.');
    $this->assertSession()
        ->pageTextContains('foo is used by the node.foo field on your site. You can not remove this comment type until you have removed the field.');
    $this->assertSession()
        ->pageTextNotContains('This action cannot be undone.');
    // 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->assertSession()
        ->pageTextContains('Are you sure you want to delete the comment type ' . $type->id() . '?');
    $this->assertSession()
        ->pageTextContains('This action cannot be undone.');
    // Test exception thrown when re-using an existing comment type.
    try {
        $this->addDefaultCommentField('comment', 'comment', 'bar');
        $this->fail('Exception not thrown.');
    } catch (\InvalidArgumentException $e) {
        // Expected exception; just continue testing.
    }
    // Delete the comment type.
    $this->drupalGet('admin/structure/comment/manage/' . $type->id() . '/delete');
    $this->submitForm([], 'Delete');
    $this->assertNull(CommentType::load($type->id()), 'Comment type deleted.');
    $this->assertSession()
        ->pageTextContains('The comment type ' . $type->label() . ' has been deleted.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.