[BUGFIX] Tame PackageStates.php in functional tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / FunctionalTestCase.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Base test case class for functional tests, all TYPO3 CMS
29 * functional tests should extend from this class!
30 *
31 * If functional tests need additional setUp() and tearDown() code,
32 * they *must* call parent::setUp() and parent::tearDown() to properly
33 * set up and destroy the test system.
34 *
35 * The functional test system creates a full new TYPO3 CMS instance
36 * within typo3temp/ of the base system and the bootstraps this TYPO3 instance.
37 * This abstract class takes care of creating this instance with its
38 * folder structure and a LocalConfiguration, creates an own database
39 * for each test run and imports tables of loaded extensions.
40 *
41 * Functional tests must be run standalone (calling native phpunit
42 * directly) and can not be executed by eg. the ext:phpunit backend module.
43 * Additionally, the script must be called from the document root
44 * of the instance, otherwise path calculation is not successfully.
45 *
46 * Call whole functional test suite, example:
47 * - cd /var/www/t3master/foo # Document root of CMS instance, here is index.php of frontend
48 * - ./typo3conf/ext/phpunit/Composer/vendor/bin/phpunit -c typo3/sysext/core/Build/FunctionalTests.xml
49 *
50 * Call single test case, example:
51 * - cd /var/www/t3master/foo # Document root of CMS instance, here is index.php of frontend
52 * - ./typo3conf/ext/phpunit/Composer/vendor/bin/phpunit \
53 * --process-isolation \
54 * --bootstrap typo3/sysext/core/Build/FunctionalTestsBootstrap.php \
55 * typo3/sysext/core/Tests/Functional/DataHandling/DataHandlerTest.php
56 */
57 abstract class FunctionalTestCase extends BaseTestCase {
58
59 /**
60 * Core extensions to load.
61 *
62 * If the test case needs additional core extensions as requirement,
63 * they can be noted here and will be added to LocalConfiguration
64 * extension list and ext_tables.sql of those extensions will be applied.
65 *
66 * This property will stay empty in this abstract, so it is possible
67 * to just overwrite it in extending classes. Extensions noted here will
68 * be loaded for every test of a test case and it is not possible to change
69 * the list of loaded extensions between single tests of a test case.
70 *
71 * A default list of core extensions is always loaded.
72 *
73 * @see FunctionalTestCaseUtility $defaultActivatedCoreExtensions
74 * @var array
75 */
76 protected $coreExtensionsToLoad = array();
77
78 /**
79 * Array of test/fixture extensions paths that should be loaded for a test.
80 *
81 * This property will stay empty in this abstract, so it is possible
82 * to just overwrite it in extending classes. Extensions noted here will
83 * be loaded for every test of a test case and it is not possible to change
84 * the list of loaded extensions between single tests of a test case.
85 *
86 * Given path is expected to be relative to your document root, example:
87 *
88 * array(
89 * 'typo3conf/ext/some_extension/Tests/Functional/Fixtures/Extensions/test_extension',
90 * 'typo3conf/ext/base_extension',
91 * );
92 *
93 * Extensions in this array are linked to the test instance, loaded
94 * and their ext_tables.sql will be applied.
95 *
96 * @var array
97 */
98 protected $testExtensionsToLoad = array();
99
100 /**
101 * Private utility class used in setUp() and tearDown(). Do NOT use in test cases!
102 *
103 * @var \TYPO3\CMS\Core\Tests\FunctionalTestCaseBootstrapUtility
104 */
105 private $bootstrapUtility = NULL;
106
107 /**
108 * Set up creates a test instance and database.
109 *
110 * This method should be called with parent::setUp() in your test cases!
111 *
112 * @return void
113 */
114 public function setUp() {
115 if (!defined('ORIGINAL_ROOT')) {
116 $this->markTestSkipped('Functional tests must be called through phpunit on CLI');
117 }
118 $this->bootstrapUtility = new FunctionalTestCaseBootstrapUtility();
119 $this->bootstrapUtility->setUp(get_class($this), $this->coreExtensionsToLoad, $this->testExtensionsToLoad);
120 }
121
122 /**
123 * Tear down destroys the instance and database.
124 *
125 * This method should be called with parent::tearDown() in your test cases!
126 *
127 * @throws Exception
128 * @return void
129 */
130 public function tearDown() {
131 if (!($this->bootstrapUtility instanceof FunctionalTestCaseBootstrapUtility)) {
132 throw new Exception(
133 'Bootstrap utility not set. Is parent::setUp() called in setUp()?',
134 1376826527
135 );
136 }
137 $this->bootstrapUtility->tearDown();
138 }
139
140 /**
141 * Get DatabaseConnection instance - $GLOBALS['TYPO3_DB']
142 *
143 * This method should be used instead of direct access to
144 * $GLOBALS['TYPO3_DB'] for easy IDE auto completion.
145 *
146 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
147 */
148 protected function getDatabase() {
149 return $GLOBALS['TYPO3_DB'];
150 }
151
152 /**
153 * Initialize backend user
154 *
155 * @param int $userUid uid of the user we want to initialize. This user must exist in the fixture file
156 * @throws Exception
157 */
158 protected function setUpBackendUserFromFixture($userUid) {
159 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/be_users.xml');
160 $database = $this->getDatabase();
161 $userRow = $database->exec_SELECTgetSingleRow('*', 'be_users', 'uid = ' . $userUid);
162
163 /** @var $backendUser \TYPO3\CMS\Core\Authentication\BackendUserAuthentication */
164 $backendUser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
165 $sessionId = $backendUser->createSessionId();
166 $_SERVER['HTTP_COOKIE'] = 'be_typo_user=' . $sessionId . '; path=/';
167 $backendUser->id = $sessionId;
168 $backendUser->sendNoCacheHeaders = FALSE;
169 $backendUser->dontSetCookie = TRUE;
170 $backendUser->createUserSession($userRow);
171
172 $GLOBALS['BE_USER'] = $backendUser;
173 $GLOBALS['BE_USER']->start();
174 if (!is_array($GLOBALS['BE_USER']->user) || !$GLOBALS['BE_USER']->user['uid']) {
175 throw new Exception(
176 'Can not initialize backend user',
177 1377095807
178 );
179 }
180 $GLOBALS['BE_USER']->backendCheckLogin();
181 }
182
183 /**
184 * Imports a data set represented as XML into the test database,
185 *
186 * @param string $path Absolute path to the XML file containing the data set to load
187 * @return void
188 * @throws Exception
189 */
190 protected function importDataSet($path) {
191 if (!is_file($path)) {
192 throw new Exception(
193 'Fixture file ' . $path . ' not found',
194 1376746261
195 );
196 }
197
198 $database = $this->getDatabase();
199
200 $xml = simplexml_load_file($path);
201 $foreignKeys = array();
202
203 /** @var $table \SimpleXMLElement */
204 foreach ($xml->children() as $table) {
205 $insertArray = array();
206
207 /** @var $column \SimpleXMLElement */
208 foreach ($table->children() as $column) {
209 $columnName = $column->getName();
210 $columnValue = NULL;
211
212 if (isset($column['ref'])) {
213 list($tableName, $elementId) = explode('#', $column['ref']);
214 $columnValue = $foreignKeys[$tableName][$elementId];
215 } elseif (isset($column['is-NULL']) && ($column['is-NULL'] === 'yes')) {
216 $columnValue = NULL;
217 } else {
218 $columnValue = (string) $table->$columnName;
219 }
220
221 $insertArray[$columnName] = $columnValue;
222 }
223
224 $tableName = $table->getName();
225 $result = $database->exec_INSERTquery($tableName, $insertArray);
226 if ($result === FALSE) {
227 throw new Exception(
228 'Error when processing fixture file: ' . $path . ' Can not insert data to table ' . $tableName,
229 1376746262
230 );
231 }
232 if (isset($table['id'])) {
233 $elementId = (string) $table['id'];
234 $foreignKeys[$tableName][$elementId] = $database->sql_insert_id();
235 }
236 }
237 }
238 }