Skip to content

Commit

Permalink
Merge branch '2.3-develop-graphql-prs' of github.com:magento-engcom/m…
Browse files Browse the repository at this point in the history
…agento2ce into fix-nightly-fails-with-msi
  • Loading branch information
ishakhsuvarov committed Nov 6, 2019
2 parents 89cf52c + 5459ada commit c0f9fb7
Show file tree
Hide file tree
Showing 10 changed files with 313 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Customer\Api\Data\AddressInterfaceFactory;
use Magento\Customer\Api\Data\RegionInterface;
use Magento\Customer\Api\Data\RegionInterfaceFactory;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Quote\Model\Quote\Address as QuoteAddress;
Expand Down Expand Up @@ -89,8 +90,15 @@ public function execute(QuoteAddress $quoteAddress, int $customerId): void
$customerAddress->setRegion($region);

$this->addressRepository->save($customerAddress);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()), $e);
} catch (InputException $inputException) {
$graphQlInputException = new GraphQlInputException(__($inputException->getMessage()));
$errors = $inputException->getErrors();
foreach ($errors as $error) {
$graphQlInputException->addError(new GraphQlInputException(__($error->getMessage())));
}
throw $graphQlInputException;
} catch (LocalizedException $exception) {
throw new GraphQlInputException(__($exception->getMessage()), $exception);
}
}
}
48 changes: 46 additions & 2 deletions app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Model\Quote\Address as QuoteAddress;
use Magento\Quote\Model\Quote\AddressFactory as BaseQuoteAddressFactory;
use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory;
use Magento\Directory\Helper\Data as CountryHelper;
use Magento\Directory\Model\AllowedCountries;

/**
* Create QuoteAddress
Expand All @@ -36,36 +39,77 @@ class QuoteAddressFactory
*/
private $addressHelper;

/**
* @var RegionCollectionFactory
*/
private $regionCollectionFactory;

/**
* @var CountryHelper
*/
private $countryHelper;

/**
* @var AllowedCountries
*/
private $allowedCountries;

/**
* @param BaseQuoteAddressFactory $quoteAddressFactory
* @param GetCustomerAddress $getCustomerAddress
* @param AddressHelper $addressHelper
* @param RegionCollectionFactory $regionCollectionFactory
* @param CountryHelper $countryHelper
* @param AllowedCountries $allowedCountries
*/
public function __construct(
BaseQuoteAddressFactory $quoteAddressFactory,
GetCustomerAddress $getCustomerAddress,
AddressHelper $addressHelper
AddressHelper $addressHelper,
RegionCollectionFactory $regionCollectionFactory,
CountryHelper $countryHelper,
AllowedCountries $allowedCountries
) {
$this->quoteAddressFactory = $quoteAddressFactory;
$this->getCustomerAddress = $getCustomerAddress;
$this->addressHelper = $addressHelper;
$this->regionCollectionFactory = $regionCollectionFactory;
$this->countryHelper = $countryHelper;
$this->allowedCountries = $allowedCountries;
}

