From d1b7097bc38e87669607cfac094d312d76ce7ab3 Mon Sep 17 00:00:00 2001 From: Iaroslav Gyryn Date: Thu, 7 Feb 2019 14:00:33 +0200 Subject: [PATCH] magento/magento2#20481 REST products update category_ids cannot be removed Code review, updated unit tests --- app/code/Magento/Catalog/Model/ProductRepository.php | 4 ++-- .../Test/Unit/Model/ProductRepositoryTest.php | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Catalog/Model/ProductRepository.php b/app/code/Magento/Catalog/Model/ProductRepository.php index 518a17ff3f101..d78d1ca2f216f 100644 --- a/app/code/Magento/Catalog/Model/ProductRepository.php +++ b/app/code/Magento/Catalog/Model/ProductRepository.php @@ -193,7 +193,7 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer * @param int $cacheLimit [optional] * @param ReadExtensions|null $readExtensions - * @param Magento\Catalog\Api\CategoryLinkManagementInterface|null $categoryLinkManagement + * @param Magento\Catalog\Api\CategoryLinkManagementInterface|null $linkManagement * @SuppressWarnings(PHPMD.ExcessiveParameterList) * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ @@ -638,7 +638,7 @@ public function save(ProductInterface $product, $saveOptions = false) } $this->saveProduct($product); - if ($assignToCategories === true) { + if ($assignToCategories === true && $product->getCategoryIds()) { $this->linkManagement->assignProductToCategories( $product->getSku(), $product->getCategoryIds() diff --git a/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php b/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php index c729a0c58e1ec..6d3b4713b830d 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php @@ -200,7 +200,8 @@ protected function setUp() 'setData', 'getStoreId', 'getMediaGalleryEntries', - 'getExtensionAttributes' + 'getExtensionAttributes', + 'getCategoryIds' ] ); @@ -220,7 +221,8 @@ protected function setUp() 'validate', 'save', 'getMediaGalleryEntries', - 'getExtensionAttributes' + 'getExtensionAttributes', + 'getCategoryIds' ] ); $this->initializedProduct->expects($this->any()) @@ -269,6 +271,12 @@ protected function setUp() $this->initializedProduct ->method('getExtensionAttributes') ->willReturn($this->productExtension); + $this->product + ->method('getCategoryIds') + ->willReturn([1, 2, 3, 4]); + $this->initializedProduct + ->method('getCategoryIds') + ->willReturn([1, 2, 3, 4]); $storeMock = $this->getMockBuilder(StoreInterface::class) ->disableOriginalConstructor() ->setMethods([])