Skip to content

Commit 1875c8c

Browse files
author
Stanislav Idolov
authored
ENGCOM-3183: Fix issue with unexpected changing of subscription status after customer saving #18488
2 parents 8e9e697 + 0628294 commit 1875c8c

File tree

4 files changed

+74
-13
lines changed

4 files changed

+74
-13
lines changed

app/code/Magento/Newsletter/Model/ResourceModel/Subscriber.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Newsletter\Model\ResourceModel;
77

8+
use Magento\Store\Model\StoreManagerInterface;
9+
use Magento\Framework\App\ObjectManager;
10+
811
/**
912
* Newsletter subscriber resource model
1013
*
@@ -48,22 +51,33 @@ class Subscriber extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
4851
*/
4952
protected $mathRandom;
5053

54+
/**
55+
* Store manager
56+
*
57+
* @var StoreManagerInterface
58+
*/
59+
private $storeManager;
60+
5161
/**
5262
* Construct
5363
*
5464
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
5565
* @param \Magento\Framework\Stdlib\DateTime\DateTime $date
5666
* @param \Magento\Framework\Math\Random $mathRandom
5767
* @param string $connectionName
68+
* @param StoreManagerInterface $storeManager
5869
*/
5970
public function __construct(
6071
\Magento\Framework\Model\ResourceModel\Db\Context $context,
6172
\Magento\Framework\Stdlib\DateTime\DateTime $date,
6273
\Magento\Framework\Math\Random $mathRandom,
63-
$connectionName = null
74+
$connectionName = null,
75+
StoreManagerInterface $storeManager = null
6476
) {
6577
$this->_date = $date;
6678
$this->mathRandom = $mathRandom;
79+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
80+
->get(StoreManagerInterface::class);
6781
parent::__construct($context, $connectionName);
6882
}
6983

@@ -117,6 +131,9 @@ public function loadByEmail($subscriberEmail)
117131
*/
118132
public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer)
119133
{
134+
$storeId = (int)$customer->getStoreId() ?: $this->storeManager
135+
->getWebsite($customer->getWebsiteId())->getDefaultStore()->getId();
136+
120137
$select = $this->connection
121138
->select()
122139
->from($this->getMainTable())
@@ -127,7 +144,7 @@ public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface
127144
$select,
128145
[
129146
'customer_id' => $customer->getId(),
130-
'store_id' => $customer->getStoreId()
147+
'store_id' => $storeId
131148
]
132149
);
133150

@@ -145,7 +162,7 @@ public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface
145162
$select,
146163
[
147164
'subscriber_email' => $customer->getEmail(),
148-
'store_id' => $customer->getStoreId()
165+
'store_id' => $storeId
149166
]
150167
);
151168

app/code/Magento/Newsletter/Model/Subscriber.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -613,16 +613,17 @@ protected function _updateCustomerSubscription($customerId, $subscribe)
613613

614614
$this->setStatus($status);
615615

616+
$storeId = $customerData->getStoreId();
617+
if ((int)$customerData->getStoreId() === 0) {
618+
$storeId = $this->_storeManager->getWebsite($customerData->getWebsiteId())->getDefaultStore()->getId();
619+
}
620+
616621
if (!$this->getId()) {
617-
$storeId = $customerData->getStoreId();
618-
if ($customerData->getStoreId() == 0) {
619-
$storeId = $this->_storeManager->getWebsite($customerData->getWebsiteId())->getDefaultStore()->getId();
620-
}
621622
$this->setStoreId($storeId)
622623
->setCustomerId($customerData->getId())
623624
->setEmail($customerData->getEmail());
624625
} else {
625-
$this->setStoreId($customerData->getStoreId())
626+
$this->setStoreId($storeId)
626627
->setEmail($customerData->getEmail());
627628
}
628629

app/code/Magento/Newsletter/Test/Unit/Model/SubscriberTest.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ public function testSubscribeNotLoggedIn()
213213

