Skip to content

Commit 67974e8

Browse files
committed
Merge branch 'MC-43189' of https://github.com/magento-l3/magento2ce into L3-PR-20210908
2 parents 7c36113 + 0f6513f commit 67974e8

File tree

5 files changed

+82
-39
lines changed

5 files changed

+82
-39
lines changed

app/code/Magento/ConfigurableProductGraphQl/Model/Cart/BuyRequest/SuperAttributeDataProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ public function execute(array $cartItemData): array
9292
} catch (NoSuchEntityException $e) {
9393
throw new GraphQlNoSuchEntityException(__('Could not find specified product.'));
9494
}
95+
if (!in_array($cart->getStore()->getWebsiteId(), $product->getWebsiteIds())) {
96+
throw new GraphQlNoSuchEntityException(__('Could not find specified product.'));
97+
}
9598

9699
$this->checkProductStock($sku, (float) $qty, (int) $cart->getStore()->getWebsiteId());
97100

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
[

app/code/Magento/DownloadableGraphQl/Model/Cart/BuyRequest/DownloadableLinksDataProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ public function execute(array $cartItemData): array
4040

4141
if (isset($cartItemData['data']) && isset($cartItemData['data']['sku'])) {
4242
$sku = $cartItemData['data']['sku'];
43-
$product = $this->productRepository->get($sku);
43+
try {
44+
$product = $this->productRepository->get($sku);
45+
} catch (NoSuchEntityException $e) {
46+
throw new GraphQlNoSuchEntityException(__('Could not find specified product.'));
47+
}
4448

4549
if ($product->getLinksPurchasedSeparately() && isset($cartItemData['downloadable_product_links'])) {
4650
$downloadableLinks = $cartItemData['downloadable_product_links'];

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ public function testAddMultipleConfigurableProductToCart()
8383
$parentSku = $product['sku'];
8484
$skuOne = 'simple_10';
8585
$skuTwo = 'simple_20';
86-
$valueIdOne = $product['configurable_options'][0]['values'][0]['value_index'];
8786

8887
$query = <<<QUERY
8988
mutation {
@@ -251,7 +250,6 @@ public function testAddNonExistentConfigurableProductParentToCart()
251250

252251
/**
253252
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable_sku.php
254-
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
255253
* @magentoApiDataFixture Magento/Checkout/_files/active_quote_not_default_website.php
256254
*/
257255
public function testAddConfigurableProductNotAssignedToWebsite()
@@ -291,13 +289,33 @@ public function testAddNonExistentConfigurableProductVariationToCart()
291289

292290
$this->expectException(Exception::class);
293291
$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.'
292+
'Could not add the product with SKU configurable to the shopping cart: Could not find specified product.'
296293
);
297294

298295
$this->graphQlMutation($query);
299296
}
300297

298+
/**
299+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_product_only_parent_two_websites.php
300+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote_not_default_website.php
301+
*/
302+
public function testAddUnassignedToWebsiteConfigurableProductVariationToCart()
303+
{
304+
$this->expectException(Exception::class);
305+
$this->expectExceptionMessage(
306+
'Could not add the product with SKU configurable to the shopping cart: Could not find specified product.'
307+
);
308+
309+
$reservedOrderId = 'test_order_2';
310+
$parentSku = 'configurable';
311+
$sku = 'simple_20';
312+
$headerMap = ['Store' => 'fixture_second_store'];
313+
314+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);
315+
$query = $this->getQuery($maskedQuoteId, $parentSku, $sku, 1);
316+
$this->graphQlMutation($query, [], '', $headerMap);
317+
}
318+
301319
/**
302320
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable_disable_first_child.php
303321
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
@@ -604,8 +622,12 @@ private function getQuery(string $maskedQuoteId, string $parentSku, string $sku,
604622
* @param int $quantity
605623
* @return string
606624
*/
607-
private function graphQlQueryForVariant(string $maskedQuoteId, string $parentSku, string $sku, int $quantity): string
608-
{
625+
private function graphQlQueryForVariant(
626+
string $maskedQuoteId,
627+
string $parentSku,
628+
string $sku,
629+
int $quantity
630+
): string {
609631
return <<<QUERY
610632
mutation {
611633
addConfigurableProductsToCart(
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)