diff --git a/Neos.Media/Classes/Command/MediaCommandController.php b/Neos.Media/Classes/Command/MediaCommandController.php index c1fec0dc515..1fea332c94b 100644 --- a/Neos.Media/Classes/Command/MediaCommandController.php +++ b/Neos.Media/Classes/Command/MediaCommandController.php @@ -420,18 +420,21 @@ public function renderThumbnailsCommand(int $limit = null, bool $quiet = false) */ public function removeOutdatedVariantsCommand(bool $quiet = false, bool $assumeYes = false, bool $filterCroppedVariants = false, int $limit = null, string $assetSource = '', string $presetIdentifier = '') { - // will read all presets defined in 'Settings.Neos.Media.yaml' - $variantPresetConfigs = $this->assetVariantGenerator->getVariantPresets(); - - $filterByAssetSourceIdentifier = $assetSource; + if (empty($presetIdentifier)) { + // will read all presets defined in 'Settings.Neos.Media.yaml' + $currentPresets = $this->imageVariantService->getAllPresetsByConfiguration(); + } else { + // is --preset-identifier used + $currentPresets = $this->imageVariantService->getAllPresetsOfIdentifier($presetIdentifier); + } - // is --preset-identifier used - $currentPresets = $this->imageVariantService->getAllPresetsByConfigs($variantPresetConfigs, $presetIdentifier); if (empty($currentPresets)) { !$quiet && $this->output->outputLine(PHP_EOL . PHP_EOL . 'No presets found.'); exit; } + $filterByAssetSourceIdentifier = $assetSource; + // is --asset-source-filter used if (empty($filterByAssetSourceIdentifier)) { !$quiet && $this->outputLine(PHP_EOL . 'Searching for assets in all sources:'); diff --git a/Neos.Media/Classes/Domain/Repository/ImageVariantRepository.php b/Neos.Media/Classes/Domain/Repository/ImageVariantRepository.php index a3d6ab18ced..6ee257faf68 100644 --- a/Neos.Media/Classes/Domain/Repository/ImageVariantRepository.php +++ b/Neos.Media/Classes/Domain/Repository/ImageVariantRepository.php @@ -15,6 +15,7 @@ use Neos\Flow\Annotations as Flow; use Neos\Media\Domain\Model\ImageVariant; +use Neos\Media\Domain\ValueObject\Configuration\VariantPreset; /** * A repository for ImageVariants @@ -26,10 +27,13 @@ class ImageVariantRepository extends AssetRepository /** * Returns array of ImageVariants with outdated presets * - * @param String[] $configuredPresets + * @param VariantPreset[] $configuredPresets + * @param bool $deleteOnlyFromGivenPresets + * @param bool $filterCroppedVariants + * @param int|null $limit * @return ImageVariant[] */ - public function findAllWithOutdatedPresets(array $configuredPresets, bool $isCliPresetFilter, bool $filterCroppedVariants, int $limit = null): array + public function findAllWithOutdatedPresets(array $configuredPresets, bool $deleteOnlyFromGivenPresets, bool $filterCroppedVariants, int $limit = null): array { $configuredIdentifiers = array_keys($configuredPresets); $queryBuilder = $this->entityManager->createQueryBuilder() @@ -37,14 +41,26 @@ public function findAllWithOutdatedPresets(array $configuredPresets, bool $isCli ->from(ImageVariant::class, 'iv') ->setMaxResults($limit); - if (!$isCliPresetFilter) { + if (!$deleteOnlyFromGivenPresets) { + /** + * for completely outdated preset configurations + * + * EXAMPLE: + * - you have the identifiers Neos.Cool, Neos.Yeah (and there was a Neos.Awesome previously) + * case 1: + * - the user want to delete variants from Neos.Yeah + * - condition will not be executed - deleteFromGivenPresets is true + * case 2: + * - no preset to delete from configured + * - condition will be executed - whole Neos.Awesome will be added to query + */ $queryBuilder ->where('iv.presetIdentifier NOT IN (:configuredIdentifiers)') ->setParameter('configuredIdentifiers', $configuredIdentifiers); } if ($filterCroppedVariants) { - // custom crops will be saved with null value as presetIdentifier and presetVariantName + // custom cropped variants will be saved with null value as presetIdentifier and presetVariantName $queryBuilder ->orWhere('iv.presetIdentifier IS NULL AND iv.presetVariantName IS NULL'); } diff --git a/Neos.Media/Classes/Domain/Service/ImageVariantService.php b/Neos.Media/Classes/Domain/Service/ImageVariantService.php index c34f6e6400d..65c496df433 100644 --- a/Neos.Media/Classes/Domain/Service/ImageVariantService.php +++ b/Neos.Media/Classes/Domain/Service/ImageVariantService.php @@ -22,36 +22,39 @@ */ class ImageVariantService { + /** + * @Flow\Inject + * @var AssetVariantGenerator + */ + protected $assetVariantGenerator; + /** * Return all presets defined in 'Settings.Neos.Media.yaml' with presetName as key * - * @param array $variantPresetConfigs * @param string $presetIdentifier - * @return array + * @return VariantPreset[] */ - public function getAllPresetsByConfigs(array $variantPresetConfigs, string $presetIdentifier = ''): array + public function getAllPresetsOfIdentifier(string $presetIdentifier): array { - $presets = []; + $variantPresetConfigurations = $this->getAllPresetsByConfiguration(); - if (!empty($presetIdentifier) && !key_exists($presetIdentifier, $variantPresetConfigs)) { - return $presets; - } + $variantPresetName = array_key_exists($presetIdentifier, $variantPresetConfigurations); - /** @var VariantPreset[] $variantPresetConfigs */ - if (!empty($presetIdentifier)) { - foreach ($variantPresetConfigs[$presetIdentifier]->variants() as $presetVariant) { - $presets[$presetIdentifier][] = $presetVariant->identifier(); - } - } else { - foreach ($variantPresetConfigs as $presetsConfig) { - $variantPresetName = array_search($presetsConfig, $variantPresetConfigs); - $presetKeys = array_keys($presetsConfig->variants()); - foreach ($presetKeys as $preset) { - $presets[(string)$variantPresetName][] = $preset; - } - } + if (!$variantPresetName) { + // the given presetIdentifier ist not included in variantPresetConfigurations + return []; } - return $presets; + return [$variantPresetConfigurations[$presetIdentifier]]; + } + + /** + * Return presets from 'Settings.Neos.Media.yaml' + * + * @return VariantPreset[] + */ + public function getAllPresetsByConfiguration(): array + { + return $this->assetVariantGenerator->getVariantPresets(); } } diff --git a/Neos.Media/Tests/Unit/Domain/Service/ImageVariantServiceTest.php b/Neos.Media/Tests/Unit/Domain/Service/ImageVariantServiceTest.php index 557615d1bd6..9179f9789ff 100644 --- a/Neos.Media/Tests/Unit/Domain/Service/ImageVariantServiceTest.php +++ b/Neos.Media/Tests/Unit/Domain/Service/ImageVariantServiceTest.php @@ -1,4 +1,5 @@ setValue($flowPreset, $flowPresetConfiguration); return [ - [['neos' => $neosPreset, 'flow' => $flowPreset], null], - [['neos' => $neosPreset, 'flow' => $flowPreset], ''], - [['neos' => $neosPreset, 'flow' => $flowPreset], 'neos'] + 'empty' => [['neos' => $neosPreset, 'flow' => $flowPreset], '', true], + 'known preset' => [['neos' => $neosPreset, 'flow' => $flowPreset], 'neos', false], + 'unknown preset' => [['neos' => $neosPreset, 'flow' => $flowPreset], 'imageVariant', true], ]; } /** * @test - * @param array $variantPresetConfigs + * @dataProvider getAllPresetsByConfigurationsProvider + * + * @param VariantPreset[] $variantPresets * @param string|null $presetIdentifier - * @dataProvider getAllPresetsByConfigsProvider + * @param bool $emptyResult + * + * @throws ReflectionException */ - public function getAllPresetsByConfigs(array $variantPresetConfigs, ?string $presetIdentifier): void + public function getAllPresetsByConfigs(array $variantPresets, ?string $presetIdentifier, bool $emptyResult): void { - if (is_null($presetIdentifier)) { - $presetsConfig = $this->imageVariantService->getAllPresetsByConfigs($variantPresetConfigs); - } else { - $presetsConfig = $this->imageVariantService->getAllPresetsByConfigs($variantPresetConfigs, $presetIdentifier); - } + $assetVariantGeneratorMock = $this->getMockBuilder(AssetVariantGenerator::class)->getMock(); + $assetVariantGeneratorMock->expects($this->once())->method('getVariantPresets')->willReturn($variantPresets); + + $imageVariantService = new ImageVariantService(); + + $imageVariantServiceReflection = new ReflectionClass(ImageVariantService::class); + $reflectionProperty = $imageVariantServiceReflection->getProperty('assetVariantGenerator'); + $reflectionProperty->setAccessible(true); + $reflectionProperty->setValue($imageVariantService, $assetVariantGeneratorMock); + + $reflectionMethod = $imageVariantServiceReflection->getMethod('getAllPresetsOfIdentifier'); + $reflectionMethod->setAccessible(true); + $presetsConfig = $reflectionMethod->invokeArgs($imageVariantService, [$presetIdentifier]); - if (!$presetIdentifier) { - self::assertEquals(['neos' => ['square', 'portrait'], 'flow' => ['panorama']], $presetsConfig); + if ($emptyResult) { + self::assertEquals([], $presetsConfig); } else { - self::assertEquals(['neos' => ['square', 'portrait']], $presetsConfig); + self::assertEquals([$variantPresets['neos']], $presetsConfig); } } }