From 232bb8661d7df712365531d5221ed90df70e0faf Mon Sep 17 00:00:00 2001 From: eliseacornejo Date: Wed, 6 Mar 2024 14:54:09 +0100 Subject: [PATCH] LYNX-362: Add confirmation status (#216) --- .../Model/Resolver/ConfirmationStatus.php | 58 ++++++ .../CustomerGraphQl/etc/graphql/di.xml | 1 + .../CustomerGraphQl/etc/schema.graphqls | 7 + .../Customer/ConfirmationStatusTest.php | 188 ++++++++++++++++++ .../GraphQl/Customer/StoreConfigTest.php | 35 ++++ 5 files changed, 289 insertions(+) create mode 100644 app/code/Magento/CustomerGraphQl/Model/Resolver/ConfirmationStatus.php create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/ConfirmationStatusTest.php diff --git a/app/code/Magento/CustomerGraphQl/Model/Resolver/ConfirmationStatus.php b/app/code/Magento/CustomerGraphQl/Model/Resolver/ConfirmationStatus.php new file mode 100644 index 0000000000000..70ef16bc5cb7c --- /dev/null +++ b/app/code/Magento/CustomerGraphQl/Model/Resolver/ConfirmationStatus.php @@ -0,0 +1,58 @@ +accountManagement->getConfirmationStatus($context->getUserId()); + } catch (LocalizedException $e) { + throw new GraphQlInputException(__($e->getMessage()), $e); + } + return strtoupper($confirmationStatus); + } +} diff --git a/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml b/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml index 0636098cd6d2d..305e9cd12d676 100644 --- a/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml +++ b/app/code/Magento/CustomerGraphQl/etc/graphql/di.xml @@ -37,6 +37,7 @@ customer/password/required_character_classes_number customer/password/minimum_password_length customer/password/autocomplete_on_storefront + customer/create_account/confirm diff --git a/app/code/Magento/CustomerGraphQl/etc/schema.graphqls b/app/code/Magento/CustomerGraphQl/etc/schema.graphqls index fb7b3d4dd6c27..848d0a9be8d07 100644 --- a/app/code/Magento/CustomerGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CustomerGraphQl/etc/schema.graphqls @@ -5,6 +5,7 @@ type StoreConfig { required_character_classes_number : String @doc(description: "The number of different character classes (lowercase, uppercase, digits, special characters) required in a password.") minimum_password_length : String @doc(description: "The minimum number of characters required for a valid password.") autocomplete_on_storefront : Boolean @doc(description: "Indicates whether to enable autocomplete on login and forgot password forms.") + create_account_confirmation: Boolean @doc(description: "Indicates if the new accounts need confirmation.") } type Query { @@ -146,6 +147,7 @@ type Customer @doc(description: "Defines the customer name, addresses, and other addresses: [CustomerAddress] @doc(description: "An array containing the customer's shipping and billing addresses.") @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\CustomerAddresses") gender: Int @doc(description: "The customer's gender (Male - 1, Female - 2).") custom_attributes(attributeCodes: [ID!]): [AttributeValueInterface] @doc(description: "Customer's custom attributes.") @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\CustomAttributeFilter") + confirmation_status: ConfirmationStatusEnum! @doc(description: "The customer's confirmation status.") @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\ConfirmationStatus") } type CustomerAddress @doc(description: "Contains detailed information about a customer's billing or shipping address."){ @@ -473,3 +475,8 @@ enum ValidationRuleEnum @doc(description: "List of validation rule names applied MAX_IMAGE_HEIGHT MAX_IMAGE_WIDTH } + +enum ConfirmationStatusEnum @doc(description: "List of account confirmation statuses.") { + ACCOUNT_CONFIRMED @doc(description: "Account confirmed") + ACCOUNT_CONFIRMATION_NOT_REQUIRED @doc(description: "Account confirmation not required") +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/ConfirmationStatusTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/ConfirmationStatusTest.php new file mode 100644 index 0000000000000..e2e05392917ee --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/ConfirmationStatusTest.php @@ -0,0 +1,188 @@ +customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class); + $this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class); + } + + #[ + Config('customer/create_account/confirm', 0), + DataFixture( + Customer::class, + [ + 'email' => 'customer@example.com', + ], + 'customer' + ) + ] + public function testGetConfirmationStatusConfirmationNotRequiredTest() + { + $customer = DataFixtureStorageManager::getStorage()->get('customer'); + $query = <<graphQlQuery( + $query, + [], + '', + $this->getHeaderMap($customer->getEmail(), 'password') + ); + $this->assertEquals( + strtoupper(AccountManagementInterface::ACCOUNT_CONFIRMATION_NOT_REQUIRED), + $response['customer']['confirmation_status'] + ); + } + + #[ + Config('customer/create_account/confirm', 1), + DataFixture( + Customer::class, + [ + 'email' => 'customer@example.com', + ], + 'customer' + ) + ] + public function testGetConfirmationStatusConfirmedTest() + { + $customer = DataFixtureStorageManager::getStorage()->get('customer'); + $query = <<graphQlQuery( + $query, + [], + '', + $this->getHeaderMap($customer->getEmail(), 'password') + ); + $this->assertEquals( + strtoupper(AccountManagementInterface::ACCOUNT_CONFIRMED), + $response['customer']['confirmation_status'] + ); + } + + #[ + Config('customer/create_account/confirm', 1), + DataFixture( + Customer::class, + [ + 'email' => 'another@example.com', + ], + 'customer' + ) + ] + public function testGetConfirmationStatusConfirmationRequiredTest() + { + $this->expectExceptionMessage("This account isn't confirmed. Verify and try again."); + /** @var CustomerInterface $customer */ + $customer = DataFixtureStorageManager::getStorage()->get('customer'); + $headersMap = $this->getHeaderMap($customer->getEmail(), 'password'); + $customer->setConfirmation(AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED); + $this->customerRepository->save($this->customerRepository->get($customer->getEmail())); + $query = <<graphQlQuery( + $query, + [], + '', + $headersMap + ); + } + + #[ + DataFixture( + Customer::class, + [ + 'email' => 'test@example.com', + ], + 'customer' + ) + ] + public function testGetConfirmationStatusIfUserIsNotAuthorizedTest() + { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('The current customer isn\'t authorized.'); + + $query = <<graphQlQuery($query); + } + + /** + * @param string $email + * @param string $password + * + * @return array + * @throws AuthenticationException + */ + private function getHeaderMap(string $email, string $password): array + { + $customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password); + return ['Authorization' => 'Bearer ' . $customerToken]; + } +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/StoreConfigTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/StoreConfigTest.php index 473105c2c46be..99b9e95e8d1f3 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/StoreConfigTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/StoreConfigTest.php @@ -8,6 +8,7 @@ namespace Magento\GraphQl\Customer; use Exception; +use Magento\TestFramework\Fixture\Config; use Magento\TestFramework\TestCase\GraphQlAbstract; /** @@ -35,4 +36,38 @@ public function testReturnTypeAutocompleteOnStorefrontConfig() self::assertArrayHasKey('autocomplete_on_storefront', $response['storeConfig']); self::assertTrue($response['storeConfig']['autocomplete_on_storefront']); } + + #[ + Config('customer/create_account/confirm', 1) + ] + public function testCreateAccountConfirmationEnabledStorefrontConfig() + { + $query = <<graphQlQuery($query); + self::assertArrayHasKey('create_account_confirmation', $response['storeConfig']); + self::assertTrue($response['storeConfig']['create_account_confirmation']); + } + + #[ + Config('customer/create_account/confirm', 0) + ] + public function testCreateAccountConfirmationDisabledStorefrontConfig() + { + $query = <<graphQlQuery($query); + self::assertArrayHasKey('create_account_confirmation', $response['storeConfig']); + self::assertFalse($response['storeConfig']['create_account_confirmation']); + } }