Skip to content

Commit

Permalink
#28239: resize command does not process hidden images
Browse files Browse the repository at this point in the history
- added new options --skip-hidden-images
- added new unit test
  • Loading branch information
BGorbach committed Jul 10, 2021
1 parent bf4cdad commit c35cd1f
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ class ImagesResizeCommand extends Command
*/
const ASYNC_RESIZE = 'async';

/**
* Do not process images marked as hidden from product page
*/
const SKIP_HIDDEN_IMAGES = 'skip_hidden_images';

/**
* @var ImageResizeScheduler
*/
Expand All @@ -55,6 +60,11 @@ class ImagesResizeCommand extends Command
*/
private $productImage;

/**
* @var bool
*/
private $skipHiddenImages = false;

/**
* @param State $appState
* @param ImageResize $imageResize
Expand Down Expand Up @@ -102,6 +112,12 @@ private function getOptionsList() : array
InputOption::VALUE_NONE,
'Resize image in asynchronous mode'
),
new InputOption(
self::SKIP_HIDDEN_IMAGES,
null,
InputOption::VALUE_NONE,
'Do not process images marked as hidden from product page'
),
];
}

Expand All @@ -112,6 +128,7 @@ private function getOptionsList() : array
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->skipHiddenImages = $input->getOption(self::SKIP_HIDDEN_IMAGES);
$result = $input->getOption(self::ASYNC_RESIZE) ?
$this->executeAsync($output) : $this->executeSync($output);

Expand All @@ -129,7 +146,7 @@ private function executeSync(OutputInterface $output): int
try {
$errors = [];
$this->appState->setAreaCode(Area::AREA_GLOBAL);
$generator = $this->imageResize->resizeFromThemes();
$generator = $this->imageResize->resizeFromThemes(null, $this->skipHiddenImages);

/** @var ProgressBar $progress */
$progress = $this->progressBarFactory->create(
Expand Down Expand Up @@ -194,7 +211,7 @@ private function executeAsync(OutputInterface $output): int
$progress = $this->progressBarFactory->create(
[
'output' => $output,
'max' => $this->productImage->getCountUsedProductImages()
'max' => $this->imageResize->getCountProductImages($this->skipHiddenImages)
]
);
$progress->setFormat(
Expand All @@ -205,7 +222,7 @@ private function executeAsync(OutputInterface $output): int
$progress->setOverwrite(false);
}

$productImages = $this->productImage->getUsedProductImages();
$productImages = $this->imageResize->getProductImages($this->skipHiddenImages);
foreach ($productImages as $image) {
$result = $this->imageResizeScheduler->schedule($image['filepath']);

Expand Down
27 changes: 24 additions & 3 deletions app/code/Magento/MediaStorage/Service/ImageResize.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,18 @@ public function resizeFromImageName(string $originalImageName)
* Create resized images of different sizes from themes.
*
* @param array|null $themes
* @param bool $skipHiddenImages
* @return Generator
* @throws NotFoundException
*/
public function resizeFromThemes(array $themes = null): Generator
public function resizeFromThemes(array $themes = null, bool $skipHiddenImages = false): Generator

This comment has been minimized.

Copy link
@koenner01

koenner01 Mar 21, 2023

Contributor

By setting the $skipHiddenImages flag by default to false you are breaking compatibility with previous logic;
Because the value is false, the ImageResize::getAllProductImages function is now called by default instead of ImageResize::getUsedProductImages.

Anybody that was previously using the "resizeFromThemes" function should now be setting the flag to "true" to keep the same logic that they had before.

{
$count = $this->productImage->getCountUsedProductImages();
$count = $this->getCountProductImages($skipHiddenImages);
if (!$count) {
throw new NotFoundException(__('Cannot resize images - product images not found'));
}

$productImages = $this->productImage->getUsedProductImages();
$productImages = $this->getProductImages($skipHiddenImages);
$viewImages = $this->getViewImages($themes ?? $this->getThemesInUse());

foreach ($productImages as $image) {
Expand Down Expand Up @@ -210,6 +211,26 @@ public function resizeFromThemes(array $themes = null): Generator
}
}

/**
* @param bool $skipHiddenImages
* @return int
*/
public function getCountProductImages(bool $skipHiddenImages = false): int
{
return $skipHiddenImages ?
$this->productImage->getCountUsedProductImages() : $this->productImage->getCountAllProductImages();
}

/**
* @param bool $skipHiddenImages
* @return Generator
*/
public function getProductImages(bool $skipHiddenImages = false): \Generator
{
return $skipHiddenImages ?
$this->productImage->getUsedProductImages() : $this->productImage->getAllProductImages();
}

/**
* Search the current theme.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ class ImageResizeTest extends TestCase
* @var string
*/
private $testfilepath;

/**
* @var string
*/
private $testImageHiddenFilename;

/**
* @var MockObject|StoreManagerInterface
*/
Expand All @@ -131,6 +137,7 @@ class ImageResizeTest extends TestCase
protected function setUp(): void
{
$this->testfilename = "image.jpg";
$this->testImageHiddenFilename = "image_hidden.jpg";
$this->testfilepath = "/image.jpg";

$this->appStateMock = $this->createMock(State::class);
Expand Down Expand Up @@ -278,7 +285,7 @@ function () {
->method('saveFile')
->with($this->testfilepath);

$generator = $this->service->resizeFromThemes(['test-theme']);
$generator = $this->service->resizeFromThemes(['test-theme'], true);
while ($generator->valid()) {
$resizeInfo = $generator->key();
$this->assertEquals('image.jpg', $resizeInfo['filename']);
Expand All @@ -287,6 +294,73 @@ function () {
}
}

public function testResizeFromThemesHiddenImagesMediaStorageDatabase()
{
$this->databaseMock->expects($this->any())
->method('checkDbUsage')
->willReturn(true);
$this->databaseMock->expects($this->any())
->method('fileExists')
->willReturn(false);

$imageMock = $this->createMock(Image::class);
$this->imageFactoryMock->expects($this->once())
->method('create')
->willReturn($imageMock);

$this->productImageMock->expects($this->any())
->method('getCountUsedProductImages')
->willReturn(1);
$this->productImageMock->expects($this->any())
->method('getUsedProductImages')
->willReturnCallback(
function () {
$data = [[ 'filepath' => $this->testfilename ]];
foreach ($data as $e) {
yield $e;
}
}
);

$this->productImageMock->expects($this->any())
->method('getCountAllProductImages')
->willReturn(2);
$this->productImageMock->expects($this->any())
->method('getAllProductImages')
->willReturnCallback(
function () {
$data = [[ 'filepath' => $this->testfilename ], [ 'filepath' => $this->testImageHiddenFilename ]];
foreach ($data as $e) {
yield $e;
}
}
);

$this->mediaDirectoryMock->expects($this->any())
->method('isFile')
->with($this->testfilepath)
->willReturn(true);

$this->databaseMock->expects($this->once())
->method('saveFileToFilesystem')
->with($this->testfilepath);
$this->databaseMock->expects($this->once())
->method('saveFile')
->with($this->testfilepath);

$this->assertEquals(2, $this->service->getCountProductImages());
$this->assertEquals(1, $this->service->getCountProductImages(true));

$generator = $this->service->resizeFromThemes(['test-theme']);
while ($generator->valid()) {
$resizeInfo = $generator->key();
$this->assertContains($resizeInfo['filename'], [$this->testfilename, $this->testImageHiddenFilename]);
$this->assertEmpty($resizeInfo['error']);
$generator->next();
}

}

public function testResizeFromThemesUnsupportedImage()
{
$this->databaseMock->expects($this->any())
Expand Down Expand Up @@ -319,7 +393,7 @@ function () {
->with($this->testfilepath)
->willReturn(true);

$generator = $this->service->resizeFromThemes(['test-theme']);
$generator = $this->service->resizeFromThemes(['test-theme'], true);
while ($generator->valid()) {
$resizeInfo = $generator->key();
$this->assertEquals('Unsupported image format.', $resizeInfo['error']);
Expand Down

0 comments on commit c35cd1f

Please sign in to comment.