3a43dd1334b47646c4f71f11a0812198bf92b90c
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Format / HtmlentitiesDecodeViewHelperTest.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 Prophecy\Prophecy\ObjectProphecy;
18 use TYPO3\CMS\Extbase\Reflection\ReflectionService;
19 use TYPO3\CMS\Fluid\ViewHelpers\Format\HtmlentitiesDecodeViewHelper;
20 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
21
22 /**
23 * Test case
24 */
25 class HtmlentitiesDecodeViewHelperTest extends ViewHelperBaseTestcase
26 {
27 /**
28 * @var ReflectionService|ObjectProphecy
29 */
30 protected $reflectionServiceProphecy;
31
32 /**
33 * @var HtmlentitiesDecodeViewHelper
34 */
35 protected $viewHelper;
36
37 /**
38 * shortcut for default Arguments which would be prepared by initializeArguments()
39 *
40 * @var array
41 */
42 protected $defaultArguments;
43
44 protected function setUp()
45 {
46 parent::setUp();
47 $this->reflectionServiceProphecy = $this->prophesize(ReflectionService::class);
48 $this->viewHelper = new HtmlentitiesDecodeViewHelper();
49 $this->viewHelper->injectReflectionService($this->reflectionServiceProphecy->reveal());
50 $this->injectDependenciesIntoViewHelper($this->viewHelper);
51 }
52
53 /**
54 * @test
55 */
56 public function renderUsesValueAsSourceIfSpecified()
57 {
58 $this->setArgumentsUnderTest(
59 $this->viewHelper,
60 [
61 'value' => 'Some string'
62 ]
63 );
64 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
65 $this->assertEquals('Some string', $actualResult);
66 }
67
68 /**
69 * @test
70 */
71 public function renderUsesChildnodesAsSourceIfSpecified()
72 {
73 $this->viewHelper->setRenderChildrenClosure(
74 function () {
75 return 'Some string';
76 }
77 );
78 $this->setArgumentsUnderTest($this->viewHelper);
79 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
80 $this->assertEquals('Some string', $actualResult);
81 }
82
83 /**
84 * @test
85 */
86 public function renderDoesNotModifyValueIfItDoesNotContainSpecialCharacters()
87 {
88 $source = 'This is a sample text without special characters. <> &©"\'';
89 $this->setArgumentsUnderTest(
90 $this->viewHelper,
91 [
92 'value' => $source,
93 ]
94 );
95 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
96 $this->assertSame($source, $actualResult);
97 }
98
99 /**
100 * @test
101 */
102 public function renderDecodesSimpleString()
103 {
104 $source = 'Some special characters: &amp; &quot; \' &lt; &gt; *';
105 $expectedResult = 'Some special characters: & " \' < > *';
106 $this->setArgumentsUnderTest(
107 $this->viewHelper,
108 [
109 'value' => $source
110 ]
111 );
112 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
113 $this->assertEquals($expectedResult, $actualResult);
114 }
115
116 /**
117 * @test
118 */
119 public function renderRespectsKeepQuoteArgument()
120 {
121 $source = 'Some special characters: &amp; &quot; \' &lt; &gt; *';
122 $expectedResult = 'Some special characters: & &quot; \' < > *';
123 $this->setArgumentsUnderTest(
124 $this->viewHelper,
125 [
126 'value' => $source,
127 'keepQuotes' => true,
128 ]
129 );
130 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
131 $this->assertEquals($expectedResult, $actualResult);
132 }
133
134 /**
135 * @test
136 */
137 public function renderRespectsEncodingArgument()
138 {
139 $source = utf8_decode('Some special characters: &amp; &quot; \' &lt; &gt; *');
140 $expectedResult = 'Some special characters: & " \' < > *';
141 $this->setArgumentsUnderTest(
142 $this->viewHelper,
143 [
144 'value' => $source,
145 'encoding' => 'ISO-8859-1',
146 ]
147 );
148 $actualResult = $this->viewHelper->initializeArgumentsAndRender();
149 $this->assertEquals($expectedResult, $actualResult);
150 }
151
152 /**
153 * @test
154 */
155 public function renderReturnsUnmodifiedSourceIfItIsNoString()
156 {
157 $source = new \stdClass();
158 $this->setArgumentsUnderTest(
159 $this->viewHelper,
160 [
161 'value' => $source
162 ]
163 );
164 $actualResult = $this->viewHelper->render();
165 $this->assertSame($source, $actualResult);
166 }
167 }