diff --git a/app/code/Magento/Swatches/Model/Plugin/EavAttribute.php b/app/code/Magento/Swatches/Model/Plugin/EavAttribute.php index 76bf5782d3922..588ffb6e8e835 100644 --- a/app/code/Magento/Swatches/Model/Plugin/EavAttribute.php +++ b/app/code/Magento/Swatches/Model/Plugin/EavAttribute.php @@ -9,6 +9,7 @@ use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\InputException; use Magento\Framework\Serialize\Serializer\Json; +use Magento\Store\Model\StoreManagerInterface; use Magento\Swatches\Model\Swatch; use Magento\Swatches\Model\ResourceModel\Swatch as SwatchResource; @@ -65,25 +66,33 @@ class EavAttribute */ private $serializer; + /** + * @var StoreManagerInterface + */ + private $storeManager; + /** * @param \Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory $collectionFactory * @param \Magento\Swatches\Model\SwatchFactory $swatchFactory * @param \Magento\Swatches\Helper\Data $swatchHelper * @param Json|null $serializer * @param SwatchResource|null $swatchResource + * @param StoreManagerInterface|null $storeManager */ public function __construct( \Magento\Swatches\Model\ResourceModel\Swatch\CollectionFactory $collectionFactory, \Magento\Swatches\Model\SwatchFactory $swatchFactory, \Magento\Swatches\Helper\Data $swatchHelper, Json $serializer = null, - SwatchResource $swatchResource = null + SwatchResource $swatchResource = null, + StoreManagerInterface $storeManager = null ) { $this->swatchCollectionFactory = $collectionFactory; $this->swatchFactory = $swatchFactory; $this->swatchHelper = $swatchHelper; $this->serializer = $serializer ?: ObjectManager::getInstance()->create(Json::class); $this->swatchResource = $swatchResource ?: ObjectManager::getInstance()->create(SwatchResource::class); + $this->storeManager = $storeManager ?: ObjectManager::getInstance()->create(StoreManagerInterface::class); } /** @@ -256,25 +265,41 @@ protected function saveSwatchParams(Attribute $attribute) * Save Visual Swatch data * * @param Attribute $attribute + * * @return void */ protected function processVisualSwatch(Attribute $attribute) { $swatchArray = $attribute->getData('swatch/value'); + if (isset($swatchArray) && is_array($swatchArray)) { - foreach ($swatchArray as $optionId => $value) { + foreach ($swatchArray as $key => $value) { + $optionId = $key; + $storeValues = [ + self::DEFAULT_STORE_ID => $value, + $this->storeManager->getStore()->getId() => '', + ]; + $optionId = $this->getAttributeOptionId($optionId); $isOptionForDelete = $this->isOptionForDelete($attribute, $optionId); + if ($optionId === null || $isOptionForDelete) { //option was deleted by button with basket continue; } - $swatch = $this->loadSwatchIfExists($optionId, self::DEFAULT_STORE_ID); - $swatchType = $this->determineSwatchType($value); + $defaultSwatchValue = reset($storeValues); + foreach ($storeValues as $storeId => $storeValue) { + if (!$storeValue) { + $storeValue = $defaultSwatchValue; + } + + $swatch = $this->loadSwatchIfExists($optionId, $storeId); + $swatchType = $this->determineSwatchType($storeValue); - $this->saveSwatchData($swatch, $optionId, self::DEFAULT_STORE_ID, $swatchType, $value); - $this->isSwatchExists = null; + $this->saveSwatchData($swatch, $optionId, $storeId, $swatchType, $storeValue); + $this->isSwatchExists = null; + } } } } diff --git a/app/code/Magento/Swatches/Test/Unit/Model/Plugin/EavAttributeTest.php b/app/code/Magento/Swatches/Test/Unit/Model/Plugin/EavAttributeTest.php index 317ea77107222..65e3027c42b7e 100644 --- a/app/code/Magento/Swatches/Test/Unit/Model/Plugin/EavAttributeTest.php +++ b/app/code/Magento/Swatches/Test/Unit/Model/Plugin/EavAttributeTest.php @@ -9,6 +9,10 @@ use Magento\Swatches\Model\Plugin\EavAttribute; use Magento\Swatches\Model\Swatch; +/** + * Class EavAttributeTest + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class EavAttributeTest extends \PHPUnit\Framework\TestCase { const ATTRIBUTE_ID = 123; @@ -45,6 +49,12 @@ class EavAttributeTest extends \PHPUnit\Framework\TestCase /** @var \Magento\Swatches\Model\ResourceModel\Swatch\Collection|\PHPUnit_Framework_MockObject_MockObject */ private $collection; + /** @var \Magento\Store\Model\StoreManager|\PHPUnit_Framework_MockObject_MockObject */ + private $storeManager; + + /** @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject */ + private $store; + /** @var array */ private $optionIds = []; @@ -61,6 +71,8 @@ protected function setUp() $this->swatchHelper = $this->createMock(\Magento\Swatches\Helper\Data::class); $this->swatch = $this->createMock(\Magento\Swatches\Model\Swatch::class); $this->resource = $this->createMock(\Magento\Swatches\Model\ResourceModel\Swatch::class); + $this->storeManager = $this->createPartialMock(\Magento\Store\Model\StoreManager::class, ['getStore']); + $this->store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getId']); $this->collection = $this->createMock(\Magento\Swatches\Model\ResourceModel\Swatch\Collection::class); $this->collectionFactory = $this->createPartialMock( @@ -92,6 +104,7 @@ protected function setUp() 'swatchFactory' => $this->swatchFactory, 'swatchHelper' => $this->swatchHelper, 'serializer' => $serializer, + 'storeManager' => $this->storeManager ] ); @@ -337,10 +350,10 @@ public function testAfterAfterSaveVisualSwatch($swatchType, $swatchValue) $this->swatch->expects($this->once())->method('getResource') ->willReturn($this->resource); - $this->swatch->expects($this->once())->method('getId') + $this->swatch->expects($this->exactly(2))->method('getId') ->willReturn(EavAttribute::DEFAULT_STORE_ID); - $this->swatch->expects($this->once())->method('save'); - $this->swatch->expects($this->exactly(4))->method('setData') + $this->swatch->expects($this->exactly(2))->method('save'); + $this->swatch->expects($this->exactly(8))->method('setData') ->withConsecutive( ['option_id', self::OPTION_ID], ['store_id', EavAttribute::DEFAULT_STORE_ID], @@ -348,15 +361,15 @@ public function testAfterAfterSaveVisualSwatch($swatchType, $swatchValue) ['value', $swatchValue] ); - $this->collection->expects($this->exactly(2))->method('addFieldToFilter') + $this->collection->expects($this->exactly(4))->method('addFieldToFilter') ->withConsecutive( ['option_id', self::OPTION_ID], ['store_id', EavAttribute::DEFAULT_STORE_ID] )->willReturnSelf(); - $this->collection->expects($this->once())->method('getFirstItem') + $this->collection->expects($this->exactly(2))->method('getFirstItem') ->willReturn($this->swatch); - $this->collectionFactory->expects($this->once())->method('create') + $this->collectionFactory->expects($this->exactly(2))->method('create') ->willReturn($this->collection); $this->attribute->expects($this->at(0))->method('getData') @@ -384,6 +397,8 @@ public function testAfterAfterSaveVisualSwatch($swatchType, $swatchValue) ->with($this->attribute) ->willReturn(true); $this->swatchHelper->expects($this->never())->method('isTextSwatch'); + $this->storeManager->expects($this->once())->method('getStore')->willReturn($this->store); + $this->store->expects($this->once())->method('getId')->willReturn(1); $this->eavAttribute->afterAfterSave($this->attribute); } @@ -545,6 +560,8 @@ public function testAfterAfterSaveVisualSwatchIsDelete() $this->swatchHelper->expects($this->once())->method('isVisualSwatch') ->with($this->attribute) ->willReturn(true); + $this->storeManager->expects($this->once())->method('getStore')->willReturn($this->store); + $this->store->expects($this->once())->method('getId')->willReturn(1); $this->swatchHelper->expects($this->never())->method('isTextSwatch'); $this->eavAttribute->afterAfterSave($this->attribute);