Skip to content

Commit b3d1c48

Browse files
authored
ENGCOM-4391: Fix message when maxSaleQty is set and qty is more than maxSaleQty #367
2 parents 21c15c9 + 0b9d27b commit b3d1c48

File tree

2 files changed

+161
-17
lines changed

2 files changed

+161
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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\GraphQl\CatalogInventory;
9+
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\TestFramework\TestCase\GraphQlAbstract;
12+
use Magento\Quote\Model\QuoteFactory;
13+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
14+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
15+
16+
class AddProductToCartTest extends GraphQlAbstract
17+
{
18+
/**
19+
* @var QuoteResource
20+
*/
21+
private $quoteResource;
22+
23+
/**
24+
* @var QuoteFactory
25+
*/
26+
private $quoteFactory;
27+
28+
/**
29+
* @var QuoteIdToMaskedQuoteIdInterface
30+
*/
31+
private $quoteIdToMaskedId;
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
protected function setUp()
37+
{
38+
$objectManager = Bootstrap::getObjectManager();
39+
$this->quoteResource = $objectManager->get(QuoteResource::class);
40+
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
41+
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
42+
}
43+
44+
/**
45+
* @magentoApiDataFixture Magento/Catalog/_files/products.php
46+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
47+
* @expectedException \Exception
48+
* @expectedExceptionMessage The requested qty is not available
49+
*/
50+
public function testAddProductIfQuantityIsNotAvailable()
51+
{
52+
$sku = 'simple';
53+
$qty = 200;
54+
55+
$maskedQuoteId = $this->getMaskedQuoteId();
56+
$query = $this->getAddSimpleProductQuery($maskedQuoteId, $sku, $qty);
57+
$this->graphQlQuery($query);
58+
self::fail('Should be "The requested qty is not available" error message.');
59+
}
60+
61+
/**
62+
* @magentoApiDataFixture Magento/Catalog/_files/products.php
63+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
64+
* @magentoConfigFixture default cataloginventory/item_options/max_sale_qty 5
65+
* @expectedException \Exception
66+
* @expectedExceptionMessage The most you may purchase is 5.
67+
*/
68+
public function testAddMoreProductsThatAllowed()
69+
{
70+
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/167');
71+
72+
$sku = 'custom-design-simple-product';
73+
$qty = 7;
74+
75+
$maskedQuoteId = $this->getMaskedQuoteId();
76+
$query = $this->getAddSimpleProductQuery($maskedQuoteId, $sku, $qty);
77+
$this->graphQlQuery($query);
78+
self::fail('Should be "The most you may purchase is 5." error message.');
79+
}
80+
81+
/**
82+
* @return string
83+
*/
84+
public function getMaskedQuoteId() : string
85+
{
86+
$quote = $this->quoteFactory->create();
87+
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
88+
89+
return $this->quoteIdToMaskedId->execute((int)$quote->getId());
90+
}
91+
92+
/**
93+
* @param string $maskedQuoteId
94+
* @param string $sku
95+
* @param int $qty
96+
* @return string
97+
*/
98+
public function getAddSimpleProductQuery(string $maskedQuoteId, string $sku, int $qty) : string
99+
{
100+
return <<<QUERY
101+
mutation {
102+
addSimpleProductsToCart(
103+
input: {
104+
cart_id: "{$maskedQuoteId}",
105+
cartItems: [
106+
{
107+
data: {
108+
qty: $qty
109+
sku: "$sku"
110+
}
111+
}
112+
]
113+
}
114+
) {
115+
cart {
116+
items {
117+
qty
118+
}
119+
}
120+
}
121+
}
122+
QUERY;
123+
}
124+
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddSimpleProductToCartTest.php

+37-17
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
use Magento\TestFramework\Helper\Bootstrap;
1111
use Magento\TestFramework\TestCase\GraphQlAbstract;
12-
use Magento\Quote\Model\Quote;
12+
use Magento\Quote\Model\QuoteFactory;
1313
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
1414
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
1515

@@ -21,9 +21,9 @@ class AddSimpleProductToCartTest extends GraphQlAbstract
2121
private $quoteResource;
2222

2323
/**
24-
* @var Quote
24+
* @var QuoteFactory
2525
*/
26-
private $quote;
26+
private $quoteFactory;
2727

2828
/**
2929
* @var QuoteIdToMaskedQuoteIdInterface
@@ -37,29 +37,48 @@ protected function setUp()
3737
{
3838
$objectManager = Bootstrap::getObjectManager();
3939
$this->quoteResource = $objectManager->get(QuoteResource::class);
40-
$this->quote = $objectManager->create(Quote::class);
40+
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
4141
$this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
4242
}
4343

4444
/**
4545
* @magentoApiDataFixture Magento/Catalog/_files/products.php
4646
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
47-
* @expectedException \Exception
48-
* @expectedExceptionMessage The requested qty is not available
4947
*/
50-
public function testAddProductIfQuantityIsNotAvailable()
48+
public function testAddSimpleProductToCart()
5149
{
5250
$sku = 'simple';
53-
$qty = 200;
51+
$qty = 2;
52+
$maskedQuoteId = $this->getMaskedQuoteId();
5453

55-
$this->quoteResource->load(
56-
$this->quote,
57-
'test_order_1',
58-
'reserved_order_id'
59-
);
60-
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
54+
$query = $this->getAddSimpleProductQuery($maskedQuoteId, $sku, $qty);
55+
$response = $this->graphQlQuery($query);
56+
self::assertArrayHasKey('cart', $response['addSimpleProductsToCart']);
6157

62-
$query = <<<QUERY
58+
self::assertEquals($qty, $response['addSimpleProductsToCart']['cart']['items'][0]['qty']);
59+
self::assertEquals($sku, $response['addSimpleProductsToCart']['cart']['items'][0]['product']['sku']);
60+
}
61+
62+
/**
63+
* @return string
64+
*/
65+
public function getMaskedQuoteId() : string
66+
{
67+
$quote = $this->quoteFactory->create();
68+
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
69+
70+
return $this->quoteIdToMaskedId->execute((int)$quote->getId());
71+
}
72+
73+
/**
74+
* @param string $maskedQuoteId
75+
* @param string $sku
76+
* @param int $qty
77+
* @return string
78+
*/
79+
public function getAddSimpleProductQuery(string $maskedQuoteId, string $sku, int $qty): string
80+
{
81+
return <<<QUERY
6382
mutation {
6483
addSimpleProductsToCart(
6584
input: {
@@ -77,12 +96,13 @@ public function testAddProductIfQuantityIsNotAvailable()
7796
cart {
7897
items {
7998
qty
99+
product {
100+
sku
101+
}
80102
}
81103
}
82104
}
83105
}
84106
QUERY;
85-
86-
$this->graphQlQuery($query);
87107
}
88108
}

0 commit comments

Comments
 (0)