5
5
*/
6
6
namespace Magento \Customer \Test \Unit \Block \Adminhtml \Edit \Tab ;
7
7
8
+ use Magento \Backend \Model \Session ;
8
9
use Magento \Customer \Controller \RegistryConstants ;
9
10
10
11
class NewsletterTest extends \PHPUnit_Framework_TestCase
@@ -44,6 +45,11 @@ class NewsletterTest extends \PHPUnit_Framework_TestCase
44
45
*/
45
46
protected $ urlBuilderMock ;
46
47
48
+ /**
49
+ * @var Session|\PHPUnit_Framework_MockObject_MockObject
50
+ */
51
+ protected $ backendSessionMock ;
52
+
47
53
public function setUp ()
48
54
{
49
55
$ this ->contextMock = $ this ->getMock ('\Magento\Backend\Block\Template\Context ' , [], [], '' , false );
@@ -64,7 +70,12 @@ public function setUp()
64
70
false
65
71
);
66
72
$ this ->urlBuilderMock = $ this ->getMock ('\Magento\Framework\UrlInterface ' , [], [], '' , false );
73
+ $ this ->backendSessionMock = $ this ->getMockBuilder ('Magento\Backend\Model\Session ' )
74
+ ->setMethods (['getCustomerFormData ' ])
75
+ ->disableOriginalConstructor ()
76
+ ->getMock ();
67
77
$ this ->contextMock ->expects ($ this ->once ())->method ('getUrlBuilder ' )->willReturn ($ this ->urlBuilderMock );
78
+ $ this ->contextMock ->expects ($ this ->once ())->method ('getBackendSession ' )->willReturn ($ this ->backendSessionMock );
68
79
69
80
$ this ->model = new \Magento \Customer \Block \Adminhtml \Edit \Tab \Newsletter (
70
81
$ this ->contextMock ,
@@ -84,28 +95,98 @@ public function testInitFormCanNotShowTab()
84
95
85
96
public function testInitForm ()
86
97
{
98
+ $ customerId = 1 ;
99
+
87
100
$ subscriberMock = $ this ->getMock ('\Magento\Newsletter\Model\Subscriber ' , [], [], '' , false );
88
101
$ fieldsetMock = $ this ->getMock ('\Magento\Framework\Data\Form\Element\Fieldset ' , [], [], '' , false );
89
- $ elementMock = $ this ->getMock ('\ Magento\Framework\Data\Form\Element\AbstractElement ' , [], [], '' , false );
102
+ $ elementMock = $ this ->getMock ('Magento\Framework\Data\Form\Element\Checkbox ' , [' setIsChecked ' ], [], '' , false );
90
103
$ formMock = $ this ->getMock (
91
104
'\Magento\Framework\Data\Form ' ,
92
105
['setHtmlIdPrefix ' , 'addFieldset ' , 'setValues ' , 'getElement ' , 'setForm ' , 'setParent ' , 'setBaseUrl ' ],
93
106
[],
94
107
'' ,
95
108
false
96
109
);
97
- $ this ->registryMock ->expects ($ this ->atLeastOnce ())->method ('registry ' )->willReturn ($ subscriberMock );
110
+ $ this ->registryMock ->expects ($ this ->exactly (3 ))
111
+ ->method ('registry ' )
112
+ ->willReturnMap (
113
+ [
114
+ [RegistryConstants::CURRENT_CUSTOMER_ID , $ customerId ],
115
+ ['subscriber ' , $ subscriberMock ],
116
+ ]
117
+ );
98
118
$ this ->formFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ formMock );
99
119
$ formMock ->expects ($ this ->once ())->method ('setHtmlIdPrefix ' )->with ('_newsletter ' );
100
120
$ this ->subscriberFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ subscriberMock );
101
- $ subscriberMock ->expects ($ this ->once ())->method ('loadByCustomerId ' )->with ($ subscriberMock )->willReturnSelf ();
121
+ $ subscriberMock ->expects ($ this ->once ())->method ('loadByCustomerId ' )->with ($ customerId )->willReturnSelf ();
102
122
$ this ->registryMock ->expects ($ this ->once ())->method ('register ' )->with ('subscriber ' , $ subscriberMock );
103
123
$ formMock ->expects ($ this ->once ())->method ('addFieldset ' )->willReturn ($ fieldsetMock );
104
- $ this ->accountManagementMock ->expects ($ this ->once ())->method ('isReadOnly ' )->with ($ subscriberMock )
124
+ $ fieldsetMock ->expects ($ this ->once ())->method ('addField ' )->willReturn ($ elementMock );
125
+ $ this ->accountManagementMock ->expects ($ this ->once ())->method ('isReadOnly ' )->with ($ customerId )
105
126
->willReturn (false );
106
127
$ subscriberMock ->expects ($ this ->once ())->method ('isSubscribed ' )->willReturn (true );
107
- $ formMock ->expects ($ this ->once ())->method ('getElement ' )->willReturn ($ elementMock );
108
128
$ this ->urlBuilderMock ->expects ($ this ->once ())->method ('getBaseUrl ' )->willReturn ('domain.com ' );
129
+
130
+ $ this ->backendSessionMock ->expects ($ this ->once ())->method ('getCustomerFormData ' )->willReturn (null );
131
+
132
+ $ elementMock ->expects ($ this ->once ())
133
+ ->method ('setIsChecked ' )
134
+ ->with (true );
135
+
136
+ $ this ->assertSame ($ this ->model , $ this ->model ->initForm ());
137
+ }
138
+
139
+ public function testInitFormWithCustomerFormData ()
140
+ {
141
+ $ customerId = 1 ;
142
+
143
+ $ subscriberMock = $ this ->getMock ('\Magento\Newsletter\Model\Subscriber ' , [], [], '' , false );
144
+ $ fieldsetMock = $ this ->getMock ('\Magento\Framework\Data\Form\Element\Fieldset ' , [], [], '' , false );
145
+ $ elementMock = $ this ->getMock ('Magento\Framework\Data\Form\Element\Checkbox ' , ['setIsChecked ' ], [], '' , false );
146
+ $ formMock = $ this ->getMock (
147
+ '\Magento\Framework\Data\Form ' ,
148
+ ['setHtmlIdPrefix ' , 'addFieldset ' , 'setValues ' , 'getElement ' , 'setForm ' , 'setParent ' , 'setBaseUrl ' ],
149
+ [],
150
+ '' ,
151
+ false
152
+ );
153
+ $ this ->registryMock ->expects ($ this ->exactly (3 ))
154
+ ->method ('registry ' )
155
+ ->willReturnMap (
156
+ [
157
+ [RegistryConstants::CURRENT_CUSTOMER_ID , $ customerId ],
158
+ ['subscriber ' , $ subscriberMock ],
159
+ ]
160
+ );
161
+ $ this ->formFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ formMock );
162
+ $ formMock ->expects ($ this ->once ())->method ('setHtmlIdPrefix ' )->with ('_newsletter ' );
163
+ $ this ->subscriberFactoryMock ->expects ($ this ->once ())->method ('create ' )->willReturn ($ subscriberMock );
164
+ $ subscriberMock ->expects ($ this ->once ())->method ('loadByCustomerId ' )->with ($ customerId )->willReturnSelf ();
165
+ $ formMock ->expects ($ this ->once ())->method ('addFieldset ' )->willReturn ($ fieldsetMock );
166
+ $ fieldsetMock ->expects ($ this ->once ())->method ('addField ' )->willReturn ($ elementMock );
167
+ $ this ->accountManagementMock ->expects ($ this ->once ())->method ('isReadOnly ' )->with ($ customerId )
168
+ ->willReturn (false );
169
+ $ subscriberMock ->expects ($ this ->once ())->method ('isSubscribed ' )->willReturn (false );
170
+ $ this ->urlBuilderMock ->expects ($ this ->once ())->method ('getBaseUrl ' )->willReturn ('domain.com ' );
171
+
172
+ $ this ->backendSessionMock ->expects ($ this ->once ())
173
+ ->method ('getCustomerFormData ' )
174
+ ->willReturn ([
175
+ 'customer ' => [
176
+ 'entity_id ' => $ customerId ,
177
+ ],
178
+ 'subscription ' => true ,
179
+ ]);
180
+
181
+ $ elementMock ->expects ($ this ->exactly (2 ))
182
+ ->method ('setIsChecked ' )
183
+ ->willReturnMap (
184
+ [
185
+ [false ],
186
+ [true ],
187
+ ]
188
+ );
189
+
109
190
$ this ->assertSame ($ this ->model , $ this ->model ->initForm ());
110
191
}
111
192
}
0 commit comments