Skip to content

Commit 42ccbc3

Browse files
committed
MC-20684: Admin: Add/remove product from other storeviews and websites
1 parent 3397b2b commit 42ccbc3

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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\Product;
9+
10+
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Catalog\Model\ResourceModel\Product\WebsiteFactory;
13+
use Magento\Framework\Exception\NoSuchEntityException;
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+
* Checks product websites attribute save behaviour
21+
*
22+
* @magentoDbIsolation enabled
23+
*/
24+
class UpdateProductWebsiteTest extends TestCase
25+
{
26+
/** @var ObjectManagerInterface */
27+
private $objectManager;
28+
29+
/** @var WebsiteFactory */
30+
private $websiteProductsResourceFactory;
31+
32+
/** @var WebsiteRepositoryInterface */
33+
private $websiteRepository;
34+
35+
/** @var ProductRepositoryInterface */
36+
private $productRepository;
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
protected function setUp()
42+
{
43+
parent::setUp();
44+
45+
$this->objectManager = Bootstrap::getObjectManager();
46+
$this->websiteProductsResourceFactory = $this->objectManager->get(WebsiteFactory::class);
47+
$this->websiteRepository = $this->objectManager->get(WebsiteRepositoryInterface::class);
48+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
49+
}
50+
51+
/**
52+
* @magentoDataFixture Magento/Store/_files/website.php
53+
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
54+
* @return void
55+
*/
56+
public function testAssignProductToWebsite(): void
57+
{
58+
$defaultWebsiteId = $this->websiteRepository->get('base')->getId();
59+
$secondWebsiteId = $this->websiteRepository->get('test')->getId();
60+
$product = $this->updateProductWebsites('simple2', [$defaultWebsiteId, $secondWebsiteId]);
61+
$this->assertProductWebsites((int)$product->getId(), [$defaultWebsiteId, $secondWebsiteId]);
62+
}
63+
64+
/**
65+
* @magentoDbIsolation disabled
66+
* @magentoDataFixture Magento/Catalog/_files/product_two_websites.php
67+
* @return void
68+
*/
69+
public function testUnassignProductFromWebsite(): void
70+
{
71+
$product = $this->productRepository->get('simple-on-two-websites');
72+
$secondWebsiteId = $this->websiteRepository->get('test')->getId();
73+
$product->setWebsiteIds([$secondWebsiteId]);
74+
$product = $this->productRepository->save($product);
75+
$this->assertProductWebsites((int)$product->getId(), [$secondWebsiteId]);
76+
}
77+
78+
/**
79+
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
80+
* @return void
81+
*/
82+
public function testAssignNonExistingWebsite(): void
83+
{
84+
$messageFormat = 'The website with id %s that was requested wasn\'t found. Verify the website and try again.';
85+
$nonExistingWebsiteId = 921564;
86+
$this->expectException(NoSuchEntityException::class);
87+
$this->expectExceptionMessage((string)__(sprintf($messageFormat, $nonExistingWebsiteId)));
88+
$this->updateProductWebsites('simple2', [$nonExistingWebsiteId]);
89+
}
90+
91+
/**
92+
* Update product websites attribute
93+
*
94+
* @param string $productSku
95+
* @param array $websiteIds
96+
* @return ProductInterface
97+
*/
98+
private function updateProductWebsites(string $productSku, array $websiteIds): ProductInterface
99+
{
100+
$product = $this->productRepository->get($productSku);
101+
$product->setWebsiteIds($websiteIds);
102+
103+
return $this->productRepository->save($product);
104+
}
105+
106+
/**
107+
* Assert that websites attribute was correctly saved
108+
*
109+
* @param int $productId
110+
* @param array $expectedData
111+
* @return void
112+
*/
113+
private function assertProductWebsites(int $productId, array $expectedData): void
114+
{
115+
$websiteResource = $this->websiteProductsResourceFactory->create();
116+
$this->assertEquals($expectedData, $websiteResource->getWebsites([$productId])[$productId]);
117+
}
118+
}

0 commit comments

Comments
 (0)