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