|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2024 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + * |
| 6 | + * NOTICE: All information contained herein is, and remains |
| 7 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 8 | + * and technical concepts contained herein are proprietary to Adobe |
| 9 | + * and its suppliers and are protected by all applicable intellectual |
| 10 | + * property laws, including trade secret and copyright laws. |
| 11 | + * Dissemination of this information or reproduction of this material |
| 12 | + * is strictly forbidden unless prior written permission is obtained from |
| 13 | + * Adobe. |
| 14 | + */ |
| 15 | +declare(strict_types=1); |
| 16 | + |
| 17 | +namespace Magento\GraphQl\Customer; |
| 18 | + |
| 19 | +use Magento\Customer\Test\Fixture\Customer as CustomerFixture; |
| 20 | +use Magento\Framework\Exception\AuthenticationException; |
| 21 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 22 | +use Magento\TestFramework\Fixture\DataFixture; |
| 23 | +use Magento\TestFramework\Helper\Bootstrap; |
| 24 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 25 | + |
| 26 | +/** |
| 27 | + * Tests for confirm customer email |
| 28 | + */ |
| 29 | +#[ |
| 30 | + DataFixture( |
| 31 | + CustomerFixture::class, |
| 32 | + [ |
| 33 | + 'email' => 'customer@example.com', |
| 34 | + 'confirmation' => 'abcde', |
| 35 | + ], |
| 36 | + 'customer' |
| 37 | + ) |
| 38 | +] |
| 39 | +class ConfirmEmailTest extends GraphQlAbstract |
| 40 | +{ |
| 41 | + private const QUERY = <<<QUERY |
| 42 | +mutation { |
| 43 | + confirmEmail(input: { |
| 44 | + email: "%s" |
| 45 | + confirmation_key: "%s" |
| 46 | + }) { |
| 47 | + customer { |
| 48 | + email |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | +QUERY; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var string |
| 56 | + */ |
| 57 | + private const PASSWORD = 'password'; |
| 58 | + |
| 59 | + /** |
| 60 | + * @return void |
| 61 | + * @throws AuthenticationException |
| 62 | + */ |
| 63 | + public function testConfirmEmail() |
| 64 | + { |
| 65 | + $response = $this->graphQlMutation( |
| 66 | + sprintf( |
| 67 | + self::QUERY, |
| 68 | + 'customer@example.com', |
| 69 | + 'abcde', |
| 70 | + ), |
| 71 | + [], |
| 72 | + '', |
| 73 | + $this->getCustomerAuthHeaders('customer@example.com', self::PASSWORD) |
| 74 | + ); |
| 75 | + |
| 76 | + $this->assertEquals( |
| 77 | + [ |
| 78 | + 'confirmEmail' => [ |
| 79 | + 'customer' => [ |
| 80 | + 'email' => 'customer@example.com' |
| 81 | + ] |
| 82 | + ] |
| 83 | + ], |
| 84 | + $response |
| 85 | + ); |
| 86 | + |
| 87 | + $this->expectException(\Exception::class); |
| 88 | + $this->expectExceptionMessage('The account is already active.'); |
| 89 | + |
| 90 | + $this->graphQlMutation( |
| 91 | + sprintf( |
| 92 | + self::QUERY, |
| 93 | + 'customer@example.com', |
| 94 | + 'abcde', |
| 95 | + ), |
| 96 | + [], |
| 97 | + '', |
| 98 | + $this->getCustomerAuthHeaders('customer@example.com', self::PASSWORD) |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @return void |
| 104 | + * @throws AuthenticationException |
| 105 | + */ |
| 106 | + public function testConfirmEmailWrongEmail() |
| 107 | + { |
| 108 | + $this->expectException(\Exception::class); |
| 109 | + |
| 110 | + $this->graphQlMutation( |
| 111 | + sprintf( |
| 112 | + self::QUERY, |
| 113 | + 'bad-email', |
| 114 | + 'abcde', |
| 115 | + ), |
| 116 | + [], |
| 117 | + '', |
| 118 | + $this->getCustomerAuthHeaders('customer@example.com', self::PASSWORD) |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @return void |
| 124 | + * @throws AuthenticationException |
| 125 | + */ |
| 126 | + public function testConfirmEmailWrongConfirmation() |
| 127 | + { |
| 128 | + $this->expectException(\Exception::class); |
| 129 | + |
| 130 | + $this->graphQlMutation( |
| 131 | + sprintf( |
| 132 | + self::QUERY, |
| 133 | + 'customer@example.com', |
| 134 | + 'wrong-confirmation', |
| 135 | + ), |
| 136 | + [], |
| 137 | + '', |
| 138 | + $this->getCustomerAuthHeaders('customer@example.com', self::PASSWORD) |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @param string $email |
| 144 | + * @param string $password |
| 145 | + * @return array |
| 146 | + * @throws AuthenticationException |
| 147 | + */ |
| 148 | + private function getCustomerAuthHeaders(string $email, string $password): array |
| 149 | + { |
| 150 | + $customerToken = Bootstrap::getObjectManager()->get( |
| 151 | + CustomerTokenServiceInterface::class |
| 152 | + )->createCustomerAccessToken( |
| 153 | + $email, |
| 154 | + $password |
| 155 | + ); |
| 156 | + return ['Authorization' => 'Bearer ' . $customerToken]; |
| 157 | + } |
| 158 | +} |
0 commit comments