Skip to content

Commit bb0e27a

Browse files
committed
Merge branch 'MC-39598' into 2.4-develop-sidecar-pr9
2 parents 94d9f89 + 9b0799b commit bb0e27a

File tree

2 files changed

+196
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)