Skip to content

Commit 6b34148

Browse files
committed
Merge remote-tracking branch 'origin/2.4-develop' into MC-40541
2 parents 7cb843d + f6e06ec commit 6b34148

File tree

11 files changed

+702
-171
lines changed

11 files changed

+702
-171
lines changed

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductLinkRepositoryInterfaceTest.php

Lines changed: 121 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,100 +4,175 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
declare(strict_types=1);
8+
79
namespace Magento\Catalog\Api;
810

11+
use Magento\Catalog\Model\ProductLink\Link;
12+
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\Framework\Webapi\Rest\Request;
914
use Magento\TestFramework\Helper\Bootstrap;
1015
use Magento\TestFramework\TestCase\WebapiAbstract;
1116

17+
/**
18+
* Class checks product relations functionality
19+
*
20+
* @see \Magento\Catalog\Api\ProductLinkRepositoryInterface
21+
*/
1222
class ProductLinkRepositoryInterfaceTest extends WebapiAbstract
1323
{
24+
/**
25+
* @var string
26+
*/
1427
const SERVICE_NAME = 'catalogProductLinkRepositoryV1';
28+
29+
/**
30+
* @var string
31+
*/
1532
const SERVICE_VERSION = 'V1';
33+
34+
/**
35+
* @var string
36+
*/
1637
const RESOURCE_PATH = '/V1/products/';
1738

1839
/**
19-
* @var \Magento\Framework\ObjectManagerInterface
40+
* @var ObjectManagerInterface
2041
*/
21-
protected $objectManager;
42+
private $objectManager;
2243

44+
/**
45+
* @var ProductLinkManagementInterface
46+
*/
47+
private $linkManagement;
48+
49+
/**
50+
* @inheritdoc
51+
*/
2352
protected function setUp(): void
2453
{
54+
parent::setUp();
55+
2556
$this->objectManager = Bootstrap::getObjectManager();
57+
$this->linkManagement = $this->objectManager->get(ProductLinkManagementInterface::class);
2658
}
2759

2860
/**
2961
* @magentoApiDataFixture Magento/Catalog/_files/products_related_multiple.php
30-
* @magentoAppIsolation enabled
62+
*
63+
* @return void
3164
*/
32-
public function testDelete()
65+
public function testDelete(): void
3366
{
3467
$productSku = 'simple_with_cross';
35-
$linkedSku = 'simple';
3668
$linkType = 'related';
37-
$this->_webApiCall(
38-
[
39-
'rest' => [
40-
'resourcePath' => self::RESOURCE_PATH . $productSku . '/links/' . $linkType . '/' . $linkedSku,
41-
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
42-
],
43-
'soap' => [
44-
'service' => self::SERVICE_NAME,
45-
'serviceVersion' => self::SERVICE_VERSION,
46-
'operation' => self::SERVICE_NAME . 'DeleteById',
47-
],
48-
],
49-
[
50-
'sku' => $productSku,
51-
'type' => $linkType,
52-
'linkedProductSku' => $linkedSku
53-
]
54-
);
55-
/** @var \Magento\Catalog\Model\ProductLink\Management $linkManagement */
56-
$linkManagement = $this->objectManager->create(\Magento\Catalog\Api\ProductLinkManagementInterface::class);
57-
$linkedProducts = $linkManagement->getLinkedItemsByType($productSku, $linkType);
69+
$this->deleteApiCall($productSku, $linkType, 'simple');
70+
$linkedProducts = $this->linkManagement->getLinkedItemsByType($productSku, $linkType);
5871
$this->assertCount(1, $linkedProducts);
59-
/** @var \Magento\Catalog\Api\Data\ProductLinkInterface $product */
6072
$product = current($linkedProducts);
61-
$this->assertEquals($product->getLinkedProductSku(), 'simple_with_cross_two');
73+
$this->assertEquals('simple_with_cross_two', $product->getLinkedProductSku());
74+
}
75+
76+
/**
77+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
78+
*
79+
* @return void
80+
*/
81+
public function testDeleteNotExistedProductLink(): void
82+
{
83+
$this->expectException(\Exception::class);
84+
$this->expectExceptionMessage((string)__("Product %1 doesn't have linked %2 as %3"));
85+
$this->deleteApiCall('simple', 'related', 'not_exists_product');
6286
}
6387

6488
/**
6589
* @magentoApiDataFixture Magento/Catalog/_files/products_related.php
90+
*
91+
* @return void
6692
*/
67-
public function testSave()
93+
public function testSave(): void
6894
{
6995
$productSku = 'simple_with_cross';
7096
$linkType = 'related';
97+
$data = [
98+
'entity' => [
99+
Link::KEY_SKU => 'simple_with_cross',
100+
Link::KEY_LINK_TYPE => 'related',
101+
Link::KEY_LINKED_PRODUCT_SKU => 'simple',
102+
Link::KEY_LINKED_PRODUCT_TYPE => 'simple',
103+
Link::KEY_POSITION => 1000,
104+
],
105+
];
106+
$this->saveApiCall($productSku, $data);
107+
$actual = $this->linkManagement->getLinkedItemsByType($productSku, $linkType);
108+
$this->assertCount(1, $actual, 'Invalid actual linked products count');
109+
$this->assertEquals(1000, $actual[0]->getPosition(), 'Product position is not updated');
110+
}
71111

72-
$serviceInfo = [
112+
/**
113+
* Get service info for api call
114+
*
115+
* @param string $resourcePath
116+
* @param string $httpMethod
117+
* @param string $operation
118+
* @return array
119+
*/
120+
private function getServiceInfo(string $resourcePath, string $httpMethod, string $operation): array
121+
{
122+
return [
73123
'rest' => [
74-
'resourcePath' => self::RESOURCE_PATH . $productSku . '/links',
75-
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
124+
'resourcePath' => self::RESOURCE_PATH . $resourcePath,
125+
'httpMethod' => $httpMethod,
76126
],
77127
'soap' => [
78128
'service' => self::SERVICE_NAME,
79129
'serviceVersion' => self::SERVICE_VERSION,
80-
'operation' => self::SERVICE_NAME . 'Save',
130+
'operation' => self::SERVICE_NAME . $operation,
81131
],
82132
];
133+
}
83134

84-
$this->_webApiCall(
135+
/**
136+
* Make api call to delete product link
137+
*
138+
* @param string $productSku
139+
* @param string $linkType
140+
* @param string $linkedSku
141+
* @return array|int|string|float|bool
142+
*/
143+
private function deleteApiCall(string $productSku, string $linkType, string $linkedSku)
144+
{
145+
$serviceInfo = $this->getServiceInfo(
146+
$productSku . '/links/' . $linkType . '/' . $linkedSku,
147+
Request::HTTP_METHOD_DELETE,
148+
'DeleteById'
149+
);
150+
151+
return $this->_webApiCall(
85152
$serviceInfo,
86153
[
87-
'entity' => [
88-
'sku' => 'simple_with_cross',
89-
'link_type' => 'related',
90-
'linked_product_sku' => 'simple',
91-
'linked_product_type' => 'simple',
92-
'position' => 1000,
93-
]
154+
'sku' => $productSku,
155+
'type' => $linkType,
156+
'linkedProductSku' => $linkedSku,
94157
]
95158
);
159+
}
96160

97-
/** @var \Magento\Catalog\Model\ProductLink\Management $linkManagement */
98-
$linkManagement = $this->objectManager->get(\Magento\Catalog\Api\ProductLinkManagementInterface::class);
99-
$actual = $linkManagement->getLinkedItemsByType($productSku, $linkType);
100-
$this->assertCount(1, $actual, 'Invalid actual linked products count');
101-
$this->assertEquals(1000, $actual[0]->getPosition(), 'Product position is not updated');
161+
/**
162+
* Make api call to save product link
163+
*
164+
* @param string $productSku
165+
* @param array $data
166+
* @return array|bool|float|int|string
167+
*/
168+
private function saveApiCall(string $productSku, array $data)
169+
{
170+
$serviceInfo = $this->getServiceInfo(
171+
$productSku . '/links',
172+
Request::HTTP_METHOD_PUT,
173+
'Save'
174+
);
175+
176+
return $this->_webApiCall($serviceInfo, $data);
102177
}
103178
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\Category;
9+
10+
use Magento\Catalog\Helper\DefaultCategory;
11+
use Magento\TestFramework\TestCase\AbstractBackendController;
12+
13+
/**
14+
* Tests for catalog category Add controller
15+
*
16+
* @see \Magento\Catalog\Controller\Adminhtml\Category\Add
17+
*
18+
* @magentoAppArea adminhtml
19+
* @magentoDbIsolation enabled
20+
*/
21+
class AddTest extends AbstractBackendController
22+
{
23+
/**
24+
* @var DefaultCategory
25+
*/
26+
private $defaultCategoryHelper;
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
protected function setUp(): void
32+
{
33+
parent::setUp();
34+
35+
$this->defaultCategoryHelper = $this->_objectManager->get(DefaultCategory::class);
36+
}
37+
38+
/**
39+
* @return void
40+
*/
41+
public function testExecuteWithoutParams(): void
42+
{
43+
$this->dispatch('backend/catalog/category/add');
44+
$this->assertRedirect($this->stringContains('catalog/category/index'));
45+
}
46+
47+
/**
48+
* @return void
49+
*/
50+
public function testExecuteAsAjax(): void
51+
{
52+
$this->getRequest()->setQueryValue('isAjax', true);
53+
$this->getRequest()->setParam('parent', $this->defaultCategoryHelper->getId());
54+
$this->dispatch('backend/catalog/category/add');
55+
$this->assertJson($this->getResponse()->getBody());
56+
}
57+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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\Layer\Category;
9+
10+
use Magento\Catalog\Api\CategoryRepositoryInterface;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Tests for category collection provider.
17+
*
18+
* @see \Magento\Catalog\Model\Layer\Category\ItemCollectionProvider
19+
* @magentoAppArea frontend
20+
*/
21+
class ItemCollectionProviderTest extends TestCase
22+
{
23+
/** @var ObjectManagerInterface */
24+
private $objectManager;
25+
26+
/** @var CategoryRepositoryInterface */
27+
private $categoryRepository;
28+
29+
/** @var ItemCollectionProvider */
30+
private $itemCollectionProvider;
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
protected function setUp(): void
36+
{
37+
parent::setUp();
38+
39+
$this->objectManager = Bootstrap::getObjectManager();
40+
$this->itemCollectionProvider = $this->objectManager->get(ItemCollectionProvider::class);
41+
$this->categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class);
42+
}
43+
44+
/**
45+
* @magentoDbIsolation disabled
46+
* @magentoDataFixture Magento/Catalog/_files/category_product.php
47+
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
48+
*
49+
* @return void
50+
*/
51+
public function testGetCollection(): void
52+
{
53+
$category = $this->categoryRepository->get(333);
54+
$categoryProductsCollection = $this->itemCollectionProvider->getCollection($category);
55+
$this->assertCount(1, $categoryProductsCollection);
56+
$this->assertEquals('simple333', $categoryProductsCollection->getFirstItem()->getSku());
57+
}
58+
}

0 commit comments

Comments
 (0)