Skip to content

Commit d9f6941

Browse files
committed
MAGETWO-91497: Product websites are not honored
1 parent de8066c commit d9f6941

File tree

1 file changed

+122
-20
lines changed

1 file changed

+122
-20
lines changed

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

Lines changed: 122 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
use Magento\Store\Model\Store;
1111
use Magento\CatalogInventory\Api\Data\StockItemInterface;
1212
use Magento\Store\Model\Website;
13+
use Magento\Store\Model\WebsiteRepository;
1314
use Magento\TestFramework\Helper\Bootstrap;
1415
use Magento\TestFramework\TestCase\WebapiAbstract;
1516
use Magento\Framework\Api\FilterBuilder;
1617
use Magento\Framework\Api\SearchCriteriaBuilder;
1718
use Magento\Framework\Api\SortOrder;
1819
use Magento\Framework\Api\SortOrderBuilder;
1920
use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
21+
use Magento\Framework\Exception\NoSuchEntityException;
2022

2123
/**
2224
* @magentoAppIsolation enabled
@@ -136,6 +138,24 @@ public function productCreationProvider()
136138
];
137139
}
138140

141+
/**
142+
* Load website by website code
143+
*
144+
* @param $websiteCode
145+
* @return Website
146+
*/
147+
private function loadWebsiteByCode($websiteCode)
148+
{
149+
$websiteRepository = Bootstrap::getObjectManager()->get(WebsiteRepository::class);
150+
try{
151+
$website = $websiteRepository->get($websiteCode);
152+
} catch (NoSuchEntityException $e){
153+
$this->fail("Couldn`t load website: {$websiteCode}");
154+
}
155+
156+
return $website;
157+
}
158+
139159
/**
140160
* Test removing association between product and website 1
141161
* @magentoApiDataFixture Magento/Catalog/_files/product_with_two_websites.php
@@ -144,12 +164,7 @@ public function testUpdateWithDeleteWebsites()
144164
{
145165
$productBuilder[ProductInterface::SKU] = 'unique-simple-azaza';
146166
/** @var Website $website */
147-
$website = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(Website::class);
148-
$website->load('second_website', 'code');
149-
150-
if (!$website->getId()) {
151-
$this->fail("Couldn`t load website");
152-
}
167+
$website = $this->loadWebsiteByCode('second_website');
153168

