UpdateCustomerAddressTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GraphQl\Customer;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Customer\Api\AddressRepositoryInterface;
  10. use Magento\Customer\Api\Data\AddressInterface;
  11. use Magento\TestFramework\Helper\Bootstrap;
  12. use Magento\TestFramework\TestCase\GraphQlAbstract;
  13. use Magento\Integration\Api\CustomerTokenServiceInterface;
  14. class UpdateCustomerAddressTest extends GraphQlAbstract
  15. {
  16. /**
  17. * @var CustomerTokenServiceInterface
  18. */
  19. private $customerTokenService;
  20. /**
  21. * @var CustomerRepositoryInterface
  22. */
  23. private $customerRepository;
  24. /**
  25. * @var AddressRepositoryInterface
  26. */
  27. private $addressRepository;
  28. protected function setUp()
  29. {
  30. parent::setUp();
  31. $this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
  32. $this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
  33. $this->addressRepository = Bootstrap::getObjectManager()->get(AddressRepositoryInterface::class);
  34. }
  35. /**
  36. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  37. * @magentoApiDataFixture Magento/Customer/_files/customer_address.php
  38. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  39. */
  40. public function testUpdateCustomerAddress()
  41. {
  42. $userName = 'customer@example.com';
  43. $password = 'password';
  44. $customerId = 1;
  45. $addressId = 1;
  46. $updateAddress = [
  47. 'region' => [
  48. 'region' => 'Alaska',
  49. 'region_id' => 2,
  50. 'region_code' => 'AK'
  51. ],
  52. 'country_id' => 'US',
  53. 'street' => ['Line 1 Street', 'Line 2'],
  54. 'company' => 'Company Name',
  55. 'telephone' => '123456789',
  56. 'fax' => '123123123',
  57. 'postcode' => '7777',
  58. 'city' => 'City Name',
  59. 'firstname' => 'Adam',
  60. 'lastname' => 'Phillis',
  61. 'middlename' => 'A',
  62. 'prefix' => 'Mr.',
  63. 'suffix' => 'Jr.',
  64. 'vat_id' => '1',
  65. 'default_shipping' => true,
  66. 'default_billing' => true
  67. ];
  68. $defaultShippingText = $updateAddress['default_shipping'] ? "true": "false";
  69. $defaultBillingText = $updateAddress['default_billing'] ? "true": "false";
  70. $mutation
  71. = <<<MUTATION
  72. mutation {
  73. updateCustomerAddress(id: {$addressId}, input: {
  74. region: {
  75. region: "{$updateAddress['region']['region']}"
  76. region_id: {$updateAddress['region']['region_id']}
  77. region_code: "{$updateAddress['region']['region_code']}"
  78. }
  79. country_id: {$updateAddress['country_id']}
  80. street: ["{$updateAddress['street'][0]}","{$updateAddress['street'][1]}"]
  81. company: "{$updateAddress['company']}"
  82. telephone: "{$updateAddress['telephone']}"
  83. fax: "{$updateAddress['fax']}"
  84. postcode: "{$updateAddress['postcode']}"
  85. city: "{$updateAddress['city']}"
  86. firstname: "{$updateAddress['firstname']}"
  87. lastname: "{$updateAddress['lastname']}"
  88. middlename: "{$updateAddress['middlename']}"
  89. prefix: "{$updateAddress['prefix']}"
  90. suffix: "{$updateAddress['suffix']}"
  91. vat_id: "{$updateAddress['vat_id']}"
  92. default_shipping: {$defaultShippingText}
  93. default_billing: {$defaultBillingText}
  94. }) {
  95. id
  96. customer_id
  97. region {
  98. region
  99. region_id
  100. region_code
  101. }
  102. country_id
  103. street
  104. company
  105. telephone
  106. fax
  107. postcode
  108. city
  109. firstname
  110. lastname
  111. middlename
  112. prefix
  113. suffix
  114. vat_id
  115. default_shipping
  116. default_billing
  117. }
  118. }
  119. MUTATION;
  120. $response = $this->graphQlQuery($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
  121. $this->assertArrayHasKey('updateCustomerAddress', $response);
  122. $this->assertArrayHasKey('customer_id', $response['updateCustomerAddress']);
  123. $this->assertEquals($customerId, $response['updateCustomerAddress']['customer_id']);
  124. $this->assertArrayHasKey('id', $response['updateCustomerAddress']);
  125. $address = $this->addressRepository->getById($addressId);
  126. $this->assertEquals($address->getId(), $response['updateCustomerAddress']['id']);
  127. $this->assertCustomerAddressesFields($address, $response['updateCustomerAddress']);
  128. $this->assertCustomerAddressesFields($address, $updateAddress);
  129. }
  130. /**
  131. * @expectedException \Exception
  132. * @expectedExceptionMessage The current customer isn't authorized.
  133. */
  134. public function testUpdateCustomerAddressIfUserIsNotAuthorized()
  135. {
  136. $addressId = 1;
  137. $mutation
  138. = <<<MUTATION
  139. mutation {
  140. updateCustomerAddress(id:{$addressId}, input: {
  141. city: "New City"
  142. postcode: "5555"
  143. }) {
  144. id
  145. }
  146. }
  147. MUTATION;
  148. $this->graphQlQuery($mutation);
  149. }
  150. /**
  151. * Verify customers with credentials update address
  152. * with missing required Firstname attribute
  153. *
  154. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  155. * @magentoApiDataFixture Magento/Customer/_files/customer_address.php
  156. * @expectedException \Exception
  157. * @expectedExceptionMessage Required parameters are missing: firstname
  158. */
  159. public function testUpdateCustomerAddressWithMissingAttribute()
  160. {
  161. $userName = 'customer@example.com';
  162. $password = 'password';
  163. $addressId = 1;
  164. $mutation
  165. = <<<MUTATION
  166. mutation {
  167. updateCustomerAddress(id: {$addressId}, input: {
  168. firstname: ""
  169. lastname: "Phillis"
  170. }) {
  171. id
  172. }
  173. }
  174. MUTATION;
  175. $this->graphQlQuery($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
  176. }
  177. /**
  178. * Verify the fields for Customer address
  179. *
  180. * @param AddressInterface $address
  181. * @param array $actualResponse
  182. */
  183. private function assertCustomerAddressesFields(AddressInterface $address, $actualResponse): void
  184. {
  185. /** @var $addresses */
  186. $assertionMap = [
  187. ['response_field' => 'country_id', 'expected_value' => $address->getCountryId()],
  188. ['response_field' => 'street', 'expected_value' => $address->getStreet()],
  189. ['response_field' => 'company', 'expected_value' => $address->getCompany()],
  190. ['response_field' => 'telephone', 'expected_value' => $address->getTelephone()],
  191. ['response_field' => 'fax', 'expected_value' => $address->getFax()],
  192. ['response_field' => 'postcode', 'expected_value' => $address->getPostcode()],
  193. ['response_field' => 'city', 'expected_value' => $address->getCity()],
  194. ['response_field' => 'firstname', 'expected_value' => $address->getFirstname()],
  195. ['response_field' => 'lastname', 'expected_value' => $address->getLastname()],
  196. ['response_field' => 'middlename', 'expected_value' => $address->getMiddlename()],
  197. ['response_field' => 'prefix', 'expected_value' => $address->getPrefix()],
  198. ['response_field' => 'suffix', 'expected_value' => $address->getSuffix()],
  199. ['response_field' => 'vat_id', 'expected_value' => $address->getVatId()],
  200. ['response_field' => 'default_shipping', 'expected_value' => (bool)$address->isDefaultShipping()],
  201. ['response_field' => 'default_billing', 'expected_value' => (bool)$address->isDefaultBilling()],
  202. ];
  203. $this->assertResponseFields($actualResponse, $assertionMap);
  204. $this->assertTrue(is_array([$actualResponse['region']]), "region field must be of an array type.");
  205. $assertionRegionMap = [
  206. ['response_field' => 'region', 'expected_value' => $address->getRegion()->getRegion()],
  207. ['response_field' => 'region_code', 'expected_value' => $address->getRegion()->getRegionCode()],
  208. ['response_field' => 'region_id', 'expected_value' => $address->getRegion()->getRegionId()]
  209. ];
  210. $this->assertResponseFields($actualResponse['region'], $assertionRegionMap);
  211. }
  212. /**
  213. * @param string $email
  214. * @param string $password
  215. * @return array
  216. */
  217. private function getCustomerAuthHeaders(string $email, string $password): array
  218. {
  219. $customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
  220. return ['Authorization' => 'Bearer ' . $customerToken];
  221. }
  222. }