642be17cb2e0f0e8caa7107d76fa0b65ae515cff
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Encoder / JavaScriptEncoderTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Encoder;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Test cases for TYPO3\CMS\Core\Encoder\JavaScriptEncoder.
19 */
20 class JavaScriptEncoderTest extends \TYPO3\Components\TestingFramework\Core\UnitTestCase
21 {
22 /**
23 * @var \TYPO3\CMS\Core\Encoder\JavaScriptEncoder
24 */
25 protected $subject = null;
26
27 protected function setUp()
28 {
29 $this->subject = new \TYPO3\CMS\Core\Encoder\JavaScriptEncoder();
30 }
31
32 /**
33 * Data provider for encodeEncodesCorrectly.
34 *
35 * @return array
36 */
37 public function encodeEncodesCorrectlyDataProvider()
38 {
39 return [
40 'Immune characters are returned as is' => [
41 '._,',
42 '._,'
43 ],
44 'Alphanumerical characters are returned as is' => [
45 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
46 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
47 ],
48 'Angel brackets and ampersand are encoded' => [
49 '<>&',
50 '\\x3C\\x3E\\x26'
51 ],
52 'Quotes and slashes are encoded' => [
53 '"\'\\/',
54 '\\x22\\x27\\x5C\\x2F'
55 ],
56 'Empty string stays empty' => [
57 '',
58 ''
59 ],
60 'Exclamation mark and space are properly encoded' => [
61 'Hello World!',
62 'Hello\\x20World\\x21'
63 ],
64 'Whitespaces are properly encoded' => [
65 TAB . LF . CR . ' ',
66 '\\x09\\x0A\\x0D\\x20'
67 ],
68 'Null byte is properly encoded' => [
69 chr(0),
70 '\\x00'
71 ],
72 'Umlauts are properly encoded' => [
73 'ÜüÖöÄä',
74 '\\xDC\\xFC\\xD6\\xF6\\xC4\\xE4'
75 ]
76 ];
77 }
78
79 /**
80 * @test
81 * @param string $input
82 * @param string $expected
83 * @dataProvider encodeEncodesCorrectlyDataProvider
84 */
85 public function encodeEncodesCorrectly($input, $expected)
86 {
87 $this->assertSame($expected, $this->subject->encode($input));
88 }
89 }