Skip to content

Commit ceca1ea

Browse files
author
Yushkin, Dmytro
committed
MAGETWO-39685: Order number is absent on success page for PayPal Advanced Checkout
- refactor - add unit test
1 parent debc97e commit ceca1ea

File tree

2 files changed

+282
-13
lines changed

2 files changed

+282
-13
lines changed

app/code/Magento/Paypal/Controller/Payflow/ReturnUrl.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,19 @@
66
*/
77
namespace Magento\Paypal\Controller\Payflow;
88

9-
class ReturnUrl extends \Magento\Paypal\Controller\Payflow
9+
use Magento\Paypal\Controller\Payflow;
10+
use Magento\Sales\Model\Order;
11+
12+
class ReturnUrl extends Payflow
1013
{
14+
/**
15+
* @var array of allowed order states on frontend
16+
*/
17+
protected $allowedOrderStates = [
18+
Order::STATE_PROCESSING,
19+
Order::STATE_COMPLETE,
20+
];
21+
1122
/**
1223
* When a customer return to website from payflow gateway.
1324
*
@@ -16,23 +27,19 @@ class ReturnUrl extends \Magento\Paypal\Controller\Payflow
1627
public function execute()
1728
{
1829
$this->_view->loadLayout(false);
30+
/** @var \Magento\Checkout\Block\Onepage\Success $redirectBlock */
1931
$redirectBlock = $this->_view->getLayout()->getBlock($this->_redirectBlockName);
2032

2133
if ($this->_checkoutSession->getLastRealOrderId()) {
34+
/** @var \Magento\Sales\Model\Order $order */
2235
$order = $this->_orderFactory->create()->loadByIncrementId($this->_checkoutSession->getLastRealOrderId());
2336

24-
if ($order && $order->getIncrementId() == $this->_checkoutSession->getLastRealOrderId()) {
25-
$allowedOrderStates = [
26-
\Magento\Sales\Model\Order::STATE_PROCESSING,
27-
\Magento\Sales\Model\Order::STATE_COMPLETE,
28-
];
29-
if (in_array($order->getState(), $allowedOrderStates)) {
30-
$redirectBlock->setGotoSuccessPage(true);
31-
} else {
32-
$gotoSection = $this->_cancelPayment(strval($this->getRequest()->getParam('RESPMSG')));
33-
$redirectBlock->setGotoSection($gotoSection);
34-
$redirectBlock->setErrorMsg(__('Your payment has been declined. Please try again.'));
35-
}
37+
if (in_array($order->getState(), $this->allowedOrderStates)) {
38+
$redirectBlock->setData('goto_success_page', true);
39+
} else {
40+
$gotoSection = $this->_cancelPayment(strval($this->getRequest()->getParam('RESPMSG')));
41+
$redirectBlock->setData('goto_section', $gotoSection);
42+
$redirectBlock->setData('error_msg', __('Your payment has been declined. Please try again.'));
3643
}
3744
}
3845

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Paypal\Test\Unit\Controller\Payflow;
8+
9+
use Magento\Checkout\Block\Onepage\Success;
10+
use Magento\Checkout\Model\Session;
11+
use Magento\Framework\App\Http;
12+
use Magento\Framework\App\View;
13+
use Magento\Framework\View\LayoutInterface;
14+
use Magento\Paypal\Controller\Payflow\ReturnUrl;
15+
use Magento\Paypal\Helper\Checkout;
16+
use Magento\Sales\Model\Order;
17+
use Psr\Log\LoggerInterface;
18+
19+
class ReturnUrlTest extends \PHPUnit_Framework_TestCase
20+
{
21+
/**
22+
* @var ReturnUrl
23+
*/
24+
protected $returnUrl;
25+
26+
/**
27+
* @var \Magento\Framework\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
28+
*/
29+
protected $contextMock;
30+
31+
/**
32+
* @var View|\PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
protected $viewMock;
35+
36+
/**
37+
* @var Http|\PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
protected $requestMock;
40+
41+
/**
42+
* @var Session|\PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
protected $checkoutSessionMock;
45+
46+
/**
47+
* @var \Magento\Sales\Model\OrderFactory|\PHPUnit_Framework_MockObject_MockObject
48+
*/
49+
protected $orderFactoryMock;
50+
51+
/**
52+
* @var \Magento\Paypal\Model\PayflowlinkFactory|\PHPUnit_Framework_MockObject_MockObject
53+
*/
54+
protected $payflowlinkFactoryMock;
55+
56+
/**
57+
* @var Checkout|\PHPUnit_Framework_MockObject_MockObject
58+
*/
59+
protected $helperCheckoutMock;
60+
61+
/**
62+
* @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
63+
*/
64+
protected $loggerMock;
65+
66+
/**
67+
* @var Success|\PHPUnit_Framework_MockObject_MockObject
68+
*/
69+
protected $blockMock;
70+
71+
/**
72+
* @var LayoutInterface|\PHPUnit_Framework_MockObject_MockObject
73+
*/
74+
protected $layoutMock;
75+
76+
/**
77+
* @var Order|\PHPUnit_Framework_MockObject_MockObject
78+
*/
79+
protected $orderMock;
80+
81+
protected function setUp()
82+
{
83+
$this->contextMock = $this->getMock('Magento\Framework\App\Action\Context', [], [], '', false);
84+
$this->viewMock = $this->getMock('Magento\Framework\App\ViewInterface');
85+
$this->requestMock = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false);
86+
$this->layoutMock = $this->getMock('Magento\Framework\View\LayoutInterface');
87+
$this->blockMock = $this
88+
->getMockBuilder('\Magento\Checkout\Block\Onepage\Success')
89+
->disableOriginalConstructor()
90+
->getMock();
91+
$this->orderFactoryMock = $this->getMock('\Magento\Sales\Model\OrderFactory', ['create'], [], '', false);
92+
$this->payflowlinkFactoryMock = $this->getMock('\Magento\Paypal\Model\PayflowlinkFactory', [], [], '', false);
93+
$this->helperCheckoutMock = $this->getMock('\Magento\Paypal\Helper\Checkout', [], [], '', false);
94+
$this->loggerMock = $this->getMockForAbstractClass('\Psr\Log\LoggerInterface');
95+
$this->orderMock = $this
96+
->getMockBuilder('\Magento\Sales\Model\Order')
97+
->disableOriginalConstructor()
98+
->getMock();
99+
$this->checkoutSessionMock = $this
100+
->getMockBuilder('\Magento\Checkout\Model\Session')
101+
->setMethods(['getLastRealOrderId', 'getLastRealOrder', 'restoreQuote'])
102+
->disableOriginalConstructor()
103+
->getMock();
104+
105+
$this->contextMock->expects($this->once())->method('getView')->will($this->returnValue($this->viewMock));
106+
$this->contextMock->expects($this->any())->method('getRequest')->will($this->returnValue($this->requestMock));
107+
108+
$this->returnUrl = new ReturnUrl(
109+
$this->contextMock,
110+
$this->checkoutSessionMock,
111+
$this->orderFactoryMock,
112+
$this->payflowlinkFactoryMock,
113+
$this->helperCheckoutMock,
114+
$this->loggerMock
115+
);
116+
}
117+
118+
/**
119+
* @return array
120+
*/
121+
public function testAllowedOrderStateDataProvider()
122+
{
123+
return [
124+
[Order::STATE_PROCESSING],
125+
[Order::STATE_COMPLETE],
126+
];
127+
}
128+
129+
/**
130+
* @return array
131+
*/
132+
public function testNotAllowedOrderStateDataProvider()
133+
{
134+
return [
135+
[Order::STATE_NEW, false, ''],
136+
[Order::STATE_NEW, true, 'paymentMethod'],
137+
[Order::STATE_PENDING_PAYMENT, false, ''],
138+
[Order::STATE_PENDING_PAYMENT, true, 'paymentMethod'],
139+
[Order::STATE_CLOSED, false, ''],
140+
[Order::STATE_CLOSED, true, 'paymentMethod'],
141+
[Order::STATE_CANCELED, false, ''],
142+
[Order::STATE_CANCELED, true, 'paymentMethod'],
143+
[Order::STATE_HOLDED, false, ''],
144+
[Order::STATE_HOLDED, true, 'paymentMethod'],
145+
[Order::STATE_PAYMENT_REVIEW, false, ''],
146+
[Order::STATE_PAYMENT_REVIEW, true, 'paymentMethod'],
147+
];
148+
}
149+
150+
/**
151+
* @param $state
152+
* @dataProvider testAllowedOrderStateDataProvider
153+
*/
154+
public function testExecuteAllowedOrderState($state)
155+
{
156+
$lastRealOrderId = '000000001';
157+
158+
$this->viewMock
159+
->expects($this->once())
160+
->method('getLayout')
161+
->will($this->returnValue($this->layoutMock));
162+
163+
$this->layoutMock
164+
->expects($this->once())
165+
->method('getBlock')
166+
->will($this->returnValue($this->blockMock));
167+
168+
$this->checkoutSessionMock
169+
->expects($this->exactly(2))
170+
->method('getLastRealOrderId')
171+
->will($this->returnValue($lastRealOrderId));
172+
173+
$this->orderFactoryMock
174+
->expects($this->once())
175+
->method('create')
176+
->will($this->returnValue($this->orderMock));
177+
178+
$this->orderMock
179+
->expects($this->once())
180+
->method('loadByIncrementId')
181+
->with($lastRealOrderId)
182+
->will($this->returnSelf());
183+
184+
$this->orderMock
185+
->expects($this->once())
186+
->method('getState')
187+
->will($this->returnValue($state));
188+
189+
$this->blockMock
190+
->expects($this->once())
191+
->method('setData')
192+
->with('goto_success_page', true)
193+
->will($this->returnSelf());
194+
195+
$this->returnUrl->execute();
196+
}
197+
198+
/**
199+
* @param $state
200+
* @param $restoreQuote
201+
* @param $expectedGotoSection
202+
* @dataProvider testNotAllowedOrderStateDataProvider
203+
*/
204+
public function testExecuteNotAllowedOrderState($state, $restoreQuote, $expectedGotoSection)
205+
{
206+
$lastRealOrderId = '000000001';
207+
$this->viewMock
208+
->expects($this->once())
209+
->method('getLayout')
210+
->will($this->returnValue($this->layoutMock));
211+
212+
$this->layoutMock
213+
->expects($this->once())
214+
->method('getBlock')
215+
->will($this->returnValue($this->blockMock));
216+
217+
$this->checkoutSessionMock
218+
->expects($this->any())
219+
->method('getLastRealOrderId')
220+
->will($this->returnValue($lastRealOrderId));
221+
222+
$this->checkoutSessionMock
223+
->expects($this->any())
224+
->method('getLastRealOrder')
225+
->will($this->returnValue($this->orderMock));
226+
227+
$this->checkoutSessionMock
228+
->expects($this->any())
229+
->method('restoreQuote')
230+
->will($this->returnValue($restoreQuote));
231+
232+
$this->orderFactoryMock
233+
->expects($this->any())
234+
->method('create')
235+
->will($this->returnValue($this->orderMock));
236+
237+
$this->orderMock
238+
->expects($this->once())
239+
->method('loadByIncrementId')
240+
->with($lastRealOrderId)
241+
->will($this->returnSelf());
242+
243+
$this->orderMock
244+
->expects($this->once())
245+
->method('getState')
246+
->will($this->returnValue($state));
247+
248+
$this->blockMock
249+
->expects($this->at(0))
250+
->method('setData')
251+
->with('goto_section', $expectedGotoSection)
252+
->will($this->returnSelf());
253+
254+
$this->blockMock
255+
->expects($this->at(1))
256+
->method('setData')
257+
->with('error_msg', __('Your payment has been declined. Please try again.'))
258+
->will($this->returnSelf());
259+
260+
$this->returnUrl->execute();
261+
}
262+
}

0 commit comments

Comments
 (0)