‪TYPO3CMS  ‪main
FormInlineAjaxControllerTest.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;
28 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
29 
30 final class ‪FormInlineAjaxControllerTest extends FunctionalTestCase
31 {
33 
36 
37  protected array ‪$testExtensionsToLoad = [
38  'typo3/sysext/core/Tests/Functional/Fixtures/Extensions/test_irre_csv',
39  ];
40 
44  protected const ‪LANGUAGE_PRESETS = [
45  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8'],
46  'DA' => ['id' => 1, 'title' => 'Dansk', 'locale' => 'da_DK.UTF8'],
47  ];
48 
52  protected function ‪setUp(): void
53  {
54  parent::setUp();
55 
56  $this->importCSVDataSet(__DIR__ . '/../Fixtures/pages.csv');
57  $this->importCSVDataSet(__DIR__ . '/../Fixtures/tx_testirrecsv_hotel.csv');
58  $this->importCSVDataSet(__DIR__ . '/../Fixtures/be_users.csv');
59  $backendUser = $this->setUpBackendUser(1);
60  ‪$GLOBALS['LANG'] = $this->get(LanguageServiceFactory::class)->createFromUserPreferences($backendUser);
61 
63  'website-local',
64  $this->‪buildSiteConfiguration(1, 'http://localhost/'),
65  [
66  $this->‪buildDefaultLanguageConfiguration('EN', '/en/'),
67  $this->‪buildLanguageConfiguration('DA', '/da/'),
68  ]
69  );
70 
71  $this->subject = new ‪FormInlineAjaxController(new ‪FormDataCompiler(), new ‪HashService());
72  $this->hashService = new ‪HashService();
73  }
74 
75  #[Test]
77  {
78  $parsedBody = [
79  'ajax' => [
80  0 => 'data-1-tx_testirrecsv_hotel-NEW59c1062549e56282348897-offers-tx_testirrecsv_offer',
81  'context' => json_encode($this->‪getContextForSysLanguageUid(0)),
82  ],
83  ];
84 
85  $request = new ‪ServerRequest();
86  $request = $request->withAttribute('route', new ‪Route('path', ['packageName' => 'typo3/cms-backend']));
87  $request = $request->withParsedBody($parsedBody);
88 
89  $response = $this->subject->createAction($request);
90  $body = (string)$response->getBody();
91  $jsonArray = json_decode($body, true);
92 
93  self::assertNotEmpty($jsonArray['data']);
94  }
95 
96  #[Test]
98  {
99  $parsedBody = [
100  'ajax' => [
101  0 => 'data-1-tx_testirrecsv_hotel-NEW59c1062549e56282348897-offers-tx_testirrecsv_offer',
102  'context' => json_encode($this->‪getContextForSysLanguageUid(0)),
103  ],
104  ];
105 
106  $request = new ‪ServerRequest();
107  $request = $request->withAttribute('route', new ‪Route('path', ['packageName' => 'typo3/cms-backend']));
108  $request = $request->withParsedBody($parsedBody);
109 
110  $response = $this->subject->createAction($request);
111  $body = (string)$response->getBody();
112  $jsonArray = json_decode($body, true);
113 
114  self::assertNotEmpty($jsonArray['data']);
115  }
116 
117  #[Test]
119  {
120  $parsedBody = [
121  'ajax' => [
122  0 => 'data-1-tx_testirrecsv_hotel-NEW59c1062549e56282348897-offers-tx_testirrecsv_offer',
123  'context' => json_encode($this->‪getContextForSysLanguageUid(1)),
124  ],
125  ];
126 
127  $request = new ‪ServerRequest();
128  $request = $request->withAttribute('route', new ‪Route('path', ['packageName' => 'typo3/cms-backend']));
129  $request = $request->withParsedBody($parsedBody);
130 
131  $response = $this->subject->createAction($request);
132  $body = (string)$response->getBody();
133  $jsonArray = json_decode($body, true);
134 
135  self::assertMatchesRegularExpression('/<option value="1"[^>]* selected="selected">Dansk<\/option>/', $jsonArray['data']);
136  }
137 
138  #[Test]
140  {
141  unset(‪$GLOBALS['TCA']['tx_testirrecsv_offer']['ctrl']['languageField']);
142  unset(‪$GLOBALS['TCA']['tx_testirrecsv_offer']['ctrl']['transOrigPointerField']);
143  unset(‪$GLOBALS['TCA']['tx_testirrecsv_offer']['ctrl']['transOrigDiffSourceField']);
144 
145  $parsedBody = [
146  'ajax' => [
147  0 => 'data-1-tx_testirrecsv_hotel-NEW59c1062549e56282348897-offers-tx_testirrecsv_offer',
148  'context' => json_encode($this->‪getContextForSysLanguageUid(1)),
149  ],
150  ];
151 
152  $request = new ‪ServerRequest();
153  $request = $request->withAttribute('route', new ‪Route('path', ['packageName' => 'typo3/cms-backend']));
154  $request = $request->withParsedBody($parsedBody);
155 
156  $response = $this->subject->createAction($request);
157  $body = (string)$response->getBody();
158  $jsonArray = json_decode($body, true);
159 
160  self::assertDoesNotMatchRegularExpression('/<select[^>]* name="data\[tx_testirrecsv_offer\]\[NEW[1-9]+\]\[sys_language_uid\]"[^>]*>/', $jsonArray['data']);
161  }
162 
163  protected function ‪getContextForSysLanguageUid(int $sysLanguageUid): array
164  {
165  $config = [
166  'type' => 'inline',
167  'foreign_table' => 'tx_testirrecsv_offer',
168  'maxitems' => 10,
169  'appearance' => [
170  'showSynchronizationLink' => 1,
171  'showAllLocalizationLink' => 1,
172  'showPossibleLocalizationRecords' => true,
173  'levelLinksPosition' => 'top',
174  'enabledControls' => [
175  'info' => true,
176  'new' => true,
177  'dragdrop' => true,
178  'sort' => true,
179  'hide' => true,
180  'delete' => true,
181  'localize' => true,
182  ],
183  ],
184  'default' => '',
185  'inline' => [
186  'parentSysLanguageUid' => $sysLanguageUid,
187  'first' => false,
188  'last' => false,
189  ],
190  ];
191 
192  $configJson = json_encode($config);
193  return [
194  'config' => $configJson,
195  'hmac' => $this->hashService->hmac($configJson, 'InlineContext'),
196  ];
197  }
198 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Backend\Tests\Functional\Controller\FormInlineAjaxControllerTest\getContextForSysLanguageUid
‪getContextForSysLanguageUid(int $sysLanguageUid)
Definition: FormInlineAjaxControllerTest.php:162
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildLanguageConfiguration
‪buildLanguageConfiguration(string $identifier, string $base, array $fallbackIdentifiers=[], string $fallbackType=null)
Definition: SiteBasedTestTrait.php:108
‪TYPO3\CMS\Backend\Controller\FormInlineAjaxController
Definition: FormInlineAjaxController.php:43
‪TYPO3\CMS\Backend\Tests\Functional\Controller\FormInlineAjaxControllerTest\LANGUAGE_PRESETS
‪const LANGUAGE_PRESETS
Definition: FormInlineAjaxControllerTest.php:43
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.php:37
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\writeSiteConfiguration
‪writeSiteConfiguration(string $identifier, array $site=[], array $languages=[], array $errorHandling=[])
Definition: SiteBasedTestTrait.php:50
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:88
‪TYPO3\CMS\Backend\Tests\Functional\Controller\FormInlineAjaxControllerTest\createActionWithExistingLocalizedParentAndNotLocalizableChildReturnsResponseWithChildData
‪createActionWithExistingLocalizedParentAndNotLocalizableChildReturnsResponseWithChildData()
Definition: FormInlineAjaxControllerTest.php:138
‪TYPO3\CMS\Backend\Routing\Route
Definition: Route.php:24
‪TYPO3\CMS\Backend\Tests\Functional\Controller\FormInlineAjaxControllerTest\createActionWithNewParentReturnsResponseForInlineChildData
‪createActionWithNewParentReturnsResponseForInlineChildData()
Definition: FormInlineAjaxControllerTest.php:75
‪TYPO3\CMS\Backend\Tests\Functional\Controller\FormInlineAjaxControllerTest\$testExtensionsToLoad
‪array $testExtensionsToLoad
Definition: FormInlineAjaxControllerTest.php:36
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Backend\Tests\Functional\Controller\FormInlineAjaxControllerTest\setUp
‪setUp()
Definition: FormInlineAjaxControllerTest.php:51
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:98
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Tests\Functional\Controller\FormInlineAjaxControllerTest\createActionWithExistingLocalizedParentReturnsResponseWithLocalizedChildData
‪createActionWithExistingLocalizedParentReturnsResponseWithLocalizedChildData()
Definition: FormInlineAjaxControllerTest.php:117
‪TYPO3\CMS\Backend\Tests\Functional\Controller\FormInlineAjaxControllerTest\$subject
‪FormInlineAjaxController $subject
Definition: FormInlineAjaxControllerTest.php:33
‪TYPO3\CMS\Backend\Tests\Functional\Controller\FormInlineAjaxControllerTest
Definition: FormInlineAjaxControllerTest.php:31
‪TYPO3\CMS\Backend\Form\FormDataCompiler
Definition: FormDataCompiler.php:26
‪TYPO3\CMS\Core\Crypto\HashService
Definition: HashService.php:27
‪TYPO3\CMS\Backend\Tests\Functional\Controller
Definition: BackendControllerTest.php:18
‪TYPO3\CMS\Backend\Tests\Functional\Controller\FormInlineAjaxControllerTest\$hashService
‪HashService $hashService
Definition: FormInlineAjaxControllerTest.php:34
‪TYPO3\CMS\Backend\Tests\Functional\Controller\FormInlineAjaxControllerTest\createActionWithExistingParentReturnsResponseForInlineChildData
‪createActionWithExistingParentReturnsResponseForInlineChildData()
Definition: FormInlineAjaxControllerTest.php:96