You are here

class HtmlTitleFilterTest in HTML Title 8

@coversDefaultClass \Drupal\html_title\HtmlTitleFilter

@group html_title

Hierarchy

Expanded class hierarchy of HtmlTitleFilterTest

File

tests/src/Unit/HtmlTitleFilterTest.php, line 16

Namespace

Drupal\Tests\html_title\Unit
View source
class HtmlTitleFilterTest extends RendererTestBase {

  /**
   * The availability manager.
   *
   * @var \Drupal\html_title\HtmlTitleFilter
   */
  protected $htmlTitleFilter;

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * Tests HtmlTitleFilter::decodeToText().
   *
   * @param string|array $input
   *   The input passed to decodeToText().
   * @param string $expected
   *   The expected result from calling the function.
   *
   * @see HtmlTitleFilter::decodeToText()
   *
   * @dataProvider providerDecodeToText
   */
  public function testDecodeToText($input, string $expected) {
    $this
      ->assertEquals($expected, $this->htmlTitleFilter
      ->decodeToText($input));
  }

  /**
   * Data provider for testDecodeToText().
   *
   * @see testDecodeToText()
   */
  public function providerDecodeToText() {
    return [
      [
        'test <sup>sup</sup>-tag',
        'test <sup>sup</sup>-tag',
      ],
      [
        'test <p>p</p>-tag',
        'test p-tag',
      ],
      [
        'test &',
        'test &amp;',
      ],
      [
        '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',
      ],
      // The html title filter service should also works with renderable arrays.
      [
        [
          '#markup' => '<p>Test renderable <sub>array</sub></p>',
        ],
        'Test renderable <sub>array</sub>',
      ],
    ];
  }

  /**
   * Tests HtmlTitleFilter::decodeToText().
   *
   * @param string|array $input
   *   The input passed to decodeToText().
   * @param \Drupal\Core\Render\Markup $expected
   *   The expected result from calling the function.
   *
   * @see HtmlTitleFilter::decodeToMarkup()
   *
   * @dataProvider providerDecodeToMarkup
   */
  public function testDecodeToMarkup($input, Markup $expected) {
    $this
      ->assertEquals($expected, $this->htmlTitleFilter
      ->decodeToMarkup($input));
  }

  /**
   * Data provider for testDecodeToText().
   *
   * @see testDecodeToMarkup()
   */
  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 &amp;'),
      ],
      [
        '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'),
      ],
      // The html title filter service should also works with renderable arrays.
      [
        [
          '#markup' => '<p>Test renderable <sub>array</sub></p>',
        ],
        Markup::create('Test renderable <sub>array</sub>'),
      ],
    ];
  }

  /**
   * Tests HtmlTitleFilter::getAllowHtmlTags().
   *
   * @see HtmlTitleFilter::getAllowHtmlTags()
   */
  public function testGetAllowedHtmlTags() {
    $this
      ->assertEquals([
      'br',
      'sub',
      'sup',
    ], $this->htmlTitleFilter
      ->getAllowHtmlTags());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HtmlTitleFilterTest::$htmlTitleFilter protected property The availability manager.
HtmlTitleFilterTest::providerDecodeToMarkup public function Data provider for testDecodeToText().
HtmlTitleFilterTest::providerDecodeToText public function Data provider for testDecodeToText().
HtmlTitleFilterTest::setUp protected function Overrides RendererTestBase::setUp
HtmlTitleFilterTest::testDecodeToMarkup public function Tests HtmlTitleFilter::decodeToText().
HtmlTitleFilterTest::testDecodeToText public function Tests HtmlTitleFilter::decodeToText().
HtmlTitleFilterTest::testGetAllowedHtmlTags public function Tests HtmlTitleFilter::getAllowHtmlTags().
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
RendererTestBase::$cacheContexts protected property
RendererTestBase::$cacheFactory protected property
RendererTestBase::$controllerResolver protected property The mocked controller resolver.
RendererTestBase::$currentUserRole protected property The simulated "current" user role, for use in tests with cache contexts.
RendererTestBase::$elementInfo protected property The mocked element info.
RendererTestBase::$memoryCache protected property
RendererTestBase::$placeholderGenerator protected property The tested placeholder generator. 1
RendererTestBase::$renderCache protected property The tested render cache.
RendererTestBase::$renderer protected property The tested renderer.
RendererTestBase::$rendererConfig protected property The mocked renderer configuration.
RendererTestBase::$requestStack protected property
RendererTestBase::$themeManager protected property The mocked theme manager.
RendererTestBase::assertRenderCacheItem protected function Asserts a render cache item.
RendererTestBase::randomContextValue protected function Generates a random context value for the placeholder tests.
RendererTestBase::setupMemoryCache protected function Sets up a memory-based render cache back-end.
RendererTestBase::setUpRequest protected function Sets up a request object on the request stack.
RendererTestBase::setUpUnusedCache protected function Sets up a render cache back-end that is asserted to be never used.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.