214214
public function testUpdateSubscription()
215215
{
216+
$storeId = 2;
216217
$customerId = 1;
217218
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
218219
->getMock();
@@ -234,7 +235,7 @@ public function testUpdateSubscription()
234235
->method('getConfirmationStatus')
235236
->with($customerId)
236237
->willReturn('account_confirmation_required');
237-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
238+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
238239
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
239240

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

249250
public function testUnsubscribeCustomerById()
250251
{
252+
$storeId = 2;
251253
$customerId = 1;
252254
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
253255
->getMock();
@@ -265,7 +267,7 @@ public function testUnsubscribeCustomerById()
265267
);
266268
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
267269
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
268-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
270+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
269271
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
270272
$this->sendEmailCheck();
271273

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

275277
public function testSubscribeCustomerById()
276278
{
279+
$storeId = 2;
277280
$customerId = 1;
278281
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
279282
->getMock();
@@ -291,7 +294,7 @@ public function testSubscribeCustomerById()
291294
);
292295
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
293296
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
294-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
297+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
295298
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
296299
$this->sendEmailCheck();
297300

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

301304
public function testSubscribeCustomerById1()
302305
{
306+
$storeId = 2;
303307
$customerId = 1;
304308
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
305309
->getMock();
@@ -317,7 +321,7 @@ public function testSubscribeCustomerById1()
317321
);
318322
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
319323
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
320-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
324+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
321325
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
322326
$this->sendEmailCheck();
323327
$this->customerAccountManagement->expects($this->once())
@@ -331,6 +335,7 @@ public function testSubscribeCustomerById1()
331335

332336
public function testSubscribeCustomerByIdAfterConfirmation()
333337
{
338+
$storeId = 2;
334339
$customerId = 1;
335340
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
336341
->getMock();
@@ -348,7 +353,7 @@ public function testSubscribeCustomerByIdAfterConfirmation()
348353
);
349354
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
350355
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
351-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
356+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
352357
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
353358
$this->sendEmailCheck();
354359
$this->customerAccountManagement->expects($this->never())->method('getConfirmationStatus');

dev/tests/integration/testsuite/Magento/Newsletter/Model/Plugin/PluginTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,42 @@ private function verifySubscriptionNotExist($email)
167167
$this->assertEquals(0, (int)$subscriber->getId());
168168
return $subscriber;
169169
}
170+
171+
/**
172+
* @magentoAppArea adminhtml
173+
* @magentoDbIsolation enabled
174+
*/
175+
public function testCustomerWithZeroStoreIdIsSubscribed()
176+
{
177+
$objectManager = Bootstrap::getObjectManager();
178+
179+
$currentStore = $objectManager->get(
180+
\Magento\Store\Model\StoreManagerInterface::class
181+
)->getStore()->getId();
182+
183+
$subscriber = $objectManager->create(\Magento\Newsletter\Model\Subscriber::class);
184+
/** @var \Magento\Newsletter\Model\Subscriber $subscriber */
185+
$subscriber->setStoreId($currentStore)
186+
->setCustomerId(0)
187+
->setSubscriberEmail('customer@example.com')
188+
->setSubscriberStatus(\Magento\Newsletter\Model\Subscriber::STATUS_SUBSCRIBED)
189+
->save();
190+
191+
/** @var \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory */
192+
$customerFactory = $objectManager->get(\Magento\Customer\Api\Data\CustomerInterfaceFactory::class);
193+
$customerDataObject = $customerFactory->create()
194+
->setFirstname('Firstname')
195+
->setLastname('Lastname')
196+
->setStoreId(0)
197+
->setEmail('customer@example.com');
198+
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
199+
$customer = $this->accountManagement->createAccount($customerDataObject);
200+
201+
$this->customerRepository->save($customer);
202+
203+
$subscriber->loadByEmail('customer@example.com');
204+
205+
$this->assertEquals($customer->getId(), (int)$subscriber->getCustomerId());
206+
$this->assertEquals($currentStore, (int)$subscriber->getStoreId());
207+
}
170208
}

0 commit comments

Comments
 (0)