‪TYPO3CMS  10.4
HookTest.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
24 
29 {
30  const ‪VALUE_PageId = 89;
31  const ‪VALUE_ContentId = 297;
32  const ‪TABLE_Content = 'tt_content';
33  const ‪TABLE_Hotel = 'tx_irretutorial_1nff_hotel';
34  const ‪TABLE_Category = 'sys_category';
35  const ‪FIELD_ContentHotel = 'tx_irretutorial_1nff_hotels';
36  const ‪FIELD_Categories = 'categories';
37 
41  protected ‪$assertCleanReferenceIndex = false;
42 
46  protected ‪$hookFixture;
47 
51  protected ‪$scenarioDataSetDirectory = 'typo3/sysext/core/Tests/Functional/DataHandling/DataHandler/DataSet/';
52 
53  protected function ‪setUp(): void
54  {
55  parent::setUp();
56  $this->‪importScenarioDataSet('LiveDefaultPages');
57  $this->‪importScenarioDataSet('LiveDefaultElements');
58  $this->backendUser->workspace = 0;
59 
60  $this->hookFixture = GeneralUtility::makeInstance(HookFixture::class);
61  $this->hookFixture->purge();
62  ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][__CLASS__] = HookFixture::class;
63  ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'][__CLASS__] = HookFixture::class;
64  }
65 
66  protected function ‪tearDown(): void
67  {
68  parent::tearDown();
69 
70  unset(‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][__CLASS__]);
71  unset(‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processCmdmapClass'][__CLASS__]);
72  unset($this->hookFixture);
73  }
74 
78  public function ‪hooksAreExecutedForNewRecords()
79  {
80  $newTableIds = $this->actionService->createNewRecord(
81  self::TABLE_Content,
82  self::VALUE_PageId,
83  ['header' => 'Testing #1']
84  );
85  $this->recordIds['newContentId'] = $newTableIds[‪self::TABLE_Content][0];
86 
88  'processDatamap_beforeStart',
89  'processDatamap_afterAllOperations'
90  ], 1);
91 
93  'processDatamap_preProcessFieldArray',
94  'processDatamap_postProcessFieldArray',
95  'processDatamap_afterDatabaseOperations',
96  ], [
97  [
98  'table' => self::TABLE_Content,
99  'fieldArray' => [ 'header' => 'Testing #1', 'pid' => self::VALUE_PageId ]
100  ]
101  ]);
102  }
103 
107  public function ‪hooksAreExecutedForExistingRecords()
108  {
109  $this->actionService->modifyRecord(
110  self::TABLE_Content,
111  self::VALUE_ContentId,
112  ['header' => 'Testing #1']
113  );
114 
116  'processDatamap_beforeStart',
117  'processDatamap_afterAllOperations'
118  ], 1);
119 
121  'processDatamap_preProcessFieldArray',
122  'processDatamap_postProcessFieldArray',
123  'processDatamap_afterDatabaseOperations',
124  ], [
125  [
126  'table' => self::TABLE_Content,
127  'fieldArray' => [ 'header' => 'Testing #1' ]
128  ]
129  ]);
130  }
131 
135  public function ‪hooksAreExecutedForNewRelations()
136  {
137  $contentNewId = ‪StringUtility::getUniqueId('NEW');
138  $hotelNewId = ‪StringUtility::getUniqueId('NEW');
139  $categoryNewId = ‪StringUtility::getUniqueId('NEW');
140 
141  $this->actionService->modifyRecords(
142  self::VALUE_PageId,
143  [
144  self::TABLE_Content => [
145  'uid' => $contentNewId,
146  'header' => 'Testing #1',
147  self::FIELD_ContentHotel => $hotelNewId,
148  self::FIELD_Categories => $categoryNewId,
149  ],
150  self::TABLE_Hotel => [
151  'uid' => $hotelNewId,
152  'title' => 'Hotel #1',
153  ],
154  self::TABLE_Category => [
155  'uid' => $categoryNewId,
156  'title' => 'Category #1',
157  ],
158  ]
159  );
160 
162  'processDatamap_beforeStart',
163  'processDatamap_afterAllOperations'
164  ], 1);
165 
167  'processDatamap_preProcessFieldArray',
168  [
169  [
170  'table' => self::TABLE_Content,
171  'fieldArray' => [
172  'header' => 'Testing #1',
173  self::FIELD_ContentHotel => $hotelNewId,
174  self::FIELD_Categories => $categoryNewId,
175  ],
176  ],
177  [
178  'table' => self::TABLE_Hotel,
179  'fieldArray' => [ 'title' => 'Hotel #1' ],
180  ],
181  [
182  'table' => self::TABLE_Category,
183  'fieldArray' => [ 'title' => 'Category #1' ],
184  ],
185  ]
186  );
187 
189  'processDatamap_postProcessFieldArray',
190  [
191  [
192  'table' => self::TABLE_Content,
193  'fieldArray' => [ 'header' => 'Testing #1' ],
194  ],
195  [
196  'table' => self::TABLE_Hotel,
197  'fieldArray' => [ 'title' => 'Hotel #1' ],
198  ],
199  [
200  'table' => self::TABLE_Category,
201  'fieldArray' => [ 'title' => 'Category #1' ],
202  ],
203  ]
204  );
205 
207  'processDatamap_afterDatabaseOperations',
208  [
209  [
210  'table' => self::TABLE_Content,
211  'fieldArray' => [
212  'header' => 'Testing #1',
213  self::FIELD_ContentHotel => 1,
214  self::FIELD_Categories => 1,
215  ],
216  ],
217  [
218  'table' => self::TABLE_Hotel,
219  'fieldArray' => [ 'title' => 'Hotel #1' ],
220  ],
221  [
222  'table' => self::TABLE_Category,
223  'fieldArray' => [ 'title' => 'Category #1' ],
224  ],
225  ]
226  );
227  }
228 
233  {
234  $this->actionService->modifyRecord(
235  self::TABLE_Content,
236  self::VALUE_ContentId,
237  [
238  'header' => 'Testing #1',
239  self::FIELD_ContentHotel => '3,4,5',
240  self::FIELD_Categories => '28,29,30',
241  ]
242  );
243 
245  'processDatamap_beforeStart',
246  'processDatamap_afterAllOperations'
247  ], 1);
248 
250  'processDatamap_preProcessFieldArray',
251  [
252  [
253  'table' => self::TABLE_Content,
254  'fieldArray' => [
255  'header' => 'Testing #1',
256  self::FIELD_ContentHotel => '3,4,5',
257  self::FIELD_Categories => '28,29,30',
258  ]
259  ]
260  ]
261  );
262 
264  'processDatamap_postProcessFieldArray',
265  'processDatamap_afterDatabaseOperations',
266  ], [
267  [
268  'table' => self::TABLE_Content,
269  'fieldArray' => [
270  'header' => 'Testing #1',
271  self::FIELD_ContentHotel => 3,
272  self::FIELD_Categories => 3,
273  ]
274  ]
275  ]);
276  }
277 
282  protected function ‪assertHookInvocationsCount(array $methodNames, int $count)
283  {
284  $message = 'Unexpected invocations of method "%s"';
285  foreach ($methodNames as $methodName) {
286  $invocations = $this->hookFixture->findInvocationsByMethodName($methodName);
287  self::assertCount(
288  $count,
289  $invocations,
290  sprintf($message, $methodName)
291  );
292  }
293  }
294 
299  protected function ‪assertHookInvocationsPayload(array $methodNames, array $assertions)
300  {
301  foreach ($methodNames as $methodName) {
302  $this->‪assertHookInvocationPayload($methodName, $assertions);
303  }
304  }
305 
310  protected function ‪assertHookInvocationPayload(string $methodName, array $assertions)
311  {
312  $message = 'Unexpected hook payload amount found for method "%s"';
313  $invocations = $this->hookFixture->findInvocationsByMethodName($methodName);
314  self::assertNotNull($invocations);
315 
316  foreach ($assertions as $assertion) {
317  $indexes = $this->‪findAllArrayValuesInHaystack($invocations, $assertion);
318  self::assertCount(
319  1,
320  $indexes,
321  sprintf($message, $methodName)
322  );
323  $index = $indexes[0];
324  unset($invocations[$index]);
325  }
326  }
327 
333  protected function ‪findAllArrayValuesInHaystack(array $haystack, array $assertion)
334  {
335  $found = [];
336  foreach ($haystack as $index => $item) {
337  if ($this->‪equals($assertion, $item)) {
338  $found[] = $index;
339  }
340  }
341  return $found;
342  }
343 
349  protected function ‪equals(array $left, array $right)
350  {
351  foreach ($left as $key => $leftValue) {
352  $rightValue = $right[$key] ?? null;
353  if (!is_array($leftValue) && (string)$leftValue !== (string)$rightValue) {
354  return false;
355  }
356  if (is_array($leftValue)) {
357  if (!$this->‪equals($leftValue, $rightValue)) {
358  return false;
359  }
360  }
361  }
362  return true;
363  }
364 }
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler
Definition: DefaultValuesTest.php:18
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest
Definition: HookTest.php:29
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\FIELD_ContentHotel
‪const FIELD_ContentHotel
Definition: HookTest.php:35
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\TABLE_Hotel
‪const TABLE_Hotel
Definition: HookTest.php:33
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\FIELD_Categories
‪const FIELD_Categories
Definition: HookTest.php:36
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\equals
‪bool equals(array $left, array $right)
Definition: HookTest.php:346
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase
Definition: AbstractDataHandlerActionTestCase.php:37
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\setUp
‪setUp()
Definition: HookTest.php:50
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\$assertCleanReferenceIndex
‪bool $assertCleanReferenceIndex
Definition: HookTest.php:40
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\TABLE_Content
‪const TABLE_Content
Definition: HookTest.php:32
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\hooksAreExecutedForExistingRecords
‪hooksAreExecutedForExistingRecords()
Definition: HookTest.php:104
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\hooksAreExecutedForNewRelations
‪hooksAreExecutedForNewRelations()
Definition: HookTest.php:132
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\Fixtures\HookFixture
Definition: HookFixture.php:27
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\assertHookInvocationsPayload
‪assertHookInvocationsPayload(array $methodNames, array $assertions)
Definition: HookTest.php:296
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\$hookFixture
‪HookFixture $hookFixture
Definition: HookTest.php:44
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\VALUE_ContentId
‪const VALUE_ContentId
Definition: HookTest.php:31
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\TABLE_Category
‪const TABLE_Category
Definition: HookTest.php:34
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\hooksAreExecutedForNewRecords
‪hooksAreExecutedForNewRecords()
Definition: HookTest.php:75
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\assertHookInvocationsCount
‪assertHookInvocationsCount(array $methodNames, int $count)
Definition: HookTest.php:279
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\VALUE_PageId
‪const VALUE_PageId
Definition: HookTest.php:30
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\findAllArrayValuesInHaystack
‪int[] findAllArrayValuesInHaystack(array $haystack, array $assertion)
Definition: HookTest.php:330
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\hooksAreExecutedForExistingRelations
‪hooksAreExecutedForExistingRelations()
Definition: HookTest.php:229
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\tearDown
‪tearDown()
Definition: HookTest.php:63
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\importScenarioDataSet
‪importScenarioDataSet($dataSetName)
Definition: AbstractDataHandlerActionTestCase.php:201
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\assertHookInvocationPayload
‪assertHookInvocationPayload(string $methodName, array $assertions)
Definition: HookTest.php:307
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\$scenarioDataSetDirectory
‪string $scenarioDataSetDirectory
Definition: HookTest.php:48