/** * {@inheritdoc} */ public function deleteById($sku, $type, $linkedProductSku) { $linkItems = $this->linkManagement->getLinkedItemsByType($sku, $type); /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $linkItem */ foreach ($linkItems as $linkItem) { if ($linkItem->getLinkedProductSku() == $linkedProductSku) { return $this->delete($linkItem); } } throw new NoSuchEntityException(__('Product %1 doesn\'t have linked %2 as %3', [$sku, $linkedProductSku, $type])); }
/** * @expectedException \Magento\Framework\Exception\CouldNotSaveException * @expectedExceptionMessage Invalid data provided for linked products */ public function testSetProductLinksInvalidDataException() { $productSku = 'Simple Product 1'; $linkType = 'related'; $this->productRepositoryMock->expects($this->once())->method('get')->with($productSku)->willReturn($this->productMock); $inputRelatedLink = $this->objectManager->getObject('Magento\\Catalog\\Model\\ProductLink\\Link'); $inputRelatedLink->setProductSku($productSku); $inputRelatedLink->setLinkType($linkType); $inputRelatedLink->setData("sku", "bad sku"); $inputRelatedLink->setData("type_id", "bad type"); $inputRelatedLink->setPosition(0); $links = [$inputRelatedLink]; $linkTypes = ['related' => 1, 'upsell' => 4, 'crosssell' => 5, 'associated' => 3]; $this->linkTypeProviderMock->expects($this->once())->method('getLinkTypes')->willReturn($linkTypes); $this->productMock->expects($this->once())->method('getProductLinks')->willReturn([]); $this->productRepositoryMock->expects($this->once())->method('save')->willThrowException(new \Exception()); $this->model->setProductLinks($productSku, $linkType, $links); }
/** * @expectedException \Magento\Framework\Exception\CouldNotSaveException * @expectedExceptionMessage Invalid data provided for linked products */ public function testSetProductLinksInvalidDataException() { $type = 'type'; $linkedProductsMock = []; $linksData = []; $this->linkTypeProviderMock->expects($this->once())->method('getLinkTypes')->willReturn([$type => 'link']); for ($i = 0; $i < 2; $i++) { $linkMock = $this->getMockForAbstractClass('\\Magento\\Catalog\\Api\\Data\\ProductLinkInterface', [], '', false, false, true, ['getLinkedProductSku', '__toArray']); $linkMock->expects($this->exactly(2))->method('getLinkedProductSku')->willReturn('linkedProduct' . $i . 'Sku'); $linkMock->expects($this->once())->method('__toArray'); $linkedProductsMock[$i] = $linkMock; $linksData['productSku']['link'][] = $linkMock; } $linkedSkuList = ['linkedProduct0Sku', 'linkedProduct1Sku']; $linkedProductIds = ['linkedProduct0Sku' => 1, 'linkedProduct1Sku' => 2]; $this->productResourceMock->expects($this->once())->method('getProductsIdsBySkus')->with($linkedSkuList)->willReturn($linkedProductIds); $this->productRepositoryMock->expects($this->once())->method('get')->willReturn($this->productMock); $this->linkInitializerMock->expects($this->once())->method('initializeLinks')->with($this->productMock, [$type => [1 => ['product_id' => 1], 2 => ['product_id' => 2]]]); $this->productMock->expects($this->once())->method('save')->willThrowException(new \Exception()); $this->model->setProductLinks('', $type, $linkedProductsMock); }