FieldFilteredMarkupTest.php

Same filename in other branches
  1. 9 core/tests/Drupal/Tests/Core/Field/FieldFilteredMarkupTest.php
  2. 8.9.x core/tests/Drupal/Tests/Core/Field/FieldFilteredMarkupTest.php
  3. 10 core/tests/Drupal/Tests/Core/Field/FieldFilteredMarkupTest.php

Namespace

Drupal\Tests\Core\Field

File

core/tests/Drupal/Tests/Core/Field/FieldFilteredMarkupTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\Core\Field;

use Drupal\Tests\UnitTestCase;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Component\Render\MarkupInterface;
use Prophecy\Prophet;

/**
 * @coversDefaultClass \Drupal\Core\Field\FieldFilteredMarkup
 * @group Field
 */
class FieldFilteredMarkupTest extends UnitTestCase {
    
    /**
     * @covers ::create
     * @dataProvider providerTestCreate
     */
    public function testCreate($string, $expected, $instance_of_check) : void {
        $filtered_string = FieldFilteredMarkup::create($string);
        if ($instance_of_check) {
            $this->assertInstanceOf(FieldFilteredMarkup::class, $filtered_string);
        }
        $this->assertSame($expected, (string) $filtered_string);
    }
    
    /**
     * Provides data for testCreate().
     */
    public static function providerTestCreate() {
        $data = [];
        $data[] = [
            '',
            '',
            FALSE,
        ];
        // Certain tags are filtered.
        $data[] = [
            '<script>test string</script>',
            'test string',
            TRUE,
        ];
        // Certain tags are not filtered.
        $data[] = [
            '<em>test string</em>',
            '<em>test string</em>',
            TRUE,
        ];
        // HTML will be normalized.
        $data[] = [
            '<em>test string',
            '<em>test string</em>',
            TRUE,
        ];
        // Even safe strings will be escaped.
        $safe_string = (new Prophet())->prophesize(MarkupInterface::class);
        $safe_string->__toString()
            ->willReturn('<script>test string</script>');
        $data[] = [
            $safe_string->reveal(),
            'test string',
            TRUE,
        ];
        return $data;
    }
    
    /**
     * @covers ::displayAllowedTags
     */
    public function testDisplayAllowedTags() : void {
        $expected = '<a> <b> <big> <code> <del> <em> <i> <ins> <pre> <q> <small> <span> <strong> <sub> <sup> <tt> <ol> <ul> <li> <p> <br> <img>';
        $this->assertSame($expected, FieldFilteredMarkup::displayAllowedTags());
    }

}

Classes

Title Deprecated Summary
FieldFilteredMarkupTest @coversDefaultClass \Drupal\Core\Field\FieldFilteredMarkup @group Field

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.