Skip to content

Commit

Permalink
[TASK] Clean up and cover two base VH classes
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessCoder committed Jan 30, 2023
1 parent 547028a commit 7d14504
Show file tree
Hide file tree
Showing 4 changed files with 273 additions and 63 deletions.
63 changes: 6 additions & 57 deletions Classes/ViewHelpers/Resource/AbstractImageViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
*/

use FluidTYPO3\Vhs\Utility\ContextUtility;
use FluidTYPO3\Vhs\Utility\CoreUtility;
use FluidTYPO3\Vhs\Utility\FrontendSimulationUtility;
use FluidTYPO3\Vhs\Utility\ResourceUtility;
use TYPO3\CMS\Core\TypoScript\TemplateService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
Expand All @@ -24,17 +22,6 @@
*/
abstract class AbstractImageViewHelper extends AbstractResourceViewHelper
{
/**
* @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController contains a backup of
* the current $GLOBALS['TSFE'] if used in BE mode
*/
protected $tsfeBackup;

/**
* @var string|false
*/
protected $workingDirectoryBackup;

/**
* @var ConfigurationManagerInterface
*/
Expand Down Expand Up @@ -110,12 +97,12 @@ public function preprocessImages(array $files, bool $onlyProperties = false): ?a
$tsfeBackup = FrontendSimulationUtility::simulateFrontendEnvironment();

$setup = [
'width' => $this->arguments['width'],
'height' => $this->arguments['height'],
'minW' => $this->arguments['minWidth'],
'minH' => $this->arguments['minHeight'],
'maxW' => $this->arguments['maxWidth'],
'maxH' => $this->arguments['maxHeight'],
'width' => $this->arguments['width'] ?? null,
'height' => $this->arguments['height'] ?? null,
'minW' => $this->arguments['minWidth'] ?? null,
'minH' => $this->arguments['minHeight'] ?? null,
'maxW' => $this->arguments['maxWidth'] ?? null,
'maxH' => $this->arguments['maxHeight'] ?? null,
'treatIdAsReference' => false
];

Expand Down Expand Up @@ -156,44 +143,6 @@ public function preprocessImages(array $files, bool $onlyProperties = false): ?a
return $images;
}

/**
* Prepares $GLOBALS['TSFE'] for Backend mode
* This somewhat hacky work around is currently needed because the getImgResource() function of
* \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer relies on those variables to be set.
*/
protected function simulateFrontendEnvironment(): void
{
$this->tsfeBackup = $GLOBALS['TSFE'] ?? null;
$this->workingDirectoryBackup = getcwd();
chdir(CoreUtility::getSitePath());
$typoScriptSetup = $this->configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT
);
$GLOBALS['TSFE'] = new \stdClass();
/** @var TemplateService $template */
$template = GeneralUtility::makeInstance(TemplateService::class);
$template->tt_track = false;
if (property_exists($template, 'getFileName_backPath')) {
$template->getFileName_backPath = CoreUtility::getSitePath();
}
$GLOBALS['TSFE']->tmpl = $template;
$GLOBALS['TSFE']->tmpl->setup = $typoScriptSetup;
$GLOBALS['TSFE']->config = $typoScriptSetup;
}

/**
* Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
*
* @see simulateFrontendEnvironment()
*/
protected function resetFrontendEnvironment(): void
{
$GLOBALS['TSFE'] = $this->tsfeBackup;
if ($this->workingDirectoryBackup !== false) {
chdir($this->workingDirectoryBackup);
}
}

/**
* Turns a relative source URI into an absolute URL
* if required.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function getResources(array $record): array

public function getTable(): string
{
$table = $this->arguments['table'];
$table = $this->arguments['table'] ?? null;
if (null === $table) {
$table = $this->table;
}
Expand All @@ -114,7 +114,7 @@ public function getTable(): string

public function getField(): string
{
$field = $this->arguments['field'];
$field = $this->arguments['field'] ?? null;
if (null === $field) {
$field = $this->field;
}
Expand Down Expand Up @@ -175,8 +175,8 @@ public function getActiveRecord(): array
*/
public function render()
{
$record = $this->arguments['record'];
$uid = $this->arguments['uid'];
$record = $this->arguments['record'] ?? null;
$uid = $this->arguments['uid'] ?? null;

if (null === $record) {
if (null === $uid) {
Expand All @@ -195,9 +195,10 @@ public function render()

// attempt to load resources. If any Exceptions happen, transform them to
// ViewHelperExceptions which render as an inline text error message.
$content = null;
try {
$resources = $this->getResources($record);
return $this->renderChildrenWithVariableOrReturnInput($resources);
$content = $this->renderChildrenWithVariableOrReturnInput($resources);
} catch (\Exception $error) {
// we are doing the pokemon-thing and catching the very top level
// of Exception because the range of Exceptions that are possibly
Expand All @@ -206,6 +207,6 @@ public function render()
// we are forced to "catch them all" - but we also output them.
ErrorUtility::throwViewHelperException($error->getMessage(), $error->getCode());
}
return null;
return $content;
}
}
155 changes: 155 additions & 0 deletions Tests/Unit/ViewHelpers/Resource/AbstractImageViewHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace FluidTYPO3\Vhs\Tests\Unit\ViewHelpers\Resource;

/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

use FluidTYPO3\Vhs\Tests\Unit\AbstractTestCase;
use FluidTYPO3\Vhs\ViewHelpers\Resource\AbstractImageViewHelper;
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\ResourceStorage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

class AbstractImageViewHelperTest extends AbstractTestCase
{
private ?AbstractImageViewHelper $subject = null;
private ?ContentObjectRenderer $contentObjectRenderer = null;

protected function setUp(): void
{
$GLOBALS['TYPO3_REQUEST'] = $this->getMockBuilder(ServerRequest::class)
->setMethods(['getAttribute'])
->disableOriginalConstructor()
->getMock();
$GLOBALS['TYPO3_REQUEST']->method('getAttribute')->willReturn(SystemEnvironmentBuilder::REQUESTTYPE_FE);

$this->subject = $this->getMockBuilder(AbstractImageViewHelper::class)
->disableOriginalConstructor()
->getMockForAbstractClass();
$this->contentObjectRenderer = $this->getMockBuilder(ContentObjectRenderer::class)
->setMethods(['getImgResource'])
->disableOriginalConstructor()
->getMock();

$configurationManager = $this->getMockBuilder(ConfigurationManagerInterface::class)
->getMockForAbstractClass();
$configurationManager->method('getContentObject')->willReturn($this->contentObjectRenderer);

$this->subject->injectConfigurationManager($configurationManager);

parent::setUp();
}

public function testProcessImage(): void
{
$path = 'https://foo.bar/path/to/file';
$file = $this->getMockBuilder(File::class)->disableOriginalConstructor()->getMock();
self::assertSame(
[
[
'info' => [
123,
456,
789,
$path,
],
'source' => $path,
'file' => $file,
],
],
$this->runTestWithImage($file, $path, false),
);
}

public function testProcessImageWithOnlyProperties(): void
{
$path = '/path/to/file';

$storage = $this->getMockBuilder(ResourceStorage::class)
->setMethods(['getFileInfo'])
->disableOriginalConstructor()
->getMock();
$storage->method('getFileInfo')->willReturn(['foo' => 'bar']);

$file = $this->getMockBuilder(File::class)
->setMethods(['getStorage', 'hasProperty', 'getProperty', 'getProperties', 'toArray'])
->disableOriginalConstructor()
->getMock();
$file->method('getStorage')->willReturn($storage);
$file->method('hasProperty')->willReturn(true);
$file->method('getProperty')->willReturn('prop');
$file->method('getProperties')->willReturn(['foo' => 'bar']);
$file->method('toArray')->willReturn(['foo' => 'bar']);

self::assertSame(
[
[
'info' => [
123,
456,
789,
'/path/to/file',
],
'source' => $path,
'file' => ['foo' => 'bar'],
],
],
$this->runTestWithImage($file, $path, true),
);
}

public function testProcessImageThrowsExceptionOnInvalidImage(): void
{
$files = [
$this->getMockBuilder(File::class)->disableOriginalConstructor()->getMock(),
];
$this->contentObjectRenderer->method('getImgResource')->willReturn(null);
self::expectExceptionCode(1253191060);
$this->subject->preprocessImages($files);
}

private function runTestWithImage(File $file, string $path, bool $onlyProperties): array
{
$GLOBALS['TSFE'] = (object) ['lastImageInfo' => null, 'imagesOnPage' => [], 'absRefPrefix' => ''];
$this->contentObjectRenderer->method('getImgResource')->willReturn(
[
123,
456,
789,
$path
]
);
return $this->subject->preprocessImages([$file], $onlyProperties);
}

public function testPreProcessSourceUriWithPrependPath(): void
{
$GLOBALS['TSFE'] = (object) [
'tmpl' => (object) ['setup' => ['plugin.' => ['tx_vhs.' => ['settings.' => ['prependPath' => 'prepend']]]]],
];

$output = $this->subject->preprocessSourceUri('source');
self::assertSame('prependsource', $output);
}

public function testPreProcessSourceUriInBackendContext(): void
{
$GLOBALS['TYPO3_REQUEST'] = $this->getMockBuilder(ServerRequest::class)
->setMethods(['getAttribute'])
->disableOriginalConstructor()
->getMock();
$GLOBALS['TYPO3_REQUEST']->method('getAttribute')->willReturn(SystemEnvironmentBuilder::REQUESTTYPE_BE);

$output = $this->subject->preprocessSourceUri('source');
self::assertSame(GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . 'source', $output);
}
}
Loading

0 comments on commit 7d14504

Please sign in to comment.