CommentBundlesTest.php in Drupal 8
File
core/modules/comment/tests/src/Kernel/CommentBundlesTest.php
View source
<?php
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\Entity\CommentType;
use Drupal\KernelTests\KernelTestBase;
class CommentBundlesTest extends KernelTestBase {
public static $modules = [
'comment',
'node',
'taxonomy',
'user',
];
protected $targetEntityTypes;
protected $entityFieldManager;
protected function setUp() {
parent::setUp();
$this->entityFieldManager = $this->container
->get('entity_field.manager');
$this
->installEntitySchema('comment');
$this->targetEntityTypes = [
'comment' => 'Comment',
'node' => 'Node',
'taxonomy_term' => 'Taxonomy Term',
];
foreach ($this->targetEntityTypes as $id => $label) {
CommentType::create([
'id' => 'comment_on_' . $id,
'label' => 'Comment on ' . $label,
'target_entity_type_id' => $id,
])
->save();
}
}
public function testEntityIdField() {
$field_definitions = [];
foreach (array_keys($this->targetEntityTypes) as $id) {
$bundle = 'comment_on_' . $id;
$field_definitions[$bundle] = $this->entityFieldManager
->getFieldDefinitions('comment', $bundle);
}
foreach ($field_definitions as $bundle => $definition) {
$entity_type_id = str_replace('comment_on_', '', $bundle);
$target_type = $definition['entity_id']
->getSetting('target_type');
$this
->assertEquals($entity_type_id, $target_type);
$nested_target_type = $definition['entity_id']
->getItemDefinition()
->getFieldDefinition()
->getSetting('target_type');
$this
->assertEquals($entity_type_id, $nested_target_type);
}
}
}