|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Customer; |
| 9 | + |
| 10 | +use Exception; |
| 11 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 12 | +use Magento\CustomerGraphQl\Model\Customer\UpdateCustomerAccount; |
| 13 | +use Magento\Framework\Exception\AuthenticationException; |
| 14 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 15 | +use Magento\Store\Api\StoreRepositoryInterface; |
| 16 | +use Magento\TestFramework\Helper\Bootstrap; |
| 17 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 18 | + |
| 19 | +/** |
| 20 | + * Test for update customer's email |
| 21 | + */ |
| 22 | +class UpdateCustomerEmailTest extends GraphQlAbstract |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var CustomerTokenServiceInterface |
| 26 | + */ |
| 27 | + private $customerTokenService; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var CustomerRepositoryInterface |
| 31 | + */ |
| 32 | + private $customerRepository; |
| 33 | + /** |
| 34 | + * @var UpdateCustomerAccount |
| 35 | + */ |
| 36 | + private $updateCustomerAccount; |
| 37 | + /** |
| 38 | + * @var StoreRepositoryInterface |
| 39 | + */ |
| 40 | + private $storeRepository; |
| 41 | + |
| 42 | + /** |
| 43 | + * Setting up tests |
| 44 | + */ |
| 45 | + protected function setUp(): void |
| 46 | + { |
| 47 | + parent::setUp(); |
| 48 | + |
| 49 | + $this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class); |
| 50 | + $this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class); |
| 51 | + $this->updateCustomerAccount = Bootstrap::getObjectManager()->get(UpdateCustomerAccount::class); |
| 52 | + $this->storeRepository = Bootstrap::getObjectManager()->get(StoreRepositoryInterface::class); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 57 | + */ |
| 58 | + public function testUpdateCustomerEmail(): void |
| 59 | + { |
| 60 | + $currentEmail = '[email protected]'; |
| 61 | + $currentPassword = 'password'; |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + $query = <<<QUERY |
| 66 | +mutation { |
| 67 | + updateCustomerEmail( |
| 68 | + email: "{$newEmail}" |
| 69 | + password: "{$currentPassword}" |
| 70 | + ) { |
| 71 | + customer { |
| 72 | + email |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | +QUERY; |
| 77 | + |
| 78 | + $response = $this->graphQlMutation( |
| 79 | + $query, |
| 80 | + [], |
| 81 | + '', |
| 82 | + $this->getCustomerAuthHeaders($currentEmail, $currentPassword) |
| 83 | + ); |
| 84 | + |
| 85 | + $this->assertEquals($newEmail, $response['updateCustomerEmail']['customer']['email']); |
| 86 | + |
| 87 | +/* $this->updateCustomerAccount->execute( |
| 88 | + $this->customerRepository->get($newEmail), |
| 89 | + ['email' => $currentEmail, 'password' => $currentPassword], |
| 90 | + $this->storeRepository->getById(1) |
| 91 | + );*/ |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 96 | + */ |
| 97 | + public function testUpdateCustomerEmailIfPasswordIsWrong(): void |
| 98 | + { |
| 99 | + $this->expectException(Exception::class); |
| 100 | + $this->expectExceptionMessage('Invalid login or password.'); |
| 101 | + |
| 102 | + $currentEmail = '[email protected]'; |
| 103 | + $currentPassword = 'password'; |
| 104 | + |
| 105 | + |
| 106 | + $wrongPassword = 'wrongpassword'; |
| 107 | + |
| 108 | + $query = <<<QUERY |
| 109 | +mutation { |
| 110 | + updateCustomerEmail( |
| 111 | + email: "{$newEmail}" |
| 112 | + password: "{$wrongPassword}" |
| 113 | + ) { |
| 114 | + customer { |
| 115 | + email |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | +QUERY; |
| 120 | + |
| 121 | + $this->graphQlMutation( |
| 122 | + $query, |
| 123 | + [], |
| 124 | + '', |
| 125 | + $this->getCustomerAuthHeaders($currentEmail, $currentPassword) |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @magentoApiDataFixture Magento/Customer/_files/two_customers.php |
| 131 | + */ |
| 132 | + public function testUpdateEmailIfEmailAlreadyExists() |
| 133 | + { |
| 134 | + $this->expectException(Exception::class); |
| 135 | + $this->expectExceptionMessage( |
| 136 | + 'A customer with the same email address already exists in an associated website.' |
| 137 | + ); |
| 138 | + |
| 139 | + $currentEmail = '[email protected]'; |
| 140 | + $currentPassword = 'password'; |
| 141 | + $existedEmail = '[email protected]'; |
| 142 | + |
| 143 | + $query = <<<QUERY |
| 144 | +mutation { |
| 145 | + updateCustomerEmail( |
| 146 | + email: "{$existedEmail}" |
| 147 | + password: "{$currentPassword}" |
| 148 | + ) { |
| 149 | + customer { |
| 150 | + firstname |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | +QUERY; |
| 155 | + $this->graphQlMutation($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword)); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Get customer authorization headers |
| 160 | + * |
| 161 | + * @param string $email |
| 162 | + * @param string $password |
| 163 | + * @return array |
| 164 | + * @throws AuthenticationException |
| 165 | + */ |
| 166 | + private function getCustomerAuthHeaders(string $email, string $password): array |
| 167 | + { |
| 168 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password); |
| 169 | + return ['Authorization' => 'Bearer ' . $customerToken]; |
| 170 | + } |
| 171 | +} |
0 commit comments