154169
$websitesData = [
155170
'website_ids' => [
@@ -172,12 +187,7 @@ public function testDeleteAllWebsiteAssociations()
172187
{
173188
$productBuilder[ProductInterface::SKU] = 'unique-simple-azaza';
174189
/** @var Website $website */
175-
$website = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(Website::class);
176-
$website->load('second_website', 'code');
177-
178-
if (!$website->getId()) {
179-
$this->fail("Couldn`t load website");
180-
}
190+
$website = $this->loadWebsiteByCode('second_website');
181191

182192
$websitesData = [
183193
'website_ids' => []
@@ -198,14 +208,9 @@ public function testCreateWithMultipleWebsites()
198208
$productBuilder = $this->getSimpleProductData();
199209
$productBuilder[ProductInterface::SKU] = 'test-test-sku';
200210
$productBuilder[ProductInterface::TYPE_ID] = 'simple';
201-
202211
/** @var Website $website */
203-
$website = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(Website::class);
204-
$website->load('test_website', 'code');
212+
$website = $this->loadWebsiteByCode('test_website');
205213

206-
if (!$website->getId()) {
207-
$this->fail("Couldn`t load website");
208-
}
209214
$websitesData = [
210215
'website_ids' => [
211216
1,
@@ -218,6 +223,104 @@ public function testCreateWithMultipleWebsites()
218223
$response[ProductInterface::EXTENSION_ATTRIBUTES_KEY]["website_ids"],
219224
$websitesData["website_ids"]
220225
);
226+
$this->deleteProduct($productBuilder[ProductInterface::SKU]);
227+
}
228+
229+
/**
230+
* Add product associated with website that is not associated with default store
231+
*
232+
* @magentoApiDataFixture Magento/Store/_files/second_website_with_two_stores.php
233+
*/
234+
public function testCreateWithNonDefaultStoreWebsite()
235+
{
236+
$productBuilder = $this->getSimpleProductData();
237+
$productBuilder[ProductInterface::SKU] = 'test-sku-second-site-123';
238+
$productBuilder[ProductInterface::TYPE_ID] = 'simple';
239+
/** @var Website $website */
240+
$website = $this->loadWebsiteByCode('test');
241+
242+
$websitesData = [
243+
'website_ids' => [
244+
$website->getId(),
245+
]
246+
];
247+
$productBuilder[ProductInterface::EXTENSION_ATTRIBUTES_KEY] = $websitesData;
248+
$response = $this->saveProduct($productBuilder);
249+
$this->assertEquals(
250+
$websitesData["website_ids"],
251+
$response[ProductInterface::EXTENSION_ATTRIBUTES_KEY]["website_ids"]
252+
);
253+
$this->deleteProduct($productBuilder[ProductInterface::SKU]);
254+
}
255+
256+
/**
257+
* Update product to be associated with website that is not associated with default store
258+
*
259+
* @magentoApiDataFixture Magento/Catalog/_files/product_with_two_websites.php
260+
* @magentoApiDataFixture Magento/Store/_files/second_website_with_two_stores.php
261+
*/
262+
public function testUpdateWithNonDefaultStoreWebsite()
263+
{
264+
$productBuilder[ProductInterface::SKU] = 'unique-simple-azaza';
265+
/** @var Website $website */
266+
$website = $this->loadWebsiteByCode('test');
267+
268+
$this->assertNotContains(Store::SCOPE_DEFAULT, $website->getStoreCodes());
269+
270+
$websitesData = [
271+
'website_ids' => [
272+
$website->getId(),
273+
]
274+
];
275+
$productBuilder[ProductInterface::EXTENSION_ATTRIBUTES_KEY] = $websitesData;
276+
$response = $this->updateProduct($productBuilder);
277+
$this->assertEquals(
278+
$websitesData["website_ids"],
279+
$response[ProductInterface::EXTENSION_ATTRIBUTES_KEY]["website_ids"]
280+
);
281+
}
282+
283+
/**
284+
* Update product to be associated with no websites
285+
*
286+
* @magentoApiDataFixture Magento/Catalog/_files/product_with_two_websites.php
287+
*/
288+
public function testUpdateWithNoWebsite()
289+
{
290+
$productBuilder[ProductInterface::SKU] = 'unique-simple-azaza';
291+
292+
$websitesData = [
293+
'website_ids' => []
294+
];
295+
$productBuilder[ProductInterface::EXTENSION_ATTRIBUTES_KEY] = $websitesData;
296+
$response = $this->updateProduct($productBuilder);
297+
$this->assertEquals(
298+
$websitesData["website_ids"],
299+
$response[ProductInterface::EXTENSION_ATTRIBUTES_KEY]["website_ids"]
300+
);
301+
}
302+
303+
/**
304+
* Update product without specifying websites
305+
*
306+
* @magentoApiDataFixture Magento/Catalog/_files/product_with_two_websites.php
307+
*/
308+
public function testUpdateWithoutWebsiteIds()
309+
{
310+
$productBuilder[ProductInterface::SKU] = 'unique-simple-azaza';
311+
$originalProduct = $this->getProduct($productBuilder[ProductInterface::SKU]);
312+
$newName = 'Updated Product';
313+
314+
$productBuilder[ProductInterface::NAME] = $newName;
315+
$response = $this->updateProduct($productBuilder);
316+
$this->assertEquals(
317+
$newName,
318+
$response[ProductInterface::NAME]
319+
);
320+
$this->assertEquals(
321+
$originalProduct[ProductInterface::EXTENSION_ATTRIBUTES_KEY]["website_ids"],
322+
$response[ProductInterface::EXTENSION_ATTRIBUTES_KEY]["website_ids"]
323+
);
221324
}
222325

223326
/**
@@ -727,8 +830,7 @@ public function testGetList()
727830
*/
728831
public function testGetListWithFilteringByWebsite()
729832
{
730-
$website = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(Website::class);
731-
$website->load('test', 'code');
833+
$website = $this->loadWebsiteByCode('test');
732834
$searchCriteria = [
733835
'searchCriteria' => [
734836
'filter_groups' => [

0 commit comments

Comments
 (0)