TYPO3 CMS  TYPO3_7-6
HookTest.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 
21 
26 {
27  const VALUE_PageId = 89;
28  const VALUE_ContentId = 297;
29  const TABLE_Content = 'tt_content';
30  const TABLE_Hotel = 'tx_irretutorial_1nff_hotel';
31  const TABLE_Category = 'sys_category';
32  const FIELD_ContentHotel = 'tx_irretutorial_1nff_hotels';
33  const FIELD_Categories = 'categories';
34 
38  protected $hookFixture;
39 
43  protected $scenarioDataSetDirectory = 'typo3/sysext/core/Tests/Functional/DataHandling/DataHandler/DataSet/';
44 
45  protected function setUp()
46  {
47  parent::setUp();
48  $this->importScenarioDataSet('LiveDefaultPages');
49  $this->importScenarioDataSet('LiveDefaultElements');
50  $this->backendUser->workspace = 0;
51 
52  $this->hookFixture = GeneralUtility::makeInstance(HookFixture::class);
53  $this->hookFixture->purge();
54  $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][__CLASS__] = HookFixture::class;
55  $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'][__CLASS__] = HookFixture::class;
56  }
57 
58  protected function tearDown()
59  {
60  parent::tearDown();
61 
62  unset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][__CLASS__]);
63  unset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'][__CLASS__]);
64  unset($this->hookFixture);
65  }
66 
71  {
72  $newTableIds = $this->actionService->createNewRecord(
73  self::TABLE_Content,
74  self::VALUE_PageId,
75  ['header' => 'Testing #1']
76  );
77  $this->recordIds['newContentId'] = $newTableIds[self::TABLE_Content][0];
78 
80  'processDatamap_beforeStart',
81  'processDatamap_afterAllOperations'
82  ], 1);
83 
85  'processDatamap_preProcessFieldArray',
86  'processDatamap_postProcessFieldArray',
87  'processDatamap_afterDatabaseOperations',
88  ], [
89  [
90  'table' => self::TABLE_Content,
91  'fieldArray' => [ 'header' => 'Testing #1', 'pid' => self::VALUE_PageId ]
92  ]
93  ]);
94  }
95 
100  {
101  $this->actionService->modifyRecord(
102  self::TABLE_Content,
103  self::VALUE_ContentId,
104  ['header' => 'Testing #1']
105  );
106 
108  'processDatamap_beforeStart',
109  'processDatamap_afterAllOperations'
110  ], 1);
111 
113  'processDatamap_preProcessFieldArray',
114  'processDatamap_postProcessFieldArray',
115  'processDatamap_afterDatabaseOperations',
116  ], [
117  [
118  'table' => self::TABLE_Content,
119  'fieldArray' => [ 'header' => 'Testing #1' ]
120  ]
121  ]);
122  }
123 
128  {
129  $contentNewId = StringUtility::getUniqueId('NEW');
130  $hotelNewId = StringUtility::getUniqueId('NEW');
131  $categoryNewId = StringUtility::getUniqueId('NEW');
132 
133  $this->actionService->modifyRecords(
134  self::VALUE_PageId,
135  [
136  self::TABLE_Content => [
137  'uid' => $contentNewId,
138  'header' => 'Testing #1',
139  self::FIELD_ContentHotel => $hotelNewId,
140  self::FIELD_Categories => $categoryNewId,
141  ],
142  self::TABLE_Hotel => [
143  'uid' => $hotelNewId,
144  'title' => 'Hotel #1',
145  ],
146  self::TABLE_Category => [
147  'uid' => $categoryNewId,
148  'title' => 'Category #1',
149  ],
150  ]
151  );
152 
154  'processDatamap_beforeStart',
155  'processDatamap_afterAllOperations'
156  ], 1);
157 
159  'processDatamap_preProcessFieldArray',
160  [
161  [
162  'table' => self::TABLE_Content,
163  'fieldArray' => [
164  'header' => 'Testing #1',
165  self::FIELD_ContentHotel => $hotelNewId,
166  self::FIELD_Categories => $categoryNewId,
167  ],
168  ],
169  [
170  'table' => self::TABLE_Hotel,
171  'fieldArray' => [ 'title' => 'Hotel #1' ],
172  ],
173  [
174  'table' => self::TABLE_Category,
175  'fieldArray' => [ 'title' => 'Category #1' ],
176  ],
177  ]
178  );
179 
181  'processDatamap_postProcessFieldArray',
182  [
183  [
184  'table' => self::TABLE_Content,
185  'fieldArray' => [ 'header' => 'Testing #1' ],
186  ],
187  [
188  'table' => self::TABLE_Hotel,
189  'fieldArray' => [ 'title' => 'Hotel #1' ],
190  ],
191  [
192  'table' => self::TABLE_Category,
193  'fieldArray' => [ 'title' => 'Category #1' ],
194  ],
195  ]
196  );
197 
199  'processDatamap_afterDatabaseOperations',
200  [
201  [
202  'table' => self::TABLE_Content,
203  'fieldArray' => [
204  'header' => 'Testing #1',
205  self::FIELD_ContentHotel => 1,
206  self::FIELD_Categories => 1,
207  ],
208  ],
209  [
210  'table' => self::TABLE_Hotel,
211  'fieldArray' => [ 'title' => 'Hotel #1' ],
212  ],
213  [
214  'table' => self::TABLE_Category,
215  'fieldArray' => [ 'title' => 'Category #1' ],
216  ],
217  ]
218  );
219  }
220 
225  {
226  $this->actionService->modifyRecord(
227  self::TABLE_Content,
228  self::VALUE_ContentId,
229  [
230  'header' => 'Testing #1',
231  self::FIELD_ContentHotel => '3,4,5',
232  self::FIELD_Categories => '28,29,30',
233  ]
234  );
235 
237  'processDatamap_beforeStart',
238  'processDatamap_afterAllOperations'
239  ], 1);
240 
242  'processDatamap_preProcessFieldArray',
243  [
244  [
245  'table' => self::TABLE_Content,
246  'fieldArray' => [
247  'header' => 'Testing #1',
248  self::FIELD_ContentHotel => '3,4,5',
249  self::FIELD_Categories => '28,29,30',
250  ]
251  ]
252  ]
253  );
254 
256  'processDatamap_postProcessFieldArray',
257  'processDatamap_afterDatabaseOperations',
258  ], [
259  [
260  'table' => self::TABLE_Content,
261  'fieldArray' => [
262  'header' => 'Testing #1',
263  self::FIELD_ContentHotel => 3,
264  self::FIELD_Categories => 3,
265  ]
266  ]
267  ]);
268  }
269 
274  protected function assertHookInvocationsCount(array $methodNames, $count)
275  {
276  $message = 'Unexpected invocations of method "%s"';
277  foreach ($methodNames as $methodName) {
278  $invocations = $this->hookFixture->findInvocationsByMethodName($methodName);
279  $this->assertCount(
280  $count,
281  $invocations,
282  sprintf($message, $methodName)
283  );
284  }
285  }
286 
291  protected function assertHookInvocationsPayload(array $methodNames, array $assertions)
292  {
293  foreach ($methodNames as $methodName) {
294  $this->assertHookInvocationPayload($methodName, $assertions);
295  }
296  }
297 
302  protected function assertHookInvocationPayload($methodName, array $assertions)
303  {
304  $message = 'Unexpected hook payload amount found for method "%s"';
305  $invocations = $this->hookFixture->findInvocationsByMethodName($methodName);
306  $this->assertNotNull($invocations);
307 
308  foreach ($assertions as $assertion) {
309  $indexes = $this->findAllArrayValuesInHaystack($invocations, $assertion);
310  $this->assertCount(
311  1,
312  $indexes,
313  sprintf($message, $methodName)
314  );
315  $index = $indexes[0];
316  unset($invocations[$index]);
317  }
318  }
319 
325  protected function findAllArrayValuesInHaystack(array $haystack, array $assertion)
326  {
327  $found = [];
328  foreach ($haystack as $index => $item) {
329  if ($this->equals($assertion, $item)) {
330  $found[] = $index;
331  }
332  }
333  return $found;
334  }
335 
341  protected function equals(array $left, array $right)
342  {
343  foreach ($left as $key => $leftValue) {
344  $rightValue = null;
345  if (isset($right[$key])) {
346  $rightValue = $right[$key];
347  }
348  if (!is_array($leftValue) && (string)$leftValue !== (string)$rightValue) {
349  return false;
350  } elseif (is_array($leftValue)) {
351  if (!$this->equals($leftValue, $rightValue)) {
352  return false;
353  }
354  }
355  }
356  return true;
357  }
358 }
findAllArrayValuesInHaystack(array $haystack, array $assertion)
Definition: HookTest.php:325
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
assertHookInvocationsPayload(array $methodNames, array $assertions)
Definition: HookTest.php:291