You are here

public function CommentTypeTest::testCommentTypeDeletion in Drupal 8

Same name and namespace in other branches
  1. 9 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()

Tests deleting a comment type that still has content.

File

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

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
    ->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.', [
    '%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.', [
    '%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?', [
    '%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) {

    // Expected exception; just continue testing.
  }

  // Delete the comment type.
  $this
    ->drupalPostForm('admin/structure/comment/manage/' . $type
    ->id() . '/delete', [], t('Delete'));
  $this
    ->assertNull(CommentType::load($type
    ->id()), 'Comment type deleted.');
  $this
    ->assertRaw(t('The comment type %label has been deleted.', [
    '%label' => $type
      ->label(),
  ]));
}