Skip to content

Commit 717f615

Browse files
author
Stanislav Idolov
authored
ENGCOM-3460: [Newsletter] Fixing the customer subscribing from different stores #19195
2 parents 6db210f + 0382fb1 commit 717f615

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

app/code/Magento/Newsletter/Model/Plugin/CustomerPlugin.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
namespace Magento\Newsletter\Model\Plugin;
77

88
use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
9+
use Magento\Customer\Api\Data\CustomerExtensionInterface;
910
use Magento\Customer\Api\Data\CustomerInterface;
10-
use Magento\Newsletter\Model\SubscriberFactory;
1111
use Magento\Framework\Api\ExtensionAttributesFactory;
12+
use Magento\Framework\App\ObjectManager;
1213
use Magento\Newsletter\Model\ResourceModel\Subscriber;
13-
use Magento\Customer\Api\Data\CustomerExtensionInterface;
14+
use Magento\Newsletter\Model\SubscriberFactory;
15+
use Magento\Store\Model\StoreManagerInterface;
1416

1517
class CustomerPlugin
1618
{
@@ -36,21 +38,29 @@ class CustomerPlugin
3638
*/
3739
private $customerSubscriptionStatus = [];
3840

41+
/**
42+
* @var StoreManagerInterface
43+
*/
44+
private $storeManager;
45+
3946
/**
4047
* Initialize dependencies.
4148
*
4249
* @param SubscriberFactory $subscriberFactory
4350
* @param ExtensionAttributesFactory $extensionFactory
4451
* @param Subscriber $subscriberResource
52+
* @param StoreManagerInterface|null $storeManager
4553
*/
4654
public function __construct(
4755
SubscriberFactory $subscriberFactory,
4856
ExtensionAttributesFactory $extensionFactory,
49-
Subscriber $subscriberResource
57+
Subscriber $subscriberResource,
58+
StoreManagerInterface $storeManager = null
5059
) {
5160
$this->subscriberFactory = $subscriberFactory;
5261
$this->extensionFactory = $extensionFactory;
5362
$this->subscriberResource = $subscriberResource;
63+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
5464
}
5565

5666
/**
@@ -151,7 +161,8 @@ public function afterDelete(CustomerRepository $subject, $result, CustomerInterf
151161
public function afterGetById(CustomerRepository $subject, CustomerInterface $customer)
152162
{
153163
$extensionAttributes = $customer->getExtensionAttributes();
154-
164+
$storeId = $this->storeManager->getStore()->getId();
165+
$customer->setStoreId($storeId);
155166
if ($extensionAttributes === null) {
156167
/** @var CustomerExtensionInterface $extensionAttributes */
157168
$extensionAttributes = $this->extensionFactory->create(CustomerInterface::class);

app/code/Magento/Newsletter/Test/Unit/Model/Plugin/CustomerPluginTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Magento\Customer\Api\Data\CustomerExtensionInterface;
1111
use Magento\Framework\Api\ExtensionAttributesFactory;
1212
use Magento\Newsletter\Model\ResourceModel\Subscriber;
13+
use Magento\Store\Model\Store;
14+
use Magento\Store\Model\StoreManagerInterface;
1315

1416
class CustomerPluginTest extends \PHPUnit\Framework\TestCase
1517
{
@@ -53,6 +55,11 @@ class CustomerPluginTest extends \PHPUnit\Framework\TestCase
5355
*/
5456
private $customerMock;
5557

58+
/**
59+
* @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
60+
*/
61+
private $storeManagerMock;
62+
5663
protected function setUp()
5764
{
5865
$this->subscriberFactory = $this->getMockBuilder(\Magento\Newsletter\Model\SubscriberFactory::class)
@@ -87,6 +94,8 @@ protected function setUp()
8794
->setMethods(['getExtensionAttributes'])
8895
->disableOriginalConstructor()
8996
->getMockForAbstractClass();
97+
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
98+
9099
$this->subscriberFactory->expects($this->any())->method('create')->willReturn($this->subscriber);
91100
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
92101

@@ -96,6 +105,7 @@ protected function setUp()
96105
'subscriberFactory' => $this->subscriberFactory,
97106
'extensionFactory' => $this->extensionFactoryMock,
98107
'subscriberResource' => $this->subscriberResourceMock,
108+
'storeManager' => $this->storeManagerMock,
99109
]
100110
);
101111
}
@@ -206,6 +216,7 @@ public function testAfterGetByIdCreatesExtensionAttributesIfItIsNotSet(
206216
) {
207217
$subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
208218
$subscriber = [$subscriberStatusKey => $subscriberStatusValue];
219+
$this->prepareStoreData();
209220

210221
$this->extensionFactoryMock->expects($this->any())
211222
->method('create')
@@ -233,6 +244,7 @@ public function testAfterGetByIdSetsIsSubscribedFlagIfItIsNotSet()
233244
{
234245
$subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
235246
$subscriber = ['subscriber_id' => 1, 'subscriber_status' => 1];
247+
$this->prepareStoreData();
236248

237249
$this->customerMock->expects($this->any())
238250
->method('getExtensionAttributes')
@@ -267,4 +279,17 @@ public function afterGetByIdDataProvider()
267279
[null, null, false],
268280
];
269281
}
282+
283+
/**
284+
* Prepare store information
285+
*
286+
* @return void
287+
*/
288+
private function prepareStoreData()
289+
{
290+
$storeId = 1;
291+
$storeMock = $this->createMock(Store::class);
292+
$storeMock->expects($this->any())->method('getId')->willReturn($storeId);
293+
$this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
294+
}
270295
}

0 commit comments

Comments
 (0)