View source
<?php
namespace Drupal\KernelTests\Component\Utility;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Url;
use Drupal\KernelTests\KernelTestBase;
class SafeMarkupKernelTest extends KernelTestBase {
public static $modules = [
'system',
];
protected function setUp() {
parent::setUp();
$this
->installSchema('system', 'router');
$this->container
->get('router.builder')
->rebuild();
}
protected static function getSafeMarkupUriArgs($uri, $options = []) {
$args[':url'] = Url::fromUri($uri, $options)
->toString();
return $args;
}
public function testSafeMarkupUri($string, $uri, $options, $expected) {
$args = self::getSafeMarkupUriArgs($uri, $options);
$this
->assertEquals($expected, SafeMarkup::format($string, $args));
}
public function providerTestSafeMarkupUri() {
$data = [];
$data['routed-url'] = [
'Hey giraffe <a href=":url">MUUUH</a>',
'route:system.admin',
[],
'Hey giraffe <a href="/admin">MUUUH</a>',
];
$data['routed-with-query'] = [
'Hey giraffe <a href=":url">MUUUH</a>',
'route:system.admin',
[
'query' => [
'bar' => 'baz#',
],
],
'Hey giraffe <a href="/admin?bar=baz%23">MUUUH</a>',
];
$data['routed-with-fragment'] = [
'Hey giraffe <a href=":url">MUUUH</a>',
'route:system.admin',
[
'fragment' => 'bar<',
],
'Hey giraffe <a href="/admin#bar&lt;">MUUUH</a>',
];
$data['unrouted-url'] = [
'Hey giraffe <a href=":url">MUUUH</a>',
'base://foo',
[],
'Hey giraffe <a href="/foo">MUUUH</a>',
];
$data['unrouted-with-query'] = [
'Hey giraffe <a href=":url">MUUUH</a>',
'base://foo',
[
'query' => [
'bar' => 'baz#',
],
],
'Hey giraffe <a href="/foo?bar=baz%23">MUUUH</a>',
];
$data['unrouted-with-fragment'] = [
'Hey giraffe <a href=":url">MUUUH</a>',
'base://foo',
[
'fragment' => 'bar<',
],
'Hey giraffe <a href="/foo#bar&lt;">MUUUH</a>',
];
$data['mailto-protocol'] = [
'Hey giraffe <a href=":url">MUUUH</a>',
'mailto:test@example.com',
[],
'Hey giraffe <a href="mailto:test@example.com">MUUUH</a>',
];
return $data;
}
public function testSafeMarkupUriWithExceptionUri($string, $uri) {
$args = self::getSafeMarkupUriArgs($uri);
SafeMarkup::format($string, $args);
}
public function providerTestSafeMarkupUriWithException() {
$data = [];
$data['js-protocol'] = [
'Hey giraffe <a href=":url">MUUUH</a>',
"javascript:alert('xss')",
];
$data['js-with-fromCharCode'] = [
'Hey giraffe <a href=":url">MUUUH</a>',
"javascript:alert(String.fromCharCode(88,83,83))",
];
$data['non-url-with-colon'] = [
'Hey giraffe <a href=":url">MUUUH</a>',
"llamas: they are not URLs",
];
$data['non-url-with-html'] = [
'Hey giraffe <a href=":url">MUUUH</a>',
'<span>not a url</span>',
];
return $data;
}
}