[TASK] Move CSV fixtures handling to FunctionalTestCase
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / DataHandling / AbstractDataHandlerActionTestCase.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Functional\DataHandling;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Database\Connection;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Functional test for the DataHandler
22 */
23 abstract class AbstractDataHandlerActionTestCase extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
24 {
25 const VALUE_BackendUserId = 1;
26
27 /**
28 * @var string
29 */
30 protected $scenarioDataSetDirectory;
31
32 /**
33 * @var string
34 */
35 protected $assertionDataSetDirectory;
36
37 /**
38 * If this value is NULL, log entries are not considered.
39 * If it's an integer value, the number of log entries is asserted.
40 *
41 * @var NULL|int
42 */
43 protected $expectedErrorLogEntries = 0;
44
45 /**
46 * @var array
47 */
48 protected $testExtensionsToLoad = [
49 'typo3/sysext/core/Tests/Functional/Fixtures/Extensions/irre_tutorial',
50 ];
51
52 /**
53 * @var array
54 */
55 protected $pathsToLinkInTestInstance = [
56 'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/AdditionalConfiguration.php' => 'typo3conf/AdditionalConfiguration.php',
57 ];
58
59 /**
60 * @var array
61 */
62 protected $recordIds = [];
63
64 /**
65 * @var \TYPO3\CMS\Core\Tests\Functional\DataHandling\Framework\ActionService
66 */
67 protected $actionService;
68
69 /**
70 * @var \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
71 */
72 protected $backendUser;
73
74 protected function setUp()
75 {
76 parent::setUp();
77
78 $this->backendUser = $this->setUpBackendUserFromFixture(self::VALUE_BackendUserId);
79 // By default make tests on live workspace
80 $this->backendUser->workspace = 0;
81
82 $this->actionService = $this->getActionService();
83 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()->initializeLanguageObject();
84 }
85
86 protected function tearDown()
87 {
88 $this->assertErrorLogEntries();
89 unset($this->actionService);
90 unset($this->recordIds);
91 parent::tearDown();
92 }
93
94 /**
95 * @return \TYPO3\CMS\Core\Tests\Functional\DataHandling\Framework\ActionService
96 */
97 protected function getActionService()
98 {
99 return GeneralUtility::makeInstance(
100 \TYPO3\CMS\Core\Tests\Functional\DataHandling\Framework\ActionService::class
101 );
102 }
103
104 /**
105 * @param string $dataSetName
106 */
107 protected function importScenarioDataSet($dataSetName)
108 {
109 $fileName = rtrim($this->scenarioDataSetDirectory, '/') . '/' . $dataSetName . '.csv';
110 $fileName = GeneralUtility::getFileAbsFileName($fileName);
111 $this->importCSVDataSet($fileName);
112 }
113
114 protected function assertAssertionDataSet($dataSetName)
115 {
116 $fileName = rtrim($this->assertionDataSetDirectory, '/') . '/' . $dataSetName . '.csv';
117 $fileName = GeneralUtility::getFileAbsFileName($fileName);
118 $this->assertCSVDataSet($fileName);
119 }
120
121 /**
122 * Asserts correct number of warning and error log entries.
123 *
124 * @return void
125 */
126 protected function assertErrorLogEntries()
127 {
128 if ($this->expectedErrorLogEntries === null) {
129 return;
130 }
131
132 $queryBuilder = $this->getConnectionPool()
133 ->getQueryBuilderForTable('sys_log');
134 $queryBuilder->getRestrictions()->removeAll();
135 $statement = $queryBuilder
136 ->select('*')
137 ->from('sys_log')
138 ->where(
139 $queryBuilder->expr()->in(
140 'error',
141 $queryBuilder->createNamedParameter([1, 2], Connection::PARAM_INT_ARRAY)
142 )
143 )
144 ->execute();
145
146 $actualErrorLogEntries = $statement->rowCount();
147 if ($actualErrorLogEntries === $this->expectedErrorLogEntries) {
148 $this->assertSame($this->expectedErrorLogEntries, $actualErrorLogEntries);
149 } else {
150 $failureMessage = 'Expected ' . $this->expectedErrorLogEntries . ' entries in sys_log, but got ' . $actualErrorLogEntries . LF;
151 while ($entry = $statement->fetch()) {
152 $entryData = unserialize($entry['log_data']);
153 $entryMessage = vsprintf($entry['details'], $entryData);
154 $failureMessage .= '* ' . $entryMessage . LF;
155 }
156 $this->fail($failureMessage);
157 }
158 }
159
160 /**
161 * @return \TYPO3\TestingFramework\Core\Functional\Framework\Constraint\RequestSection\HasRecordConstraint
162 */
163 protected function getRequestSectionHasRecordConstraint()
164 {
165 return new \TYPO3\TestingFramework\Core\Functional\Framework\Constraint\RequestSection\HasRecordConstraint();
166 }
167
168 /**
169 * @return \TYPO3\TestingFramework\Core\Functional\Framework\Constraint\RequestSection\DoesNotHaveRecordConstraint
170 */
171 protected function getRequestSectionDoesNotHaveRecordConstraint()
172 {
173 return new \TYPO3\TestingFramework\Core\Functional\Framework\Constraint\RequestSection\DoesNotHaveRecordConstraint();
174 }
175
176 /**
177 * @return \TYPO3\TestingFramework\Core\Functional\Framework\Constraint\RequestSection\StructureHasRecordConstraint
178 */
179 protected function getRequestSectionStructureHasRecordConstraint()
180 {
181 return new \TYPO3\TestingFramework\Core\Functional\Framework\Constraint\RequestSection\StructureHasRecordConstraint();
182 }
183
184 /**
185 * @return \TYPO3\TestingFramework\Core\Functional\Framework\Constraint\RequestSection\StructureDoesNotHaveRecordConstraint
186 */
187 protected function getRequestSectionStructureDoesNotHaveRecordConstraint()
188 {
189 return new \TYPO3\TestingFramework\Core\Functional\Framework\Constraint\RequestSection\StructureDoesNotHaveRecordConstraint();
190 }
191 }