Skip to content

Commit

Permalink
[TASK] Avoid vfsStream in functional tests
Browse files Browse the repository at this point in the history
There is little reason to fake filesystems
details within functional tests that use an
isolated instance.

Resolves: #98923
Related: #97762
Releases: main
Change-Id: I83579eab82655c4cd464050e4fbcd7608ba0062e
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/76268
Tested-by: core-ci <typo3@b13.com>
Tested-by: Oliver Klee <typo3-coding@oliverklee.de>
Tested-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Andreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
  • Loading branch information
lolli42 committed Oct 26, 2022
1 parent b8d63f4 commit 7975ac2
Show file tree
Hide file tree
Showing 16 changed files with 16,792 additions and 199 deletions.
1,018 changes: 1,018 additions & 0 deletions typo3/sysext/core/Tests/Functional/Imaging/Fixtures/file.ai.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7,842 changes: 7,842 additions & 0 deletions typo3/sysext/core/Tests/Functional/Imaging/Fixtures/file.eps.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7,843 changes: 7,843 additions & 0 deletions typo3/sysext/core/Tests/Functional/Imaging/Fixtures/file.ps.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions typo3/sysext/core/Tests/Functional/Imaging/Fixtures/file.svg.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
127 changes: 13 additions & 114 deletions typo3/sysext/core/Tests/Functional/Imaging/ImageMagickFileTest.php
Expand Up @@ -17,8 +17,6 @@

namespace TYPO3\CMS\Core\Tests\Functional\Imaging;

use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use TYPO3\CMS\Core\Exception;
use TYPO3\CMS\Core\Imaging\ImageMagickFile;
use TYPO3\CMS\Core\Type\File\FileInfo;
Expand All @@ -29,41 +27,6 @@ class ImageMagickFileTest extends FunctionalTestCase
{
protected bool $initializeDatabase = false;

/**
* @var vfsStreamDirectory
*/
private $directory;

protected function setUp(): void
{
parent::setUp();

$fixturePath = __DIR__ . '/Fixtures';
$structure = [];
$this->addFiles($structure, ['file.ai', 'file.ai.jpg'], $fixturePath . '/file.ai');
$this->addFiles($structure, ['file.bmp', 'file.bmp.jpg'], $fixturePath . '/file.bmp');
$this->addFiles($structure, ['file.gif', 'file.gif.jpg'], $fixturePath . '/file.gif');
$this->addFiles($structure, ['file.fax', 'file.fax.jpg'], $fixturePath . '/file.fax');
$this->addFiles($structure, ['file.jpg', 'file.jpg.png'], $fixturePath . '/file.jpg');
$this->addFiles($structure, ['file.png', 'file.png.jpg'], $fixturePath . '/file.png');
$this->addFiles($structure, ['file.svg', 'file.svg.jpg'], $fixturePath . '/file.svg');
$this->addFiles($structure, ['file.tif', 'file.tif.jpg'], $fixturePath . '/file.tif');
$this->addFiles($structure, ['file.webp', 'file.webp.jpg'], $fixturePath . '/file.webp');
$this->addFiles($structure, ['file.pdf', 'file.pdf.jpg'], $fixturePath . '/file.pdf');
$this->addFiles($structure, ['file.ps', 'file.ps.jpg'], $fixturePath . '/file.ps');
$this->addFiles($structure, ['file.eps', 'file.eps.jpg'], $fixturePath . '/file.eps');
$this->directory = vfsStream::setup('root', null, $structure);
}

protected function tearDown(): void
{
unset($this->directory);
parent::tearDown();
}

/**
* @return array
*/
public function framesAreConsideredDataProvider(): array
{
return [
Expand All @@ -73,24 +36,17 @@ public function framesAreConsideredDataProvider(): array
}

/**
* @param string $fileName
* @param int|null $frame
* @param string $expectation
*
* @test
* @dataProvider framesAreConsideredDataProvider
*/
public function framesAreConsidered(string $fileName, ?int $frame, string $expectation): void
{
$expectation = $this->substituteVariables($expectation);
$filePath = sprintf('%s/%s', $this->directory->url(), $fileName);
$filePath = sprintf('%s/%s', __DIR__ . '/Fixtures', $fileName);
$file = ImageMagickFile::fromFilePath($filePath, $frame);
self::assertSame($expectation, (string)$file);
}

/**
* @return array
*/
public function resultIsEscapedDataProvider(): array
{
// probably Windows system
Expand All @@ -110,24 +66,17 @@ public function resultIsEscapedDataProvider(): array
}

/**
* @param string $fileName
* @param int|null $frame
* @param string $expectation
*
* @test
* @dataProvider resultIsEscapedDataProvider
*/
public function resultIsEscaped(string $fileName, ?int $frame, string $expectation): void
{
$expectation = $this->substituteVariables($expectation);
$filePath = sprintf('%s/%s', $this->directory->url(), $fileName);
$filePath = sprintf('%s/%s', __DIR__ . '/Fixtures', $fileName);
$file = ImageMagickFile::fromFilePath($filePath, $frame);
self::assertSame($expectation, (string)$file);
}

/**
* @return array
*/
public function fileStatementIsResolvedDataProvider(): array
{
return [
Expand All @@ -154,25 +103,20 @@ public function fileStatementIsResolvedDataProvider(): array
}

/**
* @param string $fileName
* @param string $expectation
*
* @test
* @dataProvider fileStatementIsResolvedDataProvider
*/
public function fileStatementIsResolved(string $fileName, string $expectation): void
{
$expectation = $this->substituteVariables($expectation);
$filePath = sprintf('%s/%s', $this->directory->url(), $fileName);
$file = ImageMagickFile::fromFilePath($filePath, null);
$filePath = sprintf('%s/%s', __DIR__ . '/Fixtures', $fileName);
$file = ImageMagickFile::fromFilePath($filePath);
self::assertSame($expectation, (string)$file);
}

/**
* In case mime-types cannot be resolved (or cannot be verified), allowed extensions
* are used as conversion format (e.g. 'file.ai.jpg' -> 'jpg:...').
*
* @return array
*/
public function fileStatementIsResolvedForEnforcedMimeTypeDataProvider(): array
{
Expand All @@ -195,25 +139,18 @@ public function fileStatementIsResolvedForEnforcedMimeTypeDataProvider(): array
}

/**
* @param string $fileName
* @param string $expectation
* @param string $mimeType
*
* @test
* @dataProvider fileStatementIsResolvedForEnforcedMimeTypeDataProvider
*/
public function fileStatementIsResolvedForEnforcedMimeType(string $fileName, string $expectation, string $mimeType): void
{
$this->simulateNextFileInfoInvocation($mimeType);
$expectation = $this->substituteVariables($expectation);
$filePath = sprintf('%s/%s', $this->directory->url(), $fileName);
$filePath = sprintf('%s/%s', __DIR__ . '/Fixtures', $fileName);
$file = ImageMagickFile::fromFilePath($filePath, null);
self::assertSame($expectation, (string)$file);
}

/**
* @return array
*/
public function fileStatementIsResolvedForConfiguredMimeTypeDataProvider(): array
{
return [
Expand All @@ -223,9 +160,6 @@ public function fileStatementIsResolvedForConfiguredMimeTypeDataProvider(): arra
}

/**
* @param string $fileName
* @param string $expectation
*
* @test
* @dataProvider fileStatementIsResolvedForConfiguredMimeTypeDataProvider
*/
Expand All @@ -237,14 +171,11 @@ public function fileStatementIsResolvedForConfiguredMimeType(string $fileName, s
$GLOBALS['TYPO3_CONF_VARS']['SYS']['FileInfo']['fileExtensionToMimeType']['bmp'] = 'image/x-ms-bmp';

$expectation = $this->substituteVariables($expectation);
$filePath = sprintf('%s/%s', $this->directory->url(), $fileName);
$filePath = sprintf('%s/%s', __DIR__ . '/Fixtures', $fileName);
$file = ImageMagickFile::fromFilePath($filePath, null);
self::assertSame($expectation, (string)$file);
}

/**
* @return array
*/
public function fileStatementIsDeniedDataProvider(): array
{
return [
Expand All @@ -258,13 +189,10 @@ public function fileStatementIsDeniedDataProvider(): array
}

/**
* @param string $fileName
* @param string|null $mimeType
*
* @test
* @dataProvider fileStatementIsDeniedDataProvider
*/
public function fileStatementIsDenied(string $fileName, string $mimeType = null): void
public function fileStatementIsDenied(string $fileName, ?string $mimeType = null): void
{
$this->expectException(Exception::class);
$this->expectExceptionCode(1550060977);
Expand All @@ -273,13 +201,10 @@ public function fileStatementIsDenied(string $fileName, string $mimeType = null)
$this->simulateNextFileInfoInvocation($mimeType);
}

$filePath = sprintf('%s/%s', $this->directory->url(), $fileName);
ImageMagickFile::fromFilePath($filePath, null);
$filePath = sprintf('%s/%s', __DIR__ . '/Fixtures', $fileName);
ImageMagickFile::fromFilePath($filePath);
}

/**
* @return array
*/
public function fileStatementIsDeniedForConfiguredMimeTypeDataProvider(): array
{
return [
Expand All @@ -289,8 +214,6 @@ public function fileStatementIsDeniedForConfiguredMimeTypeDataProvider(): array
}

/**
* @param string $fileName
*
* @test
* @dataProvider fileStatementIsDeniedForConfiguredMimeTypeDataProvider
*/
Expand All @@ -302,44 +225,20 @@ public function fileStatementIsDeniedForConfiguredMimeType(string $fileName): vo
$this->expectException(Exception::class);
$this->expectExceptionCode(1550060977);

$filePath = sprintf('%s/%s', $this->directory->url(), $fileName);
$filePath = sprintf('%s/%s', __DIR__ . '/Fixtures', $fileName);
ImageMagickFile::fromFilePath($filePath, null);
}

/**
* @param array $structure
* @param array $fileNames
* @param string $sourcePath
*/
private function addFiles(array &$structure, array $fileNames, string $sourcePath): void
{
$structure = array_merge(
$structure,
array_fill_keys(
$fileNames,
file_get_contents($sourcePath)
)
);
}

/**
* @param string $value
* @return string
*/
private function substituteVariables(string $value): string
{
return str_replace(
['{directory}'],
[$this->directory->url()],
[__DIR__ . '/Fixtures'],
$value
);
}

/**
* @param string $mimeType
* @param string[] $mimeExtensions
*/
private function simulateNextFileInfoInvocation(string $mimeType, array $mimeExtensions = []): void
private function simulateNextFileInfoInvocation(string $mimeType): void
{
$fileInfo = $this->getAccessibleMock(
FileInfo::class,
Expand All @@ -349,7 +248,7 @@ private function simulateNextFileInfoInvocation(string $mimeType, array $mimeExt
false
);
$fileInfo->expects(self::atLeastOnce())->method('getMimeType')->willReturn($mimeType);
$fileInfo->expects(self::atLeastOnce())->method('getMimeExtensions')->willReturn($mimeExtensions);
$fileInfo->expects(self::atLeastOnce())->method('getMimeExtensions')->willReturn([]);
GeneralUtility::addInstance(FileInfo::class, $fileInfo);
}
}

0 comments on commit 7975ac2

Please sign in to comment.