Skip to content

Commit 1b90302

Browse files
ENGCOM-9148: #28239: resize command does not process hidden images #33452
2 parents 70d1517 + bbb09dc commit 1b90302

File tree

3 files changed

+140
-30
lines changed

3 files changed

+140
-30
lines changed

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Symfony\Component\Console\Output\OutputInterface;
1919
use Symfony\Component\Console\Input\InputOption;
2020
use Symfony\Component\Console\Command\Command;
21-
use Magento\Catalog\Model\ResourceModel\Product\Image as ProductImage;
21+
2222

2323
/**
2424
* Resizes product images according to theme view definitions.
@@ -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
*/
@@ -51,31 +56,28 @@ class ImagesResizeCommand extends Command
5156
private $progressBarFactory;
5257

5358
/**
54-
* @var ProductImage
59+
* @var bool
5560
*/
56-
private $productImage;
61+
private $skipHiddenImages = false;
5762

5863
/**
5964
* @param State $appState
6065
* @param ImageResize $imageResize
6166
* @param ImageResizeScheduler $imageResizeScheduler
6267
* @param ProgressBarFactory $progressBarFactory
63-
* @param ProductImage $productImage
6468
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
6569
*/
6670
public function __construct(
6771
State $appState,
6872
ImageResize $imageResize,
6973
ImageResizeScheduler $imageResizeScheduler,
70-
ProgressBarFactory $progressBarFactory,
71-
ProductImage $productImage
74+
ProgressBarFactory $progressBarFactory
7275
) {
7376
parent::__construct();
7477
$this->appState = $appState;
7578
$this->imageResize = $imageResize;
7679
$this->imageResizeScheduler = $imageResizeScheduler;
7780
$this->progressBarFactory = $progressBarFactory;
78-
$this->productImage = $productImage;
7981
}
8082

