39c557c386cb236810fd0ccd4d3e82905ebc2731
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Configuration / RichtextTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Configuration;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Configuration\Richtext;
19
20 /**
21 * Test case
22 */
23 class RichtextTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
24 {
25 /**
26 * @test
27 */
28 public function getConfigurationUsesOverruleModeFromType()
29 {
30 $fieldConfig = [
31 'type' => 'text',
32 'enableRichtext' => true,
33 ];
34 $pageTsConfig = [
35 'properties' => [
36 'classes.' => [
37 'aClass' => 'aConfig',
38 ],
39 'default.' => [
40 'removeComments' => '1',
41 ],
42 'config.' => [
43 'aTable.' => [
44 'aField.' => [
45 'types.' => [
46 'textmedia.' => [
47 'proc.' => [
48 'overruleMode' => 'myTransformation',
49 ],
50 ]
51 ]
52 ]
53 ]
54 ]
55 ]
56 ];
57 $expected = [
58 'classes.' => [
59 'aClass' => 'aConfig',
60 ],
61 'removeComments' => '1',
62 'proc.' => [
63 'overruleMode' => 'myTransformation',
64 ],
65 'classes' => [
66 'aClass' => 'aConfig',
67 ],
68 'proc' => [
69 'overruleMode' => 'myTransformation',
70 ],
71 ];
72 // Accessible mock to $subject since getRtePageTsConfigOfPid calls BackendUtility::getPagesTSconfig()
73 // which can't be mocked in a sane way
74 $subject = $this->getAccessibleMock(Richtext::class, ['getRtePageTsConfigOfPid'], [], '', false);
75 $subject->expects($this->once())->method('getRtePageTsConfigOfPid')->with(42)->willReturn($pageTsConfig);
76 $output = $subject->getConfiguration('aTable', 'aField', 42, 'textmedia', $fieldConfig);
77 $this->assertSame($expected, $output);
78 }
79
80 /**
81 * @test
82 */
83 public function getConfigurationUsesOverruleModeFromConfig()
84 {
85 $fieldConfig = [
86 'type' => 'text',
87 'enableRichtext' => true,
88 ];
89 $pageTsConfig = [
90 'properties' => [
91 'classes.' => [
92 'aClass' => 'aConfig',
93 ],
94 'default.' => [
95 'removeComments' => '1',
96 ],
97 'config.' => [
98 'aTable.' => [
99 'aField.' => [
100 'proc.' => [
101 'overruleMode' => 'myTransformation',
102 ],
103 ]
104 ]
105 ]
106 ]
107 ];
108 $expected = [
109 'classes.' => [
110 'aClass' => 'aConfig',
111 ],
112 'removeComments' => '1',
113 'proc.' => [
114 'overruleMode' => 'myTransformation',
115 ],
116 'classes' => [
117 'aClass' => 'aConfig',
118 ],
119 'proc' => [
120 'overruleMode' => 'myTransformation',
121 ],
122 ];
123 // Accessible mock to $subject since getRtePageTsConfigOfPid calls BackendUtility::getPagesTSconfig()
124 // which can't be mocked in a sane way
125 $subject = $this->getAccessibleMock(Richtext::class, ['getRtePageTsConfigOfPid'], [], '', false);
126 $subject->expects($this->once())->method('getRtePageTsConfigOfPid')->with(42)->willReturn($pageTsConfig);
127 $output = $subject->getConfiguration('aTable', 'aField', 42, 'textmedia', $fieldConfig);
128 $this->assertSame($expected, $output);
129 }
130
131 /**
132 * @test
133 */
134 public function getConfigurationSetsOverruleModeIfMissing()
135 {
136 $fieldConfig = [
137 'type' => 'text',
138 'enableRichtext' => true,
139 ];
140 $pageTsConfig = [
141 'properties' => [
142 'classes.' => [
143 'aClass' => 'aConfig',
144 ],
145 'default.' => [
146 'removeComments' => '1',
147 ],
148 ]
149 ];
150 $expected = [
151 'classes.' => [
152 'aClass' => 'aConfig',
153 ],
154 'removeComments' => '1',
155 'classes' => [
156 'aClass' => 'aConfig',
157 ],
158 'proc.' => [
159 'overruleMode' => 'default',
160 ],
161 ];
162 // Accessible mock to $subject since getRtePageTsConfigOfPid calls BackendUtility::getPagesTSconfig()
163 // which can't be mocked in a sane way
164 $subject = $this->getAccessibleMock(Richtext::class, ['getRtePageTsConfigOfPid'], [], '', false);
165 $subject->expects($this->once())->method('getRtePageTsConfigOfPid')->with(42)->willReturn($pageTsConfig);
166 $output = $subject->getConfiguration('aTable', 'aField', 42, 'textmedia', $fieldConfig);
167 $this->assertSame($expected, $output);
168 }
169
170 /**
171 * @test
172 */
173 public function getConfigurationOverridesByDefault()
174 {
175 $fieldConfig = [
176 'type' => 'text',
177 'enableRichtext' => true,
178 ];
179 $pageTsConfig = [
180 'properties' => [
181 'classes.' => [
182 'aClass' => 'aConfig',
183 ],
184 'default.' => [
185 'classes.' => [
186 'aClass' => 'anotherConfig',
187 ],
188 'editor.' => [
189 'config.' => [
190 'contentsCss' => 'my.css'
191 ]
192 ],
193 ],
194 ],
195 ];
196 $expected = [
197 'classes.' => [
198 'aClass' => 'anotherConfig',
199 ],
200 'editor.' => [
201 'config.' => [
202 'contentsCss' => 'my.css'
203 ]
204 ],
205 'classes' => [
206 'aClass' => 'anotherConfig',
207 ],
208 'editor' => [
209 'config' => [
210 'contentsCss' => 'my.css'
211 ]
212 ],
213 'proc.' => [
214 'overruleMode' => 'default',
215 ],
216 ];
217 // Accessible mock to $subject since getRtePageTsConfigOfPid calls BackendUtility::getPagesTSconfig()
218 // which can't be mocked in a sane way
219 $subject = $this->getAccessibleMock(Richtext::class, ['getRtePageTsConfigOfPid'], [], '', false);
220 $subject->expects($this->once())->method('getRtePageTsConfigOfPid')->with(42)->willReturn($pageTsConfig);
221 $output = $subject->getConfiguration('aTable', 'aField', 42, 'textmedia', $fieldConfig);
222 $this->assertSame($expected, $output);
223 }
224
225 /**
226 * @test
227 */
228 public function getConfigurationOverridesByFieldSpecificConfig()
229 {
230 $fieldConfig = [
231 'type' => 'text',
232 'enableRichtext' => true,
233 ];
234 $pageTsConfig = [
235 'properties' => [
236 'classes.' => [
237 'aClass' => 'aConfig',
238 ],
239 'default.' => [
240 'classes.' => [
241 'aClass' => 'anotherConfig',
242 ],
243 ],
244 'config.' => [
245 'aTable.' => [
246 'aField.' => [
247 'classes.' => [
248 'aClass' => 'aThirdConfig',
249 ],
250 'editor.' => [
251 'config.' => [
252 'contentsCss' => 'my.css'
253 ]
254 ],
255 ],
256 ],
257 ],
258 ],
259 ];
260 $expected = [
261 // Config with pagets dots
262 'classes.' => [
263 'aClass' => 'aThirdConfig',
264 ],
265 'editor.' => [
266 'config.' => [
267 'contentsCss' => 'my.css'
268 ]
269 ],
270 // Config without pagets dots
271 'classes' => [
272 'aClass' => 'aThirdConfig',
273 ],
274 'editor' => [
275 'config' => [
276 'contentsCss' => 'my.css'
277 ]
278 ],
279 'proc.' => [
280 'overruleMode' => 'default',
281 ],
282 ];
283 // Accessible mock to $subject since getRtePageTsConfigOfPid calls BackendUtility::getPagesTSconfig()
284 // which can't be mocked in a sane way
285 $subject = $this->getAccessibleMock(Richtext::class, ['getRtePageTsConfigOfPid'], [], '', false);
286 $subject->expects($this->once())->method('getRtePageTsConfigOfPid')->with(42)->willReturn($pageTsConfig);
287 $output = $subject->getConfiguration('aTable', 'aField', 42, 'textmedia', $fieldConfig);
288 $this->assertSame($expected, $output);
289 }
290
291 /**
292 * @test
293 */
294 public function getConfigurationOverridesByFieldAndTypeSpecificConfig()
295 {
296 $fieldConfig = [
297 'type' => 'text',
298 'enableRichtext' => true,
299 ];
300 $pageTsConfig = [
301 'properties' => [
302 'classes.' => [
303 'aClass' => 'aConfig',
304 ],
305 'default.' => [
306 'classes.' => [
307 'aClass' => 'anotherConfig',
308 ],
309 ],
310 'config.' => [
311 'aTable.' => [
312 'aField.' => [
313 'classes.' => [
314 'aClass' => 'aThirdConfig',
315 ],
316 'editor.' => [
317 'config.' => [
318 'contentsCss' => 'my.css'
319 ]
320 ],
321 'types.' => [
322 'textmedia.' => [
323 'classes.' => [
324 'aClass' => 'aTypeSpecifcConfig',
325 ],
326 'editor.' => [
327 'config.' => [
328 'contentsCss' => 'your.css'
329 ]
330 ],
331 ]
332 ]
333 ],
334 ],
335 ],
336 ],
337 ];
338 $expected = [
339 // Config with pagets dots
340 'classes.' => [
341 'aClass' => 'aTypeSpecifcConfig',
342 ],
343 'editor.' => [
344 'config.' => [
345 'contentsCss' => 'your.css'
346 ]
347 ],
348 // Config without pagets dots
349 'classes' => [
350 'aClass' => 'aTypeSpecifcConfig',
351 ],
352 'editor' => [
353 'config' => [
354 'contentsCss' => 'your.css'
355 ]
356 ],
357 'proc.' => [
358 'overruleMode' => 'default',
359 ],
360 ];
361 // Accessible mock to $subject since getRtePageTsConfigOfPid calls BackendUtility::getPagesTSconfig()
362 // which can't be mocked in a sane way
363 $subject = $this->getAccessibleMock(Richtext::class, ['getRtePageTsConfigOfPid'], [], '', false);
364 $subject->expects($this->once())->method('getRtePageTsConfigOfPid')->with(42)->willReturn($pageTsConfig);
365 $output = $subject->getConfiguration('aTable', 'aField', 42, 'textmedia', $fieldConfig);
366 $this->assertSame($expected, $output);
367 }
368
369 /**
370 * @test
371 */
372 public function getConfigurationPageTsOverridesPreset()
373 {
374 $pageId = 42;
375 $presetKey = 'default';
376
377 $preset = [
378 'editor' => [
379 'config' => [
380 'width' => 100
381 ],
382 ],
383 ];
384
385 $pageTsConfigArray = [
386 'properties' => [
387 'preset' => $presetKey,
388 'editor.' => [
389 'config.' => [
390 'width' => 200
391 ],
392 ],
393 ],
394 ];
395
396 $subject = $this->getAccessibleMock(
397 Richtext::class,
398 ['loadConfigurationFromPreset', 'getRtePageTsConfigOfPid'],
399 [],
400 '',
401 false
402 );
403 $subject->expects($this->once())->method('loadConfigurationFromPreset')->with($presetKey)->willReturn($preset);
404 $subject->expects($this->once())->method('getRtePageTsConfigOfPid')->with($pageId)->willReturn($pageTsConfigArray);
405
406 $output = $subject->getConfiguration('tt_content', 'bodytext', $pageId, 'textmedia', $pageTsConfigArray);
407
408 $expected = [
409 'editor' => [
410 'config' => [
411 'width' => 200
412 ],
413 ],
414 'preset' => 'default',
415 'editor.' => [
416 'config.' => [
417 'width' => 200
418 ],
419 ],
420 'proc.' => [
421 'overruleMode' => 'default',
422 ],
423 ];
424
425 $this->assertSame($expected, $output);
426 }
427 }