‪TYPO3CMS  ‪main
FrontendConfigurationManagerTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
22 use Symfony\Component\DependencyInjection\Container;
30 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
31 
32 final class ‪FrontendConfigurationManagerTest extends FunctionalTestCase
33 {
34  #[Test]
36  {
37  $typoScript = [
38  'plugin.' => [
39  'tx_foo.' => [
40  'settings.' => [
41  'foo' => 'bar',
42  ],
43  ],
44  ],
45  ];
46  $flexForm = '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
47 <T3FlexForms>
48  <data>
49  <sheet index="sDEF">
50  <language index="lDEF">
51  <field index="settings.foo">
52  <value index="vDEF">0</value>
53  </field>
54  </language>
55  </sheet>
56  </data>
57 </T3FlexForms>';
58 
59  $frontendTypoScript = new ‪FrontendTypoScript(new ‪RootNode(), [], [], []);
60  $frontendTypoScript->setSetupArray($typoScript);
61  ‪$GLOBALS['TYPO3_REQUEST'] = (new ‪ServerRequest())->withAttribute('frontend.typoscript', $frontendTypoScript);
62 
64  $container = $this->get('service_container');
65  $container->set('foo-flexform-listener', static function (‪BeforeFlexFormConfigurationOverrideEvent $event) {
67  'settings' => [
68  'foo' => 'from eventlistener',
69  ],
70  ]);
71  });
72  $eventListener = $container->get(ListenerProvider::class);
73  $eventListener->addListener(BeforeFlexFormConfigurationOverrideEvent::class, 'foo-flexform-listener');
74 
75  $contentObject = new ‪ContentObjectRenderer();
76  $contentObject->data = ['pi_flexform' => $flexForm];
77  $request = (new ‪ServerRequest())->withAttribute('currentContentObject', $contentObject);
78  $frontendTypoScript = new ‪FrontendTypoScript(new ‪RootNode(), [], [], []);
79  $frontendTypoScript->setSetupArray([]);
80  $request = $request->withAttribute('frontend.typoscript', $frontendTypoScript);
81  $frontendConfigurationManager = $this->get(FrontendConfigurationManager::class);
82  $frontendConfigurationManager->setRequest($request);
83  $frontendConfigurationManager->setConfiguration(['extensionName' => 'foo']);
84 
85  self::assertSame('from eventlistener', $frontendConfigurationManager->getConfiguration('foo')['settings']['foo']);
86  }
87 
89  {
90  yield 'no flexForm override configuration' => [
91  'flexForm' => '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
92 <T3FlexForms>
93  <data>
94  <sheet index="sDEF">
95  <language index="lDEF">
96  <field index="settings.booleanField">
97  <value index="vDEF">1</value>
98  </field>
99  </language>
100  </sheet>
101  </data>
102 </T3FlexForms>',
103  'typoScript' => [
104  'plugin.' => [
105  'tx_foo.' => [
106  'settings.' => [
107  'booleanField' => '0',
108  ],
109  ],
110  'tx_foo_foo.' => [
111  'settings.' => [
112  'booleanField' => '0',
113  ],
114  ],
115  ],
116  ],
117  'expected' => [
118  'booleanField' => '1',
119  ],
120  ];
121 
122  yield 'flexForm override configuration empty' => [
123  'flexForm' => '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
124 <T3FlexForms>
125  <data>
126  <sheet index="sDEF">
127  <language index="lDEF">
128  <field index="settings.booleanField">
129  <value index="vDEF">1</value>
130  </field>
131  </language>
132  </sheet>
133  </data>
134 </T3FlexForms>',
135  'typoScript' => [
136  'plugin.' => [
137  'tx_foo.' => [
138  'ignoreFlexFormSettingsIfEmpty' => '',
139  'settings.' => [
140  'booleanField' => '0',
141  ],
142  ],
143  'tx_foo_foo.' => [
144  'settings.' => [
145  'booleanField' => '0',
146  ],
147  ],
148  ],
149  ],
150  'expected' => [
151  'booleanField' => '1',
152  ],
153  ];
154 
155  yield 'flexForm override configuration for empty boolean field' => [
156  'flexForm' => '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
157 <T3FlexForms>
158  <data>
159  <sheet index="sDEF">
160  <language index="lDEF">
161  <field index="settings.booleanField">
162  <value index="vDEF">0</value>
163  </field>
164  </language>
165  </sheet>
166  </data>
167 </T3FlexForms>',
168  'typoScript' => [
169  'plugin.' => [
170  'tx_foo.' => [
171  'ignoreFlexFormSettingsIfEmpty' => 'booleanField',
172  'settings.' => [
173  'booleanField' => '1',
174  ],
175  ],
176  ],
177  ],
178  'expected' => [
179  'booleanField' => '1',
180  ],
181  ];
182 
183  yield 'flexForm override configuration for empty boolean field and plugin configuration' => [
184  'flexForm' => '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
185 <T3FlexForms>
186  <data>
187  <sheet index="sDEF">
188  <language index="lDEF">
189  <field index="settings.booleanField">
190  <value index="vDEF">0</value>
191  </field>
192  </language>
193  </sheet>
194  </data>
195 </T3FlexForms>',
196  'typoScript' => [
197  'plugin.' => [
198  'tx_foo.' => [
199  'settings.' => [
200  'booleanField' => '0',
201  ],
202  ],
203  'tx_foo_foo.' => [
204  'ignoreFlexFormSettingsIfEmpty' => 'booleanField',
205  'settings.' => [
206  'booleanField' => '1',
207  ],
208  ],
209  ],
210  ],
211  'expected' => [
212  'booleanField' => '1',
213  ],
214  ];
215 
216  yield 'flexForm override configuration for empty string field' => [
217  'flexForm' => '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
218 <T3FlexForms>
219  <data>
220  <sheet index="sDEF">
221  <language index="lDEF">
222  <field index="settings.stringField">
223  <value index="vDEF"></value>
224  </field>
225  </language>
226  </sheet>
227  </data>
228 </T3FlexForms>',
229  'typoScript' => [
230  'plugin.' => [
231  'tx_foo.' => [
232  'ignoreFlexFormSettingsIfEmpty' => 'stringField',
233  'settings.' => [
234  'stringField' => 'default value',
235  ],
236  ],
237  ],
238  ],
239  'expected' => [
240  'stringField' => 'default value',
241  ],
242  ];
243 
244  yield 'flexForm override configuration for empty string field and plugin configuration' => [
245  'flexForm' => '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
246 <T3FlexForms>
247  <data>
248  <sheet index="sDEF">
249  <language index="lDEF">
250  <field index="settings.stringField">
251  <value index="vDEF"></value>
252  </field>
253  </language>
254  </sheet>
255  </data>
256 </T3FlexForms>',
257  'typoScript' => [
258  'plugin.' => [
259  'tx_foo.' => [
260  'ignoreFlexFormSettingsIfEmpty' => 'stringField',
261  'settings.' => [
262  'stringField' => '',
263  ],
264  ],
265  'tx_foo_foo.' => [
266  'settings.' => [
267  'stringField' => 'default value',
268  ],
269  ],
270  ],
271  ],
272  'expected' => [
273  'stringField' => 'default value',
274  ],
275  ];
276 
277  yield 'flexForm override configuration for empty string field in sub array' => [
278  'flexForm' => '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
279 <T3FlexForms>
280  <data>
281  <sheet index="sDEF">
282  <language index="lDEF">
283  <field index="settings.subarray.stringField">
284  <value index="vDEF"></value>
285  </field>
286  </language>
287  </sheet>
288  </data>
289 </T3FlexForms>',
290  'typoScript' => [
291  'plugin.' => [
292  'tx_foo.' => [
293  'ignoreFlexFormSettingsIfEmpty' => 'subarray.stringField',
294  'settings.' => [
295  'subarray.' => [
296  'stringField' => 'default value',
297  ],
298  ],
299  ],
300  ],
301  ],
302  'expected' => [
303  'subarray' => [
304  'stringField' => 'default value',
305  ],
306  ],
307  ];
308 
309  yield 'flexForm override configuration for empty string field in sub array and plugin configuration' => [
310  'flexForm' => '<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
311 <T3FlexForms>
312  <data>
313  <sheet index="sDEF">
314  <language index="lDEF">
315  <field index="settings.subarray.stringField">
316  <value index="vDEF"></value>
317  </field>
318  </language>
319  </sheet>
320  </data>
321 </T3FlexForms>',
322  'typoScript' => [
323  'plugin.' => [
324  'tx_foo.' => [
325  'settings.' => [
326  'subarray.' => [
327  'stringField' => '',
328  ],
329  ],
330  ],
331  'tx_foo_foo.' => [
332  'ignoreFlexFormSettingsIfEmpty' => 'subarray.stringField',
333  'settings.' => [
334  'subarray.' => [
335  'stringField' => 'default value',
336  ],
337  ],
338  ],
339  ],
340  ],
341  'expected' => [
342  'subarray' => [
343  'stringField' => 'default value',
344  ],
345  ],
346  ];
347  }
348 
349  #[DataProvider('overrideConfigurationFromFlexFormSettingsDataProvider')]
350  #[Test]
352  string $flexForm,
353  array $typoScript,
354  array $expected
355  ): void {
356  $contentObject = new ‪ContentObjectRenderer();
357  $contentObject->data = ['pi_flexform' => $flexForm];
358  $request = (new ‪ServerRequest())->withAttribute('currentContentObject', $contentObject);
359  $frontendTypoScript = new ‪FrontendTypoScript(new ‪RootNode(), [], [], []);
360  $frontendTypoScript->setSetupArray($typoScript);
361  $request = $request->withAttribute('frontend.typoscript', $frontendTypoScript);
362  $frontendConfigurationManager = $this->get(FrontendConfigurationManager::class);
363  $frontendConfigurationManager->setRequest($request);
364  $frontendConfigurationManager->setConfiguration(['extensionName' => 'foo', 'pluginName' => 'foo']);
365 
366  self::assertSame($expected, $frontendConfigurationManager->getConfiguration('foo', 'foo')['settings']);
367  }
368 }
‪TYPO3\CMS\Extbase\Tests\Functional\Configuration\FrontendConfigurationManagerTest\overrideConfigurationFromFlexFormIgnoresConfiguredEmptyFlexFormSettings
‪overrideConfigurationFromFlexFormIgnoresConfiguredEmptyFlexFormSettings(string $flexForm, array $typoScript, array $expected)
Definition: FrontendConfigurationManagerTest.php:351
‪TYPO3\CMS\Extbase\Event\Configuration\BeforeFlexFormConfigurationOverrideEvent\setFlexFormConfiguration
‪setFlexFormConfiguration(array $flexFormConfiguration)
Definition: BeforeFlexFormConfigurationOverrideEvent.php:50
‪TYPO3\CMS\Extbase\Tests\Functional\Configuration\FrontendConfigurationManagerTest
Definition: FrontendConfigurationManagerTest.php:33
‪TYPO3\CMS\Extbase\Tests\Functional\Configuration\FrontendConfigurationManagerTest\beforeFlexFormConfigurationOverrideEventIsDispatched
‪beforeFlexFormConfigurationOverrideEventIsDispatched()
Definition: FrontendConfigurationManagerTest.php:35
‪TYPO3\CMS\Extbase\Configuration\FrontendConfigurationManager
Definition: FrontendConfigurationManager.php:38
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Extbase\Event\Configuration\BeforeFlexFormConfigurationOverrideEvent
Definition: BeforeFlexFormConfigurationOverrideEvent.php:28
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\TypoScript\AST\Node\RootNode
Definition: RootNode.php:26
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:102
‪TYPO3\CMS\Core\TypoScript\FrontendTypoScript
Definition: FrontendTypoScript.php:30
‪TYPO3\CMS\Extbase\Tests\Functional\Configuration
Definition: BackendConfigurationManagerTest.php:18
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider
Definition: ListenerProvider.php:30
‪TYPO3\CMS\Extbase\Tests\Functional\Configuration\FrontendConfigurationManagerTest\overrideConfigurationFromFlexFormSettingsDataProvider
‪static overrideConfigurationFromFlexFormSettingsDataProvider()
Definition: FrontendConfigurationManagerTest.php:88