|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Customer; |
| 9 | + |
| 10 | +use Exception; |
| 11 | +use Magento\Customer\Api\CustomerMetadataInterface; |
| 12 | +use Magento\Customer\Test\Fixture\Customer; |
| 13 | +use Magento\Eav\Test\Fixture\Attribute; |
| 14 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 15 | +use Magento\TestFramework\Fixture\DataFixture; |
| 16 | +use Magento\Framework\Exception\AuthenticationException; |
| 17 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 18 | +use Magento\TestFramework\Helper\Bootstrap; |
| 19 | + |
| 20 | +#[ |
| 21 | + DataFixture(Customer::class, ['email' => 'customer@example.com'], as: 'customer'), |
| 22 | + DataFixture( |
| 23 | + Attribute::class, |
| 24 | + [ |
| 25 | + 'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER, |
| 26 | + 'is_required' => true, |
| 27 | + ], |
| 28 | + 'customer_attribute' |
| 29 | + ) |
| 30 | +] |
| 31 | +class CustomerEmailUpdateTest extends GraphQlAbstract |
| 32 | +{ |
| 33 | + /** |
| 34 | + * Test customer email update |
| 35 | + * |
| 36 | + * @return void |
| 37 | + * @throws Exception |
| 38 | + */ |
| 39 | + public function testUpdateCustomerEmail(): void |
| 40 | + { |
| 41 | + $response = $this->graphQlMutation( |
| 42 | + $this->getQuery('newcustomer@example.com', 'password'), |
| 43 | + [], |
| 44 | + '', |
| 45 | + $this->getCustomerAuthHeaders('customer@example.com', 'password') |
| 46 | + ); |
| 47 | + |
| 48 | + $this->assertEquals('newcustomer@example.com', $response['updateCustomerEmail']['customer']['email']); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Test customer email update with empty fields |
| 53 | + * |
| 54 | + * @param string $email |
| 55 | + * @param string $password |
| 56 | + * @param string $message |
| 57 | + * @dataProvider customerInputFieldDataProvider |
| 58 | + * @return void |
| 59 | + * @throws AuthenticationException |
| 60 | + */ |
| 61 | + public function testUpdateCustomerEmailWithEmptyFields( |
| 62 | + string $email, |
| 63 | + string $password, |
| 64 | + string $message |
| 65 | + ): void { |
| 66 | + $this->expectException(Exception::class); |
| 67 | + $this->expectExceptionMessage($message); |
| 68 | + $this->graphQlMutation( |
| 69 | + $this->getQuery($email, $password), |
| 70 | + [], |
| 71 | + '', |
| 72 | + $this->getCustomerAuthHeaders('customer@example.com', 'password') |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Data provider for testUpdateCustomerEmailWithEmptyFields |
| 78 | + * |
| 79 | + * @return array |
| 80 | + */ |
| 81 | + public static function customerInputFieldDataProvider(): array |
| 82 | + { |
| 83 | + return [ |
| 84 | + [ |
| 85 | + 'email' => 'newcustomer@example.com', |
| 86 | + 'password' => '', |
| 87 | + 'message' => 'Provide the current "password" to change "email".', |
| 88 | + ], |
| 89 | + [ |
| 90 | + 'email' => '', |
| 91 | + 'password' => 'password', |
| 92 | + 'message' => '"" is not a valid email address.', |
| 93 | + ] |
| 94 | + ]; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get customer email update mutation |
| 99 | + * |
| 100 | + * @param string $newEmail |
| 101 | + * @param string $currentPassword |
| 102 | + * @return string |
| 103 | + */ |
| 104 | + private function getQuery(string $newEmail, string $currentPassword): string |
| 105 | + { |
| 106 | + return <<<MUTATION |
| 107 | + mutation { |
| 108 | + updateCustomerEmail( |
| 109 | + email: "{$newEmail}" |
| 110 | + password: "{$currentPassword}" |
| 111 | + ) { |
| 112 | + customer { |
| 113 | + email |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + MUTATION; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Get customer authorization headers |
| 122 | + * |
| 123 | + * @param string $email |
| 124 | + * @param string $password |
| 125 | + * @return array |
| 126 | + * @throws AuthenticationException |
| 127 | + */ |
| 128 | + private function getCustomerAuthHeaders(string $email, string $password): array |
| 129 | + { |
| 130 | + $customerToken = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class) |
| 131 | + ->createCustomerAccessToken($email, $password); |
| 132 | + |
| 133 | + return ['Authorization' => 'Bearer ' . $customerToken]; |
| 134 | + } |
| 135 | +} |
0 commit comments