You are here

public function TextFormatterTest::testFormatters in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/text/tests/src/Kernel/TextFormatterTest.php \Drupal\Tests\text\Kernel\TextFormatterTest::testFormatters()
  2. 10 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
      ->assertEqual($build[0]['#markup'], "<p>Hello, world!</p>\n");
    $this
      ->assertEqual($build[0]['#cache']['tags'], FilterFormat::load('my_text_format')
      ->getCacheTags(), new FormattableMarkup('The @formatter formatter has the expected cache tags when formatting a formatted text field.', [
      '@formatter' => $formatter,
    ]));
  }
}