View source
<?php
namespace Drupal\Tests\html_title\Unit;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Render\Markup;
use Drupal\html_title\HtmlTitleFilter;
use Drupal\Tests\Core\Render\RendererTestBase;
class HtmlTitleFilterTest extends RendererTestBase {
protected $htmlTitleFilter;
protected function setUp() : void {
parent::setUp();
$config_factory = $this
->createMock(ConfigFactoryInterface::class);
$config = $this
->createMock(Config::class);
$config_factory
->method('get')
->willReturn($config);
$config
->method('get')
->willReturn('<br> <sub> <sup>');
$this->htmlTitleFilter = new HtmlTitleFilter($config_factory, $this->renderer);
}
public function testDecodeToText($input, string $expected) {
$this
->assertEquals($expected, $this->htmlTitleFilter
->decodeToText($input));
}
public function providerDecodeToText() {
return [
[
'test <sup>sup</sup>-tag',
'test <sup>sup</sup>-tag',
],
[
'test <p>p</p>-tag',
'test p-tag',
],
[
'test &',
'test &',
],
[
'test without tags',
'test without tags',
],
[
'test <br> br-tag',
'test <br> br-tag',
],
[
'test <sub>sub</sub>-tag',
'test <sub>sub</sub>-tag',
],
[
'test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br> br-tag',
'test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br> br-tag',
],
[
'test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br> br-tag <p>p</p>-tag',
'test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br> br-tag p-tag',
],
[
[
'#markup' => '<p>Test renderable <sub>array</sub></p>',
],
'Test renderable <sub>array</sub>',
],
];
}
public function testDecodeToMarkup($input, Markup $expected) {
$this
->assertEquals($expected, $this->htmlTitleFilter
->decodeToMarkup($input));
}
public function providerDecodeToMarkup() {
return [
[
'test <sup>sup</sup>-tag',
Markup::create('test <sup>sup</sup>-tag'),
],
[
'test <p>p</p>-tag',
Markup::create('test p-tag'),
],
[
'test &',
Markup::create('test &'),
],
[
'test without tags',
Markup::create('test without tags'),
],
[
'test <br> br-tag',
Markup::create('test <br> br-tag'),
],
[
'test <sub>sub</sub>-tag',
Markup::create('test <sub>sub</sub>-tag'),
],
[
'test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br> br-tag',
Markup::create('test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag and <br> br-tag'),
],
[
'test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag, <br> br-tag and <p>p</p>-tag',
Markup::create('test multiple tags: <sup>sup</sup>-tag, <sub>sub</sub>-tag, <br> br-tag and p-tag'),
],
[
[
'#markup' => '<p>Test renderable <sub>array</sub></p>',
],
Markup::create('Test renderable <sub>array</sub>'),
],
];
}
public function testGetAllowedHtmlTags() {
$this
->assertEquals([
'br',
'sub',
'sup',
], $this->htmlTitleFilter
->getAllowHtmlTags());
}
}