function CommentTypeTest::testCommentTypeCreation

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

Tests creating a comment type programmatically and via a form.

File

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

Class

CommentTypeTest
Ensures that comment type functions work correctly.

Namespace

Drupal\Tests\comment\Functional

Code

public function testCommentTypeCreation() {
  // Create a comment type programmatically.
  $type = $this->createCommentType('other');
  $comment_type = CommentType::load('other');
  $this->assertInstanceOf(CommentType::class, $comment_type);
  // Log in a test user.
  $this->drupalLogin($this->adminUser);
  // Ensure that the new comment type admin page can be accessed.
  $this->drupalGet('admin/structure/comment/manage/' . $type->id());
  $this->assertSession()
    ->statusCodeEquals(200);
  // Create a comment type via the user interface.
  $edit = [
    'id' => 'foo',
    'label' => 'title for foo',
    'description' => '',
    'target_entity_type_id' => 'node',
  ];
  $this->drupalGet('admin/structure/comment/types/add');
  $this->submitForm($edit, 'Save');
  $comment_type = CommentType::load('foo');
  $this->assertInstanceOf(CommentType::class, $comment_type);
  // Check that the comment type was created in site default language.
  $default_langcode = \Drupal::languageManager()->getDefaultLanguage()
    ->getId();
  $this->assertEquals($default_langcode, $comment_type->language()
    ->getId());
  // Edit the comment-type and ensure that we cannot change the entity-type.
  $this->drupalGet('admin/structure/comment/manage/foo');
  $this->assertSession()
    ->fieldNotExists('target_entity_type_id');
  $this->assertSession()
    ->pageTextContains('Target entity type');
  // Save the form and ensure the entity-type value is preserved even though
  // the field isn't present.
  $this->submitForm([], 'Save');
  \Drupal::entityTypeManager()->getStorage('comment_type')
    ->resetCache([
    'foo',
  ]);
  $comment_type = CommentType::load('foo');
  $this->assertEquals('node', $comment_type->getTargetEntityTypeId());
}

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