From d8c3fcdd98c6555a4d42f209819cad3d6a466e30 Mon Sep 17 00:00:00 2001
From: Leonid Poluyanov <lpoluyanov@magento.com>
Date: Fri, 15 Dec 2017 19:37:44 +0200
Subject: [PATCH] MAGETWO-84448: Product Import does not allow store-specific
 Custom Option labels

---
 .../Model/Export/RowCustomizer.php            | 22 ++++++------
 .../Model/Import/Product/Type/Bundle.php      | 34 ++++++++-----------
 .../Magento/Bundle/_files/product.php         |  2 --
 .../Model/Export/RowCustomizerTest.php        |  1 +
 .../Model/Import/Product/Type/BundleTest.php  |  2 +-
 .../Model/Import/ProductTest.php              |  2 +-
 6 files changed, 28 insertions(+), 35 deletions(-)

diff --git a/app/code/Magento/BundleImportExport/Model/Export/RowCustomizer.php b/app/code/Magento/BundleImportExport/Model/Export/RowCustomizer.php
index 86126773f24..dbe583d716e 100644
--- a/app/code/Magento/BundleImportExport/Model/Export/RowCustomizer.php
+++ b/app/code/Magento/BundleImportExport/Model/Export/RowCustomizer.php
@@ -12,7 +12,7 @@ use Magento\Bundle\Model\ResourceModel\Selection\Collection as SelectionCollecti
 use Magento\ImportExport\Model\Import as ImportModel;
 use \Magento\Catalog\Model\Product\Type\AbstractType;
 use \Magento\Framework\App\ObjectManager;
-use \Magento\Framework\App\ScopeResolverInterface;
+use \Magento\Store\Model\StoreManagerInterface;
 
 /**
  * Class RowCustomizer
@@ -122,9 +122,9 @@ class RowCustomizer implements RowCustomizerInterface
     private $optionCollectionCacheKey = '_cache_instance_options_collection';
 
     /**
-     * @var ScopeResolverInterface
+     * @var StoreManagerInterface
      */
