From 44554ffe439547d72cc632dca42f5c6c1d1c99a4 Mon Sep 17 00:00:00 2001 From: Achim Fritz Date: Wed, 1 Feb 2023 12:41:21 +0100 Subject: [PATCH] [BUGFIX] cropVariants of sources Fixes: #37 --- .github/workflows/ci.yml | 3 + Classes/ViewHelpers/ImageViewHelper.php | 8 +- .../Unit/ViewHelpers/ImageViewHelperTest.php | 113 ++++++++++++++++++ 3 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 Tests/Unit/ViewHelpers/ImageViewHelperTest.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c1e5c19..ac4b1ff 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -43,5 +43,8 @@ jobs: - name: phpstan run: Build/Scripts/runTests.sh -p ${{ matrix.php }} -s phpstan + - name: Unit Tests + run: Build/Scripts/runTests.sh -p ${{ matrix.php }} -s unit + - name: Functional Tests with mariadb run: Build/Scripts/runTests.sh -p ${{ matrix.php }} -t ${{ matrix.TYPO3 }} -s functional diff --git a/Classes/ViewHelpers/ImageViewHelper.php b/Classes/ViewHelpers/ImageViewHelper.php index 54b6769..9132279 100644 --- a/Classes/ViewHelpers/ImageViewHelper.php +++ b/Classes/ViewHelpers/ImageViewHelper.php @@ -240,7 +240,7 @@ protected function buildSingleTag(string $tagName, array $configuration): TagBui if (!$tag->hasAttribute('data-focus-area')) { $cropVariantCollection = $this->getCropVariantCollection(); - $focusArea = $cropVariantCollection->getFocusArea($this->getCropVariant()); + $focusArea = $cropVariantCollection->getFocusArea($this->getImageCropVariant()); if (!$focusArea->isEmpty()) { $tag->addAttribute('data-focus-area', (string)$focusArea->makeAbsoluteBasedOnFile($this->image)); } @@ -405,7 +405,7 @@ protected function wrapWithPictureElement(array $output): array protected function getProcessingInstructions(array $configuration): array { $cropVariantCollection = $this->getCropVariantCollection(); - $cropVariant = $this->getCropVariant(); + $cropVariant = $configuration['cropVariant'] ?? $this->getImageCropVariant(); $cropArea = $cropVariantCollection->getCropArea($cropVariant); $processingInstructions = [ 'width' => $configuration['width'], @@ -422,9 +422,9 @@ protected function getProcessingInstructions(array $configuration): array return $processingInstructions; } - protected function getCropVariant(): string + protected function getImageCropVariant(): string { - return $this->arguments['cropVariant'] ?: 'default'; + return $this->arguments['cropVariant'] ?? 'default'; } protected function getCropVariantCollection(): CropVariantCollection diff --git a/Tests/Unit/ViewHelpers/ImageViewHelperTest.php b/Tests/Unit/ViewHelpers/ImageViewHelperTest.php new file mode 100644 index 0000000..66119cb --- /dev/null +++ b/Tests/Unit/ViewHelpers/ImageViewHelperTest.php @@ -0,0 +1,113 @@ +getAccessibleMock( + ImageViewHelper::class, + ['getCropVariantCollection', 'getImageCropVariant'], + [], + '', + false + ); + $image = $this->getMockBuilder(File::class)->disableOriginalConstructor(true)->getMock(); + $cropVariantCollection = $this->getMockBuilder(CropVariantCollection::class)->disableOriginalConstructor(true)->getMock(); + $cropVariantCollection->expects(self::once())->method('getCropArea')->with('foo'); + $imageViewHelper->expects(self::once())->method('getCropVariantCollection')->willReturn($cropVariantCollection); + $imageViewHelper->expects(self::never())->method('getImageCropVariant'); + $imageViewHelper->_set('image', $image); + $configuration = [ + 'width' => null, + 'height' => null, + 'minWidth' => null, + 'minHeight' => null, + 'maxWidth' => null, + 'maxHeight' => null, + 'cropVariant' => 'foo', + ]; + $imageViewHelper->_call('getProcessingInstructions', $configuration); + } + + /** + * @test + */ + public function getProcessingInstructionsCallsGetImageCropVariantIfNotConfigured(): void + { + $imageViewHelper = $this->getAccessibleMock( + ImageViewHelper::class, + ['getCropVariantCollection', 'getImageCropVariant'], + [], + '', + false + ); + $image = $this->getMockBuilder(File::class)->disableOriginalConstructor(true)->getMock(); + $cropVariantCollection = $this->getMockBuilder(CropVariantCollection::class)->disableOriginalConstructor(true)->getMock(); + $cropVariantCollection->expects(self::once())->method('getCropArea')->with('bar'); + $imageViewHelper->expects(self::once())->method('getCropVariantCollection')->willReturn($cropVariantCollection); + $imageViewHelper->expects(self::once())->method('getImageCropVariant')->willReturn('bar'); + $imageViewHelper->_set('image', $image); + $configuration = [ + 'width' => null, + 'height' => null, + 'minWidth' => null, + 'minHeight' => null, + 'maxWidth' => null, + 'maxHeight' => null, + ]; + $imageViewHelper->_call('getProcessingInstructions', $configuration); + } + + /** + * @test + */ + public function getImageCropVariantReturnsCropVariantFromArguments(): void + { + $imageViewHelper = $this->getAccessibleMock( + ImageViewHelper::class, + ['foo'], + [], + '', + false + ); + $imageViewHelper->_set('arguments', ['cropVariant' => 'bar']); + $cropVariant = $imageViewHelper->_call('getImageCropVariant'); + self::assertSame('bar', $cropVariant); + } + + /** + * @test + */ + public function getImageCropVariantReturnsDefaultIfNotSet(): void + { + $imageViewHelper = $this->getAccessibleMock( + ImageViewHelper::class, + ['foo'], + [], + '', + false + ); + $cropVariant = $imageViewHelper->_call('getImageCropVariant'); + self::assertSame('default', $cropVariant); + } +}