‪TYPO3CMS  10.4
ActionControllerValidationTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
28 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
29 
33 class ‪ActionControllerValidationTest extends FunctionalTestCase
34 {
38  protected ‪$testExtensionsToLoad = [
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)
69  {
70  ‪$GLOBALS['LANG'] = $this->getContainer()->get(LanguageService::class);
71  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 'testkey';
72 
73  $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/pages.xml');
74  $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/blogs.xml');
75  $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/posts.xml');
76 
77  $objectManager = $this->‪getObjectManager();
78  $response = $objectManager->get(Response::class);
79  $request = $objectManager->get(Request::class);
80 
81  $request->setControllerActionName('testForward');
82  $request->setArgument('blogPost', $blogPostArgument);
83  $request->setArgument('__trustedProperties', $this->‪generateTrustedPropertiesToken($trustedProperties));
84 
85  $referrerRequest = [];
86  $referrerRequest['@action'] = 'testForm';
87  $request->setArgument(
88  '__referrer',
89  ['@request' => $this->‪getHashService()->appendHmac(json_encode($referrerRequest))]
90  );
91 
92  while (!$request->isDispatched()) {
93  try {
94  $blogController = $objectManager->get(BlogController::class);
95  $blogController->processRequest($request, $response);
96  } catch (‪StopActionException $e) {
97  }
98  }
99 
100  /* @var \TYPO3\CMS\Extbase\Error\Error $titleLengthError */
101  $titleMappingResults = $request->getOriginalRequestMappingResults()->forProperty('blogPost.title');
102  $titleErrors = $titleMappingResults->getFlattenedErrors();
103  self::assertCount(count($expectedErrorCodes), $titleErrors['']);
104 
105  $titleErrors = $titleErrors[''];
107  foreach ($titleErrors as $titleError) {
108  self::assertContains($titleError->getCode(), $expectedErrorCodes);
109  }
110  self::assertEquals('testFormAction', $response->getContent());
111  }
112 
117  {
118  ‪$GLOBALS['LANG'] = $this->getContainer()->get(LanguageService::class);
119  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 'testkey';
120 
121  $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/pages.xml');
122  $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/blogs.xml');
123  $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/posts.xml');
124 
125  $objectManager = $this->‪getObjectManager();
126  $response = $objectManager->get(Response::class);
127  $request = $objectManager->get(Request::class);
128 
129  $request->setControllerActionName('testRelatedObject');
130  $request->setArgument('blog', ['__identity' => 1, 'description' => str_repeat('test', 40)]);
131  $request->setArgument(
132  'blogPost',
133  ['__identity' => 1, 'title' => '77', 'blog' => ['__identity' => 1, 'title' => str_repeat('test', 21)]]
134  );
135  $request->setArgument(
136  '__trustedProperties',
138  [
139  'blog[__identity]',
140  'blog[description]',
141  'blogPost[__identity]',
142  'blogPost[title]',
143  'blogPost[blog][__identity]',
144  'blogPost[blog][title]'
145  ]
146  )
147  );
148 
149  $referrerRequest = [];
150  $referrerRequest['@action'] = 'testForm';
151  $request->setArgument(
152  '__referrer',
153  ['@request' => $this->‪getHashService()->appendHmac(json_encode($referrerRequest))]
154  );
155 
156  while (!$request->isDispatched()) {
157  try {
158  $blogController = $objectManager->get(BlogController::class);
159  $blogController->processRequest($request, $response);
160  } catch (‪StopActionException $e) {
161  }
162  }
163 
164  /* @var \TYPO3\CMS\Extbase\Error\Error $titleLengthError */
165  ‪$errors = $request->getOriginalRequestMappingResults()->getFlattenedErrors();
166  self::assertCount(1, ‪$errors['blog.title']);
167  self::assertCount(1, ‪$errors['blog.description']);
168  self::assertCount(1, ‪$errors['blogPost.title']);
169 
170  self::assertEquals('testFormAction', $response->getContent());
171  }
172 
177  protected function ‪generateTrustedPropertiesToken(array $formFieldNames)
178  {
179  $mvcPropertyMappingConfigurationService = $this->‪getObjectManager()->get(
180  MvcPropertyMappingConfigurationService::class
181  );
182  return $mvcPropertyMappingConfigurationService->generateTrustedPropertiesToken($formFieldNames, '');
183  }
184 
188  protected function ‪getHashService()
189  {
190  return $this->‪getObjectManager()->get(HashService::class);
191  }
192 
196  protected function ‪getObjectManager()
197  {
198  return GeneralUtility::makeInstance(ObjectManager::class);
199  }
200 }
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Validation\ActionControllerValidationTest\getObjectManager
‪TYPO3 CMS Extbase Object ObjectManagerInterface getObjectManager()
Definition: ActionControllerValidationTest.php:195
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Validation
Definition: ActionControllerValidationTest.php:16
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Validation\ActionControllerValidationTest\getHashService
‪HashService getHashService()
Definition: ActionControllerValidationTest.php:187
‪ExtbaseTeam\BlogExample\Controller\BlogController
Definition: BlogController.php:32
‪TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
Definition: StopActionException.php:31
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Validation\ActionControllerValidationTest\validationResultsAreProvidedForTheSameObjectInDifferentArguments
‪validationResultsAreProvidedForTheSameObjectInDifferentArguments()
Definition: ActionControllerValidationTest.php:115
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Validation\ActionControllerValidationTest\forwardedActionValidatesPreviouslyIgnoredArgumentDataProvider
‪array forwardedActionValidatesPreviouslyIgnoredArgumentDataProvider()
Definition: ActionControllerValidationTest.php:44
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Validation\ActionControllerValidationTest\generateTrustedPropertiesToken
‪string generateTrustedPropertiesToken(array $formFieldNames)
Definition: ActionControllerValidationTest.php:176
‪TYPO3\CMS\Extbase\Security\Cryptography\HashService
Definition: HashService.php:31
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Validation\ActionControllerValidationTest\$testExtensionsToLoad
‪array $testExtensionsToLoad
Definition: ActionControllerValidationTest.php:37
‪TYPO3\CMS\Extbase\Error\Error
Definition: Error.php:25
‪TYPO3\CMS\Extbase\Mvc\Web\Response
Definition: Response.php:26
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Validation\ActionControllerValidationTest
Definition: ActionControllerValidationTest.php:34
‪TYPO3\CMS\Extbase\Mvc\Web\Request
Definition: Request.php:23
‪$errors
‪$errors
Definition: annotationChecker.php:121
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfigurationService
Definition: MvcPropertyMappingConfigurationService.php:46
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:28
‪TYPO3\CMS\Extbase\Tests\Functional\Mvc\Validation\ActionControllerValidationTest\forwardedActionValidatesPreviouslyIgnoredArgument
‪forwardedActionValidatesPreviouslyIgnoredArgument(array $blogPostArgument, array $trustedProperties, array $expectedErrorCodes)
Definition: ActionControllerValidationTest.php:67