Skip to content

Commit e1d5544

Browse files
author
Vitaliy Boyko
committed
graphQl-427: adjusted code coverage of set shipping address feature
1 parent 75cf826 commit e1d5544

File tree

2 files changed

+496
-141
lines changed

2 files changed

+496
-141
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/SetShippingAddressOnCartTest.php renamed to dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php

Lines changed: 54 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\GraphQl\Quote;
8+
namespace Magento\GraphQl\Quote\Customer;
99

10-
use Magento\Framework\App\Config\ScopeConfigInterface;
1110
use Magento\Integration\Api\CustomerTokenServiceInterface;
12-
use Magento\Multishipping\Helper\Data;
1311
use Magento\Quote\Model\QuoteFactory;
1412
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
1513
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
1614
use Magento\TestFramework\Helper\Bootstrap;
1715
use Magento\TestFramework\TestCase\GraphQlAbstract;
18-
use Magento\TestFramework\ObjectManager;
1916

2017
/**
2118
* Test for set shipping addresses on cart mutation
@@ -53,10 +50,11 @@ protected function setUp()
5350

5451
/**
5552
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
53+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
5654
*/
57-
public function testSetNewShippingAddressByGuest()
55+
public function testSetNewShippingAddress()
5856
{
59-
$maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address');
57+
$maskedQuoteId = $this->assignQuoteToCustomer();
6058

6159
$query = <<<QUERY
6260
mutation {
@@ -91,16 +89,16 @@ public function testSetNewShippingAddressByGuest()
9189
postcode
9290
telephone
9391
country {
94-
code
9592
label
93+
code
9694
}
9795
address_type
9896
}
9997
}
10098
}
10199
}
102100
QUERY;
103-
$response = $this->graphQlQuery($query);
101+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
104102

105103
self::assertArrayHasKey('cart', $response['setShippingAddressesOnCart']);
106104
$cartResponse = $response['setShippingAddressesOnCart']['cart'];
@@ -111,12 +109,15 @@ public function testSetNewShippingAddressByGuest()
111109

112110
/**
113111
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
114-
* @expectedException \Exception
115-
* @expectedExceptionMessage The current customer isn't authorized.
112+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
113+
* @dataProvider requestWithoutRequiredParamsDataProvider
114+
* @param string $params
115+
* @param string $expectedException
116+
* @throws \Exception
116117
*/
117-
public function testSetShippingAddressFromAddressBookByGuest()
118+
public function testSetNewShippingAddressWithEmptyRequiredParams(string $params, string $expectedException)
118119
{
119-
$maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address');
120+
$maskedQuoteId = $this->assignQuoteToCustomer();
120121

121122
$query = <<<QUERY
122123
mutation {
@@ -125,7 +126,9 @@ public function testSetShippingAddressFromAddressBookByGuest()
125126
cart_id: "$maskedQuoteId"
126127
shipping_addresses: [
127128
{
128-
customer_address_id: 1
129+
address: {
130+
$params
131+
}
129132
}
130133
]
131134
}
@@ -138,16 +141,21 @@ public function testSetShippingAddressFromAddressBookByGuest()
138141
}
139142
}
140143
QUERY;
141-
$this->graphQlQuery($query);
144+
$this->expectExceptionMessage(
145+
$expectedException
146+
);
147+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
142148
}
143149

