Skip to content

Commit d689d40

Browse files
author
Yushkin, Dmytro
committed
Merge branch 'MAGETWO-45303' into MAGETWO-45269
2 parents 600e79a + 6589f27 commit d689d40

File tree

5 files changed

+102
-170
lines changed

5 files changed

+102
-170
lines changed

app/code/Magento/Paypal/Controller/Hostedpro/Cancel.php

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,38 @@
66
*/
77
namespace Magento\Paypal\Controller\Hostedpro;
88

9+
use Magento\Framework\App\Action\Context;
10+
use Magento\Paypal\Helper\Checkout;
11+
912
class Cancel extends \Magento\Framework\App\Action\Action
1013
{
1114
/**
12-
* @var \Magento\Checkout\Model\Session
15+
* @var Checkout
1316
*/
14-
protected $_session;
17+
private $checkoutHelper;
1518

1619
/**
17-
* @param \Magento\Framework\App\Action\Context $context
18-
* @param \Magento\Checkout\Model\Session $session
20+
* @param Context $context
21+
* @param Checkout $checkoutHelper
1922
*/
2023
public function __construct(
21-
\Magento\Framework\App\Action\Context $context,
22-
\Magento\Checkout\Model\Session $session
24+
Context $context,
25+
Checkout $checkoutHelper
2326
) {
2427
parent::__construct($context);
25-
$this->_session = $session;
28+
$this->checkoutHelper = $checkoutHelper;
2629
}
2730

2831
/**
29-
* Cancel order, return quote to customer
30-
*
31-
* @param string $errorMsg
32-
* @return false|string
33-
*/
34-
protected function _cancelPayment($errorMsg = '')
35-
{
36-
$gotoSection = false;
37-
$helper = $this->_objectManager->get('Magento\Paypal\Helper\Checkout');
38-
$helper->cancelCurrentOrder($errorMsg);
39-
if ($this->_session->restoreQuote()) {
40-
$gotoSection = 'paymentMethod';
41-
}
42-
43-
return $gotoSection;
44-
}
45-
46-
/**
47-
* When a customer cancel payment from gateway.
32+
* Customer canceled payment on gateway side.
4833
*
4934
* @return void
5035
*/
5136
public function execute()
5237
{
53-
$this->_view->loadLayout(false);
54-
$gotoSection = $this->_cancelPayment();
55-
$redirectBlock = $this->_view->getLayout()->getBlock('hosted.pro.iframe');
56-
$redirectBlock->setGotoSection($gotoSection);
57-
//TODO: clarify return logic whether customer will be returned in iframe or in parent window
58-
$this->_view->renderLayout();
38+
$this->checkoutHelper->cancelCurrentOrder('');
39+
$this->checkoutHelper->restoreQuote();
40+
41+
$this->_redirect('checkout', ['_fragment' => 'payment']);
5942
}
6043
}

app/code/Magento/Paypal/Helper/Checkout.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@
44
* See COPYING.txt for license details.
55
*/
66
namespace Magento\Paypal\Helper;
7+
use Magento\Sales\Model\Order;
78

