function CommentFieldsTest::testCommentDefaultFields

Same name in other branches
  1. 7.x modules/comment/comment.test \CommentFieldsTest::testCommentDefaultFields()
  2. 9 core/modules/comment/tests/src/Functional/CommentFieldsTest.php \Drupal\Tests\comment\Functional\CommentFieldsTest::testCommentDefaultFields()
  3. 8.9.x core/modules/comment/tests/src/Functional/CommentFieldsTest.php \Drupal\Tests\comment\Functional\CommentFieldsTest::testCommentDefaultFields()
  4. 11.x core/modules/comment/tests/src/Functional/CommentFieldsTest.php \Drupal\Tests\comment\Functional\CommentFieldsTest::testCommentDefaultFields()

Tests that the default 'comment_body' field is correctly added.

File

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

Class

CommentFieldsTest
Tests fields on comments.

Namespace

Drupal\Tests\comment\Functional

Code

public function testCommentDefaultFields() : void {
    // Do not make assumptions on default node types created by the test
    // installation profile, and create our own.
    $this->drupalCreateContentType([
        'type' => 'test_node_type',
    ]);
    $this->addDefaultCommentField('node', 'test_node_type');
    // Check that the 'comment_body' field is present on the comment bundle.
    $field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
    $this->assertNotEmpty($field, 'The comment_body field is added when a comment bundle is created');
    $field->delete();
    // Check that the 'comment_body' field is not deleted since it is persisted
    // even if it has no fields.
    $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
    $this->assertInstanceOf(FieldStorageConfig::class, $field_storage);
    // Create a new content type.
    $type_name = 'test_node_type_2';
    $this->drupalCreateContentType([
        'type' => $type_name,
    ]);
    $this->addDefaultCommentField('node', $type_name);
    // Check that the 'comment_body' field exists and has an instance on the
    // new comment bundle.
    $field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
    $this->assertInstanceOf(FieldStorageConfig::class, $field_storage);
    $field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
    $this->assertTrue(isset($field), "The comment_body field is present for comments on type {$type_name}");
    // Test adding a field that defaults to CommentItemInterface::CLOSED.
    $this->addDefaultCommentField('node', 'test_node_type', 'who_likes_ponies', CommentItemInterface::CLOSED, 'who_likes_ponies');
    $field = FieldConfig::load('node.test_node_type.who_likes_ponies');
    $this->assertEquals(CommentItemInterface::CLOSED, $field->getDefaultValueLiteral()[0]['status']);
}

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