DeleteCustomerAddressTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\TestFramework\Helper\Bootstrap;
  11. use Magento\TestFramework\TestCase\GraphQlAbstract;
  12. use Magento\Integration\Api\CustomerTokenServiceInterface;
  13. class DeleteCustomerAddressTest extends GraphQlAbstract
  14. {
  15. /**
  16. * @var CustomerTokenServiceInterface
  17. */
  18. private $customerTokenService;
  19. /**
  20. * @var CustomerRepositoryInterface
  21. */
  22. private $customerRepository;
  23. /**
  24. * @var AddressRepositoryInterface
  25. */
  26. private $addressRepository;
  27. protected function setUp()
  28. {
  29. parent::setUp();
  30. $this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
  31. $this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
  32. $this->addressRepository = Bootstrap::getObjectManager()->get(AddressRepositoryInterface::class);
  33. }
  34. /**
  35. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  36. * @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php
  37. */
  38. public function testDeleteCustomerAddress()
  39. {
  40. $userName = 'customer@example.com';
  41. $password = 'password';
  42. $addressId = 2;
  43. $mutation
  44. = <<<MUTATION
  45. mutation {
  46. deleteCustomerAddress(id: {$addressId})
  47. }
  48. MUTATION;
  49. $response = $this->graphQlQuery($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
  50. $this->assertArrayHasKey('deleteCustomerAddress', $response);
  51. $this->assertEquals(true, $response['deleteCustomerAddress']);
  52. }
  53. /**
  54. * @expectedException \Exception
  55. * @expectedExceptionMessage The current customer isn't authorized.
  56. */
  57. public function testDeleteCustomerAddressIfUserIsNotAuthorized()
  58. {
  59. $addressId = 1;
  60. $mutation
  61. = <<<MUTATION
  62. mutation {
  63. deleteCustomerAddress(id: {$addressId})
  64. }
  65. MUTATION;
  66. $this->graphQlQuery($mutation);
  67. }
  68. /**
  69. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  70. * @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php
  71. *
  72. * @expectedException \Exception
  73. * @expectedExceptionMessage Customer Address 2 is set as default shipping address and can not be deleted
  74. */
  75. public function testDeleteDefaultShippingCustomerAddress()
  76. {
  77. $userName = 'customer@example.com';
  78. $password = 'password';
  79. $addressId = 2;
  80. $address = $this->addressRepository->getById($addressId);
  81. $address->setIsDefaultShipping(true);
  82. $this->addressRepository->save($address);
  83. $mutation
  84. = <<<MUTATION
  85. mutation {
  86. deleteCustomerAddress(id: {$addressId})
  87. }
  88. MUTATION;
  89. $this->graphQlQuery($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
  90. }
  91. /**
  92. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  93. * @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php
  94. *
  95. * @expectedException \Exception
  96. * @expectedExceptionMessage Customer Address 2 is set as default billing address and can not be deleted
  97. */
  98. public function testDeleteDefaultBillingCustomerAddress()
  99. {
  100. $userName = 'customer@example.com';
  101. $password = 'password';
  102. $addressId = 2;
  103. $address = $this->addressRepository->getById($addressId);
  104. $address->setIsDefaultBilling(true);
  105. $this->addressRepository->save($address);
  106. $mutation
  107. = <<<MUTATION
  108. mutation {
  109. deleteCustomerAddress(id: {$addressId})
  110. }
  111. MUTATION;
  112. $this->graphQlQuery($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
  113. }
  114. /**
  115. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  116. *
  117. * @expectedException \Exception
  118. * @expectedExceptionMessage Address id 9999 does not exist.
  119. */
  120. public function testDeleteNonExistCustomerAddress()
  121. {
  122. $userName = 'customer@example.com';
  123. $password = 'password';
  124. $mutation
  125. = <<<MUTATION
  126. mutation {
  127. deleteCustomerAddress(id: 9999)
  128. }
  129. MUTATION;
  130. $this->graphQlQuery($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
  131. }
  132. /**
  133. * @param string $email
  134. * @param string $password
  135. * @return array
  136. */
  137. private function getCustomerAuthHeaders(string $email, string $password): array
  138. {
  139. $customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
  140. return ['Authorization' => 'Bearer ' . $customerToken];
  141. }
  142. }