89
/**
910
* Checkout workflow helper
1011
*/
1112
class Checkout
1213
{
1314
/**
14-
* @var \Magento\Checkout\Model\SessionFactory
15+
* @var \Magento\Checkout\Model\Session
1516
*/
16-
protected $_session;
17+
protected $session;
1718

1819
/**
1920
* @param \Magento\Checkout\Model\Session $session
2021
*/
2122
public function __construct(
2223
\Magento\Checkout\Model\Session $session
2324
) {
24-
$this->_session = $session;
25+
$this->session = $session;
2526
}
2627

2728
/**
@@ -32,11 +33,21 @@ public function __construct(
3233
*/
3334
public function cancelCurrentOrder($comment)
3435
{
35-
$order = $this->_session->getLastRealOrder();
36-
if ($order->getId() && $order->getState() != \Magento\Sales\Model\Order::STATE_CANCELED) {
36+
$order = $this->session->getLastRealOrder();
37+
if ($order->getId() && $order->getState() != Order::STATE_CANCELED) {
3738
$order->registerCancellation($comment)->save();
3839
return true;
3940
}
4041
return false;
4142
}
43+
44+
/**
45+
* Restores quote
46+
*
47+
* @return bool
48+
*/
49+
public function restoreQuote()
50+
{
51+
return $this->session->restoreQuote();
52+
}
4253
}

app/code/Magento/Paypal/Test/Unit/Helper/CheckoutTest.php

Lines changed: 71 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -9,105 +9,95 @@
99
*/
1010
namespace Magento\Paypal\Test\Unit\Helper;
1111

12+
use Magento\Checkout\Model\Session;
13+
use Magento\Paypal\Helper\Checkout;
14+
use Magento\Sales\Model\Order;
15+
1216
class CheckoutTest extends \PHPUnit_Framework_TestCase
1317
{
1418
/**
15-
* @var \Magento\Checkout\Model\Session|\PHPUnit_Framework_MockObject_MockObject
19+
* @var Session|\PHPUnit_Framework_MockObject_MockObject
1620
*/
17-
protected $_session;
21+
private $session;
1822

1923
/**
20-
* @var \Magento\Quote\Model\QuoteFactory|\PHPUnit_Framework_MockObject_MockObject
24+
* @var Checkout
2125
*/
22-
protected $_quoteFactory;
26+
private $checkout;
2327

24-
/**
25-
* @var \Magento\Paypal\Helper\Checkout
26-
*/
27-
protected $_checkout;
28-
29-
/**
30-
* Sets up the fixture, for example, opens a network connection.
31-
* This method is called before a test is executed.
32-
*/
3328
protected function setUp()
3429
{
35-
$this->_session = $this->getMockBuilder(
36-
'Magento\Checkout\Model\Session'
37-
)->disableOriginalConstructor()->setMethods(
38-
['getLastRealOrder', 'replaceQuote', 'unsLastRealOrderId', '__wakeup']
39-
)->getMock();
40-
$this->_quoteFactory = $this->getMockBuilder(
41-
'Magento\Quote\Model\QuoteFactory'
42-
)->disableOriginalConstructor()->setMethods(
43-
['create', '__wakeup']
44-
)->getMock();
45-
46-
$this->_checkout = new \Magento\Paypal\Helper\Checkout($this->_session, $this->_quoteFactory);
30+
$this->session = $this->getMockBuilder(Session::class)
31+
->disableOriginalConstructor()
32+
->getMock();
33+
34+
$this->checkout = new Checkout($this->session);
4735
}
4836

49-
/**
50-
* Get order mock
51-
*
52-
* @param bool $hasOrderId
53-
* @param array $mockMethods
54-
* @return \Magento\Sales\Model\Order|\PHPUnit_Framework_MockObject_MockObject
55-
*/
56-
protected function _getOrderMock($hasOrderId, $mockMethods = [])
37+
public function testCancelCurrentOrder()
5738
{
58-
$order = $this->getMockBuilder(
59-
'Magento\Sales\Model\Order'
60-
)->disableOriginalConstructor()->setMethods(
61-
array_merge(['getId', '__wakeup'], $mockMethods)
62-
)->getMock();
63-
$order->expects($this->once())->method('getId')->will($this->returnValue($hasOrderId ? 'order id' : null));
64-
return $order;
39+
$id = 1;
40+
$state = Order::STATE_PENDING_PAYMENT;
41+
$comment = 'Bla Bla';
42+
43+
$order = $this->getMockBuilder(Order::class)
44+
->disableOriginalConstructor()
45+
->getMock();
46+
47+
$this->session->expects(static::once())
48+
->method('getLastRealOrder')
49+
->willReturn($order);
50+
$order->expects(static::once())
51+
->method('getId')
52+
->willReturn($id);
53+
$order->expects(static::once())
54+
->method('getState')
55+
->willReturn($state);
56+
$order->expects(static::once())
57+
->method('registerCancellation')
58+
->with($comment)
59+
->willReturnSelf();
60+
$order->expects(static::once())
61+
->method('save');
62+
63+
static::assertTrue($this->checkout->cancelCurrentOrder($comment));
6564
}
6665

67-
/**
68-
* @param bool $hasOrderId
69-
* @param bool $isOrderCancelled
70-
* @param bool $expectedResult
71-
* @dataProvider cancelCurrentOrderDataProvider
72-
*/
73-
public function testCancelCurrentOrder($hasOrderId, $isOrderCancelled, $expectedResult)
66+
public function testCancelCurrentOrderWhichIsCancelled()
7467
{
75-
$comment = 'Some test comment';
76-
$order = $this->_getOrderMock($hasOrderId, ['registerCancellation', 'save']);
77-
$order->setData(
78-
'state',
79-
$isOrderCancelled ? \Magento\Sales\Model\Order::STATE_CANCELED : 'some another state'
80-
);
81-
if ($expectedResult) {
82-
$order->expects(
83-
$this->once()
84-
)->method(
85-
'registerCancellation'
86-
)->with(
87-
$this->equalTo($comment)
88-
)->will(
89-
$this->returnSelf()
90-
);
91-
$order->expects($this->once())->method('save');
92-
} else {
93-
$order->expects($this->never())->method('registerCancellation');
94-
$order->expects($this->never())->method('save');
95-
}
96-
97-
$this->_session->expects($this->any())->method('getLastRealOrder')->will($this->returnValue($order));
98-
$this->assertEquals($expectedResult, $this->_checkout->cancelCurrentOrder($comment));
68+
$id = 1;
69+
$state = Order::STATE_CANCELED;
70+
$comment = 'Bla Bla';
71+
72+
$order = $this->getMockBuilder(Order::class)
73+
->disableOriginalConstructor()
74+
->getMock();
75+
76+
$this->session->expects(static::once())
77+
->method('getLastRealOrder')
78+
->willReturn($order);
79+
$order->expects(static::once())
80+
->method('getId')
81+
->willReturn($id);
82+
$order->expects(static::once())
83+
->method('getState')
84+
->willReturn($state);
85+
$order->expects(static::never())
86+
->method('registerCancellation')
87+
->with($comment)
88+
->willReturnSelf();
89+
$order->expects(static::never())
90+
->method('save');
91+
92+
static::assertFalse($this->checkout->cancelCurrentOrder($comment));
9993
}
10094

101-
/**
102-
* @return array
103-
*/
104-
public function cancelCurrentOrderDataProvider()
95+
public function testRestoreQuote()
10596
{
106-
return [
107-
[true, false, true],
108-
[true, true, false],
109-
[false, true, false],
110-
[false, false, false]
111-
];
97+
$this->session->expects(static::once())
98+
->method('restoreQuote')
99+
->willReturn(true);
100+
101+
static::assertTrue($this->checkout->restoreQuote());
112102
}
113103
}

app/code/Magento/Paypal/view/frontend/layout/paypal_hostedpro_cancel.xml

Lines changed: 0 additions & 12 deletions
This file was deleted.

app/code/Magento/Paypal/view/frontend/templates/hss/redirect.phtml

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)