GuestBillingAddressManagementTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Api;
  7. use Magento\Quote\Api\Data\AddressInterface;
  8. use Magento\TestFramework\TestCase\WebapiAbstract;
  9. class GuestBillingAddressManagementTest extends WebapiAbstract
  10. {
  11. const SERVICE_VERSION = 'V1';
  12. const SERVICE_NAME = 'quoteGuestBillingAddressManagementV1';
  13. const RESOURCE_PATH = '/V1/guest-carts/';
  14. /**
  15. * @var \Magento\TestFramework\ObjectManager
  16. */
  17. protected $objectManager;
  18. protected function setUp()
  19. {
  20. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  21. }
  22. protected function getQuoteMaskedId($quoteId)
  23. {
  24. /** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
  25. $quoteIdMask = $this->objectManager->create(\Magento\Quote\Model\QuoteIdMaskFactory::class)->create();
  26. $quoteIdMask->load($quoteId, 'quote_id');
  27. return $quoteIdMask->getMaskedId();
  28. }
  29. /**
  30. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  31. */
  32. public function testGetAddress()
  33. {
  34. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  35. $quote->load('test_order_1', 'reserved_order_id');
  36. /** @var \Magento\Quote\Model\Quote\Address $address */
  37. $address = $quote->getBillingAddress();
  38. $data = [
  39. AddressInterface::KEY_ID => (int)$address->getId(),
  40. AddressInterface::KEY_REGION => $address->getRegion(),
  41. AddressInterface::KEY_REGION_ID => $address->getRegionId(),
  42. AddressInterface::KEY_REGION_CODE => $address->getRegionCode(),
  43. AddressInterface::KEY_COUNTRY_ID => $address->getCountryId(),
  44. AddressInterface::KEY_STREET => $address->getStreet(),
  45. AddressInterface::KEY_COMPANY => $address->getCompany(),
  46. AddressInterface::KEY_TELEPHONE => $address->getTelephone(),
  47. AddressInterface::KEY_POSTCODE => $address->getPostcode(),
  48. AddressInterface::KEY_CITY => $address->getCity(),
  49. AddressInterface::KEY_FIRSTNAME => $address->getFirstname(),
  50. AddressInterface::KEY_LASTNAME => $address->getLastname(),
  51. AddressInterface::KEY_CUSTOMER_ID => $address->getCustomerId(),
  52. AddressInterface::KEY_EMAIL => $address->getEmail(),
  53. AddressInterface::SAME_AS_BILLING => $address->getSameAsBilling(),
  54. AddressInterface::CUSTOMER_ADDRESS_ID => $address->getCustomerAddressId(),
  55. AddressInterface::SAVE_IN_ADDRESS_BOOK => $address->getSaveInAddressBook(),
  56. ];
  57. $cartId = $this->getQuoteMaskedId($quote->getId());
  58. $serviceInfo = [
  59. 'rest' => [
  60. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/billing-address',
  61. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  62. ],
  63. 'soap' => [
  64. 'service' => self::SERVICE_NAME,
  65. 'serviceVersion' => self::SERVICE_VERSION,
  66. 'operation' => self::SERVICE_NAME . 'Get',
  67. ],
  68. ];
  69. $requestData = ["cartId" => $cartId];
  70. $response = $this->_webApiCall($serviceInfo, $requestData);
  71. asort($data);
  72. asort($response);
  73. $this->assertEquals($data, $response);
  74. }
  75. /**
  76. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  77. * @dataProvider setAddressDataProvider
  78. */
  79. public function testSetAddress($useForShipping)
  80. {
  81. /** @var \Magento\Quote\Model\Quote $quote */
  82. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  83. $quote->load('test_order_1', 'reserved_order_id');
  84. $cartId = $this->getQuoteMaskedId($quote->getId());
  85. $serviceInfo = [
  86. 'rest' => [
  87. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/billing-address',
  88. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  89. ],
  90. 'soap' => [
  91. 'service' => self::SERVICE_NAME,
  92. 'serviceVersion' => self::SERVICE_VERSION,
  93. 'operation' => self::SERVICE_NAME . 'Assign',
  94. ],
  95. ];
  96. $addressData = [
  97. 'firstname' => 'John',
  98. 'lastname' => 'Smith',
  99. 'email' => '',
  100. 'company' => 'Magento Commerce Inc.',
  101. 'street' => ['Typical Street', 'Tiny House 18'],
  102. 'city' => 'Big City',
  103. 'region_id' => 12,
  104. 'region' => 'California',
  105. 'region_code' => 'CA',
  106. 'postcode' => '0985432',
  107. 'country_id' => 'US',
  108. 'telephone' => '88776655',
  109. 'fax' => '44332255',
  110. ];
  111. $requestData = [
  112. 'cartId' => $cartId,
  113. 'address' => $addressData,
  114. 'useForShipping' => $useForShipping
  115. ];
  116. $addressId = $this->_webApiCall($serviceInfo, $requestData);
  117. //reset $quote to reload data
  118. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  119. $quote->load('test_order_1', 'reserved_order_id');
  120. $address = $quote->getBillingAddress();
  121. $address->getRegionCode();
  122. $savedData = $address->getData();
  123. $this->assertEquals($addressId, $savedData['address_id']);
  124. //custom checks for street, region and address_type
  125. foreach ($addressData['street'] as $streetLine) {
  126. $this->assertContains($streetLine, $quote->getBillingAddress()->getStreet());
  127. }
  128. unset($addressData['street']);
  129. unset($addressData['email']);
  130. $this->assertEquals('billing', $savedData['address_type']);
  131. //check the rest of fields
  132. foreach ($addressData as $key => $value) {
  133. $this->assertEquals($value, $savedData[$key]);
  134. }
  135. $address = $quote->getShippingAddress();
  136. $address->getRegionCode();
  137. $savedData = $address->getData();
  138. if ($useForShipping) {
  139. //check that shipping address set
  140. $this->assertEquals('shipping', $savedData['address_type']);
  141. $this->assertEquals(1, $savedData['same_as_billing']);
  142. //check the rest of fields
  143. foreach ($addressData as $key => $value) {
  144. $this->assertEquals($value, $savedData[$key]);
  145. }
  146. } else {
  147. $this->assertEquals(0, $savedData['same_as_billing']);
  148. }
  149. }
  150. public function setAddressDataProvider()
  151. {
  152. return [
  153. [true],
  154. [false]
  155. ];
  156. }
  157. }