aa33f232e0f1cfdd076a569155bf6fb7b76681ac
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Domain / Finishers / AbstractFinisherTest.php
1 <?php
2 namespace TYPO3\CMS\Form\Tests\Unit\Domain\Finishers;
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
17 use Prophecy\Argument;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Extbase\Object\ObjectManager;
20 use TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher;
21 use TYPO3\CMS\Form\Domain\Finishers\FinisherContext;
22 use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
23 use TYPO3\CMS\Form\Service\TranslationService;
24
25 /**
26 * Test case
27 */
28 class AbstractFinisherTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
29 {
30 /**
31 * @var array A backup of registered singleton instances
32 */
33 protected $singletonInstances = [];
34
35 /**
36 * Set up
37 */
38 public function setUp()
39 {
40 $this->singletonInstances = GeneralUtility::getSingletonInstances();
41 }
42
43 /**
44 * Tear down
45 */
46 public function tearDown()
47 {
48 GeneralUtility::resetSingletonInstances($this->singletonInstances);
49 parent::tearDown();
50 }
51
52 /**
53 * @test
54 */
55 public function parseOptionReturnsNullIfOptionNameIsTranslation()
56 {
57 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
58 AbstractFinisher::class,
59 [],
60 '',
61 false
62 );
63
64 $this->assertNull($mockAbstractFinisher->_call('parseOption', 'translation'));
65 }
66
67 /**
68 * @test
69 */
70 public function parseOptionReturnsNullIfOptionNameNotExistsWithinOptions()
71 {
72 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
73 AbstractFinisher::class,
74 [],
75 '',
76 false
77 );
78
79 $mockAbstractFinisher->_set('options', []);
80
81 $this->assertNull($mockAbstractFinisher->_call('parseOption', 'foo'));
82 }
83
84 /**
85 * @test
86 */
87 public function parseOptionReturnsNullIfOptionNameNotExistsWithinDefaultOptions()
88 {
89 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
90 AbstractFinisher::class,
91 [],
92 '',
93 false
94 );
95
96 $mockAbstractFinisher->_set('options', []);
97
98 $this->assertNull($mockAbstractFinisher->_call('parseOption', 'foo'));
99 }
100
101 /**
102 * @test
103 */
104 public function parseOptionReturnsArrayOptionValuesAsArray()
105 {
106 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
107 AbstractFinisher::class,
108 [],
109 '',
110 false
111 );
112
113 $mockAbstractFinisher->_set('options', [
114 'foo' => ['bar', 'foobar']
115 ]);
116
117 $expected = ['bar', 'foobar'];
118
119 $this->assertSame($expected, $mockAbstractFinisher->_call('parseOption', 'foo'));
120 }
121
122 /**
123 * @test
124 */
125 public function parseOptionReturnsValueFromFormRuntimeIfOptionNameReferenceAFormElementIdentifierWhoseValueIsAString()
126 {
127 $objectMangerProphecy = $this->prophesize(ObjectManager::class);
128 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectMangerProphecy->reveal());
129
130 $mockTranslationService = $this->getAccessibleMock(TranslationService::class, [
131 'translateFinisherOption'
132 ], [], '', false);
133
134 $mockTranslationService
135 ->expects($this->any())
136 ->method('translateFinisherOption')
137 ->willReturnArgument(3);
138
139 $objectMangerProphecy
140 ->get(TranslationService::class)
141 ->willReturn($mockTranslationService);
142
143 $expected = 'element-value';
144 $elementIdentifier = 'element-identifier-1';
145
146 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
147 AbstractFinisher::class,
148 [],
149 '',
150 false
151 );
152
153 $mockAbstractFinisher->_set('options', [
154 'subject' => '{' . $elementIdentifier . '}'
155 ]);
156
157 $finisherContextProphecy = $this->prophesize(FinisherContext::class);
158
159 $formRuntimeProphecy = $this->prophesize(FormRuntime::class);
160 $formRuntimeProphecy->offsetExists(Argument::exact($elementIdentifier))->willReturn(true);
161 $formRuntimeProphecy->offsetGet(Argument::exact($elementIdentifier))->willReturn($expected);
162
163 $finisherContextProphecy->getFormRuntime(Argument::cetera())
164 ->willReturn($formRuntimeProphecy->reveal());
165
166 $mockAbstractFinisher->_set('finisherContext', $finisherContextProphecy->reveal());
167
168 $this->assertSame($expected, $mockAbstractFinisher->_call('parseOption', 'subject'));
169 }
170
171 /**
172 * @test
173 */
174 public function parseOptionReturnsNoReplacedValueFromFormRuntimeIfOptionNameReferenceAFormElementIdentifierWhoseValueIsNotAString()
175 {
176 $objectMangerProphecy = $this->prophesize(ObjectManager::class);
177 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectMangerProphecy->reveal());
178
179 $mockTranslationService = $this->getAccessibleMock(TranslationService::class, [
180 'translateFinisherOption'
181 ], [], '', false);
182
183 $mockTranslationService
184 ->expects($this->any())
185 ->method('translateFinisherOption')
186 ->willReturnArgument(3);
187
188 $objectMangerProphecy
189 ->get(TranslationService::class)
190 ->willReturn($mockTranslationService);
191
192 $elementIdentifier = 'element-identifier-1';
193 $expected = '{' . $elementIdentifier . '}';
194
195 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
196 AbstractFinisher::class,
197 [],
198 '',
199 false
200 );
201
202 $mockAbstractFinisher->_set('options', [
203 'subject' => '{' . $elementIdentifier . '}'
204 ]);
205
206 $finisherContextProphecy = $this->prophesize(FinisherContext::class);
207
208 $formRuntimeProphecy = $this->prophesize(FormRuntime::class);
209 $formRuntimeProphecy->offsetExists(Argument::exact($elementIdentifier))->willReturn(true);
210 $formElementValue = new \DateTime;
211 $formRuntimeProphecy->offsetGet(Argument::exact($elementIdentifier))->willReturn($formElementValue);
212
213 $finisherContextProphecy->getFormRuntime(Argument::cetera())
214 ->willReturn($formRuntimeProphecy->reveal());
215
216 $mockAbstractFinisher->_set('finisherContext', $finisherContextProphecy->reveal());
217
218 $this->assertSame($expected, $mockAbstractFinisher->_call('parseOption', 'subject'));
219 }
220
221 /**
222 * @test
223 */
224 public function parseOptionReturnsNoReplacedValueFromFormRuntimeIfOptionNameReferenceANonExistingFormElement()
225 {
226 $objectMangerProphecy = $this->prophesize(ObjectManager::class);
227 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectMangerProphecy->reveal());
228
229 $mockTranslationService = $this->getAccessibleMock(TranslationService::class, [
230 'translateFinisherOption'
231 ], [], '', false);
232
233 $mockTranslationService
234 ->expects($this->any())
235 ->method('translateFinisherOption')
236 ->willReturnArgument(3);
237
238 $objectMangerProphecy
239 ->get(TranslationService::class)
240 ->willReturn($mockTranslationService);
241
242 $elementIdentifier = 'element-identifier-1';
243
244 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
245 AbstractFinisher::class,
246 [],
247 '',
248 false
249 );
250
251 $mockAbstractFinisher->_set('options', [
252 'subject' => '{' . $elementIdentifier . '}'
253 ]);
254
255 $finisherContextProphecy = $this->prophesize(FinisherContext::class);
256
257 $formRuntimeProphecy = $this->prophesize(FormRuntime::class);
258 $formRuntimeProphecy->offsetExists(Argument::cetera())->willReturn(true);
259 $formRuntimeProphecy->offsetGet(Argument::cetera())->willReturn(false);
260
261 $finisherContextProphecy->getFormRuntime(Argument::cetera())
262 ->willReturn($formRuntimeProphecy->reveal());
263
264 $mockAbstractFinisher->_set('finisherContext', $finisherContextProphecy->reveal());
265
266 $expected = '{' . $elementIdentifier . '}';
267 $this->assertSame($expected, $mockAbstractFinisher->_call('parseOption', 'subject'));
268 }
269
270 /**
271 * @test
272 */
273 public function parseOptionReturnsDefaultOptionValueIfOptionNameNotExistsWithinOptionsButWithinDefaultOptions()
274 {
275 $objectMangerProphecy = $this->prophesize(ObjectManager::class);
276 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectMangerProphecy->reveal());
277
278 $mockTranslationService = $this->getAccessibleMock(TranslationService::class, [
279 'translateFinisherOption'
280 ], [], '', false);
281
282 $mockTranslationService
283 ->expects($this->any())
284 ->method('translateFinisherOption')
285 ->willReturnArgument(3);
286
287 $objectMangerProphecy
288 ->get(TranslationService::class)
289 ->willReturn($mockTranslationService);
290
291 $expected = 'defaultValue';
292
293 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
294 AbstractFinisher::class,
295 [],
296 '',
297 false
298 );
299
300 $mockAbstractFinisher->_set('options', []);
301 $mockAbstractFinisher->_set('defaultOptions', [
302 'subject' => $expected
303 ]);
304
305 $finisherContextProphecy = $this->prophesize(FinisherContext::class);
306
307 $formRuntimeProphecy = $this->prophesize(FormRuntime::class);
308 $formRuntimeProphecy->offsetExists(Argument::cetera())->willReturn(true);
309 $formRuntimeProphecy->offsetGet(Argument::cetera())->willReturn(false);
310
311 $finisherContextProphecy->getFormRuntime(Argument::cetera())
312 ->willReturn($formRuntimeProphecy->reveal());
313
314 $mockAbstractFinisher->_set('finisherContext', $finisherContextProphecy->reveal());
315
316 $this->assertSame($expected, $mockAbstractFinisher->_call('parseOption', 'subject'));
317 }
318
319 /**
320 * @test
321 */
322 public function parseOptionReturnsDefaultOptionValueIfOptionValueIsAFormElementReferenceAndTheFormElementValueIsEmpty()
323 {
324 $objectMangerProphecy = $this->prophesize(ObjectManager::class);
325 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectMangerProphecy->reveal());
326
327 $mockTranslationService = $this->getAccessibleMock(TranslationService::class, [
328 'translateFinisherOption'
329 ], [], '', false);
330
331 $mockTranslationService
332 ->expects($this->any())
333 ->method('translateFinisherOption')
334 ->willReturnArgument(3);
335
336 $objectMangerProphecy
337 ->get(TranslationService::class)
338 ->willReturn($mockTranslationService);
339
340 $elementIdentifier = 'element-identifier-1';
341 $expected = 'defaultValue';
342
343 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
344 AbstractFinisher::class,
345 [],
346 '',
347 false
348 );
349
350 $mockAbstractFinisher->_set('options', [
351 'subject' => '{' . $elementIdentifier . '}'
352 ]);
353 $mockAbstractFinisher->_set('defaultOptions', [
354 'subject' => $expected
355 ]);
356
357 $finisherContextProphecy = $this->prophesize(FinisherContext::class);
358
359 $formRuntimeProphecy = $this->prophesize(FormRuntime::class);
360 $formRuntimeProphecy->offsetExists(Argument::exact($elementIdentifier))->willReturn(true);
361 $formRuntimeProphecy->offsetGet(Argument::exact($elementIdentifier))->willReturn('');
362
363 $finisherContextProphecy->getFormRuntime(Argument::cetera())
364 ->willReturn($formRuntimeProphecy->reveal());
365
366 $mockAbstractFinisher->_set('finisherContext', $finisherContextProphecy->reveal());
367
368 $this->assertSame($expected, $mockAbstractFinisher->_call('parseOption', 'subject'));
369 }
370
371 /**
372 * @test
373 */
374 public function parseOptionReturnsTimestampIfOptionValueIsATimestampRequestTrigger()
375 {
376 $objectMangerProphecy = $this->prophesize(ObjectManager::class);
377 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectMangerProphecy->reveal());
378
379 $mockTranslationService = $this->getAccessibleMock(TranslationService::class, [
380 'translateFinisherOption'
381 ], [], '', false);
382
383 $mockTranslationService
384 ->expects($this->any())
385 ->method('translateFinisherOption')
386 ->willReturnArgument(3);
387
388 $objectMangerProphecy
389 ->get(TranslationService::class)
390 ->willReturn($mockTranslationService);
391
392 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
393 AbstractFinisher::class,
394 [],
395 '',
396 false
397 );
398
399 $mockAbstractFinisher->_set('options', [
400 'crdate' => '{__currentTimestamp}'
401 ]);
402
403 $finisherContextProphecy = $this->prophesize(FinisherContext::class);
404
405 $formRuntimeProphecy = $this->prophesize(FormRuntime::class);
406
407 $finisherContextProphecy->getFormRuntime(Argument::cetera())
408 ->willReturn($formRuntimeProphecy->reveal());
409
410 $mockAbstractFinisher->_set('finisherContext', $finisherContextProphecy->reveal());
411
412 $expected = '#^([0-9]{10})$#';
413 $this->assertEquals(1, preg_match($expected, $mockAbstractFinisher->_call('parseOption', 'crdate')));
414 }
415 }