BillingAddressManagementTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 BillingAddressManagementTest extends WebapiAbstract
  10. {
  11. const SERVICE_VERSION = 'V1';
  12. const SERVICE_NAME = 'quoteBillingAddressManagementV1';
  13. const RESOURCE_PATH = '/V1/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. /**
  23. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  24. */
  25. public function testGetAddress()
  26. {
  27. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  28. $quote->load('test_order_1', 'reserved_order_id');
  29. /** @var \Magento\Quote\Model\Quote\Address $address */
  30. $address = $quote->getBillingAddress();
  31. $data = [
  32. AddressInterface::KEY_ID => (int)$address->getId(),
  33. AddressInterface::KEY_REGION => $address->getRegion(),
  34. AddressInterface::KEY_REGION_ID => $address->getRegionId(),
  35. AddressInterface::KEY_REGION_CODE => $address->getRegionCode(),
  36. AddressInterface::KEY_COUNTRY_ID => $address->getCountryId(),
  37. AddressInterface::KEY_STREET => $address->getStreet(),
  38. AddressInterface::KEY_COMPANY => $address->getCompany(),
  39. AddressInterface::KEY_TELEPHONE => $address->getTelephone(),
  40. AddressInterface::KEY_POSTCODE => $address->getPostcode(),
  41. AddressInterface::KEY_CITY => $address->getCity(),
  42. AddressInterface::KEY_FIRSTNAME => $address->getFirstname(),
  43. AddressInterface::KEY_LASTNAME => $address->getLastname(),
  44. AddressInterface::KEY_CUSTOMER_ID => $address->getCustomerId(),
  45. AddressInterface::KEY_EMAIL => $address->getEmail(),
  46. AddressInterface::SAME_AS_BILLING => $address->getSameAsBilling(),
  47. AddressInterface::CUSTOMER_ADDRESS_ID => $address->getCustomerAddressId(),
  48. AddressInterface::SAVE_IN_ADDRESS_BOOK => $address->getSaveInAddressBook()
  49. ];
  50. $cartId = $quote->getId();
  51. $serviceInfo = [
  52. 'rest' => [
  53. 'resourcePath' => self::RESOURCE_PATH . $cartId . '/billing-address',
  54. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  55. ],
  56. 'soap' => [
  57. 'service' => self::SERVICE_NAME,
  58. 'serviceVersion' => self::SERVICE_VERSION,
  59. 'operation' => self::SERVICE_NAME . 'Get',
  60. ],
  61. ];
  62. $requestData = ["cartId" => $cartId];
  63. $response = $this->_webApiCall($serviceInfo, $requestData);
  64. asort($data);
  65. asort($response);
  66. $this->assertEquals($data, $response);
  67. }
  68. /**
  69. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  70. */
  71. public function testSetAddress()
  72. {
  73. /** @var \Magento\Quote\Model\Quote $quote */
  74. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  75. $quote->load('test_order_1', 'reserved_order_id');
  76. $serviceInfo = [
  77. 'rest' => [
  78. 'resourcePath' => self::RESOURCE_PATH . $quote->getId() . '/billing-address',
  79. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  80. ],
  81. 'soap' => [
  82. 'service' => self::SERVICE_NAME,
  83. 'serviceVersion' => self::SERVICE_VERSION,
  84. 'operation' => self::SERVICE_NAME . 'Assign',
  85. ],
  86. ];
  87. $addressData = [
  88. 'firstname' => 'John',
  89. 'lastname' => 'Smith',
  90. 'email' => '',
  91. 'company' => 'Magento Commerce Inc.',
  92. 'street' => ['Typical Street', 'Tiny House 18'],
  93. 'city' => 'Big City',
  94. 'region_id' => 12,
  95. 'region' => 'California',
  96. 'region_code' => 'CA',
  97. 'postcode' => '0985432',
  98. 'country_id' => 'US',
  99. 'telephone' => '88776655',
  100. 'fax' => '44332255',
  101. ];
  102. $requestData = [
  103. "cartId" => $quote->getId(),
  104. 'address' => $addressData,
  105. ];
  106. $addressId = $this->_webApiCall($serviceInfo, $requestData);
  107. //reset $quote to reload data
  108. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  109. $quote->load('test_order_1', 'reserved_order_id');
  110. $address = $quote->getBillingAddress();
  111. $address->getRegionCode();
  112. $savedData = $address->getData();
  113. $this->assertEquals($addressId, $savedData['address_id']);
  114. //custom checks for street, region and address_type
  115. foreach ($addressData['street'] as $streetLine) {
  116. $this->assertContains($streetLine, $quote->getBillingAddress()->getStreet());
  117. }
  118. unset($addressData['street']);
  119. unset($addressData['email']);
  120. $this->assertEquals('billing', $savedData['address_type']);
  121. //check the rest of fields
  122. foreach ($addressData as $key => $value) {
  123. $this->assertEquals($value, $savedData[$key]);
  124. }
  125. }
  126. /**
  127. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  128. */
  129. public function testGetMyAddress()
  130. {
  131. $this->_markTestAsRestOnly();
  132. // get customer ID token
  133. /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
  134. $customerTokenService = $this->objectManager->create(
  135. \Magento\Integration\Api\CustomerTokenServiceInterface::class
  136. );
  137. $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
  138. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  139. $quote->load('test_order_1', 'reserved_order_id');
  140. /** @var \Magento\Quote\Model\Quote\Address $address */
  141. $address = $quote->getBillingAddress();
  142. $data = [
  143. AddressInterface::KEY_ID => (int)$address->getId(),
  144. AddressInterface::KEY_REGION => $address->getRegion(),
  145. AddressInterface::KEY_REGION_ID => $address->getRegionId(),
  146. AddressInterface::KEY_REGION_CODE => $address->getRegionCode(),
  147. AddressInterface::KEY_COUNTRY_ID => $address->getCountryId(),
  148. AddressInterface::KEY_STREET => $address->getStreet(),
  149. AddressInterface::KEY_COMPANY => $address->getCompany(),
  150. AddressInterface::KEY_TELEPHONE => $address->getTelephone(),
  151. AddressInterface::KEY_POSTCODE => $address->getPostcode(),
  152. AddressInterface::KEY_CITY => $address->getCity(),
  153. AddressInterface::KEY_FIRSTNAME => $address->getFirstname(),
  154. AddressInterface::KEY_LASTNAME => $address->getLastname(),
  155. AddressInterface::KEY_CUSTOMER_ID => $address->getCustomerId(),
  156. AddressInterface::KEY_EMAIL => $address->getEmail(),
  157. AddressInterface::SAME_AS_BILLING => $address->getSameAsBilling(),
  158. AddressInterface::CUSTOMER_ADDRESS_ID => $address->getCustomerAddressId(),
  159. AddressInterface::SAVE_IN_ADDRESS_BOOK => $address->getSaveInAddressBook()
  160. ];
  161. $serviceInfo = [
  162. 'rest' => [
  163. 'resourcePath' => self::RESOURCE_PATH . 'mine/billing-address',
  164. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
  165. 'token' => $token
  166. ],
  167. ];
  168. $response = $this->_webApiCall($serviceInfo);
  169. asort($data);
  170. asort($response);
  171. $this->assertEquals($data, $response);
  172. }
  173. /**
  174. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
  175. */
  176. public function testSetMyAddress()
  177. {
  178. $this->_markTestAsRestOnly();
  179. // get customer ID token
  180. /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
  181. $customerTokenService = $this->objectManager->create(
  182. \Magento\Integration\Api\CustomerTokenServiceInterface::class
  183. );
  184. $token = $customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
  185. /** @var \Magento\Quote\Model\Quote $quote */
  186. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  187. $quote->load('test_order_1', 'reserved_order_id');
  188. $serviceInfo = [
  189. 'rest' => [
  190. 'resourcePath' => self::RESOURCE_PATH . 'mine/billing-address',
  191. 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
  192. 'token' => $token
  193. ],
  194. ];
  195. $addressData = [
  196. 'firstname' => 'John',
  197. 'lastname' => 'Smith',
  198. 'company' => 'Magento Commerce Inc.',
  199. 'street' => ['Typical Street', 'Tiny House 18'],
  200. 'city' => 'Big City',
  201. 'region_id' => 12,
  202. 'region' => 'California',
  203. 'region_code' => 'CA',
  204. 'postcode' => '0985432',
  205. 'country_id' => 'US',
  206. 'telephone' => '88776655',
  207. 'fax' => '44332255',
  208. ];
  209. $requestData = [
  210. 'address' => $addressData,
  211. ];
  212. $addressId = $this->_webApiCall($serviceInfo, $requestData);
  213. //reset $quote to reload data
  214. $quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
  215. $quote->load('test_order_1', 'reserved_order_id');
  216. $address = $quote->getBillingAddress();
  217. $address->getRegionCode();
  218. $savedData = $address->getData();
  219. $this->assertEquals($addressId, $savedData['address_id']);
  220. //custom checks for street, region and address_type
  221. foreach ($addressData['street'] as $streetLine) {
  222. $this->assertContains($streetLine, $quote->getBillingAddress()->getStreet());
  223. }
  224. unset($addressData['street']);
  225. $this->assertEquals('billing', $savedData['address_type']);
  226. //check the rest of fields
  227. foreach ($addressData as $key => $value) {
  228. $this->assertEquals($value, $savedData[$key]);
  229. }
  230. }
  231. }