‪TYPO3CMS  10.4
HtmlentitiesViewHelperTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
20 
24 class ‪HtmlentitiesViewHelperTest extends ViewHelperBaseTestcase
25 {
26 
30  protected ‪$viewHelper;
31 
38 
39  protected function ‪setUp(): void
40  {
41  parent::setUp();
42  $this->viewHelper = new ‪HtmlentitiesViewHelper();
43  $this->injectDependenciesIntoViewHelper($this->viewHelper);
44  }
45 
50  {
51  $this->setArgumentsUnderTest(
52  $this->viewHelper,
53  [
54  'value' => 'Some string',
55  ]
56  );
57  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
58  self::assertEquals('Some string', $actualResult);
59  }
60 
65  {
66  $this->viewHelper->setRenderChildrenClosure(
67  function () {
68  return 'Some string';
69  }
70  );
71  $this->setArgumentsUnderTest($this->viewHelper);
72  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
73  self::assertEquals('Some string', $actualResult);
74  }
75 
80  {
81  $source = 'This is a sample text without special characters.';
82  $this->setArgumentsUnderTest(
83  $this->viewHelper,
84  [
85  'value' => $source,
86  ]
87  );
88  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
89  self::assertSame($source, $actualResult);
90  }
91 
95  public function ‪renderEncodesSimpleString()
96  {
97  $source = 'Some special characters: &©"\'';
98  $this->setArgumentsUnderTest(
99  $this->viewHelper,
100  [
101  'value' => $source
102  ]
103  );
104  $expectedResult = 'Some special characters: &amp;&copy;&quot;&#039;';
105  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
106  self::assertEquals($expectedResult, $actualResult);
107  }
108 
112  public function ‪renderRespectsKeepQuoteArgument()
113  {
114  $source = 'Some special characters: &©"\'';
115  $this->setArgumentsUnderTest(
116  $this->viewHelper,
117  [
118  'value' => $source,
119  'keepQuotes' => true,
120  ]
121  );
122  $expectedResult = 'Some special characters: &amp;&copy;"\'';
123  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
124  self::assertEquals($expectedResult, $actualResult);
125  }
126 
130  public function ‪renderRespectsEncodingArgument()
131  {
132  $source = utf8_decode('Some special characters: &©"\'');
133  $this->setArgumentsUnderTest(
134  $this->viewHelper,
135  [
136  'value' => $source,
137  'encoding' => 'ISO-8859-1',
138  ]
139  );
140  $expectedResult = 'Some special characters: &amp;&copy;&quot;&#039;';
141  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
142  self::assertEquals($expectedResult, $actualResult);
143  }
144 
149  {
150  $source = 'already &quot;encoded&quot;';
151  $this->setArgumentsUnderTest(
152  $this->viewHelper,
153  [
154  'value' => $source
155  ]
156  );
157  $expectedResult = 'already &amp;quot;encoded&amp;quot;';
158  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
159  self::assertEquals($expectedResult, $actualResult);
160  }
161 
166  {
167  $source = 'already &quot;encoded&quot;';
168  $this->setArgumentsUnderTest(
169  $this->viewHelper,
170  [
171  'value' => $source,
172  'doubleEncode' => false,
173  ]
174  );
175  $expectedResult = 'already &quot;encoded&quot;';
176  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
177  self::assertEquals($expectedResult, $actualResult);
178  }
179 
187  {
188  $source = new \stdClass();
189  $this->setArgumentsUnderTest(
190  $this->viewHelper,
191  [
192  'value' => $source
193  ]
194  );
195  $actualResult = $this->viewHelper->render();
196  self::assertSame($source, $actualResult);
197  }
198 
209  public function ‪renderEscapesObjectIfPossible($source, $expectation)
210  {
211  $this->setArgumentsUnderTest(
212  $this->viewHelper,
213  [
214  'value' => $source
215  ]
216  );
217  $actualResult = $this->viewHelper->render();
218  self::assertSame($expectation, $actualResult);
219  }
220 
224  public function ‪renderEscapesObjectIfPossibleDataProvider(): array
225  {
226  $stdClass = new \stdClass();
227  $toStringClass = new class() {
228  public function __toString(): string
229  {
230  return '<script>alert(\'"&xss"\')</script>';
231  }
232  };
233 
234  return [
235  'plain object' => [$stdClass, $stdClass],
236  'object with __toString()' => [$toStringClass, '&lt;script&gt;alert(&#039;&quot;&amp;xss&quot;&#039;)&lt;/script&gt;'],
237  ];
238  }
239 }
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\HtmlentitiesViewHelperTest\$defaultArguments
‪array $defaultArguments
Definition: HtmlentitiesViewHelperTest.php:35
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\HtmlentitiesViewHelperTest\renderUsesChildnodesAsSourceIfSpecified
‪renderUsesChildnodesAsSourceIfSpecified()
Definition: HtmlentitiesViewHelperTest.php:62
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\HtmlentitiesViewHelperTest\renderDoesNotModifyValueIfItDoesNotContainSpecialCharacters
‪renderDoesNotModifyValueIfItDoesNotContainSpecialCharacters()
Definition: HtmlentitiesViewHelperTest.php:77
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\HtmlentitiesViewHelperTest\renderDoesNotConvertAlreadyConvertedEntitiesIfDoubleQuoteIsFalse
‪renderDoesNotConvertAlreadyConvertedEntitiesIfDoubleQuoteIsFalse()
Definition: HtmlentitiesViewHelperTest.php:163
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\HtmlentitiesViewHelperTest\renderEscapesObjectIfPossible
‪renderEscapesObjectIfPossible($source, $expectation)
Definition: HtmlentitiesViewHelperTest.php:207
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format
Definition: ScriptViewHelperTest.php:18
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\HtmlentitiesViewHelperTest\renderEscapesObjectIfPossibleDataProvider
‪array renderEscapesObjectIfPossibleDataProvider()
Definition: HtmlentitiesViewHelperTest.php:222
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\HtmlentitiesViewHelperTest\renderRespectsEncodingArgument
‪renderRespectsEncodingArgument()
Definition: HtmlentitiesViewHelperTest.php:128
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\HtmlentitiesViewHelperTest\renderUsesValueAsSourceIfSpecified
‪renderUsesValueAsSourceIfSpecified()
Definition: HtmlentitiesViewHelperTest.php:47
‪TYPO3\CMS\Fluid\ViewHelpers\Format\HtmlentitiesViewHelper
Definition: HtmlentitiesViewHelper.php:51
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\HtmlentitiesViewHelperTest
Definition: HtmlentitiesViewHelperTest.php:25
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\HtmlentitiesViewHelperTest\renderReturnsUnmodifiedSourceIfItIsNoString
‪renderReturnsUnmodifiedSourceIfItIsNoString()
Definition: HtmlentitiesViewHelperTest.php:184
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\HtmlentitiesViewHelperTest\renderConvertsAlreadyConvertedEntitiesByDefault
‪renderConvertsAlreadyConvertedEntitiesByDefault()
Definition: HtmlentitiesViewHelperTest.php:146
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\HtmlentitiesViewHelperTest\$viewHelper
‪TYPO3 CMS Fluid ViewHelpers Format HtmlentitiesViewHelper $viewHelper
Definition: HtmlentitiesViewHelperTest.php:29
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\HtmlentitiesViewHelperTest\renderEncodesSimpleString
‪renderEncodesSimpleString()
Definition: HtmlentitiesViewHelperTest.php:93
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\HtmlentitiesViewHelperTest\setUp
‪setUp()
Definition: HtmlentitiesViewHelperTest.php:37
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\HtmlentitiesViewHelperTest\renderRespectsKeepQuoteArgument
‪renderRespectsKeepQuoteArgument()
Definition: HtmlentitiesViewHelperTest.php:110