8183
/**
@@ -102,6 +104,12 @@ private function getOptionsList() : array
102104
InputOption::VALUE_NONE,
103105
'Resize image in asynchronous mode'
104106
),
107+
new InputOption(
108+
self::SKIP_HIDDEN_IMAGES,
109+
null,
110+
InputOption::VALUE_NONE,
111+
'Do not process images marked as hidden from product page'
112+
),
105113
];
106114
}
107115

@@ -112,6 +120,7 @@ private function getOptionsList() : array
112120
*/
113121
protected function execute(InputInterface $input, OutputInterface $output)
114122
{
123+
$this->skipHiddenImages = $input->getOption(self::SKIP_HIDDEN_IMAGES);
115124
$result = $input->getOption(self::ASYNC_RESIZE) ?
116125
$this->executeAsync($output) : $this->executeSync($output);
117126

@@ -129,7 +138,7 @@ private function executeSync(OutputInterface $output): int
129138
try {
130139
$errors = [];
131140
$this->appState->setAreaCode(Area::AREA_GLOBAL);
132-
$generator = $this->imageResize->resizeFromThemes();
141+
$generator = $this->imageResize->resizeFromThemes(null, $this->skipHiddenImages);
133142

134143
/** @var ProgressBar $progress */
135144
$progress = $this->progressBarFactory->create(
@@ -194,7 +203,7 @@ private function executeAsync(OutputInterface $output): int
194203
$progress = $this->progressBarFactory->create(
195204
[
196205
'output' => $output,
197-
'max' => $this->productImage->getCountUsedProductImages()
206+
'max' => $this->imageResize->getCountProductImages($this->skipHiddenImages)
198207
]
199208
);
200209
$progress->setFormat(
@@ -205,7 +214,7 @@ private function executeAsync(OutputInterface $output): int
205214
$progress->setOverwrite(false);
206215
}
207216

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

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: 97 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,33 @@ 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
*/
125131
private $storeManager;
126132

133+
/**
134+
* @var string
135+
*/
136+
private $testImageHiddenfilepath;
137+
127138
/**
128139
* @inheritDoc
129140
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
130141
*/
131142
protected function setUp(): void
132143
{
133144
$this->testfilename = "image.jpg";
145+
$this->testImageHiddenFilename = "image_hidden.jpg";
134146
$this->testfilepath = "/image.jpg";
147+
$this->testImageHiddenfilepath = "/image_hidden.jpg";
148+
135149

136150
$this->appStateMock = $this->createMock(State::class);
137151
$this->imageConfigMock = $this->createMock(MediaConfig::class);
@@ -160,7 +174,7 @@ protected function setUp(): void
160174

161175
$this->assetImageMock->expects($this->any())
162176
->method('getPath')
163-
->willReturn($this->testfilepath);
177+
->willReturnOnConsecutiveCalls($this->testfilepath, $this->testImageHiddenfilepath);
164178
$this->assetImageFactoryMock->expects($this->any())
165179
->method('create')
166180
->willReturn($this->assetImageMock);
@@ -182,16 +196,15 @@ protected function setUp(): void
182196

183197
$this->imageConfigMock->expects($this->any())
184198
->method('getMediaPath')
185-
->with($this->testfilename)
186-
->willReturn($this->testfilepath);
199+
->withConsecutive([$this->testfilename], [$this->testImageHiddenFilename])
200+
->willReturnOnConsecutiveCalls($this->testfilepath, $this->testImageHiddenfilepath);
187201
$this->mediaDirectoryMock->expects($this->any())
188202
->method('getAbsolutePath')
189-
->with($this->testfilepath)
190-
->willReturn($this->testfilepath);
203+
->withConsecutive([$this->testfilepath], [$this->testImageHiddenfilepath])
204+
->willReturnOnConsecutiveCalls($this->testfilepath, $this->testImageHiddenfilepath);
191205
$this->mediaDirectoryMock->expects($this->any())
192206
->method('getRelativePath')
193-
->with($this->testfilepath)
194-
->willReturn($this->testfilepath);
207+
->willReturnOnConsecutiveCalls($this->testfilepath, $this->testImageHiddenfilepath);
195208

196209
$this->viewMock->expects($this->any())
197210
->method('getMediaEntities')
@@ -248,7 +261,7 @@ public function testResizeFromThemesMediaStorageDatabase()
248261
->willReturn(false);
249262

250263
$imageMock = $this->createMock(Image::class);
251-
$this->imageFactoryMock->expects($this->once())
264+
$this->imageFactoryMock->expects($this->any())
252265
->method('create')
253266
->willReturn($imageMock);
254267

@@ -268,17 +281,17 @@ function () {
268281

269282
$this->mediaDirectoryMock->expects($this->any())
270283
->method('isFile')
271-
->with($this->testfilepath)
284+
->withConsecutive([$this->testfilepath], [$this->testImageHiddenfilepath])
272285
->willReturn(true);
273286

274-
$this->databaseMock->expects($this->once())
287+
$this->databaseMock->expects($this->any())
275288
->method('saveFileToFilesystem')
276-
->with($this->testfilepath);
277-
$this->databaseMock->expects($this->once())
289+
->withConsecutive([$this->testfilepath], [$this->testImageHiddenfilepath]);
290+
$this->databaseMock->expects($this->any())
278291
->method('saveFile')
279-
->with($this->testfilepath);
292+
->withConsecutive([$this->testfilepath], [$this->testImageHiddenfilepath]);
280293

281-
$generator = $this->service->resizeFromThemes(['test-theme']);
294+
$generator = $this->service->resizeFromThemes(['test-theme'], true);
282295
while ($generator->valid()) {
283296
$resizeInfo = $generator->key();
284297
$this->assertEquals('image.jpg', $resizeInfo['filename']);
@@ -287,6 +300,73 @@ function () {
287300
}
288301
}
289302

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

299-
$this->imageFactoryMock->expects($this->once())
379+
$this->imageFactoryMock->expects($this->any())
300380
->method('create')
301381
->willThrowException(new \InvalidArgumentException('Unsupported image format.'));
302382

@@ -316,10 +396,10 @@ function () {
316396

317397
$this->mediaDirectoryMock->expects($this->any())
318398
->method('isFile')
319-
->with($this->testfilepath)
399+
->withConsecutive([$this->testfilepath], [$this->testImageHiddenfilepath])
320400
->willReturn(true);
321401

322-
$generator = $this->service->resizeFromThemes(['test-theme']);
402+
$generator = $this->service->resizeFromThemes(['test-theme'], true);
323403
while ($generator->valid()) {
324404
$resizeInfo = $generator->key();
325405
$this->assertEquals('Unsupported image format.', $resizeInfo['error']);

0 commit comments

Comments
 (0)