144150
/**
145-
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
151+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
146152
* @magentoApiDataFixture Magento/Customer/_files/customer.php
153+
* @expectedException \Exception
154+
* @expectedExceptionMessage The Cart includes virtual product(s) only, so a shipping address is not used.
147155
*/
148-
public function testSetNewShippingAddressByRegisteredCustomer()
156+
public function testSetNewShippingAddressOnQuoteWithVirtualProducts()
149157
{
150-
$maskedQuoteId = $this->assignQuoteToCustomer();
158+
$maskedQuoteId = $this->assignQuoteToCustomer('test_order_with_virtual_product_without_address');
151159

152160
$query = <<<QUERY
153161
mutation {
@@ -174,38 +182,21 @@ public function testSetNewShippingAddressByRegisteredCustomer()
174182
) {
175183
cart {
176184
shipping_addresses {
177-
firstname
178-
lastname
179-
company
180-
street
181185
city
182-
postcode
183-
telephone
184-
country {
185-
label
186-
code
187-
}
188-
address_type
189186
}
190187
}
191188
}
192189
}
193190
QUERY;
194-
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
195-
196-
self::assertArrayHasKey('cart', $response['setShippingAddressesOnCart']);
197-
$cartResponse = $response['setShippingAddressesOnCart']['cart'];
198-
self::assertArrayHasKey('shipping_addresses', $cartResponse);
199-
$shippingAddressResponse = current($cartResponse['shipping_addresses']);
200-
$this->assertNewShippingAddressFields($shippingAddressResponse);
191+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
201192
}
202193

203194
/**
204195
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
205196
* @magentoApiDataFixture Magento/Customer/_files/customer.php
206197
* @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php
207198
*/
208-
public function testSetShippingAddressFromAddressBookByRegisteredCustomer()
199+
public function testSetShippingAddressFromAddressBook()
209200
{
210201
$maskedQuoteId = $this->assignQuoteToCustomer();
211202

@@ -277,36 +268,6 @@ public function testSetNotExistedShippingAddressFromAddressBook()
277268
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
278269
}
279270

280-
/**
281-
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
282-
* @expectedException \Exception
283-
* @expectedExceptionMessage The shipping address must contain either "customer_address_id" or "address".
284-
*/
285-
public function testSetShippingAddressWithoutAddresses()
286-
{
287-
$maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address');
288-
289-
$query = <<<QUERY
290-
mutation {
291-
setShippingAddressesOnCart(
292-
input: {
293-
cart_id: "$maskedQuoteId"
294-
shipping_addresses: [
295-
{}
296-
]
297-
}
298-
) {
299-
cart {
300-
shipping_addresses {
301-
city
302-
}
303-
}
304-
}
305-
}
306-
QUERY;
307-
$this->graphQlQuery($query);
308-
}
309-
310271
/**
311272
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
312273
* @magentoApiDataFixture Magento/Customer/_files/customer.php
@@ -355,13 +316,15 @@ public function testSetNewShippingAddressAndFromAddressBookAtSameTime()
355316
}
356317

357318
/**
319+
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
320+
* @magentoApiDataFixture Magento/Customer/_files/customer_address.php
358321
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
359322
* @expectedException \Exception
360-
* @expectedExceptionMessage You cannot specify multiple shipping addresses.
323+
* @expectedExceptionMessage The current user cannot use address with ID "1"
361324
*/
362-
public function testSetMultipleNewShippingAddresses()
325+
public function testSetShippingAddressIfCustomerIsNotOwnerOfAddress()
363326
{
364-
$maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address');
327+
$maskedQuoteId = $this->assignQuoteToCustomer('test_order_with_simple_product_without_address', 2);
365328

366329
$query = <<<QUERY
367330
mutation {
@@ -370,69 +333,32 @@ public function testSetMultipleNewShippingAddresses()
370333
cart_id: "$maskedQuoteId"
371334
shipping_addresses: [
372335
{
373-
address: {
374-
firstname: "test firstname"
375-
lastname: "test lastname"
376-
company: "test company"
377-
street: ["test street 1", "test street 2"]
378-
city: "test city"
379-
region: "test region"
380-
postcode: "887766"
381-
country_code: "US"
382-
telephone: "88776655"
383-
save_in_address_book: false
384-
}
385-
},
386-
{
387-
address: {
388-
firstname: "test firstname 2"
389-
lastname: "test lastname 2"
390-
company: "test company 2"
391-
street: ["test street 1", "test street 2"]
392-
city: "test city"
393-
region: "test region"
394-
postcode: "887766"
395-
country_code: "US"
396-
telephone: "88776655"
397-
save_in_address_book: false
398-
}
336+
customer_address_id: 1
399337
}
400338
]
401339
}
402340
) {
403341
cart {
404342
shipping_addresses {
405-
city
343+
postcode
406344
}
407345
}
408346
}
409347
}
410348
QUERY;
411-
/** @var \Magento\Config\Model\ResourceModel\Config $config */
412-
$config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class);
413-
$config->saveConfig(
414-
Data::XML_PATH_CHECKOUT_MULTIPLE_AVAILABLE,
415-
null,
416-
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
417-
0
418-
);
419-
/** @var \Magento\Framework\App\Config\ReinitableConfigInterface $config */
420-
$config = ObjectManager::getInstance()->get(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
421-
$config->reinit();
422349

423-
$this->graphQlQuery($query);
350+
$this->graphQlQuery($query, [], '', $this->getHeaderMap('customer2@search.example.com'));
424351
}
425352

