UpdateCustomerTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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\Model\CustomerAuthUpdate;
  9. use Magento\Customer\Model\CustomerRegistry;
  10. use Magento\Integration\Api\CustomerTokenServiceInterface;
  11. use Magento\TestFramework\Helper\Bootstrap;
  12. use Magento\TestFramework\TestCase\GraphQlAbstract;
  13. class UpdateCustomerTest extends GraphQlAbstract
  14. {
  15. /**
  16. * @var CustomerTokenServiceInterface
  17. */
  18. private $customerTokenService;
  19. /**
  20. * @var CustomerRegistry
  21. */
  22. private $customerRegistry;
  23. /**
  24. * @var CustomerAuthUpdate
  25. */
  26. private $customerAuthUpdate;
  27. protected function setUp()
  28. {
  29. parent::setUp();
  30. $this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
  31. $this->customerRegistry = Bootstrap::getObjectManager()->get(CustomerRegistry::class);
  32. $this->customerAuthUpdate = Bootstrap::getObjectManager()->get(CustomerAuthUpdate::class);
  33. }
  34. /**
  35. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  36. */
  37. public function testUpdateCustomer()
  38. {
  39. $currentEmail = 'customer@example.com';
  40. $currentPassword = 'password';
  41. $newFirstname = 'Richard';
  42. $newLastname = 'Rowe';
  43. $newEmail = 'customer_updated@example.com';
  44. $query = <<<QUERY
  45. mutation {
  46. updateCustomer(
  47. input: {
  48. firstname: "{$newFirstname}"
  49. lastname: "{$newLastname}"
  50. email: "{$newEmail}"
  51. password: "{$currentPassword}"
  52. }
  53. ) {
  54. customer {
  55. firstname
  56. lastname
  57. email
  58. }
  59. }
  60. }
  61. QUERY;
  62. $response = $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
  63. $this->assertEquals($newFirstname, $response['updateCustomer']['customer']['firstname']);
  64. $this->assertEquals($newLastname, $response['updateCustomer']['customer']['lastname']);
  65. $this->assertEquals($newEmail, $response['updateCustomer']['customer']['email']);
  66. }
  67. /**
  68. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  69. * @expectedException \Exception
  70. * @expectedExceptionMessage "input" value should be specified
  71. */
  72. public function testUpdateCustomerIfInputDataIsEmpty()
  73. {
  74. $currentEmail = 'customer@example.com';
  75. $currentPassword = 'password';
  76. $query = <<<QUERY
  77. mutation {
  78. updateCustomer(
  79. input: {
  80. }
  81. ) {
  82. customer {
  83. firstname
  84. }
  85. }
  86. }
  87. QUERY;
  88. $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
  89. }
  90. /**
  91. * @expectedException \Exception
  92. * @expectedExceptionMessage The current customer isn't authorized.
  93. */
  94. public function testUpdateCustomerIfUserIsNotAuthorized()
  95. {
  96. $newFirstname = 'Richard';
  97. $query = <<<QUERY
  98. mutation {
  99. updateCustomer(
  100. input: {
  101. firstname: "{$newFirstname}"
  102. }
  103. ) {
  104. customer {
  105. firstname
  106. }
  107. }
  108. }
  109. QUERY;
  110. $this->graphQlQuery($query);
  111. }
  112. /**
  113. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  114. * @expectedException \Exception
  115. * @expectedExceptionMessage The account is locked.
  116. */
  117. public function testUpdateCustomerIfAccountIsLocked()
  118. {
  119. $this->lockCustomer(1);
  120. $currentEmail = 'customer@example.com';
  121. $currentPassword = 'password';
  122. $newFirstname = 'Richard';
  123. $query = <<<QUERY
  124. mutation {
  125. updateCustomer(
  126. input: {
  127. firstname: "{$newFirstname}"
  128. }
  129. ) {
  130. customer {
  131. firstname
  132. }
  133. }
  134. }
  135. QUERY;
  136. $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
  137. }
  138. /**
  139. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  140. * @expectedException \Exception
  141. * @expectedExceptionMessage Provide the current "password" to change "email".
  142. */
  143. public function testUpdateEmailIfPasswordIsMissed()
  144. {
  145. $currentEmail = 'customer@example.com';
  146. $currentPassword = 'password';
  147. $newEmail = 'customer_updated@example.com';
  148. $query = <<<QUERY
  149. mutation {
  150. updateCustomer(
  151. input: {
  152. email: "{$newEmail}"
  153. }
  154. ) {
  155. customer {
  156. firstname
  157. }
  158. }
  159. }
  160. QUERY;
  161. $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
  162. }
  163. /**
  164. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  165. * @expectedException \Exception
  166. * @expectedExceptionMessage The password doesn't match this account. Verify the password and try again.
  167. */
  168. public function testUpdateEmailIfPasswordIsInvalid()
  169. {
  170. $currentEmail = 'customer@example.com';
  171. $currentPassword = 'password';
  172. $invalidPassword = 'invalid_password';
  173. $newEmail = 'customer_updated@example.com';
  174. $query = <<<QUERY
  175. mutation {
  176. updateCustomer(
  177. input: {
  178. email: "{$newEmail}"
  179. password: "{$invalidPassword}"
  180. }
  181. ) {
  182. customer {
  183. firstname
  184. }
  185. }
  186. }
  187. QUERY;
  188. $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
  189. }
  190. /**
  191. * @magentoApiDataFixture Magento/Customer/_files/two_customers.php
  192. * @expectedException \Exception
  193. * @expectedExceptionMessage A customer with the same email address already exists in an associated website.
  194. */
  195. public function testUpdateEmailIfEmailAlreadyExists()
  196. {
  197. $currentEmail = 'customer@example.com';
  198. $currentPassword = 'password';
  199. $existedEmail = 'customer_two@example.com';
  200. $query = <<<QUERY
  201. mutation {
  202. updateCustomer(
  203. input: {
  204. email: "{$existedEmail}"
  205. password: "{$currentPassword}"
  206. }
  207. ) {
  208. customer {
  209. firstname
  210. }
  211. }
  212. }
  213. QUERY;
  214. $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
  215. }
  216. /**
  217. * @param string $email
  218. * @param string $password
  219. * @return array
  220. */
  221. private function getCustomerAuthHeaders(string $email, string $password): array
  222. {
  223. $customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
  224. return ['Authorization' => 'Bearer ' . $customerToken];
  225. }
  226. /**
  227. * @param int $customerId
  228. * @return void
  229. */
  230. private function lockCustomer(int $customerId): void
  231. {
  232. $customerSecure = $this->customerRegistry->retrieveSecureData($customerId);
  233. $customerSecure->setLockExpires('2030-12-31 00:00:00');
  234. $this->customerAuthUpdate->saveAuth($customerId);
  235. }
  236. }