Skip to content

Commit 30ee8b7

Browse files
committed
MC-29102: [2.4.x] [Magento Cloud] Customer receives newsletter unsubscription email after registering for new account
1 parent c57f3ca commit 30ee8b7

File tree

6 files changed

+48
-36
lines changed

6 files changed

+48
-36
lines changed

app/code/Magento/Newsletter/Model/Plugin/CustomerPlugin.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,20 @@ public function afterSave(
109109
CustomerInterface $result,
110110
CustomerInterface $customer
111111
) {
112+
/** @var Subscriber $subscriber */
112113
$subscriber = $this->getSubscriber($result);
113-
$subscribeStatus = $this->getIsSubscribedFromExtensionAttr($customer) ?? $subscriber->isSubscribed();
114+
$subscribeStatus = $this->getIsSubscribedFromExtensionAttributes($customer) ?? $subscriber->isSubscribed();
114115
$needToUpdate = $this->isSubscriptionChanged($result, $subscriber, $subscribeStatus);
115116

116117
/**
117118
* If subscriber is waiting to confirm customer registration
118119
* and customer is already confirmed registration
119120
* than need to subscribe customer
120121
*/
121-
if ((int)$subscriber->getStatus() === Subscriber::STATUS_UNCONFIRMED && empty($result->getConfirmation())) {
122+
if ($subscriber->getId()
123+
&& (int)$subscriber->getStatus() === Subscriber::STATUS_UNCONFIRMED
124+
&& empty($result->getConfirmation())
125+
) {
122126
$needToUpdate = true;
123127
$subscribeStatus = true;
124128
}
@@ -129,7 +133,7 @@ public function afterSave(
129133
: $this->subscriptionManager->unsubscribeCustomer((int)$result->getId(), $storeId);
130134
$this->customerSubscriber[(int)$result->getId()] = $subscriber;
131135
}
132-
$this->addIsSubscribedExtensionAttr($result, $subscriber->isSubscribed());
136+
$this->addIsSubscribedExtensionAttribute($result, $subscriber->isSubscribed());
133137

134138
return $result;
135139
}
@@ -140,14 +144,14 @@ public function afterSave(
140144
* @param CustomerInterface $customer
141145
* @return bool|null
142146
*/
143-
private function getIsSubscribedFromExtensionAttr(CustomerInterface $customer): ?bool
147+
private function getIsSubscribedFromExtensionAttributes(CustomerInterface $customer): ?bool
144148
{
145-
$newExtensionAttributes = $customer->getExtensionAttributes();
146-
if ($newExtensionAttributes === null || $newExtensionAttributes->getIsSubscribed() === null) {
149+
$extensionAttributes = $customer->getExtensionAttributes();
150+
if ($extensionAttributes === null || $extensionAttributes->getIsSubscribed() === null) {
147151
return null;
148152
}
149153

150-
return (bool)$newExtensionAttributes->getIsSubscribed();
154+
return (bool)$extensionAttributes->getIsSubscribed();
151155
}
152156

153157
/**
@@ -223,7 +227,7 @@ public function afterGetById(CustomerRepositoryInterface $subject, CustomerInter
223227
$extensionAttributes = $customer->getExtensionAttributes();
224228
if ($extensionAttributes === null || $extensionAttributes->getIsSubscribed() === null) {
225229
$isSubscribed = $this->getSubscriber($customer)->isSubscribed();
226-
$this->addIsSubscribedExtensionAttr($customer, $isSubscribed);
230+
$this->addIsSubscribedExtensionAttribute($customer, $isSubscribed);
227231
}
228232

229233
return $customer;
@@ -235,7 +239,7 @@ public function afterGetById(CustomerRepositoryInterface $subject, CustomerInter
235239
* @param CustomerInterface $customer
236240
* @param bool $isSubscribed
237241
*/
238-
private function addIsSubscribedExtensionAttr(CustomerInterface $customer, bool $isSubscribed): void
242+
private function addIsSubscribedExtensionAttribute(CustomerInterface $customer, bool $isSubscribed): void
239243
{
240244
$extensionAttributes = $customer->getExtensionAttributes();
241245
if ($extensionAttributes === null) {

app/code/Magento/Newsletter/Test/Unit/Model/Plugin/CustomerPluginTest.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,14 @@ public function testAfterSave(?int $originalStatus, ?bool $newValue, ?bool $expe
124124
->method('loadByCustomer')
125125
->with($customerId, $websiteId)
126126
->willReturnSelf();
127-
$subscriber->expects($this->once())
128-
->method('loadBySubscriberEmail')
129-
->with($customerEmail, $websiteId)
130-
->willReturnSelf();
127+
if ($originalStatus == Subscriber::STATUS_UNCONFIRMED) {
128+
$subscriber->method('getId')->willReturn(1);
129+
} else {
130+
$subscriber->expects($this->once())
131+
->method('loadBySubscriberEmail')
132+
->with($customerEmail, $websiteId)
133+
->willReturnSelf();
134+
}
131135
$this->subscriberFactory->method('create')->willReturn($subscriber);
132136

133137
$customerExtension = $this->createPartialMock(CustomerExtensionInterface::class, ['getIsSubscribed']);
@@ -162,23 +166,25 @@ public function testAfterSave(?int $originalStatus, ?bool $newValue, ?bool $expe
162166
}
163167

164168
/**
169+
* Data provider for testAfterSave()
170+
*
165171
* @return array
166172
*/
167-
public function afterSaveDataProvider()
173+
public function afterSaveDataProvider(): array
168174
{
169175
return [
170-
[null, null, null],
171-
[null, true, true],
172-
[null, false, null],
173-
[Subscriber::STATUS_SUBSCRIBED, null, null],
174-
[Subscriber::STATUS_SUBSCRIBED, true, null],
175-
[Subscriber::STATUS_SUBSCRIBED, false, false],
176-
[Subscriber::STATUS_UNSUBSCRIBED, null, null],
177-
[Subscriber::STATUS_UNSUBSCRIBED, true, true],
178-
[Subscriber::STATUS_UNSUBSCRIBED, false, null],
179-
[Subscriber::STATUS_UNCONFIRMED, null, true],
180-
[Subscriber::STATUS_UNCONFIRMED, true, true],
181-
[Subscriber::STATUS_UNCONFIRMED, false, true],
176+
'missing_previous_and_new_status' => [null, null, null],
177+
'missing_previous_status_and_subscribe' => [null, true, true],
178+
'new_unsubscribed_value_and_missing_previous_status' => [null, false, null],
179+
'previous_subscribed_status_without_new_value' => [Subscriber::STATUS_SUBSCRIBED, null, null],
180+
'same_subscribed_previous_and_new_status' => [Subscriber::STATUS_SUBSCRIBED, true, null],
181+
'unsubscribe_previously_subscribed_customer' => [Subscriber::STATUS_SUBSCRIBED, false, false],
182+
'previously_unsubscribed_status_without_new_value' => [Subscriber::STATUS_UNSUBSCRIBED, null, null],
183+
'subscribe_previously_unsubscribed_customer' => [Subscriber::STATUS_UNSUBSCRIBED, true, true],
184+
'same_unsubscribed_previous_and_new_status' => [Subscriber::STATUS_UNSUBSCRIBED, false, null],
185+
'previous_unconfirmed_status_without_new_value' => [Subscriber::STATUS_UNCONFIRMED, null, true],
186+
'subscribe_previously_unconfirmed_status' => [Subscriber::STATUS_UNCONFIRMED, true, true],
187+
'unsubscribe_previously_unconfirmed_status' => [Subscriber::STATUS_UNCONFIRMED, false, true],
182188
];
183189
}
184190

app/code/Magento/Newsletter/view/frontend/web/js/newsletter-sign-up.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ define([
5151

5252
subscriptionStatusResolver(email, newsletterSubscription);
5353

54-
$.when(newsletterSubscription).done(function () {
55-
$(self.signUpElement).prop('checked', true);
54+
$.when(newsletterSubscription).done(function (isSubscribed) {
55+
if (isSubscribed) {
56+
$(self.signUpElement).prop('checked', true);
57+
}
5658
}).always(function () {
5759
$(self.submitButton).prop('disabled', false);
5860
});

app/code/Magento/Newsletter/view/frontend/web/js/subscription-status-resolver.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ define([
1616
email: email
1717
}
1818
).done(function (response) {
19-
if (response.errors || !response.subscribed) {
19+
if (response.errors) {
2020
deferred.reject();
2121
} else {
22-
deferred.resolve();
22+
deferred.resolve(response.subscribed);
2323
}
2424
}).fail(function () {
2525
deferred.reject();

dev/tests/integration/testsuite/Magento/Newsletter/Controller/Ajax/StatusTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ public function testExecute(string $expStatus, string $email): void
5757
public function ajaxSubscriberDataProvider(): array
5858
{
5959
return [
60-
'empty_string' => [false, ''],
61-
'unsubscribed_email' => [false, 'sample@email.com'],
62-
'registered_email' => [false, 'customer@example.com'],
63-
'subscribed_email' => [true, 'customer_two@example.com'],
60+
'empty_email_parameter' => [false, ''],
6461
'invalid_email' => [false, 'invalid_email.com'],
62+
'unsubscribed_email' => [false, 'sample@email.com'],
63+
'subscribed_guest_email' => [true, 'customer_two@example.com'],
64+
'subscribed_registered_customer_email' => [false, 'customer@example.com'],
6565
];
6666
}
6767
}

dev/tests/js/jasmine/tests/app/code/Magento/Newsletter/frontend/js/newsletter-sign-up.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ define([
1818
button,
1919
response = ko.observable({}),
2020
resolverMock = jasmine.createSpy('subscription-status-resolver', function (email, deferred) {
21-
if (response().errors || !response().subscribed) {
21+
if (response().errors) {
2222
deferred.reject();
2323
} else {
24-
deferred.resolve();
24+
deferred.resolve(response().subscribed);
2525
}
2626
}).and.callThrough(),
2727
mocks = {

0 commit comments

Comments
 (0)