View source
<?php
namespace Drupal\Tests\Component\Utility;
use Drupal\Component\Render\HtmlEscapedText;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Component\Render\MarkupTrait;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Tests\UnitTestCase;
class SafeMarkupTest extends UnitTestCase {
protected $lastErrorMessage;
protected $lastErrorNumber;
protected function tearDown() {
parent::tearDown();
UrlHelper::setAllowedProtocols([
'http',
'https',
]);
}
public function testIsSafe() {
$safe_string = $this
->getMock('\\Drupal\\Component\\Render\\MarkupInterface');
$this
->assertTrue(SafeMarkup::isSafe($safe_string));
$string_object = new SafeMarkupTestString('test');
$this
->assertFalse(SafeMarkup::isSafe($string_object));
}
function testCheckPlain($text, $expected, $message) {
$result = SafeMarkup::checkPlain($text);
$this
->assertTrue($result instanceof HtmlEscapedText);
$this
->assertEquals($expected, $result, $message);
}
function testHtmlEscapedText($text, $expected, $message) {
$result = new HtmlEscapedText($text);
$this
->assertEquals($expected, $result, $message);
}
function providerCheckPlain() {
$tests[] = array(
"",
'Foo�barbaz',
'Escapes invalid sequence "Foo\\xC0barbaz"',
);
$tests[] = array(
"",
'�"',
'Escapes invalid sequence "\\xc2\\""',
);
$tests[] = array(
"Fooÿñ",
"Fooÿñ",
'Does not escape valid sequence "Fooÿñ"',
);
$tests[] = array(
SafeMarkupTestMarkup::create("<script>"),
'<script>',
'Escapes <script> even inside an object that implements MarkupInterface.',
);
$tests[] = array(
"<script>",
'<script>',
'Escapes <script>',
);
$tests[] = array(
'<>&"\'',
'<>&"'',
'Escapes reserved HTML characters.',
);
$tests[] = array(
SafeMarkupTestMarkup::create('<>&"\''),
'<>&"'',
'Escapes reserved HTML characters even inside an object that implements MarkupInterface.',
);
return $tests;
}
public function testFormat($string, array $args, $expected, $message, $expected_is_safe) {
UrlHelper::setAllowedProtocols([
'http',
'https',
'mailto',
]);
$result = SafeMarkup::format($string, $args);
$this
->assertEquals($expected, $result, $message);
$this
->assertEquals($expected_is_safe, SafeMarkup::isSafe($result), 'SafeMarkup::format correctly sets the result as safe or not safe.');
foreach ($args as $arg) {
$this
->assertSame($arg instanceof SafeMarkupTestMarkup, SafeMarkup::isSafe($arg));
}
}
function providerFormat() {
$tests[] = array(
'Simple text',
array(),
'Simple text',
'SafeMarkup::format leaves simple text alone.',
TRUE,
);
$tests[] = array(
'Escaped text: @value',
array(
'@value' => '<script>',
),
'Escaped text: <script>',
'SafeMarkup::format replaces and escapes string.',
TRUE,
);
$tests[] = array(
'Escaped text: @value',
array(
'@value' => SafeMarkupTestMarkup::create('<span>Safe HTML</span>'),
),
'Escaped text: <span>Safe HTML</span>',
'SafeMarkup::format does not escape an already safe string.',
TRUE,
);
$tests[] = array(
'Placeholder text: %value',
array(
'%value' => '<script>',
),
'Placeholder text: <em class="placeholder"><script></em>',
'SafeMarkup::format replaces, escapes and themes string.',
TRUE,
);
$tests[] = array(
'Placeholder text: %value',
array(
'%value' => SafeMarkupTestMarkup::create('<span>Safe HTML</span>'),
),
'Placeholder text: <em class="placeholder"><span>Safe HTML</span></em>',
'SafeMarkup::format does not escape an already safe string themed as a placeholder.',
TRUE,
);
$tests['javascript-protocol-url'] = [
'Simple text <a href=":url">giraffe</a>',
[
':url' => 'javascript://example.com?foo&bar',
],
'Simple text <a href="//example.com?foo&bar">giraffe</a>',
'Support for filtering bad protocols',
TRUE,
];
$tests['external-url'] = [
'Simple text <a href=":url">giraffe</a>',
[
':url' => 'http://example.com?foo&bar',
],
'Simple text <a href="http://example.com?foo&bar">giraffe</a>',
'Support for filtering bad protocols',
TRUE,
];
$tests['relative-url'] = [
'Simple text <a href=":url">giraffe</a>',
[
':url' => '/node/1?foo&bar',
],
'Simple text <a href="/node/1?foo&bar">giraffe</a>',
'Support for filtering bad protocols',
TRUE,
];
$tests['fragment-with-special-chars'] = [
'Simple text <a href=":url">giraffe</a>',
[
':url' => 'http://example.com/#<',
],
'Simple text <a href="http://example.com/#&lt;">giraffe</a>',
'Support for filtering bad protocols',
TRUE,
];
$tests['mailto-protocol'] = [
'Hey giraffe <a href=":url">MUUUH</a>',
[
':url' => 'mailto:test@example.com',
],
'Hey giraffe <a href="mailto:test@example.com">MUUUH</a>',
'',
TRUE,
];
$tests['js-with-fromCharCode'] = [
'Hey giraffe <a href=":url">MUUUH</a>',
[
':url' => "javascript:alert(String.fromCharCode(88,83,83))",
],
'Hey giraffe <a href="alert(String.fromCharCode(88,83,83))">MUUUH</a>',
'',
TRUE,
];
$tests['non-url-with-colon'] = [
'Hey giraffe <a href=":url">MUUUH</a>',
[
':url' => "llamas: they are not URLs",
],
'Hey giraffe <a href=" they are not URLs">MUUUH</a>',
'',
TRUE,
];
$tests['non-url-with-html'] = [
'Hey giraffe <a href=":url">MUUUH</a>',
[
':url' => "<span>not a url</span>",
],
'Hey giraffe <a href="<span>not a url</span>">MUUUH</a>',
'',
TRUE,
];
return $tests;
}
public function errorHandler($error_number, $error_message) {
$this->lastErrorNumber = $error_number;
$this->lastErrorMessage = $error_message;
}
public function testUnexpectedFormat() {
set_error_handler([
$this,
'errorHandler',
]);
$error = SafeMarkup::format('Broken placeholder: ~placeholder', [
'~placeholder' => 'broken',
])
->__toString();
restore_error_handler();
$this
->assertEquals(E_USER_ERROR, $this->lastErrorNumber);
$this
->assertEquals('Invalid placeholder: ~placeholder', $this->lastErrorMessage);
}
}
class SafeMarkupTestString {
protected $string;
public function __construct($string) {
$this->string = $string;
}
public function __toString() {
return $this->string;
}
}
class SafeMarkupTestMarkup implements MarkupInterface {
use MarkupTrait;
}