Skip to content

Commit 70e984f

Browse files
committed
ACP2E-3785: updateCustomerEmail GraphQL mutation(Change email Address) doesn't trigger the email Notification.
- initial solution
1 parent 9608ca2 commit 70e984f

File tree

2 files changed

+77
-9
lines changed

2 files changed

+77
-9
lines changed

app/code/Magento/CustomerGraphQl/Model/Resolver/UpdateCustomerEmail.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
<?php
22
/**
3-
* Copyright 2025 Adobe
3+
* Copyright 2020 Adobe
44
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\CustomerGraphQl\Model\Resolver;
99

10+
use Exception;
11+
use Magento\Customer\Model\EmailNotificationInterface;
1012
use Magento\CustomerGraphQl\Model\Customer\ExtractCustomerData;
1113
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
1214
use Magento\CustomerGraphQl\Model\Customer\UpdateCustomerAccount;
15+
use Magento\Framework\App\ObjectManager;
1316
use Magento\Framework\GraphQl\Config\Element\Field;
1417
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
18+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface as ResolverContext;
1519
use Magento\Framework\GraphQl\Query\Resolver\Value;
1620
use Magento\Framework\GraphQl\Query\ResolverInterface;
1721
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
@@ -37,31 +41,40 @@ class UpdateCustomerEmail implements ResolverInterface
3741
*/
3842
private $extractCustomerData;
3943

44+
/**
45+
* @var EmailNotificationInterface
46+
*/
47+
private $emailNotification;
48+
4049
/**
4150
* @param GetCustomer $getCustomer
4251
* @param UpdateCustomerAccount $updateCustomerAccount
4352
* @param ExtractCustomerData $extractCustomerData
53+
* @param EmailNotificationInterface|null $emailNotification
4454
*/
4555
public function __construct(
4656
GetCustomer $getCustomer,
4757
UpdateCustomerAccount $updateCustomerAccount,
48-
ExtractCustomerData $extractCustomerData
58+
ExtractCustomerData $extractCustomerData,
59+
?EmailNotificationInterface $emailNotification = null
4960
) {
5061
$this->getCustomer = $getCustomer;
5162
$this->updateCustomerAccount = $updateCustomerAccount;
5263
$this->extractCustomerData = $extractCustomerData;
64+
$this->emailNotification = $emailNotification
65+
?? ObjectManager::getInstance()->get(EmailNotificationInterface::class);
5366
}
5467

5568
/**
5669
* Resolve customer email update mutation
5770
*
58-
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
59-
* @param \Magento\Framework\GraphQl\Query\Resolver\ContextInterface $context
71+
* @param Field $field
72+
* @param ResolverContext $context
6073
* @param ResolveInfo $info
6174
* @param array|null $value
6275
* @param array|null $args
6376
* @return array|Value
64-
* @throws \Exception
77+
* @throws Exception
6578
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
6679
*/
6780
public function resolve(
@@ -77,6 +90,7 @@ public function resolve(
7790
}
7891

7992
$customer = $this->getCustomer->execute($context);
93+
$customerOriginalEmail = $customer->getEmail();
8094
$customer->setData('ignore_validation_flag', true);
8195
$this->updateCustomerAccount->execute(
8296
$customer,
@@ -86,7 +100,11 @@ public function resolve(
86100
],
87101
$context->getExtensionAttributes()->getStore()
88102
);
89-
103+
$this->emailNotification->credentialsChanged(
104+
$customer,
105+
$customerOriginalEmail,
106+
false
107+
);
90108
return ['customer' => $this->extractCustomerData->execute($customer)];
91109
}
92110
}

dev/tests/api-functional/testsuite/Magento/GraphQl/CustomerGraphQl/Model/Resolver/CustomerTest.php

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

88
namespace Magento\GraphQl\CustomerGraphQl\Model\Resolver;
99

10+
use Exception;
1011
use Magento\Customer\Api\CustomerRepositoryInterface;
1112
use Magento\Customer\Api\Data\CustomerInterface;
1213
use Magento\Customer\Model\Customer;
1314
use Magento\Customer\Test\Fixture\Customer as CustomerFixture;
1415
use Magento\CustomerGraphQl\Model\Resolver\Customer as CustomerResolver;
16+
use Magento\Framework\Serialize\SerializerInterface;
1517
use Magento\NewsletterGraphQl\Model\Resolver\IsSubscribed;
1618
use Magento\Framework\ObjectManagerInterface;
1719
use Magento\Framework\Registry;
@@ -24,6 +26,7 @@
2426
use Magento\Store\Test\Fixture\Store as StoreFixture;
2527
use Magento\Store\Test\Fixture\Website as WebsiteFixture;
2628
use Magento\TestFramework\Fixture\DataFixture;
29+
use Magento\TestFramework\Fixture\DbIsolation;
2730
use Magento\TestFramework\Helper\Bootstrap;
2831
use Magento\TestFramework\TestCase\GraphQl\ResolverCacheAbstract;
2932
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
@@ -60,6 +63,11 @@ class CustomerTest extends ResolverCacheAbstract
6063
*/
6164
private $registry;
6265

66+
/**
67+
* @var SerializerInterface
68+
*/
69+
private $json;
70+
6371
protected function setUp(): void
6472
{
6573
$this->objectManager = Bootstrap::getObjectManager();
@@ -75,6 +83,7 @@ protected function setUp(): void
7583
$this->websiteRepository = $this->objectManager->get(
7684
WebsiteRepositoryInterface::class
7785
);
86+
$this->json = $this->objectManager->get(SerializerInterface::class);
7887

7988
// first register secure area so we have permission to delete customer in tests
8089
$this->registry = $this->objectManager->get(Registry::class);
@@ -829,14 +838,55 @@ private function getCustomerQuery(): string
829838
QUERY;
830839
}
831840

841+
/**
842+
* Test that updated customer email is returned in the response
843+
*
844+
* @throws Exception
845+
*/
846+
#[
847+
DbIsolation(false),
848+
DataFixture(CustomerFixture::class, ['email' => 'customer@example.com'], as: 'customer'),
849+
]
850+
public function testChangeEmailSuccessfully(): void
851+
{
852+
$currentPassword = 'password';
853+
$updatedEmail = 'customer2@example.com';
854+
$query
855+
= <<<QUERY
856+
mutation {
857+
updateCustomerEmail(
858+
email: "$updatedEmail",
859+
password: "$currentPassword"
860+
) {
861+
customer {
862+
email
863+
}
864+
}
865+
}
866+
QUERY;
867+
$customer = $this->customerRepository->get('customer@example.com');
868+
$customerToken = $this->generateCustomerToken(
869+
$customer->getEmail(),
870+
'password'
871+
);
872+
$response = $this->graphQlMutation(
873+
$query,
874+
[],
875+
'',
876+
['Authorization' => 'Bearer ' . $customerToken]
877+
);
878+
879+
$this->assertEquals($updatedEmail, $response['updateCustomerEmail']['customer']['email']);
880+
}
881+
832882
/**
833883
* Generate customer token
834884
*
835885
* @param string $email
836886
* @param string $password
837887
* @param string $storeCode
838888
* @return string
839-
* @throws \Exception
889+
* @throws Exception
840890
*/
841891
private function generateCustomerToken(string $email, string $password, string $storeCode = 'default'): string
842892
{

0 commit comments

Comments
 (0)