Skip to content

Commit 266e13f

Browse files
MAGETWO-82886: MAGETWO-4711: Improve error reporting for products images import. #11779
2 parents 31062ce + b9359b5 commit 266e13f

File tree

4 files changed

+118
-4
lines changed

4 files changed

+118
-4
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,6 +2042,7 @@ protected function uploadMediaFiles($fileName, $renameFileOff = false)
20422042
$res = $this->_getUploader()->move($fileName, $renameFileOff);
20432043
return $res['file'];
20442044
} catch (\Exception $e) {
2045+
$this->_logger->critical($e);
20452046
return '';
20462047
}
20472048
}

app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/ProductTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,70 @@ public function testParseAttributesWithWrappedValuesWillReturnsLowercasedAttribu
11941194
$this->assertArrayNotHasKey('PARAM2', $attributes);
11951195
}
11961196

1197+
/**
1198+
* Test that errors occurred during importing images are logged.
1199+
*
1200+
* @param string $fileName
1201+
* @param bool $throwException
1202+
* @dataProvider uploadMediaFilesDataProvider
1203+
*/
1204+
public function testUploadMediaFiles(string $fileName, bool $throwException)
1205+
{
1206+
$exception = new \Exception();
1207+
$expectedFileName = $fileName;
1208+
if ($throwException) {
1209+
$expectedFileName = '';
1210+
$this->_logger->expects($this->once())->method('critical')->with($exception);
1211+
}
1212+
1213+
$fileUploaderMock = $this
1214+
->getMockBuilder(\Magento\CatalogImportExport\Model\Import\Uploader::class)
1215+
->disableOriginalConstructor()
1216+
->getMock();
1217+
1218+
$fileUploaderMock
1219+
->expects($this->once())
1220+
->method('move')
1221+
->willReturnCallback(
1222+
function ($name) use ($throwException, $exception) {
1223+
if ($throwException) {
1224+
throw $exception;
1225+
}
1226+
return ['file' => $name];
1227+
}
1228+
);
1229+
1230+
$this->setPropertyValue(
1231+
$this->importProduct,
1232+
'_fileUploader',
1233+
$fileUploaderMock
1234+
);
1235+
1236+
$actualFileName = $this->invokeMethod(
1237+
$this->importProduct,
1238+
'uploadMediaFiles',
1239+
[$fileName]
1240+
);
1241+
1242+
$this->assertEquals(
1243+
$expectedFileName,
1244+
$actualFileName
1245+
);
1246+
}
1247+
1248+
/**
1249+
* Data provider for testUploadMediaFiles.
1250+
*
1251+
* @return array
1252+
*/
1253+
public function uploadMediaFilesDataProvider()
1254+
{
1255+
return [
1256+
['test1.jpg', false],
1257+
['test2.jpg', true],
1258+
];
1259+
}
1260+
11971261
public function getImagesFromRowDataProvider()
11981262
{
11991263
return [

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
use Magento\Framework\Filesystem;
2727
use Magento\ImportExport\Model\Import;
2828
use Magento\Store\Model\Store;
29-
use Magento\UrlRewrite\Model\UrlRewrite;
29+
use Psr\Log\LoggerInterface;
3030

3131
/**
3232
* Class ProductTest
@@ -62,11 +62,20 @@ class ProductTest extends \Magento\TestFramework\Indexer\TestCase
6262
*/
6363
protected $objectManager;
6464

65+
/**
66+
* @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
67+
*/
68+
private $logger;
69+
6570
protected function setUp()
6671
{
6772
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
73+
$this->logger = $this->getMockBuilder(LoggerInterface::class)
74+
->disableOriginalConstructor()
75+
->getMock();
6876
$this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
69-
\Magento\CatalogImportExport\Model\Import\Product::class
77+
\Magento\CatalogImportExport\Model\Import\Product::class,
78+
['logger' => $this->logger]
7079
);
7180
parent::setUp();
7281
}
@@ -659,6 +668,21 @@ public function testSaveMediaImage()
659668
$this->assertEquals('Additional Image Label Two', $additionalImageTwoItem->getLabel());
660669
}
661670

671+
/**
672+
* Test that errors occurred during importing images are logged.
673+
*
674+
* @magentoDataIsolation enabled
675+
* @magentoAppIsolation enabled
676+
* @magentoDataFixture mediaImportImageFixture
677+
* @magentoDataFixture mediaImportImageFixtureError
678+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
679+
*/
680+
public function testSaveMediaImageError()
681+
{
682+
$this->logger->expects(self::once())->method('critical');
683+
$this->importDataForMediaTest('import_media.csv', 1);
684+
}
685+
662686
/**
663687
* Copy fixture images into media import directory
664688
*/
@@ -717,6 +741,30 @@ public static function mediaImportImageFixtureRollback()
717741
$mediaDirectory->delete('catalog');
718742
}
719743

744+
/**
745+
* Copy incorrect fixture image into media import directory.
746+
*/
747+
public static function mediaImportImageFixtureError()
748+
{
749+
/** @var \Magento\Framework\Filesystem\Directory\Write $mediaDirectory */
750+
$mediaDirectory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
751+
\Magento\Framework\Filesystem::class
752+
)->getDirectoryWrite(
753+
DirectoryList::MEDIA
754+
);
755+
$dirPath = $mediaDirectory->getAbsolutePath('import');
756+
$items = [
757+
[
758+
'source' => __DIR__ . '/_files/magento_additional_image_error.jpg',
759+
'dest' => $dirPath . '/magento_additional_image_two.jpg',
760+
],
761+
];
762+
foreach ($items as $item) {
763+
copy($item['source'], $item['dest']);
764+
}
765+
}
766+
767+
720768
/**
721769
* Export CSV string to array
722770
*
@@ -1564,8 +1612,9 @@ public function testProductWithWrappedAdditionalAttributes()
15641612
* Import and check data from file
15651613
*
15661614
* @param string $fileName
1615+
* @param int $expectedErrors
15671616
*/
1568-
private function importDataForMediaTest($fileName)
1617+
private function importDataForMediaTest(string $fileName, int $expectedErrors = 0)
15691618
{
15701619
$filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
15711620
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
@@ -1603,7 +1652,7 @@ private function importDataForMediaTest($fileName)
16031652
$this->assertTrue($errors->getErrorsCount() == 0);
16041653

16051654
$this->_model->importData();
1606-
$this->assertTrue($this->_model->getErrorAggregator()->getErrorsCount() == 0);
1655+
$this->assertTrue($this->_model->getErrorAggregator()->getErrorsCount() == $expectedErrors);
16071656
}
16081657

16091658
/**

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/_files/magento_additional_image_error.jpg

Loading

0 commit comments

Comments
 (0)