CreateCustomerAddressTest.php 8.1 KB

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