function CommentTypeTest::testCommentTypeCreation

Same name and namespace in other branches
  1. 9 core/modules/comment/tests/src/Functional/CommentTypeTest.php \Drupal\Tests\comment\Functional\CommentTypeTest::testCommentTypeCreation()
  2. 8.9.x core/modules/comment/tests/src/Functional/CommentTypeTest.php \Drupal\Tests\comment\Functional\CommentTypeTest::testCommentTypeCreation()
  3. 10 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 60

Class

CommentTypeTest
Ensures that comment type functions work correctly.

Namespace

Drupal\Tests\comment\Functional

Code

public function testCommentTypeCreation() : void {
  // 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);
  $this->assertSession()
    ->elementTextEquals('css', 'h1', "Edit {$comment_type->label()} comment type");
  // Create a comment type via the user interface.
  $edit = [
    'id' => 'foo',
    'label' => 'title for foo',
    'description' => '',
  ];
  $this->drupalGet('admin/structure/comment/types/add');
  // Ensure that target entity type is a required field.
  $this->submitForm($edit, 'Save and manage fields');
  $this->assertSession()
    ->pageTextContains('Target entity type field is required.');
  // Ensure that comment type is saved when target entity type is provided.
  $edit['target_entity_type_id'] = 'node';
  $this->submitForm($edit, 'Save and manage fields');
  $this->assertSession()
    ->pageTextContains('Comment type title for foo has been added.');
  // Asserts that form submit redirects to the expected manage fields page.
  $this->assertSession()
    ->addressEquals('admin/structure/comment/manage/' . $edit['id'] . '/fields');
  // Asserts that the comment type is visible in breadcrumb.
  $this->assertTrue($this->assertSession()
    ->elementExists('css', 'nav[role="navigation"]')
    ->hasLink('title for foo'));
  $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');
  $comment_type = CommentType::load('foo');
  $this->assertEquals('node', $comment_type->getTargetEntityTypeId());
  // Ensure that target type is displayed in the comment type list.
  $this->drupalGet('admin/structure/comment');
  $this->assertSession()
    ->elementExists('xpath', '//td[text() = "Content"]');
}

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