[FEATURE] EXT:form - support translation arguments
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Service / TranslationServiceTest.php
1 <?php
2 namespace TYPO3\CMS\Form\Tests\Unit\Mvc\Service;
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 TYPO3\CMS\Core\Cache\CacheManager;
18 use TYPO3\CMS\Core\Localization\LanguageService;
19 use TYPO3\CMS\Core\Localization\LanguageStore;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
22 use TYPO3\CMS\Form\Domain\Model\FormElements\GenericFormElement;
23 use TYPO3\CMS\Form\Domain\Model\FormElements\Page;
24 use TYPO3\CMS\Form\Domain\Model\Renderable\RootRenderableInterface;
25 use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
26 use TYPO3\CMS\Form\Service\TranslationService;
27
28 /**
29 * Test case
30 */
31 class TranslationServiceTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
32 {
33
34 /**
35 * @var array A backup of registered singleton instances
36 */
37 protected $singletonInstances = [];
38
39 /**
40 * @var ConfigurationManager
41 */
42 protected $mockConfigurationManager;
43
44 /**
45 * @var TranslationService
46 */
47 protected $mockTranslationService;
48
49 /**
50 * @var LanguageStore
51 */
52 protected $store;
53
54 /**
55 * Set up
56 */
57 public function setUp()
58 {
59 $this->mockConfigurationManager = $this->getAccessibleMock(ConfigurationManager::class, [
60 'getConfiguration'
61 ], [], '', false);
62
63 $this->mockTranslationService = $this->getAccessibleMock(TranslationService::class, [
64 'getConfigurationManager',
65 'getLanguageService'
66 ], [], '', false);
67
68 $this->mockTranslationService
69 ->expects($this->any())
70 ->method('getLanguageService')
71 ->willReturn(GeneralUtility::makeInstance(LanguageService::class));
72
73 $this->mockTranslationService
74 ->expects($this->any())
75 ->method('getConfigurationManager')
76 ->willReturn($this->mockConfigurationManager);
77
78 GeneralUtility::makeInstance(CacheManager::class)->getCache('l10n')->flush();
79 $this->store = GeneralUtility::makeInstance(LanguageStore::class);
80 $this->store->initialize();
81
82 $this->singletonInstances = GeneralUtility::getSingletonInstances();
83 }
84
85 /**
86 * Tear down
87 */
88 public function tearDown()
89 {
90 GeneralUtility::resetSingletonInstances($this->singletonInstances);
91 parent::tearDown();
92 }
93
94 /**
95 * @test
96 */
97 public function translateReturnsExistingDefaultLanguageKeyIfFullExtDefaultLanguageKeyIsRequested()
98 {
99 $xlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
100 $expected = 'FORM EN';
101
102 $this->store->flushData($xlfPath);
103 $this->assertEquals($expected, $this->mockTranslationService->_call(
104 'translate',
105 $xlfPath . ':element.Page.renderingOptions.nextButtonLabel'
106 ));
107 }
108
109 /**
110 * @test
111 */
112 public function translateReturnsExistingDefaultLanguageKeyIfFullLLLExtDefaultLanguageKeyIsRequested()
113 {
114 $xlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
115 $expected = 'FORM EN';
116
117 $this->store->flushData($xlfPath);
118 $this->assertEquals($expected, $this->mockTranslationService->_call(
119 'translate',
120 'LLL:' . $xlfPath . ':element.Page.renderingOptions.nextButtonLabel'
121 ));
122 }
123
124 /**
125 * @test
126 */
127 public function translateReturnsExistingDefaultLanguageKeyIfDefaultLanguageKeyIsRequestedAndDefaultValueIsGiven()
128 {
129 $xlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
130 $expected = 'FORM EN';
131
132 $this->store->flushData($xlfPath);
133 $this->assertEquals($expected, $this->mockTranslationService->_call(
134 'translate',
135 $xlfPath . ':element.Page.renderingOptions.nextButtonLabel',
136 null,
137 null,
138 null,
139 'defaultValue'
140 ));
141 }
142
143 /**
144 * @test
145 */
146 public function translateReturnsEmptyStringIfNonExistingDefaultLanguageKeyIsRequested()
147 {
148 $xlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
149 $this->store->flushData($xlfPath);
150
151 $expected = '';
152 $this->assertEquals($expected, $this->mockTranslationService->_call(
153 'translate',
154 $xlfPath . ':element.Page.renderingOptions.nonExisting'
155 ));
156 }
157
158 /**
159 * @test
160 */
161 public function translateReturnsDefaultValueIfNonExistingDefaultLanguageKeyIsRequestedAndDefaultValueIsGiven()
162 {
163 $xlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
164 $this->store->flushData($xlfPath);
165
166 $expected = 'defaultValue';
167 $this->assertEquals($expected, $this->mockTranslationService->_call(
168 'translate',
169 $xlfPath . ':element.Page.renderingOptions.nonExisting',
170 null,
171 null,
172 null,
173 'defaultValue'
174 ));
175 }
176
177 /**
178 * @test
179 */
180 public function translateReturnsExistingLanguageKeyForLanguageIfExtPathLanguageKeyIsRequested()
181 {
182 $xlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
183 $expected = 'FORM DE';
184
185 $this->store->flushData($xlfPath);
186 $this->assertEquals($expected, $this->mockTranslationService->_call(
187 'translate',
188 $xlfPath . ':element.Page.renderingOptions.nextButtonLabel',
189 null,
190 null,
191 'de'
192 ));
193 }
194
195 /**
196 * @test
197 */
198 public function translateReturnsDefaultValueIfNonExistingLanguageKeyForLanguageIsRequestedAndDefaultValueIsGiven()
199 {
200 $xlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
201 $expected = 'defaultValue';
202
203 $this->store->flushData($xlfPath);
204 $this->assertEquals($expected, $this->mockTranslationService->_call(
205 'translate',
206 $xlfPath . ':element.Page.renderingOptions.nonExisting',
207 null,
208 null,
209 'de',
210 'defaultValue'
211 ));
212 }
213
214 /**
215 * @test
216 */
217 public function translateReturnsEmptyStringIfNonExistingLanguageKeyForLanguageIsRequested()
218 {
219 $xlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
220 $expected = '';
221
222 $this->store->flushData($xlfPath);
223 $this->assertEquals($expected, $this->mockTranslationService->_call(
224 'translate',
225 $xlfPath . ':element.Page.renderingOptions.nonExisting',
226 null,
227 null,
228 'de'
229 ));
230 }
231
232 /**
233 * @test
234 */
235 public function translateReturnsExistingDefaultLanguageKeyIfDefaultLanguageKeyIsRequestedAndExtFilePathIsGiven()
236 {
237 $xlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
238 $expected = 'FORM EN';
239
240 $this->store->flushData($xlfPath);
241 $this->assertEquals($expected, $this->mockTranslationService->_call(
242 'translate',
243 'element.Page.renderingOptions.nextButtonLabel',
244 null,
245 $xlfPath
246 ));
247 }
248
249 /**
250 * @test
251 */
252 public function translateReturnsExistingDefaultLanguageKeyIfDefaultLanguageKeyIsRequestedAndLLLExtFilePathIsGiven()
253 {
254 $xlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
255 $expected = 'FORM EN';
256
257 $this->store->flushData($xlfPath);
258 $this->assertEquals($expected, $this->mockTranslationService->_call(
259 'translate',
260 'element.Page.renderingOptions.nextButtonLabel',
261 null,
262 'LLL:' . $xlfPath
263 ));
264 }
265
266 /**
267 * @test
268 */
269 public function translateValuesRecursiveTranslateRecursive()
270 {
271 $xlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
272
273 $input = [
274 'Stan' => [
275 'Steve' => 'Roger'
276 ],
277 [
278 'Francine' => [
279 'Klaus' => 'element.Page.renderingOptions.nextButtonLabel'
280 ],
281 ],
282 ];
283
284 $expected = [
285 'Stan' => [
286 'Steve' => 'Roger'
287 ],
288 [
289 'Francine' => [
290 'Klaus' => 'FORM EN'
291 ],
292 ],
293 ];
294
295 $this->store->flushData($xlfPath);
296 $this->assertEquals($expected, $this->mockTranslationService->_call(
297 'translateValuesRecursive',
298 $input,
299 $xlfPath
300 ));
301 }
302
303 /**
304 * @test
305 */
306 public function translateFormElementValueTranslateLabelForConcreteFormAndConcreteElementIfElementRenderingOptionsContainsATranslationFileAndElementLabelIsNotEmptyAndPropertyShouldBeTranslatedAndTranslationExists()
307 {
308 $formRuntimeXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
309 $textElementXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_text.xlf';
310
311 $formRuntimeIdentifier = 'form-runtime-identifier';
312 $formElementIdentifier = 'form-element-identifier';
313
314 $formRuntimeRenderingOptions = [
315 'translation' => [
316 'translationFile' => $formRuntimeXlfPath,
317 'translatePropertyValueIfEmpty' => true
318 ],
319 ];
320
321 $formElementRenderingOptions = [
322 'translation' => [
323 'translationFile' => $textElementXlfPath,
324 'translatePropertyValueIfEmpty' => true
325 ],
326 ];
327
328 $expected = 'form-element-identifier LABEL EN';
329
330 $this->store->flushData($formRuntimeXlfPath);
331 $this->store->flushData($textElementXlfPath);
332
333 $mockFormElement = $this->getAccessibleMock(GenericFormElement::class, ['dummy'], [], '', false);
334
335 $mockFormElement->_set('type', 'Text');
336 $mockFormElement->_set('renderingOptions', $formElementRenderingOptions);
337 $mockFormElement->_set('identifier', $formElementIdentifier);
338 $mockFormElement->_set('label', 'some label');
339
340 $mockFormRuntime = $this->getAccessibleMock(FormRuntime::class, ['getIdentifier', 'getRenderingOptions'], [], '', false);
341 $mockFormRuntime->expects($this->any())->method('getIdentifier')->willReturn($formRuntimeIdentifier);
342 $mockFormRuntime->expects($this->any())->method('getRenderingOptions')->willReturn($formRuntimeRenderingOptions);
343
344 $this->assertEquals($expected, $this->mockTranslationService->_call('translateFormElementValue', $mockFormElement, ['label'], $mockFormRuntime));
345 }
346
347 /**
348 * @test
349 */
350 public function translateFormElementValueTranslateLabelForConcreteFormAndConcreteElementIfElementRenderingOptionsContainsATranslationFileAndElementLabelIsEmptyAndPropertyShouldBeTranslatedAndTranslationExists()
351 {
352 $formRuntimeXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
353 $textElementXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_text.xlf';
354
355 $formRuntimeIdentifier = 'form-runtime-identifier';
356 $formElementIdentifier = 'form-element-identifier';
357
358 $formRuntimeRenderingOptions = [
359 'translation' => [
360 'translationFile' => $formRuntimeXlfPath,
361 'translatePropertyValueIfEmpty' => true
362 ],
363 ];
364
365 $formElementRenderingOptions = [
366 'translation' => [
367 'translationFile' => $textElementXlfPath,
368 'translatePropertyValueIfEmpty' => true
369 ],
370 ];
371
372 $expected = 'form-element-identifier LABEL EN';
373
374 $this->store->flushData($formRuntimeXlfPath);
375 $this->store->flushData($textElementXlfPath);
376
377 $mockFormElement = $this->getAccessibleMock(GenericFormElement::class, ['dummy'], [], '', false);
378
379 $mockFormElement->_set('type', 'Text');
380 $mockFormElement->_set('renderingOptions', $formElementRenderingOptions);
381 $mockFormElement->_set('identifier', $formElementIdentifier);
382 $mockFormElement->_set('label', '');
383
384 $mockFormRuntime = $this->getAccessibleMock(FormRuntime::class, ['getIdentifier', 'getRenderingOptions'], [], '', false);
385 $mockFormRuntime->expects($this->any())->method('getIdentifier')->willReturn($formRuntimeIdentifier);
386 $mockFormRuntime->expects($this->any())->method('getRenderingOptions')->willReturn($formRuntimeRenderingOptions);
387
388 $this->assertEquals($expected, $this->mockTranslationService->_call('translateFormElementValue', $mockFormElement, ['label'], $mockFormRuntime));
389 }
390
391 /**
392 * @test
393 */
394 public function translateFormElementValueNotTranslateLabelForConcreteFormAndConcreteElementIfElementRenderingOptionsContainsATranslationFileAndElementLabelIsEmptyAndPropertyShouldNotBeTranslatedAndTranslationExists()
395 {
396 $formRuntimeXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
397 $textElementXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_text.xlf';
398
399 $formRuntimeIdentifier = 'form-runtime-identifier';
400 $formElementIdentifier = 'form-element-identifier';
401
402 $formRuntimeRenderingOptions = [
403 'translation' => [
404 'translationFile' => $formRuntimeXlfPath,
405 'translatePropertyValueIfEmpty' => true
406 ],
407 ];
408
409 $formElementRenderingOptions = [
410 'translation' => [
411 'translationFile' => $textElementXlfPath,
412 'translatePropertyValueIfEmpty' => false
413 ],
414 ];
415
416 $expected = '';
417
418 $this->store->flushData($formRuntimeXlfPath);
419 $this->store->flushData($textElementXlfPath);
420
421 $mockFormElement = $this->getAccessibleMock(GenericFormElement::class, ['dummy'], [], '', false);
422
423 $mockFormElement->_set('type', 'Text');
424 $mockFormElement->_set('renderingOptions', $formElementRenderingOptions);
425 $mockFormElement->_set('identifier', $formElementIdentifier);
426 $mockFormElement->_set('label', '');
427
428 $mockFormRuntime = $this->getAccessibleMock(FormRuntime::class, ['getIdentifier', 'getRenderingOptions'], [], '', false);
429 $mockFormRuntime->expects($this->any())->method('getIdentifier')->willReturn($formRuntimeIdentifier);
430 $mockFormRuntime->expects($this->any())->method('getRenderingOptions')->willReturn($formRuntimeRenderingOptions);
431
432 $this->assertEquals($expected, $this->mockTranslationService->_call('translateFormElementValue', $mockFormElement, ['label'], $mockFormRuntime));
433 }
434
435 /**
436 * @test
437 */
438 public function translateFormElementValueTranslateLabelForConcreteFormElementIfElementRenderingOptionsContainsATranslationFileAndElementLabelIsNotEmptyAndPropertyShouldBeTranslatedAndTranslationExists()
439 {
440 $formRuntimeXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
441 $textElementXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_text.xlf';
442
443 $formRuntimeIdentifier = 'another-form-runtime-identifier';
444 $formElementIdentifier = 'form-element-identifier';
445
446 $formRuntimeRenderingOptions = [
447 'translation' => [
448 'translationFile' => $formRuntimeXlfPath,
449 'translatePropertyValueIfEmpty' => true
450 ],
451 ];
452
453 $formElementRenderingOptions = [
454 'translation' => [
455 'translationFile' => $textElementXlfPath,
456 'translatePropertyValueIfEmpty' => true
457 ],
458 ];
459
460 $expected = 'form-element-identifier LABEL EN 1';
461
462 $this->store->flushData($formRuntimeXlfPath);
463 $this->store->flushData($textElementXlfPath);
464
465 $mockFormElement = $this->getAccessibleMock(GenericFormElement::class, ['dummy'], [], '', false);
466
467 $mockFormElement->_set('type', 'Text');
468 $mockFormElement->_set('renderingOptions', $formElementRenderingOptions);
469 $mockFormElement->_set('identifier', $formElementIdentifier);
470 $mockFormElement->_set('label', 'some label');
471
472 $mockFormRuntime = $this->getAccessibleMock(FormRuntime::class, ['getIdentifier', 'getRenderingOptions'], [], '', false);
473 $mockFormRuntime->expects($this->any())->method('getIdentifier')->willReturn($formRuntimeIdentifier);
474 $mockFormRuntime->expects($this->any())->method('getRenderingOptions')->willReturn($formRuntimeRenderingOptions);
475
476 $this->assertEquals($expected, $this->mockTranslationService->_call('translateFormElementValue', $mockFormElement, ['label'], $mockFormRuntime));
477 }
478
479 /**
480 * @test
481 */
482 public function translateFormElementValueTranslateLabelForFormElementTypeIfElementRenderingOptionsContainsATranslationFileAndElementLabelIsNotEmptyAndPropertyShouldBeTranslatedAndTranslationExists()
483 {
484 $formRuntimeXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
485 $textElementXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_text.xlf';
486
487 $formRuntimeIdentifier = 'another-form-runtime-identifier';
488 $formElementIdentifier = 'another-form-element-identifier';
489
490 $formRuntimeRenderingOptions = [
491 'translation' => [
492 'translationFile' => $formRuntimeXlfPath,
493 'translatePropertyValueIfEmpty' => true
494 ],
495 ];
496
497 $formElementRenderingOptions = [
498 'translation' => [
499 'translationFile' => $textElementXlfPath,
500 'translatePropertyValueIfEmpty' => true
501 ],
502 ];
503
504 $expected = 'form-element-identifier LABEL EN 2';
505
506 $this->store->flushData($formRuntimeXlfPath);
507 $this->store->flushData($textElementXlfPath);
508
509 $mockFormElement = $this->getAccessibleMock(GenericFormElement::class, ['dummy'], [], '', false);
510
511 $mockFormElement->_set('type', 'Text');
512 $mockFormElement->_set('renderingOptions', $formElementRenderingOptions);
513 $mockFormElement->_set('identifier', $formElementIdentifier);
514 $mockFormElement->_set('label', 'some label');
515
516 $mockFormRuntime = $this->getAccessibleMock(FormRuntime::class, ['getIdentifier', 'getRenderingOptions'], [], '', false);
517 $mockFormRuntime->expects($this->any())->method('getIdentifier')->willReturn($formRuntimeIdentifier);
518 $mockFormRuntime->expects($this->any())->method('getRenderingOptions')->willReturn($formRuntimeRenderingOptions);
519
520 $this->assertEquals($expected, $this->mockTranslationService->_call('translateFormElementValue', $mockFormElement, ['label'], $mockFormRuntime));
521 }
522
523 /**
524 * @test
525 */
526 public function translateFormElementValueTranslatePropertyForConcreteFormAndConcreteElementIfElementRenderingOptionsContainsATranslationFileAndElementPropertyIsNotEmptyAndPropertyShouldBeTranslatedAndTranslationExists()
527 {
528 $formRuntimeXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
529 $textElementXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_text.xlf';
530
531 $formRuntimeIdentifier = 'form-runtime-identifier';
532 $formElementIdentifier = 'form-element-identifier';
533
534 $formRuntimeRenderingOptions = [
535 'translation' => [
536 'translationFile' => $formRuntimeXlfPath,
537 'translatePropertyValueIfEmpty' => true
538 ],
539 ];
540
541 $formElementRenderingOptions = [
542 'translation' => [
543 'translationFile' => $textElementXlfPath,
544 'translatePropertyValueIfEmpty' => true
545 ],
546 ];
547 $formElementProperties = [
548 'placeholder' => 'placeholder',
549 ];
550
551 $expected = 'form-element-identifier PLACEHOLDER EN';
552
553 $this->store->flushData($formRuntimeXlfPath);
554 $this->store->flushData($textElementXlfPath);
555
556 $mockFormElement = $this->getAccessibleMock(GenericFormElement::class, ['dummy'], [], '', false);
557
558 $mockFormElement->_set('type', 'Text');
559 $mockFormElement->_set('renderingOptions', $formElementRenderingOptions);
560 $mockFormElement->_set('identifier', $formElementIdentifier);
561 $mockFormElement->_set('properties', $formElementProperties);
562
563 $mockFormRuntime = $this->getAccessibleMock(FormRuntime::class, ['getIdentifier', 'getRenderingOptions'], [], '', false);
564 $mockFormRuntime->expects($this->any())->method('getIdentifier')->willReturn($formRuntimeIdentifier);
565 $mockFormRuntime->expects($this->any())->method('getRenderingOptions')->willReturn($formRuntimeRenderingOptions);
566
567 $this->assertEquals($expected, $this->mockTranslationService->_call('translateFormElementValue', $mockFormElement, ['placeholder'], $mockFormRuntime));
568 }
569
570 /**
571 * @test
572 */
573 public function translateFormElementValueNotTranslatePropertyForConcreteFormAndConcreteElementIfElementRenderingOptionsContainsATranslationFileAndElementPropertyIsNotEmptyAndPropertyShouldBeTranslatedAndTranslationNotExists()
574 {
575 $formRuntimeXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
576 $textElementXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_text.xlf';
577
578 $formRuntimeIdentifier = 'another-form-runtime-identifier';
579 $formElementIdentifier = 'another-form-element-identifier';
580
581 $formRuntimeRenderingOptions = [
582 'translation' => [
583 'translationFile' => $formRuntimeXlfPath,
584 'translatePropertyValueIfEmpty' => true
585 ],
586 ];
587
588 $formElementRenderingOptions = [
589 'translation' => [
590 'translationFile' => $textElementXlfPath,
591 'translatePropertyValueIfEmpty' => true
592 ],
593 ];
594 $formElementProperties = [
595 'placeholder' => 'placeholder',
596 ];
597
598 $expected = 'placeholder';
599
600 $this->store->flushData($formRuntimeXlfPath);
601 $this->store->flushData($textElementXlfPath);
602
603 $mockFormElement = $this->getAccessibleMock(GenericFormElement::class, ['dummy'], [], '', false);
604
605 $mockFormElement->_set('type', 'Text');
606 $mockFormElement->_set('renderingOptions', $formElementRenderingOptions);
607 $mockFormElement->_set('identifier', $formElementIdentifier);
608 $mockFormElement->_set('properties', $formElementProperties);
609
610 $mockFormRuntime = $this->getAccessibleMock(FormRuntime::class, ['getIdentifier', 'getRenderingOptions'], [], '', false);
611 $mockFormRuntime->expects($this->any())->method('getIdentifier')->willReturn($formRuntimeIdentifier);
612 $mockFormRuntime->expects($this->any())->method('getRenderingOptions')->willReturn($formRuntimeRenderingOptions);
613
614 $this->assertEquals($expected, $this->mockTranslationService->_call('translateFormElementValue', $mockFormElement, ['placeholder'], $mockFormRuntime));
615 }
616
617 /**
618 * @test
619 */
620 public function translateFormElementValueTranslateRenderingOptionForConcreteFormAndConcreteSectionElementIfElementRenderingOptionsContainsATranslationFileAndElementRenderingOptionIsNotEmptyAndRenderingOptionShouldBeTranslatedAndTranslationExists()
621 {
622 $formRuntimeXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
623 $textElementXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_text.xlf';
624
625 $formRuntimeIdentifier = 'form-runtime-identifier';
626 $formElementIdentifier = 'form-element-identifier-page';
627
628 $formRuntimeRenderingOptions = [
629 'translation' => [
630 'translationFile' => $formRuntimeXlfPath,
631 'translatePropertyValueIfEmpty' => true
632 ],
633 ];
634
635 $formElementRenderingOptions = [
636 'nextButtonLabel' => 'next button label',
637 'translation' => [
638 'translationFile' => $textElementXlfPath,
639 'translatePropertyValueIfEmpty' => true
640 ],
641 ];
642 $formElementProperties = [
643 'placeholder' => 'placeholder',
644 ];
645
646 $expected = 'form-element-identifier nextButtonLabel EN';
647
648 $this->store->flushData($formRuntimeXlfPath);
649 $this->store->flushData($textElementXlfPath);
650
651 $mockFormElement = $this->getAccessibleMock(Page::class, ['dummy'], [], '', false);
652
653 $mockFormElement->_set('type', 'Page');
654 $mockFormElement->_set('renderingOptions', $formElementRenderingOptions);
655 $mockFormElement->_set('identifier', $formElementIdentifier);
656 $mockFormElement->_set('properties', $formElementProperties);
657
658 $mockFormRuntime = $this->getAccessibleMock(FormRuntime::class, ['getIdentifier', 'getRenderingOptions'], [], '', false);
659 $mockFormRuntime->expects($this->any())->method('getIdentifier')->willReturn($formRuntimeIdentifier);
660 $mockFormRuntime->expects($this->any())->method('getRenderingOptions')->willReturn($formRuntimeRenderingOptions);
661
662 $this->assertEquals($expected, $this->mockTranslationService->_call('translateFormElementValue', $mockFormElement, ['nextButtonLabel'], $mockFormRuntime));
663 }
664
665 /**
666 * @test
667 */
668 public function translateFormElementValueTranslateOptionsPropertyForConcreteFormAndConcreteElementIfElementRenderingOptionsContainsATranslationFileAndElementOptionsPropertyIsAnArrayAndPropertyShouldBeTranslatedAndTranslationExists()
669 {
670 $formRuntimeXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
671 $textElementXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_text.xlf';
672
673 $formRuntimeIdentifier = 'form-runtime-identifier';
674 $formElementIdentifier = 'options-form-element-identifier';
675
676 $formRuntimeRenderingOptions = [
677 'translation' => [
678 'translationFile' => $formRuntimeXlfPath,
679 'translatePropertyValueIfEmpty' => true
680 ],
681 ];
682
683 $formElementRenderingOptions = [
684 'translation' => [
685 'translationFile' => $textElementXlfPath,
686 'translatePropertyValueIfEmpty' => true
687 ],
688 ];
689 $formElementProperties = [
690 'options' => [
691 'optionValue1' => 'optionLabel1',
692 'optionValue2' => 'optionLabel2'
693 ],
694 ];
695
696 $expected = [
697 'optionValue1' => 'options-form-element-identifier option 1 EN',
698 'optionValue2' => 'options-form-element-identifier option 2 EN'
699 ];
700
701 $this->store->flushData($formRuntimeXlfPath);
702 $this->store->flushData($textElementXlfPath);
703
704 $mockFormElement = $this->getAccessibleMock(GenericFormElement::class, ['dummy'], [], '', false);
705
706 $mockFormElement->_set('type', 'Text');
707 $mockFormElement->_set('renderingOptions', $formElementRenderingOptions);
708 $mockFormElement->_set('identifier', $formElementIdentifier);
709 $mockFormElement->_set('properties', $formElementProperties);
710
711 $mockFormRuntime = $this->getAccessibleMock(FormRuntime::class, ['getIdentifier', 'getRenderingOptions'], [], '', false);
712 $mockFormRuntime->expects($this->any())->method('getIdentifier')->willReturn($formRuntimeIdentifier);
713 $mockFormRuntime->expects($this->any())->method('getRenderingOptions')->willReturn($formRuntimeRenderingOptions);
714
715 $this->assertEquals($expected, $this->mockTranslationService->_call('translateFormElementValue', $mockFormElement, ['options'], $mockFormRuntime));
716 }
717
718 /**
719 * @test
720 */
721 public function translateFormElementValueTranslateOptionsPropertyForConcreteElementIfElementRenderingOptionsContainsATranslationFileAndElementOptionsPropertyIsAnArrayAndPropertyShouldBeTranslatedAndTranslationExists()
722 {
723 $formRuntimeXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
724 $textElementXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_text.xlf';
725
726 $formRuntimeIdentifier = 'another-form-runtime-identifier';
727 $formElementIdentifier = 'options-form-element-identifier';
728
729 $formRuntimeRenderingOptions = [
730 'translation' => [
731 'translationFile' => $formRuntimeXlfPath,
732 'translatePropertyValueIfEmpty' => true
733 ],
734 ];
735
736 $formElementRenderingOptions = [
737 'translation' => [
738 'translationFile' => $textElementXlfPath,
739 'translatePropertyValueIfEmpty' => true
740 ],
741 ];
742 $formElementProperties = [
743 'options' => [
744 'optionValue1' => 'optionLabel1',
745 'optionValue2' => 'optionLabel2'
746 ],
747 ];
748
749 $expected = [
750 'optionValue1' => 'options-form-element-identifier option 1 EN',
751 'optionValue2' => 'options-form-element-identifier option 2 EN'
752 ];
753
754 $this->store->flushData($formRuntimeXlfPath);
755 $this->store->flushData($textElementXlfPath);
756
757 $mockFormElement = $this->getAccessibleMock(GenericFormElement::class, ['dummy'], [], '', false);
758
759 $mockFormElement->_set('type', 'Text');
760 $mockFormElement->_set('renderingOptions', $formElementRenderingOptions);
761 $mockFormElement->_set('identifier', $formElementIdentifier);
762 $mockFormElement->_set('properties', $formElementProperties);
763
764 $mockFormRuntime = $this->getAccessibleMock(FormRuntime::class, ['getIdentifier', 'getRenderingOptions'], [], '', false);
765 $mockFormRuntime->expects($this->any())->method('getIdentifier')->willReturn($formRuntimeIdentifier);
766 $mockFormRuntime->expects($this->any())->method('getRenderingOptions')->willReturn($formRuntimeRenderingOptions);
767
768 $this->assertEquals($expected, $this->mockTranslationService->_call('translateFormElementValue', $mockFormElement, ['options'], $mockFormRuntime));
769 }
770
771 /**
772 * @test
773 */
774 public function translateFinisherOptionTranslateOptionForConcreteFormIfFinisherTranslationOptionsContainsATranslationFileAndFinisherOptionIsNotEmptyAndPropertyShouldBeTranslatedAndTranslationExists()
775 {
776 $formRuntimeXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
777 $textElementXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_text.xlf';
778
779 $formRuntimeIdentifier = 'form-runtime-identifier';
780 $finisherIdentifier = 'SaveToDatabaseFinisher';
781
782 $formRuntimeRenderingOptions = [
783 'translation' => [
784 'translationFile' => $formRuntimeXlfPath,
785 'translatePropertyValueIfEmpty' => true
786 ],
787 ];
788
789 $finisherRenderingOptions = [
790 'translationFile' => $textElementXlfPath,
791 'translatePropertyValueIfEmpty' => true
792 ];
793
794 $expected = 'form-element-identifier SaveToDatabase subject EN';
795
796 $this->store->flushData($formRuntimeXlfPath);
797 $this->store->flushData($textElementXlfPath);
798
799 $mockFormRuntime = $this->getAccessibleMock(FormRuntime::class, ['getIdentifier', 'getRenderingOptions'], [], '', false);
800 $mockFormRuntime->expects($this->any())->method('getIdentifier')->willReturn($formRuntimeIdentifier);
801 $mockFormRuntime->expects($this->any())->method('getRenderingOptions')->willReturn($formRuntimeRenderingOptions);
802
803 $this->assertEquals($expected, $this->mockTranslationService->_call('translateFinisherOption', $mockFormRuntime, $finisherIdentifier, 'subject', 'subject value', $finisherRenderingOptions));
804 }
805
806 /**
807 * @test
808 */
809 public function translateFinisherOptionTranslateOptionIfFinisherTranslationOptionsContainsATranslationFileAndFinisherOptionIsNotEmptyAndPropertyShouldBeTranslatedAndTranslationExists()
810 {
811 $formRuntimeXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
812 $textElementXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_text.xlf';
813
814 $formRuntimeIdentifier = 'another-form-runtime-identifier';
815 $finisherIdentifier = 'SaveToDatabaseFinisher';
816
817 $formRuntimeRenderingOptions = [
818 'translation' => [
819 'translationFile' => $formRuntimeXlfPath,
820 'translatePropertyValueIfEmpty' => true
821 ],
822 ];
823
824 $finisherRenderingOptions = [
825 'translationFile' => $textElementXlfPath,
826 'translatePropertyValueIfEmpty' => true
827 ];
828
829 $expected = 'form-element-identifier SaveToDatabase subject EN 1';
830
831 $this->store->flushData($formRuntimeXlfPath);
832 $this->store->flushData($textElementXlfPath);
833
834 $mockFormRuntime = $this->getAccessibleMock(FormRuntime::class, ['getIdentifier', 'getRenderingOptions'], [], '', false);
835 $mockFormRuntime->expects($this->any())->method('getIdentifier')->willReturn($formRuntimeIdentifier);
836 $mockFormRuntime->expects($this->any())->method('getRenderingOptions')->willReturn($formRuntimeRenderingOptions);
837
838 $this->assertEquals($expected, $this->mockTranslationService->_call('translateFinisherOption', $mockFormRuntime, $finisherIdentifier, 'subject', 'subject value', $finisherRenderingOptions));
839 }
840
841 /**
842 * @test
843 */
844 public function translateFormElementValueTranslateLabelForConcreteFormAndConcreteElementFromFormRumtimeTranslationFileIfElementRenderingOptionsContainsNoTranslationFileAndElementLabelIsNotEmptyAndPropertyShouldBeTranslatedAndTranslationExists()
845 {
846 $formRuntimeXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
847 $textElementXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_text.xlf';
848
849 $formRuntimeIdentifier = 'my-form-runtime-identifier';
850 $formElementIdentifier = 'my-form-element-identifier';
851
852 $formRuntimeRenderingOptions = [
853 'translation' => [
854 'translationFile' => $formRuntimeXlfPath,
855 'translatePropertyValueIfEmpty' => true
856 ],
857 ];
858
859 $formElementRenderingOptions = [];
860
861 $expected = 'my-form-runtime-identifier my-form-element-identifier LABEL EN';
862
863 $this->store->flushData($formRuntimeXlfPath);
864 $this->store->flushData($textElementXlfPath);
865
866 $mockFormElement = $this->getAccessibleMock(GenericFormElement::class, ['dummy'], [], '', false);
867
868 $mockFormElement->_set('type', 'Text');
869 $mockFormElement->_set('renderingOptions', $formElementRenderingOptions);
870 $mockFormElement->_set('identifier', $formElementIdentifier);
871 $mockFormElement->_set('label', 'some label');
872
873 $mockFormRuntime = $this->getAccessibleMock(FormRuntime::class, ['getIdentifier', 'getRenderingOptions'], [], '', false);
874 $mockFormRuntime->expects($this->any())->method('getIdentifier')->willReturn($formRuntimeIdentifier);
875 $mockFormRuntime->expects($this->any())->method('getRenderingOptions')->willReturn($formRuntimeRenderingOptions);
876
877 $this->assertEquals($expected, $this->mockTranslationService->_call('translateFormElementValue', $mockFormElement, ['label'], $mockFormRuntime));
878 }
879
880 /**
881 * @test
882 */
883 public function supportsArgumentsForFormElementValueTranslations()
884 {
885 $formRuntimeXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
886
887 $this->store->flushData($formRuntimeXlfPath);
888
889 /** @var FormRuntime|\Prophecy\Prophecy\ObjectProphecy */
890 $formRuntime = $this->prophesize(FormRuntime::class);
891 $formRuntime->getIdentifier()->willReturn('my-form-runtime-identifier');
892 $formRuntime->getRenderingOptions()->willReturn([
893 'translation' => [
894 'translationFile' => $formRuntimeXlfPath,
895 'translatePropertyValueIfEmpty' => true,
896 ],
897 ]);
898
899 /** @var RootRenderableInterface|\Prophecy\Prophecy\ObjectProphecy */
900 $element = $this->prophesize(RootRenderableInterface::class);
901 $element->getIdentifier()->willReturn('my-form-element-with-translation-arguments');
902 $element->getType()->willReturn(RootRenderableInterface::class);
903 $element->getLabel()->willReturn('See %s or %s');
904 $element->getRenderingOptions()->willReturn([
905 'translation' => [
906 'arguments' => [
907 'label' => [
908 'this',
909 'that',
910 ],
911 ],
912 ],
913 ]);
914
915 $expected = 'See this or that';
916 $result = $this->mockTranslationService->_call('translateFormElementValue', $element->reveal(), ['label'], $formRuntime->reveal());
917
918 $this->assertEquals($expected, $result);
919 }
920
921 /**
922 * @test
923 */
924 public function translateFinisherOptionTranslateOptionForConcreteFormFromFormRuntimeIfFinisherTranslationOptionsContainsNoTranslationFileAndFinisherOptionIsNotEmptyAndPropertyShouldBeTranslatedAndTranslationExists()
925 {
926 $formRuntimeXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
927 $textElementXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_text.xlf';
928
929 $formRuntimeIdentifier = 'my-form-runtime-identifier';
930 $finisherIdentifier = 'SaveToDatabaseFinisher';
931
932 $formRuntimeRenderingOptions = [
933 'translation' => [
934 'translationFile' => $formRuntimeXlfPath,
935 'translatePropertyValueIfEmpty' => true
936 ],
937 ];
938
939 $finisherRenderingOptions = [];
940
941 $expected = 'my-form-runtime-identifier form-element-identifier SaveToDatabase subject EN';
942
943 $this->store->flushData($formRuntimeXlfPath);
944 $this->store->flushData($textElementXlfPath);
945
946 $mockFormRuntime = $this->getAccessibleMock(FormRuntime::class, ['getIdentifier', 'getRenderingOptions'], [], '', false);
947 $mockFormRuntime->expects($this->any())->method('getIdentifier')->willReturn($formRuntimeIdentifier);
948 $mockFormRuntime->expects($this->any())->method('getRenderingOptions')->willReturn($formRuntimeRenderingOptions);
949
950 $this->assertEquals($expected, $this->mockTranslationService->_call('translateFinisherOption', $mockFormRuntime, $finisherIdentifier, 'subject', 'subject value', $finisherRenderingOptions));
951 }
952
953 /**
954 * @test
955 */
956 public function supportsArgumentsForFinisherOptionTranslations()
957 {
958 $formRuntimeXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
959
960 $this->store->flushData($formRuntimeXlfPath);
961
962 /** @var FormRuntime|\Prophecy\Prophecy\ObjectProphecy */
963 $formRuntime = $this->prophesize(FormRuntime::class);
964 $formRuntime->getIdentifier()->willReturn('my-form-runtime-identifier');
965 $formRuntime->getRenderingOptions()->willReturn([
966 'translation' => [
967 'translationFile' => $formRuntimeXlfPath,
968 'translatePropertyValueIfEmpty' => true,
969 ],
970 ]);
971 $renderingOptions = [
972 'arguments' => [
973 'subject' => [
974 'awesome',
975 ],
976 ],
977 ];
978
979 $expected = 'My awesome subject';
980 $result = $this->mockTranslationService->_call('translateFinisherOption', $formRuntime->reveal(), 'EmailToReceiverWithTranslationArguments', 'subject', 'My %s subject', $renderingOptions);
981
982 $this->assertEquals($expected, $result);
983 }
984
985 /**
986 * @test
987 */
988 public function translateFormElementValueTranslateLabelFromAdditionalTranslationForConcreteFormAndConcreteElementIfElementRenderingOptionsContainsATranslationFileAndElementLabelIsNotEmptyAndPropertyShouldBeTranslatedAndTranslationExists()
989 {
990 $formRuntimeXlfPath = 'EXT:form/Tests/Unit/Service/Fixtures/locallang_form.xlf';
991 $textElementXlfPaths = [
992 10 => 'EXT:form/Tests/Unit/Service/Fixtures/locallang_text.xlf',
993 20 => 'EXT:form/Tests/Unit/Service/Fixtures/locallang_additional_text.xlf'
994 ];
995
996 $formRuntimeIdentifier = 'form-runtime-identifier';
997 $formElementIdentifier = 'form-element-identifier';
998
999 $formRuntimeRenderingOptions = [
1000 'translation' => [
1001 'translationFile' => $formRuntimeXlfPath,
1002 'translatePropertyValueIfEmpty' => true
1003 ],
1004 ];
1005
1006 $formElementRenderingOptions = [
1007 'translation' => [
1008 'translationFile' => $textElementXlfPaths,
1009 'translatePropertyValueIfEmpty' => true
1010 ],
1011 ];
1012
1013 $expected = 'form-element-identifier ADDITIONAL LABEL EN';
1014
1015 $this->store->flushData($formRuntimeXlfPath);
1016
1017 foreach ($textElementXlfPaths as $textElementXlfPath) {
1018 $this->store->flushData($textElementXlfPath);
1019 }
1020
1021 $mockFormElement = $this->getAccessibleMock(GenericFormElement::class, ['dummy'], [], '', false);
1022
1023 $mockFormElement->_set('type', 'Text');
1024 $mockFormElement->_set('renderingOptions', $formElementRenderingOptions);
1025 $mockFormElement->_set('identifier', $formElementIdentifier);
1026 $mockFormElement->_set('label', 'some label');
1027
1028 $mockFormRuntime = $this->getAccessibleMock(FormRuntime::class, ['getIdentifier', 'getRenderingOptions'], [], '', false);
1029 $mockFormRuntime->expects($this->any())->method('getIdentifier')->willReturn($formRuntimeIdentifier);
1030 $mockFormRuntime->expects($this->any())->method('getRenderingOptions')->willReturn($formRuntimeRenderingOptions);
1031
1032 $this->assertEquals($expected, $this->mockTranslationService->_call('translateFormElementValue', $mockFormElement, ['label'], $mockFormRuntime));
1033 }
1034 }