Same name and namespace in other branches
- 8.9.x core/modules/text/tests/src/Kernel/TextFormatterTest.php \Drupal\Tests\text\Kernel\TextFormatterTest
- 9 core/modules/text/tests/src/Kernel/TextFormatterTest.php \Drupal\Tests\text\Kernel\TextFormatterTest
Tests the text formatters functionality.
@group text
Hierarchy
- class \Drupal\KernelTests\Core\Entity\EntityKernelTestBase extends \Drupal\KernelTests\KernelTestBase uses UserCreationTrait
- class \Drupal\Tests\text\Kernel\TextFormatterTest
Expanded class hierarchy of TextFormatterTest
File
- core/
modules/ text/ tests/ src/ Kernel/ TextFormatterTest.php, line 17
Namespace
Drupal\Tests\text\KernelView source
class TextFormatterTest extends EntityKernelTestBase {
/**
* The entity type used in this test.
*
* @var string
*/
protected $entityType = 'entity_test';
/**
* The bundle used in this test.
*
* @var string
*/
protected $bundle = 'entity_test';
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'text',
];
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
FilterFormat::create([
'format' => 'my_text_format',
'name' => 'My text format',
'filters' => [
'filter_autop' => [
'module' => 'filter',
'status' => TRUE,
],
],
])
->save();
FieldStorageConfig::create([
'field_name' => 'formatted_text',
'entity_type' => $this->entityType,
'type' => 'text',
'settings' => [],
])
->save();
FieldConfig::create([
'entity_type' => $this->entityType,
'bundle' => $this->bundle,
'field_name' => 'formatted_text',
'label' => 'Filtered text',
])
->save();
}
/**
* Tests all text field formatters.
*/
public function testFormatters() {
$formatters = [
'text_default',
'text_trimmed',
'text_summary_or_trimmed',
];
// Create the entity to be referenced.
$entity = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->create([
'name' => $this
->randomMachineName(),
]);
$entity->formatted_text = [
'value' => 'Hello, world!',
'format' => 'my_text_format',
];
$entity
->save();
foreach ($formatters as $formatter) {
// Verify the text field formatter's render array.
$build = $entity
->get('formatted_text')
->view([
'type' => $formatter,
]);
\Drupal::service('renderer')
->renderRoot($build[0]);
$this
->assertSame("<p>Hello, world!</p>\n", (string) $build[0]['#markup']);
$this
->assertEquals(FilterFormat::load('my_text_format')
->getCacheTags(), $build[0]['#cache']['tags'], "The {$formatter} formatter has the expected cache tags when formatting a formatted text field.");
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityKernelTestBase:: |
protected | property | The entity type manager service. | 1 |
EntityKernelTestBase:: |
protected | property | A list of generated identifiers. | |
EntityKernelTestBase:: |
protected | property | The state service. | |
EntityKernelTestBase:: |
protected | function | Creates a user. | |
EntityKernelTestBase:: |
protected | function | Generates a random ID avoiding collisions. | |
EntityKernelTestBase:: |
protected | function | Returns the entity_test hook invocation info. | |
EntityKernelTestBase:: |
protected | function | Installs a module and refreshes services. | |
EntityKernelTestBase:: |
protected | function | Refresh services. | |
EntityKernelTestBase:: |
protected | function | Reloads the given entity from the storage and returns it. | |
EntityKernelTestBase:: |
protected | function | Uninstalls a module and refreshes services. | |
TextFormatterTest:: |
protected | property | The bundle used in this test. | |
TextFormatterTest:: |
protected | property | The entity type used in this test. | |
TextFormatterTest:: |
protected static | property |
Modules to enable. Overrides EntityKernelTestBase:: |
|
TextFormatterTest:: |
protected | function |
Overrides EntityKernelTestBase:: |
|
TextFormatterTest:: |
public | function | Tests all text field formatters. | |
UserCreationTrait:: |
protected | function | Checks whether a given list of permission names is valid. Aliased as: drupalCheckPermissions | |
UserCreationTrait:: |
protected | function | Creates an administrative role. Aliased as: drupalCreateAdminRole | |
UserCreationTrait:: |
protected | function | Creates a role with specified permissions. Aliased as: drupalCreateRole | |
UserCreationTrait:: |
protected | function | Create a user with a given set of permissions. Aliased as: drupalCreateUser | |
UserCreationTrait:: |
protected | function | Grant permissions to a user role. Aliased as: drupalGrantPermissions | |
UserCreationTrait:: |
protected | function | Switch the current logged in user. Aliased as: drupalSetCurrentUser | |
UserCreationTrait:: |
protected | function | Creates a random user account and sets it as current user. Aliased as: drupalSetUpCurrentUser |