‪TYPO3CMS  ‪main
CreateRecordReactionTest.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\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
22 use Psr\Http\Message\ServerRequestInterface;
33 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
34 
35 final class ‪CreateRecordReactionTest extends FunctionalTestCase
36 {
37  protected array ‪$coreExtensionsToLoad = ['reactions'];
38 
39  protected function ‪setUp(): void
40  {
41  parent::setUp();
42 
43  ‪$GLOBALS['LANG'] = GeneralUtility::makeInstance(LanguageServiceFactory::class)->create('default');
44  $this->importCSVDataSet(__DIR__ . '/../../../../core/Tests/Functional/Fixtures/be_users.csv');
45  $this->importCSVDataSet(__DIR__ . '/../Fixtures/ReactionsRepositoryTest_pages.csv');
46  $this->importCSVDataSet(__DIR__ . '/../Fixtures/ReactionsRepositoryTest_reactions.csv');
47  }
48 
49  #[Test]
50  public function ‪reactWorksForAValidRequest(): void
51  {
52  $reactionRecord = (new ‪ReactionRepository())->getReactionRecordByIdentifier('visual-reaction-uuid');
53  $reaction = GeneralUtility::makeInstance(CreateRecordReaction::class);
54  $request = new ‪ServerRequest('http://localhost/', 'POST');
55  $payload = [
56  'foo' => 'bar',
57  'bar' => [
58  'string' => 'bar.foo',
59  'int' => 42,
60  'bool' => true,
61  ],
62  ];
63  $user = $this->‪setUpReactionBackendUser($request, $reactionRecord);
64  ‪$GLOBALS['BE_USER'] = $user;
65  $request = $request->withHeader('x-api-key', $reactionRecord->toArray()['secret']);
66 
67  self::assertCount(0, $this->‪getTestPages());
68 
69  $response = $reaction->react($request, $payload, $reactionRecord);
70 
71  self::assertEquals(201, $response->getStatusCode());
72  self::assertCount(1, $this->‪getTestPages());
73  }
74 
75  #[Test]
76  public function ‪reactFailsOnInvalidTable(): void
77  {
78  $reactionRecord = (new ‪ReactionRepository())->getReactionRecordByIdentifier('invalid-table');
79  $reaction = GeneralUtility::makeInstance(CreateRecordReaction::class);
80  $request = new ‪ServerRequest('http://localhost/', 'POST');
81  $request = $request->withHeader('x-api-key', $reactionRecord->toArray()['secret']);
82 
83  $response = $reaction->react($request, [], $reactionRecord);
84  self::assertEquals(400, $response->getStatusCode());
85  self::assertEquals('Invalid argument "table_name"', json_decode((string)$response->getBody(), true)['error']);
86  }
87 
88  #[Test]
89  public function ‪reactFailsOnInvalidFields(): void
90  {
91  $reactionRecord = (new ‪ReactionRepository())->getReactionRecordByIdentifier('invalid-fields');
92  $reaction = GeneralUtility::makeInstance(CreateRecordReaction::class);
93  $request = new ‪ServerRequest('http://localhost/', 'POST');
94  $request = $request->withHeader('x-api-key', $reactionRecord->toArray()['secret']);
95 
96  $response = $reaction->react($request, [], $reactionRecord);
97  self::assertEquals(400, $response->getStatusCode());
98  self::assertEquals('No fields given.', json_decode((string)$response->getBody(), true)['error']);
99  }
100 
101  protected function ‪getTestPages(): array
102  {
103  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
104  ->getQueryBuilderForTable('pages');
105  $queryBuilder->getRestrictions()->removeAll();
106  $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
107  return $queryBuilder->select('*')
108  ->from('pages')
109  ->where(
110  $queryBuilder->expr()->eq('title', $queryBuilder->createNamedParameter('Test bar')),
111  $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter(1))
112  )
113  ->executeQuery()
114  ->fetchAllAssociative();
115  }
116 
117  protected function ‪setUpReactionBackendUser(ServerRequestInterface $request, ‪ReactionInstruction $reactionInstruction): ‪BackendUserAuthentication
118  {
119  $backendUser = GeneralUtility::makeInstance(ReactionUserAuthentication::class);
121  $backendUser->setReactionInstruction($reactionInstruction);
122  return $this->authenticateBackendUser($backendUser, $request);
123  }
124 
125  public static function ‪replacePlaceHolderDataProvider(): array
126  {
127  return [
128  'no placeholders' => [
129  'value' => 'foo',
130  'payload' => [],
131  'expected' => 'foo',
132  ],
133  'placeholder in value' => [
134  'value' => '${foo}',
135  'payload' => [
136  'foo' => 'bar',
137  ],
138  'expected' => 'bar',
139  ],
140  'placeholder in value is integer' => [
141  'value' => '${foo}',
142  'payload' => [
143  'foo' => 42,
144  ],
145  'expected' => '42',
146  ],
147  'placeholder in value is float' => [
148  'value' => '${foo}',
149  'payload' => [
150  'foo' => 42.5,
151  ],
152  'expected' => '42.5',
153  ],
154  'placeholder in value is boolean true' => [
155  'value' => '${foo}',
156  'payload' => [
157  'foo' => true,
158  ],
159  'expected' => '1',
160  ],
161  'placeholder in value is boolean false' => [
162  'value' => '${foo}',
163  'payload' => [
164  'foo' => false,
165  ],
166  'expected' => '',
167  ],
168  'two placeholder in value' => [
169  'value' => '${foo} ${bar}',
170  'payload' => [
171  'foo' => 'bar',
172  'bar' => 'foo',
173  ],
174  'expected' => 'bar foo',
175  ],
176  'placeholder in value with dot' => [
177  'value' => '${foo.bar}',
178  'payload' => [
179  'foo' => [
180  'bar' => 'baz',
181  ],
182  ],
183  'expected' => 'baz',
184  ],
185  'placeholder in value with dot and array access' => [
186  'value' => '${foo.bar.0}',
187  'payload' => [
188  'foo' => [
189  'bar' => [
190  '0' => 'baz',
191  ],
192  ],
193  ],
194  'expected' => 'baz',
195  ],
196  'placeholder in value with dot and numeric array access' => [
197  'value' => '${foo.bar.0}',
198  'payload' => [
199  'foo' => [
200  'bar' => [
201  'baz',
202  ],
203  ],
204  ],
205  'expected' => 'baz',
206  ],
207  'placeholder in value with dot and array access and array access in value' => [
208  'value' => '${foo.bar.0.baz}',
209  'payload' => [
210  'foo' => [
211  'bar' => [
212  '0' => [
213  'baz' => 'qux',
214  ],
215  ],
216  ],
217  ],
218  'expected' => 'qux',
219  ],
220  'placeholder in value with dot and array access and numeric array access in value' => [
221  'value' => '${foo.bar.0.baz}',
222  'payload' => [
223  'foo' => [
224  'bar' => [
225  [
226  'baz' => 'qux',
227  ],
228  ],
229  ],
230  ],
231  'expected' => 'qux',
232  ],
233  'placeholder in value with dot and array access and array access in value and array access in value' => [
234  'value' => '${foo.bar.0.baz.0}',
235  'payload' => [
236  'foo' => [
237  'bar' => [
238  '0' => [
239  'baz' => [
240  '0' => 'qux',
241  ],
242  ],
243  ],
244  ],
245  ],
246  'expected' => 'qux',
247  ],
248  'placeholder in value with dot and array numeric access and numeric array access in value and numeric array access in value' => [
249  'value' => '${foo.bar.0.baz.0}',
250  'payload' => [
251  'foo' => [
252  'bar' => [
253  [
254  'baz' => [
255  'qux',
256  ],
257  ],
258  ],
259  ],
260  ],
261  'expected' => 'qux',
262  ],
263  'placeholder in value with dot and array access and array access in value and array access in value and array access in value' => [
264  'value' => '${foo.bar.0.baz.0.qux}',
265  'payload' => [
266  'foo' => [
267  'bar' => [
268  '0' => [
269  'baz' => [
270  '0' => [
271  'qux' => 'quux',
272  ],
273  ],
274  ],
275  ],
276  ],
277  ],
278  'expected' => 'quux',
279  ],
280  'placeholder in value with dot and array access and numeric array access in value and numeric array access in value and numeric array access in value' => [
281  'value' => '${foo.bar.0.baz.0.qux}',
282  'payload' => [
283  'foo' => [
284  'bar' => [
285  [
286  'baz' => [
287  [
288  'qux' => 'quux',
289  ],
290  ],
291  ],
292  ],
293  ],
294  ],
295  'expected' => 'quux',
296  ],
297  ];
298  }
299 
300  #[DataProvider('replacePlaceHolderDataProvider')]
301  #[Test]
302  public function ‪replacePlaceHolders(mixed $value, array $payload, string $expected): void
303  {
304  $subject = GeneralUtility::makeInstance(CreateRecordReaction::class);
305  self::assertSame($expected, $subject->replacePlaceHolders($value, $payload));
306  }
307 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Reactions\Model\ReactionInstruction
Definition: ReactionInstruction.php:27
‪TYPO3\CMS\Reactions\Tests\Functional\Reaction\CreateRecordReactionTest\reactFailsOnInvalidTable
‪reactFailsOnInvalidTable()
Definition: CreateRecordReactionTest.php:76
‪TYPO3\CMS\Reactions\Tests\Functional\Reaction
Definition: CreateRecordReactionTest.php:18
‪TYPO3\CMS\Reactions\Tests\Functional\Reaction\CreateRecordReactionTest\$coreExtensionsToLoad
‪array $coreExtensionsToLoad
Definition: CreateRecordReactionTest.php:37
‪TYPO3\CMS\Reactions\Tests\Functional\Reaction\CreateRecordReactionTest\getTestPages
‪getTestPages()
Definition: CreateRecordReactionTest.php:101
‪TYPO3\CMS\Reactions\Repository\ReactionRepository
Definition: ReactionRepository.php:36
‪TYPO3\CMS\Reactions\Authentication\ReactionUserAuthentication
Definition: ReactionUserAuthentication.php:31
‪TYPO3\CMS\Reactions\Tests\Functional\Reaction\CreateRecordReactionTest\setUp
‪setUp()
Definition: CreateRecordReactionTest.php:39
‪TYPO3\CMS\Reactions\Tests\Functional\Reaction\CreateRecordReactionTest\setUpReactionBackendUser
‪setUpReactionBackendUser(ServerRequestInterface $request, ReactionInstruction $reactionInstruction)
Definition: CreateRecordReactionTest.php:117
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Reactions\Tests\Functional\Reaction\CreateRecordReactionTest\reactWorksForAValidRequest
‪reactWorksForAValidRequest()
Definition: CreateRecordReactionTest.php:50
‪TYPO3\CMS\Reactions\Reaction\CreateRecordReaction
Definition: CreateRecordReaction.php:39
‪TYPO3\CMS\Reactions\Tests\Functional\Reaction\CreateRecordReactionTest\replacePlaceHolderDataProvider
‪static replacePlaceHolderDataProvider()
Definition: CreateRecordReactionTest.php:125
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:28
‪TYPO3\CMS\Reactions\Tests\Functional\Reaction\CreateRecordReactionTest\replacePlaceHolders
‪replacePlaceHolders(mixed $value, array $payload, string $expected)
Definition: CreateRecordReactionTest.php:302
‪TYPO3\CMS\Reactions\Tests\Functional\Reaction\CreateRecordReactionTest
Definition: CreateRecordReactionTest.php:36
‪TYPO3\CMS\Reactions\Tests\Functional\Reaction\CreateRecordReactionTest\reactFailsOnInvalidFields
‪reactFailsOnInvalidFields()
Definition: CreateRecordReactionTest.php:89
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52