Skip to content

Commit

Permalink
Merge pull request magento#3177 from magento-epam/EPAM-PR-9
Browse files Browse the repository at this point in the history
[epam] MAGETWO-66442: Changes in default scope not effect product images in other scopes
  • Loading branch information
rganin authored Sep 26, 2018
2 parents ab5c07d + 73af931 commit 4b0750c
Show file tree
Hide file tree
Showing 22 changed files with 373 additions and 66 deletions.
12 changes: 9 additions & 3 deletions app/code/Magento/Catalog/Model/Product/Gallery/UpdateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
class UpdateHandler extends \Magento\Catalog\Model\Product\Gallery\CreateHandler
{
/**
* {@inheritdoc}
* @inheritdoc
*
* @since 101.0.0
*/
protected function processDeletedImages($product, array &$images)
Expand All @@ -31,7 +32,7 @@ protected function processDeletedImages($product, array &$images)

foreach ($images as &$image) {
if (!empty($image['removed'])) {
if (!empty($image['value_id']) && !isset($picturesInOtherStores[$image['file']])) {
if (!empty($image['value_id'])) {
if (preg_match('/\.\.(\\\|\/)/', $image['file'])) {
continue;
}
Expand All @@ -52,7 +53,8 @@ protected function processDeletedImages($product, array &$images)
}

/**
* {@inheritdoc}
* @inheritdoc
*
* @since 101.0.0
*/
protected function processNewImage($product, array &$image)
Expand All @@ -79,6 +81,8 @@ protected function processNewImage($product, array &$image)
}

/**
* Retrieve store ids from product.
*
* @param \Magento\Catalog\Model\Product $product
* @return array
* @since 101.0.0
Expand All @@ -97,6 +101,8 @@ protected function extractStoreIds($product)
}

/**
* Remove deleted images.
*
* @param array $files
* @return null
* @since 101.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
<element name="descriptionTextArea" type="textarea" selector="#product_form_description"/>
<element name="shortDescriptionTextArea" type="textarea" selector="#product_form_short_description"/>
<element name="sectionHeaderIfNotShowing" type="button" selector="//div[@data-index='content']//div[contains(@class, '_hide')]"/>
<element name="pageHeader" type="textarea" selector="//*[@class='page-header row']"/>
</section>
</sections>
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->

<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
<test name="AdminRemoveImageAffectsAllScopesTest">
<annotations>
<features value="Catalog"/>
<stories value="MAGETWO-66442: Changes in default scope not effect product images in other scopes"/>
<title value="Effect of product images changes in default scope to other scopes"/>
<description value="Product image should be deleted from all scopes"/>
<severity value="MAJOR"/>
<testCaseId value="MAGETWO-94265"/>
<group value="Catalog"/>
</annotations>
<before>
<!--Create 2 websites (with stores, store views)-->
<actionGroup ref="LoginAsAdmin" stepKey="loginAsAdmin"/>
<createData entity="_defaultCategory" stepKey="category"/>
<createData entity="_defaultProduct" stepKey="product">
<requiredEntity createDataKey="category"/>
</createData>
<actionGroup ref="AdminCreateWebsiteActionGroup" stepKey="createWebsite">
<argument name="newWebsiteName" value="FirstWebSite"/>
<argument name="websiteCode" value="FirstWebSiteCode"/>
</actionGroup>
<actionGroup ref="AdminCreateNewStoreGroupActionGroup" stepKey="createNewStore" after="createWebsite">
<argument name="website" value="FirstWebSite"/>
<argument name="storeGroupName" value="NewStore"/>
<argument name="storeGroupCode" value="Base1"/>
</actionGroup>
<actionGroup ref="AdminCreateStoreViewActionGroup" stepKey="createCustomStoreView" after="createNewStore">
<argument name="StoreGroup" value="staticFirstStoreGroup"/>
<argument name="customStore" value="staticStore"/>
</actionGroup>

<actionGroup ref="AdminCreateWebsiteActionGroup" stepKey="createSecondWebsite" after="createCustomStoreView">
<argument name="newWebsiteName" value="SecondWebSite"/>
<argument name="websiteCode" value="SecondWebSiteCode"/>
</actionGroup>
<actionGroup ref="AdminCreateNewStoreGroupActionGroup" stepKey="createSecondStore" after="createSecondWebsite">
<argument name="website" value="SecondWebSite"/>
<argument name="storeGroupName" value="SecondStore"/>
<argument name="storeGroupCode" value="Base2"/>
</actionGroup>
<actionGroup ref="AdminCreateStoreViewActionGroup" stepKey="createCustomStoreView2" after="createSecondStore">
<argument name="StoreGroup" value="staticStoreGroup"/>
<argument name="customStore" value="staticSecondStore"/>
</actionGroup>
</before>

<after>
<actionGroup ref="ResetWebUrlOptions" stepKey="resetUrlOption"/>
<actionGroup ref="AdminDeleteWebsiteActionGroup" stepKey="deleteWebsite">
<argument name="websiteName" value="FirstWebSite"/>
</actionGroup>

<actionGroup ref="AdminDeleteWebsiteActionGroup" stepKey="deleteSecondWebsite">
<argument name="websiteName" value="SecondWebSite"/>
</actionGroup>
<deleteData createDataKey="category" stepKey="deletePreReqCategory"/>
<deleteData createDataKey="product" stepKey="deleteFirstProduct"/>
<actionGroup ref="logout" stepKey="logout"/>
</after>

<!--Create product-->
<amOnPage url="{{AdminProductIndexPage.url}}" stepKey="navigateToProductIndex"/>
<waitForPageLoad stepKey="waitForProductIndexPage"/>
<actionGroup ref="resetProductGridToDefaultView" stepKey="resetProductGridColumnsInitial"/>

<!--Open created product-->
<click selector="{{AdminProductGridSection.productGridNameProduct($$product.name$$)}}" stepKey="createdProduct"/>
<waitForPageLoad stepKey="waitForOpenedCreatedProduct"/>

<!-- Add image to product -->
<actionGroup ref="addProductImage" stepKey="addFirstImageForProduct">
<argument name="image" value="TestImageNew"/>
</actionGroup>

<!-- Add second image to product -->
<actionGroup ref="addProductImage" stepKey="addSecondImageForProduct">
<argument name="image" value="MagentoLogo"/>
</actionGroup>
<!--"Product in Websites": select both Websites-->
<actionGroup ref="ProductSetWebsite" stepKey="ProductSetWebsite1">
<argument name="website" value="FirstWebSite"/>
</actionGroup>
<actionGroup ref="ProductSetWebsite" stepKey="ProductSetWebsite2">
<argument name="website" value="SecondWebSite"/>
</actionGroup>

<!--Go to "Catalog" -> "Products". Open created product-->
<amOnPage url="{{AdminProductIndexPage.url}}" stepKey="navigateToProductPage"/>
<waitForPageLoad stepKey="waitForProductPageLoaded"/>
<click selector="{{AdminProductGridSection.productGridNameProduct($$product.name$$)}}" stepKey="openCreatedProduct"/>
<waitForPageLoad stepKey="waitForCreatedProductOpened"/>

<!--Delete Image 1-->
<actionGroup ref="removeProductImage" stepKey="removeProductImage"/>

<!--Click "Save" in the upper right corner-->
<actionGroup ref="saveProductForm" stepKey="saveProductFormAfterRemove"/>

<!--Switch to "Store view 1"-->
<actionGroup ref="SwitchToTheNewStoreView" stepKey="selectStoreView">
<argument name="storeViewName" value="Store View"/>
</actionGroup>

<!-- Assert product first image not in admin product form -->
<actionGroup ref="assertProductImageNotInAdminProductPage" stepKey="assertProductImageNotInAdminProductPage">
<argument name="image" value="TestImageNew"/>
</actionGroup>

<!--Switch to "Store view 2"-->
<actionGroup ref="SwitchToTheNewStoreView" stepKey="selectSecondStoreView">
<argument name="storeViewName" value="Second Store View"/>
</actionGroup>

<!-- Verify that Image 1 is deleted from the Second Store View list -->
<actionGroup ref="assertProductImageNotInAdminProductPage" stepKey="assertProductImageNotInSecondStoreViewPage">
<argument name="image" value="TestImageNew"/>
</actionGroup>
</test>
</tests>
43 changes: 42 additions & 1 deletion app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\CatalogImportExport\Model\Import;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Config as CatalogConfig;
use Magento\Catalog\Model\Product\Visibility;
use Magento\CatalogImportExport\Model\Import\Product\MediaGalleryProcessor;
Expand All @@ -15,6 +16,7 @@
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Filesystem;
use Magento\Framework\Intl\DateTimeFactory;
use Magento\Framework\Model\ResourceModel\Db\ObjectRelationProcessor;
Expand Down Expand Up @@ -732,6 +734,11 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
*/
private $dateTimeFactory;

/**
* @var ProductRepositoryInterface
*/
private $productRepository;

/**
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
* @param \Magento\ImportExport\Helper\Data $importExportData
Expand Down Expand Up @@ -776,7 +783,9 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
* @param MediaGalleryProcessor $mediaProcessor
* @param StockItemImporterInterface|null $stockItemImporter
* @param DateTimeFactory $dateTimeFactory
* @param ProductRepositoryInterface|null $productRepository
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function __construct(
\Magento\Framework\Json\Helper\Data $jsonHelper,
Expand Down Expand Up @@ -821,7 +830,8 @@ public function __construct(
ImageTypeProcessor $imageTypeProcessor = null,
MediaGalleryProcessor $mediaProcessor = null,
StockItemImporterInterface $stockItemImporter = null,
DateTimeFactory $dateTimeFactory = null
DateTimeFactory $dateTimeFactory = null,
ProductRepositoryInterface $productRepository = null
) {
$this->_eventManager = $eventManager;
$this->stockRegistry = $stockRegistry;
Expand Down Expand Up @@ -875,6 +885,8 @@ public function __construct(
->initImagesArrayKeys();
$this->validator->init($this);
$this->dateTimeFactory = $dateTimeFactory ?? ObjectManager::getInstance()->get(DateTimeFactory::class);
$this->productRepository = $productRepository ?? ObjectManager::getInstance()
->get(ProductRepositoryInterface::class);
}

/**
Expand Down Expand Up @@ -1698,6 +1710,14 @@ protected function _saveProducts()
$websiteId = $this->storeResolver->getWebsiteCodeToId($websiteCode);
$this->websitesCache[$rowSku][$websiteId] = true;
}
} else {
$product = $this->retrieveProductBySku($rowSku);
if ($product) {
$websiteIds = $product->getWebsiteIds();
foreach ($websiteIds as $websiteId) {
$this->websitesCache[$rowSku][$websiteId] = true;
}
}
}

// 3. Categories phase
Expand Down Expand Up @@ -1996,6 +2016,11 @@ protected function processRowCategories($rowData)
. ' ' . $error['exception']->getMessage()
);
}
} else {
$product = $this->retrieveProductBySku($rowData['sku']);
if ($product) {
$categoryIds = $product->getCategoryIds();
}
}
return $categoryIds;
}
Expand Down Expand Up @@ -2998,4 +3023,20 @@ private function formatStockDataForRow(array $rowData): array

return $row;
}

/**
* Retrieve product by sku.
*
* @param string $sku
* @return \Magento\Catalog\Api\Data\ProductInterface|null
*/
private function retrieveProductBySku($sku)
{
try {
$product = $this->productRepository->get($sku);
} catch (NoSuchEntityException $e) {
return null;
}
return $product;
}
}
Loading

0 comments on commit 4b0750c

Please sign in to comment.