d35ea53907a29f964ab2b9d35e2d8145aca05ac7
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / Core / Parser / PreProcessor / XmlnsNamespaceTemplatePreProcessorTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\Core\Parser\PreProcessor;
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 use TYPO3\CMS\Core\Utility\GeneralUtility;
17 use TYPO3\CMS\Fluid\Core\Parser\PreProcessor\XmlnsNamespaceTemplatePreProcessor;
18 use TYPO3\CMS\Fluid\Core\ViewHelper\ViewHelperResolver;
19 use TYPO3\CMS\Fluid\Tests\Unit\Core\Rendering\RenderingContextFixture;
20
21 /**
22 * Class XmlnsNamespaceTemplatePreProcessorTest
23 */
24 class XmlnsNamespaceTemplatePreProcessorTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
25 {
26 /**
27 * @param string $source
28 * @param array $expectedNamespaces
29 * @param string $expectedSource
30 * @test
31 * @dataProvider preProcessSourceExtractsNamespacesAndRemovesTagsAndAttributesDataProvider
32 */
33 public function preProcessSourceExtractsNamespacesAndRemovesTagsAndAttributes($source, array $expectedNamespaces, $expectedSource)
34 {
35 $subject = new XmlnsNamespaceTemplatePreProcessor();
36 $resolver = $this->getMockBuilder(ViewHelperResolver::class)
37 ->setMethods(['addNamespace'])
38 ->getMock();
39 $context = $this->getMockBuilder(RenderingContextFixture::class)
40 ->setMethods(['getViewHelperResolver'])
41 ->getMock();
42 if (empty($expectedNamespaces)) {
43 $context->expects($this->never())->method('getViewHelperResolver');
44 $resolver->expects($this->never())->method('addNamespace');
45 } else {
46 $context->expects($this->exactly(count($expectedNamespaces)))->method('getViewHelperResolver')->willReturn($resolver);
47 foreach ($expectedNamespaces as $index => $expectedNamespaceParts) {
48 list($prefix, $phpNamespace) = $expectedNamespaceParts;
49 $resolver->expects($this->at($index))->method('addNamespace')->with($prefix, $phpNamespace);
50 }
51 }
52 $subject->setRenderingContext($context);
53 $result = $subject->preProcessSource($source);
54 if ($expectedSource === null) {
55 $this->assertEquals($source, $result);
56 } else {
57 $this->assertEquals($expectedSource, $result);
58 }
59 }
60
61 /**
62 * DataProvider for preProcessSourceExtractsNamespacesAndRemovesTagsAndAttributes test
63 *
64 * @return array
65 */
66 public function preProcessSourceExtractsNamespacesAndRemovesTagsAndAttributesDataProvider()
67 {
68 return [
69 'Empty source raises no errors' => [
70 '',
71 [],
72 null,
73 ],
74 'Tags without xmlns remain untouched' => [
75 '<div class="not-touched">...</div>',
76 [],
77 null
78 ],
79 'Third-party namespace not detected' => [
80 '<html xmlns:notdetected="http://thirdparty.org/ns/Foo/Bar/ViewHelpers">...</html>',
81 [],
82 null
83 ],
84 'Detects and removes Fluid namespaces by namespace URL' => [
85 '<html xmlns:detected="http://typo3.org/ns/Foo/Bar/ViewHelpers" data-namespace-typo3-fluid="true">...</html>',
86 [
87 ['detected', 'Foo\\Bar\\ViewHelpers']
88 ],
89 '...'
90 ],
91 'Skips fluid namespace if namespace URL is not the correct case' => [
92 '<html xmlns:detected="http://typo3.org/Ns/Foo/Bar/ViewHelpers" data-namespace-typo3-fluid="true">...</html>',
93 [],
94 null
95 ],
96 'Skips fluid namespace if attribute is not the correct case' => [
97 '<html xmlNS:detected="http://typo3.org/ns/Foo/Bar/ViewHelpers" data-namespace-typo3-fluid="true">...</html>',
98 [],
99 null
100 ],
101 'Skips namespace if attribute before xmlns attribute without space in between' => [
102 '<html lang="de"xmlns:detected="http://typo3.org/ns/Foo/Bar/ViewHelpers" data-namespace-typo3-fluid="true">...</html>',
103 [],
104 null
105 ],
106 'Removes tag if data attribute set and non xmlns attributes are used prior to xmlns' => [
107 '<html lang="de" xmlns:detected="http://typo3.org/ns/Foo/Bar/ViewHelpers" data-namespace-typo3-fluid="true">...</html>',
108 [
109 ['detected', 'Foo\\Bar\\ViewHelpers']
110 ],
111 '...'
112 ],
113 'Skips invalid namespace prefixes' => [
114 '<html xmlns:bad-prefix="http://typo3.org/ns/Foo/Bar/ViewHelpers">...</html>',
115 [],
116 null
117 ],
118 'Detect and remove multiple ViewHelper attributes' => [
119 '<div xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"' . "\n"
120 . "\t" . 'xmlns:fe="http://typo3.org/ns/TYPO3/CMS/Frontend/ViewHelpers">' . "\n"
121 . "\t\t" . '<f:if condition="{demo}">Hello World</f:if>' . "\n"
122 . '</div>',
123 [
124 ['f', 'TYPO3\\CMS\\Fluid\\ViewHelpers'],
125 ['fe', 'TYPO3\\CMS\\Frontend\\ViewHelpers']
126 ],
127 '<div >' . "\n"
128 . "\t\t" . '<f:if condition="{demo}">Hello World</f:if>' . "\n"
129 . '</div>'
130 ],
131 'ViewHelpers found with non ViewHelper xmlns at beginning' => [
132 '<div xmlns:z="http://www.typo3.org/foo"' . "\n"
133 . "\t" . 'xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"' . "\n"
134 . "\t" . 'xmlns:fe="http://typo3.org/ns/TYPO3/CMS/Frontend/ViewHelpers">' . "\n"
135 . "\t\t" . '<f:if condition="{demo}">Hello World</f:if>' . "\n"
136 . '</div>',
137 [
138 ['f', 'TYPO3\\CMS\\Fluid\\ViewHelpers'],
139 ['fe', 'TYPO3\\CMS\\Frontend\\ViewHelpers']
140 ],
141 '<div xmlns:z="http://www.typo3.org/foo" >' . "\n"
142 . "\t\t" . '<f:if condition="{demo}">Hello World</f:if>' . "\n"
143 . '</div>'
144 ],
145 'ViewHelpers found with non ViewHelper xmlns at end' => [
146 '<div xmlns:fe="http://typo3.org/ns/TYPO3/CMS/Frontend/ViewHelpers"' . "\n"
147 . "\t" . 'xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"' . "\n"
148 . "\t" . 'xmlns:z="http://www.typo3.org/foo">' . "\n"
149 . "\t\t" . '<f:if condition="{demo}">Hello World</f:if>' . "\n"
150 . '</div>',
151 [
152 ['fe', 'TYPO3\\CMS\\Frontend\\ViewHelpers'],
153 ['f', 'TYPO3\\CMS\\Fluid\\ViewHelpers']
154 ],
155 '<div xmlns:z="http://www.typo3.org/foo">' . "\n"
156 . "\t\t" . '<f:if condition="{demo}">Hello World</f:if>' . "\n"
157 . '</div>',
158 ],
159 'Xmlns ViewHelpers found with multiple non ViewHelper xmlns attributes' => [
160 '<div xmlns:fe="http://typo3.org/ns/TYPO3/CMS/Frontend/ViewHelpers"' . "\n"
161 . "\t" . 'xmlns:y="http://www.typo3.org/bar"' . "\n"
162 . "\t" . 'xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"' . "\n"
163 . "\t" . 'xmlns:z="http://www.typo3.org/foo">' . "\n"
164 . "\t\t" . '<f:if condition="{demo}">Hello World</f:if>' . "\n"
165 . '</div>',
166 [
167 ['fe', 'TYPO3\\CMS\\Frontend\\ViewHelpers'],
168 ['f', 'TYPO3\\CMS\\Fluid\\ViewHelpers']
169 ],
170 '<div xmlns:y="http://www.typo3.org/bar" xmlns:z="http://www.typo3.org/foo">' . "\n"
171 . "\t\t" . '<f:if condition="{demo}">Hello World</f:if>' . "\n"
172 . '</div>'
173 ],
174 'Xmlns ViewHelpers found with non ViewHelpers between' => [
175 '<div xmlns:fe="http://typo3.org/ns/TYPO3/CMS/Frontend/ViewHelpers"' . "\n"
176 . "\t" . 'xmlns:z="http://www.typo3.org/foo"' . "\n"
177 . "\t" . 'xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers">' . "\n"
178 . "\t\t" . '<f:if condition="{demo}">Hello World</f:if>' . "\n"
179 . '</div>',
180 [
181 ['fe', 'TYPO3\\CMS\\Frontend\\ViewHelpers'],
182 ['f', 'TYPO3\\CMS\\Fluid\\ViewHelpers']
183 ],
184 '<div xmlns:z="http://www.typo3.org/foo" >' . "\n"
185 . "\t\t" . '<f:if condition="{demo}">Hello World</f:if>' . "\n"
186 . '</div>'
187 ],
188 'Do not remove Html tag with data attribute but no xmlns ViewHelpers found' => [
189 '<html data-namespace-typo3-fluid="true">' . "\n"
190 . "\t" . '<f:if condition="{demo}">Hello World</f:if>' . "\n"
191 . '</html>',
192 [],
193 null
194 ],
195 'Keep html tag if data attribute is not set and remove ViewHelper attributes' => [
196 '<html xmlns:fe="http://typo3.org/ns/TYPO3/CMS/Frontend/ViewHelpers"' . "\n"
197 . "\t" . 'xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers"' . "\n"
198 . "\t" . 'xmlns:z="http://www.typo3.org/foo">' . "\n"
199 . "\t\t" . '<f:if condition="{demo}">Hello World</f:if>' . "\n"
200 . '</html>',
201 [
202 ['fe', 'TYPO3\\CMS\\Frontend\\ViewHelpers'],
203 ['f', 'TYPO3\\CMS\\Fluid\\ViewHelpers']
204 ],
205 '<html xmlns:z="http://www.typo3.org/foo">' . "\n"
206 . "\t\t" . '<f:if condition="{demo}">Hello World</f:if>' . "\n"
207 . '</html>',
208 ],
209 'Remove html tag because xmlns ViewHelpers found and data attribute set' => [
210 '<html data-namespace-typo3-fluid="true"' . "\n"
211 . "\t" . 'xmlns:fe="http://typo3.org/ns/TYPO3/CMS/Frontend/ViewHelpers"' . "\n"
212 . "\t" . 'xmlns:z="http://www.typo3.org/foo">' . "\n"
213 . "\t\t" . '<f:if condition="{demo}">Hello World</f:if>' . "\n"
214 . '</html>',
215 [
216 ['fe', 'TYPO3\\CMS\\Frontend\\ViewHelpers']
217 ],
218 "\n\t\t" . '<f:if condition="{demo}">Hello World</f:if>' . "\n"
219 ],
220 'Test with big markup template' => [
221 file_get_contents(GeneralUtility::getFileAbsFileName('EXT:fluid/Tests/Unit/Core/Fixtures/TestNamespaceTemplateBig.html')),
222 [
223 ['f', 'TYPO3\\CMS\\Fluid\\ViewHelpers'],
224 ['core', 'TYPO3\\CMS\\Core\\ViewHelpers'],
225 ['fl', 'TYPO3\\CMS\\Filelist\\ViewHelpers']
226 ],
227 file_get_contents(GeneralUtility::getFileAbsFileName('EXT:fluid/Tests/Unit/Core/Fixtures/TestNamespaceTemplateBigExpectedResult.html'))
228 ],
229 'Only handle first tag with xmlns ViewHelpers found' => [
230 '<div xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers">' . "\n"
231 . "\t" . '<div data-namespace-typo3-fluid="true" xmlns:fe="http://typo3.org/ns/TYPO3/CMS/Frontend/ViewHelpers"' . "\n"
232 . "\t\t" . 'xmlns:z="http://www.typo3.org/foo">' . "\n"
233 . "\t\t\t" . '<f:if condition="{demo}">Hello World</f:if>' . "\n"
234 . "\t" . '</div>' . "\n"
235 . '</div>',
236 [
237 ['f', 'TYPO3\\CMS\\Fluid\\ViewHelpers']
238 ],
239 '<div >' . "\n"
240 . "\t" . '<div data-namespace-typo3-fluid="true" xmlns:fe="http://typo3.org/ns/TYPO3/CMS/Frontend/ViewHelpers"' . "\n"
241 . "\t\t" . 'xmlns:z="http://www.typo3.org/foo">' . "\n"
242 . "\t\t\t" . '<f:if condition="{demo}">Hello World</f:if>' . "\n"
243 . "\t" . '</div>' . "\n"
244 . '</div>'
245 ]
246 ];
247 }
248 }