‪TYPO3CMS  10.4
AbstractDataHandlerActionTestCase.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Symfony\Component\Yaml\Yaml;
26 use TYPO3\TestingFramework\Core\Functional\Framework\Constraint\RequestSection\DoesNotHaveRecordConstraint;
27 use TYPO3\TestingFramework\Core\Functional\Framework\Constraint\RequestSection\HasRecordConstraint;
28 use TYPO3\TestingFramework\Core\Functional\Framework\Constraint\RequestSection\StructureDoesNotHaveRecordConstraint;
29 use TYPO3\TestingFramework\Core\Functional\Framework\Constraint\RequestSection\StructureHasRecordConstraint;
30 use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\ActionService;
31 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
32 
36 abstract class ‪AbstractDataHandlerActionTestCase extends FunctionalTestCase
37 {
39 
43  protected ‪$assertCleanReferenceIndex = true;
44 
49 
54 
62 
66  protected ‪$testExtensionsToLoad = [
67  'typo3/sysext/core/Tests/Functional/Fixtures/Extensions/irre_tutorial',
68  ];
69 
74  'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/AdditionalConfiguration.php' => 'typo3conf/AdditionalConfiguration.php',
75  ];
76 
80  protected ‪$recordIds = [];
81 
85  protected ‪$actionService;
86 
90  protected ‪$backendUser;
91 
96  protected ‪$siteLanguageConfiguration = [
97  1 => [
98  'title' => 'Dansk',
99  'enabled' => true,
100  'languageId' => 1,
101  'base' => '/dk/',
102  'typo3Language' => 'dk',
103  'locale' => 'da_DK.UTF-8',
104  'iso-639-1' => 'da',
105  'flag' => 'dk',
106  'fallbackType' => 'fallback',
107  'fallbacks' => '0'
108  ],
109  2 => [
110  'title' => 'Deutsch',
111  'enabled' => true,
112  'languageId' => 2,
113  'base' => '/de/',
114  'typo3Language' => 'de',
115  'locale' => 'de_DE.UTF-8',
116  'iso-639-1' => 'de',
117  'flag' => 'de',
118  'fallbackType' => 'fallback',
119  'fallbacks' => '1,0'
120  ],
121  ];
122 
123  protected function ‪setUp(): void
124  {
125  parent::setUp();
126 
127  $this->backendUser = $this->setUpBackendUserFromFixture(self::VALUE_BackendUserId);
128  // By default make tests on live workspace
129  $this->‪setWorkspaceId(0);
130 
131  $this->actionService = $this->‪getActionService();
133  }
134 
135  protected function ‪tearDown(): void
136  {
137  $this->‪assertErrorLogEntries();
138  if ($this->‪assertCleanReferenceIndex) {
140  }
141  unset($this->actionService);
142  unset($this->recordIds);
143  parent::tearDown();
144  }
145 
153  protected function ‪setUpFrontendSite(int $pageId, array $additionalLanguages = [])
154  {
155  $languages = [
156  0 => [
157  'title' => 'English',
158  'enabled' => true,
159  'languageId' => 0,
160  'base' => '/',
161  'typo3Language' => 'default',
162  'locale' => 'en_US.UTF-8',
163  'iso-639-1' => 'en',
164  'navigationTitle' => '',
165  'hreflang' => '',
166  'direction' => '',
167  'flag' => 'us',
168  ]
169  ];
170  $languages = array_merge($languages, $additionalLanguages);
171  $configuration = [
172  'rootPageId' => $pageId,
173  'base' => '/',
174  'languages' => $languages,
175  'errorHandling' => [],
176  'routes' => [],
177  ];
178  ‪GeneralUtility::mkdir_deep($this->instancePath . '/typo3conf/sites/testing/');
179  $yamlFileContents = Yaml::dump($configuration, 99, 2);
180  $fileName = $this->instancePath . '/typo3conf/sites/testing/config.yaml';
181  ‪GeneralUtility::writeFile($fileName, $yamlFileContents);
182  // Ensure that no other site configuration was cached before
183  $cache = GeneralUtility::makeInstance(CacheManager::class)->getCache('core');
184  if ($cache->has('sites-configuration')) {
185  $cache->remove('sites-configuration');
186  }
187  }
188 
192  protected function ‪setWorkspaceId(int $workspaceId)
193  {
194  $this->backendUser->workspace = $workspaceId;
195  GeneralUtility::makeInstance(Context::class)->setAspect('workspace', new WorkspaceAspect($workspaceId));
196  }
197 
201  protected function ‪getActionService()
202  {
203  return GeneralUtility::makeInstance(
204  ActionService::class
205  );
206  }
207 
211  protected function ‪importScenarioDataSet($dataSetName)
212  {
213  $fileName = rtrim($this->scenarioDataSetDirectory, '/') . '/' . $dataSetName . '.csv';
214  $fileName = GeneralUtility::getFileAbsFileName($fileName);
215  $this->importCSVDataSet($fileName);
216  }
217 
218  protected function ‪assertAssertionDataSet($dataSetName)
219  {
220  $fileName = rtrim($this->assertionDataSetDirectory, '/') . '/' . $dataSetName . '.csv';
221  $fileName = GeneralUtility::getFileAbsFileName($fileName);
222  $this->assertCSVDataSet($fileName);
223  }
224 
230  protected function ‪assertErrorLogEntries(array $expectedMessages = null)
231  {
232  if ($this->expectedErrorLogEntries === null && $expectedMessages === null) {
233  return;
234  }
235 
236  if ($expectedMessages !== null) {
237  ‪$expectedErrorLogEntries = count($expectedMessages);
238  } else {
239  ‪$expectedErrorLogEntries = (int)$this->expectedErrorLogEntries;
240  }
241 
242  $queryBuilder = $this->getConnectionPool()
243  ->getQueryBuilderForTable('sys_log');
244  $queryBuilder->getRestrictions()->removeAll();
245  $statement = $queryBuilder
246  ->select('*')
247  ->from('sys_log')
248  ->where(
249  $queryBuilder->expr()->in(
250  'error',
251  $queryBuilder->createNamedParameter([1, 2], Connection::PARAM_INT_ARRAY)
252  )
253  )
254  ->execute();
255 
256  $actualErrorLogEntries = (int)$queryBuilder
257  ->count('uid')
258  ->execute()
259  ->fetchColumn(0);
260 
261  $entryMessages = array_map(
262  function (array $entry) {
263  $entryData = unserialize($entry['log_data'], ['allowed_classes' => false]);
264  return vsprintf($entry['details'], $entryData);
265  },
266  $statement->fetchAll()
267  );
268 
269  if ($expectedMessages !== null) {
270  self::assertEqualsCanonicalizing($expectedMessages, $entryMessages);
271  } elseif ($actualErrorLogEntries === ‪$expectedErrorLogEntries) {
272  self::assertSame(‪$expectedErrorLogEntries, $actualErrorLogEntries);
273  } else {
274  $failureMessage = sprintf(
275  'Expected %d entries in sys_log, but got %d' . LF,
276  $expectedMessages,
277  $actualErrorLogEntries
278  );
279  $failureMessage .= '* ' . implode(LF . '* ', $entryMessages) . LF;
280  self::fail($failureMessage);
281  }
282  }
283 
287  protected function ‪assertCleanReferenceIndex(): void
288  {
289  $referenceIndex = GeneralUtility::makeInstance(ReferenceIndex::class);
290  $referenceIndexFixResult = $referenceIndex->updateIndex(true);
291  if (count($referenceIndexFixResult['errors']) > 0) {
292  self::fail('Reference index not clean. ' . LF . implode(LF, $referenceIndexFixResult['errors']));
293  }
294  }
295 
299  protected function ‪getRequestSectionHasRecordConstraint()
300  {
301  return new HasRecordConstraint();
302  }
303 
308  {
309  return new DoesNotHaveRecordConstraint();
310  }
311 
316  {
317  return new StructureHasRecordConstraint();
318  }
319 
324  {
325  return new StructureDoesNotHaveRecordConstraint();
326  }
327 }
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\assertCleanReferenceIndex
‪assertCleanReferenceIndex()
Definition: AbstractDataHandlerActionTestCase.php:277
‪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:143
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\getRequestSectionHasRecordConstraint
‪HasRecordConstraint getRequestSectionHasRecordConstraint()
Definition: AbstractDataHandlerActionTestCase.php:289
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\setUp
‪setUp()
Definition: AbstractDataHandlerActionTestCase.php:113
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\getRequestSectionStructureHasRecordConstraint
‪StructureHasRecordConstraint getRequestSectionStructureHasRecordConstraint()
Definition: AbstractDataHandlerActionTestCase.php:305
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\$expectedErrorLogEntries
‪int null $expectedErrorLogEntries
Definition: AbstractDataHandlerActionTestCase.php:57
‪TYPO3\CMS\Core\Database\ReferenceIndex
Definition: ReferenceIndex.php:48
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\VALUE_BackendUserId
‪const VALUE_BackendUserId
Definition: AbstractDataHandlerActionTestCase.php:38
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\$pathsToLinkInTestInstance
‪array $pathsToLinkInTestInstance
Definition: AbstractDataHandlerActionTestCase.php:67
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\getRequestSectionStructureDoesNotHaveRecordConstraint
‪StructureDoesNotHaveRecordConstraint getRequestSectionStructureDoesNotHaveRecordConstraint()
Definition: AbstractDataHandlerActionTestCase.php:313
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase
Definition: AbstractDataHandlerActionTestCase.php:37
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\$backendUser
‪TYPO3 CMS Core Authentication BackendUserAuthentication $backendUser
Definition: AbstractDataHandlerActionTestCase.php:81
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\assertErrorLogEntries
‪assertErrorLogEntries(array $expectedMessages=null)
Definition: AbstractDataHandlerActionTestCase.php:220
‪TYPO3\CMS\Core\Core\Bootstrap\initializeLanguageObject
‪static initializeLanguageObject()
Definition: Bootstrap.php:617
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir_deep
‪static mkdir_deep($directory)
Definition: GeneralUtility.php:2022
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\$scenarioDataSetDirectory
‪string $scenarioDataSetDirectory
Definition: AbstractDataHandlerActionTestCase.php:46
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\tearDown
‪tearDown()
Definition: AbstractDataHandlerActionTestCase.php:125
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\setWorkspaceId
‪setWorkspaceId(int $workspaceId)
Definition: AbstractDataHandlerActionTestCase.php:182
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:35
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\$assertionDataSetDirectory
‪string $assertionDataSetDirectory
Definition: AbstractDataHandlerActionTestCase.php:50
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\$recordIds
‪array $recordIds
Definition: AbstractDataHandlerActionTestCase.php:73
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\assertAssertionDataSet
‪assertAssertionDataSet($dataSetName)
Definition: AbstractDataHandlerActionTestCase.php:208
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\getRequestSectionDoesNotHaveRecordConstraint
‪DoesNotHaveRecordConstraint getRequestSectionDoesNotHaveRecordConstraint()
Definition: AbstractDataHandlerActionTestCase.php:297
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\$actionService
‪ActionService $actionService
Definition: AbstractDataHandlerActionTestCase.php:77
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\getActionService
‪ActionService getActionService()
Definition: AbstractDataHandlerActionTestCase.php:191
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:36
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\$siteLanguageConfiguration
‪array $siteLanguageConfiguration
Definition: AbstractDataHandlerActionTestCase.php:86
‪TYPO3\CMS\Core\Core\Bootstrap
Definition: Bootstrap.php:66
‪TYPO3\CMS\Core\Tests\Functional\DataHandling
Definition: AbstractDataHandlerActionTestCase.php:16
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\$assertCleanReferenceIndex
‪bool $assertCleanReferenceIndex
Definition: AbstractDataHandlerActionTestCase.php:42
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility\writeFile
‪static bool writeFile($file, $content, $changePermissions=false)
Definition: GeneralUtility.php:1836
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\importScenarioDataSet
‪importScenarioDataSet($dataSetName)
Definition: AbstractDataHandlerActionTestCase.php:201
‪TYPO3\CMS\Core\Tests\Functional\DataHandling\AbstractDataHandlerActionTestCase\$testExtensionsToLoad
‪array $testExtensionsToLoad
Definition: AbstractDataHandlerActionTestCase.php:61