View source
<?php
namespace Drupal\Tests\comment\Functional;
use Drupal\Core\Url;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Entity\CommentType;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\Entity\Node;
class CommentTypeTest extends CommentTestBase {
protected $defaultTheme = 'stark';
protected $adminUser;
protected $permissions = [
'administer comments',
'administer comment fields',
'administer comment types',
];
protected function setUp() : void {
parent::setUp();
$this
->drupalPlaceBlock('page_title_block');
$this->adminUser = $this
->drupalCreateUser($this->permissions);
}
public function testCommentTypeCreation() {
$type = $this
->createCommentType('other');
$comment_type = CommentType::load('other');
$this
->assertInstanceOf(CommentType::class, $comment_type);
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/structure/comment/manage/' . $type
->id());
$this
->assertSession()
->statusCodeEquals(200);
$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);
$default_langcode = \Drupal::languageManager()
->getDefaultLanguage()
->getId();
$this
->assertEquals($default_langcode, $comment_type
->language()
->getId());
$this
->drupalGet('admin/structure/comment/manage/foo');
$this
->assertSession()
->fieldNotExists('target_entity_type_id');
$this
->assertSession()
->pageTextContains('Target entity type');
$this
->submitForm([], 'Save');
\Drupal::entityTypeManager()
->getStorage('comment_type')
->resetCache([
'foo',
]);
$comment_type = CommentType::load('foo');
$this
->assertEquals('node', $comment_type
->getTargetEntityTypeId());
}
public function testCommentTypeEditing() {
$this
->drupalLogin($this->adminUser);
$field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
$this
->assertEquals('Comment', $field
->getLabel(), 'Comment body field was found.');
$this
->drupalGet('admin/structure/comment');
$edit = [
'label' => 'Bar',
];
$this
->drupalGet('admin/structure/comment/manage/comment');
$this
->submitForm($edit, 'Save');
$this
->drupalGet('admin/structure/comment');
$this
->assertSession()
->pageTextContains('Bar');
$this
->clickLink('Manage fields');
$this
->assertSession()
->addressEquals(Url::fromRoute('entity.comment.field_ui_fields', [
'comment_type' => 'comment',
]));
$this
->assertCount(1, $this
->cssSelect('tr#comment-body'), 'Body field exists.');
$this
->drupalGet('admin/structure/comment/manage/comment/fields/comment.comment.comment_body/delete');
$this
->submitForm([], 'Delete');
$this
->drupalGet('admin/structure/comment/manage/comment');
$this
->submitForm([], 'Save');
$this
->drupalGet('admin/structure/comment/manage/comment/fields');
$this
->assertCount(0, $this
->cssSelect('tr#comment-body'), 'Body field does not exist.');
}
public function testCommentTypeDeletion() {
$type = $this
->createCommentType('foo');
$this
->drupalCreateContentType([
'type' => 'page',
]);
$this
->addDefaultCommentField('node', 'page', 'foo', CommentItemInterface::OPEN, 'foo');
$field_storage = FieldStorageConfig::loadByName('node', 'foo');
$this
->drupalLogin($this->adminUser);
$node = Node::create([
'type' => 'page',
'title' => 'foo',
]);
$node
->save();
$comment = Comment::create([
'comment_type' => 'foo',
'entity_type' => 'node',
'field_name' => 'foo',
'entity_id' => $node
->id(),
]);
$comment
->save();
$this
->drupalGet('admin/structure/comment/manage/' . $type
->id() . '/delete');
$this
->assertSession()
->pageTextContains($type
->label() . ' is used by 1 comment on your site. You can not remove this comment type until you have removed all of the ' . $type
->label() . ' comments.');
$this
->assertSession()
->pageTextContains('foo is used by the node.foo field on your site. You can not remove this comment type until you have removed the field.');
$this
->assertSession()
->pageTextNotContains('This action cannot be undone.');
$comment
->delete();
$field_storage
->delete();
$this
->drupalGet('admin/structure/comment/manage/' . $type
->id() . '/delete');
$this
->assertSession()
->pageTextContains('Are you sure you want to delete the comment type ' . $type
->id() . '?');
$this
->assertSession()
->pageTextContains('This action cannot be undone.');
try {
$this
->addDefaultCommentField('comment', 'comment', 'bar');
$this
->fail('Exception not thrown.');
} catch (\InvalidArgumentException $e) {
}
$this
->drupalGet('admin/structure/comment/manage/' . $type
->id() . '/delete');
$this
->submitForm([], 'Delete');
$this
->assertNull(CommentType::load($type
->id()), 'Comment type deleted.');
$this
->assertSession()
->pageTextContains('The comment type ' . $type
->label() . ' has been deleted.');
}
}