|
| 1 | +<?php |
| 2 | +/************************************************************************ |
| 3 | + * |
| 4 | + * Copyright 2024 Adobe |
| 5 | + * All Rights Reserved. |
| 6 | + * |
| 7 | + * NOTICE: All information contained herein is, and remains |
| 8 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 9 | + * and technical concepts contained herein are proprietary to Adobe |
| 10 | + * and its suppliers and are protected by all applicable intellectual |
| 11 | + * property laws, including trade secret and copyright laws. |
| 12 | + * Dissemination of this information or reproduction of this material |
| 13 | + * is strictly forbidden unless prior written permission is obtained |
| 14 | + * from Adobe. |
| 15 | + * ************************************************************************ |
| 16 | + */ |
| 17 | +declare(strict_types=1); |
| 18 | + |
| 19 | +namespace Magento\GraphQl\Customer; |
| 20 | + |
| 21 | +use Magento\Customer\Api\AccountManagementInterface; |
| 22 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 23 | +use Magento\Customer\Api\Data\CustomerInterface; |
| 24 | +use Magento\Customer\Test\Fixture\Customer; |
| 25 | +use Magento\Framework\Exception\AuthenticationException; |
| 26 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 27 | +use Magento\TestFramework\Fixture\Config; |
| 28 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 29 | +use Magento\TestFramework\Helper\Bootstrap; |
| 30 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 31 | +use Magento\TestFramework\Fixture\DataFixture; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests for confirmation status |
| 35 | + */ |
| 36 | +class ConfirmationStatusTest extends GraphQlAbstract |
| 37 | +{ |
| 38 | + /** |
| 39 | + * @var CustomerTokenServiceInterface |
| 40 | + */ |
| 41 | + private $customerTokenService; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var CustomerRepositoryInterface |
| 45 | + */ |
| 46 | + private $customerRepository; |
| 47 | + |
| 48 | + protected function setUp(): void |
| 49 | + { |
| 50 | + parent::setUp(); |
| 51 | + $this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class); |
| 52 | + $this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class); |
| 53 | + } |
| 54 | + |
| 55 | + #[ |
| 56 | + Config('customer/create_account/confirm', 0), |
| 57 | + DataFixture( |
| 58 | + Customer::class, |
| 59 | + [ |
| 60 | + |
| 61 | + ], |
| 62 | + 'customer' |
| 63 | + ) |
| 64 | + ] |
| 65 | + public function testGetConfirmationStatusConfirmationNotRequiredTest() |
| 66 | + { |
| 67 | + $customer = DataFixtureStorageManager::getStorage()->get('customer'); |
| 68 | + $query = <<<QUERY |
| 69 | +query { |
| 70 | + customer { |
| 71 | + confirmation_status |
| 72 | + } |
| 73 | +} |
| 74 | +QUERY; |
| 75 | + $response = $this->graphQlQuery( |
| 76 | + $query, |
| 77 | + [], |
| 78 | + '', |
| 79 | + $this->getHeaderMap($customer->getEmail(), 'password') |
| 80 | + ); |
| 81 | + $this->assertEquals( |
| 82 | + strtoupper(AccountManagementInterface::ACCOUNT_CONFIRMATION_NOT_REQUIRED), |
| 83 | + $response['customer']['confirmation_status'] |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + #[ |
| 88 | + Config('customer/create_account/confirm', 1), |
| 89 | + DataFixture( |
| 90 | + Customer::class, |
| 91 | + [ |
| 92 | + |
| 93 | + ], |
| 94 | + 'customer' |
| 95 | + ) |
| 96 | + ] |
| 97 | + public function testGetConfirmationStatusConfirmedTest() |
| 98 | + { |
| 99 | + $customer = DataFixtureStorageManager::getStorage()->get('customer'); |
| 100 | + $query = <<<QUERY |
| 101 | +query { |
| 102 | + customer { |
| 103 | + confirmation_status |
| 104 | + } |
| 105 | +} |
| 106 | +QUERY; |
| 107 | + $response = $this->graphQlQuery( |
| 108 | + $query, |
| 109 | + [], |
| 110 | + '', |
| 111 | + $this->getHeaderMap($customer->getEmail(), 'password') |
| 112 | + ); |
| 113 | + $this->assertEquals( |
| 114 | + strtoupper(AccountManagementInterface::ACCOUNT_CONFIRMED), |
| 115 | + $response['customer']['confirmation_status'] |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + #[ |
| 120 | + Config('customer/create_account/confirm', 1), |
| 121 | + DataFixture( |
| 122 | + Customer::class, |
| 123 | + [ |
| 124 | + |
| 125 | + ], |
| 126 | + 'customer' |
| 127 | + ) |
| 128 | + ] |
| 129 | + public function testGetConfirmationStatusConfirmationRequiredTest() |
| 130 | + { |
| 131 | + $this->expectExceptionMessage("This account isn't confirmed. Verify and try again."); |
| 132 | + /** @var CustomerInterface $customer */ |
| 133 | + $customer = DataFixtureStorageManager::getStorage()->get('customer'); |
| 134 | + $headersMap = $this->getHeaderMap($customer->getEmail(), 'password'); |
| 135 | + $customer->setConfirmation(AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED); |
| 136 | + $this->customerRepository->save($this->customerRepository->get($customer->getEmail())); |
| 137 | + $query = <<<QUERY |
| 138 | +query { |
| 139 | + customer { |
| 140 | + confirmation_status |
| 141 | + } |
| 142 | +} |
| 143 | +QUERY; |
| 144 | + $this->graphQlQuery( |
| 145 | + $query, |
| 146 | + [], |
| 147 | + '', |
| 148 | + $headersMap |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + #[ |
| 153 | + DataFixture( |
| 154 | + Customer::class, |
| 155 | + [ |
| 156 | + |
| 157 | + ], |
| 158 | + 'customer' |
| 159 | + ) |
| 160 | + ] |
| 161 | + public function testGetConfirmationStatusIfUserIsNotAuthorizedTest() |
| 162 | + { |
| 163 | + $this->expectException(\Exception::class); |
| 164 | + $this->expectExceptionMessage('The current customer isn\'t authorized.'); |
| 165 | + |
| 166 | + $query = <<<QUERY |
| 167 | +query { |
| 168 | + customer { |
| 169 | + confirmation_status |
| 170 | + } |
| 171 | +} |
| 172 | +QUERY; |
| 173 | + $this->graphQlQuery($query); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * @param string $email |
| 178 | + * @param string $password |
| 179 | + * |
| 180 | + * @return array |
| 181 | + * @throws AuthenticationException |
| 182 | + */ |
| 183 | + private function getHeaderMap(string $email, string $password): array |
| 184 | + { |
| 185 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password); |
| 186 | + return ['Authorization' => 'Bearer ' . $customerToken]; |
| 187 | + } |
| 188 | +} |
0 commit comments