Skip to content

Commit 88d5dfc

Browse files
author
Oleksii Korshenko
authored
MAGETWO-81973: Allow setting of http response status code in a Redirection magento#11405
2 parents ada5ec9 + 8e2feb4 commit 88d5dfc

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

lib/internal/Magento/Framework/Controller/Result/Redirect.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Magento\Framework\App;
1010
use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
1111
use Magento\Framework\Controller\AbstractResult;
12+
use Magento\Framework\App\Response\RedirectInterface;
13+
use Magento\Framework\UrlInterface;
1214

1315
/**
1416
* In many cases controller actions may result in a redirect
@@ -18,13 +20,14 @@
1820
*/
1921
class Redirect extends AbstractResult
2022
{
23+
2124
/**
22-
* @var \Magento\Framework\App\Response\RedirectInterface
25+
* @var RedirectInterface
2326
*/
2427
protected $redirect;
2528

2629
/**
27-
* @var \Magento\Framework\UrlInterface
30+
* @var UrlInterface
2831
*/
2932
protected $urlBuilder;
3033

@@ -37,11 +40,11 @@ class Redirect extends AbstractResult
3740
* Constructor
3841
*
3942
* @param App\Response\RedirectInterface $redirect
40-
* @param \Magento\Framework\UrlInterface $urlBuilder
43+
* @param UrlInterface $urlBuilder
4144
*/
4245
public function __construct(
4346
App\Response\RedirectInterface $redirect,
44-
\Magento\Framework\UrlInterface $urlBuilder
47+
UrlInterface $urlBuilder
4548
) {
4649
$this->redirect = $redirect;
4750
$this->urlBuilder = $urlBuilder;
@@ -70,6 +73,7 @@ public function setRefererOrBaseUrl()
7073
}
7174

7275
/**
76+
* URL Setter
7377
* @param string $url
7478
* @return $this
7579
*/
@@ -97,7 +101,11 @@ public function setPath($path, array $params = [])
97101
*/
98102
protected function render(HttpResponseInterface $response)
99103
{
100-
$response->setRedirect($this->url);
104+
if (empty($this->httpResponseCode)) {
105+
$response->setRedirect($this->url);
106+
} else {
107+
$response->setRedirect($this->url, $this->httpResponseCode);
108+
}
101109
return $this;
102110
}
103111
}

lib/internal/Magento/Framework/Controller/Test/Unit/Result/RedirectTest.php

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66

77
namespace Magento\Framework\Controller\Test\Unit\Result;
88

9-
use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
9+
use \PHPUnit\Framework\TestCase;
10+
use \Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
11+
use \Magento\Framework\App\Response\RedirectInterface;
1012
use \Magento\Framework\Controller\Result\Redirect;
13+
use \Magento\Framework\UrlInterface;
1114

12-
class RedirectTest extends \PHPUnit\Framework\TestCase
15+
class RedirectTest extends TestCase
1316
{
1417
/** @var \Magento\Framework\Controller\Result\Redirect */
1518
protected $redirect;
@@ -28,9 +31,9 @@ class RedirectTest extends \PHPUnit\Framework\TestCase
2831

2932
protected function setUp()
3033
{
31-
$this->redirectInterface = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
32-
$this->urlBuilder = $this->createMock(\Magento\Framework\UrlInterface::class);
33-
$this->urlInterface = $this->createMock(\Magento\Framework\UrlInterface::class);
34+
$this->redirectInterface = $this->createMock(RedirectInterface::class);
35+
$this->urlBuilder = $this->createMock(UrlInterface::class);
36+
$this->urlInterface = $this->createMock(UrlInterface::class);
3437
$this->response = $this->createMock(HttpResponseInterface::class);
3538
$this->redirect = new Redirect($this->redirectInterface, $this->urlInterface);
3639
}
@@ -39,7 +42,7 @@ public function testSetRefererUrl()
3942
{
4043
$this->redirectInterface->expects($this->once())->method('getRefererUrl');
4144
$this->assertInstanceOf(
42-
\Magento\Framework\Controller\Result\Redirect::class,
45+
Redirect::class,
4346
$this->redirect->setRefererUrl()
4447
);
4548
}
@@ -48,15 +51,15 @@ public function testSetRefererOrBaseUrl()
4851
{
4952
$this->redirectInterface->expects($this->once())->method('getRedirectUrl');
5053
$this->assertInstanceOf(
51-
\Magento\Framework\Controller\Result\Redirect::class,
54+
Redirect::class,
5255
$this->redirect->setRefererOrBaseUrl()
5356
);
5457
}
5558

5659
public function testSetUrl()
5760
{
5861
$url = 'http://test.com';
59-
$this->assertInstanceOf(\Magento\Framework\Controller\Result\Redirect::class, $this->redirect->setUrl($url));
62+
$this->assertInstanceOf(Redirect::class, $this->redirect->setUrl($url));
6063
}
6164

6265
public function testSetPath()
@@ -67,17 +70,36 @@ public function testSetPath()
6770
$this->returnValue($params)
6871
);
6972
$this->assertInstanceOf(
70-
\Magento\Framework\Controller\Result\Redirect::class,
73+
Redirect::class,
7174
$this->redirect->setPath($path, $params)
7275
);
7376
}
7477

75-
public function testRender()
78+
public function httpRedirectResponseStatusCodes()
7679
{
77-
$this->response->expects($this->once())->method('setRedirect');
78-
$this->assertInstanceOf(
79-
\Magento\Framework\Controller\Result\Redirect::class,
80-
$this->redirect->renderResult($this->response)
81-
);
80+
return [
81+
[302, null],
82+
[302, 302],
83+
[303, 303]
84+
];
85+
}
86+
87+
/**
88+
* @param int $expectedStatusCode
89+
* @param int|null $actualStatusCode
90+
* @dataProvider httpRedirectResponseStatusCodes
91+
*/
92+
public function testRender($expectedStatusCode, $actualStatusCode)
93+
{
94+
$url = 'http://test.com';
95+
$this->redirect->setUrl($url);
96+
$this->redirect->setHttpResponseCode($actualStatusCode);
97+
98+
$this->response
99+
->expects($this->once())
100+
->method('setRedirect')
101+
->with($url, $expectedStatusCode);
102+
103+
$this->redirect->renderResult($this->response);
82104
}
83105
}

0 commit comments

Comments
 (0)