5
5
*/
6
6
namespace Magento \Captcha \Test \Unit \Observer ;
7
7
8
+ use Magento \Captcha \Helper \Data ;
9
+ use Magento \Captcha \Model \DefaultModel ;
10
+ use Magento \Captcha \Observer \CaptchaStringResolver ;
11
+ use Magento \Captcha \Observer \CheckContactUsFormObserver ;
12
+ use Magento \Framework \App \Action \Action ;
13
+ use Magento \Framework \App \ActionFlag ;
14
+ use Magento \Framework \App \Request \DataPersistorInterface ;
15
+ use Magento \Framework \App \Request \Http ;
16
+ use Magento \Framework \App \Response \RedirectInterface ;
17
+ use Magento \Framework \Event \Observer ;
18
+ use Magento \Framework \Message \ManagerInterface ;
19
+ use Magento \Framework \Session \SessionManager ;
20
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
21
+ use PHPUnit \Framework \MockObject \MockObject ;
22
+ use PHPUnit \Framework \TestCase ;
23
+
8
24
/**
25
+ * Test class for \Magento\Captcha\Observer\CheckContactUsFormObserver
26
+ *
9
27
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
10
28
*/
11
- class CheckContactUsFormObserverTest extends \ PHPUnit \ Framework \ TestCase
29
+ class CheckContactUsFormObserverTest extends TestCase
12
30
{
13
31
/**
14
- * @var \Magento\Captcha\Observer\CheckContactUsFormObserver
32
+ * @var ObjectManager
15
33
*/
16
- protected $ checkContactUsFormObserver ;
34
+ private $ objectManagerHelper ;
17
35
18
36
/**
19
- * @var \Magento\Captcha\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
37
+ * @var CheckContactUsFormObserver
20
38
*/
21
- protected $ helperMock ;
39
+ private $ checkContactUsFormObserver ;
22
40
23
41
/**
24
- * @var \Magento\Framework\App\ActionFlag|\PHPUnit_Framework_MockObject_MockObject
42
+ * @var Data|MockObject
25
43
*/
26
- protected $ actionFlagMock ;
44
+ private $ helperMock ;
27
45
28
- /*
29
- * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
46
+ /**
47
+ * @var ActionFlag|MockObject
30
48
*/
31
- protected $ messageManagerMock ;
49
+ private $ actionFlagMock ;
32
50
33
51
/**
34
- * @var \Magento\Framework\App\Response\RedirectInterface|\PHPUnit_Framework_MockObject_MockObject
52
+ * @var ManagerInterface|MockObject
35
53
*/
36
- protected $ redirectMock ;
54
+ private $ messageManagerMock ;
37
55
38
56
/**
39
- * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
57
+ * @var RedirectInterface|MockObject
40
58
*/
41
- protected $ objectManagerHelper ;
59
+ private $ redirectMock ;
42
60
43
61
/**
44
- * @var \Magento\Captcha\Observer\ CaptchaStringResolver|\PHPUnit_Framework_MockObject_MockObject
62
+ * @var CaptchaStringResolver|MockObject
45
63
*/
46
- protected $ captchaStringResolverMock ;
64
+ private $ captchaStringResolverMock ;
47
65
48
66
/**
49
- * @var \Magento\Framework\Session\SessionManager|\PHPUnit_Framework_MockObject_MockObject
67
+ * @var DataPersistorInterface|MockObject
50
68
*/
51
- protected $ sessionMock ;
69
+ private $ dataPersistorMock ;
52
70
53
71
/**
54
- * @var \Magento\Captcha\Model\DefaultModel|\PHPUnit_Framework_MockObject_MockObject
72
+ * @var SessionManager|MockObject
55
73
*/
56
- protected $ captchaMock ;
74
+ private $ sessionMock ;
57
75
58
76
/**
59
- * @var \Magento\Framework\App\Request\DataPersistorInterface|\PHPUnit_Framework_MockObject_MockObject
77
+ * @var DefaultModel|MockObject
60
78
*/
61
- protected $ dataPersistorMock ;
79
+ private $ captchaMock ;
62
80
63
81
protected function setUp ()
64
82
{
65
- $ this ->objectManagerHelper = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
83
+ $ this ->objectManagerHelper = new ObjectManager ($ this );
84
+
85
+ $ this ->helperMock = $ this ->createMock (Data::class);
86
+ $ this ->actionFlagMock = $ this ->createMock (ActionFlag::class);
87
+ $ this ->messageManagerMock = $ this ->createMock (ManagerInterface::class);
88
+ $ this ->redirectMock = $ this ->createMock (RedirectInterface::class);
89
+ $ this ->captchaStringResolverMock = $ this ->createMock (CaptchaStringResolver::class);
90
+ $ this ->dataPersistorMock = $ this ->getMockBuilder (DataPersistorInterface::class)
91
+ ->getMockForAbstractClass ();
66
92
67
- $ this ->helperMock = $ this ->createMock (\Magento \Captcha \Helper \Data::class);
68
- $ this ->actionFlagMock = $ this ->createMock (\Magento \Framework \App \ActionFlag::class);
69
- $ this ->messageManagerMock = $ this ->createMock (\Magento \Framework \Message \ManagerInterface::class);
70
- $ this ->redirectMock = $ this ->createMock (\Magento \Framework \App \Response \RedirectInterface::class);
71
- $ this ->captchaStringResolverMock = $ this ->createMock (\Magento \Captcha \Observer \CaptchaStringResolver::class);
72
93
$ this ->sessionMock = $ this ->createPartialMock (
73
- \ Magento \ Framework \ Session \ SessionManager::class,
94
+ SessionManager::class,
74
95
['addErrorMessage ' ]
75
96
);
76
- $ this ->dataPersistorMock = $ this ->getMockBuilder (\Magento \Framework \App \Request \DataPersistorInterface::class)
77
- ->getMockForAbstractClass ();
97
+ $ this ->captchaMock = $ this ->createMock (DefaultModel::class);
78
98
79
99
$ this ->checkContactUsFormObserver = $ this ->objectManagerHelper ->getObject (
80
- \ Magento \ Captcha \ Observer \ CheckContactUsFormObserver::class,
100
+ CheckContactUsFormObserver::class,
81
101
[
82
102
'helper ' => $ this ->helperMock ,
83
103
'actionFlag ' => $ this ->actionFlagMock ,
84
104
'messageManager ' => $ this ->messageManagerMock ,
85
105
'redirect ' => $ this ->redirectMock ,
86
- 'captchaStringResolver ' => $ this ->captchaStringResolverMock
106
+ 'captchaStringResolver ' => $ this ->captchaStringResolverMock ,
107
+ 'dataPersistor ' => $ this ->dataPersistorMock
87
108
]
88
109
);
89
- $ this ->objectManagerHelper ->setBackwardCompatibleProperty (
90
- $ this ->checkContactUsFormObserver ,
91
- 'dataPersistor ' ,
92
- $ this ->dataPersistorMock
93
- );
94
-
95
- $ this ->captchaMock = $ this ->createMock (\Magento \Captcha \Model \DefaultModel::class);
96
110
}
97
111
98
112
public function testCheckContactUsFormWhenCaptchaIsRequiredAndValid ()
99
113
{
100
114
$ formId = 'contact_us ' ;
101
115
$ captchaValue = 'some-value ' ;
102
116
103
- $ controller = $ this ->createMock (\Magento \Framework \App \Action \Action::class);
104
- $ request = $ this ->createMock (\Magento \Framework \App \Request \Http::class);
105
- $ request ->expects ($ this ->any ())
106
- ->method ('getPost ' )
107
- ->with (\Magento \Captcha \Helper \Data::INPUT_NAME_FIELD_VALUE , null )
117
+ $ controller = $ this ->createMock (Action::class);
118
+ $ request = $ this ->createMock (Http::class);
119
+ $ request ->method ('getPost ' )
120
+ ->with (Data::INPUT_NAME_FIELD_VALUE , null )
108
121
->willReturn ([$ formId => $ captchaValue ]);
109
- $ controller ->expects ( $ this -> any ())-> method ('getRequest ' )->willReturn ($ request );
110
- $ this ->captchaMock ->expects ( $ this -> any ())-> method ('isRequired ' )->willReturn (true );
122
+ $ controller ->method ('getRequest ' )->willReturn ($ request );
123
+ $ this ->captchaMock ->method ('isRequired ' )->willReturn (true );
111
124
$ this ->captchaMock ->expects ($ this ->once ())
112
125
->method ('isCorrect ' )
113
126
->with ($ captchaValue )
@@ -116,13 +129,13 @@ public function testCheckContactUsFormWhenCaptchaIsRequiredAndValid()
116
129
->method ('resolve ' )
117
130
->with ($ request , $ formId )
118
131
->willReturn ($ captchaValue );
119
- $ this ->helperMock ->expects ( $ this -> any () )
120
- ->method ( ' getCaptcha ' )
121
- ->with ( $ formId )-> willReturn ($ this ->captchaMock );
132
+ $ this ->helperMock ->method ( ' getCaptcha ' )
133
+ ->with ( $ formId )
134
+ ->willReturn ($ this ->captchaMock );
122
135
$ this ->sessionMock ->expects ($ this ->never ())->method ('addErrorMessage ' );
123
136
124
137
$ this ->checkContactUsFormObserver ->execute (
125
- new \ Magento \ Framework \ Event \ Observer (['controller_action ' => $ controller ])
138
+ new Observer (['controller_action ' => $ controller ])
126
139
);
127
140
}
128
141
@@ -135,11 +148,10 @@ public function testCheckContactUsFormRedirectsCustomerWithWarningMessageWhenCap
135
148
$ redirectUrl = 'http://magento.com/contacts/ ' ;
136
149
$ postData = ['name ' => 'Some Name ' ];
137
150
138
- $ request = $ this ->createMock (\ Magento \ Framework \ App \ Request \ Http::class);
151
+ $ request = $ this ->createMock (Http::class);
139
152
$ response = $ this ->createMock (\Magento \Framework \App \Response \Http::class);
140
- $ request ->expects ($ this ->any ())
141
- ->method ('getPost ' )
142
- ->with (\Magento \Captcha \Helper \Data::INPUT_NAME_FIELD_VALUE , null )
153
+ $ request ->method ('getPost ' )
154
+ ->with (Data::INPUT_NAME_FIELD_VALUE , null )
143
155
->willReturn ([$ formId => $ captchaValue ]);
144
156
$ request ->expects ($ this ->once ())
145
157
->method ('getPostValue ' )
@@ -150,10 +162,10 @@ public function testCheckContactUsFormRedirectsCustomerWithWarningMessageWhenCap
150
162
->with ($ response , $ redirectRoutePath , [])
151
163
->willReturn ($ redirectUrl );
152
164
153
- $ controller = $ this ->createMock (\ Magento \ Framework \ App \ Action \ Action::class);
154
- $ controller ->expects ( $ this -> any ())-> method ('getRequest ' )->willReturn ($ request );
155
- $ controller ->expects ( $ this -> any ())-> method ('getResponse ' )->willReturn ($ response );
156
- $ this ->captchaMock ->expects ( $ this -> any ())-> method ('isRequired ' )->willReturn (true );
165
+ $ controller = $ this ->createMock (Action::class);
166
+ $ controller ->method ('getRequest ' )->willReturn ($ request );
167
+ $ controller ->method ('getResponse ' )->willReturn ($ response );
168
+ $ this ->captchaMock ->method ('isRequired ' )->willReturn (true );
157
169
$ this ->captchaMock ->expects ($ this ->once ())
158
170
->method ('isCorrect ' )
159
171
->with ($ captchaValue )
@@ -162,32 +174,32 @@ public function testCheckContactUsFormRedirectsCustomerWithWarningMessageWhenCap
162
174
->method ('resolve ' )
163
175
->with ($ request , $ formId )
164
176
->willReturn ($ captchaValue );
165
- $ this ->helperMock ->expects ($ this ->any ())
166
- ->method ('getCaptcha ' )
177
+ $ this ->helperMock ->method ('getCaptcha ' )
167
178
->with ($ formId )
168
179
->willReturn ($ this ->captchaMock );
169
- $ this ->messageManagerMock ->expects ($ this ->once ())->method ('addErrorMessage ' )->with ($ warningMessage );
180
+ $ this ->messageManagerMock ->expects ($ this ->once ())
181
+ ->method ('addErrorMessage ' )
182
+ ->with ($ warningMessage );
170
183
$ this ->actionFlagMock ->expects ($ this ->once ())
171
184
->method ('set ' )
172
- ->with ('' , \ Magento \ Framework \ App \ Action \ Action::FLAG_NO_DISPATCH , true );
185
+ ->with ('' , Action::FLAG_NO_DISPATCH , true );
173
186
$ this ->dataPersistorMock ->expects ($ this ->once ())
174
187
->method ('set ' )
175
188
->with ($ formId , $ postData );
176
189
177
190
$ this ->checkContactUsFormObserver ->execute (
178
- new \ Magento \ Framework \ Event \ Observer (['controller_action ' => $ controller ])
191
+ new Observer (['controller_action ' => $ controller ])
179
192
);
180
193
}
181
194
182
195
public function testCheckContactUsFormDoesNotCheckCaptchaWhenItIsNotRequired ()
183
196
{
184
- $ this ->helperMock ->expects ($ this ->any ())
185
- ->method ('getCaptcha ' )
197
+ $ this ->helperMock ->method ('getCaptcha ' )
186
198
->with ('contact_us ' )
187
199
->willReturn ($ this ->captchaMock );
188
- $ this ->captchaMock ->expects ( $ this -> any ())-> method ('isRequired ' )->willReturn (false );
200
+ $ this ->captchaMock ->method ('isRequired ' )->willReturn (false );
189
201
$ this ->captchaMock ->expects ($ this ->never ())->method ('isCorrect ' );
190
202
191
- $ this ->checkContactUsFormObserver ->execute (new \ Magento \ Framework \ Event \ Observer ());
203
+ $ this ->checkContactUsFormObserver ->execute (new Observer ());
192
204
}
193
205
}
0 commit comments