|
| 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\Controller\Adminhtml\Product\Save; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\Catalog\Model\Product; |
| 12 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 13 | +use Magento\Framework\App\Request\Http as HttpRequest; |
| 14 | +use Magento\Framework\Message\MessageInterface; |
| 15 | +use Magento\TestFramework\TestCase\AbstractBackendController; |
| 16 | + |
| 17 | +/** |
| 18 | + * Saving product with linked products |
| 19 | + * |
| 20 | + * @magentoAppArea adminhtml |
| 21 | + */ |
| 22 | +class LinksTest extends AbstractBackendController |
| 23 | +{ |
| 24 | + /** @var array */ |
| 25 | + private $linkTypes = [ |
| 26 | + 'upsell', |
| 27 | + 'crosssell', |
| 28 | + 'related', |
| 29 | + ]; |
| 30 | + |
| 31 | + /** @var ProductRepositoryInterface $productRepository */ |
| 32 | + private $productRepository; |
| 33 | + |
| 34 | + /** |
| 35 | + * @inheritdoc |
| 36 | + */ |
| 37 | + protected function setUp() |
| 38 | + { |
| 39 | + parent::setUp(); |
| 40 | + $this->productRepository = $this->_objectManager->create(ProductRepositoryInterface::class); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Test add simple related, up-sells, cross-sells product |
| 45 | + * |
| 46 | + * @magentoDataFixture Magento/Catalog/_files/multiple_products.php |
| 47 | + * @magentoDbIsolation enabled |
| 48 | + * @return void |
| 49 | + */ |
| 50 | + public function testAddRelatedUpSellCrossSellProducts(): void |
| 51 | + { |
| 52 | + $postData = $this->getPostData(); |
| 53 | + $this->getRequest()->setMethod(HttpRequest::METHOD_POST); |
| 54 | + $this->getRequest()->setPostValue($postData); |
| 55 | + $this->dispatch('backend/catalog/product/save'); |
| 56 | + $this->assertSessionMessages( |
| 57 | + $this->equalTo(['You saved the product.']), |
| 58 | + MessageInterface::TYPE_SUCCESS |
| 59 | + ); |
| 60 | + $product = $this->productRepository->get('simple'); |
| 61 | + $this->assertEquals( |
| 62 | + $this->getExpectedLinks($postData['links']), |
| 63 | + $this->getActualLinks($product), |
| 64 | + "Expected linked products do not match actual linked products!" |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Get post data for the request |
| 70 | + * |
| 71 | + * @return array |
| 72 | + */ |
| 73 | + private function getPostData(): array |
| 74 | + { |
| 75 | + return [ |
| 76 | + 'product' => [ |
| 77 | + 'attribute_set_id' => '4', |
| 78 | + 'status' => '1', |
| 79 | + 'name' => 'Simple Product', |
| 80 | + 'sku' => 'simple', |
| 81 | + 'url_key' => 'simple-product', |
| 82 | + ], |
| 83 | + 'links' => [ |
| 84 | + 'upsell' => [ |
| 85 | + ['id' => '10'], |
| 86 | + ], |
| 87 | + 'crosssell' => [ |
| 88 | + ['id' => '11'], |
| 89 | + ], |
| 90 | + 'related' => [ |
| 91 | + ['id' => '12'], |
| 92 | + ], |
| 93 | + ] |
| 94 | + ]; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Set an array of expected related, up-sells, cross-sells product identifiers |
| 99 | + * |
| 100 | + * @param array $links |
| 101 | + * @return array |
| 102 | + */ |
| 103 | + private function getExpectedLinks(array $links): array |
| 104 | + { |
| 105 | + $expectedLinks = []; |
| 106 | + foreach ($this->linkTypes as $linkType) { |
| 107 | + $expectedLinks[$linkType] = []; |
| 108 | + foreach ($links[$linkType] as $productData) { |
| 109 | + $expectedLinks[$linkType][] = $productData['id']; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return $expectedLinks; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Get an array of received related, up-sells, cross-sells products |
| 118 | + * |
| 119 | + * @param ProductInterface|Product $product |
| 120 | + * @return array |
| 121 | + */ |
| 122 | + private function getActualLinks(ProductInterface $product): array |
| 123 | + { |
| 124 | + $actualLinks = []; |
| 125 | + foreach ($this->linkTypes as $linkType) { |
| 126 | + $ids = []; |
| 127 | + switch ($linkType) { |
| 128 | + case 'upsell': |
| 129 | + $ids = $product->getUpSellProductIds(); |
| 130 | + break; |
| 131 | + case 'crosssell': |
| 132 | + $ids = $product->getCrossSellProductIds(); |
| 133 | + break; |
| 134 | + case 'related': |
| 135 | + $ids = $product->getRelatedProductIds(); |
| 136 | + break; |
| 137 | + } |
| 138 | + $actualLinks[$linkType] = $ids; |
| 139 | + } |
| 140 | + |
| 141 | + return $actualLinks; |
| 142 | + } |
| 143 | +} |
0 commit comments