Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 0d01f54

Browse files
committed
magento/magento2#4711: Improve error reporting for products images import[forwardport].
1 parent e150a7e commit 0d01f54

File tree

4 files changed

+115
-5
lines changed

4 files changed

+115
-5
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,6 +2047,7 @@ protected function uploadMediaFiles($fileName, $renameFileOff = false)
20472047
$res = $this->_getUploader()->move($fileName, $renameFileOff);
20482048
return $res['file'];
20492049
} catch (\Exception $e) {
2050+
$this->_logger->critical($e);
20502051
return '';
20512052
}
20522053
}

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,65 @@ 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+
$fileUploaderMock = $this
1213+
->getMockBuilder(\Magento\CatalogImportExport\Model\Import\Uploader::class)
1214+
->disableOriginalConstructor()
1215+
->getMock();
1216+
$fileUploaderMock
1217+
->expects($this->once())
1218+
->method('move')
1219+
->willReturnCallback(
1220+
function ($name) use ($throwException, $exception) {
1221+
if ($throwException) {
1222+
throw $exception;
1223+
}
1224+
return ['file' => $name];
1225+
}
1226+
);
1227+
$this->setPropertyValue(
1228+
$this->importProduct,
1229+
'_fileUploader',
1230+
$fileUploaderMock
1231+
);
1232+
$actualFileName = $this->invokeMethod(
1233+
$this->importProduct,
1234+
'uploadMediaFiles',
1235+
[$fileName]
1236+
);
1237+
$this->assertEquals(
1238+
$expectedFileName,
1239+
$actualFileName
1240+
);
1241+
}
1242+
1243+
/**
1244+
* Data provider for testUploadMediaFiles.
1245+
*
1246+
* @return array
1247+
*/
1248+
public function uploadMediaFilesDataProvider()
1249+
{
1250+
return [
1251+
['test1.jpg', false],
1252+
['test2.jpg', true],
1253+
];
1254+
}
1255+
11971256
public function getImagesFromRowDataProvider()
11981257
{
11991258
return [

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

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
use Magento\Framework\Filesystem;
2525
use Magento\ImportExport\Model\Import;
2626
use Magento\Store\Model\Store;
27-
use Magento\UrlRewrite\Model\UrlRewrite;
27+
use Psr\Log\LoggerInterface;
2828

2929
/**
3030
* Class ProductTest
@@ -60,11 +60,20 @@ class ProductTest extends \Magento\TestFramework\Indexer\TestCase
6060
*/
6161
protected $objectManager;
6262

63+
/**
64+
* @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
65+
*/
66+
private $logger;
67+
6368
protected function setUp()
6469
{
6570
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
71+
$this->logger = $this->getMockBuilder(LoggerInterface::class)
72+
->disableOriginalConstructor()
73+
->getMock();
6674
$this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
67-
\Magento\CatalogImportExport\Model\Import\Product::class
75+
\Magento\CatalogImportExport\Model\Import\Product::class,
76+
['logger' => $this->logger]
6877
);
6978
parent::setUp();
7079
}
@@ -657,6 +666,21 @@ public function testSaveMediaImage()
657666
$this->assertEquals('Additional Image Label Two', $additionalImageTwoItem->getLabel());
658667
}
659668

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

742+
/**
743+
* Copy incorrect fixture image into media import directory.
744+
*/
745+
public static function mediaImportImageFixtureError()
746+
{
747+
/** @var \Magento\Framework\Filesystem\Directory\Write $mediaDirectory */
748+
$mediaDirectory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
749+
\Magento\Framework\Filesystem::class
750+
)->getDirectoryWrite(
751+
DirectoryList::MEDIA
752+
);
753+
$dirPath = $mediaDirectory->getAbsolutePath('import');
754+
$items = [
755+
[
756+
'source' => __DIR__ . '/_files/magento_additional_image_error.jpg',
757+
'dest' => $dirPath . '/magento_additional_image_two.jpg',
758+
],
759+
];
760+
foreach ($items as $item) {
761+
copy($item['source'], $item['dest']);
762+
}
763+
}
764+
765+
718766
/**
719767
* Export CSV string to array
720768
*
@@ -1563,11 +1611,13 @@ public function testProductWithWrappedAdditionalAttributes()
15631611
}
15641612

15651613
/**
1566-
* Import and check data from file
1614+
* Import and check data from file.
15671615
*
15681616
* @param string $fileName
1617+
* @param int $expectedErrors
1618+
* @return void
15691619
*/
1570-
private function importDataForMediaTest($fileName)
1620+
private function importDataForMediaTest(string $fileName, int $expectedErrors = 0)
15711621
{
15721622
$filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
15731623
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
@@ -1605,7 +1655,7 @@ private function importDataForMediaTest($fileName)
16051655
$this->assertTrue($errors->getErrorsCount() == 0);
16061656

16071657
$this->_model->importData();
1608-
$this->assertTrue($this->_model->getErrorAggregator()->getErrorsCount() == 0);
1658+
$this->assertTrue($this->_model->getErrorAggregator()->getErrorsCount() == $expectedErrors);
16091659
}
16101660

16111661
/**

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

Loading

0 commit comments

Comments
 (0)