Skip to content

Commit cbb0206

Browse files
committed
MAGETWO-96522: Fix action behavior
1 parent 86b1962 commit cbb0206

File tree

2 files changed

+126
-14
lines changed

2 files changed

+126
-14
lines changed

app/code/Magento/Customer/Test/Unit/Controller/Account/ForgotPasswordPostTest.php

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ class ForgotPasswordPostTest extends \PHPUnit_Framework_TestCase
7373
*/
7474
private $formKeyValidatorMock;
7575

76+
/**
77+
* @inheritdoc
78+
*/
7679
protected function setUp()
7780
{
7881
$this->prepareContext();
@@ -92,12 +95,6 @@ protected function setUp()
9295
->setMethods(['validate'])
9396
->getMock();
9497

95-
$this->request->expects($this->once())->method('isPost')->willReturn(true);
96-
$this->formKeyValidatorMock->expects($this->once())
97-
->method('validate')
98-
->with($this->request)
99-
->willReturn(true);
100-
10198
$this->controller = new ForgotPasswordPost(
10299
$this->context,
103100
$this->session,
@@ -107,8 +104,12 @@ protected function setUp()
107104
);
108105
}
109106

107+
/**
108+
* @return void
109+
*/
110110
public function testExecuteEmptyEmail()
111111
{
112+
$this->validateRequest();
112113
$this->request->expects($this->once())
113114
->method('getPost')
114115
->with('email')
@@ -127,10 +128,14 @@ public function testExecuteEmptyEmail()
127128
$this->assertSame($this->resultRedirect, $this->controller->execute());
128129
}
129130

131+
/**
132+
* @return void
133+
*/
130134
public function testExecute()
131135
{
132136
$email = 'user1@example.com';
133137

138+
$this->validateRequest();
134139
$this->request->expects($this->once())
135140
->method('getPost')
136141
->with('email')
@@ -163,10 +168,14 @@ public function testExecute()
163168
$this->controller->execute();
164169
}
165170

171+
/**
172+
* @return void
173+
*/
166174
public function testExecuteNoSuchEntityException()
167175
{
168176
$email = 'user1@example.com';
169177

178+
$this->validateRequest();
170179
$this->request->expects($this->once())
171180
->method('getPost')
172181
->with('email')
@@ -199,11 +208,15 @@ public function testExecuteNoSuchEntityException()
199208
$this->controller->execute();
200209
}
201210

211+
/**
212+
* @return void
213+
*/
202214
public function testExecuteException()
203215
{
204216
$email = 'user1@example.com';
205217
$exception = new \Exception(__('Exception'));
206218

219+
$this->validateRequest();
207220
$this->request->expects($this->once())
208221
->method('getPost')
209222
->with('email')
@@ -227,6 +240,38 @@ public function testExecuteException()
227240
$this->controller->execute();
228241
}
229242

243+
/**
244+
* @return void
245+
* @expectedException \Magento\Framework\Exception\NotFoundException
246+
* @expectedExceptionMessage Page not found.
247+
*/
248+
public function testExecuteWithNonPostRequest()
249+
{
250+
$this->request->expects($this->once())->method('isPost')->willReturn(false);
251+
252+
$this->controller->execute();
253+
}
254+
255+
/**
256+
* @return void
257+
*/
258+
public function testExecuteWithInvalidFormKey()
259+
{
260+
$this->request->expects($this->once())->method('isPost')->willReturn(true);
261+
$this->formKeyValidatorMock->expects($this->once())
262+
->method('validate')
263+
->with($this->request)
264+
->willReturn(false);
265+
$this->resultRedirect->expects($this->once())->method('setPath')->with('*/*/forgotpassword')->willReturnSelf();
266+
267+
$this->controller->execute();
268+
}
269+
270+
/**
271+
* Prepare action context.
272+
*
273+
* @return void
274+
*/
230275
protected function prepareContext()
231276
{
232277
$this->resultRedirect = $this->getMockBuilder(\Magento\Framework\Controller\Result\Redirect::class)
@@ -267,4 +312,18 @@ protected function prepareContext()
267312
->method('getMessageManager')
268313
->willReturn($this->messageManager);
269314
}
315+
316+
/**
317+
* Validate request.
318+
*
319+
* @return void
320+
*/
321+
private function validateRequest()
322+
{
323+
$this->request->expects($this->once())->method('isPost')->willReturn(true);
324+
$this->formKeyValidatorMock->expects($this->once())
325+
->method('validate')
326+
->with($this->request)
327+
->willReturn(true);
328+
}
270329
}

