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