TYPO3 CMS  TYPO3_8-7
FormDefinitionValidationServiceTest.php
Go to the documentation of this file.
1 <?php
2 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 
25 
26 class FormDefinitionValidationServiceTest extends UnitTestCase
27 {
31  protected $singletonInstances = [];
32 
36  public function setUp()
37  {
38  parent::setUp();
39  $this->singletonInstances = GeneralUtility::getSingletonInstances();
40  $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '12345';
41  }
42 
46  public function tearDown()
47  {
48  GeneralUtility::resetSingletonInstances($this->singletonInstances);
49  parent::tearDown();
50  }
51 
56  {
57  $this->expectException(PropertyException::class);
58  $this->expectExceptionCode(1528588036);
59 
60  $typeConverter = $this->getAccessibleMock(FormDefinitionValidationService::class, ['dummy'], [], '', false);
61 
62  $prototypeName = 'standard';
63  $identifier = 'some-text';
64 
65  $sessionToken = '123';
66 
67  $validationDto = new ValidationDto($prototypeName, 'Text', $identifier);
68 
69  $input = [
70  'label' => 'xxx',
71  '_orig_label' => [
72  'value' => 'aaa',
73  'hmac' => GeneralUtility::hmac(serialize([$identifier, 'label', 'aaa']), $sessionToken),
74  ],
75  ];
76 
77  $typeConverter->_call('validateAllFormElementPropertyValuesByHmac', $input, $sessionToken, $validationDto);
78  }
79 
84  {
85  $this->expectException(PropertyException::class);
86  $this->expectExceptionCode(1528588037);
87 
88  $typeConverter = $this->getAccessibleMock(FormDefinitionValidationService::class, ['dummy'], [], '', false);
89 
90  $prototypeName = 'standard';
91  $identifier = 'some-text';
92 
93  $sessionToken = '123';
94 
95  $validationDto = new ValidationDto($prototypeName, 'Text', $identifier);
96 
97  $input = [
98  'label' => 'xxx',
99  ];
100 
101  $typeConverter->_call('validateAllFormElementPropertyValuesByHmac', $input, $sessionToken, $validationDto);
102  }
103 
108  {
109  $typeConverter = $this->getAccessibleMock(FormDefinitionValidationService::class, ['dummy'], [], '', false);
110 
111  $prototypeName = 'standard';
112  $identifier = 'some-text';
113 
114  $sessionToken = '123';
115 
116  $validationDto = new ValidationDto($prototypeName, 'Text', $identifier);
117 
118  $input = [
119  'label' => 'aaa',
120  '_orig_label' => [
121  'value' => 'aaa',
122  'hmac' => GeneralUtility::hmac(serialize([$identifier, 'label', 'aaa']), $sessionToken),
123  ],
124  ];
125 
126  $failed = false;
127  try {
128  $typeConverter->_call(
129  'validateAllFormElementPropertyValuesByHmac',
130  $input,
131  $sessionToken,
132  $validationDto
133  );
134  } catch (PropertyException $e) {
135  $failed = true;
136  }
137  $this->assertFalse($failed);
138  }
139 
144  {
145  $this->expectException(PropertyException::class);
146  $this->expectExceptionCode(1528591586);
147 
148  $typeConverter = $this->getAccessibleMock(FormDefinitionValidationService::class, ['dummy'], [], '', false);
149 
150  $prototypeName = 'standard';
151  $identifier = 'some-text';
152 
153  $sessionToken = '123';
154 
155  $validationDto = new ValidationDto($prototypeName, 'Text', $identifier, null, 'validators');
156 
157  $input = [
158  'identifier' => 'StringLength',
159  '_orig_identifier' => [
160  'value' => 'StringLength',
161  'hmac' => GeneralUtility::hmac(serialize([$identifier, 'validators', 'StringLength', 'identifier', 'StringLength']), $sessionToken),
162  ],
163  'options' => [
164  'test' => 'xxx',
165  '_orig_test' => [
166  'value' => 'aaa',
167  'hmac' => GeneralUtility::hmac(serialize([$identifier, 'validators', 'StringLength', 'options.test', 'aaa']), $sessionToken),
168  ],
169  ],
170  ];
171 
172  $typeConverter->_call(
173  'validateAllPropertyCollectionElementValuesByHmac',
174  $input,
175  $sessionToken,
176  $validationDto
177  );
178  }
179 
184  {
185  $this->expectException(PropertyException::class);
186  $this->expectExceptionCode(1528591585);
187 
188  $typeConverter = $this->getAccessibleMock(FormDefinitionValidationService::class, ['dummy'], [], '', false);
189 
190  $prototypeName = 'standard';
191  $identifier = 'some-text';
192 
193  $sessionToken = '123';
194 
195  $validationDto = new ValidationDto($prototypeName, 'Text', $identifier, null, 'validators');
196 
197  $input = [
198  'identifier' => 'StringLength',
199  '_orig_identifier' => [
200  'value' => 'StringLength',
201  'hmac' => GeneralUtility::hmac(serialize([$identifier, 'validators', 'StringLength', 'identifier', 'StringLength']), $sessionToken),
202  ],
203  'options' => [
204  'test' => 'xxx',
205  ],
206  ];
207 
208  $typeConverter->_call(
209  'validateAllPropertyCollectionElementValuesByHmac',
210  $input,
211  $sessionToken,
212  $validationDto
213  );
214  }
215 
220  {
221  $typeConverter = $this->getAccessibleMock(FormDefinitionValidationService::class, ['dummy'], [], '', false);
222 
223  $prototypeName = 'standard';
224  $identifier = 'some-text';
225 
226  $sessionToken = '123';
227 
228  $validationDto = new ValidationDto($prototypeName, 'Text', $identifier, null, 'validators');
229 
230  $input = [
231  'identifier' => 'StringLength',
232  '_orig_identifier' => [
233  'value' => 'StringLength',
234  'hmac' => GeneralUtility::hmac(serialize([$identifier, 'validators', 'StringLength', 'identifier', 'StringLength']), $sessionToken),
235  ],
236  'options' => [
237  'test' => 'aaa',
238  '_orig_test' => [
239  'value' => 'aaa',
240  'hmac' => GeneralUtility::hmac(serialize([$identifier, 'validators', 'StringLength', 'options.test', 'aaa']), $sessionToken),
241  ],
242  ],
243  ];
244 
245  $failed = false;
246  try {
247  $typeConverter->_call(
248  'validateAllPropertyCollectionElementValuesByHmac',
249  $input,
250  $sessionToken,
251  $validationDto
252  );
253  } catch (PropertyException $e) {
254  $failed = true;
255  }
256  $this->assertFalse($failed);
257  }
258 
269  array $mockConfiguration,
270  array $formElement,
271  string $sessionToken,
272  int $exceptionCode,
273  ValidationDto $validationDto
274  ) {
275  $typeConverter = $this->getAccessibleMock(
276  FormDefinitionValidationService::class,
277  ['getConfigurationService'],
278  [],
279  '',
280  false
281  );
282 
283  $configurationService = $this->createMock(ConfigurationService::class);
284  $configurationService->expects($this->any())
285  ->method('isFormElementPropertyDefinedInFormEditorSetup')
286  ->willReturn($mockConfiguration['isFormElementPropertyDefinedInFormEditorSetup']);
287  $configurationService->expects($this->any())->method(
288  'isFormElementPropertyDefinedInPredefinedDefaultsInFormEditorSetup'
289  )->willReturn($mockConfiguration['isFormElementPropertyDefinedInPredefinedDefaultsInFormEditorSetup']);
290  $configurationService->expects($this->any())
291  ->method('getFormElementPredefinedDefaultValueFromFormEditorSetup')
292  ->willReturn($mockConfiguration['getFormElementPredefinedDefaultValueFromFormEditorSetup']);
293  $typeConverter->expects($this->any())->method('getConfigurationService')->willReturn($configurationService);
294  $formDefinitionValidationService = $this->getAccessibleMock(FormDefinitionValidationService::class, ['getConfigurationService']);
295  $formDefinitionValidationService->expects($this->any())->method('getConfigurationService')->willReturn($configurationService);
296  GeneralUtility::setSingletonInstance(FormDefinitionValidationService::class, $formDefinitionValidationService);
297  GeneralUtility::setSingletonInstance(ConfigurationService::class, $configurationService);
298 
299  $objectMangerProphecy = $this->prophesize(ObjectManager::class);
300  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectMangerProphecy->reveal());
301 
302  $objectMangerProphecy
303  ->get(ConfigurationService::class)
304  ->willReturn($configurationService);
305 
306  $returnedExceptionCode = -1;
307  try {
308  $typeConverter->_call(
309  'validateAllPropertyValuesFromCreatableFormElement',
310  $formElement,
311  $sessionToken,
312  $validationDto
313  );
314  } catch (PropertyException $e) {
315  $returnedExceptionCode = $e->getCode();
316  }
317  $this->assertEquals($returnedExceptionCode, $exceptionCode);
318  }
319 
330  array $mockConfiguration,
331  array $formElement,
332  string $sessionToken,
333  int $exceptionCode,
334  ValidationDto $validationDto
335  ) {
336  $typeConverter = $this->getAccessibleMock(
337  FormDefinitionValidationService::class,
338  ['getConfigurationService'],
339  [],
340  '',
341  false
342  );
343 
344  $configurationService = $this->createMock(ConfigurationService::class);
345  $configurationService->expects($this->any())
346  ->method('isPropertyCollectionPropertyDefinedInFormEditorSetup')
347  ->willReturn($mockConfiguration['isPropertyCollectionPropertyDefinedInFormEditorSetup']);
348  $configurationService->expects($this->any())->method(
349  'isPropertyCollectionPropertyDefinedInPredefinedDefaultsInFormEditorSetup'
350  )->willReturn($mockConfiguration['isPropertyCollectionPropertyDefinedInPredefinedDefaultsInFormEditorSetup']);
351  $configurationService->expects($this->any())->method(
352  'getPropertyCollectionPredefinedDefaultValueFromFormEditorSetup'
353  )->willReturn($mockConfiguration['getPropertyCollectionPredefinedDefaultValueFromFormEditorSetup']);
354  $typeConverter->expects($this->any())->method('getConfigurationService')->willReturn($configurationService);
355  $formDefinitionValidationService = $this->getAccessibleMock(FormDefinitionValidationService::class, ['getConfigurationService']);
356  $formDefinitionValidationService->expects($this->any())->method('getConfigurationService')->willReturn($configurationService);
357  GeneralUtility::setSingletonInstance(FormDefinitionValidationService::class, $formDefinitionValidationService);
358  GeneralUtility::setSingletonInstance(ConfigurationService::class, $configurationService);
359 
360  $objectMangerProphecy = $this->prophesize(ObjectManager::class);
361  GeneralUtility::setSingletonInstance(ObjectManager::class, $objectMangerProphecy->reveal());
362 
363  $objectMangerProphecy
364  ->get(ConfigurationService::class)
365  ->willReturn($configurationService);
366 
367  $returnedExceptionCode = -1;
368  try {
369  $typeConverter->_call(
370  'validateAllPropertyValuesFromCreatablePropertyCollectionElement',
371  $formElement,
372  $sessionToken,
373  $validationDto
374  );
375  } catch (PropertyException $e) {
376  $returnedExceptionCode = $e->getCode();
377  }
378  $this->assertEquals($returnedExceptionCode, $exceptionCode);
379  }
380 
385  {
386  $encryptionKeyBackup = $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
387  $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '12345';
388 
389  $sessionToken = '54321';
390  $identifier = 'text-1';
391 
392  $validationDto = new ValidationDto('standard', 'Text', $identifier);
393  $formElement = [
394  'test' => 'xxx',
395  '_orig_test' => [
396  'value' => 'xxx',
397  'hmac' => GeneralUtility::hmac(serialize([$identifier, 'test', 'xxx']), $sessionToken),
398  ],
399  ];
400 
401  $invalidFormElement = [
402  'test' => 'xxx1',
403  '_orig_test' => [
404  'value' => 'xxx',
405  'hmac' => GeneralUtility::hmac(serialize([$identifier, 'test', 'xxx']), $sessionToken),
406  ],
407  ];
408 
409  // be aware that backup globals does not impact globals used in data providers as these are called before the setUp/tearDown is done
410  $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = $encryptionKeyBackup;
411 
412  return [
413  [
414  [
415  'isFormElementPropertyDefinedInFormEditorSetup' => true,
416  'isFormElementPropertyDefinedInPredefinedDefaultsInFormEditorSetup' => false,
417  'getFormElementPredefinedDefaultValueFromFormEditorSetup' => '',
418  ],
419  $formElement,
420  $sessionToken,
421  -1,
422  $validationDto
423  ],
424  [
425  [
426  'isFormElementPropertyDefinedInFormEditorSetup' => false,
427  'isFormElementPropertyDefinedInPredefinedDefaultsInFormEditorSetup' => false,
428  'getFormElementPredefinedDefaultValueFromFormEditorSetup' => 'default',
429  ],
430  $formElement,
431  $sessionToken,
432  -1,
433  $validationDto
434  ],
435  [
436  [
437  'isFormElementPropertyDefinedInFormEditorSetup' => false,
438  'isFormElementPropertyDefinedInPredefinedDefaultsInFormEditorSetup' => false,
439  'getFormElementPredefinedDefaultValueFromFormEditorSetup' => 'default',
440  ],
441  ['test' => 'xxx'],
442  $sessionToken,
443  1528588037,
444  $validationDto
445  ],
446  [
447  [
448  'isFormElementPropertyDefinedInFormEditorSetup' => false,
449  'isFormElementPropertyDefinedInPredefinedDefaultsInFormEditorSetup' => false,
450  'getFormElementPredefinedDefaultValueFromFormEditorSetup' => 'default',
451  ],
452  ['test' => 'xxx', '_orig_test' => []],
453  $sessionToken,
454  1528538222,
455  $validationDto
456  ],
457  [
458  [
459  'isFormElementPropertyDefinedInFormEditorSetup' => false,
460  'isFormElementPropertyDefinedInPredefinedDefaultsInFormEditorSetup' => false,
461  'getFormElementPredefinedDefaultValueFromFormEditorSetup' => 'default',
462  ],
463  ['test' => 'xxx', '_orig_test' => ['hmac' => '4242']],
464  $sessionToken,
465  1528538252,
466  $validationDto
467  ],
468  [
469  [
470  'isFormElementPropertyDefinedInFormEditorSetup' => false,
471  'isFormElementPropertyDefinedInPredefinedDefaultsInFormEditorSetup' => false,
472  'getFormElementPredefinedDefaultValueFromFormEditorSetup' => 'default',
473  ],
474  $invalidFormElement,
475  $sessionToken,
476  1528588036,
477  $validationDto
478  ],
479 
480  [
481  [
482  'isFormElementPropertyDefinedInFormEditorSetup' => false,
483  'isFormElementPropertyDefinedInPredefinedDefaultsInFormEditorSetup' => true,
484  'getFormElementPredefinedDefaultValueFromFormEditorSetup' => 'xxx',
485  ],
486  $formElement,
487  $sessionToken,
488  -1,
489  $validationDto
490  ],
491  [
492  [
493  'isFormElementPropertyDefinedInFormEditorSetup' => false,
494  'isFormElementPropertyDefinedInPredefinedDefaultsInFormEditorSetup' => true,
495  'getFormElementPredefinedDefaultValueFromFormEditorSetup' => 'default',
496  ],
497  $formElement,
498  $sessionToken,
499  1528588035,
500  $validationDto
501  ],
502  ];
503  }
504 
509  {
510  $encryptionKeyBackup = $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
511  $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = '12345';
512 
513  $sessionToken = '54321';
514  $identifier = 'text-1';
515 
516  $validationDto = new ValidationDto('standard', 'Text', $identifier, null, 'validators', 'StringLength');
517  $formElement = [
518  'test' => 'xxx',
519  '_orig_test' => [
520  'value' => 'xxx',
521  'hmac' => GeneralUtility::hmac(serialize([$identifier, 'validators', 'StringLength', 'test', 'xxx']), $sessionToken),
522  ],
523  ];
524 
525  $invalidFormElement = [
526  'test' => 'xxx1',
527  '_orig_test' => [
528  'value' => 'xxx',
529  'hmac' => GeneralUtility::hmac(serialize([$identifier, 'validators', 'StringLength', 'test', 'xxx']), $sessionToken),
530  ],
531  ];
532 
533  // be aware that backup globals does not impact globals used in data providers as these are called before the setUp/tearDown is done
534  $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = $encryptionKeyBackup;
535 
536  return [
537  [
538  [
539  'isPropertyCollectionPropertyDefinedInFormEditorSetup' => true,
540  'isPropertyCollectionPropertyDefinedInPredefinedDefaultsInFormEditorSetup' => false,
541  'getPropertyCollectionPredefinedDefaultValueFromFormEditorSetup' => 'default',
542  ],
543  $formElement,
544  $sessionToken,
545  -1,
546  $validationDto
547  ],
548  [
549  [
550  'isPropertyCollectionPropertyDefinedInFormEditorSetup' => false,
551  'isPropertyCollectionPropertyDefinedInPredefinedDefaultsInFormEditorSetup' => false,
552  'getPropertyCollectionPredefinedDefaultValueFromFormEditorSetup' => 'default',
553  ],
554  $formElement,
555  $sessionToken,
556  -1,
557  $validationDto
558  ],
559  [
560  [
561  'isPropertyCollectionPropertyDefinedInFormEditorSetup' => false,
562  'isPropertyCollectionPropertyDefinedInPredefinedDefaultsInFormEditorSetup' => false,
563  'getPropertyCollectionPredefinedDefaultValueFromFormEditorSetup' => 'default',
564  ],
565  ['test' => 'xxx'],
566  $sessionToken,
567  1528591585,
568  $validationDto
569  ],
570  [
571  [
572  'isPropertyCollectionPropertyDefinedInFormEditorSetup' => false,
573  'isPropertyCollectionPropertyDefinedInPredefinedDefaultsInFormEditorSetup' => false,
574  'getPropertyCollectionPredefinedDefaultValueFromFormEditorSetup' => 'default',
575  ],
576  ['test' => 'xxx', '_orig_test' => []],
577  $sessionToken,
578  1528538222,
579  $validationDto
580  ],
581  [
582  [
583  'isPropertyCollectionPropertyDefinedInFormEditorSetup' => false,
584  'isPropertyCollectionPropertyDefinedInPredefinedDefaultsInFormEditorSetup' => false,
585  'getPropertyCollectionPredefinedDefaultValueFromFormEditorSetup' => 'default',
586  ],
587  ['test' => 'xxx', '_orig_test' => ['hmac' => '4242']],
588  $sessionToken,
589  1528538252,
590  $validationDto
591  ],
592  [
593  [
594  'isPropertyCollectionPropertyDefinedInFormEditorSetup' => false,
595  'isPropertyCollectionPropertyDefinedInPredefinedDefaultsInFormEditorSetup' => false,
596  'getPropertyCollectionPredefinedDefaultValueFromFormEditorSetup' => 'default',
597  ],
598  $invalidFormElement,
599  $sessionToken,
600  1528591586,
601  $validationDto
602  ],
603 
604  [
605  [
606  'isPropertyCollectionPropertyDefinedInFormEditorSetup' => false,
607  'isPropertyCollectionPropertyDefinedInPredefinedDefaultsInFormEditorSetup' => true,
608  'getPropertyCollectionPredefinedDefaultValueFromFormEditorSetup' => 'xxx',
609  ],
610  $formElement,
611  $sessionToken,
612  -1,
613  $validationDto
614  ],
615  [
616  [
617  'isPropertyCollectionPropertyDefinedInFormEditorSetup' => false,
618  'isPropertyCollectionPropertyDefinedInPredefinedDefaultsInFormEditorSetup' => true,
619  'getPropertyCollectionPredefinedDefaultValueFromFormEditorSetup' => 'default',
620  ],
621  $formElement,
622  $sessionToken,
623  1528591502,
624  $validationDto
625  ],
626  ];
627  }
628 }
static setSingletonInstance($className, SingletonInterface $instance)
static hmac($input, $additionalSecret='')
static resetSingletonInstances(array $newSingletonInstances)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
validateAllPropertyValuesFromCreatablePropertyCollectionElement(array $mockConfiguration, array $formElement, string $sessionToken, int $exceptionCode, ValidationDto $validationDto)
validateAllPropertyValuesFromCreatableFormElement(array $mockConfiguration, array $formElement, string $sessionToken, int $exceptionCode, ValidationDto $validationDto)