3 namespace Drupal\Tests\text\Kernel;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\filter\Entity\FilterFormat;
7 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
8 use Drupal\field\Entity\FieldStorageConfig;
11 * Tests the text formatters functionality.
15 class TextFormatterTest extends EntityKernelTestBase {
18 * The entity type used in this test.
22 protected $entityType = 'entity_test';
25 * The bundle used in this test.
29 protected $bundle = 'entity_test';
36 public static $modules = ['text'];
41 protected function setUp() {
44 FilterFormat::create([
45 'format' => 'my_text_format',
46 'name' => 'My text format',
55 FieldStorageConfig::create([
56 'field_name' => 'formatted_text',
57 'entity_type' => $this->entityType,
62 'entity_type' => $this->entityType,
63 'bundle' => $this->bundle,
64 'field_name' => 'formatted_text',
65 'label' => 'Filtered text',
70 * Tests all text field formatters.
72 public function testFormatters() {
76 'text_summary_or_trimmed',
79 // Create the entity to be referenced.
80 $entity = $this->container->get('entity_type.manager')
81 ->getStorage($this->entityType)
82 ->create(['name' => $this->randomMachineName()]);
83 $entity->formatted_text = [
84 'value' => 'Hello, world!',
85 'format' => 'my_text_format',
89 foreach ($formatters as $formatter) {
90 // Verify the text field formatter's render array.
91 $build = $entity->get('formatted_text')->view(['type' => $formatter]);
92 \Drupal::service('renderer')->renderRoot($build[0]);
93 $this->assertEqual($build[0]['#markup'], "<p>Hello, world!</p>\n");
94 $this->assertEqual($build[0]['#cache']['tags'], FilterFormat::load('my_text_format')->getCacheTags(), format_string('The @formatter formatter has the expected cache tags when formatting a formatted text field.', ['@formatter' => $formatter]));