|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Catalog\Model\Category\Link; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\Data\CategoryLinkInterfaceFactory; |
| 11 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 12 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 13 | +use Magento\Framework\EntityManager\MetadataPool; |
| 14 | +use Magento\TestFramework\Helper\Bootstrap; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * Save handler test |
| 19 | + * |
| 20 | + * @magentoDataFixture Magento/Catalog/_files/categories_no_products.php |
| 21 | + * @magentoDataFixture Magento/Catalog/_files/second_product_simple.php |
| 22 | + */ |
| 23 | +class SaveHandlerTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var ProductRepositoryInterface |
| 27 | + */ |
| 28 | + private $productRepository; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + private $productLinkField; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var CategoryLinkInterfaceFactory |
| 37 | + */ |
| 38 | + private $categoryLinkFactory; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var SaveHandler |
| 42 | + */ |
| 43 | + private $saveHandler; |
| 44 | + |
| 45 | + /** |
| 46 | + * @inheritdoc |
| 47 | + */ |
| 48 | + protected function setUp() |
| 49 | + { |
| 50 | + $objectManager = Bootstrap::getObjectManager(); |
| 51 | + $this->productRepository = $objectManager->create(ProductRepositoryInterface::class); |
| 52 | + $metadataPool = $objectManager->create(MetadataPool::class); |
| 53 | + $this->productLinkField = $metadataPool->getMetadata(ProductInterface::class) |
| 54 | + ->getLinkField(); |
| 55 | + $this->categoryLinkFactory = $objectManager->create(CategoryLinkInterfaceFactory::class); |
| 56 | + $this->saveHandler = $objectManager->create(SaveHandler::class); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Execute test |
| 61 | + * |
| 62 | + * @return void |
| 63 | + */ |
| 64 | + public function testExecute(): void |
| 65 | + { |
| 66 | + $product = $this->productRepository->get('simple2'); |
| 67 | + $product->setCategoryIds([3, 4, 6]); |
| 68 | + $this->productRepository->save($product); |
| 69 | + $categoryPositions = [ |
| 70 | + 3 => [ |
| 71 | + 'category_id' => 3, |
| 72 | + 'position' => 0, |
| 73 | + ], |
| 74 | + 4 => [ |
| 75 | + 'category_id' => 4, |
| 76 | + 'position' => 0, |
| 77 | + ], |
| 78 | + 6 => [ |
| 79 | + 'category_id' => 6, |
| 80 | + 'position' => 0, |
| 81 | + ], |
| 82 | + ]; |
| 83 | + |
| 84 | + $categoryLinks = $product->getExtensionAttributes()->getCategoryLinks(); |
| 85 | + $this->assertEmpty($categoryLinks); |
| 86 | + |
| 87 | + $categoryLinks = []; |
| 88 | + $categoryPositions[4]['position'] = 1; |
| 89 | + $categoryPositions[6]['position'] = 1; |
| 90 | + foreach ($categoryPositions as $categoryPosition) { |
| 91 | + $categoryLink = $this->categoryLinkFactory->create() |
| 92 | + ->setCategoryId($categoryPosition['category_id']) |
| 93 | + ->setPosition($categoryPosition['position']); |
| 94 | + $categoryLinks[] = $categoryLink; |
| 95 | + } |
| 96 | + $categoryLinks = $this->updateCategoryLinks($product, $categoryLinks); |
| 97 | + $this->assertPositions($categoryPositions, $categoryLinks); |
| 98 | + |
| 99 | + $categoryPositions[4]['position'] = 2; |
| 100 | + $categoryLink = $this->categoryLinkFactory->create() |
| 101 | + ->setCategoryId(4) |
| 102 | + ->setPosition($categoryPositions[4]['position']); |
| 103 | + $categoryLinks = $this->updateCategoryLinks($product, [$categoryLink]); |
| 104 | + $this->assertPositions($categoryPositions, $categoryLinks); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Update category links |
| 109 | + * |
| 110 | + * @param ProductInterface $product |
| 111 | + * @param \Magento\Catalog\Api\Data\CategoryLinkInterface[] $categoryLinks |
| 112 | + * @return \Magento\Catalog\Api\Data\CategoryLinkInterface[] |
| 113 | + */ |
| 114 | + private function updateCategoryLinks(ProductInterface $product, array $categoryLinks): array |
| 115 | + { |
| 116 | + $product->getExtensionAttributes()->setCategoryLinks($categoryLinks); |
| 117 | + $arguments = [$this->productLinkField => $product->getData($this->productLinkField)]; |
| 118 | + $this->saveHandler->execute($product, $arguments); |
| 119 | + $product = $this->productRepository->get($product->getSku(), false, null, true); |
| 120 | + $categoryLinks = $product->getExtensionAttributes() |
| 121 | + ->getCategoryLinks(); |
| 122 | + |
| 123 | + return $categoryLinks; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Assert positions |
| 128 | + * |
| 129 | + * @param array $categoryPositions |
| 130 | + * @param array $categoryLinks |
| 131 | + * @return void |
| 132 | + */ |
| 133 | + private function assertPositions(array $categoryPositions, array $categoryLinks): void |
| 134 | + { |
| 135 | + foreach ($categoryLinks as $categoryLink) { |
| 136 | + $categoryPosition = $categoryPositions[$categoryLink->getCategoryId()]; |
| 137 | + $this->assertEquals($categoryPosition['category_id'], $categoryLink->getCategoryId()); |
| 138 | + $this->assertEquals($categoryPosition['position'], $categoryLink->getPosition()); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments