Skip to content

Commit 6e35828

Browse files
author
Lysenko Olexandr
authored
Merge pull request #2714 from magento-chaika/MAGETWO-88217
[chaika] MAGETWO-88217: Guest users unable to sign up to newsletters between stores
2 parents a328a6b + 214d565 commit 6e35828

File tree

2 files changed

+82
-8
lines changed

2 files changed

+82
-8
lines changed

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Magento\Framework\Exception\MailException;
1212
use Magento\Framework\Exception\NoSuchEntityException;
1313
use Magento\Framework\Stdlib\DateTime\DateTime;
14+
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
15+
use Magento\Framework\Api\DataObjectHelper;
1416

1517
/**
1618
* Subscriber model
@@ -129,6 +131,16 @@ class Subscriber extends \Magento\Framework\Model\AbstractModel
129131
*/
130132
protected $inlineTranslation;
131133

134+
/**
135+
* @var CustomerInterfaceFactory
136+
*/
137+
private $customerFactory;
138+
139+
/**
140+
* @var DataObjectHelper
141+
*/
142+
private $dataObjectHelper;
143+
132144
/**
133145
* Initialize dependencies.
134146
*
@@ -146,6 +158,8 @@ class Subscriber extends \Magento\Framework\Model\AbstractModel
146158
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
147159
* @param array $data
148160
* @param DateTime|null $dateTime
161+
* @param CustomerInterfaceFactory|null $customerFactory
162+
* @param DataObjectHelper|null $dataObjectHelper
149163
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
150164
*/
151165
public function __construct(
@@ -162,7 +176,9 @@ public function __construct(
162176
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
163177
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
164178
array $data = [],
165-
DateTime $dateTime = null
179+
DateTime $dateTime = null,
180+
CustomerInterfaceFactory $customerFactory = null,
181+
DataObjectHelper $dataObjectHelper = null
166182
) {
167183
$this->_newsletterData = $newsletterData;
168184
$this->_scopeConfig = $scopeConfig;
@@ -173,6 +189,8 @@ public function __construct(
173189
$this->customerAccountManagement = $customerAccountManagement;
174190
$this->inlineTranslation = $inlineTranslation;
175191
$this->dateTime = $dateTime ?: ObjectManager::getInstance()->get(DateTime::class);
192+
$this->customerFactory = $customerFactory ?: ObjectManager::getInstance()->get(CustomerInterfaceFactory::class);
193+
$this->dataObjectHelper = $dataObjectHelper ?: ObjectManager::getInstance()->get(DataObjectHelper::class);
176194
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
177195
}
178196

@@ -346,7 +364,17 @@ public function isSubscribed()
346364
*/
347365
public function loadByEmail($subscriberEmail)
348366
{
349-
$this->addData($this->getResource()->loadByEmail($subscriberEmail));
367+
$storeId = $this->_storeManager->getStore()->getId();
368+
$customerData = ['store_id' => $storeId, 'email'=> $subscriberEmail];
369+
370+
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
371+
$customer = $this->customerFactory->create();
372+
$this->dataObjectHelper->populateWithArray(
373+
$customer,
374+
$customerData,
375+
\Magento\Customer\Api\Data\CustomerInterface::class
376+
);
377+
$this->addData($this->getResource()->loadByCustomerData($customer));
350378
return $this;
351379
}
352380

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

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ class SubscriberTest extends \PHPUnit\Framework\TestCase
6060
*/
6161
protected $objectManager;
6262

63+
/**
64+
* @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
65+
*/
66+
private $dataObjectHelper;
67+
68+
/**
69+
* @var \Magento\Customer\Api\Data\CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
70+
*/
71+
private $customerFactory;
72+
6373
/**
6474
* @var \Magento\Newsletter\Model\Subscriber
6575
*/
@@ -94,7 +104,13 @@ protected function setUp()
94104
'received'
95105
]);
96106
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
97-
107+
$this->customerFactory = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterfaceFactory::class)
108+
->setMethods(['create'])
109+
->disableOriginalConstructor()
110+
->getMock();
111+
$this->dataObjectHelper = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
112+
->disableOriginalConstructor()
113+
->getMock();
98114
$this->subscriber = $this->objectManager->getObject(
99115
\Magento\Newsletter\Model\Subscriber::class,
100116
[
@@ -106,15 +122,31 @@ protected function setUp()
106122
'customerRepository' => $this->customerRepository,
107123
'customerAccountManagement' => $this->customerAccountManagement,
108124
'inlineTranslation' => $this->inlineTranslation,
109-
'resource' => $this->resource
125+
'resource' => $this->resource,
126+
'customerFactory' => $this->customerFactory,
127+
'dataObjectHelper' => $this->dataObjectHelper
110128
]
111129
);
112130
}
113131

