Skip to content

Commit 16f82bf

Browse files
author
lpoluyanov
committed
MAGETWO-36266: [GITHUB] Something went wrong with the subscription. #1157
1 parent 948f734 commit 16f82bf

File tree

2 files changed

+163
-1
lines changed

2 files changed

+163
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,14 +442,14 @@ public function subscribe($email)
442442
$this->setStatusChanged(true);
443443

444444
try {
445-
$this->save();
446445
if ($isConfirmNeed === true
447446
&& $isOwnSubscribes === false
448447
) {
449448
$this->sendConfirmationRequestEmail();
450449
} else {
451450
$this->sendConfirmationSuccessEmail();
452451
}
452+
$this->save();
453453
return $this->getStatus();
454454
} catch (\Exception $e) {
455455
throw new \Exception($e->getMessage());
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Newsletter\Model;
7+
8+
class SubscriberTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var \Magento\Newsletter\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
12+
*/
13+
protected $newsletterData;
14+
15+
/**
16+
* @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
17+
*/
18+
protected $scopeConfig;
19+
20+
/**
21+
* @var \Magento\Framework\Mail\Template\TransportBuilder|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
protected $transportBuilder;
24+
25+
/**
26+
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
protected $storeManager;
29+
30+
/**
31+
* @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
protected $customerSession;
34+
35+
/**
36+
* @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
protected $customerRepository;
39+
40+
/**
41+
* @var \Magento\Customer\Api\AccountManagementInterface|\PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
protected $customerAccountManagement;
44+
45+
/**
46+
* @var \Magento\Framework\Translate\Inline\StateInterface|\PHPUnit_Framework_MockObject_MockObject
47+
*/
48+
protected $inlineTranslation;
49+
50+
/**
51+
* @var \Magento\Newsletter\Model\Resource\Subscriber|\PHPUnit_Framework_MockObject_MockObject
52+
*/
53+
protected $resource;
54+
55+
/**
56+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
57+
*/
58+
protected $objectManager;
59+
60+
/**
61+
* @var \Magento\Newsletter\Model\Subscriber
62+
*/
63+
protected $subscriber;
64+
65+
public function setUp() {
66+
$this->newsletterData = $this->getMock('Magento\Newsletter\Helper\Data', [], [], '', false);
67+
$this->scopeConfig = $this->getMock('Magento\Framework\App\Config\ScopeConfigInterface');
68+
$this->transportBuilder = $this->getMock(
69+
'Magento\Framework\Mail\Template\TransportBuilder',
70+
[
71+
'setTemplateIdentifier',
72+
'setTemplateOptions',
73+
'setTemplateVars',
74+
'setFrom',
75+
'addTo',
76+
'getTransport'
77+
],
78+
[],
79+
'',
80+
false
81+
);
82+
$this->storeManager = $this->getMock('Magento\Store\Model\StoreManagerInterface');
83+
$this->customerSession = $this->getMock(
84+
'Magento\Customer\Model\Session',
85+
[
86+
'isLoggedIn',
87+
'getCustomerDataObject',
88+
'getCustomerId'
89+
],
90+
[],
91+
'',
92+
false
93+
);
94+
$this->customerRepository = $this->getMock('Magento\Customer\Api\CustomerRepositoryInterface');
95+
$this->customerAccountManagement = $this->getMock('Magento\Customer\Api\AccountManagementInterface');
96+
$this->inlineTranslation = $this->getMock('Magento\Framework\Translate\Inline\StateInterface');
97+
$this->resource = $this->getMock(
98+
'Magento\Newsletter\Model\Resource\Subscriber',
99+
[
100+
'loadByEmail',
101+
'getIdFieldName',
102+
'save'
103+
],
104+
[],
105+
'',
106+
false
107+
);
108+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
109+
110+
$this->subscriber = $this->objectManager->getObject('Magento\Newsletter\Model\Subscriber',
111+
[
112+
'newsletterData' => $this->newsletterData,
113+
'scopeConfig' => $this->scopeConfig,
114+
'transportBuilder' => $this->transportBuilder,
115+
'storeManager' => $this->storeManager,
116+
'customerSession' => $this->customerSession,
117+
'customerRepository' => $this->customerRepository,
118+
'customerAccountManagement' => $this->customerAccountManagement,
119+
'inlineTranslation' => $this->inlineTranslation,
120+
'resource' => $this->resource
121+
]
122+
);
123+
}
124+
125+
public function testSubscribe()
126+
{
127+
$email = 'subscriber_email@magento.com';
128+
$this->resource->expects($this->any())->method('loadByEmail')->willReturn(
129+
[
130+
'subscriber_status' => 3,
131+
'subscriber_email' => $email,
132+
'name' => 'subscriber_name'
133+
]
134+
);
135+
$this->resource->expects($this->any())->method('getIdFieldName')->willReturn('id_field');
136+
$this->scopeConfig->expects($this->any())->method('getValue')->willReturn(true);
137+
$this->customerSession->expects($this->any())->method('isLoggedIn')->willReturn(true);
138+
$customerDataModel = $this->getMock('\Magento\Customer\Api\Data\CustomerInterface');
139+
$this->customerSession->expects($this->any())->method('getCustomerDataObject')->willReturn($customerDataModel);
140+
$this->customerSession->expects($this->any())->method('getCustomerId')->willReturn(1);
141+
$customerDataModel->expects($this->any())->method('getEmail')->willReturn($email);
142+
$this->customerRepository->expects($this->any())->method('getById')->willReturn($customerDataModel);
143+
$customerDataModel->expects($this->any())->method('getStoreId')->willReturn(1);
144+
$customerDataModel->expects($this->any())->method('getId')->willReturn(1);
145+
$this->transportBuilder->expects($this->any())->method('setTemplateIdentifier')->willReturnSelf();
146+
$this->transportBuilder->expects($this->any())->method('setTemplateOptions')->willReturnSelf();
147+
$this->transportBuilder->expects($this->any())->method('setTemplateVars')->willReturnSelf();
148+
$this->transportBuilder->expects($this->any())->method('setFrom')->willReturnSelf();
149+
$this->transportBuilder->expects($this->any())->method('addTo')->willReturnSelf();
150+
$storeModel = $this->getMock('\Magento\Store\Model\Store', ['getId'], [], '', false);
151+
$this->scopeConfig->expects($this->any())->method('getValue')->willReturn('owner_email@magento.com');
152+
$this->storeManager->expects($this->any())->method('getStore')->willReturn($storeModel);
153+
$storeModel->expects($this->any())->method('getId')->willReturn(1);
154+
$transport = $this->getMock('\Magento\Framework\Mail\TransportInterface');
155+
$this->transportBuilder->expects($this->any())->method('getTransport')->willReturn($transport);
156+
$transport->expects($this->any())->method('sendMessage')->willReturnSelf();
157+
$inlineTranslation = $this->getMock('Magento\Framework\Translate\Inline\StateInterface');
158+
$inlineTranslation->expects($this->any())->method('resume')->willReturnSelf();
159+
$this->resource->expects($this->atLeastOnce())->method('save')->willReturnSelf();
160+
$this->assertEquals(1, $this->subscriber->subscribe($email));
161+
}
162+
}

0 commit comments

Comments
 (0)