Skip to content

Commit b15dd68

Browse files
author
Sergey Shvets
committed
Merge branch 'MAGETWO-93713-22' of github.com:magento-chaika/magento2ce into chaika_sept_23
2 parents d7bb182 + bd7ba54 commit b15dd68

File tree

5 files changed

+110
-39
lines changed

5 files changed

+110
-39
lines changed

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

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,6 @@ class Subscriber extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
4848
*/
4949
protected $mathRandom;
5050

51-
/**
52-
* Guest customer id
53-
*
54-
* @var int
55-
*/
56-
private $guestCustomerId = 0;
57-
5851
/**
5952
* Construct
6053
*
@@ -75,8 +68,7 @@ public function __construct(
7568
}
7669

7770
/**
78-
* Initialize resource model
79-
* Get tablename from config
71+
* Initialize resource model. Get tablename from config
8072
*
8173
* @return void
8274
*/
@@ -143,24 +135,22 @@ public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface
143135
return $result;
144136
}
145137

146-
if ($customer->getId() === $this->guestCustomerId) {
147-
$select = $this->connection
148-
->select()
149-
->from($this->getMainTable())
150-
->where('subscriber_email=:subscriber_email and store_id=:store_id');
151-
152-
$result = $this->connection
153-
->fetchRow(
154-
$select,
155-
[
156-
'subscriber_email' => $customer->getEmail(),
157-
'store_id' => $customer->getStoreId()
158-
]
159-
);
160-
161-
if ($result) {
162-
return $result;
163-
}
138+
$select = $this->connection
139+
->select()
140+
->from($this->getMainTable())
141+
->where('subscriber_email=:subscriber_email and store_id=:store_id');
142+
143+
$result = $this->connection
144+
->fetchRow(
145+
$select,
146+
[
147+
'subscriber_email' => $customer->getEmail(),
148+
'store_id' => $customer->getStoreId()
149+
]
150+
);
151+
152+
if ($result) {
153+
return $result;
164154
}
165155

166156
return [];

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Magento\Customer\Api\CustomerRepositoryInterface;
1010
use Magento\Framework\Exception\MailException;
1111
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
13+
use Magento\Framework\Api\DataObjectHelper;
14+
use Magento\Framework\App\ObjectManager;
1215

1316
/**
1417
* Subscriber model
@@ -127,6 +130,16 @@ class Subscriber extends \Magento\Framework\Model\AbstractModel
127130
*/
128131
protected $inlineTranslation;
129132

133+
/**
134+
* @var CustomerInterfaceFactory
135+
*/
136+
private $customerFactory;
137+
138+
/**
139+
* @var DataObjectHelper
140+
*/
141+
private $dataObjectHelper;
142+
130143
/**
131144
* Initialize dependencies.
132145
*
@@ -144,6 +157,8 @@ class Subscriber extends \Magento\Framework\Model\AbstractModel
144157
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
145158
* @param array $data
146159
* @param \Magento\Framework\Stdlib\DateTime\DateTime|null $dateTime
160+
* @param CustomerInterfaceFactory|null $customerFactory
161+
* @param DataObjectHelper|null $dataObjectHelper
147162
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
148163
*/
149164
public function __construct(
@@ -160,7 +175,9 @@ public function __construct(
160175
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
161176
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
162177
array $data = [],
163-
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime = null
178+
\Magento\Framework\Stdlib\DateTime\DateTime $dateTime = null,
179+
CustomerInterfaceFactory $customerFactory = null,
180+
DataObjectHelper $dataObjectHelper = null
164181
) {
165182
$this->_newsletterData = $newsletterData;
166183
$this->_scopeConfig = $scopeConfig;
@@ -170,6 +187,10 @@ public function __construct(
170187
$this->dateTime = $dateTime ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
171188
\Magento\Framework\Stdlib\DateTime\DateTime::class
172189
);
190+
$this->customerFactory = $customerFactory ?: ObjectManager::getInstance()
191+
->get(CustomerInterfaceFactory::class);
192+
$this->dataObjectHelper = $dataObjectHelper ?: ObjectManager::getInstance()
193+
->get(DataObjectHelper::class);
173194
$this->customerRepository = $customerRepository;
174195
$this->customerAccountManagement = $customerAccountManagement;
175196
$this->inlineTranslation = $inlineTranslation;
@@ -346,7 +367,17 @@ public function isSubscribed()
346367
*/
347368
public function loadByEmail($subscriberEmail)
348369
{
349-
$this->addData($this->getResource()->loadByEmail($subscriberEmail));
370+
$storeId = $this->_storeManager->getStore()->getId();
371+
$customerData = ['store_id' => $storeId, 'email'=> $subscriberEmail];
372+
373+
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
374+
$customer = $this->customerFactory->create();
375+
$this->dataObjectHelper->populateWithArray(
376+
$customer,
377+
$customerData,
378+
\Magento\Customer\Api\Data\CustomerInterface::class
379+
);
380+
$this->addData($this->getResource()->loadByCustomerData($customer));
350381
return $this;
351382
}
352383

@@ -497,7 +528,7 @@ public function subscribeCustomerById($customerId)
497528
}
498529

499530
/**
500-
* unsubscribe the customer with the id provided
531+
* Unsubscribe the customer with the id provided
501532
*
502533
* @param int $customerId
503534
* @return $this

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

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ class SubscriberTest extends \PHPUnit\Framework\TestCase
6262
*/
6363
protected $objectManager;
6464

65+
/**
66+
* @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
67+
*/
68+
private $dataObjectHelper;
69+
70+
/**
71+
* @var \Magento\Customer\Api\Data\CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
72+
*/
73+
private $customerFactory;
74+
6575
/**
6676
* @var \Magento\Newsletter\Model\Subscriber
6777
*/
@@ -97,6 +107,14 @@ protected function setUp()
97107
]);
98108
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
99109