426353
/**
427354
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
428355
* @magentoApiDataFixture Magento/Customer/_files/customer_address.php
429356
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
430357
* @expectedException \Exception
431-
* @expectedExceptionMessage The current user cannot use address with ID "1"
432358
*/
433-
public function testSetShippingAddressIfCustomerIsNotOwnerOfAddress()
359+
public function testSetShippingAddressIfCustomerIsNotOwnerOfCart()
434360
{
435-
$maskedQuoteId = $this->assignQuoteToCustomer('test_order_with_simple_product_without_address', 2);
361+
$maskedQuoteId = $this->assignQuoteToCustomer('test_order_with_simple_product_without_address', 1);
436362

437363
$query = <<<QUERY
438364
mutation {
@@ -454,10 +380,27 @@ public function testSetShippingAddressIfCustomerIsNotOwnerOfAddress()
454380
}
455381
}
456382
QUERY;
383+
$this->expectExceptionMessage(
384+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
385+
);
457386

458387
$this->graphQlQuery($query, [], '', $this->getHeaderMap('customer2@search.example.com'));
459388
}
460389

390+
/**
391+
* TODO: currently only the city param is required, do we need to add at least ZIP code?
392+
* @return array
393+
*/
394+
public function requestWithoutRequiredParamsDataProvider()
395+
{
396+
return [
397+
[
398+
'save_in_address_book: false',
399+
'Field CartAddressInput.city of required type String! was not provided'
400+
]
401+
];
402+
}
403+
461404
/**
462405
* Verify the all the whitelisted fields for a New Address Object
463406
*
@@ -512,18 +455,6 @@ private function getHeaderMap(string $username = 'customer@example.com', string
512455
return $headerMap;
513456
}
514457

515-
/**
516-
* @param string $reversedQuoteId
517-
* @return string
518-
*/
519-
private function getMaskedQuoteIdByReversedQuoteId(string $reversedQuoteId): string
520-
{
521-
$quote = $this->quoteFactory->create();
522-
$this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id');
523-
524-
return $this->quoteIdToMaskedId->execute((int)$quote->getId());
525-
}
526-
527458
/**
528459
* @param string $reversedQuoteId
529460
* @param int $customerId
@@ -539,22 +470,4 @@ private function assignQuoteToCustomer(
539470
$this->quoteResource->save($quote);
540471
return $this->quoteIdToMaskedId->execute((int)$quote->getId());
541472
}
542-
543-
public function tearDown()
544-
{
545-
/** @var \Magento\Config\Model\ResourceModel\Config $config */
546-
$config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class);
547-
548-
//default state of multishipping config
549-
$config->saveConfig(
550-
Data::XML_PATH_CHECKOUT_MULTIPLE_AVAILABLE,
551-
1,
552-
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
553-
0
554-
);
555-
556-
/** @var \Magento\Framework\App\Config\ReinitableConfigInterface $config */
557-
$config = ObjectManager::getInstance()->get(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
558-
$config->reinit();
559-
}
560473
}

0 commit comments

Comments
 (0)