[TASK] Bootstrap for Unit Tests 96/23396/7
authorHelmut Hummel <helmut.hummel@typo3.org>
Wed, 28 Aug 2013 10:31:08 +0000 (12:31 +0200)
committerHelmut Hummel <helmut.hummel@typo3.org>
Sun, 13 Oct 2013 22:09:33 +0000 (00:09 +0200)
Until now we still needed the phpunit extension
to be installed and cli access to be configured
to execute unit tests.

This change introduces a bootstrap file which
can be used to execute the unit test suite
without the extension.

It is of course still possible to use the PHPUnit
and VHS framework files present in the extension.

This change also splits user initialization into
separate bootstrap methods and allows the PATH_site
and PATH_thisScript constants to be set outside
the bootstrap.

Activate Travis CI to make use of this.

Resolves: #51436
Releases: 6.2
Change-Id: I43f51896fd9895ead8d0084f37d34f3347f1c5e5
Reviewed-on: https://review.typo3.org/23396
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Tested-by: Philipp Gampe
Reviewed-by: Philipp Gampe
Tested-by: Markus Klein
Reviewed-by: Helmut Hummel
Tested-by: Helmut Hummel
.travis.yml
typo3/cli_dispatch.phpsh
typo3/init.php
typo3/sysext/core/Build/UnitTests.xml
typo3/sysext/core/Build/UnitTestsBootstrap.php [new file with mode: 0644]
typo3/sysext/core/Classes/Core/Bootstrap.php
typo3/sysext/core/Classes/Core/SystemEnvironmentBuilder.php

index 95dde70..a2b2d26 100644 (file)
@@ -54,7 +54,7 @@ script:
     if [[ "$PHPLINT" == "1" ]]; then
        phpLint all
     elif [[ "$UNITTESTS" == "1" ]]; then
-       php $PWD/typo3/cli_dispatch.phpsh phpunit -c typo3/sysext/core/Build/UnitTests.xml
+       ./typo3conf/ext/phpunit/Composer/vendor/bin/phpunit -c typo3/sysext/core/Build/UnitTests.xml
     elif [[ "$FUNCTIONALTESTS" == "1" ]]; then
        ./typo3conf/ext/phpunit/Composer/vendor/bin/phpunit -c typo3/sysext/core/Build/FunctionalTests.xml
     else
index 193544e..f9ba3f8 100755 (executable)
@@ -55,6 +55,7 @@ require __DIR__ . '/sysext/core/Classes/Core/Bootstrap.php';
 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()
        ->loadExtensionTables(TRUE)
        ->initializeBackendUser()
+       ->initializeBackendAuthentication()
        ->initializeBackendUserMounts()
        ->initializeLanguageObject();
 
index 954c99f..5bc3b31 100644 (file)
@@ -69,6 +69,7 @@ require __DIR__ . '/sysext/core/Classes/Core/Bootstrap.php';
        ->loadExtensionTables(TRUE)
        ->initializeSpriteManager()
        ->initializeBackendUser()
+       ->initializeBackendAuthentication()
        ->initializeBackendUserMounts()
        ->initializeLanguageObject()
        ->initializeModuleMenuObject()
index 925f8eb..3fd0b95 100644 (file)
@@ -1,6 +1,7 @@
 <phpunit
        backupGlobals="true"
        backupStaticAttributes="false"
+       bootstrap="UnitTestsBootstrap.php"
        colors="true"
        convertErrorsToExceptions="true"
        convertWarningsToExceptions="true"
diff --git a/typo3/sysext/core/Build/UnitTestsBootstrap.php b/typo3/sysext/core/Build/UnitTestsBootstrap.php
new file mode 100644 (file)
index 0000000..e6d2436
--- /dev/null
@@ -0,0 +1,111 @@
+<?php
+namespace TYPO3\CMS\Core\Build;
+
+/***************************************************************
+ *  Copyright notice
+ *
+ *  (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
+ *  (c) 2013 Helmut Hummel <helmut.hummel@typo3.org>
+ *  All rights reserved
+ *
+ *  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.
+ *
+ *  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!
+ ***************************************************************/
+
+/**
+ * This file is defined in UnitTests.xml and called by phpunit
+ * before instantiating the test suites, it must also be included
+ * with phpunit parameter --bootstrap if executing single test case classes.
+ *
+ * For easy access to the PHPUnit and VFS framework, it is recommended to install the phpunit TYPO3 Extension
+ * It does not need to be activated, nor a cli user needs to be present.
+ * But it is also possible to use other installations of PHPUnit and VFS
+ *
+ *  * Call whole unit test suite, example:
+ * - cd /var/www/t3master/foo  # Document root of TYPO3 CMS instance (location of index.php)
+ * - typo3conf/ext/phpunit/Composer/vendor/bin/phpunit -c typo3/sysext/core/Build/UnitTests.xml
+ *
+ * Call single test case, example:
+ * - cd /var/www/t3master/foo  # Document root of TYPO3 CMS instance (location of index.php)
+ * - typo3conf/ext/phpunit/Composer/vendor/bin/phpunit \
+ *     --bootstrap typo3/sysext/core/Build/UnitTestsBootstrap.php \
+ *     typo3/sysext/core/Tests/Uinit/DataHandling/DataHandlerTest.php
+ */
+
+/**
+ * Find out web path by environment variable or current working directory
+ */
+if (getenv('ENV_TYPO3_SITE_PATH')) {
+       $webRoot = realpath(getenv('TYPO3_PATH_WEB')) . '/';
+} else {
+       $webRoot = getcwd() . '/';
+}
+
+/**
+ * Fail if configuration is not found
+ */
+if (!file_exists($webRoot . 'typo3conf/LocalConfiguration.php')) {
+       throw new \Exception('TYPO3 web root not found. Call PHPUnit from that directory or set TYPO3_PATH_WEB to it.');
+}
+
+/**
+ * Define basic TYPO3 constants
+ */
+define('PATH_site', $webRoot);
+define('TYPO3_MODE', 'BE');
+define('TYPO3_cliMode', TRUE);
+
+/**
+ * We need to fake the current script to be the cli dispatcher to satisfy some GeneralUtility::getIndpEnv tests
+ * TODO: properly mock these tests
+ */
+define('PATH_thisScript', PATH_site . 'typo3/cli_dispatch.phpsh');
+$_SERVER['SCRIPT_NAME'] = PATH_thisScript;
+
+/**
+ * Basic bootstrap
+ * Can be simplified/ stripped down once the according tests are properly mocked
+ */
+require PATH_site . '/typo3/sysext/core/Classes/Core/Bootstrap.php';
+\TYPO3\CMS\Core\Core\Bootstrap::getInstance()
+       ->baseSetup()
+       ->loadConfigurationAndInitialize(FALSE)
+       // TODO: Maybe load only core extensions here? Also check if we need to change the bootstrap since we have package management now
+       ->loadTypo3LoadedExtAndExtLocalconf(FALSE)
+       ->applyAdditionalConfigurationSettings()
+       // TODO: Unit Tests should be able to run without database connection
+       ->initializeTypo3DbGlobal()
+       ->loadExtensionTables(TRUE)
+       ->initializeBackendUser()
+       // TODO: Fix FAL tests which fatal without initialized storages
+       ->initializeBackendUserMounts()
+       // TODO: Needed at least for FormProtection tests
+       ->initializeLanguageObject();
+
+/**
+ * This is needed for some tests (e.g. FormProtection)
+ * TODO: properly mock these tests
+ */
+$GLOBALS['BE_USER']->user = array(
+       'uid' => 42,
+       'username' => 'unit_test_fake_user',
+);
+
+/**
+ * This is needed for the viewOnClick test not to fail if workspace extension is installed
+ * TODO: properly mock these tests
+ */
+$GLOBALS['BE_USER']->workspace = 0;
\ No newline at end of file
index 6da84ea..2fd81f8 100644 (file)
@@ -1022,8 +1022,18 @@ class Bootstrap {
                // might trigger code which relies on it. See: #45625
                $GLOBALS['BE_USER'] = $backendUser;
                $backendUser->start();
-               $backendUser->checkCLIuser();
-               $backendUser->backendCheckLogin();
+               return $this;
+       }
+
+       /**
+        * Initializes and ensures authenticated access
+        *
+        * @internal This is not a public API method, do not use in own extensions
+        * @return \TYPO3\CMS\Core\Core\Bootstrap
+        */
+       public function initializeBackendAuthentication() {
+               $GLOBALS['BE_USER']->checkCLIuser();
+               $GLOBALS['BE_USER']->backendCheckLogin();
                return $this;
        }
 
index 44e8d56..56d2150 100644 (file)
@@ -145,10 +145,14 @@ class SystemEnvironmentBuilder {
                // All paths are unified between Windows and Unix, so the \ of Windows is substituted to a /
                // Example "/var/www/instance-name/htdocs/typo3conf/ext/wec_map/mod1/index.php"
                // Example "c:/var/www/instance-name/htdocs/typo3/backend.php" for a path in Windows
-               define('PATH_thisScript', self::getPathThisScript());
+               if (!defined('PATH_thisScript')) {
+                       define('PATH_thisScript', self::getPathThisScript());
+               }
                // Absolute path of the document root of the instance with trailing slash
                // Example "/var/www/instance-name/htdocs/"
-               define('PATH_site', self::getPathSite($relativePathPart));
+               if (!defined('PATH_site')) {
+                       define('PATH_site', self::getPathSite($relativePathPart));
+               }
                // Absolute path of the typo3 directory of the instance with trailing slash
                // Example "/var/www/instance-name/htdocs/typo3/"
                define('PATH_typo3', PATH_site . TYPO3_mainDir);