Skip to content

Fix issue with unexpected changing of subscription status after customer saving #18488

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

Merged
merged 2 commits into from
Oct 25, 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
23 changes: 20 additions & 3 deletions app/code/Magento/Newsletter/Model/ResourceModel/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\Newsletter\Model\ResourceModel;

use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\ObjectManager;

/**
* Newsletter subscriber resource model
*
Expand Down Expand Up @@ -48,22 +51,33 @@ class Subscriber extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
*/
protected $mathRandom;

/**
* Store manager
*
* @var StoreManagerInterface
*/
private $storeManager;

/**
* Construct
*
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
* @param \Magento\Framework\Stdlib\DateTime\DateTime $date
* @param \Magento\Framework\Math\Random $mathRandom
* @param string $connectionName
* @param StoreManagerInterface $storeManager
*/
public function __construct(
\Magento\Framework\Model\ResourceModel\Db\Context $context,
\Magento\Framework\Stdlib\DateTime\DateTime $date,
\Magento\Framework\Math\Random $mathRandom,
$connectionName = null
$connectionName = null,
StoreManagerInterface $storeManager = null
) {
$this->_date = $date;
$this->mathRandom = $mathRandom;
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
->get(StoreManagerInterface::class);
parent::__construct($context, $connectionName);
}

Expand Down Expand Up @@ -117,6 +131,9 @@ public function loadByEmail($subscriberEmail)
*/
public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer)
{
$storeId = (int)$customer->getStoreId() ?: $this->storeManager
->getWebsite($customer->getWebsiteId())->getDefaultStore()->getId();

$select = $this->connection
->select()
->from($this->getMainTable())
Expand All @@ -127,7 +144,7 @@ public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface
$select,
[
'customer_id' => $customer->getId(),
'store_id' => $customer->getStoreId()
'store_id' => $storeId
]
);

Expand All @@ -145,7 +162,7 @@ public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface
$select,
[
'subscriber_email' => $customer->getEmail(),
'store_id' => $customer->getStoreId()
'store_id' => $storeId
]
);

Expand Down
11 changes: 6 additions & 5 deletions app/code/Magento/Newsletter/Model/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -613,16 +613,17 @@ protected function _updateCustomerSubscription($customerId, $subscribe)

$this->setStatus($status);

$storeId = $customerData->getStoreId();
if ((int)$customerData->getStoreId() === 0) {
$storeId = $this->_storeManager->getWebsite($customerData->getWebsiteId())->getDefaultStore()->getId();
}

if (!$this->getId()) {
$storeId = $customerData->getStoreId();
if ($customerData->getStoreId() == 0) {
$storeId = $this->_storeManager->getWebsite($customerData->getWebsiteId())->getDefaultStore()->getId();
}
$this->setStoreId($storeId)
->setCustomerId($customerData->getId())
->setEmail($customerData->getEmail());
} else {
$this->setStoreId($customerData->getStoreId())
$this->setStoreId($storeId)
->setEmail($customerData->getEmail());
}

Expand Down
15 changes: 10 additions & 5 deletions app/code/Magento/Newsletter/Test/Unit/Model/SubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public function testSubscribeNotLoggedIn()

public function testUpdateSubscription()
{
$storeId = 2;
$customerId = 1;
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
->getMock();
Expand All @@ -234,7 +235,7 @@ public function testUpdateSubscription()
->method('getConfirmationStatus')
->with($customerId)
->willReturn('account_confirmation_required');
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');

$storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
Expand All @@ -248,6 +249,7 @@ public function testUpdateSubscription()

public function testUnsubscribeCustomerById()
{
$storeId = 2;
$customerId = 1;
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
->getMock();
Expand All @@ -265,7 +267,7 @@ public function testUnsubscribeCustomerById()
);
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
$this->sendEmailCheck();

Expand All @@ -274,6 +276,7 @@ public function testUnsubscribeCustomerById()

public function testSubscribeCustomerById()
{
$storeId = 2;
$customerId = 1;
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
->getMock();
Expand All @@ -291,7 +294,7 @@ public function testSubscribeCustomerById()
);
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
$this->sendEmailCheck();

Expand All @@ -300,6 +303,7 @@ public function testSubscribeCustomerById()

public function testSubscribeCustomerById1()
{
$storeId = 2;
$customerId = 1;
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
->getMock();
Expand All @@ -317,7 +321,7 @@ public function testSubscribeCustomerById1()
);
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
$this->sendEmailCheck();
$this->customerAccountManagement->expects($this->once())
Expand All @@ -331,6 +335,7 @@ public function testSubscribeCustomerById1()

public function testSubscribeCustomerByIdAfterConfirmation()
{
$storeId = 2;
$customerId = 1;
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
->getMock();
Expand All @@ -348,7 +353,7 @@ public function testSubscribeCustomerByIdAfterConfirmation()
);
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
$this->sendEmailCheck();
$this->customerAccountManagement->expects($this->never())->method('getConfirmationStatus');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,42 @@ private function verifySubscriptionNotExist($email)
$this->assertEquals(0, (int)$subscriber->getId());
return $subscriber;
}

/**
* @magentoAppArea adminhtml
* @magentoDbIsolation enabled
*/
public function testCustomerWithZeroStoreIdIsSubscribed()
{
$objectManager = Bootstrap::getObjectManager();

$currentStore = $objectManager->get(
\Magento\Store\Model\StoreManagerInterface::class
)->getStore()->getId();

$subscriber = $objectManager->create(\Magento\Newsletter\Model\Subscriber::class);
/** @var \Magento\Newsletter\Model\Subscriber $subscriber */
$subscriber->setStoreId($currentStore)
->setCustomerId(0)
->setSubscriberEmail('[email protected]')
->setSubscriberStatus(\Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED)
->save();

/** @var \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory */
$customerFactory = $objectManager->get(\Magento\Customer\Api\Data\CustomerInterfaceFactory::class);
$customerDataObject = $customerFactory->create()
->setFirstname('Firstname')
->setLastname('Lastname')
->setStoreId(0)
->setEmail('[email protected]');
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
$customer = $this->accountManagement->createAccount($customerDataObject);

$this->customerRepository->save($customer);

$subscriber->loadByEmail('[email protected]');

$this->assertEquals($customer->getId(), (int)$subscriber->getCustomerId());
$this->assertEquals($currentStore, (int)$subscriber->getStoreId());
}
}