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

Commit 4b0750c

Browse files
authored
Merge pull request #3177 from magento-epam/EPAM-PR-9
[epam] MAGETWO-66442: Changes in default scope not effect product images in other scopes
2 parents ab5c07d + 73af931 commit 4b0750c

File tree

22 files changed

+373
-66
lines changed

22 files changed

+373
-66
lines changed

app/code/Magento/Catalog/Model/Product/Gallery/UpdateHandler.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
class UpdateHandler extends \Magento\Catalog\Model\Product\Gallery\CreateHandler
1717
{
1818
/**
19-
* {@inheritdoc}
19+
* @inheritdoc
20+
*
2021
* @since 101.0.0
2122
*/
2223
protected function processDeletedImages($product, array &$images)
@@ -31,7 +32,7 @@ protected function processDeletedImages($product, array &$images)
3132

3233
foreach ($images as &$image) {
3334
if (!empty($image['removed'])) {
34-
if (!empty($image['value_id']) && !isset($picturesInOtherStores[$image['file']])) {
35+
if (!empty($image['value_id'])) {
3536
if (preg_match('/\.\.(\\\|\/)/', $image['file'])) {
3637
continue;
3738
}
@@ -52,7 +53,8 @@ protected function processDeletedImages($product, array &$images)
5253
}
5354

5455
/**
55-
* {@inheritdoc}
56+
* @inheritdoc
57+
*
5658
* @since 101.0.0
5759
*/
5860
protected function processNewImage($product, array &$image)
@@ -79,6 +81,8 @@ protected function processNewImage($product, array &$image)
7981
}
8082

8183
/**
84+
* Retrieve store ids from product.
85+
*
8286
* @param \Magento\Catalog\Model\Product $product
8387
* @return array
8488
* @since 101.0.0
@@ -97,6 +101,8 @@ protected function extractStoreIds($product)
97101
}
98102

99103
/**
104+
* Remove deleted images.
105+
*
100106
* @param array $files
101107
* @return null
102108
* @since 101.0.0

app/code/Magento/Catalog/Test/Mftf/Section/AdminProductContentSection.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
<element name="descriptionTextArea" type="textarea" selector="#product_form_description"/>
1414
<element name="shortDescriptionTextArea" type="textarea" selector="#product_form_short_description"/>
1515
<element name="sectionHeaderIfNotShowing" type="button" selector="//div[@data-index='content']//div[contains(@class, '_hide')]"/>
16+
<element name="pageHeader" type="textarea" selector="//*[@class='page-header row']"/>
1617
</section>
1718
</sections>
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
11+
<test name="AdminRemoveImageAffectsAllScopesTest">
12+
<annotations>
13+
<features value="Catalog"/>
14+
<stories value="MAGETWO-66442: Changes in default scope not effect product images in other scopes"/>
15+
<title value="Effect of product images changes in default scope to other scopes"/>
16+
<description value="Product image should be deleted from all scopes"/>
17+
<severity value="MAJOR"/>
18+
<testCaseId value="MAGETWO-94265"/>
19+
<group value="Catalog"/>
20+
</annotations>
21+
<before>
22+
<!--Create 2 websites (with stores, store views)-->
23+
<actionGroup ref="LoginAsAdmin" stepKey="loginAsAdmin"/>
24+
<createData entity="_defaultCategory" stepKey="category"/>
25+
<createData entity="_defaultProduct" stepKey="product">
26+
<requiredEntity createDataKey="category"/>
27+
</createData>
28+
<actionGroup ref="AdminCreateWebsiteActionGroup" stepKey="createWebsite">
29+
<argument name="newWebsiteName" value="FirstWebSite"/>
30+
<argument name="websiteCode" value="FirstWebSiteCode"/>
31+
</actionGroup>
32+
<actionGroup ref="AdminCreateNewStoreGroupActionGroup" stepKey="createNewStore" after="createWebsite">
33+
<argument name="website" value="FirstWebSite"/>
34+
<argument name="storeGroupName" value="NewStore"/>
35+
<argument name="storeGroupCode" value="Base1"/>
36+
</actionGroup>
37+
<actionGroup ref="AdminCreateStoreViewActionGroup" stepKey="createCustomStoreView" after="createNewStore">
38+
<argument name="StoreGroup" value="staticFirstStoreGroup"/>
39+
<argument name="customStore" value="staticStore"/>
40+
</actionGroup>
41+
42+
<actionGroup ref="AdminCreateWebsiteActionGroup" stepKey="createSecondWebsite" after="createCustomStoreView">
43+
<argument name="newWebsiteName" value="SecondWebSite"/>
44+
<argument name="websiteCode" value="SecondWebSiteCode"/>
45+
</actionGroup>
46+
<actionGroup ref="AdminCreateNewStoreGroupActionGroup" stepKey="createSecondStore" after="createSecondWebsite">
47+
<argument name="website" value="SecondWebSite"/>
48+
<argument name="storeGroupName" value="SecondStore"/>
49+
<argument name="storeGroupCode" value="Base2"/>
50+
</actionGroup>
51+
<actionGroup ref="AdminCreateStoreViewActionGroup" stepKey="createCustomStoreView2" after="createSecondStore">
52+
<argument name="StoreGroup" value="staticStoreGroup"/>
53+
<argument name="customStore" value="staticSecondStore"/>
54+
</actionGroup>
55+
</before>
56+
57+
<after>
58+
<actionGroup ref="ResetWebUrlOptions" stepKey="resetUrlOption"/>
59+
<actionGroup ref="AdminDeleteWebsiteActionGroup" stepKey="deleteWebsite">
60+
<argument name="websiteName" value="FirstWebSite"/>
61+
</actionGroup>
62+
63+
<actionGroup ref="AdminDeleteWebsiteActionGroup" stepKey="deleteSecondWebsite">
64+
<argument name="websiteName" value="SecondWebSite"/>
65+
</actionGroup>
66+
<deleteData createDataKey="category" stepKey="deletePreReqCategory"/>
67+
<deleteData createDataKey="product" stepKey="deleteFirstProduct"/>
68+
<actionGroup ref="logout" stepKey="logout"/>
69+
</after>
70+
71+
<!--Create product-->
72+
<amOnPage url="{{AdminProductIndexPage.url}}" stepKey="navigateToProductIndex"/>
73+
<waitForPageLoad stepKey="waitForProductIndexPage"/>
74+
<actionGroup ref="resetProductGridToDefaultView" stepKey="resetProductGridColumnsInitial"/>
75+
76+
<!--Open created product-->
77+
<click selector="{{AdminProductGridSection.productGridNameProduct($$product.name$$)}}" stepKey="createdProduct"/>
78+
<waitForPageLoad stepKey="waitForOpenedCreatedProduct"/>
79+
80+
<!-- Add image to product -->
81+
<actionGroup ref="addProductImage" stepKey="addFirstImageForProduct">
82+
<argument name="image" value="TestImageNew"/>
83+
</actionGroup>
84+
85+
<!-- Add second image to product -->
86+
<actionGroup ref="addProductImage" stepKey="addSecondImageForProduct">
87+
<argument name="image" value="MagentoLogo"/>
88+
</actionGroup>
89+
<!--"Product in Websites": select both Websites-->
90+
<actionGroup ref="ProductSetWebsite" stepKey="ProductSetWebsite1">
91+
<argument name="website" value="FirstWebSite"/>
92+
</actionGroup>
93+
<actionGroup ref="ProductSetWebsite" stepKey="ProductSetWebsite2">
94+
<argument name="website" value="SecondWebSite"/>
95+
</actionGroup>
96+
97+
<!--Go to "Catalog" -> "Products". Open created product-->
98+
<amOnPage url="{{AdminProductIndexPage.url}}" stepKey="navigateToProductPage"/>
99+
<waitForPageLoad stepKey="waitForProductPageLoaded"/>
100+
<click selector="{{AdminProductGridSection.productGridNameProduct($$product.name$$)}}" stepKey="openCreatedProduct"/>
101+
<waitForPageLoad stepKey="waitForCreatedProductOpened"/>
102+
103+
<!--Delete Image 1-->
104+
<actionGroup ref="removeProductImage" stepKey="removeProductImage"/>
105+
106+
<!--Click "Save" in the upper right corner-->
107+
<actionGroup ref="saveProductForm" stepKey="saveProductFormAfterRemove"/>
108+
109+
<!--Switch to "Store view 1"-->
110+
<actionGroup ref="SwitchToTheNewStoreView" stepKey="selectStoreView">
111+
<argument name="storeViewName" value="Store View"/>
112+
</actionGroup>
113+
114+
<!-- Assert product first image not in admin product form -->
115+
<actionGroup ref="assertProductImageNotInAdminProductPage" stepKey="assertProductImageNotInAdminProductPage">
116+
<argument name="image" value="TestImageNew"/>
117+
</actionGroup>
118+
119+
<!--Switch to "Store view 2"-->
120+
<actionGroup ref="SwitchToTheNewStoreView" stepKey="selectSecondStoreView">
121+
<argument name="storeViewName" value="Second Store View"/>
122+
</actionGroup>
123+
124+
<!-- Verify that Image 1 is deleted from the Second Store View list -->
125+
<actionGroup ref="assertProductImageNotInAdminProductPage" stepKey="assertProductImageNotInSecondStoreViewPage">
126+
<argument name="image" value="TestImageNew"/>
127+
</actionGroup>
128+
</test>
129+
</tests>

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\CatalogImportExport\Model\Import;
77

8+
use Magento\Catalog\Api\ProductRepositoryInterface;
89
use Magento\Catalog\Model\Config as CatalogConfig;
910
use Magento\Catalog\Model\Product\Visibility;
1011
use Magento\CatalogImportExport\Model\Import\Product\MediaGalleryProcessor;
@@ -15,6 +16,7 @@
1516
use Magento\Framework\App\Filesystem\DirectoryList;
1617
use Magento\Framework\App\ObjectManager;
1718
use Magento\Framework\Exception\LocalizedException;
19+
use Magento\Framework\Exception\NoSuchEntityException;
1820
use Magento\Framework\Filesystem;
1921
use Magento\Framework\Intl\DateTimeFactory;
2022
use Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor;
@@ -732,6 +734,11 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
732734
*/
733735
private $dateTimeFactory;
734736

737+
/**
738+
* @var ProductRepositoryInterface
739+
*/
740+
private $productRepository;
741+
735742
/**
736743
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
737744
* @param \Magento\ImportExport\Helper\Data $importExportData
@@ -776,7 +783,9 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
776783
* @param MediaGalleryProcessor $mediaProcessor
777784
* @param StockItemImporterInterface|null $stockItemImporter
778785
* @param DateTimeFactory $dateTimeFactory
786+
* @param ProductRepositoryInterface|null $productRepository
779787
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
788+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
780789
*/
781790
public function __construct(
782791
\Magento\Framework\Json\Helper\Data $jsonHelper,
@@ -821,7 +830,8 @@ public function __construct(
821830
ImageTypeProcessor $imageTypeProcessor = null,
822831
MediaGalleryProcessor $mediaProcessor = null,
823832
StockItemImporterInterface $stockItemImporter = null,
824-
DateTimeFactory $dateTimeFactory = null
833+
DateTimeFactory $dateTimeFactory = null,
834+
ProductRepositoryInterface $productRepository = null
825835
) {
826836
$this->_eventManager = $eventManager;
827837
$this->stockRegistry = $stockRegistry;
@@ -875,6 +885,8 @@ public function __construct(
875885
->initImagesArrayKeys();
876886
$this->validator->init($this);
877887
$this->dateTimeFactory = $dateTimeFactory ?? ObjectManager::getInstance()->get(DateTimeFactory::class);
888+
$this->productRepository = $productRepository ?? ObjectManager::getInstance()
889+
->get(ProductRepositoryInterface::class);
878890
}
879891

880892
/**
@@ -1698,6 +1710,14 @@ protected function _saveProducts()
16981710
$websiteId = $this->storeResolver->getWebsiteCodeToId($websiteCode);
16991711
$this->websitesCache[$rowSku][$websiteId] = true;
17001712
}
1713+
} else {
1714+
$product = $this->retrieveProductBySku($rowSku);
1715+
if ($product) {
1716+
$websiteIds = $product->getWebsiteIds();
1717+
foreach ($websiteIds as $websiteId) {
1718+
$this->websitesCache[$rowSku][$websiteId] = true;
1719+
}
1720+
}
17011721
}
17021722

17031723
// 3. Categories phase
@@ -1996,6 +2016,11 @@ protected function processRowCategories($rowData)
19962016
. ' ' . $error['exception']->getMessage()
19972017
);
19982018
}
2019+
} else {
2020+
$product = $this->retrieveProductBySku($rowData['sku']);
2021+
if ($product) {
2022+
$categoryIds = $product->getCategoryIds();
2023+
}
19992024
}
20002025
return $categoryIds;
20012026
}
@@ -2998,4 +3023,20 @@ private function formatStockDataForRow(array $rowData): array
29983023

29993024
return $row;
30003025
}
3026+
3027+
/**
3028+
* Retrieve product by sku.
3029+
*
3030+
* @param string $sku
3031+
* @return \Magento\Catalog\Api\Data\ProductInterface|null
3032+
*/
3033+
private function retrieveProductBySku($sku)
3034+
{
3035+
try {
3036+
$product = $this->productRepository->get($sku);
3037+
} catch (NoSuchEntityException $e) {
3038+
return null;
3039+
}
3040+
return $product;
3041+
}
30013042
}

0 commit comments

Comments
 (0)