‪TYPO3CMS  ‪main
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 
20 use PHPUnit\Framework\Attributes\Test;
25 use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\ActionService;
26 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
27 
31 final class ‪HookTest extends FunctionalTestCase
32 {
33  protected array ‪$testExtensionsToLoad = [
34  'typo3/sysext/core/Tests/Functional/Fixtures/Extensions/test_irre_foreignfield',
35  ];
36 
38  'SC_OPTIONS' => [
39  't3lib/class.t3lib_tcemain.php' => [
40  'processDatamapClass' => [
41  __CLASS__ => HookFixture::class,
42  ],
43  'processCmdmapClass' => [
44  __CLASS__ => HookFixture::class,
45  ],
46  ],
47  ],
48  ];
49 
50  private const ‪VALUE_PageId = 89;
51  private const ‪VALUE_ContentId = 297;
52  private const ‪TABLE_Content = 'tt_content';
53  private const ‪TABLE_Hotel = 'tx_testirreforeignfield_hotel';
54  private const ‪TABLE_Category = 'sys_category';
55  private const ‪FIELD_ContentHotel = 'tx_testirreforeignfield_hotels';
56  private const ‪FIELD_Categories = 'categories';
57 
58  protected function ‪setUp(): void
59  {
60  parent::setUp();
61  $this->importCSVDataSet(__DIR__ . '/DataSet/LiveDefaultPages.csv');
62  $this->importCSVDataSet(__DIR__ . '/DataSet/LiveDefaultElements.csv');
63  $this->importCSVDataSet(__DIR__ . '/../../Fixtures/be_users_admin.csv');
64  $backendUser = $this->setUpBackendUser(1);
65  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($backendUser);
66  }
67 
71  private function ‪assertHookInvocationsCount(‪HookFixture $hookFixture, array $methodNames, int $count): void
72  {
73  $message = 'Unexpected invocations of method "%s"';
74  foreach ($methodNames as $methodName) {
75  $invocations = $hookFixture->‪findInvocationsByMethodName($methodName);
76  self::assertCount(
77  $count,
78  $invocations,
79  sprintf($message, $methodName)
80  );
81  }
82  }
83 
87  private function ‪assertHookInvocationsPayload(‪HookFixture $hookFixture, array $methodNames, array $assertions): void
88  {
89  foreach ($methodNames as $methodName) {
90  $this->‪assertHookInvocationPayload($hookFixture, $methodName, $assertions);
91  }
92  }
93 
94  private function ‪assertHookInvocationPayload(‪HookFixture $hookFixture, string $methodName, array $assertions): void
95  {
96  $invocations = $hookFixture->‪findInvocationsByMethodName($methodName);
97  self::assertNotNull($invocations);
98  foreach ($assertions as $assertion) {
99  $indexes = [];
100  foreach ($invocations as $index => $item) {
101  if ($this->‪equals($assertion, $item)) {
102  $indexes[] = $index;
103  }
104  }
105  self::assertCount(
106  1,
107  $indexes,
108  sprintf('Unexpected hook payload amount found for method "%s"', $methodName)
109  );
110  $index = $indexes[0];
111  unset($invocations[$index]);
112  }
113  }
114 
115  private function ‪equals(array $left, array $right): bool
116  {
117  foreach ($left as $key => $leftValue) {
118  $rightValue = $right[$key] ?? null;
119  if (!is_array($leftValue) && (string)$leftValue !== (string)$rightValue) {
120  return false;
121  }
122  if (is_array($leftValue)) {
123  if (!$this->‪equals($leftValue, $rightValue)) {
124  return false;
125  }
126  }
127  }
128  return true;
129  }
130 
131  #[Test]
132  public function ‪hooksAreExecutedForNewRecords(): void
133  {
134  $hookFixture = GeneralUtility::makeInstance(HookFixture::class);
135  $actionService = new ActionService();
136  $actionService->createNewRecord(
137  self::TABLE_Content,
138  self::VALUE_PageId,
139  ['header' => 'Testing #1']
140  );
141 
142  $this->‪assertHookInvocationsCount($hookFixture, [
143  'processDatamap_beforeStart',
144  'processDatamap_afterAllOperations',
145  ], 1);
146 
147  $this->‪assertHookInvocationsPayload($hookFixture, [
148  'processDatamap_preProcessFieldArray',
149  'processDatamap_postProcessFieldArray',
150  'processDatamap_afterDatabaseOperations',
151  ], [
152  [
153  'table' => self::TABLE_Content,
154  'fieldArray' => [ 'header' => 'Testing #1', 'pid' => self::VALUE_PageId ],
155  ],
156  ]);
157  }
158 
159  #[Test]
161  {
162  $hookFixture = GeneralUtility::makeInstance(HookFixture::class);
163  $actionService = new ActionService();
164  $actionService->modifyRecord(
165  self::TABLE_Content,
166  self::VALUE_ContentId,
167  ['header' => 'Testing #1']
168  );
169 
170  $this->‪assertHookInvocationsCount($hookFixture, [
171  'processDatamap_beforeStart',
172  'processDatamap_afterAllOperations',
173  ], 1);
174 
175  $this->‪assertHookInvocationsPayload($hookFixture, [
176  'processDatamap_preProcessFieldArray',
177  'processDatamap_postProcessFieldArray',
178  'processDatamap_afterDatabaseOperations',
179  ], [
180  [
181  'table' => self::TABLE_Content,
182  'fieldArray' => [ 'header' => 'Testing #1' ],
183  ],
184  ]);
185  }
186 
187  #[Test]
188  public function ‪hooksAreExecutedForNewRelations(): void
189  {
190  $contentNewId = ‪StringUtility::getUniqueId('NEW');
191  $hotelNewId = ‪StringUtility::getUniqueId('NEW');
192  $categoryNewId = ‪StringUtility::getUniqueId('NEW');
193 
194  $hookFixture = GeneralUtility::makeInstance(HookFixture::class);
195  $actionService = new ActionService();
196  $actionService->modifyRecords(
197  self::VALUE_PageId,
198  [
199  self::TABLE_Content => [
200  'uid' => $contentNewId,
201  'header' => 'Testing #1',
202  self::FIELD_ContentHotel => $hotelNewId,
203  self::FIELD_Categories => $categoryNewId,
204  ],
205  self::TABLE_Hotel => [
206  'uid' => $hotelNewId,
207  'title' => 'Hotel #1',
208  ],
209  self::TABLE_Category => [
210  'uid' => $categoryNewId,
211  'title' => 'Category #1',
212  ],
213  ]
214  );
215 
216  $this->‪assertHookInvocationsCount($hookFixture, [
217  'processDatamap_beforeStart',
218  'processDatamap_afterAllOperations',
219  ], 1);
220 
222  $hookFixture,
223  'processDatamap_preProcessFieldArray',
224  [
225  [
226  'table' => self::TABLE_Content,
227  'fieldArray' => [
228  'header' => 'Testing #1',
229  self::FIELD_ContentHotel => $hotelNewId,
230  self::FIELD_Categories => $categoryNewId,
231  ],
232  ],
233  [
234  'table' => self::TABLE_Hotel,
235  'fieldArray' => [ 'title' => 'Hotel #1' ],
236  ],
237  [
238  'table' => self::TABLE_Category,
239  'fieldArray' => [ 'title' => 'Category #1' ],
240  ],
241  ]
242  );
243 
245  $hookFixture,
246  'processDatamap_postProcessFieldArray',
247  [
248  [
249  'table' => self::TABLE_Content,
250  'fieldArray' => [ 'header' => 'Testing #1' ],
251  ],
252  [
253  'table' => self::TABLE_Hotel,
254  'fieldArray' => [ 'title' => 'Hotel #1' ],
255  ],
256  [
257  'table' => self::TABLE_Category,
258  'fieldArray' => [ 'title' => 'Category #1' ],
259  ],
260  ]
261  );
262 
264  $hookFixture,
265  'processDatamap_afterDatabaseOperations',
266  [
267  [
268  'table' => self::TABLE_Content,
269  'fieldArray' => [
270  'header' => 'Testing #1',
271  self::FIELD_ContentHotel => 1,
272  self::FIELD_Categories => 1,
273  ],
274  ],
275  [
276  'table' => self::TABLE_Hotel,
277  'fieldArray' => [ 'title' => 'Hotel #1' ],
278  ],
279  [
280  'table' => self::TABLE_Category,
281  'fieldArray' => [ 'title' => 'Category #1' ],
282  ],
283  ]
284  );
285  }
286 
287  #[Test]
289  {
290  $hookFixture = GeneralUtility::makeInstance(HookFixture::class);
291  $actionService = new ActionService();
292  $actionService->modifyRecord(
293  self::TABLE_Content,
294  self::VALUE_ContentId,
295  [
296  'header' => 'Testing #1',
297  self::FIELD_ContentHotel => '3,4',
298  self::FIELD_Categories => '28,29,30',
299  ]
300  );
301 
302  $this->‪assertHookInvocationsCount($hookFixture, [
303  'processDatamap_beforeStart',
304  'processDatamap_afterAllOperations',
305  ], 1);
306 
308  $hookFixture,
309  'processDatamap_preProcessFieldArray',
310  [
311  [
312  'table' => self::TABLE_Content,
313  'fieldArray' => [
314  'header' => 'Testing #1',
315  self::FIELD_ContentHotel => '3,4',
316  self::FIELD_Categories => '28,29,30',
317  ],
318  ],
319  ]
320  );
321 
322  $this->‪assertHookInvocationsPayload($hookFixture, [
323  'processDatamap_postProcessFieldArray',
324  'processDatamap_afterDatabaseOperations',
325  ], [
326  [
327  'table' => self::TABLE_Content,
328  'fieldArray' => [
329  'header' => 'Testing #1',
330  self::FIELD_ContentHotel => 2,
331  self::FIELD_Categories => 3,
332  ],
333  ],
334  ]);
335  }
336 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler
Definition: CheckboxValidationTest.php:18
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\assertHookInvocationPayload
‪assertHookInvocationPayload(HookFixture $hookFixture, string $methodName, array $assertions)
Definition: HookTest.php:94
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest
Definition: HookTest.php:32
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\FIELD_ContentHotel
‪const FIELD_ContentHotel
Definition: HookTest.php:55
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\$configurationToUseInTestInstance
‪array $configurationToUseInTestInstance
Definition: HookTest.php:37
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\TABLE_Hotel
‪const TABLE_Hotel
Definition: HookTest.php:53
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\FIELD_Categories
‪const FIELD_Categories
Definition: HookTest.php:56
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\setUp
‪setUp()
Definition: HookTest.php:58
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\TABLE_Content
‪const TABLE_Content
Definition: HookTest.php:52
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\hooksAreExecutedForExistingRecords
‪hooksAreExecutedForExistingRecords()
Definition: HookTest.php:160
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\Fixtures\HookFixture\findInvocationsByMethodName
‪findInvocationsByMethodName(string $methodName)
Definition: HookFixture.php:30
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\hooksAreExecutedForNewRelations
‪hooksAreExecutedForNewRelations()
Definition: HookTest.php:188
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\$testExtensionsToLoad
‪array $testExtensionsToLoad
Definition: HookTest.php:33
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\Fixtures\HookFixture
Definition: HookFixture.php:27
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\assertHookInvocationsCount
‪assertHookInvocationsCount(HookFixture $hookFixture, array $methodNames, int $count)
Definition: HookTest.php:71
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\VALUE_ContentId
‪const VALUE_ContentId
Definition: HookTest.php:51
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\TABLE_Category
‪const TABLE_Category
Definition: HookTest.php:54
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\hooksAreExecutedForNewRecords
‪hooksAreExecutedForNewRecords()
Definition: HookTest.php:132
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\VALUE_PageId
‪const VALUE_PageId
Definition: HookTest.php:50
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\assertHookInvocationsPayload
‪assertHookInvocationsPayload(HookFixture $hookFixture, array $methodNames, array $assertions)
Definition: HookTest.php:87
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\hooksAreExecutedForExistingRelations
‪hooksAreExecutedForExistingRelations()
Definition: HookTest.php:288
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler\HookTest\equals
‪equals(array $left, array $right)
Definition: HookTest.php:115