class XssUnitTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/KernelTests/Core/Common/XssUnitTest.php \Drupal\KernelTests\Core\Common\XssUnitTest
- 10 core/tests/Drupal/KernelTests/Core/Common/XssUnitTest.php \Drupal\KernelTests\Core\Common\XssUnitTest
- 8.9.x core/tests/Drupal/KernelTests/Core/Common/XssUnitTest.php \Drupal\KernelTests\Core\Common\XssUnitTest
Tests XSS filtering.
@group Common
Hierarchy
- class \Drupal\KernelTests\KernelTestBase implements \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\KernelTests\Core\Common\XssUnitTest extends \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of XssUnitTest
See also
\Drupal\Component\Utility\Xss::filter()
\Drupal\Component\Utility\UrlHelper::filterBadProtocol
\Drupal\Component\Utility\UrlHelper::stripDangerousProtocols
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Common/ XssUnitTest.php, line 17
Namespace
Drupal\KernelTests\Core\CommonView source
class XssUnitTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'filter',
'system',
];
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->installConfig([
'system',
]);
}
/**
* Tests t() functionality.
*/
public function testT() {
$text = t('Simple text');
$this->assertEquals('Simple text', $text, 't leaves simple text alone.');
$text = t('Escaped text: @value', [
'@value' => '<script>',
]);
$this->assertEquals('Escaped text: <script>', $text, 't replaces and escapes string.');
$text = t('Placeholder text: %value', [
'%value' => '<script>',
]);
$this->assertEquals('Placeholder text: <em class="placeholder"><script></em>', $text, 't replaces, escapes and themes string.');
}
/**
* Checks that harmful protocols are stripped.
*/
public function testBadProtocolStripping() {
// Ensure that check_url() strips out harmful protocols, and encodes for
// HTML.
// Ensure \Drupal\Component\Utility\UrlHelper::stripDangerousProtocols() can
// be used to return a plain-text string stripped of harmful protocols.
$url = 'javascript:http://www.example.com/?x=1&y=2';
$expected_plain = 'http://www.example.com/?x=1&y=2';
$expected_html = 'http://www.example.com/?x=1&y=2';
$this->assertSame($expected_html, UrlHelper::filterBadProtocol($url), '\\Drupal\\Component\\Utility\\UrlHelper::filterBadProtocol() filters a URL and encodes it for HTML.');
$this->assertSame($expected_plain, UrlHelper::stripDangerousProtocols($url), '\\Drupal\\Component\\Utility\\UrlHelper::stripDangerousProtocols() filters a URL and returns plain text.');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.