‪TYPO3CMS  ‪main
EditDocumentControllerTest.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;
23 use TYPO3\CMS\Backend\Utility\BackendUtility;
29 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
30 
31 final class ‪EditDocumentControllerTest extends FunctionalTestCase
32 {
34 
36 
40  protected function ‪setUp(): void
41  {
42  parent::setUp();
43 
44  $this->importCSVDataSet(__DIR__ . '/../Fixtures/pages.csv');
45  $this->importCSVDataSet(__DIR__ . '/../Fixtures/tt_content.csv');
46  $this->importCSVDataSet(__DIR__ . '/../Fixtures/be_users.csv');
47  $backendUser = $this->setUpBackendUser(1);
48  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($backendUser);
49 
50  $this->subject = GeneralUtility::makeInstance(EditDocumentController::class);
51  $this->normalizedParams = new ‪NormalizedParams([], [], '', '');
52  }
53 
54  #[Test]
56  {
57  $request = (new ‪ServerRequest('https://www.example.com/', 'POST'))
58  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
59  $defaultValues = [
60  'colPos' => 123,
61  'CType' => 'bullets',
62  ];
63 
64  $queryParams = $this->‪getQueryParamsWithDefaults($defaultValues);
65  $parsedBody = $this->‪getParsedBody();
66 
67  $request = $request
68  ->withAttribute('normalizedParams', $this->normalizedParams)
69  ->withAttribute('route', new ‪Route('path', ['packageName' => 'typo3/cms-backend']))
70  ->withQueryParams($queryParams)
71  ->withParsedBody($parsedBody);
72  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
73  $response = $this->subject->mainAction($request);
74 
75  $newRecord = BackendUtility::getRecord('tt_content', 2);
76  self::assertEquals(
77  [$defaultValues['colPos'], $defaultValues['CType']],
78  [$newRecord['colPos'], $newRecord['CType']]
79  );
80  // Redirect to GET is applied after processing
81  self::assertEquals(302, $response->getStatusCode());
82  }
83 
84  #[Test]
86  {
87  $request = (new ‪ServerRequest('https://www.example.com/', 'POST'))
88  ->withAttribute('applicationType', ‪SystemEnvironmentBuilder::REQUESTTYPE_BE);
89  $defaultValues = [
90  'colPos' => 123,
91  'CType' => 'bullets',
92  ];
93 
94  $queryParams = $this->‪getQueryParamsWithDefaults($defaultValues);
95  $parsedBody = $this->‪getParsedBody(['colPos' => 0, 'CType' => 'text']);
96  $request = $request
97  ->withAttribute('normalizedParams', $this->normalizedParams)
98  ->withAttribute('route', new ‪Route('path', ['packageName' => 'typo3/cms-backend']))
99  ->withQueryParams($queryParams)
100  ->withParsedBody($parsedBody);
101  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
102  $response = $this->subject->mainAction($request);
103 
104  $newRecord = BackendUtility::getRecord('tt_content', 2);
105  self::assertEquals(
106  [0, 'text'],
107  [$newRecord['colPos'], $newRecord['CType']],
108  );
109  // Redirect to GET is applied after processing
110  self::assertEquals(302, $response->getStatusCode());
111  }
112 
113  protected function ‪getParsedBody(array $additionalData = []): array
114  {
115  return [
116  'data' => [
117  'tt_content' => [
118  'NEW123456' => array_replace_recursive([
119  'sys_language_uid' => 0,
120  'header' => 'Test header',
121  'pid' => -1,
122  ], $additionalData),
123  ],
124  ],
125  'doSave' => true,
126  ];
127  }
128 
129  protected function ‪getQueryParamsWithDefaults(array $defaultValues): array
130  {
131  return [
132  'edit' => [
133  'tt_content' => [
134  -1 => 'new',
135  ],
136  ],
137  'defVals' => [
138  'tt_content' => $defaultValues,
139  ],
140  ];
141  }
142 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Backend\Tests\Functional\Controller\EditDocumentControllerTest\processedDataTakesOverDefaultValues
‪processedDataTakesOverDefaultValues()
Definition: EditDocumentControllerTest.php:55
‪TYPO3\CMS\Backend\Tests\Functional\Controller\EditDocumentControllerTest
Definition: EditDocumentControllerTest.php:32
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder
Definition: SystemEnvironmentBuilder.php:41
‪TYPO3\CMS\Core\Core\SystemEnvironmentBuilder\REQUESTTYPE_BE
‪const REQUESTTYPE_BE
Definition: SystemEnvironmentBuilder.php:45
‪TYPO3\CMS\Backend\Tests\Functional\Controller\EditDocumentControllerTest\processedDataDoesNotOverridePostWithDefaultValues
‪processedDataDoesNotOverridePostWithDefaultValues()
Definition: EditDocumentControllerTest.php:85
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:24
‪TYPO3\CMS\Backend\Tests\Functional\Controller\EditDocumentControllerTest\getParsedBody
‪getParsedBody(array $additionalData=[])
Definition: EditDocumentControllerTest.php:113
‪TYPO3\CMS\Backend\Controller\EditDocumentController
Definition: EditDocumentController.php:80
‪TYPO3\CMS\Backend\Tests\Functional\Controller\EditDocumentControllerTest\$normalizedParams
‪NormalizedParams $normalizedParams
Definition: EditDocumentControllerTest.php:35
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Backend\Tests\Functional\Controller\EditDocumentControllerTest\$subject
‪EditDocumentController $subject
Definition: EditDocumentControllerTest.php:33
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Tests\Functional\Controller\EditDocumentControllerTest\getQueryParamsWithDefaults
‪getQueryParamsWithDefaults(array $defaultValues)
Definition: EditDocumentControllerTest.php:129
‪TYPO3\CMS\Backend\Tests\Functional\Controller\EditDocumentControllerTest\setUp
‪setUp()
Definition: EditDocumentControllerTest.php:40
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Tests\Functional\Controller
Definition: BackendControllerTest.php:18
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:38