Skip to content

Commit ec794f1

Browse files
Indrani sonawaneIndrani sonawane
authored andcommitted
Merge remote-tracking branch 'khrystynastolbova/33383_graphQL_delete_customer' into comm_78764_33411
2 parents 98217df + 29e8a49 commit ec794f1

File tree

4 files changed

+232
-0
lines changed

4 files changed

+232
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Model\Customer;
9+
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Customer\Api\Data\CustomerInterface;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
15+
16+
/**
17+
* Delete customer
18+
* @codingStandardsIgnoreStart
19+
*/
20+
class DeleteCustomer
21+
{
22+
/**
23+
* @var CustomerRepositoryInterface
24+
*/
25+
private $customerRepository;
26+
27+
/**
28+
* @param CustomerRepositoryInterface $customerRepository
29+
*/
30+
public function __construct(
31+
CustomerRepositoryInterface $customerRepository
32+
) {
33+
$this->customerRepository = $customerRepository;
34+
}
35+
36+
/**
37+
* Delete customer
38+
*
39+
* @param CustomerInterface $customer
40+
* @return void
41+
* @throws GraphQlInputException
42+
* @throws GraphQlNoSuchEntityException
43+
*/
44+
public function execute(CustomerInterface $customer): void
45+
{
46+
try {
47+
$this->customerRepository->delete($customer);
48+
} catch (LocalizedException $e) {
49+
throw new GraphQlInputException(__($e->getMessage()), $e);
50+
}
51+
}
52+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Model\Resolver;
9+
10+
use Magento\CustomerGraphQl\Model\Customer\DeleteCustomer as DeleteCustomerModel;
11+
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
12+
use Magento\Framework\GraphQl\Config\Element\Field;
13+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
14+
use Magento\Framework\GraphQl\Query\ResolverInterface;
15+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
16+
use Magento\Framework\Registry;
17+
use Magento\GraphQl\Model\Query\ContextInterface;
18+
19+
/**
20+
* Delete customer, used for GraphQL request processing.
21+
*/
22+
class DeleteCustomer implements ResolverInterface
23+
{
24+
/**
25+
* @var GetCustomer
26+
*/
27+
private $getCustomer;
28+
29+
/**
30+
* @var DeleteCustomerModel
31+
*/
32+
private $deleteCustomer;
33+
34+
/**
35+
* @var Registry
36+
*/
37+
private $registry;
38+
39+
/**
40+
* @param GetCustomer $getCustomer
41+
* @param DeleteCustomerModel $deleteCustomer
42+
* @param Registry $registry
43+
*/
44+
public function __construct(
45+
GetCustomer $getCustomer,
46+
DeleteCustomerModel $deleteCustomer,
47+
Registry $registry
48+
) {
49+
$this->getCustomer = $getCustomer;
50+
$this->deleteCustomer = $deleteCustomer;
51+
$this->registry = $registry;
52+
}
53+
54+
/**
55+
* @inheritdoc
56+
*/
57+
public function resolve(
58+
Field $field,
59+
$context,
60+
ResolveInfo $info,
61+
array $value = null,
62+
array $args = null
63+
) {
64+
/** @var ContextInterface $context */
65+
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
66+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
67+
}
68+
69+
$isSecure = $this->registry->registry('isSecureArea');
70+
71+
$this->registry->unregister('isSecureArea');
72+
$this->registry->register('isSecureArea', true);
73+
74+
$customer = $this->getCustomer->execute($context);
75+
$this->deleteCustomer->execute($customer);
76+
77+
$this->registry->unregister('isSecureArea');
78+
$this->registry->register('isSecureArea', $isSecure);
79+
return true;
80+
}
81+
}

