Skip to content

Commit 02bd35d

Browse files
committed
MC-43189: [Graphql] Swatch Configurable option can be added to cart on any website
1 parent 61c33ec commit 02bd35d

File tree

3 files changed

+68
-35
lines changed

3 files changed

+68
-35
lines changed

app/code/Magento/ConfigurableProductGraphQl/Test/Unit/Model/Cart/BuyRequest/SuperAttributeDataProviderTest.php

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77

88
namespace Magento\ConfigurableProductGraphQl\Test\Unit\Model\Cart\BuyRequest;
99

10+
use Magento\Catalog\Api\Data\ProductInterface;
1011
use Magento\Catalog\Api\ProductRepositoryInterface;
1112
use Magento\Catalog\Model\Product;
1213
use Magento\CatalogInventory\Api\StockStateInterface;
1314
use Magento\ConfigurableProductGraphQl\Model\Cart\BuyRequest\SuperAttributeDataProvider;
1415
use Magento\ConfigurableProductGraphQl\Model\Options\Collection as OptionCollection;
16+
use Magento\Framework\EntityManager\EntityMetadataInterface;
1517
use Magento\Framework\EntityManager\MetadataPool;
1618
use Magento\Framework\Stdlib\ArrayManager;
1719
use Magento\Quote\Model\Quote;
18-
use Magento\Store\Api\Data\StoreInterface;
20+
use Magento\Store\Model\Store;
1921
use PHPUnit\Framework\MockObject\MockObject;
2022
use PHPUnit\Framework\TestCase;
2123

@@ -50,7 +52,7 @@ class SuperAttributeDataProviderTest extends TestCase
5052
private $stockState;
5153

5254
/**
53-
* @var SuperAttributeDataProvider|MockObject
55+
* @var SuperAttributeDataProvider
5456
*/
5557
private $superAttributeDataProvider;
5658

