Skip to content

Commit 0eda742

Browse files
committed
MC-20675: Admin: Add/edit/delete related, up-sells, cross-sells products
1 parent e19e7ab commit 0eda742

File tree

2 files changed

+40
-37
lines changed

2 files changed

+40
-37
lines changed

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/Product/Save/LinksTest.php

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Catalog\Model\Product;
1212
use Magento\Catalog\Api\Data\ProductInterface;
1313
use Magento\Framework\App\Request\Http as HttpRequest;
14+
use Magento\Framework\Message\MessageInterface;
1415
use Magento\TestFramework\TestCase\AbstractBackendController;
1516

1617
/**
@@ -42,17 +43,21 @@ protected function setUp()
4243
/**
4344
* Test add simple related, up-sells, cross-sells product
4445
*
45-
* @dataProvider addRelatedUpSellCrossSellProductsProvider
4646
* @magentoDataFixture Magento/Catalog/_files/multiple_products.php
4747
* @magentoDbIsolation enabled
4848
* @param array $postData
4949
* @return void
5050
*/
51-
public function testAddRelatedUpSellCrossSellProducts(array $postData): void
51+
public function testAddRelatedUpSellCrossSellProducts(): void
5252
{
53+
$postData = $this->getPostData();
5354
$this->getRequest()->setMethod(HttpRequest::METHOD_POST);
5455
$this->getRequest()->setPostValue($postData);
5556
$this->dispatch('backend/catalog/product/save');
57+
$this->assertSessionMessages(
58+
$this->equalTo(['You saved the product.']),
59+
MessageInterface::TYPE_SUCCESS
60+
);
5661
$product = $this->productRepository->get('simple');
5762
$this->assertEquals(
5863
$this->getExpectedLinks($postData['links']),
@@ -62,34 +67,30 @@ public function testAddRelatedUpSellCrossSellProducts(array $postData): void
6267
}
6368

6469
/**
65-
* Provide test data for testAddRelatedUpSellCrossSellProducts().
70+
* Get post data for the request
6671
*
6772
* @return array
6873
*/
69-
public function addRelatedUpSellCrossSellProductsProvider(): array
74+
public function getPostData(): array
7075
{
7176
return [
72-
[
73-
'post_data' => [
74-
'product' => [
75-
'attribute_set_id' => '4',
76-
'status' => '1',
77-
'name' => 'Simple Product',
78-
'sku' => 'simple',
79-
'url_key' => 'simple-product',
80-
],
81-
'links' => [
82-
'upsell' => [
83-
['id' => '10'],
84-
],
85-
'crosssell' => [
86-
['id' => '11'],
87-
],
88-
'related' => [
89-
['id' => '12'],
90-
],
91-
]
92-
]
77+
'product' => [
78+
'attribute_set_id' => '4',
79+
'status' => '1',
80+
'name' => 'Simple Product',
81+
'sku' => 'simple',
82+
'url_key' => 'simple-product',
83+
],
84+
'links' => [
85+
'upsell' => [
86+
['id' => '10'],
87+
],
88+
'crosssell' => [
89+
['id' => '11'],
90+
],
91+
'related' => [
92+
['id' => '12'],
93+
],
9394
]
9495
];
9596
}
@@ -123,23 +124,19 @@ private function getActualLinks(ProductInterface $product): array
123124
{
124125
$actualLinks = [];
125126
foreach ($this->linkTypes as $linkType) {
126-
$products = [];
127-
$actualLinks[$linkType] = [];
127+
$ids = [];
128128
switch ($linkType) {
129129
case 'upsell':
130-
$products = $product->getUpSellProducts();
130+
$ids = $product->getUpSellProductIds();
131131
break;
132132
case 'crosssell':
133-
$products = $product->getCrossSellProducts();
133+
$ids = $product->getCrossSellProductIds();
134134
break;
135135
case 'related':
136-
$products = $product->getRelatedProducts();
136+
$ids = $product->getRelatedProductIds();
137137
break;
138138
}
139-
/** @var ProductInterface|Product $productItem */
140-
foreach ($products as $productItem) {
141-
$actualLinks[$linkType][] = $productItem->getId();
142-
}
139+
$actualLinks[$linkType] = $ids;
143140
}
144141

145142
return $actualLinks;

dev/tests/integration/testsuite/Magento/Catalog/Model/Product/LinksTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Catalog\Api\Data\ProductInterface;
1111
use Magento\Catalog\Api\Data\ProductLinkInterface;
12+
use Magento\Catalog\Api\Data\ProductLinkInterfaceFactory;
1213
use Magento\Catalog\Api\ProductRepositoryInterface;
1314
use Magento\Catalog\Model\Product;
1415
use Magento\Catalog\Model\ProductLink\Link;
@@ -62,7 +63,7 @@ class LinksTest extends TestCase
6263
],
6364
];
6465

65-
/** @var ProductRepositoryInterface $productRepository */
66+
/** @var ProductRepositoryInterface */
6667
private $productRepository;
6768

6869
/** @var ObjectManager */
@@ -71,6 +72,9 @@ class LinksTest extends TestCase
7172
/** @var ProductResource */
7273
private $productResource;
7374

75+
/** @var ProductLinkInterfaceFactory */
76+
private $productLinkInterfaceFactory;
77+
7478
/**
7579
* @inheritdoc
7680
*/
@@ -80,6 +84,7 @@ protected function setUp()
8084
$this->objectManager = Bootstrap::getObjectManager();
8185
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
8286
$this->productResource = $this->objectManager->create(ProductResource::class);
87+
$this->productLinkInterfaceFactory = $this->objectManager->create(ProductLinkInterfaceFactory::class);
8388
}
8489

8590
/**
@@ -203,14 +208,15 @@ private function getProductData(array $productFixture): array
203208
*
204209
* @param ProductInterface|Product $product
205210
* @param array $productData
211+
* @return void
206212
*/
207213
private function setCustomProductLinks(ProductInterface $product, array $productData): void
208214
{
209215
$productLinks = [];
210216
foreach ($productData as $linkType => $links) {
211217
foreach ($links as $data) {
212-
/** @var Link $productLink */
213-
$productLink = $this->objectManager->create(ProductLinkInterface::class);
218+
/** @var ProductLinkInterface|Link $productLink */
219+
$productLink = $this->productLinkInterfaceFactory->create();
214220
$productLink->setSku('simple');
215221
$productLink->setLinkedProductSku($data['sku']);
216222
if (isset($data['position'])) {

0 commit comments

Comments
 (0)