Skip to content

Commit 640adcd

Browse files
committed
MC-39598: Create automated test for: "Assign/Unassign product to websites via API"
1 parent 24cb7ac commit 640adcd

File tree

2 files changed

+185
-0
lines changed

2 files changed

+185
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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\Api;
9+
10+
use Magento\Framework\ObjectManagerInterface;
11+
use Magento\Framework\Webapi\Rest\Request;
12+
use Magento\Store\Api\WebsiteRepositoryInterface;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\TestCase\WebapiAbstract;
15+
16+
/**
17+
* Tests to check products to websites assigning.
18+
*
19+
* @see \Magento\Catalog\Model\ProductWebsiteLinkRepository
20+
*
21+
* @magentoAppIsolation enabled
22+
*/
23+
class ProductWebsiteLinkRepositoryTest extends WebapiAbstract
24+
{
25+
const SERVICE_NAME = 'catalogProductWebsiteLinkRepositoryV1';
26+
const SERVICE_VERSION = 'V1';
27+
28+
/** @var ObjectManagerInterface */
29+
private $objectManager;
30+
31+
/** @var ProductRepositoryInterface */
32+
private $productRepository;
33+
34+
/** @var WebsiteRepositoryInterface */
35+
private $websiteRepository;
36+
37+
/**
38+
* @inheritdoc
39+
*/
40+
protected function setUp(): void
41+
{
42+
parent::setUp();
43+
44+
$this->objectManager = Bootstrap::getObjectManager();
45+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
46+
$this->websiteRepository = $this->objectManager->get(WebsiteRepositoryInterface::class);
47+
}
48+
49+
/**
50+
* @magentoApiDataFixture Magento/Catalog/_files/second_product_simple.php
51+
*
52+
* @return void
53+
*/
54+
public function testSaveWebsiteLinkWithoutWebsiteId(): void
55+
{
56+
$serviceInfo = $this->fillServiceInfo('/V1/products/:sku/websites', Request::HTTP_METHOD_POST, 'Save');
57+
$requestData = ['productWebsiteLink' => ['sku' => 'simple2']];
58+
$this->expectException(\Exception::class);
59+
$this->expectErrorMessage((string)__('There are not websites for assign to product'));
60+
$this->_webApiCall($serviceInfo, $requestData);
61+
}
62+
63+
/**
64+
* @magentoApiDataFixture Magento/Catalog/_files/second_product_simple.php
65+
*
66+
* @return void
67+
*/
68+
public function testSaveWebsiteLinkWithUnexistingWebsiteId(): void
69+
{
70+
$unexistingWebsiteId = 8932568989;
71+
$serviceInfo = $this->fillServiceInfo('/V1/products/:sku/websites', Request::HTTP_METHOD_POST, 'Save');
72+
$requestData = ['productWebsiteLink' => ['sku' => 'simple2', 'websiteId' => $unexistingWebsiteId]];
73+
$this->expectException(\Exception::class);
74+
$this->expectExceptionMessageMatches('/Could not assign product \\\"%1\\\" to websites \\\"%2\\\"/');
75+
$this->_webApiCall($serviceInfo, $requestData);
76+
}
77+
78+
/**
79+
* @magentoApiDataFixture Magento/Catalog/_files/product_with_two_websites.php
80+
*
81+
* @return void
82+
*/
83+
public function testDeleteWebsiteLink(): void
84+
{
85+
$productSku = 'unique-simple-azaza';
86+
$websiteId = (int)$this->websiteRepository->get('second_website')->getId();
87+
$resourcePath = sprintf('/V1/products/%s/websites/%u', $productSku, $websiteId);
88+
$serviceInfo = $this->fillServiceInfo($resourcePath, Request::HTTP_METHOD_DELETE, 'Delete');
89+
$this->_webApiCall($serviceInfo);
90+
$product = $this->productRepository->get($productSku, false, null, true);
91+
$this->assertNotContains($websiteId, $product->getWebsiteIds());
92+
}
93+
94+
/**
95+
* Fill service information
96+
*
97+
* @param string $resourcePath
98+
* @param string $httpMethod
99+
* @param string $operation
100+
* @return array
101+
*/
102+
private function fillServiceInfo(string $resourcePath, string $httpMethod, string $operation): array
103+
{
104+
return [
105+
'rest' => ['resourcePath' => $resourcePath, 'httpMethod' => $httpMethod],
106+
'soap' => [
107+
'service' => self::SERVICE_NAME,
108+
'serviceVersion' => self::SERVICE_VERSION,
109+
'operation' => self::SERVICE_NAME . $operation,
110+
],
111+
];
112+
}
113+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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;
9+
10+
use Magento\Catalog\Api\Data\ProductWebsiteLinkInterfaceFactory;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Catalog\Api\ProductWebsiteLinkRepositoryInterface;
13+
use Magento\Framework\ObjectManagerInterface;
14+
use Magento\Store\Api\WebsiteRepositoryInterface;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* Tests to check products to websites assigning.
20+
*
21+
* @see \Magento\Catalog\Model\ProductWebsiteLinkRepository
22+
*
23+
* @magentoAppIsolation enabled
24+
*/
25+
class ProductWebsiteLinkRepositoryTest extends TestCase
26+
{
27+
/** @var ObjectManagerInterface */
28+
private $objectManager;
29+
30+
/** @var ProductWebsiteLinkRepositoryInterface */
31+
private $productWebsiteLinkRepository;
32+
33+
/** @var ProductWebsiteLinkInterfaceFactory */
34+
private $productWebsiteLinkFactory;
35+
36+
/** @var ProductRepositoryInterface */
37+
private $productRepository;
38+
39+
/** @var WebsiteRepositoryInterface */
40+
private $websiteRepository;
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function setUp(): void
46+
{
47+
parent::setUp();
48+
49+
$this->objectManager = Bootstrap::getObjectManager();
50+
$this->productWebsiteLinkRepository = $this->objectManager->get(ProductWebsiteLinkRepositoryInterface::class);
51+
$this->productWebsiteLinkFactory = $this->objectManager->get(ProductWebsiteLinkInterfaceFactory::class);
52+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
53+
$this->productRepository->cleanCache();
54+
$this->websiteRepository = $this->objectManager->get(WebsiteRepositoryInterface::class);
55+
}
56+
57+
/**
58+
* @magentoApiDataFixture Magento/Catalog/_files/product_with_two_websites.php
59+
*
60+
* @return void
61+
*/
62+
public function testDelete(): void
63+
{
64+
$this->markTestSkipped('Blocked by MC-40250');
65+
$productWebsiteLink = $this->productWebsiteLinkFactory->create();
66+
$productWebsiteLink->setSku('unique-simple-azaza');
67+
$productWebsiteLink->setWebsiteId(1);
68+
$this->productWebsiteLinkRepository->delete($productWebsiteLink);
69+
$product = $this->productRepository->get('unique-simple-azaza', false, null, true);
70+
$this->assertEquals([$this->websiteRepository->get('second_website')->getId()], $product->getWebsiteIds());
71+
}
72+
}

0 commit comments

Comments
 (0)