@@ -59,22 +61,11 @@ class SuperAttributeDataProviderTest extends TestCase
5961
*/
6062
protected function setUp(): void
6163
{
62-
$this->arrayManager = $this->getMockBuilder(ArrayManager::class)
63-
->disableOriginalConstructor()
64-
->getMock();
65-
$this->productRepository = $this->getMockBuilder(ProductRepositoryInterface::class)
66-
->disableOriginalConstructor()
67-
->getMockForAbstractClass();
64+
$this->arrayManager = $this->createMock(ArrayManager::class);
65+
$this->productRepository = $this->createMock(ProductRepositoryInterface::class);
6866
$this->optionCollection = $this->createMock(OptionCollection::class);
69-
$this->metadataPool = $this->getMockBuilder(MetadataPool::class)
70-
->disableOriginalConstructor()
71-
->onlyMethods(['getMetadata'])
72-
->addMethods(['getLinkField'])
73-
->getMock();
74-
$this->stockState = $this->getMockBuilder(StockStateInterface::class)
75-
->disableOriginalConstructor()
76-
->addMethods(['getHasError'])
77-
->getMockForAbstractClass();
67+
$this->metadataPool = $this->createMock(MetadataPool::class);
68+
$this->stockState = $this->createMock(StockStateInterface::class);
7869

7970
$this->superAttributeDataProvider = new SuperAttributeDataProvider(
8071
$this->arrayManager,
@@ -90,9 +81,7 @@ protected function setUp(): void
9081
*/
9182
public function testExecute(): void
9283
{
93-
$quoteMock = $this->getMockBuilder(Quote::class)
94-
->disableOriginalConstructor()
95-
->getMock();
84+
$quoteMock = $this->createMock(Quote::class);
9685
$cartItemData = [
9786
'data' => [
9887
'quantity' => 2.0,
@@ -116,19 +105,19 @@ public function testExecute(): void
116105
$quoteMock,
117106
);
118107

119-
$storeMock = $this->getMockBuilder(StoreInterface::class)
120-
->disableOriginalConstructor()
121-
->addMethods(['getWebsite'])
122-
->getMockForAbstractClass();
123-
$storeMock->expects($this->once())->method('getWebsiteId')->willReturn(1);
108+
$websiteId = 1;
109+
$storeMock = $this->createMock(Store::class);
110+
$storeMock->expects($this->atLeastOnce())
111+
->method('getWebsiteId')
112+
->willReturn($websiteId);
124113
$storeMock->expects($this->never())->method('getWebsite');
125114
$quoteMock->expects($this->atLeastOnce())
126115
->method('getStore')
127116
->willReturn($storeMock);
128117

129118
$productMock = $this->getMockBuilder(Product::class)
130119
->disableOriginalConstructor()
131-
->onlyMethods(['getId', 'getExtensionAttributes', 'getData'])
120+
->onlyMethods(['getId', 'getExtensionAttributes', 'getData', 'getWebsiteIds'])
132121
->addMethods(['getConfigurableProductLinks'])
133122
->getMock();
134123
$productMock->method('getId')
@@ -139,16 +128,20 @@ public function testExecute(): void
139128
->willReturn([1]);
140129
$productMock->method('getData')
141130
->willReturn(1);
131+
$productMock->method('getWebsiteIds')
132+
->willReturn([$websiteId]);
142133
$this->productRepository->method('get')
143134
->willReturn($productMock);
135+
$checkResult = new \Magento\Framework\DataObject();
136+
$checkResult->setHasError(false);
144137
$this->stockState->method('checkQuoteItemQty')
145-
->willReturnSelf();
146-
$this->stockState->method('getHasError')
147-
->willReturn(false);
138+
->willReturn($checkResult);
139+
$productMetadata = $this->createMock(EntityMetadataInterface::class);
140+
$productMetadata->method('getLinkField')
141+
->willReturn('entity_id');
148142
$this->metadataPool->method('getMetadata')
149-
->willReturnSelf();
150-
$this->metadataPool->method('getLinkField')
151-
->willReturnSelf();
143+
->with(ProductInterface::class)
144+
->willReturn($productMetadata);
152145
$this->optionCollection->method('getAttributesByProductId')
153146
->willReturn([
154147
[

dev/tests/api-functional/testsuite/Magento/GraphQl/ConfigurableProduct/AddConfigurableProductToCartTest.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ public function testAddNonExistentConfigurableProductParentToCart()
251251

252252
/**
253253
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable_sku.php
254-
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
255254
* @magentoApiDataFixture Magento/Checkout/_files/active_quote_not_default_website.php
256255
*/
257256
public function testAddConfigurableProductNotAssignedToWebsite()
@@ -291,13 +290,33 @@ public function testAddNonExistentConfigurableProductVariationToCart()
291290

292291
$this->expectException(Exception::class);
293292
$this->expectExceptionMessage(
294-
'Could not add the product with SKU configurable to the shopping cart: The product that was requested ' .
295-
'doesn\'t exist. Verify the product and try again.'
293+
'Could not add the product with SKU configurable to the shopping cart: Could not find specified product.'
296294
);
297295

298296
$this->graphQlMutation($query);
299297
}
300298

299+
/**
300+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_product_only_parent_two_websites.php
301+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote_not_default_website.php
302+
*/
303+
public function testAddUnassignedToWebsiteConfigurableProductVariationToCart()
304+
{
305+
$this->expectException(Exception::class);
306+
$this->expectExceptionMessage(
307+
'Could not add the product with SKU configurable to the shopping cart: Could not find specified product.'
308+
);
309+
310+
$reservedOrderId = 'test_order_2';
311+
$parentSku = 'configurable';
312+
$sku = 'simple_20';
313+
$headerMap = ['Store' => 'fixture_second_store'];
314+
315+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
316+
$query = $this->getQuery($maskedQuoteId, $parentSku, $sku, 1);
317+
$this->graphQlMutation($query, [], '', $headerMap);
318+
}
319+
301320
/**
302321
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable_disable_first_child.php
303322
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Catalog\Api\ProductRepositoryInterface;
8+
use Magento\Store\Api\WebsiteRepositoryInterface;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
11+
12+
Resolver::getInstance()->requireDataFixture('Magento/ConfigurableProduct/_files/product_configurable_sku.php');
13+
Resolver::getInstance()->requireDataFixture('Magento/Store/_files/second_website_with_two_stores.php');
14+
15+
$websiteRepository = Bootstrap::getObjectManager()->get(WebsiteRepositoryInterface::class);
16+
$secondWebsite = $websiteRepository->get('test');
17+
18+
$productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
19+
$configurableProduct = $productRepository->get('configurable');
20+
$configurableProduct->setWebsiteIds(array_merge($configurableProduct->getWebsiteIds(), [$secondWebsite->getId()]));
21+
$productRepository->save($configurableProduct);

0 commit comments

Comments
 (0)