TYPO3 CMS  TYPO3_7-6
MvcPropertyMappingConfigurationServiceTest.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script belongs to the Extbase framework *
6  * *
7  * It is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU Lesser General Public License as published by the *
9  * Free Software Foundation, either version 3 of the License, or (at your *
10  * option) any later version. *
11  * *
12  * This script is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15  * General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU Lesser General Public *
18  * License along with the script. *
19  * If not, see http://www.gnu.org/licenses/lgpl.html *
20  * *
21  * The TYPO3 project - inspiring people to share! *
22  * */
23 
28 {
35  {
36  return [
37  'Simple Case - Empty' => [
38  [],
39  [],
40  ],
41  'Simple Case - Single Value' => [
42  ['field1'],
43  ['field1' => 1],
44  ],
45  'Simple Case - Two Values' => [
46  ['field1', 'field2'],
47  [
48  'field1' => 1,
49  'field2' => 1
50  ],
51  ],
52  'Recursion' => [
53  ['field1', 'field[subfield1]', 'field[subfield2]'],
54  [
55  'field1' => 1,
56  'field' => [
57  'subfield1' => 1,
58  'subfield2' => 1
59  ]
60  ],
61  ],
62  'recursion with duplicated field name' => [
63  ['field1', 'field[subfield1]', 'field[subfield2]', 'field1'],
64  [
65  'field1' => 1,
66  'field' => [
67  'subfield1' => 1,
68  'subfield2' => 1
69  ]
70  ],
71  ],
72  'Recursion with un-named fields at the end (...[]). There, they should be made explicit by increasing the counter' => [
73  ['field1', 'field[subfield1][]', 'field[subfield1][]', 'field[subfield2]'],
74  [
75  'field1' => 1,
76  'field' => [
77  'subfield1' => [
78  0 => 1,
79  1 => 1
80  ],
81  'subfield2' => 1
82  ]
83  ],
84  ],
85  ];
86  }
87 
95  {
96  return [
97  'Overriding form fields (string overridden by array) - 1' => [
98  ['field1', 'field2', 'field2[bla]', 'field2[blubb]'],
99  ],
100  'Overriding form fields (string overridden by array) - 2' => [
101  ['field1', 'field2[bla]', 'field2[bla][blubb][blubb]'],
102  ],
103  'Overriding form fields (array overridden by string) - 1' => [
104  ['field1', 'field2[bla]', 'field2[blubb]', 'field2'],
105  ],
106  'Overriding form fields (array overridden by string) - 2' => [
107  ['field1', 'field2[bla][blubb][blubb]', 'field2[bla]'],
108  ],
109  'Empty [] not as last argument' => [
110  ['field1', 'field2[][bla]'],
111  ]
112 
113  ];
114  }
115 
121  {
122  $requestHashService = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService::class, ['serializeAndHashFormFieldArray']);
123  $requestHashService->expects($this->once())->method('serializeAndHashFormFieldArray')->with($expected);
124  $requestHashService->generateTrustedPropertiesToken($input);
125  }
126 
133  {
134  $requestHashService = $this->getMock(\TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService::class, ['serializeAndHashFormFieldArray']);
135  $requestHashService->generateTrustedPropertiesToken($input);
136  }
137 
142  {
143  $formFieldArray = [
144  'bla' => [
145  'blubb' => 1,
146  'hu' => 1
147  ]
148  ];
149  $mockHash = '12345';
150 
151  $hashService = $this->getMock($this->buildAccessibleProxy(\TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService::class), ['appendHmac']);
152  $hashService->expects($this->once())->method('appendHmac')->with(serialize($formFieldArray))->will($this->returnValue(serialize($formFieldArray) . $mockHash));
153 
154  $requestHashService = $this->getMock($this->buildAccessibleProxy(\TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService::class), ['dummy']);
155  $requestHashService->_set('hashService', $hashService);
156 
157  $expected = serialize($formFieldArray) . $mockHash;
158  $actual = $requestHashService->_call('serializeAndHashFormFieldArray', $formFieldArray);
159  $this->assertEquals($expected, $actual);
160  }
161 
166  {
167  $request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)->setMethods(['getInternalArgument'])->disableOriginalConstructor()->getMock();
168  $request->expects($this->any())->method('getInternalArgument')->with('__trustedProperties')->will($this->returnValue(null));
169  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
170 
171  $requestHashService = new \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService;
172  $requestHashService->initializePropertyMappingConfigurationFromRequest($request, $arguments);
173  }
174 
179  {
180  $trustedProperties = [
181  'foo' => 1
182  ];
183  $this->initializePropertyMappingConfiguration($trustedProperties);
184  }
185 
190  {
191  $trustedProperties = [
192  'nonExistingArgument' => 1
193  ];
194  $arguments = $this->initializePropertyMappingConfiguration($trustedProperties);
195  $this->assertFalse($arguments->hasArgument('nonExistingArgument'));
196  }
197 
202  {
203  $trustedProperties = [
204  'foo' => [
205  '__identity' => 1,
206  'nested' => [
207  '__identity' => 1,
208  ]
209  ]
210  ];
211  $arguments = $this->initializePropertyMappingConfiguration($trustedProperties);
212  $propertyMappingConfiguration = $arguments->getArgument('foo')->getPropertyMappingConfiguration();
213  $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__);
214  $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__);
215  $this->assertFalse($propertyMappingConfiguration->shouldMap('someProperty'), 'Value is not FALSE at line ' . __LINE__);
216 
217  $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__);
218  $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__);
219  $this->assertFalse($propertyMappingConfiguration->forProperty('nested')->shouldMap('someProperty'), 'Value is not FALSE at line ' . __LINE__);
220  }
221 
226  {
227  $trustedProperties = [
228  'foo' => [
229  'bar' => []
230  ]
231  ];
232  $arguments = $this->initializePropertyMappingConfiguration($trustedProperties);
233  $propertyMappingConfiguration = $arguments->getArgument('foo')->getPropertyMappingConfiguration();
234  $this->assertNull($propertyMappingConfiguration->getConfigurationValue(\TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED));
235  $this->assertTrue($propertyMappingConfiguration->getConfigurationValue(\TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED));
236  $this->assertFalse($propertyMappingConfiguration->shouldMap('someProperty'));
237 
238  $this->assertNull($propertyMappingConfiguration->forProperty('bar')->getConfigurationValue(\TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_MODIFICATION_ALLOWED));
239  $this->assertTrue($propertyMappingConfiguration->forProperty('bar')->getConfigurationValue(\TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::class, \TYPO3\CMS\Extbase\Property\TypeConverter\PersistentObjectConverter::CONFIGURATION_CREATION_ALLOWED));
240  $this->assertFalse($propertyMappingConfiguration->forProperty('bar')->shouldMap('someProperty'));
241  }
242 
247  {
248  $trustedProperties = [
249  'foo' => [
250  'bar' => 1
251  ]
252  ];
253  $arguments = $this->initializePropertyMappingConfiguration($trustedProperties);
254  $propertyMappingConfiguration = $arguments->getArgument('foo')->getPropertyMappingConfiguration();
255  $this->assertFalse($propertyMappingConfiguration->shouldMap('someProperty'));
256  $this->assertTrue($propertyMappingConfiguration->shouldMap('bar'));
257  }
258 
263  {
264  $trustedProperties = [
265  'foo' => [
266  'bar' => [
267  'foo' => 1
268  ]
269  ]
270  ];
271  $arguments = $this->initializePropertyMappingConfiguration($trustedProperties);
272  $propertyMappingConfiguration = $arguments->getArgument('foo')->getPropertyMappingConfiguration();
273  $this->assertFalse($propertyMappingConfiguration->shouldMap('someProperty'));
274  $this->assertTrue($propertyMappingConfiguration->shouldMap('bar'));
275  $this->assertTrue($propertyMappingConfiguration->forProperty('bar')->shouldMap('foo'));
276  }
277 
285  protected function initializePropertyMappingConfiguration(array $trustedProperties)
286  {
287  $request = $this->getMockBuilder(\TYPO3\CMS\Extbase\Mvc\Request::class)->setMethods(['getInternalArgument'])->disableOriginalConstructor()->getMock();
288  $request->expects($this->any())->method('getInternalArgument')->with('__trustedProperties')->will($this->returnValue('fooTrustedProperties'));
289 
290  $mockHashService = $this->getMock(\TYPO3\CMS\Extbase\Security\Cryptography\HashService::class, ['validateAndStripHmac']);
291  $mockHashService->expects($this->once())->method('validateAndStripHmac')->with('fooTrustedProperties')->will($this->returnValue(serialize($trustedProperties)));
292 
293  $requestHashService = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService::class, ['dummy']);
294  $requestHashService->_set('hashService', $mockHashService);
295 
296  $mockObjectManager = $this->getMock(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface::class);
297  $mockArgument = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class, ['getName'], [], '', false);
298 
299  $propertyMappingConfiguration = new \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration();
300 
301  $mockArgument->_set('propertyMappingConfiguration', $propertyMappingConfiguration);
302  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('foo'));
303  $mockObjectManager->expects($this->once())->method('get')->with(\TYPO3\CMS\Extbase\Mvc\Controller\Argument::class)->will($this->returnValue($mockArgument));
304 
305  $arguments = $this->getAccessibleMock(\TYPO3\CMS\Extbase\Mvc\Controller\Arguments::class, ['dummy']);
306  $arguments->_set('objectManager', $mockObjectManager);
307  $arguments->addNewArgument('foo');
308 
309  $requestHashService->initializePropertyMappingConfigurationFromRequest($request, $arguments);
310 
311  return $arguments;
312  }
313 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)