You are here

public function CommentFieldsTest::testCommentDefaultFields in Drupal 9

Same name and namespace in other branches
  1. 8 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 33

Class

CommentFieldsTest
Tests fields on comments.

Namespace

Drupal\Tests\comment\Functional

Code

public function testCommentDefaultFields() {

  // 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
    ->assertTrue(!empty($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), new FormattableMarkup('The comment_body field is present for comments on type @type', [
    '@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']);
}