[TASK] Split DataHandler::copyRecord_procBasedOnFieldType
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / FunctionalTestCase.php
index aa92049..34e8535 100644 (file)
@@ -1,28 +1,20 @@
 <?php
 namespace TYPO3\CMS\Core\Tests;
 
-/***************************************************************
- * Copyright notice
+/*
+ * This file is part of the TYPO3 CMS project.
  *
- * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
- * All rights reserved
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
  *
- * This script is part of the TYPO3 project. The TYPO3 project is
- * free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
  *
- * The GNU General Public License can be found at
- * http://www.gnu.org/copyleft/gpl.html.
- *
- * This script is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * This copyright notice MUST APPEAR in all copies of the script!
- ***************************************************************/
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Tests\Functional\Framework\Frontend\Response;
 
 /**
  * Base test case class for functional tests, all TYPO3 CMS
@@ -55,7 +47,6 @@ namespace TYPO3\CMS\Core\Tests;
  *     typo3/sysext/core/Tests/Functional/DataHandling/DataHandlerTest.php
  */
 abstract class FunctionalTestCase extends BaseTestCase {
-
        /**
         * Core extensions to load.
         *
@@ -128,6 +119,40 @@ abstract class FunctionalTestCase extends BaseTestCase {
        protected $pathsToLinkInTestInstance = array();
 
        /**
+        * This configuration array is merged with TYPO3_CONF_VARS
+        * that are set in default configuration and factory configuration
+        *
+        * @var array
+        */
+       protected $configurationToUseInTestInstance = array();
+
+       /**
+        * Array of folders that should be created inside the test instance document root.
+        *
+        * This property will stay empty in this abstract, so it is possible
+        * to just overwrite it in extending classes. Path noted here will
+        * be linked for every test of a test case and it is not possible to change
+        * the list of folders between single tests of a test case.
+        *
+        * Per default the following folder are created
+        * /fileadmin
+        * /typo3temp
+        * /typo3conf
+        * /typo3conf/ext
+        * /uploads
+        *
+        * To create additional folders add the paths to this array. Given paths are expected to be
+        * relative to the test instance root and have to begin with a slash. Example:
+        *
+        * array(
+        *   'fileadmin/user_upload'
+        * );
+        *
+        * @var array
+        */
+       protected $additionalFoldersToCreate = array();
+
+       /**
         * Private utility class used in setUp() and tearDown(). Do NOT use in test cases!
         *
         * @var \TYPO3\CMS\Core\Tests\FunctionalTestCaseBootstrapUtility
@@ -135,44 +160,35 @@ abstract class FunctionalTestCase extends BaseTestCase {
        private $bootstrapUtility = NULL;
 
        /**
+        * Path to TYPO3 CMS test installation for this test case
+        *
+        * @var string
+        */
+       private $instancePath;
+
+       /**
         * Set up creates a test instance and database.
         *
         * This method should be called with parent::setUp() in your test cases!
         *
         * @return void
         */
-       public function setUp() {
+       protected function setUp() {
                if (!defined('ORIGINAL_ROOT')) {
                        $this->markTestSkipped('Functional tests must be called through phpunit on CLI');
                }
                $this->bootstrapUtility = new FunctionalTestCaseBootstrapUtility();
-               $this->bootstrapUtility->setUp(
+               $this->instancePath = $this->bootstrapUtility->setUp(
                        get_class($this),
                        $this->coreExtensionsToLoad,
                        $this->testExtensionsToLoad,
-                       $this->pathsToLinkInTestInstance
+                       $this->pathsToLinkInTestInstance,
+                       $this->configurationToUseInTestInstance,
+                       $this->additionalFoldersToCreate
                );
        }
 
        /**
-        * Tear down destroys the instance and database.
-        *
-        * This method should be called with parent::tearDown() in your test cases!
-        *
-        * @throws Exception
-        * @return void
-        */
-       public function tearDown() {
-               if (!($this->bootstrapUtility instanceof FunctionalTestCaseBootstrapUtility)) {
-                       throw new Exception(
-                               'Bootstrap utility not set. Is parent::setUp() called in setUp()?',
-                               1376826527
-                       );
-               }
-               $this->bootstrapUtility->tearDown();
-       }
-
-       /**
         * Get DatabaseConnection instance - $GLOBALS['TYPO3_DB']
         *
         * This method should be used instead of direct access to
@@ -180,7 +196,7 @@ abstract class FunctionalTestCase extends BaseTestCase {
         *
         * @return \TYPO3\CMS\Core\Database\DatabaseConnection
         */
-       protected function getDatabase() {
+       protected function getDatabaseConnection() {
                return $GLOBALS['TYPO3_DB'];
        }
 
@@ -193,13 +209,13 @@ abstract class FunctionalTestCase extends BaseTestCase {
         */
        protected function setUpBackendUserFromFixture($userUid) {
                $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/be_users.xml');
-               $database = $this->getDatabase();
-               $userRow = $database->exec_SELECTgetSingleRow('*', 'be_users', 'uid = ' . $userUid);
+               $database = $this->getDatabaseConnection();
+               $userRow = $database->exec_SELECTgetSingleRow('*', 'be_users', 'uid = ' . (int)$userUid);
 
                /** @var $backendUser \TYPO3\CMS\Core\Authentication\BackendUserAuthentication */
-               $backendUser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
+               $backendUser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::class);
                $sessionId = $backendUser->createSessionId();
-               $_SERVER['HTTP_COOKIE'] = 'be_typo_user=' . $sessionId . '; path=/';
+               $_COOKIE['be_typo_user'] = $sessionId;
                $backendUser->id = $sessionId;
                $backendUser->sendNoCacheHeaders = FALSE;
                $backendUser->dontSetCookie = TRUE;
@@ -233,7 +249,7 @@ abstract class FunctionalTestCase extends BaseTestCase {
                        );
                }
 
-               $database = $this->getDatabase();
+               $database = $this->getDatabaseConnection();
 
                $xml = simplexml_load_file($path);
                $foreignKeys = array();
@@ -253,7 +269,7 @@ abstract class FunctionalTestCase extends BaseTestCase {
                                } elseif (isset($column['is-NULL']) && ($column['is-NULL'] === 'yes')) {
                                        $columnValue = NULL;
                                } else {
-                                       $columnValue = (string) $table->$columnName;
+                                       $columnValue = (string)$table->$columnName;
                                }
 
                                $insertArray[$columnName] = $columnValue;
@@ -263,14 +279,102 @@ abstract class FunctionalTestCase extends BaseTestCase {
                        $result = $database->exec_INSERTquery($tableName, $insertArray);
                        if ($result === FALSE) {
                                throw new Exception(
-                                       'Error when processing fixture file: ' . $path . ' Can not insert data to table ' . $tableName,
+                                       'Error when processing fixture file: ' . $path . ' Can not insert data to table ' . $tableName . ': ' . $database->sql_error(),
                                        1376746262
                                );
                        }
                        if (isset($table['id'])) {
-                               $elementId = (string) $table['id'];
+                               $elementId = (string)$table['id'];
                                $foreignKeys[$tableName][$elementId] = $database->sql_insert_id();
                        }
                }
        }
+
+       /**
+        * @param int $pageId
+        * @param array $typoScriptFiles
+        */
+       protected function setUpFrontendRootPage($pageId, array $typoScriptFiles = array()) {
+               $pageId = (int)$pageId;
+               $page = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('*', 'pages', 'uid=' . $pageId);
+
+               if (empty($page)) {
+                       $this->fail('Cannot set up frontend root page "' . $pageId . '"');
+               }
+
+               $pagesFields = array(
+                       'is_siteroot' => 1
+               );
+
+               $this->getDatabaseConnection()->exec_UPDATEquery('pages', 'uid=' . $pageId, $pagesFields);
+
+               $templateFields = array(
+                       'pid' => $pageId,
+                       'title' => '',
+                       'config' => '',
+                       'clear' => 3,
+                       'root' => 1,
+               );
+
+               foreach ($typoScriptFiles as $typoScriptFile) {
+                       $templateFields['config'] .= '<INCLUDE_TYPOSCRIPT: source="FILE:' . $typoScriptFile . '">' . LF;
+               }
+
+               $this->getDatabaseConnection()->exec_INSERTquery('sys_template', $templateFields);
+       }
+
+       /**
+        * @param int $pageId
+        * @param int $languageId
+        * @param int $backendUserId
+        * @param int $workspaceId
+        * @param bool $failOnFailure
+        * @param int $frontendUserId
+        * @return Response
+        */
+       protected function getFrontendResponse($pageId, $languageId = 0, $backendUserId = 0, $workspaceId = 0, $failOnFailure = TRUE, $frontendUserId = 0) {
+               $pageId = (int)$pageId;
+               $languageId = (int)$languageId;
+
+               $additionalParameter = '';
+
+               if (!empty($frontendUserId)) {
+                       $additionalParameter .= '&frontendUserId=' . (int)$frontendUserId;
+               }
+               if (!empty($backendUserId)) {
+                       $additionalParameter .= '&backendUserId=' . (int)$backendUserId;
+               }
+               if (!empty($workspaceId)) {
+                       $additionalParameter .= '&workspaceId=' . (int)$workspaceId;
+               }
+
+               $arguments = array(
+                       'documentRoot' => $this->instancePath,
+                       'requestUrl' => 'http://localhost/?id=' . $pageId . '&L=' . $languageId . $additionalParameter,
+               );
+
+               $template = new \Text_Template(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/request.tpl');
+               $template->setVar(
+                       array(
+                               'arguments' => var_export($arguments, TRUE),
+                               'originalRoot' => ORIGINAL_ROOT,
+                       )
+               );
+
+               $php = \PHPUnit_Util_PHP::factory();
+               $response = $php->runJob($template->render());
+               $result = json_decode($response['stdout'], TRUE);
+
+               if ($result === NULL) {
+                       $this->fail('Frontend Response is empty');
+               }
+
+               if ($failOnFailure && $result['status'] === Response::STATUS_Failure) {
+                       $this->fail('Frontend Response has failure:' . LF . $result['error']);
+               }
+
+               $response = new Response($result['status'], $result['content'], $result['error']);
+               return $response;
+       }
+
 }