You are here

public function XBBCodeFilterTest::testFilter in Extensible BBCode 8.3

Same name and namespace in other branches
  1. 4.0.x tests/src/Kernel/XBBCodeFilterTest.php \Drupal\Tests\xbbcode\Kernel\XBBCodeFilterTest::testFilter()

Test a few basic aspects of the filter.

File

tests/src/Kernel/XBBCodeFilterTest.php, line 124

Class

XBBCodeFilterTest
Test the filter.

Namespace

Drupal\Tests\xbbcode\Kernel

Code

public function testFilter() : void {
  $string = [
    $this
      ->randomString(),
    $this
      ->randomString(),
    $this
      ->randomString(),
    $this
      ->randomString(),
    $this
      ->randomString(),
  ];
  $escaped = array_map(static function ($x) {
    return Html::escape($x);
  }, $string);
  $key = [
    $this
      ->randomMachineName(),
    $this
      ->randomMachineName(),
  ];
  $text = "{$string[0]}[test_plugin {$key[0]}={$key[1]}]{$string[1]}" . "[test_plugin {$key[1]}={$key[0]}]{$string[2]}[/test_plugin]" . "{$string[3]}[/test_plugin]{$string[4]}";
  $expected = "{$escaped[0]}<span data-{$key[0]}=\"{$key[1]}\">{prepared:{$escaped[1]}" . "<span data-{$key[1]}=\"{$key[0]}\">{prepared:{$escaped[2]}}</span>" . "{$escaped[3]}}</span>{$escaped[4]}";
  self::assertEquals($expected, check_markup($text, 'xbbcode_test'));
  $val = preg_replace('/[\\\\\\"]/', '\\\\\\0', $string[2]);
  $text = "[test_tag]{$string[0]}[test_template]{$string[1]}" . "[test_plugin {$key[0]}=\"{$val}\"]{$string[2]}[/test_plugin]" . "{$string[3]}[/test_template]{$string[4]}[/test_tag]";

  // The external template file has a trailing \n:
  $expected = "<strong>{$escaped[0]}<em>{$escaped[1]}" . "<span data-{$key[0]}=\"{$escaped[2]}\">{prepared:{$escaped[2]}}</span>" . "{$escaped[3]}</em>\n{$escaped[4]}</strong>";
  $output = $this
    ->checkMarkup($text, 'xbbcode_test');
  self::assertEquals($expected, $output['#markup']);

  // The order of attachments is effectively arbitrary, but our plugin
  // merges them "top-down", so the outer tag's libraries precede the inner.
  self::assertEquals([
    'library' => [
      'xbbcode_test_plugin/library-template',
      'xbbcode_test_plugin/library-plugin',
    ],
  ], $output['#attached']);
}