‪TYPO3CMS  11.5
ActionControllerValidationTest.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 
31 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
32 
36 class ‪ActionControllerValidationTest extends FunctionalTestCase
37 {
39  'typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example',
40  ];
41 
46  {
47  return [
48  'new blog post' => [
49  ['title' => '12'],
50  ['blogPost[title]'],
51  [1428504122],
52  ],
53  'existing blog post' => [
54  ['__identity' => 1, 'title' => '12'],
55  ['blogPost[__identity]', 'blogPost[title]'],
56  [1428504122],
57  ],
58  ];
59  }
60 
68  public function ‪forwardedActionValidatesPreviouslyIgnoredArgument(array $blogPostArgument, array $trustedProperties, array $expectedErrorCodes): void
69  {
70  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->create('default');
71  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 'testkey';
72 
73  $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/pages.xml');
74  $this->importCSVDataSet(__DIR__ . '/../../Persistence/Fixtures/blogs.csv');
75  $this->importCSVDataSet(__DIR__ . '/../../Persistence/Fixtures/posts.csv');
76 
77  $response = new ‪Response();
78  $request = new ‪Request();
79 
80  $request->setControllerActionName('testForward');
81  $request->setArgument('blogPost', $blogPostArgument);
82  $request->setArgument('__trustedProperties', $this->‪generateTrustedPropertiesToken($trustedProperties));
83 
84  $referrerRequest = [];
85  $referrerRequest['@action'] = 'testForm';
86  $request->setArgument(
87  '__referrer',
88  ['@request' => $this->‪getHashService()->appendHmac(json_encode($referrerRequest))]
89  );
90 
91  $titleMappingResults = new ‪Result();
92  while (!$request->isDispatched()) {
93  try {
94  $blogController = $this->get(BlogController::class);
95  $response = $blogController->processRequest($request);
96  if ($response instanceof ‪ForwardResponse) {
97  $titleMappingResults = $response->getArgumentsValidationResult()->forProperty('blogPost.title');
99  }
100  } catch (‪StopActionException $e) {
101  }
102  }
103 
104  $titleErrors = $titleMappingResults->getFlattenedErrors();
105  self::assertCount(count($expectedErrorCodes), $titleErrors['']);
106 
107  $titleErrors = $titleErrors[''];
109  foreach ($titleErrors as $titleError) {
110  self::assertContains($titleError->getCode(), $expectedErrorCodes);
111  }
112  $response->getBody()->rewind();
113  self::assertEquals('testFormAction', $response->getBody()->getContents());
114  }
115 
120  {
121  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->create('default');
122  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 'testkey';
123 
124  $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/pages.xml');
125  $this->importCSVDataSet(__DIR__ . '/../../Persistence/Fixtures/blogs.csv');
126  $this->importCSVDataSet(__DIR__ . '/../../Persistence/Fixtures/posts.csv');
127 
128  $response = new ‪Response();
129  $request = new ‪Request();
130 
131  $request->setControllerActionName('testRelatedObject');
132  $request->setArgument('blog', ['__identity' => 1, 'description' => str_repeat('test', 40)]);
133  $request->setArgument(
134  'blogPost',
135  ['__identity' => 1, 'title' => '77', 'blog' => ['__identity' => 1, 'title' => str_repeat('test', 21)]]
136  );
137  $request->setArgument(
138  '__trustedProperties',
140  [
141  'blog[__identity]',
142  'blog[description]',
143  'blogPost[__identity]',
144  'blogPost[title]',
145  'blogPost[blog][__identity]',
146  'blogPost[blog][title]',
147  ]
148  )
149  );
150 
151  $referrerRequest = [];
152  $referrerRequest['@action'] = 'testForm';
153  $request->setArgument(
154  '__referrer',
155  ['@request' => $this->‪getHashService()->appendHmac(json_encode($referrerRequest))]
156  );
157 
158  while (!$request->isDispatched()) {
159  try {
160  $blogController = $this->get(BlogController::class);
161  $response = $blogController->processRequest($request);
162  if ($response instanceof ‪ForwardResponse) {
164  $validationResult = $response->getArgumentsValidationResult();
165 
166  self::assertInstanceOf(ForwardResponse::class, $response);
167  self::assertCount(1, $validationResult->forProperty('blog.title')->getErrors());
168  self::assertCount(1, $validationResult->forProperty('blog.description')->getErrors());
169  self::assertCount(1, $validationResult->forProperty('blogPost.title')->getErrors());
170 
172  }
173  } catch (‪StopActionException $e) {
174  }
175  }
176 
177  $response->getBody()->rewind();
178  self::assertEquals('testFormAction', $response->getBody()->getContents());
179  }
180 
185  {
186  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->create('default');
187  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 'testkey';
188 
189  $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/pages.xml');
190  $this->importCSVDataSet(__DIR__ . '/../../Persistence/Fixtures/blogs.csv');
191  $this->importCSVDataSet(__DIR__ . '/../../Persistence/Fixtures/posts.csv');
192 
193  $response = new ‪Response();
194  $request = new ‪Request();
195 
196  $request->setControllerActionName('testRelatedObject');
197  $request->setArgument('blog', ['__identity' => 1, 'description' => str_repeat('test', 40)]);
198  $request->setArgument(
199  'blogPost',
200  ['__identity' => 1, 'title' => '77', 'blog' => ['__identity' => 1, 'title' => str_repeat('test', 21)]]
201  );
202  $request->setArgument(
203  '__trustedProperties',
205  [
206  'blog[__identity]',
207  'blog[description]',
208  'blogPost[__identity]',
209  'blogPost[title]',
210  'blogPost[blog][__identity]',
211  'blogPost[blog][title]',
212  ]
213  )
214  );
215 
216  $referrerRequest = [];
217  $referrerRequest['@action'] = 'testForm';
218  $request->setArgument(
219  '__referrer',
220  ['@request' => $this->‪getHashService()->appendHmac(json_encode($referrerRequest))]
221  );
222 
223  $originalArguments = $request->getArguments();
224  while (!$request->isDispatched()) {
225  try {
226  $blogController = $this->get(BlogController::class);
227  $response = $blogController->processRequest($request);
228  if ($response instanceof ‪ForwardResponse) {
230  self::assertEquals($originalArguments, $request->getOriginalRequest()->getAttribute('extbase')->getArguments());
231  }
232  } catch (‪StopActionException $e) {
233  }
234  }
235 
236  $response->getBody()->rewind();
237  self::assertEquals('testFormAction', $response->getBody()->getContents());
238  }
239 
244  protected function ‪generateTrustedPropertiesToken(array $formFieldNames): string
245  {
246  $mvcPropertyMappingConfigurationService = $this->get(
247  MvcPropertyMappingConfigurationService::class
248  );
249  return $mvcPropertyMappingConfigurationService->generateTrustedPropertiesToken($formFieldNames, '');
250  }
251 
255  protected function ‪getHashService(): ‪HashService
256  {
257  return $this->get(HashService::class);
258  }
259 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Validation
Definition: ActionControllerValidationTest.php:18
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Validation\ActionControllerValidationTest\getHashService
‪HashService getHashService()
Definition: ActionControllerValidationTest.php:255
‪ExtbaseTeam\BlogExample\Controller\BlogController
Definition: BlogController.php:34
‪TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
Definition: StopActionException.php:37
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Validation\ActionControllerValidationTest\validationResultsAreProvidedForTheSameObjectInDifferentArguments
‪validationResultsAreProvidedForTheSameObjectInDifferentArguments()
Definition: ActionControllerValidationTest.php:119
‪TYPO3\CMS\Extbase\Mvc\Dispatcher
Definition: Dispatcher.php:38
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Validation\ActionControllerValidationTest\$testExtensionsToLoad
‪$testExtensionsToLoad
Definition: ActionControllerValidationTest.php:38
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Validation\ActionControllerValidationTest\argumentsOfOriginalRequestRemainOnValidationErrors
‪argumentsOfOriginalRequestRemainOnValidationErrors()
Definition: ActionControllerValidationTest.php:184
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Validation\ActionControllerValidationTest\forwardedActionValidatesPreviouslyIgnoredArgumentDataProvider
‪array forwardedActionValidatesPreviouslyIgnoredArgumentDataProvider()
Definition: ActionControllerValidationTest.php:45
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Validation\ActionControllerValidationTest\generateTrustedPropertiesToken
‪string generateTrustedPropertiesToken(array $formFieldNames)
Definition: ActionControllerValidationTest.php:244
‪TYPO3\CMS\Extbase\Http\ForwardResponse
Definition: ForwardResponse.php:24
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService
Definition: HashService.php:31
‪TYPO3\CMS\Extbase\Error\Result
Definition: Result.php:24
‪TYPO3\CMS\Core\Http\Response
Definition: Response.php:30
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:25
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Validation\ActionControllerValidationTest
Definition: ActionControllerValidationTest.php:37
‪TYPO3\CMS\Extbase\Mvc\Dispatcher\buildRequestFromCurrentRequestAndForwardResponse
‪static buildRequestFromCurrentRequestAndForwardResponse(Request $currentRequest, ForwardResponse $forwardResponse)
Definition: Dispatcher.php:147
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService
Definition: MvcPropertyMappingConfigurationService.php:46
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Validation\ActionControllerValidationTest\forwardedActionValidatesPreviouslyIgnoredArgument
‪forwardedActionValidatesPreviouslyIgnoredArgument(array $blogPostArgument, array $trustedProperties, array $expectedErrorCodes)
Definition: ActionControllerValidationTest.php:68
‪TYPO3\CMS\Extbase\Mvc\Request
Definition: Request.php:39