-    private $scopeResolver;
+    private $storeManager;
 
     /**
      * Retrieve list of bundle specific columns
@@ -422,13 +422,13 @@ class RowCustomizer implements RowCustomizerInterface
      */
     private function getBundleOptionTitles($product)
     {
-        $storeIds = $product->getStoreIds();
         $optionsCollection = $this->getProductOptionsCollection($product);
         $optionsTitles = [];
         /** @var \Magento\Bundle\Model\Option $option */
         foreach ($optionsCollection->getItems() as $option) {
             $optionsTitles[$option->getId()]['name'] = $option->getTitle();
         }
+        $storeIds = $product->getStoreIds();
         if (array_count_values($storeIds) > 1) {
             foreach ($storeIds as $storeId) {
                 $optionsCollection = $this->getProductOptionsCollection($product, $storeId);
@@ -475,22 +475,22 @@ class RowCustomizer implements RowCustomizerInterface
     private function getStoreCodeById($storeId)
     {
         if (!isset($this->storeIdToCode[$storeId])) {
-            $this->storeIdToCode[$storeId] = $this->getScopeResolver()->getScope($storeId)->getCode();
+            $this->storeIdToCode[$storeId] = $this->getStoreManager()->getStore($storeId);
         }
         return $this->storeIdToCode[$storeId];
     }
 
     /**
-     * Initialize ScopeResolver if it was not and return it.
+     * Initialize StoreManagerInterface if it was not and return it.
      *
-     * @return ScopeResolverInterface
+     * @return StoreManagerInterface
      * @throws \RuntimeException
      */
-    private function getScopeResolver()
+    private function getStoreManager()
     {
-        if (!$this->scopeResolver) {
-            $this->scopeResolver = ObjectManager::getInstance()->get(ScopeResolverInterface::class);
+        if (!$this->storeManager) {
+            $this->storeManager = ObjectManager::getInstance()->get(StoreManagerInterface::class);
         }
-        return $this->scopeResolver;
+        return $this->storeManager;
     }
 }
diff --git a/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php b/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php
index 2617cce80b6..c0e17433e43 100644
--- a/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php
+++ b/app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php
@@ -12,7 +12,7 @@ use \Magento\Framework\App\ObjectManager;
 use \Magento\Bundle\Model\Product\Price as BundlePrice;
 use \Magento\Catalog\Model\Product\Type\AbstractType;
 use \Magento\CatalogImportExport\Model\Import\Product;
-use \Magento\Framework\App\ScopeResolverInterface;
+use \Magento\Store\Model\StoreManagerInterface;
 
 /**
  * Class Bundle
@@ -138,9 +138,9 @@ class Bundle extends \Magento\CatalogImportExport\Model\Import\Product\Type\Abst
     private $relationsDataSaver;
 
     /**
-     * @var ScopeResolverInterface
+     * @var StoreManagerInterface
      */
-    private $scopeResolver;
+    private $storeManager;
 
     /**
      * @var array
@@ -154,7 +154,7 @@ class Bundle extends \Magento\CatalogImportExport\Model\Import\Product\Type\Abst
      * @param array $params
      * @param \Magento\Framework\EntityManager\MetadataPool|null $metadataPool
      * @param Bundle\RelationsDataSaver|null $relationsDataSaver
-     * @param ScopeResolverInterface $scopeResolver
+     * @param StoreManagerInterface $storeManager
      * @throws \RuntimeException
      */
     public function __construct(
@@ -164,13 +164,13 @@ class Bundle extends \Magento\CatalogImportExport\Model\Import\Product\Type\Abst
         array $params,
         \Magento\Framework\EntityManager\MetadataPool $metadataPool = null,
         Bundle\RelationsDataSaver $relationsDataSaver = null,
-        ScopeResolverInterface $scopeResolver = null
+        StoreManagerInterface $storeManager = null
     ) {
         parent::__construct($attrSetColFac, $prodAttrColFac, $resource, $params, $metadataPool);
         $this->relationsDataSaver = $relationsDataSaver
             ?: ObjectManager::getInstance()->get(Bundle\RelationsDataSaver::class);
-        $this->scopeResolver = $scopeResolver
-            ?: ObjectManager::getInstance()->get(ScopeResolverInterface::class);
+        $this->storeManager = $storeManager
+            ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
     }
 
     /**
@@ -277,8 +277,6 @@ class Bundle extends \Magento\CatalogImportExport\Model\Import\Product\Type\Abst
      * @param int $optionId
      * @param int $storeId
      * @return array
-     *
-     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
      */
     protected function populateOptionValueTemplate($option, $optionId, $storeId = 0)
     {
@@ -289,7 +287,8 @@ class Bundle extends \Magento\CatalogImportExport\Model\Import\Product\Type\Abst
             $optionNames = preg_grep($pattern, $keys);
             foreach ($optionNames as $optionName) {
                 preg_match($pattern, $optionName, $storeCodes);
-                $storeId = $this->getStoreIdByCode(array_pop($storeCodes));
+                $storeCode = array_pop($storeCodes);
+                $storeId = !$storeCode ? $storeId : $this->getStoreIdByCode($storeCode);
                 $optionValues[] = [
                     'option_id' => $optionId,
                     'parent_product_id' => $option['parent_id'],
@@ -725,21 +724,16 @@ class Bundle extends \Magento\CatalogImportExport\Model\Import\Product\Type\Abst
 
     /**
      * Get store id by store code.
-     * Default store code will be returned if no parameter or parameter with empty string provided
      *
      * @param string|null $storeCode
      * @return int
      */
-    private function getStoreIdByCode($storeCode = null)
+    private function getStoreIdByCode($storeCode)
     {
-        if (!$storeCode) {
-            return \Magento\Store\Model\Store::DEFAULT_STORE_ID;
-        } else {
-            if (!isset($this->storeIdToCode[$storeCode])) {
-                /** @var $store \Magento\Store\Model\Store */
-                foreach ($this->scopeResolver->getScopes() as $store) {
-                    $this->storeCodeToId[$store->getCode()] = $store->getId();
-                }
+        if (!isset($this->storeIdToCode[$storeCode])) {
+            /** @var $store \Magento\Store\Model\Store */
+            foreach ($this->storeManager->getStores() as $store) {
+                $this->storeCodeToId[$store->getCode()] = $store->getId();
             }
         }
         return $this->storeCodeToId[$storeCode];
diff --git a/dev/tests/integration/testsuite/Magento/Bundle/_files/product.php b/dev/tests/integration/testsuite/Magento/Bundle/_files/product.php
index d5b0bfaf1ea..ce4fca62072 100644
--- a/dev/tests/integration/testsuite/Magento/Bundle/_files/product.php
+++ b/dev/tests/integration/testsuite/Magento/Bundle/_files/product.php
@@ -9,8 +9,6 @@
  * bundled items should not contain products with required custom options.
  * However, if to create such a bundle product, it will be always out of stock.
  */
-/** Create fixture store */
-require __DIR__ . '/../../../Magento//Store/_files/second_store.php';
 /** Create simple product */
 require __DIR__ . '/../../../Magento/Catalog/_files/products.php';
 
diff --git a/dev/tests/integration/testsuite/Magento/BundleImportExport/Model/Export/RowCustomizerTest.php b/dev/tests/integration/testsuite/Magento/BundleImportExport/Model/Export/RowCustomizerTest.php
index 8962f1266d5..f130b85f35a 100644
--- a/dev/tests/integration/testsuite/Magento/BundleImportExport/Model/Export/RowCustomizerTest.php
+++ b/dev/tests/integration/testsuite/Magento/BundleImportExport/Model/Export/RowCustomizerTest.php
@@ -58,6 +58,7 @@ class RowCustomizerTest extends \PHPUnit\Framework\TestCase
     }
 
     /**
+     * @magentoDataFixture Magento/Store/_files/second_store.php
      * @magentoDataFixture Magento/Bundle/_files/product.php
      */
     public function testPrepareDataWithDifferentStoreValues()
diff --git a/dev/tests/integration/testsuite/Magento/BundleImportExport/Model/Import/Product/Type/BundleTest.php b/dev/tests/integration/testsuite/Magento/BundleImportExport/Model/Import/Product/Type/BundleTest.php
index 06c63c32806..aada927559d 100644
--- a/dev/tests/integration/testsuite/Magento/BundleImportExport/Model/Import/Product/Type/BundleTest.php
+++ b/dev/tests/integration/testsuite/Magento/BundleImportExport/Model/Import/Product/Type/BundleTest.php
@@ -109,7 +109,7 @@ class BundleTest extends \PHPUnit\Framework\TestCase
     }
 
     /**
-     * @magentoDataFixture Magento//Store/_files/second_store.php
+     * @magentoDataFixture Magento/Store/_files/second_store.php
      * @magentoAppArea adminhtml
      */
     public function testBundleImportWithMultipleStoreViews()
diff --git a/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php b/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php
index ac41274f263..e819fc5f632 100644
--- a/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php
+++ b/dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest.php
@@ -340,7 +340,7 @@ class ProductTest extends \Magento\TestFramework\Indexer\TestCase
     /**
      * Tests adding of custom options with multiple store views
      *
-     * @magentoDataFixture Magento//Store/_files/second_store.php
+     * @magentoDataFixture Magento/Store/_files/second_store.php
      * @magentoDataFixture Magento/Catalog/_files/product_simple.php
      * @magentoAppIsolation enabled
      */
-- 
GitLab