‪TYPO3CMS  10.4
MvcPropertyMappingConfigurationServiceTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
34 {
41  {
42  return [
43  'Simple Case - Empty' => [
44  [],
45  [],
46  ],
47  'Simple Case - Single Value' => [
48  ['field1'],
49  ['field1' => 1],
50  ],
51  'Simple Case - Two Values' => [
52  ['field1', 'field2'],
53  [
54  'field1' => 1,
55  'field2' => 1
56  ],
57  ],
58  'Recursion' => [
59  ['field1', 'field[subfield1]', 'field[subfield2]'],
60  [
61  'field1' => 1,
62  'field' => [
63  'subfield1' => 1,
64  'subfield2' => 1
65  ]
66  ],
67  ],
68  'recursion with duplicated field name' => [
69  ['field1', 'field[subfield1]', 'field[subfield2]', 'field1'],
70  [
71  'field1' => 1,
72  'field' => [
73  'subfield1' => 1,
74  'subfield2' => 1
75  ]
76  ],
77  ],
78  'Recursion with un-named fields at the end (...[]). There, they should be made explicit by increasing the counter' => [
79  ['field1', 'field[subfield1][]', 'field[subfield1][]', 'field[subfield2]'],
80  [
81  'field1' => 1,
82  'field' => [
83  'subfield1' => [
84  0 => 1,
85  1 => 1
86  ],
87  'subfield2' => 1
88  ]
89  ],
90  ],
91  ];
92  }
93 
101  {
102  return [
103  'Overriding form fields (string overridden by array) - 1' => [
104  ['field1', 'field2', 'field2[bla]', 'field2[blubb]'],
105  1255072196
106  ],
107  'Overriding form fields (string overridden by array) - 2' => [
108  ['field1', 'field2[bla]', 'field2[bla][blubb][blubb]'],
109  1255072196
110  ],
111  'Overriding form fields (array overridden by string) - 1' => [
112  ['field1', 'field2[bla]', 'field2[blubb]', 'field2'],
113  1255072587
114  ],
115  'Overriding form fields (array overridden by string) - 2' => [
116  ['field1', 'field2[bla][blubb][blubb]', 'field2[bla]'],
117  1255072587
118  ],
119  'Empty [] not as last argument' => [
120  ['field1', 'field2[][bla]'],
121  1255072832
122  ]
123 
124  ];
125  }
126 
132  {
133  $requestHashService = $this->getMockBuilder(MvcPropertyMappingConfigurationService::class)
134  ->setMethods(['serializeAndHashFormFieldArray'])
135  ->getMock();
136  $requestHashService->expects(self::once())->method('serializeAndHashFormFieldArray')->with($expected);
137  $requestHashService->generateTrustedPropertiesToken($input);
138  }
139 
146  public function ‪generateTrustedPropertiesTokenThrowsExceptionInWrongCases($input, $expectExceptionCode)
147  {
148  $this->expectException(InvalidArgumentForHashGenerationException::class);
149  $this->expectExceptionCode($expectExceptionCode);
150  $requestHashService = $this->getMockBuilder(MvcPropertyMappingConfigurationService::class)
151  ->setMethods(['serializeAndHashFormFieldArray'])
152  ->getMock();
153  $requestHashService->generateTrustedPropertiesToken($input);
154  }
155 
160  {
161  $formFieldArray = [
162  'bla' => [
163  'blubb' => 1,
164  'hu' => 1
165  ]
166  ];
167  $mockHash = '12345';
168 
169  $hashService = $this->getMockBuilder(HashService::class)
170  ->setMethods(['appendHmac'])
171  ->getMock();
172  $hashService->expects(self::once())->method('appendHmac')->with(json_encode($formFieldArray))->willReturn(json_encode($formFieldArray) . $mockHash);
173 
174  $requestHashService = $this->getAccessibleMock(MvcPropertyMappingConfigurationService::class, ['dummy']);
175  $requestHashService->injectHashService($hashService);
176 
177  $expected = json_encode($formFieldArray) . $mockHash;
178  $actual = $requestHashService->_call('serializeAndHashFormFieldArray', $formFieldArray);
179  self::assertEquals($expected, $actual);
180  }
181 
186  {
187  $request = $this->getMockBuilder(Request::class)->setMethods(['getInternalArgument'])->disableOriginalConstructor()->getMock();
188  $request->expects(self::any())->method('getInternalArgument')->with('__trustedProperties')->willReturn(null);
189  $arguments = new ‪Arguments();
190 
191  $requestHashService = new ‪MvcPropertyMappingConfigurationService();
192  $requestHashService->initializePropertyMappingConfigurationFromRequest($request, $arguments);
193  }
194 
199  {
200  $this->expectException(BadRequestException::class);
201  $this->expectExceptionCode(1581862822);
202 
203  $request = $this->getMockBuilder(Request::class)->setMethods(['getInternalArgument'])->disableOriginalConstructor()->getMock();
204  $request->expects(self::any())->method('getInternalArgument')->with('__trustedProperties')->willReturn('string with less than 40 characters');
205  $arguments = new ‪Arguments();
206 
207  $hashService = new ‪HashService();
208  $requestHashService = new ‪MvcPropertyMappingConfigurationService();
209  $requestHashService->injectHashService($hashService);
210  $requestHashService->initializePropertyMappingConfigurationFromRequest($request, $arguments);
211  }
212 
217  {
218  $trustedProperties = [
219  'foo' => 1
220  ];
221  $this->‪initializePropertyMappingConfiguration($trustedProperties);
222  }
223 
228  {
229  $trustedProperties = [
230  'nonExistingArgument' => 1
231  ];
232  $arguments = $this->‪initializePropertyMappingConfiguration($trustedProperties);
233  self::assertFalse($arguments->hasArgument('nonExistingArgument'));
234  }
235 
240  {
241  $trustedProperties = [
242  'foo' => [
243  '__identity' => 1,
244  'nested' => [
245  '__identity' => 1,
246  ]
247  ]
248  ];
249  $arguments = $this->‪initializePropertyMappingConfiguration($trustedProperties);
250  $propertyMappingConfiguration = $arguments->getArgument('foo')->getPropertyMappingConfiguration();
251  self::assertTrue($propertyMappingConfiguration->getConfigurationValue(PersistentObjectConverter::class, ‪PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED), 'ConfigurationValue is not CONFIGURATION_MODIFICATION_ALLOWED at line ' . __LINE__);
252  self::assertNull($propertyMappingConfiguration->getConfigurationValue(PersistentObjectConverter::class, ‪PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED), 'ConfigurationValue is not NULL at line ' . __LINE__);
253  self::assertFalse($propertyMappingConfiguration->shouldMap('someProperty'), 'Value is not FALSE at line ' . __LINE__);
254 
255  self::assertTrue($propertyMappingConfiguration->forProperty('nested')->getConfigurationValue(PersistentObjectConverter::class, ‪PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED), 'ConfigurationValue is not CONFIGURATION_MODIFICATION_ALLOWED at line ' . __LINE__);
256  self::assertNull($propertyMappingConfiguration->forProperty('nested')->getConfigurationValue(PersistentObjectConverter::class, ‪PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED), 'ConfigurationValue is not NULL at line ' . __LINE__);
257  self::assertFalse($propertyMappingConfiguration->forProperty('nested')->shouldMap('someProperty'), 'Value is not FALSE at line ' . __LINE__);
258  }
259 
264  {
265  $trustedProperties = [
266  'foo' => [
267  'bar' => []
268  ]
269  ];
270  $arguments = $this->‪initializePropertyMappingConfiguration($trustedProperties);
271  $propertyMappingConfiguration = $arguments->getArgument('foo')->getPropertyMappingConfiguration();
272  self::assertNull($propertyMappingConfiguration->getConfigurationValue(PersistentObjectConverter::class, ‪PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED));
273  self::assertTrue($propertyMappingConfiguration->getConfigurationValue(PersistentObjectConverter::class, ‪PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED));
274  self::assertFalse($propertyMappingConfiguration->shouldMap('someProperty'));
275 
276  self::assertNull($propertyMappingConfiguration->forProperty('bar')->getConfigurationValue(PersistentObjectConverter::class, ‪PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED));
277  self::assertTrue($propertyMappingConfiguration->forProperty('bar')->getConfigurationValue(PersistentObjectConverter::class, ‪PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED));
278  self::assertFalse($propertyMappingConfiguration->forProperty('bar')->shouldMap('someProperty'));
279  }
280 
285  {
286  $trustedProperties = [
287  'foo' => [
288  'bar' => 1
289  ]
290  ];
291  $arguments = $this->‪initializePropertyMappingConfiguration($trustedProperties);
292  $propertyMappingConfiguration = $arguments->getArgument('foo')->getPropertyMappingConfiguration();
293  self::assertFalse($propertyMappingConfiguration->shouldMap('someProperty'));
294  self::assertTrue($propertyMappingConfiguration->shouldMap('bar'));
295  }
296 
301  {
302  $trustedProperties = [
303  'foo' => [
304  'bar' => [
305  'foo' => 1
306  ]
307  ]
308  ];
309  $arguments = $this->‪initializePropertyMappingConfiguration($trustedProperties);
310  $propertyMappingConfiguration = $arguments->getArgument('foo')->getPropertyMappingConfiguration();
311  self::assertFalse($propertyMappingConfiguration->shouldMap('someProperty'));
312  self::assertTrue($propertyMappingConfiguration->shouldMap('bar'));
313  self::assertTrue($propertyMappingConfiguration->forProperty('bar')->shouldMap('foo'));
314  }
315 
323  protected function ‪initializePropertyMappingConfiguration(array $trustedProperties)
324  {
325  $request = $this->getMockBuilder(Request::class)->setMethods(['getInternalArgument'])->disableOriginalConstructor()->getMock();
326  $request->expects(self::any())->method('getInternalArgument')->with('__trustedProperties')->willReturn('fooTrustedProperties');
327 
328  $mockHashService = $this->getMockBuilder(HashService::class)
329  ->setMethods(['validateAndStripHmac'])
330  ->getMock();
331  $mockHashService->expects(self::once())->method('validateAndStripHmac')->with('fooTrustedProperties')->willReturn(json_encode($trustedProperties));
332 
333  $requestHashService = $this->getAccessibleMock(MvcPropertyMappingConfigurationService::class, ['dummy']);
334  $requestHashService->_set('hashService', $mockHashService);
335 
336  $mockObjectManager = $this->createMock(ObjectManagerInterface::class);
337  $mockArgument = $this->getAccessibleMock(Argument::class, ['getName'], [], '', false);
338 
339  $propertyMappingConfiguration = new ‪MvcPropertyMappingConfiguration();
340 
341  $mockArgument->_set('propertyMappingConfiguration', $propertyMappingConfiguration);
342  $mockArgument->expects(self::any())->method('getName')->willReturn('foo');
343  $mockObjectManager->expects(self::once())->method('get')->with(Argument::class)->willReturn($mockArgument);
344 
345  $arguments = $this->getAccessibleMock(Arguments::class, ['dummy']);
346  $arguments->_set('objectManager', $mockObjectManager);
347  $arguments->addNewArgument('foo');
348 
349  $requestHashService->initializePropertyMappingConfigurationFromRequest($request, $arguments);
350 
351  return $arguments;
352  }
353 }
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\initializePropertyMappingConfigurationThrowsBadRequestExceptionOnInvalidHmac
‪initializePropertyMappingConfigurationThrowsBadRequestExceptionOnInvalidHmac()
Definition: MvcPropertyMappingConfigurationServiceTest.php:198
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\initializePropertyMappingConfigurationReturnsEarlyIfNoTrustedPropertiesAreSet
‪initializePropertyMappingConfigurationReturnsEarlyIfNoTrustedPropertiesAreSet()
Definition: MvcPropertyMappingConfigurationServiceTest.php:216
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller
Definition: ActionControllerTest.php:18
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\initializePropertyMappingConfigurationDoesNothingIfTrustedPropertiesAreNotSet
‪initializePropertyMappingConfigurationDoesNothingIfTrustedPropertiesAreNotSet()
Definition: MvcPropertyMappingConfigurationServiceTest.php:185
‪TYPO3\CMS\Extbase\Mvc\Controller\Arguments
Definition: Arguments.php:27
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\initializePropertyMappingConfigurationReturnsEarlyIfArgumentIsUnknown
‪initializePropertyMappingConfigurationReturnsEarlyIfArgumentIsUnknown()
Definition: MvcPropertyMappingConfigurationServiceTest.php:227
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\initializePropertyMappingConfigurationSetsAllowedFields
‪initializePropertyMappingConfigurationSetsAllowedFields()
Definition: MvcPropertyMappingConfigurationServiceTest.php:284
‪TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException
Definition: InvalidArgumentForHashGenerationException.php:26
‪TYPO3\CMS\Core\Error\Http\BadRequestException
Definition: BadRequestException.php:24
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\generateTrustedPropertiesTokenThrowsExceptionInWrongCases
‪generateTrustedPropertiesTokenThrowsExceptionInWrongCases($input, $expectExceptionCode)
Definition: MvcPropertyMappingConfigurationServiceTest.php:146
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService
Definition: HashService.php:31
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\dataProviderForGenerateTrustedPropertiesToken
‪array dataProviderForGenerateTrustedPropertiesToken()
Definition: MvcPropertyMappingConfigurationServiceTest.php:40
‪TYPO3\CMS\Extbase\Object\ObjectManagerInterface
Definition: ObjectManagerInterface.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\initializePropertyMappingConfigurationSetsCreationAllowedIfIdentityPropertyIsNotSet
‪initializePropertyMappingConfigurationSetsCreationAllowedIfIdentityPropertyIsNotSet()
Definition: MvcPropertyMappingConfigurationServiceTest.php:263
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\CONFIGURATION_CREATION_ALLOWED
‪const CONFIGURATION_CREATION_ALLOWED
Definition: PersistentObjectConverter.php:52
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter
Definition: PersistentObjectConverter.php:43
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest
Definition: MvcPropertyMappingConfigurationServiceTest.php:34
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\dataProviderForGenerateTrustedPropertiesTokenWithUnallowedValues
‪array dataProviderForGenerateTrustedPropertiesTokenWithUnallowedValues()
Definition: MvcPropertyMappingConfigurationServiceTest.php:100
‪TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration
Definition: MvcPropertyMappingConfiguration.php:26
‪TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter\CONFIGURATION_MODIFICATION_ALLOWED
‪const CONFIGURATION_MODIFICATION_ALLOWED
Definition: PersistentObjectConverter.php:47
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\initializePropertyMappingConfigurationSetsModificationAllowedIfIdentityPropertyIsSet
‪initializePropertyMappingConfigurationSetsModificationAllowedIfIdentityPropertyIsSet()
Definition: MvcPropertyMappingConfigurationServiceTest.php:239
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\serializeAndHashFormFieldArrayWorks
‪serializeAndHashFormFieldArrayWorks()
Definition: MvcPropertyMappingConfigurationServiceTest.php:159
‪TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService
Definition: MvcPropertyMappingConfigurationService.php:46
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\initializePropertyMappingConfigurationSetsAllowedFieldsRecursively
‪initializePropertyMappingConfigurationSetsAllowedFieldsRecursively()
Definition: MvcPropertyMappingConfigurationServiceTest.php:300
‪TYPO3\CMS\Extbase\Mvc\Controller\Argument
Definition: Argument.php:28
‪TYPO3\CMS\Extbase\Mvc\Request
Definition: Request.php:31
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\generateTrustedPropertiesTokenGeneratesTheCorrectHashesInNormalOperation
‪generateTrustedPropertiesTokenGeneratesTheCorrectHashesInNormalOperation($input, $expected)
Definition: MvcPropertyMappingConfigurationServiceTest.php:131
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\initializePropertyMappingConfiguration
‪TYPO3 CMS Extbase Mvc Controller Arguments initializePropertyMappingConfiguration(array $trustedProperties)
Definition: MvcPropertyMappingConfigurationServiceTest.php:323