Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport] Fix newsletter subscription behaviour for registered customer. #17179

Merged
merged 3 commits into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions app/code/Magento/Newsletter/Model/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,6 @@ public function subscribe($email)
self::XML_PATH_CONFIRMATION_FLAG,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) == 1 ? true : false;
$isOwnSubscribes = false;

$isSubscribeOwnEmail = $this->_customerSession->isLoggedIn()
&& $this->_customerSession->getCustomerDataObject()->getEmail() == $email;
Expand All @@ -426,13 +425,7 @@ public function subscribe($email)
|| $this->getStatus() == self::STATUS_NOT_ACTIVE
) {
if ($isConfirmNeed === true) {
// if user subscribes own login email - confirmation is not needed
$isOwnSubscribes = $isSubscribeOwnEmail;
if ($isOwnSubscribes == true) {
$this->setStatus(self::STATUS_SUBSCRIBED);
} else {
$this->setStatus(self::STATUS_NOT_ACTIVE);
}
$this->setStatus(self::STATUS_NOT_ACTIVE);
} else {
$this->setStatus(self::STATUS_SUBSCRIBED);
}
Expand All @@ -458,9 +451,7 @@ public function subscribe($email)
try {
/* Save model before sending out email */
$this->save();
if ($isConfirmNeed === true
&& $isOwnSubscribes === false
) {
if ($isConfirmNeed === true) {
$this->sendConfirmationRequestEmail();
} else {
$this->sendConfirmationSuccessEmail();
Expand Down
24 changes: 13 additions & 11 deletions app/code/Magento/Newsletter/Test/Unit/Model/SubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Newsletter\Test\Unit\Model;

use Magento\Newsletter\Model\Subscriber;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
Expand Down Expand Up @@ -134,7 +136,7 @@ public function testSubscribe()
$email = 'subscriber_email@magento.com';
$this->resource->expects($this->any())->method('loadByEmail')->willReturn(
[
'subscriber_status' => 3,
'subscriber_status' => Subscriber::STATUS_UNSUBSCRIBED,
'subscriber_email' => $email,
'name' => 'subscriber_name'
]
Expand All @@ -151,15 +153,15 @@ public function testSubscribe()
$this->sendEmailCheck();
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();

$this->assertEquals(1, $this->subscriber->subscribe($email));
$this->assertEquals(Subscriber::STATUS_NOT_ACTIVE, $this->subscriber->subscribe($email));
}

public function testSubscribeNotLoggedIn()
{
$email = 'subscriber_email@magento.com';
$this->resource->expects($this->any())->method('loadByEmail')->willReturn(
[
'subscriber_status' => 3,
'subscriber_status' => Subscriber::STATUS_UNSUBSCRIBED,
'subscriber_email' => $email,
'name' => 'subscriber_name'
]
Expand All @@ -176,7 +178,7 @@ public function testSubscribeNotLoggedIn()
$this->sendEmailCheck();
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();

$this->assertEquals(2, $this->subscriber->subscribe($email));
$this->assertEquals(Subscriber::STATUS_NOT_ACTIVE, $this->subscriber->subscribe($email));
}

public function testUpdateSubscription()
Expand All @@ -193,7 +195,7 @@ public function testUpdateSubscription()
->willReturn(
[
'subscriber_id' => 1,
'subscriber_status' => 1
'subscriber_status' => Subscriber::STATUS_SUBSCRIBED
]
);
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
Expand Down Expand Up @@ -228,7 +230,7 @@ public function testUnsubscribeCustomerById()
->willReturn(
[
'subscriber_id' => 1,
'subscriber_status' => 1
'subscriber_status' => Subscriber::STATUS_SUBSCRIBED
]
);
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
Expand All @@ -254,7 +256,7 @@ public function testSubscribeCustomerById()
->willReturn(
[
'subscriber_id' => 1,
'subscriber_status' => 3
'subscriber_status' => Subscriber::STATUS_UNSUBSCRIBED
]
);
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
Expand All @@ -280,7 +282,7 @@ public function testSubscribeCustomerById1()
->willReturn(
[
'subscriber_id' => 1,
'subscriber_status' => 3
'subscriber_status' => Subscriber::STATUS_UNSUBSCRIBED
]
);
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
Expand All @@ -294,7 +296,7 @@ public function testSubscribeCustomerById1()
$this->scopeConfig->expects($this->atLeastOnce())->method('getValue')->with()->willReturn(true);

$this->subscriber->subscribeCustomerById($customerId);
$this->assertEquals(\Magento\Newsletter\Model\Subscriber::STATUS_NOT_ACTIVE, $this->subscriber->getStatus());
$this->assertEquals(Subscriber::STATUS_NOT_ACTIVE, $this->subscriber->getStatus());
}

public function testSubscribeCustomerByIdAfterConfirmation()
Expand All @@ -311,7 +313,7 @@ public function testSubscribeCustomerByIdAfterConfirmation()
->willReturn(
[
'subscriber_id' => 1,
'subscriber_status' => 4
'subscriber_status' => Subscriber::STATUS_UNCONFIRMED
]
);
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
Expand All @@ -323,7 +325,7 @@ public function testSubscribeCustomerByIdAfterConfirmation()
$this->scopeConfig->expects($this->atLeastOnce())->method('getValue')->with()->willReturn(true);

$this->subscriber->updateSubscription($customerId);
$this->assertEquals(\Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED, $this->subscriber->getStatus());
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $this->subscriber->getStatus());
}

public function testUnsubscribe()
Expand Down