-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LYNX-362: Add confirmation status (#216)
- Loading branch information
1 parent
6a65d58
commit 232bb86
Showing
5 changed files
with
289 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
app/code/Magento/CustomerGraphQl/Model/Resolver/ConfirmationStatus.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/ConfirmationStatusTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters