function CommentFieldsTest::testCommentFieldDelete

Same name in other branches
  1. 9 core/modules/comment/tests/src/Functional/CommentFieldsTest.php \Drupal\Tests\comment\Functional\CommentFieldsTest::testCommentFieldDelete()
  2. 8.9.x core/modules/comment/tests/src/Functional/CommentFieldsTest.php \Drupal\Tests\comment\Functional\CommentFieldsTest::testCommentFieldDelete()
  3. 10 core/modules/comment/tests/src/Functional/CommentFieldsTest.php \Drupal\Tests\comment\Functional\CommentFieldsTest::testCommentFieldDelete()

Tests that you can remove a comment field.

File

core/modules/comment/tests/src/Functional/CommentFieldsTest.php, line 70

Class

CommentFieldsTest
Tests fields on comments.

Namespace

Drupal\Tests\comment\Functional

Code

public function testCommentFieldDelete() : void {
    $this->drupalCreateContentType([
        'type' => 'test_node_type',
    ]);
    $this->addDefaultCommentField('node', 'test_node_type');
    // We want to test the handling of removing the primary comment field, so we
    // ensure there is at least one other comment field attached to a node type
    // so that comment_entity_load() runs for nodes.
    $this->addDefaultCommentField('node', 'test_node_type', 'comment2');
    // Create a sample node.
    $node = $this->drupalCreateNode([
        'title' => 'Baloney',
        'type' => 'test_node_type',
    ]);
    $this->drupalLogin($this->webUser);
    $this->drupalGet('node/' . $node->nid->value);
    $elements = $this->cssSelect('.comment-form');
    $this->assertCount(2, $elements, 'There are two comment fields on the node.');
    // Delete the first comment field.
    FieldStorageConfig::loadByName('node', 'comment')->delete();
    $this->drupalGet('node/' . $node->nid->value);
    $elements = $this->cssSelect('.comment-form');
    $this->assertCount(1, $elements, 'There is one comment field on the node.');
}

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