quoteResource = $objectManager->get(QuoteResource::class); $this->quoteFactory = $objectManager->get(QuoteFactory::class); $this->quoteIdToMaskedId = $objectManager->get(QuoteIdToMaskedQuoteIdInterface::class); $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); $this->customerAddressRepository = $objectManager->get(AddressRepositoryInterface::class); $this->searchCriteriaBuilder = $objectManager->get(SearchCriteriaBuilder::class); $this->customerRepository = $objectManager->get(CustomerRepositoryInterface::class); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testSetNewShippingAddressOnCartWithSimpleProduct() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($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/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testSetShippingAddressOnCartWithRegionIdForSimpleProduct() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); self::assertArrayHasKey('cart', $response['setShippingAddressesOnCart']); $cartResponse = $response['setShippingAddressesOnCart']['cart']; self::assertArrayHasKey('shipping_addresses', $cartResponse); $shippingAddressResponse = current($cartResponse['shipping_addresses']); $this->assertShippingAddressWithRegionIdFields($shippingAddressResponse); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_virtual_product.php * */ public function testSetNewShippingAddressOnCartWithVirtualProduct() { $this->expectException(\Exception::class); $this->expectExceptionMessage('Shipping address is not allowed on cart: cart contains no items for shipment.'); $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testSetShippingAddressFromAddressBook() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($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/Customer/_files/customer.php * @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testVerifyShippingAddressType() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); $shippingAddresses = current($response['setShippingAddressesOnCart']['cart']['shipping_addresses']); self::assertArrayHasKey('__typename', $shippingAddresses); self::assertEquals('ShippingCartAddress', $shippingAddresses['__typename']); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php * */ public function testSetNonExistentShippingAddressFromAddressBook() { $this->expectException(\Exception::class); $this->expectExceptionMessage('Could not find a address with ID "100"'); $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testSetNewShippingAddressAndFromAddressBookAtSameTime() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); } /** * _security * @magentoApiDataFixture Magento/Customer/_files/three_customers.php * @magentoApiDataFixture Magento/Customer/_files/customer_address.php * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php * */ public function testSetShippingAddressIfCustomerIsNotOwnerOfAddress() { $this->expectException(\Exception::class); $this->expectExceptionMessage('Current customer does not have permission to address with ID "1"'); $maskedQuoteId = $this->assignQuoteToCustomer('test_order_with_simple_product_without_address', 2); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap('customer2@search.example.com')); } /** * _security * @magentoApiDataFixture Magento/Customer/_files/three_customers.php * @magentoApiDataFixture Magento/Customer/_files/customer_address.php * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php * */ public function testSetShippingAddressToAnotherCustomerCart() { $this->expectException(\Exception::class); $maskedQuoteId = $this->assignQuoteToCustomer('test_order_with_simple_product_without_address', 1); $query = <<expectExceptionMessage( "The current user cannot perform operations on cart \"$maskedQuoteId\"" ); $this->graphQlMutation($query, [], '', $this->getHeaderMap('customer2@search.example.com')); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php * * @dataProvider dataProviderUpdateWithMissedRequiredParameters * @param string $input * @param string $message * @throws \Exception */ public function testSetNewShippingAddressWithMissedRequiredParameters(string $input, string $message) { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $input = str_replace('cart_id_value', $maskedQuoteId, $input); $query = <<expectExceptionMessage($message); $this->graphQlMutation($query, [], '', $this->getHeaderMap()); } /** * Covers case with empty street * * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php * */ public function testSetNewShippingAddressWithMissedRequiredStreetParameters() { $this->expectException(\Exception::class); $this->expectExceptionMessage('Field CartAddressInput.street of required type [String]! was not provided.'); $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); } /** * @return array */ public function dataProviderUpdateWithMissedRequiredParameters(): array { return [ 'missed_region' => [ 'cart_id: "cart_id_value" shipping_addresses: [{ address: { firstname: "test firstname" lastname: "test lastname" company: "test company" street: ["test street 1", "test street 2"] city: "test city" postcode: "887766" country_code: "US" telephone: "88776655" } }]', 'Region is required.' ], 'missed_postal_code' => [ 'cart_id: "cart_id_value" shipping_addresses: [{ address: { firstname: "test firstname" lastname: "test lastname" company: "test company" street: ["test street 1", "test street 2"] city: "test city" region: "TX" region_id: 57 country_code: "US" telephone: "88776655" } }]', '"postcode" is required. Enter and try again.' ], 'wrong_required_region' => [ 'cart_id: "cart_id_value" shipping_addresses: [{ address: { firstname: "test firstname" lastname: "test lastname" company: "test company" street: ["test street 1", "test street 2"] region: "wrong region" region_id: 17 city: "test city" country_code: "US" postcode: "887766" telephone: "88776655" } }]', 'The region_id does not match the selected country or region' ], 'wrong_required_region_name' => [ 'cart_id: "cart_id_value" shipping_addresses: [{ address: { firstname: "test firstname" lastname: "test lastname" company: "test company" street: ["test street 1", "test street 2"] region: "wrong region" region_id: 45 city: "test city" country_code: "US" postcode: "887766" telephone: "88776655" } }]', 'The region_id does not match the selected country or region' ], ]; } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php * */ public function testSetMultipleNewShippingAddresses() { $this->expectException(\Exception::class); $this->expectExceptionMessage('You cannot specify multiple shipping addresses.'); $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testSetNewShippingAddressOnCartWithRedundantStreetLine() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/Customer/_files/customer_address.php */ public function testSetShippingAddressOnNonExistentCart() { $this->expectException(\Exception::class); $this->expectExceptionMessage('Could not find a cart with ID "non_existent_masked_id"'); $maskedQuoteId = 'non_existent_masked_id'; $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); } /** * _security * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/Customer/_files/customer_address.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testSetShippingAddressToGuestCart() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<expectExceptionMessage( "The current user cannot perform operations on cart \"{$maskedQuoteId}\"" ); $this->graphQlMutation($query, [], '', $this->getHeaderMap()); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testSetShippingAddressWithLowerCaseCountry() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); self::assertCount(1, $result['setShippingAddressesOnCart']['cart']['shipping_addresses']); $address = reset($result['setShippingAddressesOnCart']['cart']['shipping_addresses']); $this->assertEquals('US', $address['country']['code']); $this->assertEquals('CA', $address['region']['code']); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testWithInvalidShippingAddressesInput() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<expectExceptionMessage('Country is not available'); $this->graphQlMutation($query, [], '', $this->getHeaderMap()); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testSetShippingAddressesWithNotRequiredRegion() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); self::assertArrayHasKey('cart', $response['setShippingAddressesOnCart']); $cartResponse = $response['setShippingAddressesOnCart']['cart']; self::assertEquals('UA', $cartResponse['shipping_addresses'][0]['country']['code']); self::assertEquals('Lviv', $cartResponse['shipping_addresses'][0]['region']['label']); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testSetNewShippingAddressWithSaveInAddressBook() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); $customer = $this->customerRepository->get('customer@example.com'); $searchCriteria = $this->searchCriteriaBuilder->addFilter('parent_id', $customer->getId())->create(); $addresses = $this->customerAddressRepository->getList($searchCriteria)->getItems(); self::assertCount(0, $addresses); self::assertArrayHasKey('cart', $response['setShippingAddressesOnCart']); $cartResponse = $response['setShippingAddressesOnCart']['cart']; self::assertArrayHasKey('shipping_addresses', $cartResponse); $shippingAddressResponse = current($cartResponse['shipping_addresses']); $this->assertNewShippingAddressFields($shippingAddressResponse); foreach ($addresses as $address) { $this->customerAddressRepository->delete($address); } } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testSetNewShippingAddressWithNotSaveInAddressBook() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); $customer = $this->customerRepository->get('customer@example.com'); $searchCriteria = $this->searchCriteriaBuilder->addFilter('parent_id', $customer->getId())->create(); $addresses = $this->customerAddressRepository->getList($searchCriteria)->getItems(); self::assertCount(0, $addresses); self::assertArrayHasKey('cart', $response['setShippingAddressesOnCart']); $cartResponse = $response['setShippingAddressesOnCart']['cart']; self::assertArrayHasKey('shipping_addresses', $cartResponse); $shippingAddressResponse = current($cartResponse['shipping_addresses']); $this->assertNewShippingAddressFields($shippingAddressResponse); foreach ($addresses as $address) { $this->customerAddressRepository->delete($address); } } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testSetShippingAddressOnCartWithBothRegionAndRegionId() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); $this->assertArrayHasKey('cart', $response['setShippingAddressesOnCart']); $cartResponse = $response['setShippingAddressesOnCart']['cart']; $this->assertArrayHasKey('shipping_addresses', $cartResponse); $shippingAddressResponse = current($cartResponse['shipping_addresses']); $expectedRegionCode = "AE"; $expectedRegionLabel = "Armed Forces Middle East"; $this->assertEquals($expectedRegionCode, $shippingAddressResponse['region']['code']); $this->assertEquals($expectedRegionLabel, $shippingAddressResponse['region']['label']); $this->assertEquals(10, $shippingAddressResponse['region']['region_id']); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testSetShippingAddressOnCartWithFreeFormRegionForNoRequiredCountry() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); $this->assertArrayHasKey('cart', $response['setShippingAddressesOnCart']); $cartResponse = $response['setShippingAddressesOnCart']['cart']; $this->assertArrayHasKey('shipping_addresses', $cartResponse); $shippingAddressResponse = current($cartResponse['shipping_addresses']); $expectedRegionCode = "Some"; $expectedRegionLabel = "Some"; $this->assertEquals($expectedRegionCode, $shippingAddressResponse['region']['code']); $this->assertEquals($expectedRegionLabel, $shippingAddressResponse['region']['label']); $this->assertEquals(null, $shippingAddressResponse['region']['region_id']); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testSetShippingAddressOnCartWithRegionOnExistingDuplicateRegionCode() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<expectExceptionMessage( 'Region input is ambiguous. Specify region_id.' ); $this->graphQlMutation($query, [], '', $this->getHeaderMap()); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testSetShippingAddressOnCartWithRegionOnExistingDuplicateRegionCodeWithWrongRegionId() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<expectExceptionMessage( 'The region_id does not match the selected country or region' ); $this->graphQlMutation($query, [], '', $this->getHeaderMap()); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testSetShippingAddressOnCartWithWrongRegionId() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<expectExceptionMessage( 'The region_id does not match the selected country or region' ); $this->graphQlMutation($query, [], '', $this->getHeaderMap()); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testSetShippingAddressOnCartWithCorrectRegionId() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); $this->assertArrayHasKey('cart', $response['setShippingAddressesOnCart']); $cartResponse = $response['setShippingAddressesOnCart']['cart']; $this->assertArrayHasKey('shipping_addresses', $cartResponse); $shippingAddressResponse = current($cartResponse['shipping_addresses']); $expectedRegionCode = "TX"; $expectedRegionLabel = "Texas"; $this->assertEquals($expectedRegionCode, $shippingAddressResponse['region']['code']); $this->assertEquals($expectedRegionLabel, $shippingAddressResponse['region']['label']); } /** * @magentoApiDataFixture Magento/Customer/_files/customer_without_addresses.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testSetShippingAddressOnCartWithCreateCustomerAddressRegionCodeAndRegionId() { $newAddress = [ 'region' => [ 'region_code' => 'NY', 'region_id' => 43 ], 'country_code' => 'US', 'street' => ['Line 1 Street', 'Line 2'], 'company' => 'Company name', 'telephone' => '123456789', 'postcode' => '10019', 'city' => 'Manhattan', 'firstname' => 'Adam', 'lastname' => 'Phillis' ]; $mutation = <<graphQlMutation($mutation, [], '', $this->getHeaderMap($userName, $password)); $this->assertArrayHasKey('createCustomerAddress', $response); $this->assertArrayHasKey('customer_id', $response['createCustomerAddress']); $this->assertEquals(null, $response['createCustomerAddress']['customer_id']); $this->assertArrayHasKey('id', $response['createCustomerAddress']); $id = $response['createCustomerAddress']['id']; $this->assertEquals(43, $response["createCustomerAddress"]["region"]["region_id"]); $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation( $query, [], '', $this->getHeaderMap($userName, $password) ); $cartResponse = $shippingAddressesResponse["setShippingAddressesOnCart"]["cart"]["shipping_addresses"][0]; $this->assertEquals( 43, $cartResponse["region"]["region_id"] ); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.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_billing_address.php */ public function testSetNewShippingAddressAndPlaceOrder() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); $this->graphQlMutation( $this->getSetShippingMethodsQuery($maskedQuoteId, 'flatrate', 'flatrate'), [], '', $this->getHeaderMap() ); $this->graphQlMutation( $this->getSetPaymentMethodQuery( $maskedQuoteId, 'checkmo' ), [], '', $this->getHeaderMap() ); $this->graphQlMutation( $this->getPlaceOrderQuery($maskedQuoteId), [], '', $this->getHeaderMap() ); $customer = $this->customerRepository->get('customer@example.com'); $searchCriteria = $this->searchCriteriaBuilder->addFilter('parent_id', $customer->getId())->create(); $addresses = $this->customerAddressRepository->getList($searchCriteria)->getItems(); self::assertCount(1, $addresses); self::assertArrayHasKey('cart', $response['setShippingAddressesOnCart']); $cartResponse = $response['setShippingAddressesOnCart']['cart']; self::assertArrayHasKey('shipping_addresses', $cartResponse); $shippingAddressResponse = current($cartResponse['shipping_addresses']); $this->assertNewShippingAddressFields($shippingAddressResponse); foreach ($addresses as $address) { $this->customerAddressRepository->delete($address); } } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.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_billing_address.php */ public function testSetNewShippingAddressWithDefaultValueOfSaveInAddressBookAndPlaceOrder() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); $this->graphQlMutation( $this->getSetShippingMethodsQuery($maskedQuoteId, 'flatrate', 'flatrate'), [], '', $this->getHeaderMap() ); $this->graphQlMutation( $this->getSetPaymentMethodQuery( $maskedQuoteId, 'checkmo' ), [], '', $this->getHeaderMap() ); $this->graphQlMutation( $this->getPlaceOrderQuery($maskedQuoteId), [], '', $this->getHeaderMap() ); $customer = $this->customerRepository->get('customer@example.com'); $searchCriteria = $this->searchCriteriaBuilder->addFilter('parent_id', $customer->getId())->create(); $addresses = $this->customerAddressRepository->getList($searchCriteria)->getItems(); $this->assertCount(1, $addresses); $this->assertArrayHasKey('cart', $response['setShippingAddressesOnCart']); $cartResponse = $response['setShippingAddressesOnCart']['cart']; $this->assertArrayHasKey('shipping_addresses', $cartResponse); $shippingAddressResponse = current($cartResponse['shipping_addresses']); $this->assertNewShippingAddressFields($shippingAddressResponse); foreach ($addresses as $address) { $this->customerAddressRepository->delete($address); } } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testSetShippingAddressOnCartWithNullCustomerAddressId() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<expectExceptionMessage( 'The shipping address must contain either "customer_address_id" or "address".' ); $this->graphQlMutation($query, [], '', $this->getHeaderMap()); } /** * 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'], ['response_field' => 'country', 'expected_value' => ['code' => 'US', 'label' => 'US']], ['response_field' => '__typename', 'expected_value' => 'ShippingCartAddress'], ['response_field' => 'customer_notes', 'expected_value' => 'Test note'] ]; $this->assertResponseFields($shippingAddressResponse, $assertionMap); } /** * Verify the all the whitelisted fields for a New Address with region id Object * * @param array $shippingAddressResponse */ private function assertShippingAddressWithRegionIdFields(array $shippingAddressResponse): void { $assertionMap = [ ['response_field' => 'firstname', 'expected_value' => 'Francesco'], ['response_field' => 'lastname', 'expected_value' => 'Alba'], ['response_field' => 'company', 'expected_value' => 'Magento'], ['response_field' => 'street', 'expected_value' => [0 => 'Via Solferino', 1 => '45']], ['response_field' => 'city', 'expected_value' => 'Ceriano Laghetto'], ['response_field' => 'postcode', 'expected_value' => '20816'], ['response_field' => 'telephone', 'expected_value' => '3273581975'], ['response_field' => 'region', 'expected_value' => ['code' => '58', 'label' => 'Nièvre']], ['response_field' => 'country', 'expected_value' => ['code' => 'FR', 'label' => 'FR']] ]; $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 $reservedOrderId * @param int $customerId * @return string */ private function assignQuoteToCustomer( string $reservedOrderId = 'test_order_with_simple_product_without_address', int $customerId = 1 ): string { $quote = $this->quoteFactory->create(); $this->quoteResource->load($quote, $reservedOrderId, 'reserved_order_id'); $quote->setCustomerId($customerId); $this->quoteResource->save($quote); return $this->quoteIdToMaskedId->execute((int)$quote->getId()); } /** * @param string $maskedQuoteId * @param string $shippingMethodCode * @param string $shippingCarrierCode * @return string */ private function getSetShippingMethodsQuery( string $maskedQuoteId, string $shippingMethodCode, string $shippingCarrierCode ): string { return <<testSetNewShippingAddressAndPlaceOrder(); } /** * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/Customer/_files/attribute_telephone_not_required_address.php * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php */ public function testSetNewShippingAddressWithoutTelephone() { $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); $query = <<graphQlMutation($query, [], '', $this->getHeaderMap()); self::assertArrayHasKey('cart', $response['setShippingAddressesOnCart']); $cartResponse = $response['setShippingAddressesOnCart']['cart']; self::assertArrayHasKey('shipping_addresses', $cartResponse); $shippingAddressResponse = current($cartResponse['shipping_addresses']); $this->assertNewAddressWithoutTelephone($shippingAddressResponse); } /** * Verify the all the whitelisted fields for a New Address Object without telephone * * @param array $addressResponse */ private function assertNewAddressWithoutTelephone( array $addressResponse ): 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' => ''], ['response_field' => 'country', 'expected_value' => ['code' => 'US', 'label' => 'US']], ['response_field' => '__typename', 'expected_value' => 'ShippingCartAddress'] ]; $this->assertResponseFields($addressResponse, $assertionMap); } }