110+
$this->customerFactory = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterfaceFactory::class)
111+
->setMethods(['create'])
112+
->disableOriginalConstructor()
113+
->getMock();
114+
$this->dataObjectHelper = $this->getMockBuilder(\Magento\Framework\Api\DataObjectHelper::class)
115+
->disableOriginalConstructor()
116+
->getMock();
117+
100118
$this->subscriber = $this->objectManager->getObject(
101119
\Magento\Newsletter\Model\Subscriber::class,
102120
[
@@ -108,15 +126,31 @@ protected function setUp()
108126
'customerRepository' => $this->customerRepository,
109127
'customerAccountManagement' => $this->customerAccountManagement,
110128
'inlineTranslation' => $this->inlineTranslation,
111-
'resource' => $this->resource
129+
'resource' => $this->resource,
130+
'customerFactory' => $this->customerFactory,
131+
'dataObjectHelper' => $this->dataObjectHelper
112132
]
113133
);
114134
}
115135

116136
public function testSubscribe()
117137
{
118138
$email = 'subscriber_email@magento.com';
119-
$this->resource->expects($this->any())->method('loadByEmail')->willReturn(
139+
$storeId = 1;
140+
$customerData = ['store_id' => $storeId, 'email' => $email];
141+
$storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
142+
->disableOriginalConstructor()
143+
->getMock();
144+
$this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel);
145+
$storeModel->expects($this->any())->method('getId')->willReturn($storeId);
146+
$customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
147+
$this->customerFactory->expects($this->once())->method('create')->willReturn($customer);
148+
$this->dataObjectHelper->expects($this->once())->method('populateWithArray')->with(
149+
$customer,
150+
$customerData,
151+
\Magento\Customer\Api\Data\CustomerInterface::class
152+
);
153+
$this->resource->expects($this->any())->method('loadByCustomerData')->with($customer)->willReturn(
120154
[
121155
'subscriber_status' => Subscriber::STATUS_UNSUBSCRIBED,
122156
'subscriber_email' => $email,
@@ -130,7 +164,7 @@ public function testSubscribe()
130164
$this->customerSession->expects($this->any())->method('getCustomerId')->willReturn(1);
131165
$customerDataModel->expects($this->any())->method('getEmail')->willReturn($email);
132166
$this->customerRepository->expects($this->any())->method('getById')->willReturn($customerDataModel);
133-
$customerDataModel->expects($this->any())->method('getStoreId')->willReturn(1);
167+
$customerDataModel->expects($this->any())->method('getStoreId')->willReturn($storeId);
134168
$customerDataModel->expects($this->any())->method('getId')->willReturn(1);
135169
$this->sendEmailCheck();
136170
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
@@ -141,7 +175,21 @@ public function testSubscribe()
141175
public function testSubscribeNotLoggedIn()
142176
{
143177
$email = 'subscriber_email@magento.com';
144-
$this->resource->expects($this->any())->method('loadByEmail')->willReturn(
178+
$storeId = 1;
179+
$customerData = ['store_id' => $storeId, 'email' => $email];
180+
$storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
181+
->disableOriginalConstructor()
182+
->getMock();
183+
$this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel);
184+
$storeModel->expects($this->any())->method('getId')->willReturn($storeId);
185+
$customer = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
186+
$this->customerFactory->expects($this->once())->method('create')->willReturn($customer);
187+
$this->dataObjectHelper->expects($this->once())->method('populateWithArray')->with(
188+
$customer,
189+
$customerData,
190+
\Magento\Customer\Api\Data\CustomerInterface::class
191+
);
192+
$this->resource->expects($this->any())->method('loadByCustomerData')->with($customer)->willReturn(
145193
[
146194
'subscriber_status' => Subscriber::STATUS_UNSUBSCRIBED,
147195
'subscriber_email' => $email,
@@ -155,7 +203,7 @@ public function testSubscribeNotLoggedIn()
155203
$this->customerSession->expects($this->any())->method('getCustomerId')->willReturn(1);
156204
$customerDataModel->expects($this->any())->method('getEmail')->willReturn($email);
157205
$this->customerRepository->expects($this->any())->method('getById')->willReturn($customerDataModel);
158-
$customerDataModel->expects($this->any())->method('getStoreId')->willReturn(1);
206+
$customerDataModel->expects($this->any())->method('getStoreId')->willReturn($storeId);
159207
$customerDataModel->expects($this->any())->method('getId')->willReturn(1);
160208
$this->sendEmailCheck();
161209
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ public function testCustomerCreated()
6363
->setFirstname('Firstname')
6464
->setLastname('Lastname')
6565
->setEmail('customer_two@example.com');
66-
$this->customerRepository->save(
66+
$createdCustomer = $this->customerRepository->save(
6767
$customerDataObject,
6868
$this->accountManagement->getPasswordHash('password')
6969
);
7070

7171
$subscriber->loadByEmail('customer_two@example.com');
7272
$this->assertTrue($subscriber->isSubscribed());
73-
$this->assertEquals(0, (int)$subscriber->getCustomerId());
73+
$this->assertEquals((int)$createdCustomer->getId(), (int)$subscriber->getCustomerId());
7474
}
7575

7676
/**

dev/tests/integration/testsuite/Magento/Newsletter/Model/ResourceModel/SubscriberTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ public function testLoadByCustomerDataWithCustomerId()
4040
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php
4141
* @magentoDataFixture Magento/Customer/_files/two_customers.php
4242
*/
43-
public function testTryLoadByCustomerDataWithoutCustomerId()
43+
public function testLoadByCustomerDataWithoutCustomerId()
4444
{
4545
/** @var \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository */
4646
$customerRepository = Bootstrap::getObjectManager()
4747
->create(\Magento\Customer\Api\CustomerRepositoryInterface::class);
4848
$customerData = $customerRepository->getById(2);
4949
$result = $this->_resourceModel->loadByCustomerData($customerData);
50-
$this->assertEmpty($result);
50+
51+
$this->assertEquals(0, $result['customer_id']);
52+
$this->assertEquals('customer_two@example.com', $result['subscriber_email']);
5153
}
5254
}

0 commit comments

Comments
 (0)