Skip to content

Commit

Permalink
[Checkout coverage] setGuestEmailOnCart mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
rogyar committed Apr 3, 2019
1 parent 4a28abb commit ed0dd32
Show file tree
Hide file tree
Showing 7 changed files with 388 additions and 0 deletions.
49 changes: 49 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/GuestEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Model\Quote;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;

/**
* @inheritdoc
*/
class GuestEmail implements ResolverInterface
{
/**
* @var GetCartForUser
*/
private $getCartForUser;

/**
* @param GetCartForUser $getCartForUser
*/
public function __construct(
GetCartForUser $getCartForUser
) {
$this->getCartForUser = $getCartForUser;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
}
/** @var Quote $cart */
$cart = $value['model'];

return $cart->getCustomerEmail();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Validator\EmailAddress as EmailAddressValidator;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;

/**
* @inheritdoc
*/
class SetGuestEmailOnCart implements ResolverInterface
{
/**
* @var CartRepositoryInterface
*/
private $cartRepository;

/**
* @var GetCartForUser
*/
private $getCartForUser;

/**
* @param GetCartForUser $getCartForUser
* @param CartRepositoryInterface $cartRepository
*/
public function __construct(
GetCartForUser $getCartForUser,
CartRepositoryInterface $cartRepository
) {
$this->getCartForUser = $getCartForUser;
$this->cartRepository = $cartRepository;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($args['input']['cart_id']) || empty($args['input']['cart_id'])) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
}
$maskedCartId = $args['input']['cart_id'];

if (!isset($args['input']['email']) || empty($args['input']['email'])) {
throw new GraphQlInputException(__('Required parameter "email" is missing'));
}

$guestEmail = $args['input']['email'];

if (!\Zend_Validate::is($guestEmail, EmailAddressValidator::class)) {
throw new GraphQlInputException(__('Invalid email format'));
}

$currentUserId = $context->getUserId();

if ($currentUserId !== 0) {
throw new GraphQlInputException(__('The request is not allowed for logged in customers'));
}

$cart = $this->getCartForUser->execute($maskedCartId, $currentUserId);
$cart->setCustomerEmail($guestEmail);

try {
$this->cartRepository->save($cart);
} catch (CouldNotSaveException $e) {
throw new LocalizedException(__($e->getMessage()), $e);
}

return [
'cart' => [
'model' => $cart,
'guest_email' => $guestEmail
],
];
}
}
11 changes: 11 additions & 0 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Mutation {
setBillingAddressOnCart(input: SetBillingAddressOnCartInput): SetBillingAddressOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetBillingAddressOnCart")
setShippingMethodsOnCart(input: SetShippingMethodsOnCartInput): SetShippingMethodsOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetShippingMethodsOnCart")
setPaymentMethodOnCart(input: SetPaymentMethodOnCartInput): SetPaymentMethodOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentMethodOnCart")
setGuestEmailOnCart(input: SetGuestEmailOnCartInput): SetGuestEmailOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetGuestEmailOnCart")
}

input AddSimpleProductsToCartInput {
Expand Down Expand Up @@ -124,6 +125,11 @@ input PaymentMethodInput {
purchase_order_number: String @doc(description:"Purchase order number")
}

input SetGuestEmailOnCartInput {
cart_id: String!
email: String!
}

type SetPaymentMethodOnCartOutput {
cart: Cart!
}
Expand All @@ -147,6 +153,7 @@ type ApplyCouponToCartOutput {
type Cart {
items: [CartItemInterface] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartItems")
applied_coupon: AppliedCoupon @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\AppliedCoupon")
guest_email: String @resolver (class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\GuestEmail")
shipping_addresses: [CartAddress]! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingAddresses")
billing_address: CartAddress! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\BillingAddress")
available_payment_methods: [AvailablePaymentMethod] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AvailablePaymentMethods") @doc(description: "Available payment methods")
Expand Down Expand Up @@ -249,6 +256,10 @@ type RemoveItemFromCartOutput {
cart: Cart!
}

type SetGuestEmailOnCartOutput {
cart: Cart!
}

type SimpleCartItem implements CartItemInterface @doc(description: "Simple Cart Item") {
customizable_options: [SelectedCustomizableOption] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\CustomizableOptions")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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 setGuestEmailOnCart mutation
*/
class SetGuestEmailOnCartTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

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

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

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
*/
public function testSetGuestEmailOnCartForLoggedInCustomer()
{
$reservedOrderId = 'test_order_1';
$email = 'some@user.com';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute($reservedOrderId);

$query = $this->getSetGuestEmailOnCartMutation($maskedQuoteId, $email);
$this->expectExceptionMessage('The request is not allowed for logged in customers');
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
}

/**
* Returns GraphQl mutation query for setting email address for a guest
*
* @param string $maskedQuoteId
* @param string $email
* @return string
*/
private function getSetGuestEmailOnCartMutation(string $maskedQuoteId, string $email): string
{
return <<<QUERY
mutation {
setGuestEmailOnCart(input: {
cart_id:"$maskedQuoteId"
email: "$email"
}) {
cart {
guest_email
}
}
}
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\GraphQl\Quote\Guest;

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

/**
* Test for getting guest email from cart
*/
class CartGuestEmailTest extends GraphQlAbstract
{
/**
* @var GetMaskedQuoteIdByReservedOrderId
*/
private $getMaskedQuoteIdByReservedOrderId;

protected function setUp()
{
$objectManager = Bootstrap::getObjectManager();
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
}

/**
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
*/
public function testGetCartGuestEmail()
{
$email = 'store@example.com';
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute(
'test_order_with_virtual_product_without_address'
);

$query = <<<QUERY
{
cart(cart_id:"$maskedQuoteId") {
guest_email
}
}
QUERY;

$response = $this->graphQlQuery($query);
$this->assertArrayHasKey('cart', $response);
$this->assertEquals($email, $response['cart']['guest_email']);
}
}
Loading

0 comments on commit ed0dd32

Please sign in to comment.