You are here

protected function TocFilterTest::assertFilteredString in TOC filter 8.2

Asserts multiple filter output expectations for multiple input strings.

$tests = [
  'Input string' => [
    '<p>Input string</p>' => TRUE,
    'Input string<br' => FALSE,
  ],
];

Parameters

FilterInterface $filter: A input filter object.

array $tests: An associative array, whereas each key is an arbitrary input string and each value is again an associative array whose keys are filter output strings and whose values are Booleans indicating whether the output is expected or not.

For example:

1 call to TocFilterTest::assertFilteredString()
TocFilterTest::testFilterProcessing in src/Tests/TocFilterTest.php
Tests the filter processing.

File

src/Tests/TocFilterTest.php, line 112
Definition of Drupal\toc_filter\Tests\TocFilterTest.

Class

TocFilterTest
Tests TOC filter.

Namespace

Drupal\toc_filter\Tests

Code

protected function assertFilteredString(FilterInterface $filter, array $tests) {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = \Drupal::service('renderer');
  $test = function ($input) use ($filter, $renderer) {
    return $renderer
      ->executeInRenderContext(new RenderContext(), function () use ($input, $filter) {
      return $filter
        ->process($input, LANGUAGE_NONE);
    });
  };
  foreach ($tests as $source => $tasks) {
    $result = $test($source)
      ->getProcessedText();
    foreach ($tasks as $value => $is_expected) {

      // Not using assertIdentical, since combination with strpos() is hard
      // to grok.
      $success = $this
        ->assertTrue(strpos($result, $value) !== FALSE, new FormattableMarkup('@source: @value @found found. Filtered result: @result.', [
        '@source' => var_export($source, TRUE),
        '@value' => var_export($value, TRUE),
        '@result' => var_export($result, TRUE),
        '@found' => $is_expected ? '' : ' not',
      ]));
      if (!$success) {
        $this
          ->verbose('Source:<pre>' . Html::escape(var_export($source, TRUE)) . '</pre>' . '<hr />' . 'Result:<pre>' . Html::escape(var_export($result, TRUE)) . '</pre>' . '<hr />' . ($is_expected ? 'Expected:' : 'Not expected:') . '<pre>' . Html::escape(var_export($value, TRUE)) . '</pre>');
      }
    }
  }
}