Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Common / XssUnitTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Common;
4
5 use Drupal\Component\Utility\UrlHelper;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9  * Confirm that \Drupal\Component\Utility\Xss::filter() and check_url() work
10  * correctly, including invalid multi-byte sequences.
11  *
12  * @group Common
13  */
14 class XssUnitTest extends KernelTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['filter', 'system'];
22
23   protected function setUp() {
24     parent::setUp();
25     $this->installConfig(['system']);
26   }
27
28   /**
29    * Tests t() functionality.
30    */
31   public function testT() {
32     $text = t('Simple text');
33     $this->assertEqual($text, 'Simple text', 't leaves simple text alone.');
34     $text = t('Escaped text: @value', ['@value' => '<script>']);
35     $this->assertEqual($text, 'Escaped text: &lt;script&gt;', 't replaces and escapes string.');
36     $text = t('Placeholder text: %value', ['%value' => '<script>']);
37     $this->assertEqual($text, 'Placeholder text: <em class="placeholder">&lt;script&gt;</em>', 't replaces, escapes and themes string.');
38   }
39
40   /**
41    * Checks that harmful protocols are stripped.
42    */
43   public function testBadProtocolStripping() {
44     // Ensure that check_url() strips out harmful protocols, and encodes for
45     // HTML.
46     // Ensure \Drupal\Component\Utility\UrlHelper::stripDangerousProtocols() can
47     // be used to return a plain-text string stripped of harmful protocols.
48     $url = 'javascript:http://www.example.com/?x=1&y=2';
49     $expected_plain = 'http://www.example.com/?x=1&y=2';
50     $expected_html = 'http://www.example.com/?x=1&amp;y=2';
51     $this->assertIdentical(check_url($url), $expected_html, 'check_url() filters a URL and encodes it for HTML.');
52     $this->assertIdentical(UrlHelper::filterBadProtocol($url), $expected_html, '\Drupal\Component\Utility\UrlHelper::filterBadProtocol() filters a URL and encodes it for HTML.');
53     $this->assertIdentical(UrlHelper::stripDangerousProtocols($url), $expected_plain, '\Drupal\Component\Utility\UrlHelper::stripDangerousProtocols() filters a URL and returns plain text.');
54
55   }
56
57 }