‪TYPO3CMS  ‪main
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 
42 abstract class ‪AbstractDataHandlerActionTestCase extends FunctionalTestCase
43 {
45  protected const ‪VALUE_BackendUserId = 1;
46  protected const ‪VALUE_WorkspaceId = 0;
47 
54  protected ‪$expectedErrorLogEntries = 0;
55 
59  protected ‪$recordIds = [];
60 
61  protected ActionService ‪$actionService;
63 
68  protected ‪$siteLanguageConfiguration = [
69  1 => [
70  'title' => 'Dansk',
71  'enabled' => true,
72  'languageId' => 1,
73  'base' => '/dk/',
74  'locale' => 'da_DK.UTF-8',
75  'flag' => 'dk',
76  'fallbackType' => 'fallback',
77  'fallbacks' => '0',
78  ],
79  2 => [
80  'title' => 'Deutsch',
81  'enabled' => true,
82  'languageId' => 2,
83  'base' => '/de/',
84  'locale' => 'de_DE.UTF-8',
85  'flag' => 'de',
86  'fallbackType' => 'fallback',
87  'fallbacks' => '1,0',
88  ],
89  ];
90 
91  protected function ‪setUp(): void
92  {
93  parent::setUp();
94 
95  $this->importCSVDataSet(__DIR__ . '/../Fixtures/be_users_admin.csv');
96  $this->backendUser = $this->setUpBackendUser(self::VALUE_BackendUserId);
97  // Note late static binding - Workspace related tests override the constant
98  $this->‪setWorkspaceId(static::VALUE_WorkspaceId);
99 
100  $this->actionService = new ActionService();
102  }
103 
104  protected function ‪tearDown(): void
105  {
106  $this->‪assertErrorLogEntries();
108  unset($this->actionService);
109  unset($this->recordIds);
110  parent::tearDown();
111  }
112 
117  protected function ‪setUpFrontendSite(int $pageId, array $additionalLanguages = []): void
118  {
119  ‪$languages = [
120  0 => [
121  'title' => 'English',
122  'enabled' => true,
123  'languageId' => 0,
124  'base' => '/',
125  'locale' => 'en_US.UTF-8',
126  'navigationTitle' => '',
127  'flag' => 'us',
128  ],
129  ];
130  ‪$languages = array_merge(‪$languages, $additionalLanguages);
131  $configuration = [
132  'rootPageId' => $pageId,
133  'base' => '/',
134  'languages' => ‪$languages,
135  'errorHandling' => [],
136  'routes' => [],
137  ];
138  ‪GeneralUtility::mkdir_deep($this->instancePath . '/typo3conf/sites/testing/');
139  $yamlFileContents = Yaml::dump($configuration, 99, 2);
140  $fileName = $this->instancePath . '/typo3conf/sites/testing/config.yaml';
141  ‪GeneralUtility::writeFile($fileName, $yamlFileContents);
142  // Ensure that no other site configuration was cached before
143  $cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('core');
144  if ($cache->has('sites-configuration')) {
145  $cache->remove('sites-configuration');
146  }
147  }
148 
149  protected function ‪setWorkspaceId(int $workspaceId): void
150  {
151  $this->backendUser->workspace = $workspaceId;
152  GeneralUtility::makeInstance(Context::class)->setAspect('workspace', new WorkspaceAspect($workspaceId));
153  }
154 
160  protected function ‪assertErrorLogEntries(array $expectedMessages = null): void
161  {
162  if ($this->expectedErrorLogEntries === null && $expectedMessages === null) {
163  return;
164  }
165 
166  if ($expectedMessages !== null) {
167  ‪$expectedErrorLogEntries = count($expectedMessages);
168  } else {
169  ‪$expectedErrorLogEntries = (int)$this->expectedErrorLogEntries;
170  }
171 
172  $queryBuilder = $this->getConnectionPool()
173  ->getQueryBuilderForTable('sys_log');
174  $queryBuilder->getRestrictions()->removeAll();
175  $statement = $queryBuilder
176  ->select('*')
177  ->from('sys_log')
178  ->where(
179  $queryBuilder->expr()->in(
180  'error',
181  $queryBuilder->createNamedParameter([1, 2], Connection::PARAM_INT_ARRAY)
182  )
183  )
184  ->executeQuery();
185 
186  $actualErrorLogEntries = (int)$queryBuilder
187  ->count('uid')
188  ->executeQuery()
189  ->fetchOne();
190 
191  $entryMessages = array_map(
192  function (array $entry) {
193  return $this->‪formatLogDetails($entry['details'] ?? '', $entry['log_data'] ?? '');
194  },
195  $statement->fetchAllAssociative()
196  );
197 
198  if ($expectedMessages !== null) {
199  self::assertEqualsCanonicalizing($expectedMessages, $entryMessages);
200  } elseif ($actualErrorLogEntries === ‪$expectedErrorLogEntries) {
201  self::assertSame(‪$expectedErrorLogEntries, $actualErrorLogEntries);
202  } else {
203  $failureMessage = sprintf(
204  'Expected %d entries in sys_log, but got %d' . LF,
205  $expectedMessages,
206  $actualErrorLogEntries
207  );
208  $failureMessage .= '* ' . implode(LF . '* ', $entryMessages) . LF;
209  self::fail($failureMessage);
210  }
211  }
212 
216  protected function ‪assertCleanReferenceIndex(): void
217  {
218  $referenceIndex = GeneralUtility::makeInstance(ReferenceIndex::class);
219  $referenceIndexFixResult = $referenceIndex->updateIndex(true);
220  if (count($referenceIndexFixResult['errors']) > 0) {
221  self::fail('Reference index not clean. ' . LF . implode(LF, $referenceIndexFixResult['errors']));
222  }
223  }
224 
225  protected function ‪getRequestSectionHasRecordConstraint(): HasRecordConstraint
226  {
227  return new HasRecordConstraint();
228  }
229 
230  protected function ‪getRequestSectionDoesNotHaveRecordConstraint(): DoesNotHaveRecordConstraint
231  {
232  return new DoesNotHaveRecordConstraint();
233  }
234 
235  protected function ‪getRequestSectionStructureHasRecordConstraint(): StructureHasRecordConstraint
236  {
237  return new StructureHasRecordConstraint();
238  }
239 
240  protected function ‪getRequestSectionStructureDoesNotHaveRecordConstraint(): StructureDoesNotHaveRecordConstraint
241  {
242  return new StructureDoesNotHaveRecordConstraint();
243  }
244 }
‪TYPO3\CMS\Core\Context\WorkspaceAspect
Definition: WorkspaceAspect.php:31
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios\AbstractDataHandlerActionTestCase\VALUE_WorkspaceId
‪const VALUE_WorkspaceId
Definition: AbstractDataHandlerActionTestCase.php:45
‪$languages
‪$languages
Definition: updateIsoDatabase.php:104
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios\AbstractDataHandlerActionTestCase\$recordIds
‪array $recordIds
Definition: AbstractDataHandlerActionTestCase.php:56
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios\AbstractDataHandlerActionTestCase\getRequestSectionStructureDoesNotHaveRecordConstraint
‪getRequestSectionStructureDoesNotHaveRecordConstraint()
Definition: AbstractDataHandlerActionTestCase.php:236
‪TYPO3\CMS\Core\Database\ReferenceIndex
Definition: ReferenceIndex.php:45
‪TYPO3\CMS\Core\Log\LogDataTrait\formatLogDetails
‪formatLogDetails(string $detailString, mixed $substitutes)
Definition: LogDataTrait.php:43
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios\AbstractDataHandlerActionTestCase\$backendUser
‪BackendUserAuthentication $backendUser
Definition: AbstractDataHandlerActionTestCase.php:59
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:55
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios\AbstractDataHandlerActionTestCase\setWorkspaceId
‪setWorkspaceId(int $workspaceId)
Definition: AbstractDataHandlerActionTestCase.php:145
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios\AbstractDataHandlerActionTestCase
Definition: AbstractDataHandlerActionTestCase.php:43
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios\AbstractDataHandlerActionTestCase\getRequestSectionDoesNotHaveRecordConstraint
‪getRequestSectionDoesNotHaveRecordConstraint()
Definition: AbstractDataHandlerActionTestCase.php:226
‪TYPO3\CMS\Core\Core\Bootstrap\initializeLanguageObject
‪static initializeLanguageObject()
Definition: Bootstrap.php:562
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios\AbstractDataHandlerActionTestCase\VALUE_BackendUserId
‪const VALUE_BackendUserId
Definition: AbstractDataHandlerActionTestCase.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir_deep
‪static mkdir_deep($directory)
Definition: GeneralUtility.php:1638
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:64
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios
Definition: AbstractDataHandlerActionTestCase.php:18
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios\AbstractDataHandlerActionTestCase\tearDown
‪tearDown()
Definition: AbstractDataHandlerActionTestCase.php:100
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios\AbstractDataHandlerActionTestCase\assertCleanReferenceIndex
‪assertCleanReferenceIndex()
Definition: AbstractDataHandlerActionTestCase.php:212
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios\AbstractDataHandlerActionTestCase\$siteLanguageConfiguration
‪array $siteLanguageConfiguration
Definition: AbstractDataHandlerActionTestCase.php:64
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios\AbstractDataHandlerActionTestCase\$expectedErrorLogEntries
‪int null $expectedErrorLogEntries
Definition: AbstractDataHandlerActionTestCase.php:52
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios\AbstractDataHandlerActionTestCase\$actionService
‪ActionService $actionService
Definition: AbstractDataHandlerActionTestCase.php:58
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:35
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios\AbstractDataHandlerActionTestCase\setUp
‪setUp()
Definition: AbstractDataHandlerActionTestCase.php:87
‪TYPO3\CMS\Core\Core\Bootstrap
Definition: Bootstrap.php:61
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios\AbstractDataHandlerActionTestCase\getRequestSectionStructureHasRecordConstraint
‪getRequestSectionStructureHasRecordConstraint()
Definition: AbstractDataHandlerActionTestCase.php:231
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios\AbstractDataHandlerActionTestCase\setUpFrontendSite
‪setUpFrontendSite(int $pageId, array $additionalLanguages=[])
Definition: AbstractDataHandlerActionTestCase.php:113
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios\AbstractDataHandlerActionTestCase\assertErrorLogEntries
‪assertErrorLogEntries(array $expectedMessages=null)
Definition: AbstractDataHandlerActionTestCase.php:156
‪TYPO3\CMS\Core\Utility\GeneralUtility\writeFile
‪static bool writeFile($file, $content, $changePermissions=false)
Definition: GeneralUtility.php:1452
‪TYPO3\CMS\Core\Log\LogDataTrait
Definition: LogDataTrait.php:25
‪TYPO3\CMS\Core\Tests\Functional\DataScenarios\AbstractDataHandlerActionTestCase\getRequestSectionHasRecordConstraint
‪getRequestSectionHasRecordConstraint()
Definition: AbstractDataHandlerActionTestCase.php:221