Skip to content

Commit 8330cda

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-58227' into develop-pr2
2 parents 01b7b7e + a9d4c18 commit 8330cda

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
use Magento\Catalog\Model\Product\Visibility;
1212
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface as ValidatorInterface;
1313
use Magento\Framework\App\Filesystem\DirectoryList;
14+
use Magento\Framework\App\ObjectManager;
1415
use Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor;
1516
use Magento\Framework\Model\ResourceModel\Db\TransactionManagerInterface;
1617
use Magento\Framework\Stdlib\DateTime;
18+
use Magento\Framework\Filesystem;
1719
use Magento\ImportExport\Model\Import;
1820
use Magento\ImportExport\Model\Import\Entity\AbstractEntity;
1921
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingError;
@@ -658,6 +660,13 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
658660
*/
659661
private $multiLineSeparatorForRegexp;
660662

663+
/**
664+
* Container for filesystem object.
665+
*
666+
* @var Filesystem
667+
*/
668+
private $filesystem;
669+
661670
/**
662671
* Catalog config.
663672
*
@@ -761,6 +770,7 @@ public function __construct(
761770
$this->_linkFactory = $linkFactory;
762771
$this->_proxyProdFactory = $proxyProdFactory;
763772
$this->_uploaderFactory = $uploaderFactory;
773+
$this->filesystem = $filesystem;
764774
$this->_mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
765775
$this->_stockResItemFac = $stockResItemFac;
766776
$this->_localeDate = $localeDate;
@@ -1678,6 +1688,7 @@ protected function _saveProducts()
16781688
foreach ($columnImages as $columnImageKey => $columnImage) {
16791689
if (!isset($uploadedImages[$columnImage])) {
16801690
$uploadedFile = $this->uploadMediaFiles($columnImage, true);
1691+
$uploadedFile = $uploadedFile ?: $this->getSystemFile($columnImage);
16811692
if ($uploadedFile) {
16821693
$uploadedImages[$columnImage] = $uploadedFile;
16831694
} else {
@@ -2013,6 +2024,21 @@ protected function uploadMediaFiles($fileName, $renameFileOff = false)
20132024
}
20142025
}
20152026

2027+
/**
2028+
* Try to find file by it's path.
2029+
*
2030+
* @param string $fileName
2031+
* @return string
2032+
*/
2033+
private function getSystemFile($fileName)
2034+
{
2035+
$filePath = 'catalog' . DIRECTORY_SEPARATOR . 'product' . DIRECTORY_SEPARATOR . $fileName;
2036+
/** @var \Magento\Framework\Filesystem\Directory\ReadInterface $read */
2037+
$read = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
2038+
2039+
return $read->isExist($filePath) && $read->isReadable($filePath) ? $fileName : '';
2040+
}
2041+
20162042
/**
20172043
* Save product media gallery.
20182044
*

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Magento\Framework\App\Filesystem\DirectoryList;
2323
use Magento\Framework\App\ObjectManager;
2424
use Magento\Framework\Registry;
25+
use Magento\Framework\Filesystem;
2526
use Magento\ImportExport\Model\Import;
2627
use Magento\Store\Model\Store;
2728
use Magento\UrlRewrite\Model\UrlRewrite;
@@ -1712,6 +1713,26 @@ public function testValidateData()
17121713
$this->assertTrue($errors->getErrorsCount() == 0);
17131714
}
17141715

1716+
/**
1717+
* Tests situation when images for importing products are already present in filesystem.
1718+
*
1719+
* @magentoDataFixture Magento/CatalogImportExport/Model/Import/_files/import_with_filesystem_images.php
1720+
* @magentoAppIsolation enabled
1721+
*/
1722+
public function testImportWithFilesystemImages()
1723+
{
1724+
/** @var Filesystem $filesystem */
1725+
$filesystem = ObjectManager::getInstance()->get(Filesystem::class);
1726+
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $writeAdapter */
1727+
$writeAdapter = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
1728+
1729+
if (!$writeAdapter->isWritable()) {
1730+
$this->markTestSkipped('Due to unwritable media directory');
1731+
}
1732+
1733+
$this->importDataForMediaTest('import_media_existing_images.csv');
1734+
}
1735+
17151736
/**
17161737
* Test if we can change attribute set for product via import.
17171738
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
sku,store_view_code,attribute_set_code,product_type,categories,product_websites,name,description,short_description,weight,product_online,tax_class_name,visibility,price,special_price,special_price_from_date,special_price_to_date,url_key,meta_title,meta_keywords,meta_description,base_image,base_image_label,small_image,small_image_label,thumbnail_image,thumbnail_image_label,swatch_image,swatch_image_label1,created_at,updated_at,new_from_date,new_to_date,display_product_options_in,map_price,msrp_price,map_enabled,gift_message_available,custom_design,custom_design_from,custom_design_to,custom_layout_update,page_layout,product_options_container,msrp_display_actual_price_type,country_of_manufacture,additional_attributes,qty,out_of_stock_qty,use_config_min_qty,is_qty_decimal,allow_backorders,use_config_backorders,min_cart_qty,use_config_min_sale_qty,max_cart_qty,use_config_max_sale_qty,is_in_stock,notify_on_stock_below,use_config_notify_stock_qty,manage_stock,use_config_manage_stock,use_config_qty_increments,qty_increments,use_config_enable_qty_inc,enable_qty_increments,is_decimal_divided,website_id,related_skus,crosssell_skus,upsell_skus,additional_images,additional_image_labels,hide_from_product_page,custom_options,bundle_price_type,bundle_sku_type,bundle_price_view,bundle_weight_type,bundle_values,associated_skus
2+
simple_new,,Default,simple,,base,New Product,,,,1,Taxable Goods,"Catalog, Search",10,,,,new-product,New Product,New Product,New Product ,magento_image.jpg,Image Label,magento_small_image.jpg,Small Image Label,magento_thumbnail.jpg,Thumbnail Label,magento_image.jpg,Image Label,10/20/15 07:05,10/20/15 07:05,,,Block after Info Column,,,,,,,,,,,,,"has_options=1,quantity_and_stock_status=In Stock,required_options=1",100,0,1,0,0,1,1,1,10000,1,1,1,1,1,0,1,1,0,0,0,1,,,,"magento_additional_image_one.jpg, magento_additional_image_two.jpg","Additional Image Label One,Additional Image Label Two",,,,,,,,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/** @var \Magento\Framework\Filesystem\Directory\Write $mediaDirectory */
8+
$mediaDirectory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
9+
\Magento\Framework\Filesystem::class
10+
)->getDirectoryWrite(
11+
\Magento\Framework\App\Filesystem\DirectoryList::MEDIA
12+
);
13+
14+
$path = 'catalog' . DIRECTORY_SEPARATOR . 'product';
15+
// Is required for using importDataForMediaTest method.
16+
$mediaDirectory->create('import');
17+
$mediaDirectory->create($path);
18+
$dirPath = $mediaDirectory->getAbsolutePath($path);
19+
20+
$items = [
21+
[
22+
'source' => __DIR__ . '/../../../../../Magento/Catalog/_files/magento_image.jpg',
23+
'dest' => $dirPath . '/magento_image.jpg',
24+
],
25+
[
26+
'source' => __DIR__ . '/../../../../../Magento/Catalog/_files/magento_small_image.jpg',
27+
'dest' => $dirPath . '/magento_small_image.jpg',
28+
],
29+
[
30+
'source' => __DIR__ . '/../../../../../Magento/Catalog/_files/magento_thumbnail.jpg',
31+
'dest' => $dirPath . '/magento_thumbnail.jpg',
32+
],
33+
[
34+
'source' => __DIR__ . '/magento_additional_image_one.jpg',
35+
'dest' => $dirPath . '/magento_additional_image_one.jpg',
36+
],
37+
[
38+
'source' => __DIR__ . '/magento_additional_image_two.jpg',
39+
'dest' => $dirPath . '/magento_additional_image_two.jpg',
40+
],
41+
];
42+
43+
foreach ($items as $item) {
44+
copy($item['source'], $item['dest']);
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/** @var \Magento\Framework\Filesystem\Directory\Write $mediaDirectory */
8+
$mediaDirectory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
9+
\Magento\Framework\Filesystem::class
10+
)->getDirectoryWrite(
11+
\Magento\Framework\App\Filesystem\DirectoryList::MEDIA
12+
);
13+
$mediaDirectory->delete('import');
14+
$mediaDirectory->delete('catalog');

0 commit comments

Comments
 (0)