[BUGFIX] Allow global RTE config again by PageTSConfig
[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 'classes.' => [
36 'aClass' => 'aConfig',
37 ],
38 'default.' => [
39 'removeComments' => '1',
40 ],
41 'config.' => [
42 'aTable.' => [
43 'aField.' => [
44 'types.' => [
45 'textmedia.' => [
46 'proc.' => [
47 'overruleMode' => 'myTransformation',
48 ],
49 ]
50 ]
51 ]
52 ]
53 ]
54 ];
55 $expected = [
56 'classes.' => [
57 'aClass' => 'aConfig',
58 ],
59 'removeComments' => '1',
60 'proc.' => [
61 'overruleMode' => 'myTransformation',
62 ],
63 'classes' => [
64 'aClass' => 'aConfig',
65 ],
66 'proc' => [
67 'overruleMode' => 'myTransformation',
68 ],
69 ];
70 // Accessible mock to $subject since getRtePageTsConfigOfPid calls BackendUtility::getPagesTSconfig()
71 // which can't be mocked in a sane way
72 $subject = $this->getAccessibleMock(Richtext::class, ['getRtePageTsConfigOfPid'], [], '', false);
73 $subject->expects($this->once())->method('getRtePageTsConfigOfPid')->with(42)->willReturn($pageTsConfig);
74 $output = $subject->getConfiguration('aTable', 'aField', 42, 'textmedia', $fieldConfig);
75 $this->assertSame($expected, $output);
76 }
77
78 /**
79 * @test
80 */
81 public function getConfigurationUsesOverruleModeFromConfig()
82 {
83 $fieldConfig = [
84 'type' => 'text',
85 'enableRichtext' => true,
86 ];
87 $pageTsConfig = [
88 'classes.' => [
89 'aClass' => 'aConfig',
90 ],
91 'default.' => [
92 'removeComments' => '1',
93 ],
94 'config.' => [
95 'aTable.' => [
96 'aField.' => [
97 'proc.' => [
98 'overruleMode' => 'myTransformation',
99 ],
100 ]
101 ]
102 ]
103 ];
104 $expected = [
105 'classes.' => [
106 'aClass' => 'aConfig',
107 ],
108 'removeComments' => '1',
109 'proc.' => [
110 'overruleMode' => 'myTransformation',
111 ],
112 'classes' => [
113 'aClass' => 'aConfig',
114 ],
115 'proc' => [
116 'overruleMode' => 'myTransformation',
117 ],
118 ];
119 // Accessible mock to $subject since getRtePageTsConfigOfPid calls BackendUtility::getPagesTSconfig()
120 // which can't be mocked in a sane way
121 $subject = $this->getAccessibleMock(Richtext::class, ['getRtePageTsConfigOfPid'], [], '', false);
122 $subject->expects($this->once())->method('getRtePageTsConfigOfPid')->with(42)->willReturn($pageTsConfig);
123 $output = $subject->getConfiguration('aTable', 'aField', 42, 'textmedia', $fieldConfig);
124 $this->assertSame($expected, $output);
125 }
126
127 /**
128 * @test
129 */
130 public function getConfigurationSetsOverruleModeIfMissing()
131 {
132 $fieldConfig = [
133 'type' => 'text',
134 'enableRichtext' => true,
135 ];
136 $pageTsConfig = [
137 'classes.' => [
138 'aClass' => 'aConfig',
139 ],
140 'default.' => [
141 'removeComments' => '1',
142 ],
143 ];
144 $expected = [
145 'classes.' => [
146 'aClass' => 'aConfig',
147 ],
148 'removeComments' => '1',
149 'classes' => [
150 'aClass' => 'aConfig',
151 ],
152 'proc.' => [
153 'overruleMode' => 'default',
154 ],
155 ];
156 // Accessible mock to $subject since getRtePageTsConfigOfPid calls BackendUtility::getPagesTSconfig()
157 // which can't be mocked in a sane way
158 $subject = $this->getAccessibleMock(Richtext::class, ['getRtePageTsConfigOfPid'], [], '', false);
159 $subject->expects($this->once())->method('getRtePageTsConfigOfPid')->with(42)->willReturn($pageTsConfig);
160 $output = $subject->getConfiguration('aTable', 'aField', 42, 'textmedia', $fieldConfig);
161 $this->assertSame($expected, $output);
162 }
163
164 /**
165 * @test
166 */
167 public function getConfigurationOverridesByDefault()
168 {
169 $fieldConfig = [
170 'type' => 'text',
171 'enableRichtext' => true,
172 ];
173 $pageTsConfig = [
174 'classes.' => [
175 'aClass' => 'aConfig',
176 ],
177 'default.' => [
178 'classes.' => [
179 'aClass' => 'anotherConfig',
180 ],
181 'editor.' => [
182 'config.' => [
183 'contentsCss' => 'my.css'
184 ]
185 ],
186 ],
187 ];
188 $expected = [
189 'classes.' => [
190 'aClass' => 'anotherConfig',
191 ],
192 'editor.' => [
193 'config.' => [
194 'contentsCss' => 'my.css'
195 ]
196 ],
197 'classes' => [
198 'aClass' => 'anotherConfig',
199 ],
200 'editor' => [
201 'config' => [
202 'contentsCss' => 'my.css'
203 ]
204 ],
205 'proc.' => [
206 'overruleMode' => 'default',
207 ],
208 ];
209 // Accessible mock to $subject since getRtePageTsConfigOfPid calls BackendUtility::getPagesTSconfig()
210 // which can't be mocked in a sane way
211 $subject = $this->getAccessibleMock(Richtext::class, ['getRtePageTsConfigOfPid'], [], '', false);
212 $subject->expects($this->once())->method('getRtePageTsConfigOfPid')->with(42)->willReturn($pageTsConfig);
213 $output = $subject->getConfiguration('aTable', 'aField', 42, 'textmedia', $fieldConfig);
214 $this->assertSame($expected, $output);
215 }
216
217 /**
218 * @test
219 */
220 public function getConfigurationOverridesByFieldSpecificConfig()
221 {
222 $fieldConfig = [
223 'type' => 'text',
224 'enableRichtext' => true,
225 ];
226 $pageTsConfig = [
227 'classes.' => [
228 'aClass' => 'aConfig',
229 ],
230 'default.' => [
231 'classes.' => [
232 'aClass' => 'anotherConfig',
233 ],
234 ],
235 'config.' => [
236 'aTable.' => [
237 'aField.' => [
238 'classes.' => [
239 'aClass' => 'aThirdConfig',
240 ],
241 'editor.' => [
242 'config.' => [
243 'contentsCss' => 'my.css'
244 ]
245 ],
246 ],
247 ],
248 ],
249 ];
250 $expected = [
251 // Config with pagets dots
252 'classes.' => [
253 'aClass' => 'aThirdConfig',
254 ],
255 'editor.' => [
256 'config.' => [
257 'contentsCss' => 'my.css'
258 ]
259 ],
260 // Config without pagets dots
261 'classes' => [
262 'aClass' => 'aThirdConfig',
263 ],
264 'editor' => [
265 'config' => [
266 'contentsCss' => 'my.css'
267 ]
268 ],
269 'proc.' => [
270 'overruleMode' => 'default',
271 ],
272 ];
273 // Accessible mock to $subject since getRtePageTsConfigOfPid calls BackendUtility::getPagesTSconfig()
274 // which can't be mocked in a sane way
275 $subject = $this->getAccessibleMock(Richtext::class, ['getRtePageTsConfigOfPid'], [], '', false);
276 $subject->expects($this->once())->method('getRtePageTsConfigOfPid')->with(42)->willReturn($pageTsConfig);
277 $output = $subject->getConfiguration('aTable', 'aField', 42, 'textmedia', $fieldConfig);
278 $this->assertSame($expected, $output);
279 }
280
281 /**
282 * @test
283 */
284 public function getConfigurationOverridesByFieldAndTypeSpecificConfig()
285 {
286 $fieldConfig = [
287 'type' => 'text',
288 'enableRichtext' => true,
289 ];
290 $pageTsConfig = [
291 'classes.' => [
292 'aClass' => 'aConfig',
293 ],
294 'default.' => [
295 'classes.' => [
296 'aClass' => 'anotherConfig',
297 ],
298 ],
299 'config.' => [
300 'aTable.' => [
301 'aField.' => [
302 'classes.' => [
303 'aClass' => 'aThirdConfig',
304 ],
305 'editor.' => [
306 'config.' => [
307 'contentsCss' => 'my.css'
308 ]
309 ],
310 'types.' => [
311 'textmedia.' => [
312 'classes.' => [
313 'aClass' => 'aTypeSpecifcConfig',
314 ],
315 'editor.' => [
316 'config.' => [
317 'contentsCss' => 'your.css'
318 ]
319 ],
320 ]
321 ]
322 ],
323 ],
324 ],
325 ];
326 $expected = [
327 // Config with pagets dots
328 'classes.' => [
329 'aClass' => 'aTypeSpecifcConfig',
330 ],
331 'editor.' => [
332 'config.' => [
333 'contentsCss' => 'your.css'
334 ]
335 ],
336 // Config without pagets dots
337 'classes' => [
338 'aClass' => 'aTypeSpecifcConfig',
339 ],
340 'editor' => [
341 'config' => [
342 'contentsCss' => 'your.css'
343 ]
344 ],
345 'proc.' => [
346 'overruleMode' => 'default',
347 ],
348 ];
349 // Accessible mock to $subject since getRtePageTsConfigOfPid calls BackendUtility::getPagesTSconfig()
350 // which can't be mocked in a sane way
351 $subject = $this->getAccessibleMock(Richtext::class, ['getRtePageTsConfigOfPid'], [], '', false);
352 $subject->expects($this->once())->method('getRtePageTsConfigOfPid')->with(42)->willReturn($pageTsConfig);
353 $output = $subject->getConfiguration('aTable', 'aField', 42, 'textmedia', $fieldConfig);
354 $this->assertSame($expected, $output);
355 }
356
357 /**
358 * @test
359 */
360 public function getConfigurationPageTsOverridesPreset()
361 {
362 $pageId = 42;
363 $presetKey = 'default';
364
365 $preset = [
366 'editor' => [
367 'config' => [
368 'width' => 100
369 ],
370 ],
371 ];
372
373 $pageTsConfigArray = [
374 'preset' => $presetKey,
375 'editor.' => [
376 'config.' => [
377 'width' => 200
378 ],
379 ],
380 ];
381
382 $subject = $this->getAccessibleMock(
383 Richtext::class,
384 ['loadConfigurationFromPreset', 'getRtePageTsConfigOfPid'],
385 [],
386 '',
387 false
388 );
389 $subject->expects($this->once())->method('loadConfigurationFromPreset')->with($presetKey)->willReturn($preset);
390 $subject->expects($this->once())->method('getRtePageTsConfigOfPid')->with($pageId)->willReturn($pageTsConfigArray);
391
392 $output = $subject->getConfiguration('tt_content', 'bodytext', $pageId, 'textmedia', $pageTsConfigArray);
393
394 $expected = [
395 'editor' => [
396 'config' => [
397 'width' => 200
398 ],
399 ],
400 'preset' => 'default',
401 'editor.' => [
402 'config.' => [
403 'width' => 200
404 ],
405 ],
406 'proc.' => [
407 'overruleMode' => 'default',
408 ],
409 ];
410
411 $this->assertSame($expected, $output);
412 }
413 }