function TextFormatterTest::testFormatters

Same name in other branches
  1. 9 core/modules/text/tests/src/Kernel/TextFormatterTest.php \Drupal\Tests\text\Kernel\TextFormatterTest::testFormatters()
  2. 8.9.x core/modules/text/tests/src/Kernel/TextFormatterTest.php \Drupal\Tests\text\Kernel\TextFormatterTest::testFormatters()
  3. 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 72

Class

TextFormatterTest
Tests the text formatters functionality.

Namespace

Drupal\Tests\text\Kernel

Code

public function testFormatters() : void {
    $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.");
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.