View source
<?php
namespace Drupal\Tests\html_title\Kernel;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
class HtmlTitleFormatterTest extends KernelTestBase {
protected static $modules = [
'field',
'text',
'entity_test',
'user',
'system',
'html_title',
];
protected $entityTypeManager;
protected $entityDisplayRepository;
protected $fieldName = 'field_text';
protected $entityTypeId = 'entity_test';
protected $bundle = 'entity_test';
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'field',
'html_title',
]);
$this
->installEntitySchema('entity_test');
FieldStorageConfig::create([
'field_name' => $this->fieldName,
'type' => 'string',
'entity_type' => $this->entityTypeId,
'cardinality' => 1,
])
->save();
FieldConfig::create([
'entity_type' => $this->entityTypeId,
'field_name' => $this->fieldName,
'bundle' => $this->bundle,
'label' => 'Test text-field',
])
->save();
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->entityDisplayRepository = $this->container
->get('entity_display.repository');
}
public function testHtmlTitleFormatter(string $expected, string $value) {
$entity = $this->entityTypeManager
->getStorage($this->entityTypeId)
->create([]);
$entity
->set($this->fieldName, [
'value' => $value,
]);
$build = $this
->buildEntityField($entity, $this->fieldName, 'html_title');
$this
->render($build);
$this
->assertRaw($expected);
}
public function htmlTitleFormatterDataProvider() {
return [
[
'Test <sup>sup</sup>-tag',
'Test <sup>sup</sup>-tag',
],
[
'Test p-tag',
'Test <p>p</p>-tag',
],
];
}
protected function buildEntityField(FieldableEntityInterface $entity, string $field_name, string $formatter_type, array $formatter_settings = []) : array {
$display = $this->entityDisplayRepository
->getViewDisplay($entity
->getEntityTypeId(), $entity
->bundle());
$display
->setComponent($field_name, [
'type' => $formatter_type,
'settings' => $formatter_settings,
]);
$display
->save();
$build = $display
->build($entity);
return $build[$field_name];
}
}