Skip to content

Commit ae09d7d

Browse files
committed
Fixed condition with usage isPost method
- removed private function isPost - removed unused imports - added request method isPost - added interface that expands HTTP request class - fixed unit test in Contact module - fixed integration tests in Contact module
1 parent 321278b commit ae09d7d

File tree

5 files changed

+64
-14
lines changed

5 files changed

+64
-14
lines changed

app/code/Magento/Contact/Controller/Index/Post.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Magento\Framework\App\Request\DataPersistorInterface;
1313
use Magento\Framework\Controller\Result\Redirect;
1414
use Magento\Framework\Exception\LocalizedException;
15-
use Magento\Framework\HTTP\PhpEnvironment\Request;
1615
use Psr\Log\LoggerInterface;
1716
use Magento\Framework\App\ObjectManager;
1817
use Magento\Framework\DataObject;
@@ -67,7 +66,7 @@ public function __construct(
6766
*/
6867
public function execute()
6968
{
70-
if (!$this->isPostRequest()) {
69+
if (!$this->getRequest()->isPost()) {
7170
return $this->resultRedirectFactory->create()->setPath('*/*/');
7271
}
7372
try {
@@ -101,16 +100,6 @@ private function sendEmail($post)
101100
);
102101
}
103102

104-
/**
105-
* @return bool
106-
*/
107-
private function isPostRequest()
108-
{
109-
/** @var Request $request */
110-
$request = $this->getRequest();
111-
return !empty($request->getPostValue());
112-
}
113-
114103
/**
115104
* @return array
116105
* @throws \Exception

app/code/Magento/Contact/Test/Unit/Controller/Index/PostTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ protected function setUp()
7878
$this->createMock(\Magento\Framework\Message\ManagerInterface::class);
7979
$this->requestStub = $this->createPartialMock(
8080
\Magento\Framework\App\Request\Http::class,
81-
['getPostValue', 'getParams', 'getParam']
81+
['getPostValue', 'getParams', 'getParam', 'isPost']
8282
);
8383
$this->redirectResultMock = $this->createMock(\Magento\Framework\Controller\Result\Redirect::class);
8484
$this->redirectResultMock->method('setPath')->willReturnSelf();
@@ -174,6 +174,10 @@ public function testExecuteValidPost()
174174
*/
175175
private function stubRequestPostData($post)
176176
{
177+
$this->requestStub
178+
->expects($this->once())
179+
->method('isPost')
180+
->willReturn(!empty($post));
177181
$this->requestStub->method('getPostValue')->willReturn($post);
178182
$this->requestStub->method('getParams')->willReturn($post);
179183
$this->requestStub->method('getParam')->willReturnCallback(

dev/tests/integration/testsuite/Magento/Contact/Controller/IndexTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Contact\Controller;
77

8+
use Zend\Http\Request;
9+
810
/**
911
* Contact index controller test
1012
*/
@@ -19,6 +21,7 @@ public function testPostAction()
1921
'hideit' => '',
2022
];
2123
$this->getRequest()->setPostValue($params);
24+
$this->getRequest()->setMethod(Request::METHOD_POST);
2225

2326
$this->dispatch('contact/index/post');
2427
$this->assertRedirect($this->stringContains('contact/index'));
@@ -38,6 +41,7 @@ public function testPostAction()
3841
public function testInvalidPostAction($params, $expectedMessage)
3942
{
4043
$this->getRequest()->setPostValue($params);
44+
$this->getRequest()->setMethod(Request::METHOD_POST);
4145

4246
$this->dispatch('contact/index/post');
4347
$this->assertRedirect($this->stringContains('contact/index'));
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\App;
8+
9+
interface HttpRequestInterface
10+
{
11+
/**
12+
* Returned true if POST request
13+
*
14+
* @return boolean
15+
*/
16+
public function isPost();
17+
18+
/**
19+
* Returned true if GET request
20+
*
21+
* @return boolean
22+
*/
23+
public function isGet();
24+
25+
/**
26+
* Returned true if PATCH request
27+
*
28+
* @return boolean
29+
*/
30+
public function isPatch();
31+
32+
/**
33+
* Returned true if DELETE request
34+
*
35+
* @return boolean
36+
*/
37+
public function isDelete();
38+
39+
/**
40+
* Returned true if PUT request
41+
*
42+
* @return boolean
43+
*/
44+
public function isPut();
45+
46+
/**
47+
* Returned true if Ajax request
48+
*
49+
* @return boolean
50+
*/
51+
public function isAjax();
52+
}

lib/internal/Magento/Framework/App/Request/Http.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Framework\App\Request;
77

8+
use Magento\Framework\App\HttpRequestInterface;
89
use Magento\Framework\App\RequestContentInterface;
910
use Magento\Framework\App\RequestSafetyInterface;
1011
use Magento\Framework\App\Route\ConfigInterface\Proxy as ConfigInterface;
@@ -16,7 +17,7 @@
1617
/**
1718
* Http request
1819
*/
19-
class Http extends Request implements RequestContentInterface, RequestSafetyInterface
20+
class Http extends Request implements RequestContentInterface, RequestSafetyInterface, HttpRequestInterface
2021
{
2122
/**#@+
2223
* HTTP Ports

0 commit comments

Comments
 (0)