‪TYPO3CMS  11.5
AbstractDataHandlerActionTestCase.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 Symfony\Component\Yaml\Yaml;
30 use TYPO3\TestingFramework\Core\Functional\Framework\Constraint\RequestSection\DoesNotHaveRecordConstraint;
31 use TYPO3\TestingFramework\Core\Functional\Framework\Constraint\RequestSection\HasRecordConstraint;
32 use TYPO3\TestingFramework\Core\Functional\Framework\Constraint\RequestSection\StructureDoesNotHaveRecordConstraint;
33 use TYPO3\TestingFramework\Core\Functional\Framework\Constraint\RequestSection\StructureHasRecordConstraint;
34 use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\ActionService;
35 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
36 
40 abstract class ‪AbstractDataHandlerActionTestCase extends FunctionalTestCase
41 {
43  protected const ‪VALUE_BackendUserId = 1;
44  protected const ‪VALUE_WorkspaceId = 0;
45 
53 
55  'SC_OPTIONS' => [
56  'Core/TypoScript/TemplateService' => [
57  'runThroughTemplatesPostProcessing' => [
58  'FunctionalTest' => \TYPO3\TestingFramework\Core\Functional\Framework\Frontend\Hook\TypoScriptInstructionModifier::class . '->apply',
59  ],
60  ],
61  ],
62  ];
63 
67  protected ‪$recordIds = [];
68 
69  protected ActionService ‪$actionService;
71 
76  protected ‪$siteLanguageConfiguration = [
77  1 => [
78  'title' => 'Dansk',
79  'enabled' => true,
80  'languageId' => 1,
81  'base' => '/dk/',
82  'typo3Language' => 'dk',
83  'locale' => 'da_DK.UTF-8',
84  'iso-639-1' => 'da',
85  'flag' => 'dk',
86  'fallbackType' => 'fallback',
87  'fallbacks' => '0',
88  ],
89  2 => [
90  'title' => 'Deutsch',
91  'enabled' => true,
92  'languageId' => 2,
93  'base' => '/de/',
94  'typo3Language' => 'de',
95  'locale' => 'de_DE.UTF-8',
96  'iso-639-1' => 'de',
97  'flag' => 'de',
98  'fallbackType' => 'fallback',
99  'fallbacks' => '1,0',
100  ],
101  ];
102 
103  protected function ‪setUp(): void
104  {
105  parent::setUp();
106 
107  $this->backendUser = $this->setUpBackendUserFromFixture(self::VALUE_BackendUserId);
108  // Note late static binding - Workspace related tests override the constant
109  $this->‪setWorkspaceId(static::VALUE_WorkspaceId);
110 
111  $this->actionService = new ActionService();
113  }
114 
115  protected function ‪tearDown(): void
116  {
117  $this->‪assertErrorLogEntries();
119  unset($this->actionService);
120  unset($this->recordIds);
121  parent::tearDown();
122  }
123 
131  protected function ‪setUpFrontendSite(int $pageId, array $additionalLanguages = []): void
132  {
133  $languages = [
134  0 => [
135  'title' => 'English',
136  'enabled' => true,
137  'languageId' => 0,
138  'base' => '/',
139  'typo3Language' => 'default',
140  'locale' => 'en_US.UTF-8',
141  'iso-639-1' => 'en',
142  'navigationTitle' => '',
143  'hreflang' => '',
144  'direction' => '',
145  'flag' => 'us',
146  ],
147  ];
148  $languages = array_merge($languages, $additionalLanguages);
149  $configuration = [
150  'rootPageId' => $pageId,
151  'base' => '/',
152  'languages' => $languages,
153  'errorHandling' => [],
154  'routes' => [],
155  ];
156  ‪GeneralUtility::mkdir_deep($this->instancePath . '/typo3conf/sites/testing/');
157  $yamlFileContents = Yaml::dump($configuration, 99, 2);
158  $fileName = $this->instancePath . '/typo3conf/sites/testing/config.yaml';
159  ‪GeneralUtility::writeFile($fileName, $yamlFileContents);
160  // Ensure that no other site configuration was cached before
161  $cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('core');
162  if ($cache->has('sites-configuration')) {
163  $cache->remove('sites-configuration');
164  }
165  }
166 
170  protected function ‪setWorkspaceId(int $workspaceId): void
171  {
172  $this->backendUser->workspace = $workspaceId;
173  GeneralUtility::makeInstance(Context::class)->setAspect('workspace', new ‪WorkspaceAspect($workspaceId));
174  }
175 
181  protected function ‪assertErrorLogEntries(?array $expectedMessages = null): void
182  {
183  if ($this->expectedErrorLogEntries === null && $expectedMessages === null) {
184  return;
185  }
186 
187  if ($expectedMessages !== null) {
188  ‪$expectedErrorLogEntries = count($expectedMessages);
189  } else {
190  ‪$expectedErrorLogEntries = (int)$this->expectedErrorLogEntries;
191  }
192 
193  $queryBuilder = $this->getConnectionPool()
194  ->getQueryBuilderForTable('sys_log');
195  $queryBuilder->getRestrictions()->removeAll();
196  $statement = $queryBuilder
197  ->select('*')
198  ->from('sys_log')
199  ->where(
200  $queryBuilder->expr()->in(
201  'error',
202  $queryBuilder->createNamedParameter([1, 2], Connection::PARAM_INT_ARRAY)
203  )
204  )
205  ->executeQuery();
206 
207  $actualErrorLogEntries = (int)$queryBuilder
208  ->count('uid')
209  ->executeQuery()
210  ->fetchOne();
211 
212  $entryMessages = array_map(
213  function (array $entry) {
214  return $this->‪formatLogDetails($entry['details'] ?? '', $entry['log_data'] ?? '');
215  },
216  $statement->fetchAllAssociative()
217  );
218 
219  if ($expectedMessages !== null) {
220  self::assertEqualsCanonicalizing($expectedMessages, $entryMessages);
221  } elseif ($actualErrorLogEntries === ‪$expectedErrorLogEntries) {
222  self::assertSame(‪$expectedErrorLogEntries, $actualErrorLogEntries);
223  } else {
224  $failureMessage = sprintf(
225  'Expected %d entries in sys_log, but got %d' . LF,
226  $expectedMessages,
227  $actualErrorLogEntries
228  );
229  $failureMessage .= '* ' . implode(LF . '* ', $entryMessages) . LF;
230  self::fail($failureMessage);
231  }
232  }
233 
237  protected function ‪assertCleanReferenceIndex(): void
238  {
239  $referenceIndex = GeneralUtility::makeInstance(ReferenceIndex::class);
240  $referenceIndexFixResult = $referenceIndex->updateIndex(true);
241  if (count($referenceIndexFixResult['errors']) > 0) {
242  self::fail('Reference index not clean. ' . LF . implode(LF, $referenceIndexFixResult['errors']));
243  }
244  }
245 
249  protected function ‪getRequestSectionHasRecordConstraint(): HasRecordConstraint
250  {
251  return new HasRecordConstraint();
252  }
253 
257  protected function ‪getRequestSectionDoesNotHaveRecordConstraint(): DoesNotHaveRecordConstraint
258  {
259  return new DoesNotHaveRecordConstraint();
260  }
261 
265  protected function ‪getRequestSectionStructureHasRecordConstraint(): StructureHasRecordConstraint
266  {
267  return new StructureHasRecordConstraint();
268  }
269 
273  protected function ‪getRequestSectionStructureDoesNotHaveRecordConstraint(): StructureDoesNotHaveRecordConstraint
274  {
275  return new StructureDoesNotHaveRecordConstraint();
276  }
277 }
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\assertCleanReferenceIndex
‪assertCleanReferenceIndex()
Definition: AbstractDataHandlerActionTestCase.php:233
‪TYPO3\CMS\Core\Context\WorkspaceAspect
Definition: WorkspaceAspect.php:31
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\setUpFrontendSite
‪setUpFrontendSite(int $pageId, array $additionalLanguages=[])
Definition: AbstractDataHandlerActionTestCase.php:127
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\getRequestSectionHasRecordConstraint
‪HasRecordConstraint getRequestSectionHasRecordConstraint()
Definition: AbstractDataHandlerActionTestCase.php:245
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\setUp
‪setUp()
Definition: AbstractDataHandlerActionTestCase.php:99
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\assertErrorLogEntries
‪assertErrorLogEntries(?array $expectedMessages=null)
Definition: AbstractDataHandlerActionTestCase.php:177
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\getRequestSectionStructureHasRecordConstraint
‪StructureHasRecordConstraint getRequestSectionStructureHasRecordConstraint()
Definition: AbstractDataHandlerActionTestCase.php:261
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\$expectedErrorLogEntries
‪int null $expectedErrorLogEntries
Definition: AbstractDataHandlerActionTestCase.php:50
‪TYPO3\CMS\Core\Database\ReferenceIndex
Definition: ReferenceIndex.php:42
‪TYPO3
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\VALUE_BackendUserId
‪const VALUE_BackendUserId
Definition: AbstractDataHandlerActionTestCase.php:42
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\getRequestSectionStructureDoesNotHaveRecordConstraint
‪StructureDoesNotHaveRecordConstraint getRequestSectionStructureDoesNotHaveRecordConstraint()
Definition: AbstractDataHandlerActionTestCase.php:269
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\$backendUser
‪BackendUserAuthentication $backendUser
Definition: AbstractDataHandlerActionTestCase.php:67
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase
Definition: AbstractDataHandlerActionTestCase.php:41
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\VALUE_WorkspaceId
‪const VALUE_WorkspaceId
Definition: AbstractDataHandlerActionTestCase.php:43
‪TYPO3\CMS\Core\Core\Bootstrap\initializeLanguageObject
‪static initializeLanguageObject()
Definition: Bootstrap.php:598
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir_deep
‪static mkdir_deep($directory)
Definition: GeneralUtility.php:1908
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\tearDown
‪tearDown()
Definition: AbstractDataHandlerActionTestCase.php:111
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\$configurationToUseInTestInstance
‪$configurationToUseInTestInstance
Definition: AbstractDataHandlerActionTestCase.php:52
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\setWorkspaceId
‪setWorkspaceId(int $workspaceId)
Definition: AbstractDataHandlerActionTestCase.php:166
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Core\Log\LogDataTrait\formatLogDetails
‪string formatLogDetails(string $detailString, $substitutes)
Definition: LogDataTrait.php:50
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\$recordIds
‪array $recordIds
Definition: AbstractDataHandlerActionTestCase.php:64
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\getRequestSectionDoesNotHaveRecordConstraint
‪DoesNotHaveRecordConstraint getRequestSectionDoesNotHaveRecordConstraint()
Definition: AbstractDataHandlerActionTestCase.php:253
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\$actionService
‪ActionService $actionService
Definition: AbstractDataHandlerActionTestCase.php:66
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\$siteLanguageConfiguration
‪array $siteLanguageConfiguration
Definition: AbstractDataHandlerActionTestCase.php:72
‪TYPO3\CMS\Core\Core\Bootstrap
Definition: Bootstrap.php:70
‪TYPO3\CMS\Core\Tests\Functional\DataHandling
Definition: AbstractDataHandlerActionTestCase.php:18
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Utility\GeneralUtility\writeFile
‪static bool writeFile($file, $content, $changePermissions=false)
Definition: GeneralUtility.php:1722
‪TYPO3\CMS\Core\Log\LogDataTrait
Definition: LogDataTrait.php:25