Skip to content

Commit

Permalink
Merge pull request #3970 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 Mar 30, 2019
2 parents a9a72b6 + 2b59efa commit b138617
Show file tree
Hide file tree
Showing 26 changed files with 1,326 additions and 246 deletions.
21 changes: 11 additions & 10 deletions app/code/Magento/QuoteGraphQl/Model/Cart/GetQuoteAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\AddressInterfaceFactory;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\ResourceModel\Quote\Address as AddressResource;

/**
Expand Down Expand Up @@ -44,14 +44,14 @@ public function __construct(
/**
* Get quote address
*
* @param CartInterface $cart
* @param int $quoteAddressId
* @param int|null $customerId
* @return AddressInterface
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
* @throws GraphQlAuthorizationException
* @throws GraphQlNoSuchEntityException
*/
public function execute(int $quoteAddressId, ?int $customerId): AddressInterface
public function execute(CartInterface $cart, int $quoteAddressId, ?int $customerId): AddressInterface
{
$quoteAddress = $this->quoteAddressFactory->create();

Expand All @@ -62,14 +62,15 @@ public function execute(int $quoteAddressId, ?int $customerId): AddressInterface
);
}

$quoteAddressCustomerId = (int)$quoteAddress->getCustomerId();

/* Guest cart, allow operations */
if (!$quoteAddressCustomerId && null === $customerId) {
return $quoteAddress;
// TODO: GetQuoteAddress::execute should depend only on AddressInterface contract
// https://github.com/magento/graphql-ce/issues/550
if ($quoteAddress->getQuoteId() !== $cart->getId()) {
throw new GraphQlNoSuchEntityException(
__('Cart does not contain address with ID "%cart_address_id"', ['cart_address_id' => $quoteAddressId])
);
}

if ($quoteAddressCustomerId !== $customerId) {
if ((int)$quoteAddress->getCustomerId() !== (int)$customerId) {
throw new GraphQlAuthorizationException(
__(
'The current user cannot use cart address with ID "%cart_address_id"',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Quote\Api\Data\CartInterface;

Expand Down Expand Up @@ -69,8 +65,7 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
}
$methodCode = $shippingMethodInput['method_code'];

$quoteAddress = $this->getQuoteAddress->execute($cartAddressId, $context->getUserId());

$quoteAddress = $this->getQuoteAddress->execute($cart, $cartAddressId, $context->getUserId());
$this->assignShippingMethodToCart->execute($cart, $quoteAddress, $carrierCode, $methodCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
try {
$this->couponManagement->set($cartId, $couponCode);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
$message = $e->getMessage();
if (preg_match('/The "\d+" Cart doesn\'t contain products/', $message)) {
$message = 'Cart does not contain products.';
}
throw new GraphQlNoSuchEntityException(__($message), $e);
} catch (CouldNotSaveException $e) {
throw new LocalizedException(__($e->getMessage()), $e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,17 @@ public function testApplyCouponTwice()
/**
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
* @expectedException \Exception
* @expectedExceptionMessage Cart does not contain products.
*/
public function testApplyCouponToCartWithNoItems()
{
$this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/191');
$couponCode = '2?ds5!2d';

$this->quoteResource->load($this->quote, 'test_order_1', 'reserved_order_id');
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
$query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode);

self::expectExceptionMessageRegExp('/Cart doesn\'t contain products/');
$this->graphQlQuery($query);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);

namespace Magento\GraphQl\Quote\Customer;

use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test for get available shipping methods
*/
class GetAvailableShippingMethodsTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;

/**
* @inheritdoc
*/
protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
}

/**
* Test case: get available shipping methods from current customer quote
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetAvailableShippingMethods()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$response = $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap());

self::assertArrayHasKey('cart', $response);
self::assertArrayHasKey('shipping_addresses', $response['cart']);
self::assertCount(1, $response['cart']['shipping_addresses']);
self::assertArrayHasKey('available_shipping_methods', $response['cart']['shipping_addresses'][0]);
self::assertCount(1, $response['cart']['shipping_addresses'][0]['available_shipping_methods']);

$expectedAddressData = [
'amount' => 10,
'base_amount' => 10,
'carrier_code' => 'flatrate',
'carrier_title' => 'Flat Rate',
'error_message' => '',
'method_code' => 'flatrate',
'method_title' => 'Fixed',
'price_incl_tax' => 10,
'price_excl_tax' => 10,
];
self::assertEquals(
$expectedAddressData,
$response['cart']['shipping_addresses'][0]['available_shipping_methods'][0]
);
}

/**
* _security
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetAvailableShippingMethodsFromGuestCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);

$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* Test case: get available shipping methods from quote of another customer
*
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
*/
public function testGetAvailableShippingMethodsFromAnotherCustomerCart()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = $this->getQuery($maskedQuoteId);

$this->expectExceptionMessage(
"The current user cannot perform operations on cart \"$maskedQuoteId\""
);
$this->graphQlQuery($query, [], '', $this->getHeaderMap('customer3@search.example.com'));
}

/**
* Test case: get available shipping methods when all shipping methods are disabled
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/disable_offline_shipping_methods.php
*/
public function testGetAvailableShippingMethodsIfShippingMethodsAreNotPresent()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$response = $this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap());

self::assertEmpty($response['cart']['shipping_addresses'][0]['available_shipping_methods']);
}

/**
* Test case: get available shipping methods from non-existent cart
*
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @expectedException \Exception
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id"
*/
public function testGetAvailableShippingMethodsOfNonExistentCart()
{
$maskedQuoteId = 'non_existent_masked_id';
$query = $this->getQuery($maskedQuoteId);

$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* @param string $maskedQuoteId
* @return string
*/
private function getQuery(string $maskedQuoteId): string
{
return <<<QUERY
query {
cart (cart_id: "{$maskedQuoteId}") {
shipping_addresses {
available_shipping_methods {
amount
base_amount
carrier_code
carrier_title
error_message
method_code
method_title
price_excl_tax
price_incl_tax
}
}
}
}
QUERY;
}

/**
* @param string $username
* @param string $password
* @return array
*/
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
return $headerMap;
}
}
Loading

0 comments on commit b138617

Please sign in to comment.