Skip to content

Commit

Permalink
Merge pull request #39 from b13/bugfix/issue-37
Browse files Browse the repository at this point in the history
[BUGFIX] cropVariants of sources
  • Loading branch information
achimfritz authored Feb 2, 2023
2 parents 5f95de5 + 44554ff commit 5323f53
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions Classes/ViewHelpers/ImageViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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'],
Expand All @@ -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
Expand Down
113 changes: 113 additions & 0 deletions Tests/Unit/ViewHelpers/ImageViewHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

declare(strict_types=1);

namespace B13\Picture\Tests\Unit\ViewHelpers;

/*
* This file is part of TYPO3 CMS-based extension "picture" by b13.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*/

use B13\Picture\ViewHelpers\ImageViewHelper;
use TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

class ImageViewHelperTest extends UnitTestCase
{
/**
* @test
*/
public function getProcessingInstructionsUseCropVariantFromConfigurationIfSet(): 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('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);
}
}

0 comments on commit 5323f53

Please sign in to comment.