Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReturnUrlTest.php
Go to the documentation of this file.
1 <?php
7 
18 use Magento\Paypal\Controller\Payflowadvanced\ReturnUrl as PayflowadvancedReturnUrl;
23 use Magento\Sales\Model\OrderFactory;
24 use PHPUnit_Framework_MockObject_MockObject as MockObject;
25 
31 class ReturnUrlTest extends \PHPUnit\Framework\TestCase
32 {
33  const LAST_REAL_ORDER_ID = '000000001';
34 
38  private $returnUrl;
39 
43  private $context;
44 
48  private $view;
49 
53  private $request;
54 
58  private $checkoutSession;
59 
63  private $orderFactory;
64 
68  private $checkoutHelper;
69 
73  private $block;
74 
78  private $layout;
79 
83  private $order;
84 
88  private $payment;
89 
93  private $objectManager;
94 
98  private $paymentFailures;
99 
103  private $eventManagerMock;
104 
108  protected function setUp()
109  {
110  $this->objectManager = new ObjectManager($this);
111 
112  $this->context = $this->getMockBuilder(Context::class)
113  ->disableOriginalConstructor()
114  ->getMock();
115 
116  $this->view = $this->getMockBuilder(ViewInterface::class)
117  ->getMock();
118 
119  $this->request = $this->getMockBuilder(Http::class)
120  ->disableOriginalConstructor()
121  ->setMethods(['getParam'])
122  ->getMock();
123 
124  $this->layout = $this->getMockBuilder(LayoutInterface::class)
125  ->getMock();
126 
127  $this->block = $this->getMockBuilder(Success::class)
128  ->disableOriginalConstructor()
129  ->getMock();
130 
131  $this->orderFactory = $this->getMockBuilder(OrderFactory::class)
132  ->disableOriginalConstructor()
133  ->setMethods(['create'])
134  ->getMock();
135 
136  $this->checkoutHelper = $this->getMockBuilder(Checkout::class)
137  ->disableOriginalConstructor()
138  ->getMock();
139 
140  $this->order = $this->getMockBuilder(Order::class)
141  ->disableOriginalConstructor()
142  ->getMock();
143 
144  $this->payment = $this->getMockBuilder(Payment::class)
145  ->disableOriginalConstructor()
146  ->getMock();
147 
148  $this->checkoutSession = $this->getMockBuilder(Session::class)
149  ->disableOriginalConstructor()
150  ->setMethods(['getLastRealOrderId', 'getLastRealOrder', 'restoreQuote'])
151  ->getMock();
152 
153  $this->quote = $this->getMockBuilder(CartInterface::class)
154  ->disableOriginalConstructor()
155  ->getMock();
156 
157  $this->paymentFailures = $this->getMockBuilder(PaymentFailuresInterface::class)
158  ->disableOriginalConstructor()
159  ->getMock();
160 
161  $this->eventManagerMock = $this->getMockBuilder(ManagerInterface::class)
162  ->disableOriginalConstructor()
163  ->getMock();
164 
165  $this->context->method('getView')
166  ->willReturn($this->view);
167  $this->context->method('getRequest')
168  ->willReturn($this->request);
169  $this->context->method('getEventManager')
170  ->willReturn($this->eventManagerMock);
171 
172  $this->returnUrl = $this->objectManager->getObject(
173  ReturnUrl::class,
174  [
175  'context' => $this->context,
176  'checkoutSession' => $this->checkoutSession,
177  'orderFactory' => $this->orderFactory,
178  'checkoutHelper' => $this->checkoutHelper,
179  'paymentFailures' => $this->paymentFailures,
180  ]
181  );
182  }
183 
190  public function testExecuteAllowedOrderState($state)
191  {
192  $this->withLayout();
193  $this->withOrder(self::LAST_REAL_ORDER_ID, $state);
194 
195  $this->checkoutSession->method('getLastRealOrderId')
196  ->willReturn(self::LAST_REAL_ORDER_ID);
197 
198  $this->block->method('setData')
199  ->with('goto_success_page', true)
200  ->willReturnSelf();
201 
202  $this->eventManagerMock->expects($this->once())
203  ->method('dispatch')
204  ->with('paypal_checkout_success', $this->arrayHasKey('order'));
205 
206  $result = $this->returnUrl->execute();
207  $this->assertNull($result);
208  }
209 
216  {
217  return [
221  ];
222  }
223 
232  public function testExecuteNotAllowedOrderState($state, $restoreQuote, $expectedGotoSection)
233  {
234  $errMessage = 'Transaction has been canceled.';
235  $this->withLayout();
236  $this->withOrder(self::LAST_REAL_ORDER_ID, $state);
237  $this->withCheckoutSession(self::LAST_REAL_ORDER_ID, $restoreQuote);
238 
239  $this->request->method('getParam')
240  ->with('RESPMSG')
241  ->willReturn($errMessage);
242 
243  $this->payment->method('getMethod')
244  ->willReturn(Config::METHOD_PAYFLOWLINK);
245 
246  $this->checkoutHelper->method('cancelCurrentOrder')
247  ->with(self::equalTo($errMessage));
248 
249  $this->withBlockContent($expectedGotoSection, 'Your payment has been declined. Please try again.');
250 
251  $this->returnUrl->execute();
252  }
253 
260  {
261  return [
262  [Order::STATE_NEW, false, ''],
263  [Order::STATE_NEW, true, 'paymentMethod'],
264  [Order::STATE_PENDING_PAYMENT, false, ''],
265  [Order::STATE_PENDING_PAYMENT, true, 'paymentMethod'],
266  [Order::STATE_CLOSED, false, ''],
267  [Order::STATE_CLOSED, true, 'paymentMethod'],
268  [Order::STATE_CANCELED, false, ''],
269  [Order::STATE_CANCELED, true, 'paymentMethod'],
270  [Order::STATE_HOLDED, false, ''],
271  [Order::STATE_HOLDED, true, 'paymentMethod'],
272  ];
273  }
274 
279  {
280  $this->withLayout();
281  $this->withOrder(self::LAST_REAL_ORDER_ID, Order::STATE_NEW);
282 
283  $this->checkoutSession->method('getLastRealOrderId')
284  ->willReturn(self::LAST_REAL_ORDER_ID);
285 
286  $this->withBlockContent(false, 'Requested payment method does not match with order.');
287 
288  $this->payment->expects(self::once())
289  ->method('getMethod')
290  ->willReturn('something_else');
291 
292  $this->returnUrl->execute();
293  }
294 
300  public function testCheckXSSEscaped($errorMsg, $errorMsgEscaped)
301  {
302  $this->withLayout();
303  $this->withOrder(self::LAST_REAL_ORDER_ID, Order::STATE_NEW);
304  $this->withCheckoutSession(self::LAST_REAL_ORDER_ID, true);
305 
306  $this->request->method('getParam')
307  ->with('RESPMSG')
308  ->willReturn($errorMsg);
309 
310  $this->checkoutHelper->method('cancelCurrentOrder')
311  ->with(self::equalTo($errorMsgEscaped));
312 
313  $this->withBlockContent('paymentMethod', 'Your payment has been declined. Please try again.');
314 
315  $this->payment->method('getMethod')
316  ->willReturn(Config::METHOD_PAYFLOWLINK);
317 
318  $this->returnUrl->execute();
319  }
320 
326  public function checkXSSEscapedDataProvider()
327  {
328  return [
329  ['simple', 'simple'],
330  ['<script>alert(1)</script>', 'alert(1)'],
331  ['<div style="background-image:url(javascript:alert(1))">', '']
332  ];
333  }
334 
339  {
340  $this->withLayout();
341  $this->withOrder(self::LAST_REAL_ORDER_ID, Order::STATE_NEW);
342  $this->withCheckoutSession(self::LAST_REAL_ORDER_ID, true);
343 
344  $this->request->method('getParam')
345  ->with('RESPMSG')
346  ->willReturn('message');
347 
348  $this->withBlockContent('paymentMethod', 'Your payment has been declined. Please try again.');
349 
350  $this->payment->method('getMethod')
351  ->willReturn(Config::METHOD_PAYFLOWADVANCED);
352 
353  $returnUrl = $this->objectManager->getObject(PayflowadvancedReturnUrl::class, [
354  'context' => $this->context,
355  'checkoutSession' => $this->checkoutSession,
356  'orderFactory' => $this->orderFactory,
357  'checkoutHelper' => $this->checkoutHelper,
358  'paymentFailures' => $this->paymentFailures,
359  ]);
360 
361  $returnUrl->execute();
362  }
363 
371  private function withOrder($incrementId, $state)
372  {
373  $this->orderFactory->method('create')
374  ->willReturn($this->order);
375 
376  $this->order->method('loadByIncrementId')
377  ->with($incrementId)
378  ->willReturnSelf();
379 
380  $this->order->method('getIncrementId')
381  ->willReturn($incrementId);
382 
383  $this->order->method('getState')
384  ->willReturn($state);
385 
386  $this->order->method('getPayment')
387  ->willReturn($this->payment);
388  }
389 
395  private function withLayout()
396  {
397  $this->view->method('getLayout')
398  ->willReturn($this->layout);
399 
400  $this->layout->method('getBlock')
401  ->willReturn($this->block);
402  }
403 
410  private function withCheckoutSession($orderId, $restoreQuote)
411  {
412  $this->checkoutSession->method('getLastRealOrderId')
413  ->willReturn($orderId);
414 
415  $this->checkoutSession->method('getLastRealOrder')
416  ->willReturn($this->order);
417 
418  $this->checkoutSession->method('restoreQuote')
419  ->willReturn($restoreQuote);
420  }
421 
429  private function withBlockContent($gotoSection, $errMsg)
430  {
431  $this->block->expects(self::at(0))
432  ->method('setData')
433  ->with('goto_section', self::equalTo($gotoSection))
434  ->willReturnSelf();
435 
436  $this->block->expects(self::at(1))
437  ->method('setData')
438  ->with('error_msg', self::equalTo(__($errMsg)))
439  ->willReturnSelf();
440  }
441 }
__()
Definition: __.php:13
testExecuteNotAllowedOrderState($state, $restoreQuote, $expectedGotoSection)