2 namespace TYPO3\CMS\Core\Tests
;
4 /***************************************************************
7 * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
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.
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
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.
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
28 * Base test case class for functional tests, all TYPO3 CMS
29 * functional tests should extend from this class!
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.
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.
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.
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
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
57 abstract class FunctionalTestCase
extends BaseTestCase
{
60 * Core extensions to load.
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.
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.
71 * Required core extensions like core, cms, extbase and so on are loaded
72 * automatically, so there is no need to add them here. See constant
73 * REQUIRED_EXTENSIONS for a list of automatically loaded extensions.
77 protected $coreExtensionsToLoad = array();
80 * Array of test/fixture extensions paths that should be loaded for a test.
82 * This property will stay empty in this abstract, so it is possible
83 * to just overwrite it in extending classes. Extensions noted here will
84 * be loaded for every test of a test case and it is not possible to change
85 * the list of loaded extensions between single tests of a test case.
87 * Given path is expected to be relative to your document root, example:
90 * 'typo3conf/ext/some_extension/Tests/Functional/Fixtures/Extensions/test_extension',
91 * 'typo3conf/ext/base_extension',
94 * Extensions in this array are linked to the test instance, loaded
95 * and their ext_tables.sql will be applied.
99 protected $testExtensionsToLoad = array();
102 * Private utility class used in setUp() and tearDown(). Do NOT use in test cases!
104 * @var \TYPO3\CMS\Core\Tests\FunctionalTestCaseBootstrapUtility
106 private $bootstrapUtility = NULL;
109 * Set up creates a test instance and database.
111 * This method should be called with parent::setUp() in your test cases!
115 public function setUp() {
116 if (!defined('ORIGINAL_ROOT')) {
117 $this->markTestSkipped('Functional tests must be called through phpunit on CLI');
119 $this->bootstrapUtility
= new FunctionalTestCaseBootstrapUtility();
120 $this->bootstrapUtility
->setUp(get_class($this), $this->coreExtensionsToLoad
, $this->testExtensionsToLoad
);
124 * Tear down destroys the instance and database.
126 * This method should be called with parent::tearDown() in your test cases!
131 public function tearDown() {
132 if (!($this->bootstrapUtility
instanceof FunctionalTestCaseBootstrapUtility
)) {
134 'Bootstrap utility not set. Is parent::setUp() called in setUp()?',
138 $this->bootstrapUtility
->tearDown();
142 * Get DatabaseConnection instance - $GLOBALS['TYPO3_DB']
144 * This method should be used instead of direct access to
145 * $GLOBALS['TYPO3_DB'] for easy IDE auto completion.
147 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
149 protected function getDatabase() {
150 return $GLOBALS['TYPO3_DB'];
154 * Initialize backend user
156 * @param int $userUid uid of the user we want to initialize. This user must exist in the fixture file
159 protected function setUpBackendUserFromFixture($userUid) {
160 $this->importDataSet(ORIGINAL_ROOT
. 'typo3/sysext/core/Tests/Functional/Fixtures/be_users.xml');
161 $database = $this->getDatabase();
162 $userRow = $database->exec_SELECTgetSingleRow('*', 'be_users', 'uid = ' . $userUid);
164 /** @var $backendUser \TYPO3\CMS\Core\Authentication\BackendUserAuthentication */
165 $backendUser = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
166 $sessionId = $backendUser->createSessionId();
167 $_SERVER['HTTP_COOKIE'] = 'be_typo_user=' . $sessionId . '; path=/';
168 $backendUser->id
= $sessionId;
169 $backendUser->sendNoCacheHeaders
= FALSE;
170 $backendUser->dontSetCookie
= TRUE;
171 $backendUser->createUserSession($userRow);
173 $GLOBALS['BE_USER'] = $backendUser;
174 $GLOBALS['BE_USER']->start();
175 if (!is_array($GLOBALS['BE_USER']->user
) ||
!$GLOBALS['BE_USER']->user
['uid']) {
177 'Can not initialize backend user',
181 $GLOBALS['BE_USER']->backendCheckLogin();
185 * Imports a data set represented as XML into the test database,
187 * @param string $path Absolute path to the XML file containing the data set to load
191 protected function importDataSet($path) {
192 if (!is_file($path)) {
194 'Fixture file ' . $path . ' not found',
199 $database = $this->getDatabase();
201 $xml = simplexml_load_file($path);
202 $foreignKeys = array();
204 /** @var $table \SimpleXMLElement */
205 foreach ($xml->children() as $table) {
206 $insertArray = array();
208 /** @var $column \SimpleXMLElement */
209 foreach ($table->children() as $column) {
210 $columnName = $column->getName();
213 if (isset($column['ref'])) {
214 list($tableName, $elementId) = explode('#', $column['ref']);
215 $columnValue = $foreignKeys[$tableName][$elementId];
216 } elseif (isset($column['is-NULL']) && ($column['is-NULL'] === 'yes')) {
219 $columnValue = (string) $table->$columnName;
222 $insertArray[$columnName] = $columnValue;
225 $tableName = $table->getName();
226 $result = $database->exec_INSERTquery($tableName, $insertArray);
227 if ($result === FALSE) {
229 'Error when processing fixture file: ' . $path . ' Can not insert data to table ' . $tableName,
233 if (isset($table['id'])) {
234 $elementId = (string) $table['id'];
235 $foreignKeys[$tableName][$elementId] = $database->sql_insert_id();