-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Checkout coverage] setGuestEmailOnCart mutation
- Loading branch information
Showing
7 changed files
with
388 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
app/code/Magento/QuoteGraphQl/Model/Resolver/GuestEmail.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
app/code/Magento/QuoteGraphQl/Model/Resolver/SetGuestEmailOnCart.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetGuestEmailOnCartTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CartGuestEmailTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
Oops, something went wrong.