Skip to content

Commit

Permalink
LYNX-362: Add confirmation status (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliseacornejo authored and svera committed Mar 8, 2024
1 parent 6a65d58 commit 232bb86
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/************************************************************************
*
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
* ************************************************************************
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Resolver;

use Magento\Customer\Api\AccountManagementInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* Confirmation status resolver
*/
class ConfirmationStatus implements ResolverInterface
{
/**
* @param AccountManagementInterface $accountManagement
*/
public function __construct(
private readonly AccountManagementInterface $accountManagement
) {
}

/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
try {
$confirmationStatus = $this->accountManagement->getConfirmationStatus($context->getUserId());
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()), $e);
}
return strtoupper($confirmationStatus);
}
}
1 change: 1 addition & 0 deletions app/code/Magento/CustomerGraphQl/etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<item name="required_character_classes_number" xsi:type="string">customer/password/required_character_classes_number</item>
<item name="minimum_password_length" xsi:type="string">customer/password/minimum_password_length</item>
<item name="autocomplete_on_storefront" xsi:type="string">customer/password/autocomplete_on_storefront</item>
<item name="create_account_confirmation" xsi:type="string">customer/create_account/confirm</item>
</argument>
</arguments>
</type>
Expand Down
7 changes: 7 additions & 0 deletions app/code/Magento/CustomerGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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."){
Expand Down Expand Up @@ -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")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php
/************************************************************************
*
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
* ************************************************************************
*/
declare(strict_types=1);

namespace Magento\GraphQl\Customer;

use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Test\Fixture\Customer;
use Magento\Framework\Exception\AuthenticationException;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Fixture\Config;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;
use Magento\TestFramework\Fixture\DataFixture;

/**
* Tests for confirmation status
*/
class ConfirmationStatusTest extends GraphQlAbstract
{
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

/**
* @var CustomerRepositoryInterface
*/
private $customerRepository;

protected function setUp(): void
{
parent::setUp();
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
$this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
}

#[
Config('customer/create_account/confirm', 0),
DataFixture(
Customer::class,
[
'email' => '[email protected]',
],
'customer'
)
]
public function testGetConfirmationStatusConfirmationNotRequiredTest()
{
$customer = DataFixtureStorageManager::getStorage()->get('customer');
$query = <<<QUERY
query {
customer {
confirmation_status
}
}
QUERY;
$response = $this->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' => '[email protected]',
],
'customer'
)
]
public function testGetConfirmationStatusConfirmedTest()
{
$customer = DataFixtureStorageManager::getStorage()->get('customer');
$query = <<<QUERY
query {
customer {
confirmation_status
}
}
QUERY;
$response = $this->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' => '[email protected]',
],
'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 = <<<QUERY
query {
customer {
confirmation_status
}
}
QUERY;
$this->graphQlQuery(
$query,
[],
'',
$headersMap
);
}

#[
DataFixture(
Customer::class,
[
'email' => '[email protected]',
],
'customer'
)
]
public function testGetConfirmationStatusIfUserIsNotAuthorizedTest()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The current customer isn\'t authorized.');

$query = <<<QUERY
query {
customer {
confirmation_status
}
}
QUERY;
$this->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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Magento\GraphQl\Customer;

use Exception;
use Magento\TestFramework\Fixture\Config;
use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
Expand Down Expand Up @@ -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 = <<<QUERY
{
storeConfig {
create_account_confirmation
}
}
QUERY;
$response = $this->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 = <<<QUERY
{
storeConfig {
create_account_confirmation
}
}
QUERY;
$response = $this->graphQlQuery($query);
self::assertArrayHasKey('create_account_confirmation', $response['storeConfig']);
self::assertFalse($response['storeConfig']['create_account_confirmation']);
}
}

0 comments on commit 232bb86

Please sign in to comment.