Skip to content

Commit

Permalink
Merge pull request #4025 from magento-engcom/graphql-develop-prs
Browse files Browse the repository at this point in the history
[EngCom] Public Pull Requests - GraphQL
  • Loading branch information
naydav authored Apr 9, 2019
2 parents 56e56ce + 3f0530a commit d81b399
Show file tree
Hide file tree
Showing 25 changed files with 1,497 additions and 364 deletions.
20 changes: 19 additions & 1 deletion app/code/Magento/QuoteGraphQl/Model/Cart/GetCartForUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
use Magento\Quote\Model\Quote;
use Magento\Store\Model\StoreManagerInterface;

/**
* Get cart
Expand All @@ -29,16 +30,24 @@ class GetCartForUser
*/
private $cartRepository;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
* @param CartRepositoryInterface $cartRepository
* @param StoreManagerInterface $storeManager
*/
public function __construct(
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
CartRepositoryInterface $cartRepository
CartRepositoryInterface $cartRepository,
StoreManagerInterface $storeManager
) {
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
$this->cartRepository = $cartRepository;
$this->storeManager = $storeManager;
}

/**
Expand Down Expand Up @@ -75,6 +84,15 @@ public function execute(string $cartHash, ?int $customerId): Quote
);
}

if ((int)$cart->getStoreId() !== (int)$this->storeManager->getStore()->getId()) {
throw new GraphQlNoSuchEntityException(
__(
'Wrong store code specified for cart "%masked_cart_id"',
['masked_cart_id' => $cartHash]
)
);
}

$cartCustomerId = (int)$cart->getCustomerId();

/* Guest cart, allow operations */
Expand Down
19 changes: 18 additions & 1 deletion app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Customer\Helper\Address as AddressHelper;
use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
Expand All @@ -30,28 +31,44 @@ class QuoteAddressFactory
*/
private $getCustomerAddress;

/**
* @var AddressHelper
*/
private $addressHelper;

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

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

$maxAllowedLineCount = $this->addressHelper->getStreetLines();
if (is_array($addressInput['street']) && count($addressInput['street']) > $maxAllowedLineCount) {
throw new GraphQlInputException(
__('"Street Address" cannot contain more than %1 lines.', $maxAllowedLineCount)
);
}

$quoteAddress = $this->quoteAddressFactory->create();
$quoteAddress->addData($addressInput);
return $quoteAddress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\Integration\Api\CustomerTokenServiceInterface;

/**
* Create customer address tests
*/
class CreateCustomerAddressTest extends GraphQlAbstract
{
/**
Expand Down Expand Up @@ -198,6 +201,72 @@ public function testCreateCustomerAddressWithMissingAttribute()
$this->graphQlQuery($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer_without_addresses.php
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testCreateCustomerAddressWithRedundantStreetLine()
{
$newAddress = [
'region' => [
'region' => 'Arizona',
'region_id' => 4,
'region_code' => 'AZ'
],
'country_id' => 'US',
'street' => ['Line 1 Street', 'Line 2', 'Line 3'],
'company' => 'Company name',
'telephone' => '123456789',
'fax' => '123123123',
'postcode' => '7777',
'city' => 'City Name',
'firstname' => 'Adam',
'lastname' => 'Phillis',
'middlename' => 'A',
'prefix' => 'Mr.',
'suffix' => 'Jr.',
'vat_id' => '1',
'default_shipping' => true,
'default_billing' => false
];

$mutation
= <<<MUTATION
mutation {
createCustomerAddress(input: {
region: {
region: "{$newAddress['region']['region']}"
region_id: {$newAddress['region']['region_id']}
region_code: "{$newAddress['region']['region_code']}"
}
country_id: {$newAddress['country_id']}
street: ["{$newAddress['street'][0]}","{$newAddress['street'][1]}","{$newAddress['street'][2]}"]
company: "{$newAddress['company']}"
telephone: "{$newAddress['telephone']}"
fax: "{$newAddress['fax']}"
postcode: "{$newAddress['postcode']}"
city: "{$newAddress['city']}"
firstname: "{$newAddress['firstname']}"
lastname: "{$newAddress['lastname']}"
middlename: "{$newAddress['middlename']}"
prefix: "{$newAddress['prefix']}"
suffix: "{$newAddress['suffix']}"
vat_id: "{$newAddress['vat_id']}"
default_shipping: true
default_billing: false
}) {
id
}
}
MUTATION;

$userName = 'customer@example.com';
$password = 'password';

self::expectExceptionMessage('"Street Address" cannot contain more than 2 lines.');
$this->graphQlQuery($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
}

/**
* Verify the fields for Customer address
*
Expand Down

This file was deleted.

Loading

0 comments on commit d81b399

Please sign in to comment.