‪TYPO3CMS  11.5
FlexFormProcessorTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
7 /*
8  * This file is part of the TYPO3 CMS project.
9  *
10  * It is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License, either version 2
12  * of the License, or any later version.
13  *
14  * For the full copyright and license information, please read the
15  * LICENSE.txt file that was distributed with this source code.
16  *
17  * The TYPO3 project - inspiring people to share!
18  */
19 
20 use Prophecy\PhpUnit\ProphecyTrait;
21 use Prophecy\Prophecy\ObjectProphecy;
25 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
32 class ‪FlexFormProcessorTest extends UnitTestCase
33 {
34  use ProphecyTrait;
35 
37  protected ObjectProphecy ‪$contentObjectRenderer;
38 
39  protected function ‪setUp(): void
40  {
41  parent::setUp();
42  $this->resetSingletonInstances = true;
43  $this->contentObjectRenderer = $this->prophesize(ContentObjectRenderer::class);
45  }
46 
51  {
52  $processorConfiguration = ['as' => 'myOutputVariable', 'fieldName' => 'non_existing_field'];
53  $this->contentObjectRenderer
54  ->stdWrapValue('fieldName', $processorConfiguration, 'pi_flexform')
55  ->willReturn('non_existing_field');
56  $this->contentObjectRenderer
57  ->stdWrapValue('as', $processorConfiguration, 'flexFormData')
58  ->willReturn('myOutputVariable');
59 
60  $processedData = [
61  'data' => [
62  'pi_flexform' => $this->‪getFlexFormStructure(),
63  ],
64  ];
65 
66  $subject = new ‪FlexFormProcessor();
67  $expected = $subject->process(
68  $this->contentObjectRenderer->reveal(),
69  [],
70  $processorConfiguration,
71  $processedData
72  );
73 
74  self::assertSame($expected, $processedData);
75  }
76 
81  {
82  $processorConfiguration = ['as' => 'myOutputVariable', 'fieldName' => 'custom_field'];
83  $this->contentObjectRenderer
84  ->stdWrapValue('fieldName', $processorConfiguration, 'pi_flexform')
85  ->willReturn('custom_field');
86  $this->contentObjectRenderer
87  ->stdWrapValue('as', $processorConfiguration, 'flexFormData')
88  ->willReturn('myOutputVariable');
89 
90  $processedData = [
91  'data' => [
92  'custom_field' => 123456789,
93  ],
94  ];
95 
96  $subject = new FlexFormProcessor();
97  $expected = $subject->process(
98  $this->contentObjectRenderer->reveal(),
99  [],
100  $processorConfiguration,
101  $processedData
102  );
103 
104  self::assertSame($expected, $processedData);
105  }
106 
111  {
112  $processorConfiguration = ['as' => 'myCustomVar'];
113  $this->contentObjectRenderer
114  ->stdWrapValue('fieldName', $processorConfiguration, 'pi_flexform')
115  ->willReturn('pi_flexform');
116  $this->contentObjectRenderer
117  ->stdWrapValue('as', $processorConfiguration, 'flexFormData')
118  ->willReturn('myCustomVar');
119 
120  $processedData = [
121  'data' => [
122  'pi_flexform' => $this->‪getFlexFormStructure(),
123  ],
124  ];
125 
126  $subject = new FlexFormProcessor();
127  $expected = $subject->process(
128  $this->contentObjectRenderer->reveal(),
129  [],
130  $processorConfiguration,
131  $processedData
132  );
133 
134  self::assertIsArray($expected['myCustomVar']);
135  }
136 
140  public function ‪defaultOutputVariableForProcessorWillBeUsed(): void
141  {
142  $processorConfiguration = [];
143  $this->contentObjectRenderer
144  ->stdWrapValue('fieldName', $processorConfiguration, 'pi_flexform')
145  ->willReturn('pi_flexform');
146  $this->contentObjectRenderer
147  ->stdWrapValue('as', $processorConfiguration, 'flexFormData')
148  ->willReturn('flexFormData');
149 
150  $processedData = [
151  'data' => [
152  'pi_flexform' => $this->‪getFlexFormStructure(),
153  ],
154  ];
155 
156  $subject = new FlexFormProcessor();
157  $expected = $subject->process(
158  $this->contentObjectRenderer->reveal(),
159  [],
160  $processorConfiguration,
161  $processedData
162  );
163 
164  self::assertSame($expected['data']['pi_flexform'], $processedData['data']['pi_flexform']);
165  self::assertIsArray($expected['flexFormData']);
166  }
167 
172  {
173  $processorConfiguration = ['as' => 'myOutputVariable', 'fieldName' => 'my_flexform'];
174  $this->contentObjectRenderer
175  ->stdWrapValue('fieldName', $processorConfiguration, 'pi_flexform')
176  ->willReturn('my_flexform');
177  $this->contentObjectRenderer
178  ->stdWrapValue('as', $processorConfiguration, 'flexFormData')
179  ->willReturn('myOutputVariable');
180 
181  $processedData = [
182  'data' => [
183  'my_flexform' => $this->‪getFlexFormStructure(),
184  ],
185  ];
186 
187  $subject = new FlexFormProcessor();
188  $expected = $subject->process(
189  $this->contentObjectRenderer->reveal(),
190  [],
191  $processorConfiguration,
192  $processedData
193  );
194 
195  self::assertIsArray($expected['myOutputVariable']);
196  }
197 
201  public function ‪subDataProcessorIsResolved(): void
202  {
204 
205  $processorConfiguration['dataProcessing.'] = [10 => 'Vendor\Acme\DataProcessing\FooProcessor'];
206 
207  $processedData = [
208  'data' => [
209  'pi_flexform' => $this->‪getFlexFormStructure(),
210  ],
211  ];
212 
213  $this->contentObjectRenderer
214  ->stdWrapValue('fieldName', $processorConfiguration, 'pi_flexform')
215  ->willReturn('pi_flexform');
216  $this->contentObjectRenderer
217  ->stdWrapValue('as', $processorConfiguration, 'flexFormData')
218  ->willReturn('flexFormData');
219  $convertedFlexFormData = [
220  'options' => [
221  'hotels' => 0,
222  'images' => '12',
223  ],
224  ];
225  $this->contentObjectRenderer->start([$convertedFlexFormData])->shouldBeCalled();
226 
227  $contentDataProcessor = $this->prophesize(ContentDataProcessor::class);
228  $renderedDataFromProcessors = [
229  'options' => [
230  'hotels' => 0,
231  'images' => 'img/foo.jpg',
232  ],
233  ];
234  $contentDataProcessor
235  ->process($this->contentObjectRenderer->reveal(), $processorConfiguration, $convertedFlexFormData)
236  ->willReturn($renderedDataFromProcessors);
237 
238  GeneralUtility::addInstance(ContentObjectRenderer::class, $this->contentObjectRenderer->reveal());
239  GeneralUtility::addInstance(ContentDataProcessor::class, $contentDataProcessor->reveal());
240 
241  $subject = new FlexFormProcessor();
242  $actual = $subject->process(
243  $this->contentObjectRenderer->reveal(),
244  [],
245  $processorConfiguration,
246  $processedData
247  );
248 
249  self::assertSame(array_merge($processedData, ['flexFormData' => $renderedDataFromProcessors]), $actual);
250  }
251 
252  private function ‪getFlexFormStructure(): string
253  {
254  return '<![CDATA[<?xml version="1.0" encoding="utf-8" standalone="yes" ?>'
255  . '<T3FlexForms>
256  <data>
257  <sheet index="options">
258  <language index="lDEF">
259  <field index="hotels">
260  <value index="vDEF">0</value>
261  </field>
262  </language>
263  </sheet>
264  </data>
265  </T3FlexForms>'
266  . ']]>';
267  }
268 
269  private function ‪prepareFlexFormService(): void
270  {
271  $convertedFlexFormData = [
272  'options' => [
273  'hotels' => 0,
274  ],
275  ];
276 
277  $flexFormService = $this->prophesize(FlexFormService::class);
278  $flexFormService->convertFlexFormContentToArray($this->‪getFlexFormStructure())->willReturn($convertedFlexFormData);
279  GeneralUtility::setSingletonInstance(FlexFormService::class, $flexFormService->reveal());
280  }
281 
283  {
284  $convertedFlexFormData = [
285  'options' => [
286  'hotels' => 0,
287  'images' => '12',
288  ],
289  ];
290 
291  $flexFormService = $this->prophesize(FlexFormService::class);
292  $flexFormService->convertFlexFormContentToArray($this->‪getFlexFormStructure())->willReturn($convertedFlexFormData);
293  GeneralUtility::setSingletonInstance(FlexFormService::class, $flexFormService->reveal());
294  }
295 }
‪TYPO3\CMS\Frontend\Tests\Unit\DataProcessing\FlexFormProcessorTest\subDataProcessorIsResolved
‪subDataProcessorIsResolved()
Definition: FlexFormProcessorTest.php:200
‪TYPO3\CMS\Frontend\ContentObject\ContentDataProcessor
Definition: ContentDataProcessor.php:26
‪TYPO3\CMS\Frontend\Tests\Unit\DataProcessing\FlexFormProcessorTest\setUp
‪setUp()
Definition: FlexFormProcessorTest.php:38
‪TYPO3\CMS\Frontend\Tests\Unit\DataProcessing\FlexFormProcessorTest\defaultOutputVariableForProcessorWillBeUsed
‪defaultOutputVariableForProcessorWillBeUsed()
Definition: FlexFormProcessorTest.php:139
‪TYPO3\CMS\Frontend\Tests\Unit\DataProcessing\FlexFormProcessorTest\prepareFlexFormService
‪prepareFlexFormService()
Definition: FlexFormProcessorTest.php:268
‪TYPO3\CMS\Frontend\Tests\Unit\DataProcessing\FlexFormProcessorTest
Definition: FlexFormProcessorTest.php:33
‪TYPO3\CMS\Frontend\Tests\Unit\DataProcessing\FlexFormProcessorTest\customFieldNameDoesNotContainFlexFormDataWillReturnUnchangedProcessedData
‪customFieldNameDoesNotContainFlexFormDataWillReturnUnchangedProcessedData()
Definition: FlexFormProcessorTest.php:79
‪TYPO3\CMS\Frontend\Tests\Unit\DataProcessing\FlexFormProcessorTest\prepareFlexFormServiceWithSubDataProcessorData
‪prepareFlexFormServiceWithSubDataProcessorData()
Definition: FlexFormProcessorTest.php:281
‪TYPO3\CMS\Frontend\DataProcessing\FlexFormProcessor
Definition: FlexFormProcessor.php:49
‪TYPO3\CMS\Frontend\Tests\Unit\DataProcessing\FlexFormProcessorTest\customOutputVariableForProcessorWillReturnParsedFlexFormToDataCustomVariable
‪customOutputVariableForProcessorWillReturnParsedFlexFormToDataCustomVariable()
Definition: FlexFormProcessorTest.php:109
‪TYPO3\CMS\Frontend\Tests\Unit\DataProcessing\FlexFormProcessorTest\customFieldNameDoesNotExistsWillReturnUnchangedProcessedData
‪customFieldNameDoesNotExistsWillReturnUnchangedProcessedData()
Definition: FlexFormProcessorTest.php:49
‪TYPO3\CMS\Core\Service\FlexFormService
Definition: FlexFormService.php:25
‪TYPO3\CMS\Frontend\Tests\Unit\DataProcessing\FlexFormProcessorTest\$contentObjectRenderer
‪ObjectProphecy $contentObjectRenderer
Definition: FlexFormProcessorTest.php:36
‪TYPO3\CMS\Frontend\Tests\Unit\DataProcessing\FlexFormProcessorTest\getFlexFormStructure
‪getFlexFormStructure()
Definition: FlexFormProcessorTest.php:251
‪TYPO3\CMS\Frontend\Tests\Unit\DataProcessing
Definition: FlexFormProcessorTest.php:5
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Frontend\Tests\Unit\DataProcessing\FlexFormProcessorTest\defaultConfigurationWithCustomFieldNameWillReturnParsedFlexFormToDefaultOutputVariable
‪defaultConfigurationWithCustomFieldNameWillReturnParsedFlexFormToDefaultOutputVariable()
Definition: FlexFormProcessorTest.php:170