‪TYPO3CMS  9.5
MvcPropertyMappingConfigurationServiceTest.php
Go to the documentation of this file.
1 <?php
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 
20 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
21 
26 {
33  {
34  return [
35  'Simple Case - Empty' => [
36  [],
37  [],
38  ],
39  'Simple Case - Single Value' => [
40  ['field1'],
41  ['field1' => 1],
42  ],
43  'Simple Case - Two Values' => [
44  ['field1', 'field2'],
45  [
46  'field1' => 1,
47  'field2' => 1
48  ],
49  ],
50  'Recursion' => [
51  ['field1', 'field[subfield1]', 'field[subfield2]'],
52  [
53  'field1' => 1,
54  'field' => [
55  'subfield1' => 1,
56  'subfield2' => 1
57  ]
58  ],
59  ],
60  'recursion with duplicated field name' => [
61  ['field1', 'field[subfield1]', 'field[subfield2]', 'field1'],
62  [
63  'field1' => 1,
64  'field' => [
65  'subfield1' => 1,
66  'subfield2' => 1
67  ]
68  ],
69  ],
70  'Recursion with un-named fields at the end (...[]). There, they should be made explicit by increasing the counter' => [
71  ['field1', 'field[subfield1][]', 'field[subfield1][]', 'field[subfield2]'],
72  [
73  'field1' => 1,
74  'field' => [
75  'subfield1' => [
76  0 => 1,
77  1 => 1
78  ],
79  'subfield2' => 1
80  ]
81  ],
82  ],
83  ];
84  }
85 
93  {
94  return [
95  'Overriding form fields (string overridden by array) - 1' => [
96  ['field1', 'field2', 'field2[bla]', 'field2[blubb]'],
97  1255072196
98  ],
99  'Overriding form fields (string overridden by array) - 2' => [
100  ['field1', 'field2[bla]', 'field2[bla][blubb][blubb]'],
101  1255072196
102  ],
103  'Overriding form fields (array overridden by string) - 1' => [
104  ['field1', 'field2[bla]', 'field2[blubb]', 'field2'],
105  1255072587
106  ],
107  'Overriding form fields (array overridden by string) - 2' => [
108  ['field1', 'field2[bla][blubb][blubb]', 'field2[bla]'],
109  1255072587
110  ],
111  'Empty [] not as last argument' => [
112  ['field1', 'field2[][bla]'],
113  1255072832
114  ]
115 
116  ];
117  }
118 
124  {
125  $requestHashService = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService::class)
126  ->setMethods(['serializeAndHashFormFieldArray'])
127  ->getMock();
128  $requestHashService->expects($this->once())->method('serializeAndHashFormFieldArray')->with($expected);
129  $requestHashService->generateTrustedPropertiesToken($input);
130  }
131 
138  public function ‪generateTrustedPropertiesTokenThrowsExceptionInWrongCases($input, $expectExceptionCode)
139  {
140  $this->expectException(InvalidArgumentForHashGenerationException::class);
141  $this->expectExceptionCode($expectExceptionCode);
142  $requestHashService = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService::class)
143  ->setMethods(['serializeAndHashFormFieldArray'])
144  ->getMock();
145  $requestHashService->generateTrustedPropertiesToken($input);
146  }
147 
152  {
153  $formFieldArray = [
154  'bla' => [
155  'blubb' => 1,
156  'hu' => 1
157  ]
158  ];
159  $mockHash = '12345';
160 
161  $hashService = $this->getMockBuilder($this->buildAccessibleProxy(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService::class))
162  ->setMethods(['appendHmac'])
163  ->getMock();
164  $hashService->expects($this->once())->method('appendHmac')->with(serialize($formFieldArray))->will($this->returnValue(serialize($formFieldArray) . $mockHash));
165 
166  $requestHashService = $this->getMockBuilder($this->buildAccessibleProxy(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService::class))
167  ->setMethods(['dummy'])
168  ->getMock();
169  $requestHashService->_set('hashService', $hashService);
170 
171  $expected = serialize($formFieldArray) . $mockHash;
172  $actual = $requestHashService->_call('serializeAndHashFormFieldArray', $formFieldArray);
173  $this->assertEquals($expected, $actual);
174  }
175 
180  {
181  $request = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Request::class)->setMethods(['getInternalArgument'])->disableOriginalConstructor()->getMock();
182  $request->expects($this->any())->method('getInternalArgument')->with('__trustedProperties')->will($this->returnValue(null));
183  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
184 
185  $requestHashService = new \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService;
186  $requestHashService->initializePropertyMappingConfigurationFromRequest($request, $arguments);
187  }
188 
193  {
194  $this->expectException(BadRequestException::class);
195  $this->expectExceptionCode(1581862822);
196 
197  $request = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Request::class)->setMethods(['getInternalArgument'])->disableOriginalConstructor()->getMock();
198  $request->expects(self::any())->method('getInternalArgument')->with('__trustedProperties')->willReturn('string with less than 40 characters');
199  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
200 
201  $hashService = new \TYPO3\CMS\Extbase\Security\Cryptography\HashService();
202  $requestHashService = new ‪MvcPropertyMappingConfigurationService;
203  $requestHashService->‪injectHashService($hashService);
204  $requestHashService->initializePropertyMappingConfigurationFromRequest($request, $arguments);
205  }
206 
211  {
212  $trustedProperties = [
213  'foo' => 1
214  ];
215  $this->‪initializePropertyMappingConfiguration($trustedProperties);
216  }
217 
222  {
223  $trustedProperties = [
224  'nonExistingArgument' => 1
225  ];
226  $arguments = $this->‪initializePropertyMappingConfiguration($trustedProperties);
227  $this->assertFalse($arguments->hasArgument('nonExistingArgument'));
228  }
229 
234  {
235  $trustedProperties = [
236  'foo' => [
237  '__identity' => 1,
238  'nested' => [
239  '__identity' => 1,
240  ]
241  ]
242  ];
243  $arguments = $this->‪initializePropertyMappingConfiguration($trustedProperties);
244  $propertyMappingConfiguration = $arguments->getArgument('foo')->getPropertyMappingConfiguration();
245  $this->assertTrue($propertyMappingConfiguration->getConfigurationValue(\‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::class, \‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED), 'ConfigurationValue is not CONFIGURATION_MODIFICATION_ALLOWED at line ' . __LINE__);
246  $this->assertNull($propertyMappingConfiguration->getConfigurationValue(\‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::class, \‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED), 'ConfigurationValue is not NULL at line ' . __LINE__);
247  $this->assertFalse($propertyMappingConfiguration->shouldMap('someProperty'), 'Value is not FALSE at line ' . __LINE__);
248 
249  $this->assertTrue($propertyMappingConfiguration->forProperty('nested')->getConfigurationValue(\‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::class, \‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED), 'ConfigurationValue is not CONFIGURATION_MODIFICATION_ALLOWED at line ' . __LINE__);
250  $this->assertNull($propertyMappingConfiguration->forProperty('nested')->getConfigurationValue(\‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::class, \‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED), 'ConfigurationValue is not NULL at line ' . __LINE__);
251  $this->assertFalse($propertyMappingConfiguration->forProperty('nested')->shouldMap('someProperty'), 'Value is not FALSE at line ' . __LINE__);
252  }
253 
258  {
259  $trustedProperties = [
260  'foo' => [
261  'bar' => []
262  ]
263  ];
264  $arguments = $this->‪initializePropertyMappingConfiguration($trustedProperties);
265  $propertyMappingConfiguration = $arguments->getArgument('foo')->getPropertyMappingConfiguration();
266  $this->assertNull($propertyMappingConfiguration->getConfigurationValue(\‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::class, \‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED));
267  $this->assertTrue($propertyMappingConfiguration->getConfigurationValue(\‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::class, \‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED));
268  $this->assertFalse($propertyMappingConfiguration->shouldMap('someProperty'));
269 
270  $this->assertNull($propertyMappingConfiguration->forProperty('bar')->getConfigurationValue(\‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::class, \‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED));
271  $this->assertTrue($propertyMappingConfiguration->forProperty('bar')->getConfigurationValue(\‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::class, \‪TYPO3\CMS\‪Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED));
272  $this->assertFalse($propertyMappingConfiguration->forProperty('bar')->shouldMap('someProperty'));
273  }
274 
279  {
280  $trustedProperties = [
281  'foo' => [
282  'bar' => 1
283  ]
284  ];
285  $arguments = $this->‪initializePropertyMappingConfiguration($trustedProperties);
286  $propertyMappingConfiguration = $arguments->getArgument('foo')->getPropertyMappingConfiguration();
287  $this->assertFalse($propertyMappingConfiguration->shouldMap('someProperty'));
288  $this->assertTrue($propertyMappingConfiguration->shouldMap('bar'));
289  }
290 
295  {
296  $trustedProperties = [
297  'foo' => [
298  'bar' => [
299  'foo' => 1
300  ]
301  ]
302  ];
303  $arguments = $this->‪initializePropertyMappingConfiguration($trustedProperties);
304  $propertyMappingConfiguration = $arguments->getArgument('foo')->getPropertyMappingConfiguration();
305  $this->assertFalse($propertyMappingConfiguration->shouldMap('someProperty'));
306  $this->assertTrue($propertyMappingConfiguration->shouldMap('bar'));
307  $this->assertTrue($propertyMappingConfiguration->forProperty('bar')->shouldMap('foo'));
308  }
309 
317  protected function ‪initializePropertyMappingConfiguration(array $trustedProperties)
318  {
319  $request = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Mvc\Request::class)->setMethods(['getInternalArgument'])->disableOriginalConstructor()->getMock();
320  $request->expects($this->any())->method('getInternalArgument')->with('__trustedProperties')->will($this->returnValue('fooTrustedProperties'));
321 
322  $mockHashService = $this->getMockBuilder(\‪TYPO3\CMS\‪Extbase\Security\Cryptography\HashService::class)
323  ->setMethods(['validateAndStripHmac'])
324  ->getMock();
325  $mockHashService->expects($this->once())->method('validateAndStripHmac')->with('fooTrustedProperties')->will($this->returnValue(serialize($trustedProperties)));
326 
327  $requestHashService = $this->getAccessibleMock(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService::class, ['dummy']);
328  $requestHashService->_set('hashService', $mockHashService);
329 
330  $mockObjectManager = $this->createMock(\‪TYPO3\CMS\‪Extbase\Object\ObjectManagerInterface::class);
331  $mockArgument = $this->getAccessibleMock(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class, ['getName'], [], '', false);
332 
333  $propertyMappingConfiguration = new \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration();
334 
335  $mockArgument->_set('propertyMappingConfiguration', $propertyMappingConfiguration);
336  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('foo'));
337  $mockObjectManager->expects($this->once())->method('get')->with(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Argument::class)->will($this->returnValue($mockArgument));
338 
339  $arguments = $this->getAccessibleMock(\‪TYPO3\CMS\‪Extbase\Mvc\Controller\Arguments::class, ['dummy']);
340  $arguments->_set('objectManager', $mockObjectManager);
341  $arguments->addNewArgument('foo');
342 
343  $requestHashService->initializePropertyMappingConfigurationFromRequest($request, $arguments);
344 
345  return $arguments;
346  }
347 }
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\initializePropertyMappingConfigurationThrowsBadRequestExceptionOnInvalidHmac
‪initializePropertyMappingConfigurationThrowsBadRequestExceptionOnInvalidHmac()
Definition: MvcPropertyMappingConfigurationServiceTest.php:192
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\initializePropertyMappingConfigurationReturnsEarlyIfNoTrustedPropertiesAreSet
‪initializePropertyMappingConfigurationReturnsEarlyIfNoTrustedPropertiesAreSet()
Definition: MvcPropertyMappingConfigurationServiceTest.php:210
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller
Definition: AbstractControllerTest.php:2
‪TYPO3
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\initializePropertyMappingConfigurationDoesNothingIfTrustedPropertiesAreNotSet
‪initializePropertyMappingConfigurationDoesNothingIfTrustedPropertiesAreNotSet()
Definition: MvcPropertyMappingConfigurationServiceTest.php:179
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\initializePropertyMappingConfigurationReturnsEarlyIfArgumentIsUnknown
‪initializePropertyMappingConfigurationReturnsEarlyIfArgumentIsUnknown()
Definition: MvcPropertyMappingConfigurationServiceTest.php:221
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\initializePropertyMappingConfigurationSetsAllowedFields
‪initializePropertyMappingConfigurationSetsAllowedFields()
Definition: MvcPropertyMappingConfigurationServiceTest.php:278
‪TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException
Definition: InvalidArgumentForHashGenerationException.php:21
‪TYPO3\CMS\Core\Error\Http\BadRequestException
Definition: BadRequestException.php:21
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\generateTrustedPropertiesTokenThrowsExceptionInWrongCases
‪generateTrustedPropertiesTokenThrowsExceptionInWrongCases($input, $expectExceptionCode)
Definition: MvcPropertyMappingConfigurationServiceTest.php:138
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\initializePropertyMappingConfigurationSetsCreationAllowedIfIdentityPropertyIsNotSet
‪initializePropertyMappingConfigurationSetsCreationAllowedIfIdentityPropertyIsNotSet()
Definition: MvcPropertyMappingConfigurationServiceTest.php:257
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest
Definition: MvcPropertyMappingConfigurationServiceTest.php:26
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\dataProviderForgenerateTrustedPropertiesTokenWithUnallowedValues
‪array dataProviderForgenerateTrustedPropertiesTokenWithUnallowedValues()
Definition: MvcPropertyMappingConfigurationServiceTest.php:92
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\initializePropertyMappingConfigurationSetsModificationAllowedIfIdentityPropertyIsSet
‪initializePropertyMappingConfigurationSetsModificationAllowedIfIdentityPropertyIsSet()
Definition: MvcPropertyMappingConfigurationServiceTest.php:233
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\dataProviderForgenerateTrustedPropertiesToken
‪array dataProviderForgenerateTrustedPropertiesToken()
Definition: MvcPropertyMappingConfigurationServiceTest.php:32
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\serializeAndHashFormFieldArrayWorks
‪serializeAndHashFormFieldArrayWorks()
Definition: MvcPropertyMappingConfigurationServiceTest.php:151
‪TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService
Definition: MvcPropertyMappingConfigurationService.php:40
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\initializePropertyMappingConfigurationSetsAllowedFieldsRecursively
‪initializePropertyMappingConfigurationSetsAllowedFieldsRecursively()
Definition: MvcPropertyMappingConfigurationServiceTest.php:294
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\generateTrustedPropertiesTokenGeneratesTheCorrectHashesInNormalOperation
‪generateTrustedPropertiesTokenGeneratesTheCorrectHashesInNormalOperation($input, $expected)
Definition: MvcPropertyMappingConfigurationServiceTest.php:123
‪TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService\injectHashService
‪injectHashService(\TYPO3\CMS\Extbase\Security\Cryptography\HashService $hashService)
Definition: MvcPropertyMappingConfigurationService.php:50
‪TYPO3\CMS\Extbase\Tests\Unit\Mvc\Controller\MvcPropertyMappingConfigurationServiceTest\initializePropertyMappingConfiguration
‪TYPO3 CMS Extbase Mvc Controller Arguments initializePropertyMappingConfiguration(array $trustedProperties)
Definition: MvcPropertyMappingConfigurationServiceTest.php:317