[BUGFIX] Fix several typos in php comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Format / HtmlentitiesViewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format;
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 use TYPO3\CMS\Fluid\ViewHelpers\Format\HtmlentitiesViewHelper;
18 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
19
20 /**
21 * Test case
22 */
23 class HtmlentitiesViewHelperTest extends ViewHelperBaseTestcase
24 {
25
26 /**
27 * @var \TYPO3\CMS\Fluid\ViewHelpers\Format\HtmlentitiesViewHelper
28 */
29 protected $viewHelper;
30
31 /**
32 * shortcut for default Arguments which would be prepared by initializeArguments()
33 *
34 * @var array
35 */
36 protected $defaultArguments;
37
38 protected function setUp(): void
39 {
40 parent::setUp();
41 $this->viewHelper = new HtmlentitiesViewHelper();
42 $this->injectDependenciesIntoViewHelper($this->viewHelper);
43 }
44
45 /**
46 * @test
47 */
48 public function renderUsesValueAsSourceIfSpecified()
49 {
50 $this->setArgumentsUnderTest(
51 $this->viewHelper,
52 [
53 'value' => 'Some string',
54 ]
55 );
56 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
57 $this->assertEquals('Some string', $actualResult);
58 }
59
60 /**
61 * @test
62 */
63 public function renderUsesChildnodesAsSourceIfSpecified()
64 {
65 $this->viewHelper->setRenderChildrenClosure(
66 function () {
67 return 'Some string';
68 }
69 );
70 $this->setArgumentsUnderTest($this->viewHelper);
71 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
72 $this->assertEquals('Some string', $actualResult);
73 }
74
75 /**
76 * @test
77 */
78 public function renderDoesNotModifyValueIfItDoesNotContainSpecialCharacters()
79 {
80 $source = 'This is a sample text without special characters.';
81 $this->setArgumentsUnderTest(
82 $this->viewHelper,
83 [
84 'value' => $source,
85 ]
86 );
87 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
88 $this->assertSame($source, $actualResult);
89 }
90
91 /**
92 * @test
93 */
94 public function renderEncodesSimpleString()
95 {
96 $source = 'Some special characters: &©"\'';
97 $this->setArgumentsUnderTest(
98 $this->viewHelper,
99 [
100 'value' => $source
101 ]
102 );
103 $expectedResult = 'Some special characters: &amp;&copy;&quot;&#039;';
104 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
105 $this->assertEquals($expectedResult, $actualResult);
106 }
107
108 /**
109 * @test
110 */
111 public function renderRespectsKeepQuoteArgument()
112 {
113 $source = 'Some special characters: &©"\'';
114 $this->setArgumentsUnderTest(
115 $this->viewHelper,
116 [
117 'value' => $source,
118 'keepQuotes' => true,
119 ]
120 );
121 $expectedResult = 'Some special characters: &amp;&copy;"\'';
122 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
123 $this->assertEquals($expectedResult, $actualResult);
124 }
125
126 /**
127 * @test
128 */
129 public function renderRespectsEncodingArgument()
130 {
131 $source = utf8_decode('Some special characters: &©"\'');
132 $this->setArgumentsUnderTest(
133 $this->viewHelper,
134 [
135 'value' => $source,
136 'encoding' => 'ISO-8859-1',
137 ]
138 );
139 $expectedResult = 'Some special characters: &amp;&copy;&quot;&#039;';
140 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
141 $this->assertEquals($expectedResult, $actualResult);
142 }
143
144 /**
145 * @test
146 */
147 public function renderConvertsAlreadyConvertedEntitiesByDefault()
148 {
149 $source = 'already &quot;encoded&quot;';
150 $this->setArgumentsUnderTest(
151 $this->viewHelper,
152 [
153 'value' => $source
154 ]
155 );
156 $expectedResult = 'already &amp;quot;encoded&amp;quot;';
157 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
158 $this->assertEquals($expectedResult, $actualResult);
159 }
160
161 /**
162 * @test
163 */
164 public function renderDoesNotConvertAlreadyConvertedEntitiesIfDoubleQuoteIsFalse()
165 {
166 $source = 'already &quot;encoded&quot;';
167 $this->setArgumentsUnderTest(
168 $this->viewHelper,
169 [
170 'value' => $source,
171 'doubleEncode' => false,
172 ]
173 );
174 $expectedResult = 'already &quot;encoded&quot;';
175 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
176 $this->assertEquals($expectedResult, $actualResult);
177 }
178
179 /**
180 * This test tests the behaviour of render without relying on the validation of registerArguments
181 * In the normal course of things "value" can't be anything but a string as it is registered that way
182 *
183 * @test
184 */
185 public function renderReturnsUnmodifiedSourceIfItIsNoString()
186 {
187 $source = new \stdClass();
188 $this->setArgumentsUnderTest(
189 $this->viewHelper,
190 [
191 'value' => $source
192 ]
193 );
194 $actualResult = $this->viewHelper->render();
195 $this->assertSame($source, $actualResult);
196 }
197
198 /**
199 * Ensures that objects are handled properly:
200 * + class not having __toString() method as given
201 * + class having __toString() method gets encoded
202 *
203 * @param object $source
204 * @param mixed $expectation
205 * @test
206 * @dataProvider renderEscapesObjectIfPossibleDataProvider
207 */
208 public function renderEscapesObjectIfPossible($source, $expectation)
209 {
210 $this->setArgumentsUnderTest(
211 $this->viewHelper,
212 [
213 'value' => $source
214 ]
215 );
216 $actualResult = $this->viewHelper->render();
217 $this->assertSame($expectation, $actualResult);
218 }
219
220 /**
221 * @return array
222 */
223 public function renderEscapesObjectIfPossibleDataProvider(): array
224 {
225 $stdClass = new \stdClass();
226 $toStringClass = new class() {
227 public function __toString(): string
228 {
229 return '<script>alert(\'"&xss"\')</script>';
230 }
231 };
232
233 return [
234 'plain object' => [$stdClass, $stdClass],
235 'object with __toString()' => [$toStringClass, '&lt;script&gt;alert(&#039;&quot;&amp;xss&quot;&#039;)&lt;/script&gt;'],
236 ];
237 }
238 }