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