TYPO3 CMS  TYPO3_8-7
HtmlentitiesViewHelperTest.php
Go to the documentation of this file.
1 <?php
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 
19 
23 class HtmlentitiesViewHelperTest extends ViewHelperBaseTestcase
24 {
25 
29  protected $viewHelper;
30 
36  protected $defaultArguments;
37 
38  protected function setUp()
39  {
40  parent::setUp();
41  $this->viewHelper = new HtmlentitiesViewHelper();
42  $this->injectDependenciesIntoViewHelper($this->viewHelper);
43  }
44 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 }