Skip to content

Commit 0bc3a68

Browse files
author
Stanislav Idolov
authored
ENGCOM-3276: [Backport] Fix issue with unexpected changing of subscription status after customer saving #18642
2 parents 762722d + 81aaf77 commit 0bc3a68

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

@@ -118,6 +132,9 @@ public function loadByEmail($subscriberEmail)
118132
*/
119133
public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer)
120134
{
135+
$storeId = (int)$customer->getStoreId() ?: $this->storeManager
136+
->getWebsite($customer->getWebsiteId())->getDefaultStore()->getId();
137+
121138
$select = $this->connection
122139
->select()
123140
->from($this->getMainTable())
@@ -128,7 +145,7 @@ public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface
128145
$select,
129146
[
130147
'customer_id' => $customer->getId(),
131-
'store_id' => $customer->getStoreId()
148+
'store_id' => $storeId
132149
]
133150
);
134151

@@ -146,7 +163,7 @@ public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface
146163
$select,
147164
[
148165
'subscriber_email' => $customer->getEmail(),
149-
'store_id' => $customer->getStoreId()
166+
'store_id' => $storeId
150167
]
151168
);
152169

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

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

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

615+
$storeId = $customerData->getStoreId();
616+
if ((int)$customerData->getStoreId() === 0) {
617+
$storeId = $this->_storeManager->getWebsite($customerData->getWebsiteId())->getDefaultStore()->getId();
618+
}
619+
615620
if (!$this->getId()) {
616-
$storeId = $customerData->getStoreId();
617-
if ($customerData->getStoreId() == 0) {
618-
$storeId = $this->_storeManager->getWebsite($customerData->getWebsiteId())->getDefaultStore()->getId();
619-
}
620621
$this->setStoreId($storeId)
621622
->setCustomerId($customerData->getId())
622623
->setEmail($customerData->getEmail());
623624
} else {
624-
$this->setStoreId($customerData->getStoreId())
625+
$this->setStoreId($storeId)
625626
->setEmail($customerData->getEmail());
626627
}
627628

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

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

212212
public function testUpdateSubscription()
213213
{
214+
$storeId = 2;
214215
$customerId = 1;
215216
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
216217
->getMock();
@@ -232,7 +233,7 @@ public function testUpdateSubscription()
232233
->method('getConfirmationStatus')
233234
->with($customerId)
234235
->willReturn('account_confirmation_required');
235-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
236+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
236237
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
237238

238239
$storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
@@ -246,6 +247,7 @@ public function testUpdateSubscription()
246247

247248
public function testUnsubscribeCustomerById()
248249
{
250+
$storeId = 2;
249251
$customerId = 1;
250252
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
251253
->getMock();
@@ -263,7 +265,7 @@ public function testUnsubscribeCustomerById()
263265
);
264266
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
265267
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
266-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
268+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
267269
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
268270
$this->sendEmailCheck();
269271

@@ -272,6 +274,7 @@ public function testUnsubscribeCustomerById()
272274

273275
public function testSubscribeCustomerById()
274276
{
277+
$storeId = 2;
275278
$customerId = 1;
276279
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
277280
->getMock();
@@ -289,7 +292,7 @@ public function testSubscribeCustomerById()
289292
);
290293
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
291294
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
292-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
295+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
293296
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
294297
$this->sendEmailCheck();
295298

@@ -298,6 +301,7 @@ public function testSubscribeCustomerById()
298301

299302
public function testSubscribeCustomerById1()
300303
{
304+
$storeId = 2;
301305
$customerId = 1;
302306
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
303307
->getMock();
@@ -315,7 +319,7 @@ public function testSubscribeCustomerById1()
315319
);
316320
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
317321
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
318-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
322+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
319323
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
320324
$this->sendEmailCheck();
321325
$this->customerAccountManagement->expects($this->once())
@@ -329,6 +333,7 @@ public function testSubscribeCustomerById1()
329333

330334
public function testSubscribeCustomerByIdAfterConfirmation()
331335
{
336+
$storeId = 2;
332337
$customerId = 1;
333338
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
334339
->getMock();
@@ -346,7 +351,7 @@ public function testSubscribeCustomerByIdAfterConfirmation()
346351
);
347352
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
348353
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
349-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
354+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
350355
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
351356
$this->sendEmailCheck();
352357
$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('[email protected]')
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('[email protected]');
198+
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
199+
$customer = $this->accountManagement->createAccount($customerDataObject);
200+
201+
$this->customerRepository->save($customer);
202+
203+
$subscriber->loadByEmail('[email protected]');
204+
205+
$this->assertEquals($customer->getId(), (int)$subscriber->getCustomerId());
206+
$this->assertEquals($currentStore, (int)$subscriber->getStoreId());
207+
}
170208
}

0 commit comments

Comments
 (0)