app/code/Magento/CustomerGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type Mutation {
2121
createCustomerV2 (input: CustomerCreateInput! @doc(description: "An input object that defines the customer to be created.")): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\CreateCustomer") @doc(description:"Create a customer account.")
2222
updateCustomer (input: CustomerInput! @doc(description: "An input object that defines the customer characteristics to update.")): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomer") @doc(description:"Use `updateCustomerV2` instead.")
2323
updateCustomerV2 (input: CustomerUpdateInput! @doc(description: "An input object that defines the customer characteristics to update.")): CustomerOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomer") @doc(description:"Update the customer's personal information.")
24+
deleteCustomer: Boolean @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\DeleteCustomer") @doc(description:"Delete customer account")
2425
revokeCustomerToken: RevokeCustomerTokenOutput @resolver(class: "\\Magento\\CustomerGraphQl\\Model\\Resolver\\RevokeCustomerToken") @doc(description:"Revoke the customer token.")
2526
createCustomerAddress(input: CustomerAddressInput!): CustomerAddress @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\CreateCustomerAddress") @doc(description: "Create a billing or shipping address for a customer or guest.")
2627
updateCustomerAddress(id: Int! @doc(description: "The ID assigned to the customer address."), input: CustomerAddressInput @doc(description: "An input object that contains changes to the customer address.")): CustomerAddress @resolver(class: "Magento\\CustomerGraphQl\\Model\\Resolver\\UpdateCustomerAddress") @doc(description: "Update the billing or shipping address of a customer or guest.")
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Customer;
9+
10+
use Magento\Framework\Exception\AuthenticationException;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\TestFramework\TestCase\GraphQlAbstract;
13+
use Magento\Integration\Api\CustomerTokenServiceInterface;
14+
15+
/**
16+
* Delete customer tests
17+
*/
18+
class DeleteCustomerTest extends GraphQlAbstract
19+
{
20+
/**
21+
* @var CustomerTokenServiceInterface
22+
*/
23+
private $customerTokenService;
24+
25+
/**
26+
* @inheritdoc
27+
*/
28+
protected function setUp(): void
29+
{
30+
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
31+
}
32+
33+
/**
34+
* Test deleting customer
35+
*
36+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
37+
*/
38+
public function testDeleteCustomer(): void
39+
{
40+
$response = $this->graphQlMutation($this->getMutation(), [], '', $this->getHeaderMap());
41+
$this->assertArrayHasKey('deleteCustomer', $response);
42+
$this->assertTrue($response['deleteCustomer']);
43+
}
44+
45+
/**
46+
* Test deleting non authorized customer
47+
*/
48+
public function testDeleteCustomerIfUserIsNotAuthorized(): void
49+
{
50+
$this->expectException(\Exception::class);
51+
$this->expectExceptionMessage('The current customer isn\'t authorized.');
52+
$this->graphQlMutation($this->getMutation());
53+
}
54+
55+
/**
56+
* Test deleting locked customer
57+
*
58+
* @magentoApiDataFixture Magento/Customer/_files/locked_customer.php
59+
* @magentoApiDataFixture Magento/Customer/_files/customer_two_addresses.php
60+
*/
61+
public function testDeleteCustomerIfAccountIsLocked(): void
62+
{
63+
$this->expectException(\Exception::class);
64+
$this->expectExceptionMessage(
65+
'The account sign-in was incorrect or your account is disabled temporarily. Please wait and try again later'
66+
);
67+
$this->graphQlMutation($this->getMutation(), [], '', $this->getHeaderMap());
68+
}
69+
70+
/**
71+
* Retrieve deleteCustomer mutation
72+
*
73+
* @return string
74+
*/
75+
private function getMutation(): string
76+
{
77+
return <<<MUTATION
78+
mutation {
79+
deleteCustomer
80+
}
81+
MUTATION;
82+
}
83+
84+
/**
85+
* Retrieve customer authorization headers
86+
*
87+
* @param string $username
88+
* @param string $password
89+
* @return array
90+
* @throws AuthenticationException
91+
*/
92+
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
93+
{
94+
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
95+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
96+
return $headerMap;
97+
}
98+
}

0 commit comments

Comments
 (0)