Commit 61481707 authored by Christian Kuhn's avatar Christian Kuhn Committed by Oliver Hader
Browse files

[TASK] Initialize & cleanup environment in test cases

Correctly initialize environment (host, script-name, ...) in test cases.
Adjustments to test cases of #87391 have been back-ported to TYPO3 v9.

Resolves: #93707
Releases: 9.5
Change-Id: I39083726fe77b13f715f03d4eb712e14a18be0c3
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/68344

Tested-by: default avatarTYPO3com <noreply@typo3.com>
Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Tested-by: Oliver Hader's avatarOliver Hader <oliver.hader@typo3.org>
Reviewed-by: Oliver Hader's avatarOliver Hader <oliver.hader@typo3.org>
parent 2d416691
...@@ -25,6 +25,11 @@ use TYPO3\CMS\Core\Utility\GeneralUtility; ...@@ -25,6 +25,11 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
*/ */
class ResourceCompressorIntegrationTest extends BaseTestCase class ResourceCompressorIntegrationTest extends BaseTestCase
{ {
/**
* @var bool Restore Environment after tests
*/
protected $backupEnvironment = true;
/** /**
* @var TestableResourceCompressor * @var TestableResourceCompressor
*/ */
...@@ -42,6 +47,7 @@ class ResourceCompressorIntegrationTest extends BaseTestCase ...@@ -42,6 +47,7 @@ class ResourceCompressorIntegrationTest extends BaseTestCase
public function setUp(): void public function setUp(): void
{ {
parent::setUp();
$this->fixtureDir = 'sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/'; $this->fixtureDir = 'sysext/core/Tests/Unit/Resource/ResourceCompressorTest/Fixtures/';
$this->fixtureDirFromTest = GeneralUtility::fixWindowsFilePath(__DIR__ . '/ResourceCompressorTest/Fixtures/'); $this->fixtureDirFromTest = GeneralUtility::fixWindowsFilePath(__DIR__ . '/ResourceCompressorTest/Fixtures/');
} }
...@@ -105,6 +111,17 @@ class ResourceCompressorIntegrationTest extends BaseTestCase ...@@ -105,6 +111,17 @@ class ResourceCompressorIntegrationTest extends BaseTestCase
*/ */
public function concatenateCssFilesWorksWithFileFromNonRootPath(): void public function concatenateCssFilesWorksWithFileFromNonRootPath(): void
{ {
Environment::initialize(
Environment::getContext(),
true,
false,
Environment::getProjectPath(),
Environment::getPublicPath(),
Environment::getVarPath(),
Environment::getConfigPath(),
Environment::getBackendPath() . '/index.php',
Environment::isWindows() ? 'WINDOWS' : 'UNIX'
);
$testFile = Environment::getPublicPath() . '/typo3temp/var/transient/css_input_with_import.css'; $testFile = Environment::getPublicPath() . '/typo3temp/var/transient/css_input_with_import.css';
$this->testFilesToDelete[] = $testFile; $this->testFilesToDelete[] = $testFile;
copy(Environment::getBackendPath() . '/' . $this->fixtureDir . 'css_input_with_import.css', $testFile); copy(Environment::getBackendPath() . '/' . $this->fixtureDir . 'css_input_with_import.css', $testFile);
......
...@@ -25,8 +25,7 @@ use TYPO3\CMS\Core\Utility\PathUtility; ...@@ -25,8 +25,7 @@ use TYPO3\CMS\Core\Utility\PathUtility;
class ResourceCompressorTest extends BaseTestCase class ResourceCompressorTest extends BaseTestCase
{ {
/** /**
* Restore Environment after the test * @var bool Restore Environment after tests
* @var bool
*/ */
protected $backupEnvironment = true; protected $backupEnvironment = true;
...@@ -538,7 +537,7 @@ class ResourceCompressorTest extends BaseTestCase ...@@ -538,7 +537,7 @@ class ResourceCompressorTest extends BaseTestCase
/** /**
* @return array * @return array
*/ */
public function getVariousFilenamesFromMainDirInBackendContextDataProvider(): array public function getFilenamesFromMainDirInBackendContextDataProvider(): array
{ {
return [ return [
// Get filename using EXT: // Get filename using EXT:
...@@ -574,24 +573,37 @@ class ResourceCompressorTest extends BaseTestCase ...@@ -574,24 +573,37 @@ class ResourceCompressorTest extends BaseTestCase
/** /**
* @test * @test
* @dataProvider getVariousFilenamesFromMainDirInBackendContextDataProvider * @dataProvider getFilenamesFromMainDirInBackendContextDataProvider
* @param string $filename input that will be fired on the extension * @param string $filename input that will be fired on the extension
* @param string $expected * @param string $expected
*/ */
public function getVariousFilenamesFromMainDirInBackendContext(string $filename, string $expected) public function getFilenamesFromMainDirInBackendContext(string $filename, string $expected)
{ {
$rootPath = \dirname($_SERVER['SCRIPT_NAME']); // getCurrentScript() called by PathUtility::getRelativePathTo() is usually something
$this->subject = $this->getAccessibleMock(ResourceCompressor::class, ['dummy']); // like '.../bin/phpunit' in testing context, but we want .../typo3/index.php as entry
$this->subject->setRootPath($rootPath . '/'); // script point here to fake the backend call.
$bePath = Environment::getBackendPath();
$relativeToRootPath = $this->subject->_call('getFilenameFromMainDir', $filename); Environment::initialize(
$this->assertSame($expected, $relativeToRootPath, 'Path to the file relative to the path converted correctly.'); Environment::getContext(),
true,
false,
Environment::getProjectPath(),
Environment::getPublicPath(),
Environment::getVarPath(),
Environment::getConfigPath(),
$bePath . '/index.php',
Environment::isWindows() ? 'WINDOWS' : 'UNIX'
);
$subject = $this->getAccessibleMock(ResourceCompressor::class, ['dummy']);
$subject->setRootPath($bePath . '/');
$relativeToRootPath = $subject->_call('getFilenameFromMainDir', $filename);
$this->assertSame($expected, $relativeToRootPath);
} }
/** /**
* @return array * @return array
*/ */
public function getVariousFilenamesFromMainDirInFrontendContextDataProvider(): array public function getFilenamesFromMainDirInFrontendContextDataProvider(): array
{ {
return [ return [
// Get filename using EXT: // Get filename using EXT:
...@@ -613,12 +625,16 @@ class ResourceCompressorTest extends BaseTestCase ...@@ -613,12 +625,16 @@ class ResourceCompressorTest extends BaseTestCase
/** /**
* @test * @test
* @dataProvider getVariousFilenamesFromMainDirInFrontendContextDataProvider * @dataProvider getFilenamesFromMainDirInFrontendContextDataProvider
* @param string $filename input that will be fired on the extension * @param string $filename input that will be fired on the extension
* @param string $expected * @param string $expected
*/ */
public function getVariousFilenamesFromMainDirInFrontendContext(string $filename, string $expected) public function getFilenamesFromMainDirInFrontendContext(string $filename, string $expected)
{ {
// getCurrentScript() called by PathUtility::getRelativePathTo() is usually something
// like '.../bin/phpunit' in testing context, but we want .../index.php as entry
// script point here to fake the frontend call.
$fePath = Environment::getPublicPath();
Environment::initialize( Environment::initialize(
Environment::getContext(), Environment::getContext(),
true, true,
...@@ -627,15 +643,13 @@ class ResourceCompressorTest extends BaseTestCase ...@@ -627,15 +643,13 @@ class ResourceCompressorTest extends BaseTestCase
Environment::getPublicPath(), Environment::getPublicPath(),
Environment::getVarPath(), Environment::getVarPath(),
Environment::getConfigPath(), Environment::getConfigPath(),
PATH_site . 'index.php', $fePath . '/index.php',
Environment::isWindows() ? 'WINDOWS' : 'UNIX' Environment::isWindows() ? 'WINDOWS' : 'UNIX'
); );
$_SERVER['ORIG_SCRIPT_NAME'] = '/index.php'; $subject = $this->getAccessibleMock(ResourceCompressor::class, ['dummy']);
$this->subject = $this->getAccessibleMock(ResourceCompressor::class, ['dummy']); $subject->setRootPath($fePath . '/');
$this->subject->setRootPath(PATH_site); $relativeToRootPath = $subject->_call('getFilenameFromMainDir', $filename);
$this->assertSame($expected, $relativeToRootPath);
$relativeToRootPath = $this->subject->_call('getFilenameFromMainDir', $filename);
$this->assertSame($expected, $relativeToRootPath, 'Path to the file relative to the path converted correctly.');
} }
public function getFilenamesFromMainDirInBackendContextInSubfolderDataProvider(): array public function getFilenamesFromMainDirInBackendContextInSubfolderDataProvider(): array
......
...@@ -54,6 +54,11 @@ class GeneralUtilityTest extends UnitTestCase ...@@ -54,6 +54,11 @@ class GeneralUtilityTest extends UnitTestCase
*/ */
protected $resetSingletonInstances = true; protected $resetSingletonInstances = true;
/**
* @var bool Restore Environment after tests
*/
protected $backupEnvironment = true;
/** /**
* @var \TYPO3\CMS\Core\Package\PackageManager * @var \TYPO3\CMS\Core\Package\PackageManager
*/ */
...@@ -64,6 +69,7 @@ class GeneralUtilityTest extends UnitTestCase ...@@ -64,6 +69,7 @@ class GeneralUtilityTest extends UnitTestCase
*/ */
protected function setUp() protected function setUp()
{ {
parent::setUp();
GeneralUtilityFixture::$isAllowedHostHeaderValueCallCount = 0; GeneralUtilityFixture::$isAllowedHostHeaderValueCallCount = 0;
GeneralUtilityFixture::setAllowHostHeaderValue(false); GeneralUtilityFixture::setAllowHostHeaderValue(false);
$GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] = GeneralUtility::ENV_TRUSTED_HOSTS_PATTERN_ALLOW_ALL; $GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] = GeneralUtility::ENV_TRUSTED_HOSTS_PATTERN_ALLOW_ALL;
...@@ -1461,7 +1467,17 @@ class GeneralUtilityTest extends UnitTestCase ...@@ -1461,7 +1467,17 @@ class GeneralUtilityTest extends UnitTestCase
*/ */
public function getIndpEnvTypo3SitePathReturnsStringStartingWithSlash() public function getIndpEnvTypo3SitePathReturnsStringStartingWithSlash()
{ {
$_SERVER['SCRIPT_NAME'] = '/typo3/'; Environment::initialize(
Environment::getContext(),
true,
false,
Environment::getProjectPath(),
Environment::getPublicPath(),
Environment::getVarPath(),
Environment::getConfigPath(),
Environment::getBackendPath() . '/index.php',
Environment::isWindows() ? 'WINDOWS' : 'UNIX'
);
$result = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH'); $result = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
$this->assertEquals('/', $result[0]); $this->assertEquals('/', $result[0]);
} }
...@@ -1938,6 +1954,17 @@ class GeneralUtilityTest extends UnitTestCase ...@@ -1938,6 +1954,17 @@ class GeneralUtilityTest extends UnitTestCase
*/ */
public function sanitizeLocalUrlAcceptsNotEncodedValidPaths($path) public function sanitizeLocalUrlAcceptsNotEncodedValidPaths($path)
{ {
Environment::initialize(
Environment::getContext(),
true,
false,
Environment::getProjectPath(),
Environment::getPublicPath(),
Environment::getVarPath(),
Environment::getConfigPath(),
Environment::getBackendPath() . '/index.php',
Environment::isWindows() ? 'WINDOWS' : 'UNIX'
);
$this->assertEquals($path, GeneralUtility::sanitizeLocalUrl($path)); $this->assertEquals($path, GeneralUtility::sanitizeLocalUrl($path));
} }
...@@ -1948,6 +1975,17 @@ class GeneralUtilityTest extends UnitTestCase ...@@ -1948,6 +1975,17 @@ class GeneralUtilityTest extends UnitTestCase
*/ */
public function sanitizeLocalUrlAcceptsEncodedValidPaths($path) public function sanitizeLocalUrlAcceptsEncodedValidPaths($path)
{ {
Environment::initialize(
Environment::getContext(),
true,
false,
Environment::getProjectPath(),
Environment::getPublicPath(),
Environment::getVarPath(),
Environment::getConfigPath(),
Environment::getBackendPath() . '/index.php',
Environment::isWindows() ? 'WINDOWS' : 'UNIX'
);
$this->assertEquals(rawurlencode($path), GeneralUtility::sanitizeLocalUrl(rawurlencode($path))); $this->assertEquals(rawurlencode($path), GeneralUtility::sanitizeLocalUrl(rawurlencode($path)));
} }
...@@ -1958,29 +1996,26 @@ class GeneralUtilityTest extends UnitTestCase ...@@ -1958,29 +1996,26 @@ class GeneralUtilityTest extends UnitTestCase
*/ */
public function sanitizeLocalUrlValidUrlsDataProvider() public function sanitizeLocalUrlValidUrlsDataProvider()
{ {
$host = 'localhost';
$subDirectory = '/cms/';
return [ return [
$subDirectory . 'typo3/alt_intro.php' => [ '/cms/typo3/alt_intro.php' => [
$subDirectory . 'typo3/alt_intro.php', '/cms/typo3/alt_intro.php',
$host, 'localhost',
$subDirectory, '/cms/',
], ],
$subDirectory . 'index.php' => [ '/cms/index.php' => [
$subDirectory . 'index.php', '/cms/index.php',
$host, 'localhost',
$subDirectory, '/cms/',
], ],
'http://' . $host . '/typo3/alt_intro.php' => [ 'http://localhost/typo3/alt_intro.php' => [
'http://' . $host . '/typo3/alt_intro.php', 'http://localhost/typo3/alt_intro.php',
$host, 'localhost',
'', '',
], ],
'http://' . $host . $subDirectory . 'typo3/alt_intro.php' => [ 'http://localhost/cms/typo3/alt_intro.php' => [
'http://' . $host . $subDirectory . 'typo3/alt_intro.php', 'http://localhost/cms/typo3/alt_intro.php',
$host, 'localhost',
$subDirectory, '/cms/',
], ],
]; ];
} }
...@@ -1994,6 +2029,17 @@ class GeneralUtilityTest extends UnitTestCase ...@@ -1994,6 +2029,17 @@ class GeneralUtilityTest extends UnitTestCase
*/ */
public function sanitizeLocalUrlAcceptsNotEncodedValidUrls($url, $host, $subDirectory) public function sanitizeLocalUrlAcceptsNotEncodedValidUrls($url, $host, $subDirectory)
{ {
Environment::initialize(
Environment::getContext(),
true,
false,
Environment::getProjectPath(),
Environment::getPublicPath(),
Environment::getVarPath(),
Environment::getConfigPath(),
Environment::getBackendPath() . '/index.php',
Environment::isWindows() ? 'WINDOWS' : 'UNIX'
);
$_SERVER['HTTP_HOST'] = $host; $_SERVER['HTTP_HOST'] = $host;
$_SERVER['SCRIPT_NAME'] = $subDirectory . 'typo3/index.php'; $_SERVER['SCRIPT_NAME'] = $subDirectory . 'typo3/index.php';
$this->assertEquals($url, GeneralUtility::sanitizeLocalUrl($url)); $this->assertEquals($url, GeneralUtility::sanitizeLocalUrl($url));
...@@ -2008,6 +2054,17 @@ class GeneralUtilityTest extends UnitTestCase ...@@ -2008,6 +2054,17 @@ class GeneralUtilityTest extends UnitTestCase
*/ */
public function sanitizeLocalUrlAcceptsEncodedValidUrls($url, $host, $subDirectory) public function sanitizeLocalUrlAcceptsEncodedValidUrls($url, $host, $subDirectory)
{ {
Environment::initialize(
Environment::getContext(),
true,
false,
Environment::getProjectPath(),
Environment::getPublicPath(),
Environment::getVarPath(),
Environment::getConfigPath(),
Environment::getBackendPath() . '/index.php',
Environment::isWindows() ? 'WINDOWS' : 'UNIX'
);
$_SERVER['HTTP_HOST'] = $host; $_SERVER['HTTP_HOST'] = $host;
$_SERVER['SCRIPT_NAME'] = $subDirectory . 'typo3/index.php'; $_SERVER['SCRIPT_NAME'] = $subDirectory . 'typo3/index.php';
$this->assertEquals(rawurlencode($url), GeneralUtility::sanitizeLocalUrl(rawurlencode($url))); $this->assertEquals(rawurlencode($url), GeneralUtility::sanitizeLocalUrl(rawurlencode($url)));
......
...@@ -57,11 +57,17 @@ class FrontendLoginControllerTest extends UnitTestCase ...@@ -57,11 +57,17 @@ class FrontendLoginControllerTest extends UnitTestCase
*/ */
protected $testTableName; protected $testTableName;
/**
* @var bool Restore Environment after tests
*/
protected $backupEnvironment = true;
/** /**
* Set up * Set up
*/ */
protected function setUp() protected function setUp()
{ {
parent::setUp();
$GLOBALS['TSFE'] = new \stdClass(); $GLOBALS['TSFE'] = new \stdClass();
$this->testTableName = 'sys_domain'; $this->testTableName = 'sys_domain';
$this->testHostName = 'hostname.tld'; $this->testHostName = 'hostname.tld';
......
...@@ -16,6 +16,7 @@ namespace TYPO3\CMS\Frontend\Tests\Unit\Typolink; ...@@ -16,6 +16,7 @@ namespace TYPO3\CMS\Frontend\Tests\Unit\Typolink;
*/ */
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Log\LogManager; use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
...@@ -33,6 +34,11 @@ class AbstractTypolinkBuilderTest extends UnitTestCase ...@@ -33,6 +34,11 @@ class AbstractTypolinkBuilderTest extends UnitTestCase
*/ */
protected $resetSingletonInstances = true; protected $resetSingletonInstances = true;
/**
* @var bool Restore Environment after tests
*/
protected $backupEnvironment = true;
/** /**
* @var \PHPUnit_Framework_MockObject_MockObject|TypoScriptFrontendController|\TYPO3\TestingFramework\Core\AccessibleObjectInterface * @var \PHPUnit_Framework_MockObject_MockObject|TypoScriptFrontendController|\TYPO3\TestingFramework\Core\AccessibleObjectInterface
*/ */
...@@ -43,8 +49,8 @@ class AbstractTypolinkBuilderTest extends UnitTestCase ...@@ -43,8 +49,8 @@ class AbstractTypolinkBuilderTest extends UnitTestCase
*/ */
protected function setUp() protected function setUp()
{ {
parent::setUp();
$this->createMockedLoggerAndLogManager(); $this->createMockedLoggerAndLogManager();
$this->frontendControllerMock = $this->getAccessibleMock( $this->frontendControllerMock = $this->getAccessibleMock(
TypoScriptFrontendController::class, TypoScriptFrontendController::class,
['dummy'], ['dummy'],
...@@ -166,6 +172,17 @@ class AbstractTypolinkBuilderTest extends UnitTestCase ...@@ -166,6 +172,17 @@ class AbstractTypolinkBuilderTest extends UnitTestCase
*/ */
public function forceAbsoluteUrlReturnsCorrectAbsoluteUrl($expected, $url, array $configuration) public function forceAbsoluteUrlReturnsCorrectAbsoluteUrl($expected, $url, array $configuration)
{ {
Environment::initialize(
Environment::getContext(),
true,
false,
Environment::getProjectPath(),
Environment::getPublicPath(),
Environment::getVarPath(),
Environment::getConfigPath(),
Environment::getBackendPath() . '/index.php',
Environment::isWindows() ? 'WINDOWS' : 'UNIX'
);
$this->frontendControllerMock->absRefPrefix = ''; $this->frontendControllerMock->absRefPrefix = '';
$contentObjectRendererProphecy = $this->prophesize(ContentObjectRenderer::class); $contentObjectRendererProphecy = $this->prophesize(ContentObjectRenderer::class);
$subject = $this->getAccessibleMock( $subject = $this->getAccessibleMock(
...@@ -184,6 +201,17 @@ class AbstractTypolinkBuilderTest extends UnitTestCase ...@@ -184,6 +201,17 @@ class AbstractTypolinkBuilderTest extends UnitTestCase
*/ */
public function forceAbsoluteUrlReturnsCorrectAbsoluteUrlWithSubfolder() public function forceAbsoluteUrlReturnsCorrectAbsoluteUrlWithSubfolder()
{ {
Environment::initialize(
Environment::getContext(),
true,
false,
Environment::getProjectPath(),
Environment::getPublicPath(),
Environment::getVarPath(),
Environment::getConfigPath(),
Environment::getBackendPath() . '/index.php',
Environment::isWindows() ? 'WINDOWS' : 'UNIX'
);
$contentObjectRendererProphecy = $this->prophesize(ContentObjectRenderer::class); $contentObjectRendererProphecy = $this->prophesize(ContentObjectRenderer::class);
$subject = $this->getAccessibleMock( $subject = $this->getAccessibleMock(
AbstractTypolinkBuilder::class, AbstractTypolinkBuilder::class,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment