Skip to content

Commit c35cd1f

Browse files
committed
#28239: resize command does not process hidden images
- added new options --skip-hidden-images - added new unit test
1 parent bf4cdad commit c35cd1f

File tree

3 files changed

+120
-8
lines changed

3 files changed

+120
-8
lines changed

app/code/Magento/MediaStorage/Console/Command/ImagesResizeCommand.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ class ImagesResizeCommand extends Command
3030
*/
3131
const ASYNC_RESIZE = 'async';
3232

33+
/**
34+
* Do not process images marked as hidden from product page
35+
*/
36+
const SKIP_HIDDEN_IMAGES = 'skip_hidden_images';
37+
3338
/**
3439
* @var ImageResizeScheduler
3540
*/
@@ -55,6 +60,11 @@ class ImagesResizeCommand extends Command
5560
*/
5661
private $productImage;
5762

63+
/**
64+
* @var bool
65+
*/
66+
private $skipHiddenImages = false;
67+
5868
/**
5969
* @param State $appState
6070
* @param ImageResize $imageResize
@@ -102,6 +112,12 @@ private function getOptionsList() : array
102112
InputOption::VALUE_NONE,
103113
'Resize image in asynchronous mode'
104114
),
115+
new InputOption(
116+
self::SKIP_HIDDEN_IMAGES,
117+
null,
118+
InputOption::VALUE_NONE,
119+
'Do not process images marked as hidden from product page'
120+
),
105121
];
106122
}
107123

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

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

134151
/** @var ProgressBar $progress */
135152
$progress = $this->progressBarFactory->create(
@@ -194,7 +211,7 @@ private function executeAsync(OutputInterface $output): int
194211
$progress = $this->progressBarFactory->create(
195212
[
196213
'output' => $output,
197-
'max' => $this->productImage->getCountUsedProductImages()
214+
'max' => $this->imageResize->getCountProductImages($this->skipHiddenImages)
198215
]
199216
);
200217
$progress->setFormat(
@@ -205,7 +222,7 @@ private function executeAsync(OutputInterface $output): int
205222
$progress->setOverwrite(false);
206223
}
207224

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

app/code/Magento/MediaStorage/Service/ImageResize.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,18 @@ public function resizeFromImageName(string $originalImageName)
171171
* Create resized images of different sizes from themes.
172172
*
173173
* @param array|null $themes
174+
* @param bool $skipHiddenImages
174175
* @return Generator
175176
* @throws NotFoundException
176177
*/
177-
public function resizeFromThemes(array $themes = null): Generator
178+
public function resizeFromThemes(array $themes = null, bool $skipHiddenImages = false): Generator
178179
{
179-
$count = $this->productImage->getCountUsedProductImages();
180+
$count = $this->getCountProductImages($skipHiddenImages);
180181
if (!$count) {
181182
throw new NotFoundException(__('Cannot resize images - product images not found'));
182183
}
183184

184-
$productImages = $this->productImage->getUsedProductImages();
185+
$productImages = $this->getProductImages($skipHiddenImages);
185186
$viewImages = $this->getViewImages($themes ?? $this->getThemesInUse());
186187

187188
foreach ($productImages as $image) {
@@ -210,6 +211,26 @@ public function resizeFromThemes(array $themes = null): Generator
210211
}
211212
}
212213

214+
/**
215+
* @param bool $skipHiddenImages
216+
* @return int
217+
*/
218+
public function getCountProductImages(bool $skipHiddenImages = false): int
219+
{
220+
return $skipHiddenImages ?
221+
$this->productImage->getCountUsedProductImages() : $this->productImage->getCountAllProductImages();
222+
}
223+
224+
/**
225+
* @param bool $skipHiddenImages
226+
* @return Generator
227+
*/
228+
public function getProductImages(bool $skipHiddenImages = false): \Generator
229+
{
230+
return $skipHiddenImages ?
231+
$this->productImage->getUsedProductImages() : $this->productImage->getAllProductImages();
232+
}
233+
213234
/**
214235
* Search the current theme.
215236
*

app/code/Magento/MediaStorage/Test/Unit/Service/ImageResizeTest.php

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ class ImageResizeTest extends TestCase
119119
* @var string
120120
*/
121121
private $testfilepath;
122+
123+
/**
124+
* @var string
125+
*/
126+
private $testImageHiddenFilename;
127+
122128
/**
123129
* @var MockObject|StoreManagerInterface
124130
*/
@@ -131,6 +137,7 @@ class ImageResizeTest extends TestCase
131137
protected function setUp(): void
132138
{
133139
$this->testfilename = "image.jpg";
140+
$this->testImageHiddenFilename = "image_hidden.jpg";
134141
$this->testfilepath = "/image.jpg";
135142

136143
$this->appStateMock = $this->createMock(State::class);
@@ -278,7 +285,7 @@ function () {
278285
->method('saveFile')
279286
->with($this->testfilepath);
280287

281-
$generator = $this->service->resizeFromThemes(['test-theme']);
288+
$generator = $this->service->resizeFromThemes(['test-theme'], true);
282289
while ($generator->valid()) {
283290
$resizeInfo = $generator->key();
284291
$this->assertEquals('image.jpg', $resizeInfo['filename']);
@@ -287,6 +294,73 @@ function () {
287294
}
288295
}
289296

297+
public function testResizeFromThemesHiddenImagesMediaStorageDatabase()
298+
{
299+
$this->databaseMock->expects($this->any())
300+
->method('checkDbUsage')
301+
->willReturn(true);
302+
$this->databaseMock->expects($this->any())
303+
->method('fileExists')
304+
->willReturn(false);
305+
306+
$imageMock = $this->createMock(Image::class);
307+
$this->imageFactoryMock->expects($this->once())
308+
->method('create')
309+
->willReturn($imageMock);
310+
311+
$this->productImageMock->expects($this->any())
312+
->method('getCountUsedProductImages')
313+
->willReturn(1);
314+
$this->productImageMock->expects($this->any())
315+
->method('getUsedProductImages')
316+
->willReturnCallback(
317+
function () {
318+
$data = [[ 'filepath' => $this->testfilename ]];
319+
foreach ($data as $e) {
320+
yield $e;
321+
}
322+
}
323+
);
324+
325+
$this->productImageMock->expects($this->any())
326+
->method('getCountAllProductImages')
327+
->willReturn(2);
328+
$this->productImageMock->expects($this->any())
329+
->method('getAllProductImages')
330+
->willReturnCallback(
331+
function () {
332+
$data = [[ 'filepath' => $this->testfilename ], [ 'filepath' => $this->testImageHiddenFilename ]];
333+
foreach ($data as $e) {
334+
yield $e;
335+
}
336+
}
337+
);
338+
339+
$this->mediaDirectoryMock->expects($this->any())
340+
->method('isFile')
341+
->with($this->testfilepath)
342+
->willReturn(true);
343+
344+
$this->databaseMock->expects($this->once())
345+
->method('saveFileToFilesystem')
346+
->with($this->testfilepath);
347+
$this->databaseMock->expects($this->once())
348+
->method('saveFile')
349+
->with($this->testfilepath);
350+
351+
$this->assertEquals(2, $this->service->getCountProductImages());
352+
$this->assertEquals(1, $this->service->getCountProductImages(true));
353+
354+
$generator = $this->service->resizeFromThemes(['test-theme']);
355+
while ($generator->valid()) {
356+
$resizeInfo = $generator->key();
357+
$this->assertContains($resizeInfo['filename'], [$this->testfilename, $this->testImageHiddenFilename]);
358+
$this->assertEmpty($resizeInfo['error']);
359+
$generator->next();
360+
}
361+
362+
}
363+
290364
public function testResizeFromThemesUnsupportedImage()
291365
{
292366
$this->databaseMock->expects($this->any())
@@ -319,7 +393,7 @@ function () {
319393
->with($this->testfilepath)
320394
->willReturn(true);
321395

322-
$generator = $this->service->resizeFromThemes(['test-theme']);
396+
$generator = $this->service->resizeFromThemes(['test-theme'], true);
323397
while ($generator->valid()) {
324398
$resizeInfo = $generator->key();
325399
$this->assertEquals('Unsupported image format.', $resizeInfo['error']);

0 commit comments

Comments
 (0)