114132
public function testSubscribe()
115133
{
116134
$email = '[email protected]';
117-
$this->resource->expects($this->any())->method('loadByEmail')->willReturn(
135+
$storeId = 1;
136+
$customerData = ['store_id' => $storeId, 'email' => $email];
137+
$storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
138+
->disableOriginalConstructor()
139+
->getMock();
140+
$this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel);
141+
$storeModel->expects($this->any())->method('getId')->willReturn($storeId);
142+
$customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
143+
$this->customerFactory->expects($this->once())->method('create')->willReturn($customer);
144+
$this->dataObjectHelper->expects($this->once())->method('populateWithArray')->with(
145+
$customer,
146+
$customerData,
147+
\Magento\Customer\Api\Data\CustomerInterface::class
148+
);
149+
$this->resource->expects($this->any())->method('loadByCustomerData')->with($customer)->willReturn(
118150
[
119151
'subscriber_status' => 3,
120152
'subscriber_email' => $email,
@@ -128,7 +160,7 @@ public function testSubscribe()
128160
$this->customerSession->expects($this->any())->method('getCustomerId')->willReturn(1);
129161
$customerDataModel->expects($this->any())->method('getEmail')->willReturn($email);
130162
$this->customerRepository->expects($this->any())->method('getById')->willReturn($customerDataModel);
131-
$customerDataModel->expects($this->any())->method('getStoreId')->willReturn(1);
163+
$customerDataModel->expects($this->any())->method('getStoreId')->willReturn($storeId);
132164
$customerDataModel->expects($this->any())->method('getId')->willReturn(1);
133165
$this->sendEmailCheck();
134166
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
@@ -139,7 +171,21 @@ public function testSubscribe()
139171
public function testSubscribeNotLoggedIn()
140172
{
141173
$email = '[email protected]';
142-
$this->resource->expects($this->any())->method('loadByEmail')->willReturn(
174+
$storeId = 1;
175+
$customerData = ['store_id' => $storeId, 'email' => $email];
176+
$storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
177+
->disableOriginalConstructor()
178+
->getMock();
179+
$this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel);
180+
$storeModel->expects($this->any())->method('getId')->willReturn($storeId);
181+
$customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
182+
$this->customerFactory->expects($this->once())->method('create')->willReturn($customer);
183+
$this->dataObjectHelper->expects($this->once())->method('populateWithArray')->with(
184+
$customer,
185+
$customerData,
186+
\Magento\Customer\Api\Data\CustomerInterface::class
187+
);
188+
$this->resource->expects($this->any())->method('loadByCustomerData')->with($customer)->willReturn(
143189
[
144190
'subscriber_status' => 3,
145191
'subscriber_email' => $email,
@@ -153,7 +199,7 @@ public function testSubscribeNotLoggedIn()
153199
$this->customerSession->expects($this->any())->method('getCustomerId')->willReturn(1);
154200
$customerDataModel->expects($this->any())->method('getEmail')->willReturn($email);
155201
$this->customerRepository->expects($this->any())->method('getById')->willReturn($customerDataModel);
156-
$customerDataModel->expects($this->any())->method('getStoreId')->willReturn(1);
202+
$customerDataModel->expects($this->any())->method('getStoreId')->willReturn($storeId);
157203
$customerDataModel->expects($this->any())->method('getId')->willReturn(1);
158204
$this->sendEmailCheck();
159205
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();

0 commit comments

Comments
 (0)