123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- declare(strict_types=1);
- namespace Magento\GraphQl\Quote;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- use Magento\Integration\Api\CustomerTokenServiceInterface;
- use Magento\Multishipping\Helper\Data;
- use Magento\Quote\Model\QuoteFactory;
- use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
- use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
- use Magento\TestFramework\Helper\Bootstrap;
- use Magento\TestFramework\TestCase\GraphQlAbstract;
- use Magento\TestFramework\ObjectManager;
- /**
- * Test for set shipping addresses on cart mutation
- */
- class SetShippingAddressOnCartTest extends GraphQlAbstract
- {
- /**
- * @var QuoteResource
- */
- private $quoteResource;
- /**
- * @var QuoteFactory
- */
- private $quoteFactory;
- /**
- * @var QuoteIdToMaskedQuoteIdInterface
- */
- private $quoteIdToMaskedId;
- /**
- * @var CustomerTokenServiceInterface
- */
- private $customerTokenService;
- protected function setUp()
- {
- $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/434');
- $objectManager = Bootstrap::getObjectManager();
- $this->quoteResource = $objectManager->get(QuoteResource::class);
- $this->quoteFactory = $objectManager->get(QuoteFactory::class);
- $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class);
- $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
- }
- /**
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
- */
- public function testSetNewShippingAddressByGuest()
- {
- $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address');
- $query = <<<QUERY
- mutation {
- setShippingAddressesOnCart(
- input: {
- cart_id: "$maskedQuoteId"
- shipping_addresses: [
- {
- address: {
- firstname: "test firstname"
- lastname: "test lastname"
- company: "test company"
- street: ["test street 1", "test street 2"]
- city: "test city"
- region: "test region"
- postcode: "887766"
- country_code: "US"
- telephone: "88776655"
- save_in_address_book: false
- }
- }
- ]
- }
- ) {
- cart {
- shipping_addresses {
- firstname
- lastname
- company
- street
- city
- postcode
- telephone
- }
- }
- }
- }
- QUERY;
- $response = $this->graphQlQuery($query);
- self::assertArrayHasKey('cart', $response['setShippingAddressesOnCart']);
- $cartResponse = $response['setShippingAddressesOnCart']['cart'];
- self::assertArrayHasKey('shipping_addresses', $cartResponse);
- $shippingAddressResponse = current($cartResponse['shipping_addresses']);
- $this->assertNewShippingAddressFields($shippingAddressResponse);
- }
- /**
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
- * @expectedException \Exception
- * @expectedExceptionMessage The current customer isn't authorized.
- */
- public function testSetShippingAddressFromAddressBookByGuest()
- {
- $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address');
- $query = <<<QUERY
- mutation {
- setShippingAddressesOnCart(
- input: {
- cart_id: "$maskedQuoteId"
- shipping_addresses: [
- {
- customer_address_id: 1
- }
- ]
- }
- ) {
- cart {
- shipping_addresses {
- city
- }
- }
- }
- }
- QUERY;
- $this->graphQlQuery($query);
- }
- /**
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
- * @magentoApiDataFixture Magento/Customer/_files/customer.php
- */
- public function testSetNewShippingAddressByRegisteredCustomer()
- {
- $maskedQuoteId = $this->assignQuoteToCustomer();
- $query = <<<QUERY
- mutation {
- setShippingAddressesOnCart(
- input: {
- cart_id: "$maskedQuoteId"
- shipping_addresses: [
- {
- address: {
- firstname: "test firstname"
- lastname: "test lastname"
- company: "test company"
- street: ["test street 1", "test street 2"]
- city: "test city"
- region: "test region"
- postcode: "887766"
- country_code: "US"
- telephone: "88776655"
- save_in_address_book: false
- }
- }
- ]
- }
- ) {
- cart {
- shipping_addresses {
- firstname
- lastname
- company
- street
- city
- postcode
- telephone
- }
- }
- }
- }
- QUERY;
- $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
- self::assertArrayHasKey('cart', $response['setShippingAddressesOnCart']);
- $cartResponse = $response['setShippingAddressesOnCart']['cart'];
- self::assertArrayHasKey('shipping_addresses', $cartResponse);
- $shippingAddressResponse = current($cartResponse['shipping_addresses']);
- $this->assertNewShippingAddressFields($shippingAddressResponse);
- }
- /**
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
- * @magentoApiDataFixture Magento/Customer/_files/customer.php
- * @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php
- */
- public function testSetShippingAddressFromAddressBookByRegisteredCustomer()
- {
- $maskedQuoteId = $this->assignQuoteToCustomer();
- $query = <<<QUERY
- mutation {
- setShippingAddressesOnCart(
- input: {
- cart_id: "$maskedQuoteId"
- shipping_addresses: [
- {
- customer_address_id: 1
- }
- ]
- }
- ) {
- cart {
- shipping_addresses {
- firstname
- lastname
- company
- street
- city
- postcode
- telephone
- }
- }
- }
- }
- QUERY;
- $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
- self::assertArrayHasKey('cart', $response['setShippingAddressesOnCart']);
- $cartResponse = $response['setShippingAddressesOnCart']['cart'];
- self::assertArrayHasKey('shipping_addresses', $cartResponse);
- $shippingAddressResponse = current($cartResponse['shipping_addresses']);
- $this->assertSavedShippingAddressFields($shippingAddressResponse);
- }
- /**
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
- * @magentoApiDataFixture Magento/Customer/_files/customer.php
- * @expectedException \Exception
- * @expectedExceptionMessage Could not find a address with ID "100"
- */
- public function testSetNotExistedShippingAddressFromAddressBook()
- {
- $maskedQuoteId = $this->assignQuoteToCustomer();
- $query = <<<QUERY
- mutation {
- setShippingAddressesOnCart(
- input: {
- cart_id: "$maskedQuoteId"
- shipping_addresses: [
- {
- customer_address_id: 100
- }
- ]
- }
- ) {
- cart {
- shipping_addresses {
- city
- }
- }
- }
- }
- QUERY;
- $this->graphQlQuery($query, [], '', $this->getHeaderMap());
- }
- /**
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
- * @expectedException \Exception
- * @expectedExceptionMessage The shipping address must contain either "customer_address_id" or "address".
- */
- public function testSetShippingAddressWithoutAddresses()
- {
- $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address');
- $query = <<<QUERY
- mutation {
- setShippingAddressesOnCart(
- input: {
- cart_id: "$maskedQuoteId"
- shipping_addresses: [
- {}
- ]
- }
- ) {
- cart {
- shipping_addresses {
- city
- }
- }
- }
- }
- QUERY;
- $this->graphQlQuery($query);
- }
- /**
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
- * @magentoApiDataFixture Magento/Customer/_files/customer.php
- * @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php
- */
- public function testSetNewShippingAddressAndFromAddressBookAtSameTime()
- {
- $maskedQuoteId = $this->assignQuoteToCustomer();
- $query = <<<QUERY
- mutation {
- setShippingAddressesOnCart(
- input: {
- cart_id: "$maskedQuoteId"
- shipping_addresses: [
- {
- customer_address_id: 1,
- address: {
- firstname: "test firstname"
- lastname: "test lastname"
- company: "test company"
- street: ["test street 1", "test street 2"]
- city: "test city"
- region: "test region"
- postcode: "887766"
- country_code: "US"
- telephone: "88776655"
- save_in_address_book: false
- }
- }
- ]
- }
- ) {
- cart {
- shipping_addresses {
- city
- }
- }
- }
- }
- QUERY;
- self::expectExceptionMessage(
- 'The shipping address cannot contain "customer_address_id" and "address" at the same time.'
- );
- $this->graphQlQuery($query, [], '', $this->getHeaderMap());
- }
- /**
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
- * @expectedException \Exception
- * @expectedExceptionMessage You cannot specify multiple shipping addresses.
- */
- public function testSetMultipleNewShippingAddresses()
- {
- $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address');
- $query = <<<QUERY
- mutation {
- setShippingAddressesOnCart(
- input: {
- cart_id: "$maskedQuoteId"
- shipping_addresses: [
- {
- address: {
- firstname: "test firstname"
- lastname: "test lastname"
- company: "test company"
- street: ["test street 1", "test street 2"]
- city: "test city"
- region: "test region"
- postcode: "887766"
- country_code: "US"
- telephone: "88776655"
- save_in_address_book: false
- }
- },
- {
- address: {
- firstname: "test firstname 2"
- lastname: "test lastname 2"
- company: "test company 2"
- street: ["test street 1", "test street 2"]
- city: "test city"
- region: "test region"
- postcode: "887766"
- country_code: "US"
- telephone: "88776655"
- save_in_address_book: false
- }
- }
- ]
- }
- ) {
- cart {
- shipping_addresses {
- city
- }
- }
- }
- }
- QUERY;
- /** @var \Magento\Config\Model\ResourceModel\Config $config */
- $config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class);
- $config->saveConfig(
- Data::XML_PATH_CHECKOUT_MULTIPLE_AVAILABLE,
- null,
- ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
- 0
- );
- /** @var \Magento\Framework\App\Config\ReinitableConfigInterface $config */
- $config = ObjectManager::getInstance()->get(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
- $config->reinit();
- $this->graphQlQuery($query);
- }
- /**
- * @magentoApiDataFixture Magento/Customer/_files/three_customers.php
- * @magentoApiDataFixture Magento/Customer/_files/customer_address.php
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
- * @expectedException \Exception
- * @expectedExceptionMessage The current user cannot use address with ID "1"
- */
- public function testSetShippingAddressIfCustomerIsNotOwnerOfAddress()
- {
- $maskedQuoteId = $this->getMaskedQuoteIdByReversedQuoteId('test_order_with_simple_product_without_address');
- $query = <<<QUERY
- mutation {
- setShippingAddressesOnCart(
- input: {
- cart_id: "$maskedQuoteId"
- shipping_addresses: [
- {
- customer_address_id: 1
- }
- ]
- }
- ) {
- cart {
- shipping_addresses {
- postcode
- }
- }
- }
- }
- QUERY;
- $this->graphQlQuery($query, [], '', $this->getHeaderMap('customer2@search.example.com'));
- }
- /**
- * Verify the all the whitelisted fields for a New Address Object
- *
- * @param array $shippingAddressResponse
- */
- private function assertNewShippingAddressFields(array $shippingAddressResponse): void
- {
- $assertionMap = [
- ['response_field' => 'firstname', 'expected_value' => 'test firstname'],
- ['response_field' => 'lastname', 'expected_value' => 'test lastname'],
- ['response_field' => 'company', 'expected_value' => 'test company'],
- ['response_field' => 'street', 'expected_value' => [0 => 'test street 1', 1 => 'test street 2']],
- ['response_field' => 'city', 'expected_value' => 'test city'],
- ['response_field' => 'postcode', 'expected_value' => '887766'],
- ['response_field' => 'telephone', 'expected_value' => '88776655']
- ];
- $this->assertResponseFields($shippingAddressResponse, $assertionMap);
- }
- /**
- * Verify the all the whitelisted fields for a Address Object
- *
- * @param array $shippingAddressResponse
- */
- private function assertSavedShippingAddressFields(array $shippingAddressResponse): void
- {
- $assertionMap = [
- ['response_field' => 'firstname', 'expected_value' => 'John'],
- ['response_field' => 'lastname', 'expected_value' => 'Smith'],
- ['response_field' => 'company', 'expected_value' => 'CompanyName'],
- ['response_field' => 'street', 'expected_value' => [0 => 'Green str, 67']],
- ['response_field' => 'city', 'expected_value' => 'CityM'],
- ['response_field' => 'postcode', 'expected_value' => '75477'],
- ['response_field' => 'telephone', 'expected_value' => '3468676']
- ];
- $this->assertResponseFields($shippingAddressResponse, $assertionMap);
- }
- /**
- * @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;
- }
- /**
- * @param string $reversedQuoteId
- * @return string
- */
- private function getMaskedQuoteIdByReversedQuoteId(string $reversedQuoteId): string
- {
- $quote = $this->quoteFactory->create();
- $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id');
- return $this->quoteIdToMaskedId->execute((int)$quote->getId());
- }
- /**
- * @param string $reversedQuoteId
- * @param int $customerId
- * @return string
- */
- private function assignQuoteToCustomer(
- string $reversedQuoteId = 'test_order_with_simple_product_without_address',
- int $customerId = 1
- ): string {
- $quote = $this->quoteFactory->create();
- $this->quoteResource->load($quote, $reversedQuoteId, 'reserved_order_id');
- $quote->setCustomerId($customerId);
- $this->quoteResource->save($quote);
- return $this->quoteIdToMaskedId->execute((int)$quote->getId());
- }
- public function tearDown()
- {
- /** @var \Magento\Config\Model\ResourceModel\Config $config */
- $config = ObjectManager::getInstance()->get(\Magento\Config\Model\ResourceModel\Config::class);
- //default state of multishipping config
- $config->saveConfig(
- Data::XML_PATH_CHECKOUT_MULTIPLE_AVAILABLE,
- 1,
- ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
- 0
- );
- /** @var \Magento\Framework\App\Config\ReinitableConfigInterface $config */
- $config = ObjectManager::getInstance()->get(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
- $config->reinit();
- }
- }
|