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