Skip to content

Commit d6e06e2

Browse files
authored
ENGCOM-4778: Added test coverage for Adding virtual product to cart functionality #617
2 parents 646e9dc + 16abb13 commit d6e06e2

File tree

4 files changed

+393
-0
lines changed

4 files changed

+393
-0
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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\Quote\Customer;
9+
10+
use Magento\Framework\Exception\AuthenticationException;
11+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
12+
use Magento\Integration\Api\CustomerTokenServiceInterface;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\TestCase\GraphQlAbstract;
15+
16+
/**
17+
* Test adding virtual product to Cart
18+
*/
19+
class AddVirtualProductToCartTest extends GraphQlAbstract
20+
{
21+
/**
22+
* @var CustomerTokenServiceInterface
23+
*/
24+
private $customerTokenService;
25+
26+
/**
27+
* @var GetMaskedQuoteIdByReservedOrderId
28+
*/
29+
private $getMaskedQuoteIdByReservedOrderId;
30+
31+
protected function setUp()
32+
{
33+
$objectManager = Bootstrap::getObjectManager();
34+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
35+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
36+
}
37+
38+
/**
39+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
40+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php
41+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
42+
*/
43+
public function testAddVirtualProductToCart()
44+
{
45+
$sku = 'virtual_product';
46+
$qty = 2;
47+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
48+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
49+
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
50+
51+
self::assertArrayHasKey('cart', $response['addVirtualProductsToCart']);
52+
self::assertEquals($qty, $response['addVirtualProductsToCart']['cart']['items'][0]['qty']);
53+
self::assertEquals($sku, $response['addVirtualProductsToCart']['cart']['items'][0]['product']['sku']);
54+
}
55+
56+
/**
57+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
58+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php
59+
*
60+
* @expectedException \Exception
61+
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id"
62+
*/
63+
public function testAddVirtualToNonExistentCart()
64+
{
65+
$sku = 'virtual_product';
66+
$qty = 2;
67+
$nonExistentMaskedQuoteId = 'non_existent_masked_id';
68+
69+
$query = $this->getQuery($nonExistentMaskedQuoteId, $sku, $qty);
70+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
71+
}
72+
73+
/**
74+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
75+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
76+
*
77+
* @expectedException \Exception
78+
* @expectedExceptionMessage Could not find a product with SKU "virtual_product"
79+
*/
80+
public function testNonExistentProductToCart()
81+
{
82+
$sku = 'virtual_product';
83+
$qty = 2;
84+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
85+
86+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
87+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
88+
}
89+
90+
/**
91+
* _security
92+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
93+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php
94+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
95+
*/
96+
public function testAddVirtualProductToGuestCart()
97+
{
98+
$sku = 'virtual_product';
99+
$qty = 2;
100+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
101+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
102+
103+
$this->expectExceptionMessage(
104+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
105+
);
106+
107+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
108+
}
109+
110+
/**
111+
* _security
112+
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
113+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php
114+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
115+
*/
116+
public function testAddVirtualProductToAnotherCustomerCart()
117+
{
118+
$sku = 'virtual_product';
119+
$qty = 2;
120+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
121+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
122+
123+
$this->expectExceptionMessage(
124+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
125+
);
126+
127+
$this->graphQlMutation($query, [], '', $this->getHeaderMap('customer2@search.example.com'));
128+
}
129+
130+
/**
131+
* @param string $maskedQuoteId
132+
* @param string $sku
133+
* @param int $qty
134+
* @return string
135+
*/
136+
private function getQuery(string $maskedQuoteId, string $sku, int $qty): string
137+
{
138+
return <<<QUERY
139+
mutation {
140+
addVirtualProductsToCart(input: {
141+
cart_id: "{$maskedQuoteId}",
142+
cartItems: [
143+
{
144+
data: {
145+
qty: {$qty}
146+
sku: "{$sku}"
147+
}
148+
}
149+
]
150+
}) {
151+
cart {
152+
items {
153+
id
154+
qty
155+
product {
156+
sku
157+
}
158+
}
159+
}
160+
}
161+
}
162+
QUERY;
163+
}
164+
165+
/**
166+
* Retrieve customer authorization headers
167+
*
168+
* @param string $username
169+
* @param string $password
170+
* @return array
171+
* @throws AuthenticationException
172+
*/
173+
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
174+
{
175+
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
176+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
177+
return $headerMap;
178+
}
179+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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\Quote\Guest;
9+
10+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\TestFramework\TestCase\GraphQlAbstract;
13+
14+
/**
15+
* Add virtual product to cart testcases
16+
*/
17+
class AddVirtualProductToCartTest extends GraphQlAbstract
18+
{
19+
/**
20+
* @var GetMaskedQuoteIdByReservedOrderId
21+
*/
22+
private $getMaskedQuoteIdByReservedOrderId;
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
protected function setUp()
28+
{
29+
$objectManager = Bootstrap::getObjectManager();
30+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
31+
}
32+
33+
/**
34+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php
35+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
36+
*/
37+
public function testAddVirtualProductToCart()
38+
{
39+
$sku = 'virtual_product';
40+
$qty = 2;
41+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
42+
43+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
44+
$response = $this->graphQlMutation($query);
45+
46+
self::assertArrayHasKey('cart', $response['addVirtualProductsToCart']);
47+
self::assertEquals($qty, $response['addVirtualProductsToCart']['cart']['items'][0]['qty']);
48+
self::assertEquals($sku, $response['addVirtualProductsToCart']['cart']['items'][0]['product']['sku']);
49+
}
50+
51+
/**
52+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php
53+
*
54+
* @expectedException \Exception
55+
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id"
56+
*/
57+
public function testAddVirtualToNonExistentCart()
58+
{
59+
$sku = 'virtual_product';
60+
$qty = 1;
61+
$maskedQuoteId = 'non_existent_masked_id';
62+
63+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
64+
$this->graphQlMutation($query);
65+
}
66+
67+
/**
68+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
69+
*
70+
* @expectedException \Exception
71+
* @expectedExceptionMessage Could not find a product with SKU "virtual_product"
72+
*/
73+
public function testNonExistentProductToCart()
74+
{
75+
$sku = 'virtual_product';
76+
$qty = 1;
77+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
78+
79+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
80+
$this->graphQlMutation($query);
81+
}
82+
83+
/**
84+
* _security
85+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/virtual_product.php
86+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
87+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
88+
*/
89+
public function testAddVirtualProductToCustomerCart()
90+
{
91+
$sku = 'virtual_product';
92+
$qty = 2;
93+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
94+
$query = $this->getQuery($maskedQuoteId, $sku, $qty);
95+
96+
$this->expectExceptionMessage(
97+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
98+
);
99+
100+
$this->graphQlMutation($query);
101+
}
102+
103+
/**
104+
* @param string $maskedQuoteId
105+
* @param string $sku
106+
* @param int $qty
107+
* @return string
108+
*/
109+
private function getQuery(string $maskedQuoteId, string $sku, int $qty): string
110+
{
111+
return <<<QUERY
112+
mutation {
113+
addVirtualProductsToCart(
114+
input: {
115+
cart_id: "{$maskedQuoteId}"
116+
cartItems: [
117+
{
118+
data: {
119+
qty: {$qty}
120+
sku: "{$sku}"
121+
}
122+
}
123+
]
124+
}
125+
) {
126+
cart {
127+
items {
128+
qty
129+
product {
130+
sku
131+
}
132+
}
133+
}
134+
}
135+
}
136+
QUERY;
137+
}
138+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
use Magento\Catalog\Api\Data\ProductInterface;
9+
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
12+
use Magento\Catalog\Model\Product\Type;
13+
use Magento\Catalog\Model\Product\Visibility;
14+
use Magento\Framework\Api\DataObjectHelper;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
17+
$objectManager = Bootstrap::getObjectManager();
18+
/** @var ProductInterfaceFactory $productFactory */
19+
$productFactory = $objectManager->get(ProductInterfaceFactory::class);
20+
/** @var DataObjectHelper $dataObjectHelper */
21+
$dataObjectHelper = Bootstrap::getObjectManager()->get(DataObjectHelper::class);
22+
/** @var ProductRepositoryInterface $productRepository */
23+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
24+
25+
$product = $productFactory->create();
26+
$productData = [
27+
ProductInterface::TYPE_ID => Type::TYPE_VIRTUAL,
28+
ProductInterface::ATTRIBUTE_SET_ID => 4,
29+
ProductInterface::SKU => 'virtual_product',
30+
ProductInterface::NAME => 'Virtual Product',
31+
ProductInterface::PRICE => 10,
32+
ProductInterface::VISIBILITY => Visibility::VISIBILITY_BOTH,
33+
ProductInterface::STATUS => Status::STATUS_ENABLED,
34+
];
35+
$dataObjectHelper->populateWithArray($product, $productData, ProductInterface::class);
36+
/** Out of interface */
37+
$product
38+
->setWebsiteIds([1])
39+
->setStockData([
40+
'qty' => 85.5,
41+
'is_in_stock' => true,
42+
'manage_stock' => true,
43+
'is_qty_decimal' => true
44+
]);
45+
$productRepository->save($product);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\Framework\Registry;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
12+
$objectManager = Bootstrap::getObjectManager();
13+
/** @var ProductRepositoryInterface $productRepository */
14+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
15+
/** @var Registry $registry */
16+
$registry = $objectManager->get(Registry::class);
17+
18+
$currentArea = $registry->registry('isSecureArea');
19+
$registry->unregister('isSecureArea');
20+
$registry->register('isSecureArea', true);
21+
22+
try {
23+
$productRepository->deleteById('virtual_product');
24+
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
25+
/**
26+
* Tests which are wrapped with MySQL transaction clear all data by transaction rollback.
27+
*/
28+
}
29+
30+
$registry->unregister('isSecureArea');
31+
$registry->register('isSecureArea', $currentArea);

0 commit comments

Comments
 (0)