You are here

public function TextFormatterTest::testFormatters in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/text/tests/src/Kernel/TextFormatterTest.php \Drupal\Tests\text\Kernel\TextFormatterTest::testFormatters()
  2. 9 core/modules/text/tests/src/Kernel/TextFormatterTest.php \Drupal\Tests\text\Kernel\TextFormatterTest::testFormatters()

Tests all text field formatters.

File

core/modules/text/tests/src/Kernel/TextFormatterTest.php, line 73

Class

TextFormatterTest
Tests the text formatters functionality.

Namespace

Drupal\Tests\text\Kernel

Code

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
      ->assertEquals("<p>Hello, world!</p>\n", $build[0]['#markup']);
    $this
      ->assertEquals(FilterFormat::load('my_text_format')
      ->getCacheTags(), $build[0]['#cache']['tags'], new FormattableMarkup('The @formatter formatter has the expected cache tags when formatting a formatted text field.', [
      '@formatter' => $formatter,
    ]));
  }
}