app/code/Magento/Sales/Test/Unit/Controller/Guest/ViewTest.php

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Sales\Test\Unit\Controller\Guest;
77

8+
use Magento\Framework\Controller\Result\RedirectFactory;
89
use Magento\Framework\Data\Form\FormKey\Validator;
910
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1011

@@ -56,7 +57,12 @@ class ViewTest extends \PHPUnit_Framework_TestCase
5657
private $formKeyValidatorMock;
5758

5859
/**
59-
* @return void
60+
* @var RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
61+
*/
62+
private $resultRedirectFactoryMock;
63+
64+
/**
65+
* @inheritdoc
6066
*/
6167
protected function setUp()
6268
{
@@ -80,18 +86,16 @@ protected function setUp()
8086
->disableOriginalConstructor()
8187
->setMethods(['validate'])
8288
->getMock();
83-
84-
$this->requestMock->expects($this->once())->method('isPost')->willReturn(true);
85-
$this->formKeyValidatorMock->expects($this->once())
86-
->method('validate')
87-
->with($this->requestMock)
88-
->willReturn(true);
89+
$this->resultRedirectFactoryMock = $this->getMockBuilder(RedirectFactory::class)
90+
->disableOriginalConstructor()
91+
->getMock();
8992

9093
$this->objectManagerHelper = new ObjectManagerHelper($this);
9194
$this->context = $this->objectManagerHelper->getObject(
9295
\Magento\Framework\App\Action\Context::class,
9396
[
94-
'request' => $this->requestMock
97+
'request' => $this->requestMock,
98+
'resultRedirectFactory' => $this->resultRedirectFactoryMock,
9599
]
96100
);
97101
$this->viewController = $this->objectManagerHelper->getObject(
@@ -110,6 +114,7 @@ protected function setUp()
110114
*/
111115
public function testExecuteOrderLoaded()
112116
{
117+
$this->validateRequest();
113118
$this->guestHelperMock->expects($this->once())
114119
->method('loadValidOrder')
115120
->with($this->requestMock)
@@ -129,11 +134,59 @@ public function testExecuteOrderLoaded()
129134
*/
130135
public function testExecuteOrderNotFound()
131136
{
137+
$this->validateRequest();
132138
$this->guestHelperMock->expects($this->once())
133139
->method('loadValidOrder')
134140
->with($this->requestMock)
135141
->willReturn($this->resultRedirectMock);
136142

137143
$this->assertSame($this->resultRedirectMock, $this->viewController->execute());
138144
}
145+
146+
/**
147+
* @return void
148+
* @expectedException \Magento\Framework\Exception\NotFoundException
149+
* @expectedExceptionMessage Page not found.
150+
*/
151+
public function testExecuteWithNonPostRequest()
152+
{
153+
$this->requestMock->expects($this->once())->method('isPost')->willReturn(false);
154+
155+
$this->viewController->execute();
156+
}
157+
158+
/**
159+
* @return void
160+
*/
161+
public function testExecuteWithInvalidFormKey()
162+
{
163+
$this->resultRedirectFactoryMock->expects($this->once())
164+
->method('create')
165+
->willReturn($this->resultRedirectMock);
166+
$this->requestMock->expects($this->once())->method('isPost')->willReturn(true);
167+
$this->formKeyValidatorMock->expects($this->once())
168+
->method('validate')
169+
->with($this->requestMock)
170+
->willReturn(false);
171+
$this->resultRedirectMock->expects($this->once())
172+
->method('setPath')
173+
->with('*/*/form/')
174+
->willReturnSelf();
175+
176+
$this->viewController->execute();
177+
}
178+
179+
/**
180+
* Validate request.
181+
*
182+
* @return void
183+
*/
184+
private function validateRequest()
185+
{
186+
$this->requestMock->expects($this->once())->method('isPost')->willReturn(true);
187+
$this->formKeyValidatorMock->expects($this->once())
188+
->method('validate')
189+
->with($this->requestMock)
190+
->willReturn(true);
191+
}
139192
}

0 commit comments

Comments
 (0)