Skip to content

Commit d382750

Browse files
committed
Update newsletter module
Fix unit tests according to new logic Add integration tests for case when customer have store_id = 0
1 parent 2e1d92f commit d382750

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

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

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

214214
public function testUpdateSubscription()
215215
{
216+
$storeId = 2;
216217
$customerId = 1;
217218
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
218219
->getMock();
@@ -234,7 +235,7 @@ public function testUpdateSubscription()
234235
->method('getConfirmationStatus')
235236
->with($customerId)
236237
->willReturn('account_confirmation_required');
237-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
238+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
238239
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
239240

240241
$storeModel = $this->getMockBuilder(\Magento\Store\Model\Store::class)
@@ -248,6 +249,7 @@ public function testUpdateSubscription()
248249

249250
public function testUnsubscribeCustomerById()
250251
{
252+
$storeId = 2;
251253
$customerId = 1;
252254
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
253255
->getMock();
@@ -265,7 +267,7 @@ public function testUnsubscribeCustomerById()
265267
);
266268
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
267269
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
268-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
270+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
269271
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
270272
$this->sendEmailCheck();
271273

@@ -274,6 +276,7 @@ public function testUnsubscribeCustomerById()
274276

275277
public function testSubscribeCustomerById()
276278
{
279+
$storeId = 2;
277280
$customerId = 1;
278281
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
279282
->getMock();
@@ -291,7 +294,7 @@ public function testSubscribeCustomerById()
291294
);
292295
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
293296
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
294-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
297+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
295298
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
296299
$this->sendEmailCheck();
297300

@@ -300,6 +303,7 @@ public function testSubscribeCustomerById()
300303

301304
public function testSubscribeCustomerById1()
302305
{
306+
$storeId = 2;
303307
$customerId = 1;
304308
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
305309
->getMock();
@@ -317,7 +321,7 @@ public function testSubscribeCustomerById1()
317321
);
318322
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
319323
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
320-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
324+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
321325
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
322326
$this->sendEmailCheck();
323327
$this->customerAccountManagement->expects($this->once())
@@ -331,6 +335,7 @@ public function testSubscribeCustomerById1()
331335

332336
public function testSubscribeCustomerByIdAfterConfirmation()
333337
{
338+
$storeId = 2;
334339
$customerId = 1;
335340
$customerDataMock = $this->getMockBuilder(\Magento\Customer\Api\Data\CustomerInterface::class)
336341
->getMock();
@@ -348,7 +353,7 @@ public function testSubscribeCustomerByIdAfterConfirmation()
348353
);
349354
$customerDataMock->expects($this->atLeastOnce())->method('getId')->willReturn('id');
350355
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
351-
$customerDataMock->expects($this->once())->method('getStoreId')->willReturn('store_id');
356+
$customerDataMock->expects($this->exactly(2))->method('getStoreId')->willReturn($storeId);
352357
$customerDataMock->expects($this->once())->method('getEmail')->willReturn('email');
353358
$this->sendEmailCheck();
354359
$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('customer@example.com')
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('customer@example.com');
198+
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
199+
$customer = $this->accountManagement->createAccount($customerDataObject);
200+
201+
$this->customerRepository->save($customer);
202+
203+
$subscriber->loadByEmail('customer@example.com');
204+
205+
$this->assertEquals($customer->getId(), (int)$subscriber->getCustomerId());
206+
$this->assertEquals($currentStore, (int)$subscriber->getStoreId());
207+
}
170208
}

0 commit comments

Comments
 (0)