/**
* Create QuoteAddress based on input data
*
* @param array $addressInput
*
* @return QuoteAddress
* @throws GraphQlInputException
*/
public function createBasedOnInputData(array $addressInput): QuoteAddress
{
$addressInput['country_id'] = '';
if ($addressInput['country_code']) {
if (isset($addressInput['country_code']) && $addressInput['country_code']) {
$addressInput['country_code'] = strtoupper($addressInput['country_code']);
$addressInput['country_id'] = $addressInput['country_code'];
}

$allowedCountries = $this->allowedCountries->getAllowedCountries();
if (!in_array($addressInput['country_code'], $allowedCountries, true)) {
throw new GraphQlInputException(__('Country is not available'));
}
$isRegionRequired = $this->countryHelper->isRegionRequired($addressInput['country_code']);
if ($isRegionRequired && !empty($addressInput['region'])) {
$regionCollection = $this->regionCollectionFactory
->create()
->addRegionCodeFilter($addressInput['region'])
->addCountryFilter($addressInput['country_code']);
if ($regionCollection->getSize() === 0) {
throw new GraphQlInputException(
__('Region is not available for the selected country')
);
}
}
$maxAllowedLineCount = $this->addressHelper->getStreetLines();
if (is_array($addressInput['street']) && count($addressInput['street']) > $maxAllowedLineCount) {
throw new GraphQlInputException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ private function createBillingAddress(
(int)$context->getUserId()
);
}
$errors = $billingAddress->validate();

if (true !== $errors) {
$e = new GraphQlInputException(__('Billing address errors'));
foreach ($errors as $error) {
$e->addError(new GraphQlInputException($error));
}
throw $e;
}

return $billingAddress;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\GraphQl\Model\Query\ContextInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote\Address;

/**
* Set single shipping address for a specified shopping cart
Expand Down Expand Up @@ -52,6 +53,15 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s

$shippingAddress = $this->getShippingAddress->execute($context, $shippingAddressInput);

$errors = $shippingAddress->validate();

if (true !== $errors) {
$e = new GraphQlInputException(__('Shipping address errors'));
foreach ($errors as $error) {
$e->addError(new GraphQlInputException($error));
}
throw $e;
}
$this->assignShippingAddressToCart->execute($cart, $shippingAddress);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function testSetNewBillingAddress()
company: "test company"
street: ["test street 1", "test street 2"]
city: "test city"
region: "test region"
region: "AZ"
postcode: "887766"
country_code: "US"
telephone: "88776655"
Expand Down Expand Up @@ -174,7 +174,7 @@ public function testSetNewBillingAddressWithUseForShippingParameter()
company: "test company"
street: ["test street 1", "test street 2"]
city: "test city"
region: "test region"
region: "AZ"
postcode: "887766"
country_code: "US"
telephone: "88776655"
Expand Down Expand Up @@ -370,7 +370,7 @@ public function testSetNewBillingAddressAndFromAddressBookAtSameTime()
company: "test company"
street: ["test street 1", "test street 2"]
city: "test city"
region: "test region"
region: "AZ"
postcode: "887766"
country_code: "US"
telephone: "88776655"
Expand Down Expand Up @@ -451,7 +451,7 @@ public function testSetNewBillingAddressWithSameAsShippingAndMultishipping()
company: "test company"
street: ["test street 1", "test street 2"]
city: "test city"
region: "test region"
region: "AZ"
postcode: "887766"
country_code: "US"
telephone: "88776655"
Expand Down Expand Up @@ -644,7 +644,7 @@ public function testSetBillingAddressWithoutRequiredParameters(string $input, st
QUERY;

$this->expectExceptionMessage($message);
$this->graphQlMutation($query);
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}

/**
Expand All @@ -661,7 +661,55 @@ public function dataProviderSetWithoutRequiredParameters(): array
'missed_cart_id' => [
'billing_address: {}',
'Required parameter "cart_id" is missing'
]
],
'missed_region' => [
'cart_id: "cart_id_value"
billing_address: {
address: {
firstname: "test firstname"
lastname: "test lastname"
company: "test company"
street: ["test street 1", "test street 2"]
city: "test city"
postcode: "887766"
country_code: "US"
telephone: "88776655"
}
}',
'"regionId" is required. Enter and try again.'
],
'missed_multiple_fields' => [
'cart_id: "cart_id_value"
billing_address: {
address: {
firstname: "test firstname"
lastname: "test lastname"
company: "test company"
street: ["test street 1", "test street 2"]
city: "test city"
country_code: "US"
telephone: "88776655"
}
}',
'"postcode" is required. Enter and try again.
"regionId" is required. Enter and try again.'
],
'wrong_required_region' => [
'cart_id: "cart_id_value"
billing_address: {
address: {
firstname: "test firstname"
lastname: "test lastname"
company: "test company"
street: ["test street 1", "test street 2"]
region: "wrong region"
city: "test city"
country_code: "US"
telephone: "88776655"
}
}',
'Region is not available for the selected country'
],
];
}

Expand All @@ -687,7 +735,7 @@ public function testSetNewBillingAddressWithRedundantStreetLine()
company: "test company"
street: ["test street 1", "test street 2", "test street 3"]
city: "test city"
region: "test region"
region: "AZ"
postcode: "887766"
country_code: "US"
telephone: "88776655"
Expand Down Expand Up @@ -729,7 +777,7 @@ public function testSetBillingAddressWithLowerCaseCountry()
company: "test company"
street: ["test street 1", "test street 2"]
city: "test city"
region: "test region"
region: "AZ"
postcode: "887766"
country_code: "us"
telephone: "88776655"
Expand Down Expand Up @@ -786,7 +834,7 @@ public function testSetNewBillingAddressWithSaveInAddressBook()
company: "test company"
street: ["test street 1", "test street 2"]
city: "test city"
region: "test region"
region: "AZ"
postcode: "887766"
country_code: "US"
telephone: "88776655"
Expand Down Expand Up @@ -853,7 +901,7 @@ public function testSetNewBillingAddressWithNotSaveInAddressBook()
company: "test company"
street: ["test street 1", "test street 2"]
city: "test city"
region: "test region"
region: "AZ"
postcode: "887766"
country_code: "US"
telephone: "88776655"
Expand Down Expand Up @@ -921,7 +969,7 @@ public function testWithInvalidBillingAddressInput()
company: "test company"
street: ["test street 1", "test street 2"]
city: "test city"
region: "test region"
region: "AZ"
postcode: "887766"
country_code: "USS"
telephone: "88776655"
Expand All @@ -948,10 +996,59 @@ public function testWithInvalidBillingAddressInput()
}
}
QUERY;
$this->expectExceptionMessage('The address failed to save. Verify the address and try again.');
$this->expectExceptionMessage('Country is not available');
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testSetShippingAddressesWithNotRequiredRegion()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');

$query = <<<QUERY
mutation {
setBillingAddressOnCart(
input: {
cart_id: "$maskedQuoteId"
billing_address: {
address: {
firstname: "Vasyl"
lastname: "Doe"
street: ["1 Svobody"]
city: "Lviv"
region: "Lviv"
postcode: "00000"
country_code: "UA"
telephone: "555-555-55-55"
}
}
}
) {
cart {
billing_address {
region {
label
}
country {
code
}
}
}
}
}
QUERY;
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
self::assertArrayHasKey('cart', $response['setBillingAddressOnCart']);
$cartResponse = $response['setBillingAddressOnCart']['cart'];
self::assertEquals('UA', $cartResponse['billing_address']['country']['code']);
self::assertEquals('Lviv', $cartResponse['billing_address']['region']['label']);
}

/**
* Verify the all the whitelisted fields for a New Address Object
*
Expand Down
Loading

0 comments on commit c0f9fb7

Please sign in to comment.