Skip to content

Commit b8fc202

Browse files
committed
Fixing the customer subscribing from different stores
1 parent 653d533 commit b8fc202

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
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,12 +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\Newsletter\Model\ResourceModel\Subscriber;
13-
use Magento\Customer\Api\Data\CustomerExtensionInterface;
1412
use Magento\Framework\App\ObjectManager;
13+
use Magento\Newsletter\Model\ResourceModel\Subscriber;
14+
use Magento\Newsletter\Model\SubscriberFactory;
15+
use Magento\Store\Model\StoreManagerInterface;
1516

1617
class CustomerPlugin
1718
{
@@ -37,22 +38,30 @@ class CustomerPlugin
3738
*/
3839
private $customerSubscriptionStatus = [];
3940

41+
/**
42+
* @var StoreManagerInterface
43+
*/
44+
private $storeManager;
45+
4046
/**
4147
* Initialize dependencies.
4248
*
4349
* @param SubscriberFactory $subscriberFactory
4450
* @param ExtensionAttributesFactory|null $extensionFactory
4551
* @param Subscriber|null $subscriberResource
52+
* @param StoreManagerInterface|null $storeManager
4653
*/
4754
public function __construct(
4855
SubscriberFactory $subscriberFactory,
4956
ExtensionAttributesFactory $extensionFactory = null,
50-
Subscriber $subscriberResource = null
57+
Subscriber $subscriberResource = null,
58+
StoreManagerInterface $storeManager = null
5159
) {
5260
$this->subscriberFactory = $subscriberFactory;
5361
$this->extensionFactory = $extensionFactory
5462
?: ObjectManager::getInstance()->get(ExtensionAttributesFactory::class);
5563
$this->subscriberResource = $subscriberResource ?: ObjectManager::getInstance()->get(Subscriber::class);
64+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManagerInterface::class);
5665
}
5766

5867
/**
@@ -149,6 +158,8 @@ public function afterDelete(CustomerRepository $subject, $result, CustomerInterf
149158
public function afterGetById(CustomerRepository $subject, CustomerInterface $customer)
150159
{
151160
$extensionAttributes = $customer->getExtensionAttributes();
161+
$storeId = $this->storeManager->getStore()->getId();
162+
$customer->setStoreId($storeId);
152163
if ($extensionAttributes === null) {
153164
/** @var CustomerExtensionInterface $extensionAttributes */
154165
$extensionAttributes = $this->extensionFactory->create(CustomerInterface::class);

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

Lines changed: 26 additions & 1 deletion
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,14 +94,17 @@ 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
$this->plugin = $this->objectManager->getObject(
93102
\Magento\Newsletter\Model\Plugin\CustomerPlugin::class,
94103
[
95104
'subscriberFactory' => $this->subscriberFactory,
96105
'extensionFactory' => $this->extensionFactoryMock,
97-
'subscriberResource' => $this->subscriberResourceMock
106+
'subscriberResource' => $this->subscriberResourceMock,
107+
'storeManager' => $this->storeManagerMock,
98108
]
99109
);
100110
}
@@ -198,6 +208,7 @@ public function testAfterGetByIdCreatesExtensionAttributesIfItIsNotSet(
198208
) {
199209
$subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
200210
$subscriber = [$subscriberStatusKey => $subscriberStatusValue];
211+
$this->prepareStoreData();
201212
$this->extensionFactoryMock->expects($this->any())
202213
->method('create')
203214
->willReturn($this->customerExtensionMock);
@@ -223,6 +234,7 @@ public function testAfterGetByIdSetsIsSubscribedFlagIfItIsNotSet()
223234
{
224235
$subject = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
225236
$subscriber = ['subscriber_id' => 1, 'subscriber_status' => 1];
237+
$this->prepareStoreData();
226238
$this->customerMock->expects($this->any())
227239
->method('getExtensionAttributes')
228240
->willReturn($this->customerExtensionMock);
@@ -255,4 +267,17 @@ public function afterGetByIdDataProvider()
255267
[null, null, false]
256268
];
257269
}
270+
271+
/**
272+
* Prepare store information
273+
*
274+
* @return void
275+
*/
276+
private function prepareStoreData()
277+
{
278+
$storeId = 1;
279+
$storeMock = $this->createMock(Store::class);
280+
$storeMock->expects($this->any())->method('getId')->willReturn($storeId);
281+
$this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
282+
}
258283
}

0 commit comments

Comments
 (0)