function FilterUnitTest::assertFilteredString

Asserts multiple filter output expectations for multiple input strings.

$tests = array(
  'Input string' => array(
    '<p>Input string</p>' => TRUE,
    'Input string<br' => FALSE,
  ),
);

Parameters

$filter: A input filter object.

$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:

3 calls to FilterUnitTest::assertFilteredString()
FilterUnitTest::testHtmlEscapeFilter in drupal/core/modules/filter/lib/Drupal/filter/Tests/FilterUnitTest.php
Tests the HTML escaping filter.
FilterUnitTest::testLineBreakFilter in drupal/core/modules/filter/lib/Drupal/filter/Tests/FilterUnitTest.php
Tests the line break filter.
FilterUnitTest::testUrlFilter in drupal/core/modules/filter/lib/Drupal/filter/Tests/FilterUnitTest.php
Tests the URL filter.

File

drupal/core/modules/filter/lib/Drupal/filter/Tests/FilterUnitTest.php, line 549
Definition of Drupal\filter\Tests\FilterUnitTest.

Class

FilterUnitTest
Unit tests for core filters.

Namespace

Drupal\filter\Tests

Code

function assertFilteredString($filter, $tests) {
  foreach ($tests as $source => $tasks) {
    $function = $filter->callback;
    $result = $function($source, $filter);
    foreach ($tasks as $value => $is_expected) {

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