Skip to content

Commit b736175

Browse files
authored
ENGCOM-4394: Refactoring the Form class #21474
2 parents e51df41 + 191200c commit b736175

File tree

2 files changed

+150
-9
lines changed
  • app/code/Magento/Sales/Controller/Guest
  • dev/tests/integration/testsuite/Magento/Sales/Controller/Guest

2 files changed

+150
-9
lines changed

app/code/Magento/Sales/Controller/Guest/Form.php

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,72 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
declare(strict_types=1);
8+
79
namespace Magento\Sales\Controller\Guest;
810

9-
class Form extends \Magento\Framework\App\Action\Action
11+
use Magento\Customer\Model\Session as CustomerSession;
12+
use Magento\Framework\App\Action\Context;
13+
use Magento\Framework\App\Action\HttpGetActionInterface;
14+
use Magento\Framework\App\ObjectManager;
15+
use Magento\Framework\Controller\Result\Redirect;
16+
use Magento\Framework\View\Result\Page;
17+
use Magento\Framework\View\Result\PageFactory;
18+
use Magento\Sales\Helper\Guest as GuestHelper;
19+
20+
/**
21+
* Class Form
22+
*/
23+
class Form extends \Magento\Framework\App\Action\Action implements HttpGetActionInterface
1024
{
1125
/**
12-
* @var \Magento\Framework\View\Result\PageFactory
26+
* @var PageFactory
1327
*/
1428
protected $resultPageFactory;
1529

1630
/**
17-
* @param \Magento\Framework\App\Action\Context $context
18-
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
31+
* @var CustomerSession|null
32+
*/
33+
private $customerSession;
34+
35+
/**
36+
* @var GuestHelper|null
37+
*/
38+
private $guestHelper;
39+
40+
/**
41+
* @param Context $context
42+
* @param PageFactory $resultPageFactory
43+
* @param CustomerSession|null $customerSession
44+
* @param GuestHelper|null $guestHelper
1945
*/
2046
public function __construct(
21-
\Magento\Framework\App\Action\Context $context,
22-
\Magento\Framework\View\Result\PageFactory $resultPageFactory
47+
Context $context,
48+
PageFactory $resultPageFactory,
49+
CustomerSession $customerSession = null,
50+
GuestHelper $guestHelper = null
2351
) {
2452
parent::__construct($context);
2553
$this->resultPageFactory = $resultPageFactory;
54+
$this->customerSession = $customerSession ?: ObjectManager::getInstance()->get(CustomerSession::class);
55+
$this->guestHelper = $guestHelper ?: ObjectManager::getInstance()->get(GuestHelper::class);
2656
}
2757

2858
/**
2959
* Order view form page
3060
*
31-
* @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page
61+
* @return Redirect|Page
3262
*/
3363
public function execute()
3464
{
35-
if ($this->_objectManager->get(\Magento\Customer\Model\Session::class)->isLoggedIn()) {
65+
if ($this->customerSession->isLoggedIn()) {
3666
return $this->resultRedirectFactory->create()->setPath('customer/account/');
3767
}
68+
3869
$resultPage = $this->resultPageFactory->create();
3970
$resultPage->getConfig()->getTitle()->set(__('Orders and Returns'));
40-
$this->_objectManager->get(\Magento\Sales\Helper\Guest::class)->getBreadcrumbs($resultPage);
71+
$this->guestHelper->getBreadcrumbs($resultPage);
72+
4173
return $resultPage;
4274
}
4375
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sales\Controller\Guest;
9+
10+
use Magento\Customer\Model\Session;
11+
use Magento\Framework\Data\Form\FormKey;
12+
use Magento\Framework\Message\MessageInterface;
13+
use Magento\TestFramework\Request;
14+
use Magento\TestFramework\TestCase\AbstractController;
15+
16+
/**
17+
* @magentoAppIsolation enabled
18+
*/
19+
class FormTest extends AbstractController
20+
{
21+
/**
22+
* Test view order as guest with correct data
23+
*
24+
* @magentoDataFixture Magento/Sales/_files/order.php
25+
*/
26+
public function testViewOrderAsGuest()
27+
{
28+
$this->prepareRequestData();
29+
$this->dispatch('sales/guest/view/');
30+
$content = $this->getResponse()->getBody();
31+
$this->assertContains('Order # 100000001', $content);
32+
}
33+
34+
/**
35+
* View order as logged in customer
36+
*
37+
* @magentoDataFixture Magento/Sales/_files/order.php
38+
* @magentoDataFixture Magento/Customer/_files/customer.php
39+
*/
40+
public function testViewOrderAsLoggedIn()
41+
{
42+
$this->login(1);
43+
$this->dispatch('sales/guest/view/');
44+
$this->assertRedirect($this->stringContains('sales/order/history/'));
45+
}
46+
47+
/**
48+
* Test attempting to open the Returns form as logged in customer
49+
*
50+
* @magentoDataFixture Magento/Sales/_files/order.php
51+
* @magentoDataFixture Magento/Customer/_files/customer.php
52+
*/
53+
public function testAttemptToOpenTheFormAsLoggedIn()
54+
{
55+
$this->login(1);
56+
$this->dispatch('sales/guest/form/');
57+
$this->assertRedirect($this->stringContains('customer/account'));
58+
}
59+
60+
/**
61+
* Test Return Order for guest with incorrect data
62+
*/
63+
public function testViewOrderAsGuestWithIncorrectData()
64+
{
65+
$this->prepareRequestData(true);
66+
$this->dispatch('sales/guest/view/');
67+
$this->assertSessionMessages(
68+
$this->equalTo(['You entered incorrect data. Please try again.']),
69+
MessageInterface::TYPE_ERROR
70+
);
71+
}
72+
73+
/**
74+
* Login the user
75+
*
76+
* @param string $customerId Customer to mark as logged in for the session
77+
* @return void
78+
*/
79+
protected function login($customerId)
80+
{
81+
/** @var Session $session */
82+
$session = $this->_objectManager->get(Session::class);
83+
$session->loginById($customerId);
84+
}
85+
86+
/**
87+
* @param bool $invalidData
88+
* @return void
89+
*/
90+
private function prepareRequestData($invalidData = false)
91+
{
92+
$orderId = 100000001;
93+
$email = $invalidData ? '[email protected]' : '[email protected]';
94+
95+
/** @var FormKey $formKey */
96+
$formKey = $this->_objectManager->get(FormKey::class);
97+
$post = [
98+
'oar_order_id' => $orderId,
99+
'oar_billing_lastname' => 'lastname',
100+
'oar_type' => 'email',
101+
'oar_email' => $email,
102+
'oar_zip' => '',
103+
'form_key' => $formKey->getFormKey(),
104+
];
105+
106+
$this->getRequest()->setMethod(Request::METHOD_POST);
107+
$this->getRequest()->setPostValue($post);
108+
}
109+
}

0 commit comments

Comments
 (0)