trait ProcessedTextTestTrait

Same name and namespace in other branches
  1. main core/modules/filter/tests/src/Traits/ProcessedTextTestTrait.php \Drupal\Tests\filter\Traits\ProcessedTextTestTrait

Provides a trait for testing processed text.

Hierarchy

5 files declare their use of ProcessedTextTestTrait
AreaTextTest.php in core/modules/views/tests/src/Kernel/Handler/AreaTextTest.php
AreaTextTokenTest.php in core/modules/views/tests/src/Kernel/Handler/AreaTextTokenTest.php
FilterAPITest.php in core/modules/filter/tests/src/Kernel/FilterAPITest.php
FilterNoFormatTest.php in core/modules/filter/tests/src/Kernel/FilterNoFormatTest.php
FilterSecurityKernelTest.php in core/modules/filter/tests/src/Kernel/FilterSecurityKernelTest.php

File

core/modules/filter/tests/src/Traits/ProcessedTextTestTrait.php, line 12

Namespace

Drupal\Tests\filter\Traits
View source
trait ProcessedTextTestTrait {
  
  /**
   * Processes a text using the renderer.
   *
   * Important: This technique of rendering processed text is only permitted for
   * testing isolated string cases. This is because flattening the render array
   * leads to loss of cacheability metadata. In production code, use the render
   * array directly.
   *
   * @param string $text
   *   Text to process.
   * @param string|null $format
   *   (optional) The format to use. Defaults to the fallback format.
   * @param string|null $langcode
   *   (optional) The language code to use. Defaults to the current language.
   * @param array $filterTypesToSkip
   *   (optional) An array of filter types to skip.
   *
   * @return \Drupal\Component\Render\MarkupInterface
   *   The processed text.
   */
  protected function processText(string $text, ?string $format = NULL, ?string $langcode = NULL, array $filterTypesToSkip = []) : MarkupInterface {
    $build = [
      '#type' => 'processed_text',
      '#text' => $text,
      '#format' => $format,
      '#langcode' => $langcode,
      '#filter_types_to_skip' => $filterTypesToSkip,
    ];
    return \Drupal::service('renderer')->renderInIsolation($build);
  }

}

Members

Title Sort descending Modifiers Object type Summary
ProcessedTextTestTrait::processText protected function Processes a text using the renderer.

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