Skip to content

Commit d81b399

Browse files
authored
Merge pull request #4025 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
2 parents 56e56ce + 3f0530a commit d81b399

25 files changed

+1497
-364
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Quote\Api\CartRepositoryInterface;
1414
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
1515
use Magento\Quote\Model\Quote;
16+
use Magento\Store\Model\StoreManagerInterface;
1617

1718
/**
1819
* Get cart
@@ -29,16 +30,24 @@ class GetCartForUser
2930
*/
3031
private $cartRepository;
3132

33+
/**
34+
* @var StoreManagerInterface
35+
*/
36+
private $storeManager;
37+
3238
/**
3339
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
3440
* @param CartRepositoryInterface $cartRepository
41+
* @param StoreManagerInterface $storeManager
3542
*/
3643
public function __construct(
3744
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
38-
CartRepositoryInterface $cartRepository
45+
CartRepositoryInterface $cartRepository,
46+
StoreManagerInterface $storeManager
3947
) {
4048
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
4149
$this->cartRepository = $cartRepository;
50+
$this->storeManager = $storeManager;
4251
}
4352

4453
/**
@@ -75,6 +84,15 @@ public function execute(string $cartHash, ?int $customerId): Quote
7584
);
7685
}
7786

87+
if ((int)$cart->getStoreId() !== (int)$this->storeManager->getStore()->getId()) {
88+
throw new GraphQlNoSuchEntityException(
89+
__(
90+
'Wrong store code specified for cart "%masked_cart_id"',
91+
['masked_cart_id' => $cartHash]
92+
)
93+
);
94+
}
95+
7896
$cartCustomerId = (int)$cart->getCustomerId();
7997

8098
/* Guest cart, allow operations */

app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\QuoteGraphQl\Model\Cart;
99

10+
use Magento\Customer\Helper\Address as AddressHelper;
1011
use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
1112
use Magento\Framework\Exception\LocalizedException;
1213
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
@@ -30,28 +31,44 @@ class QuoteAddressFactory
3031
*/
3132
private $getCustomerAddress;
3233

34+
/**
35+
* @var AddressHelper
36+
*/
37+
private $addressHelper;
38+
3339
/**
3440
* @param BaseQuoteAddressFactory $quoteAddressFactory
3541
* @param GetCustomerAddress $getCustomerAddress
42+
* @param AddressHelper $addressHelper
3643
*/
3744
public function __construct(
3845
BaseQuoteAddressFactory $quoteAddressFactory,
39-
GetCustomerAddress $getCustomerAddress
46+
GetCustomerAddress $getCustomerAddress,
47+
AddressHelper $addressHelper
4048
) {
4149
$this->quoteAddressFactory = $quoteAddressFactory;
4250
$this->getCustomerAddress = $getCustomerAddress;
51+
$this->addressHelper = $addressHelper;
4352
}
4453

4554
/**
4655
* Create QuoteAddress based on input data
4756
*
4857
* @param array $addressInput
4958
* @return QuoteAddress
59+
* @throws GraphQlInputException
5060
*/
5161
public function createBasedOnInputData(array $addressInput): QuoteAddress
5262
{
5363
$addressInput['country_id'] = $addressInput['country_code'] ?? '';
5464

65+
$maxAllowedLineCount = $this->addressHelper->getStreetLines();
66+
if (is_array($addressInput['street']) && count($addressInput['street']) > $maxAllowedLineCount) {
67+
throw new GraphQlInputException(
68+
__('"Street Address" cannot contain more than %1 lines.', $maxAllowedLineCount)
69+
);
70+
}
71+
5572
$quoteAddress = $this->quoteAddressFactory->create();
5673
$quoteAddress->addData($addressInput);
5774
return $quoteAddress;

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Magento\TestFramework\TestCase\GraphQlAbstract;
1414
use Magento\Integration\Api\CustomerTokenServiceInterface;
1515

16+
/**
17+
* Create customer address tests
18+
*/
1619
class CreateCustomerAddressTest extends GraphQlAbstract
1720
{
1821
/**
@@ -198,6 +201,72 @@ public function testCreateCustomerAddressWithMissingAttribute()
198201
$this->graphQlQuery($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
199202
}
200203

204+
/**
205+
* @magentoApiDataFixture Magento/Customer/_files/customer_without_addresses.php
206+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
207+
*/
208+
public function testCreateCustomerAddressWithRedundantStreetLine()
209+
{
210+
$newAddress = [
211+
'region' => [
212+
'region' => 'Arizona',
213+
'region_id' => 4,
214+
'region_code' => 'AZ'
215+
],
216+
'country_id' => 'US',
217+
'street' => ['Line 1 Street', 'Line 2', 'Line 3'],
218+
'company' => 'Company name',
219+
'telephone' => '123456789',
220+
'fax' => '123123123',
221+
'postcode' => '7777',
222+
'city' => 'City Name',
223+
'firstname' => 'Adam',
224+
'lastname' => 'Phillis',
225+
'middlename' => 'A',
226+
'prefix' => 'Mr.',
227+
'suffix' => 'Jr.',
228+
'vat_id' => '1',
229+
'default_shipping' => true,
230+
'default_billing' => false
231+
];
232+
233+
$mutation
234+
= <<<MUTATION
235+
mutation {
236+
createCustomerAddress(input: {
237+
region: {
238+
region: "{$newAddress['region']['region']}"
239+
region_id: {$newAddress['region']['region_id']}
240+
region_code: "{$newAddress['region']['region_code']}"
241+
}
242+
country_id: {$newAddress['country_id']}
243+
street: ["{$newAddress['street'][0]}","{$newAddress['street'][1]}","{$newAddress['street'][2]}"]
244+
company: "{$newAddress['company']}"
245+
telephone: "{$newAddress['telephone']}"
246+
fax: "{$newAddress['fax']}"
247+
postcode: "{$newAddress['postcode']}"
248+
city: "{$newAddress['city']}"
249+
firstname: "{$newAddress['firstname']}"
250+
lastname: "{$newAddress['lastname']}"
251+
middlename: "{$newAddress['middlename']}"
252+
prefix: "{$newAddress['prefix']}"
253+
suffix: "{$newAddress['suffix']}"
254+
vat_id: "{$newAddress['vat_id']}"
255+
default_shipping: true
256+
default_billing: false
257+
}) {
258+
id
259+
}
260+
}
261+
MUTATION;
262+
263+
$userName = 'customer@example.com';
264+
$password = 'password';
265+
266+
self::expectExceptionMessage('"Street Address" cannot contain more than 2 lines.');
267+
$this->graphQlQuery($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
268+
}
269+
201270
/**
202271
* Verify the fields for Customer address
203272
*

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

Lines changed: 0 additions & 148 deletions
This file was deleted.

0 commit comments

Comments
 (0)