Skip to content

Commit b2bf20b

Browse files
committed
MAGETWO-33654: Unable to save newsletter subscription information of customer in backend
1 parent 7568f7a commit b2bf20b

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Test\Unit\Block\Adminhtml\Edit\Tab;
7+
8+
use Magento\Customer\Controller\RegistryConstants;
9+
10+
class NewsletterTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* @var \Magento\Customer\Block\Adminhtml\Edit\Tab\Newsletter
14+
*/
15+
protected $model;
16+
/**
17+
* @var \PHPUnit_Framework_MockObject_MockObject
18+
*/
19+
protected $contextMock;
20+
/**
21+
* @var \PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
protected $registryMock;
24+
/**
25+
* @var \PHPUnit_Framework_MockObject_MockObject
26+
*/
27+
protected $formFactoryMock;
28+
/**
29+
* @var \PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
protected $subscriberFactoryMock;
32+
/**
33+
* @var \PHPUnit_Framework_MockObject_MockObject
34+
*/
35+
protected $accountManagementMock;
36+
/**
37+
* @var \PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
protected $urlBuilderMock;
40+
41+
public function setUp()
42+
{
43+
$this->contextMock = $this->getMock('\Magento\Backend\Block\Template\Context', [], [], '', false);
44+
$this->registryMock = $this->getMock('\Magento\Framework\Registry', [], [], '', false);
45+
$this->formFactoryMock = $this->getMock('\Magento\Framework\Data\FormFactory', [], [], '', false);
46+
$this->subscriberFactoryMock = $this->getMock(
47+
'\Magento\Newsletter\Model\SubscriberFactory',
48+
[],
49+
[],
50+
'',
51+
false
52+
);
53+
$this->accountManagementMock = $this->getMock(
54+
'\Magento\Customer\Api\AccountManagementInterface',
55+
[],
56+
[],
57+
'',
58+
false
59+
);
60+
$this->urlBuilderMock = $this->getMock('\Magento\Framework\UrlInterface', [], [], '', false);
61+
$this->contextMock->expects($this->once())->method('getUrlBuilder')->willReturn($this->urlBuilderMock);
62+
63+
$this->model = new \Magento\Customer\Block\Adminhtml\Edit\Tab\Newsletter(
64+
$this->contextMock,
65+
$this->registryMock,
66+
$this->formFactoryMock,
67+
$this->subscriberFactoryMock,
68+
$this->accountManagementMock
69+
);
70+
}
71+
72+
public function testInitFormCanNotShowTab()
73+
{
74+
$this->registryMock->expects($this->once())->method('registry')->with(RegistryConstants::CURRENT_CUSTOMER_ID)
75+
->willReturn(false);
76+
$this->assertSame($this->model, $this->model->initForm());
77+
}
78+
79+
public function testInitForm()
80+
{
81+
$subscriberMock = $this->getMock('\Magento\Newsletter\Model\Subscriber', [], [], '', false);
82+
$fieldsetMock = $this->getMock('\Magento\Framework\Data\Form\Element\Fieldset', [], [], '', false);
83+
$elementMock = $this->getMock('\Magento\Framework\Data\Form\Element\AbstractElement', [], [], '', false);
84+
$formMock = $this->getMock(
85+
'\Magento\Framework\Data\Form',
86+
['setHtmlIdPrefix', 'addFieldset', 'setValues', 'getElement', 'setForm', 'setParent', 'setBaseUrl'],
87+
[],
88+
'',
89+
false
90+
);
91+
$this->registryMock->expects($this->atLeastOnce())->method('registry')->willReturn($subscriberMock);
92+
$this->formFactoryMock->expects($this->once())->method('create')->willReturn($formMock);
93+
$formMock->expects($this->once())->method('setHtmlIdPrefix')->with('_newsletter');
94+
$this->subscriberFactoryMock->expects($this->once())->method('create')->willReturn($subscriberMock);
95+
$subscriberMock->expects($this->once())->method('loadByCustomerId')->with($subscriberMock)->willReturnSelf();
96+
$this->registryMock->expects($this->once())->method('register')->with('subscriber', $subscriberMock);
97+
$formMock->expects($this->once())->method('addFieldset')->willReturn($fieldsetMock);
98+
$this->accountManagementMock->expects($this->once())->method('isReadOnly')->with($subscriberMock)
99+
->willReturn(false);
100+
$subscriberMock->expects($this->once())->method('isSubscribed')->willReturn(true);
101+
$formMock->expects($this->once())->method('getElement')->willReturn($elementMock);
102+
$this->urlBuilderMock->expects($this->once())->method('getBaseUrl')->willReturn('domain.com');
103+
$this->assertSame($this->model, $this->model->initForm());
104+
}
105+
}

0 commit comments

Comments
 (0)