Skip to content

Commit 26d424e

Browse files
committed
[2.4.x]: [Magento Cloud] Customer receives newsletter unsubscription email after registering for new account
1 parent a7e8d39 commit 26d424e

File tree

5 files changed

+60
-25
lines changed

5 files changed

+60
-25
lines changed

app/code/Magento/Newsletter/Controller/Ajax/Status.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
namespace Magento\Newsletter\Controller\Ajax;
77

88
use Magento\Framework\App\Action;
9+
use Magento\Framework\Controller\Result\Json;
910
use Magento\Framework\Controller\ResultFactory;
10-
use Magento\Framework\Exception\LocalizedException;
1111
use Magento\Framework\Validator\EmailAddress as EmailAddressValidator;
1212
use Magento\Newsletter\Model\GuestSubscriptionChecker;
1313
use Psr\Log\LoggerInterface;
@@ -65,15 +65,12 @@ public function execute()
6565
if (!empty($email) && $this->emailAddressValidator->isValid($email)) {
6666
$response['subscribed'] = $this->guestSubscriptionChecker->isSubscribed($email);
6767
}
68-
} catch (LocalizedException $exception) {
69-
$this->logger->error($exception->getMessage());
70-
$response['errors'] = true;
7168
} catch (\Throwable $exception) {
7269
$this->logger->error($exception->getMessage());
7370
$response['errors'] = true;
7471
}
7572

76-
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
73+
/** @var Json $resultJson */
7774
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
7875

7976
return $resultJson->setData($response);

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

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

5252
subscriptionStatusResolver(email, newsletterSubscription);
5353

54-
$.when(newsletterSubscription).done(function (isSubscribed) {
55-
$(self.signUpElement).prop('checked', isSubscribed);
54+
$.when(newsletterSubscription).done(function () {
55+
$(self.signUpElement).prop('checked', true);
5656
}).always(function () {
5757
$(self.submitButton).prop('disabled', false);
5858
});

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) {
19+
if (response.errors || !response.subscribed) {
2020
deferred.reject();
2121
} else {
22-
deferred.resolve(response.subscribed);
22+
deferred.resolve();
2323
}
2424
}).fail(function () {
2525
deferred.reject();

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,28 @@
77

88
namespace Magento\Newsletter\Controller\Ajax;
99

10-
use Magento\Framework\Serialize\Serializer\Json;
10+
use Magento\Framework\Serialize\SerializerInterface;
1111
use Magento\TestFramework\TestCase\AbstractController;
1212

1313
/**
1414
* Test Subscriber status ajax
1515
*/
1616
class StatusTest extends AbstractController
1717
{
18+
/**
19+
* @var SerializerInterface
20+
*/
21+
private $json;
22+
23+
/**
24+
* @inheritDoc
25+
*/
26+
protected function setUp()
27+
{
28+
parent::setUp();
29+
$this->json = $this->_objectManager->get(SerializerInterface::class);
30+
}
31+
1832
/**
1933
* Check newsletter subscription status verification
2034
*
@@ -29,7 +43,7 @@ public function testExecute(string $expStatus, string $email): void
2943
{
3044
$this->getRequest()->setParam('email', $email);
3145
$this->dispatch('newsletter/ajax/status');
32-
$actual = $this->_objectManager->get(Json::class)->unserialize($this->getResponse()->getBody());
46+
$actual = $this->json->unserialize($this->getResponse()->getBody());
3347

3448
$this->assertEquals($expStatus, $actual['subscribed']);
3549
}
@@ -43,12 +57,11 @@ public function testExecute(string $expStatus, string $email): void
4357
public function ajaxSubscriberDataProvider(): array
4458
{
4559
return [
46-
[false, ''],
47-
[false, 'sample@email.com'],
48-
[false, 'customer@example.com'],
49-
[true, 'customer_two@example.com'],
50-
[false, 'customer_confirm@example.com'],
51-
[false, 'invalid_email.com'],
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'],
64+
'invalid_email' => [false, 'invalid_email.com'],
5265
];
5366
}
5467
}

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

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ define([
1616
checkbox,
1717
emailElem,
1818
button,
19-
resolveStatus = ko.observable(true),
19+
response = ko.observable({}),
2020
resolverMock = jasmine.createSpy('subscription-status-resolver', function (email, deferred) {
21-
deferred.resolve(resolveStatus());
21+
if (response().errors || !response().subscribed) {
22+
deferred.reject();
23+
} else {
24+
deferred.resolve();
25+
}
2226
}).and.callThrough(),
2327
mocks = {
2428
'Magento_Newsletter/js/subscription-status-resolver': resolverMock
@@ -61,7 +65,7 @@ define([
6165
expect(typeof obj.element).toEqual('string');
6266
});
6367

64-
it('Verify Subscription is checked', function () {
68+
it('Check sign-up process when Subscription is checked', function () {
6569
emailElem.val('email@example.com');
6670
checkbox.prop('checked', true);
6771

@@ -72,7 +76,7 @@ define([
7276
expect(checkbox.is(':checked')).toBeTruthy();
7377
});
7478

75-
it('Verify sign-up process without email', function () {
79+
it('Check sign-up process without email', function () {
7680
checkbox.prop('checked', false);
7781

7882
obj.updateSignUpStatus();
@@ -81,7 +85,7 @@ define([
8185
expect(checkbox.is(':checked')).toBeFalsy();
8286
});
8387

84-
it('Verify sign-up process with incorrect email', function () {
88+
it('Check sign-up process with incorrect email', function () {
8589
emailElem.val('emailexample.com');
8690
checkbox.prop('checked', false);
8791

@@ -91,7 +95,11 @@ define([
9195
expect(checkbox.is(':checked')).toBeFalsy();
9296
});
9397

94-
it('Verify Subscription with correct data', function () {
98+
it('Check Subscription with correct data', function () {
99+
response({
100+
subscribed: true,
101+
errors: false
102+
});
95103
emailElem.val('email@example.com');
96104
checkbox.prop('checked', false);
97105

@@ -102,8 +110,25 @@ define([
102110
expect(button.is(':disabled')).toBeFalsy();
103111
});
104112

105-
it('Verify sign-up process with non-subscribed email', function () {
106-
resolveStatus(false);
113+
it('Check sign-up process with non-subscribed email', function () {
114+
response({
115+
subscribed: false,
116+
errors: false
117+
});
118+
emailElem.val('email@example.com');
119+
checkbox.prop('checked', false);
120+
121+
obj.updateSignUpStatus();
122+
123+
expect(resolverMock).toHaveBeenCalled();
124+
expect(checkbox.is(':checked')).toBeFalsy();
125+
});
126+
127+
it('Check sign-up process with errors', function () {
128+
response({
129+
subscribed: true,
130+
errors: true
131+
});
107132
emailElem.val('email@example.com');
108133
checkbox.prop('checked', false);
109134

0 commit comments

Comments
 (0)