Skip to content

Commit fecce53

Browse files
author
Oleksandr Iegorov
committed
Fix for issue #21299. Change HEAD action mapping to GET action interface and add HEAD request handling
1 parent be77120 commit fecce53

File tree

3 files changed

+94
-53
lines changed

3 files changed

+94
-53
lines changed

lib/internal/Magento/Framework/App/Test/Unit/HttpTest.php

Lines changed: 77 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,17 @@ private function setUpLaunch()
135135
{
136136
$frontName = 'frontName';
137137
$areaCode = 'areaCode';
138-
$this->requestMock->expects($this->once())->method('getFrontName')->will($this->returnValue($frontName));
138+
$this->requestMock->expects($this->once())
139+
->method('getFrontName')
140+
->willReturn($frontName);
139141
$this->areaListMock->expects($this->once())
140142
->method('getCodeByFrontName')
141-
->with($frontName)->will($this->returnValue($areaCode));
143+
->with($frontName)
144+
->willReturn($areaCode);
142145
$this->configLoaderMock->expects($this->once())
143-
->method('load')->with($areaCode)->will($this->returnValue([]));
146+
->method('load')
147+
->with($areaCode)
148+
->willReturn([]);
144149
$this->objectManagerMock->expects($this->once())->method('configure')->with([]);
145150
$this->objectManagerMock->expects($this->once())
146151
->method('get')
@@ -149,13 +154,15 @@ private function setUpLaunch()
149154
$this->frontControllerMock->expects($this->once())
150155
->method('dispatch')
151156
->with($this->requestMock)
152-
->will($this->returnValue($this->responseMock));
157+
->willReturn($this->responseMock);
153158
}
154159

155160
public function testLaunchSuccess()
156161
{
157162
$this->setUpLaunch();
158-
$this->requestMock->expects($this->once())->method('isHead')->will($this->returnValue(false));
163+
$this->requestMock->expects($this->once())
164+
->method('isHead')
165+
->willReturn(false);
159166
$this->eventManagerMock->expects($this->once())
160167
->method('dispatch')
161168
->with(
@@ -172,14 +179,16 @@ public function testLaunchSuccess()
172179
public function testLaunchException()
173180
{
174181
$this->setUpLaunch();
175-
$this->frontControllerMock->expects($this->once())->method('dispatch')->with($this->requestMock)->will(
176-
$this->returnCallback(
177-
function () {
178-
// phpcs:ignore Magento2.Exceptions.DirectThrow
179-
throw new \Exception('Message');
180-
}
181-
)
182-
);
182+
$this->frontControllerMock->expects($this->once())
183+
->method('dispatch')
184+
->with($this->requestMock)->will(
185+
$this->returnCallback(
186+
function () {
187+
// phpcs:ignore Magento2.Exceptions.DirectThrow
188+
throw new \Exception('Message');
189+
}
190+
)
191+
);
183192
$this->http->launch();
184193
}
185194

@@ -192,20 +201,22 @@ function () {
192201
public function testLaunchHeadRequest($body, $expectedLength)
193202
{
194203
$this->setUpLaunch();
195-
$this->requestMock->expects($this->once())->method('isHead')->will($this->returnValue(true));
204+
$this->requestMock->expects($this->once())
205+
->method('isHead')
206+
->willReturn(true);
196207
$this->responseMock->expects($this->once())
197208
->method('getHttpResponseCode')
198-
->will($this->returnValue(200));
209+
->willReturn(200);
199210
$this->responseMock->expects($this->once())
200211
->method('getContent')
201-
->will($this->returnValue($body));
212+
->willReturn($body);
202213
$this->responseMock->expects($this->once())
203214
->method('clearBody')
204-
->will($this->returnValue($this->responseMock));
215+
->willReturn($this->responseMock);
205216
$this->responseMock->expects($this->once())
206217
->method('setHeader')
207218
->with('Content-Length', $expectedLength)
208-
->will($this->returnValue($this->responseMock));
219+
->willReturn($this->responseMock);
209220
$this->eventManagerMock->expects($this->once())
210221
->method('dispatch')
211222
->with(
@@ -219,7 +230,7 @@ public function testLaunchHeadRequest($body, $expectedLength)
219230
* Different test content for responseMock with their expected lengths in bytes.
220231
* @return array
221232
*/
222-
public function dataProviderForTestLaunchHeadRequest()
233+
public function dataProviderForTestLaunchHeadRequest(): array
223234
{
224235
return [
225236
[
@@ -244,20 +255,29 @@ public function dataProviderForTestLaunchHeadRequest()
244255
public function testHandleDeveloperModeNotInstalled()
245256
{
246257
$dir = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
247-
$dir->expects($this->once())->method('getAbsolutePath')->willReturn(__DIR__);
258+
$dir->expects($this->once())
259+
->method('getAbsolutePath')
260+
->willReturn(__DIR__);
248261
$this->filesystemMock->expects($this->once())
249262
->method('getDirectoryRead')
250263
->with(DirectoryList::ROOT)
251264
->willReturn($dir);
252-
$this->responseMock->expects($this->once())->method('setRedirect')->with('/_files/');
253-
$this->responseMock->expects($this->once())->method('sendHeaders');
265+
$this->responseMock->expects($this->once())
266+
->method('setRedirect')
267+
->with('/_files/');
268+
$this->responseMock->expects($this->once())
269+
->method('sendHeaders');
254270
$bootstrap = $this->getBootstrapNotInstalled();
255-
$bootstrap->expects($this->once())->method('getParams')->willReturn([
256-
'SCRIPT_NAME' => '/index.php',
257-
'DOCUMENT_ROOT' => __DIR__,
258-
'SCRIPT_FILENAME' => __DIR__ . '/index.php',
259-
SetupInfo::PARAM_NOT_INSTALLED_URL_PATH => '_files',
260-
]);
271+
$bootstrap->expects($this->once())
272+
->method('getParams')
273+
->willReturn(
274+
[
275+
'SCRIPT_NAME' => '/index.php',
276+
'DOCUMENT_ROOT' => __DIR__,
277+
'SCRIPT_FILENAME' => __DIR__ . '/index.php',
278+
SetupInfo::PARAM_NOT_INSTALLED_URL_PATH => '_files',
279+
]
280+
);
261281
$this->assertTrue($this->http->catchException($bootstrap, new \Exception('Test Message')));
262282
}
263283

@@ -266,24 +286,37 @@ public function testHandleDeveloperMode()
266286
$this->filesystemMock->expects($this->once())
267287
->method('getDirectoryRead')
268288
->will($this->throwException(new \Exception('strange error')));
269-
$this->responseMock->expects($this->once())->method('setHttpResponseCode')->with(500);
270-
$this->responseMock->expects($this->once())->method('setHeader')->with('Content-Type', 'text/plain');
289+
$this->responseMock->expects($this->once())
290+
->method('setHttpResponseCode')
291+
->with(500);
292+
$this->responseMock->expects($this->once())
293+
->method('setHeader')
294+
->with('Content-Type', 'text/plain');
271295
$constraint = new \PHPUnit\Framework\Constraint\StringStartsWith('1 exception(s):');
272-
$this->responseMock->expects($this->once())->method('setBody')->with($constraint);
273-
$this->responseMock->expects($this->once())->method('sendResponse');
296+
$this->responseMock->expects($this->once())
297+
->method('setBody')
298+
->with($constraint);
299+
$this->responseMock->expects($this->once())
300+
->method('sendResponse');
274301
$bootstrap = $this->getBootstrapNotInstalled();
275-
$bootstrap->expects($this->once())->method('getParams')->willReturn(
276-
['DOCUMENT_ROOT' => 'something', 'SCRIPT_FILENAME' => 'something/else']
277-
);
302+
$bootstrap->expects($this->once())
303+
->method('getParams')
304+
->willReturn(
305+
['DOCUMENT_ROOT' => 'something', 'SCRIPT_FILENAME' => 'something/else']
306+
);
278307
$this->assertTrue($this->http->catchException($bootstrap, new \Exception('Test')));
279308
}
280309

281310
public function testCatchExceptionSessionException()
282311
{
283-
$this->responseMock->expects($this->once())->method('setRedirect');
284-
$this->responseMock->expects($this->once())->method('sendHeaders');
312+
$this->responseMock->expects($this->once())
313+
->method('setRedirect');
314+
$this->responseMock->expects($this->once())
315+
->method('sendHeaders');
285316
$bootstrap = $this->createMock(\Magento\Framework\App\Bootstrap::class);
286-
$bootstrap->expects($this->once())->method('isDeveloperMode')->willReturn(false);
317+
$bootstrap->expects($this->once())
318+
->method('isDeveloperMode')
319+
->willReturn(false);
287320
$this->assertTrue($this->http->catchException(
288321
$bootstrap,
289322
new \Magento\Framework\Exception\SessionException(new \Magento\Framework\Phrase('Test'))
@@ -298,8 +331,12 @@ public function testCatchExceptionSessionException()
298331
private function getBootstrapNotInstalled()
299332
{
300333
$bootstrap = $this->createMock(\Magento\Framework\App\Bootstrap::class);
301-
$bootstrap->expects($this->once())->method('isDeveloperMode')->willReturn(true);
302-
$bootstrap->expects($this->once())->method('getErrorCode')->willReturn(Bootstrap::ERR_IS_INSTALLED);
334+
$bootstrap->expects($this->once())
335+
->method('isDeveloperMode')
336+
->willReturn(true);
337+
$bootstrap->expects($this->once())
338+
->method('getErrorCode')
339+
->willReturn(Bootstrap::ERR_IS_INSTALLED);
303340
return $bootstrap;
304341
}
305342
}

lib/internal/Magento/Framework/File/Test/Unit/Transfer/Adapter/HttpTest.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,34 @@
66

77
namespace Magento\Framework\File\Test\Unit\Transfer\Adapter;
88

9-
use \Magento\Framework\File\Transfer\Adapter\Http;
9+
use Magento\Framework\File\Transfer\Adapter\Http;
10+
use Magento\Framework\File\Mime;
11+
use Magento\Framework\HTTP\PhpEnvironment\Response;
12+
use Magento\Framework\App\Request\Http as RequestHttp;
13+
use PHPUnit\Framework\MockObject\MockObject;
1014

1115
/**
1216
* Tests http transfer adapter.
1317
*/
1418
class HttpTest extends \PHPUnit\Framework\TestCase
1519
{
1620
/**
17-
* @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
21+
* @var RequestHttp|MockObject
1822
*/
1923
private $request;
2024

2125
/**
22-
* @var \Magento\Framework\HTTP\PhpEnvironment\Response|\PHPUnit_Framework_MockObject_MockObject
26+
* @var Response|MockObject
2327
*/
2428
private $response;
2529

2630
/**
27-
* @var Http|\PHPUnit_Framework_MockObject_MockObject
31+
* @var Http|MockObject
2832
*/
2933
private $object;
3034

3135
/**
32-
* @var \Magento\Framework\File\Mime|\PHPUnit_Framework_MockObject_MockObject
36+
* @var Mime|MockObject
3337
*/
3438
private $mime;
3539

@@ -39,12 +43,12 @@ class HttpTest extends \PHPUnit\Framework\TestCase
3943
protected function setUp()
4044
{
4145
$this->response = $this->createPartialMock(
42-
\Magento\Framework\HTTP\PhpEnvironment\Response::class,
46+
Response::class,
4347
['setHeader', 'sendHeaders', 'setHeaders']
4448
);
45-
$this->mime = $this->createMock(\Magento\Framework\File\Mime::class);
49+
$this->mime = $this->createMock(Mime::class);
4650
$this->request = $this->createPartialMock(
47-
\Magento\Framework\App\Request\Http::class,
51+
RequestHttp::class,
4852
['isHead']
4953
);
5054
$this->object = new Http($this->response, $this->mime, $this->request);
@@ -98,10 +102,10 @@ public function testSendWithOptions(): void
98102
$this->mime->expects($this->once())
99103
->method('getMimeType')
100104
->with($file)
101-
->will($this->returnValue($contentType));
105+
->willReturn($contentType);
102106
$this->request->expects($this->once())
103107
->method('isHead')
104-
->will($this->returnValue(false));
108+
->willReturn(false);
105109
$this->expectOutputString(file_get_contents($file));
106110

107111
$this->object->send(['filepath' => $file, 'headers' => $headers]);
@@ -145,10 +149,10 @@ public function testSendHeadRequest(): void
145149
$this->mime->expects($this->once())
146150
->method('getMimeType')
147151
->with($file)
148-
->will($this->returnValue($contentType));
152+
->willReturn($contentType);
149153
$this->request->expects($this->once())
150154
->method('isHead')
151-
->will($this->returnValue(true));
155+
->willReturn(true);
152156

153157
$this->object->send($file);
154158
$this->assertEquals(false, $this->hasOutput());

lib/internal/Magento/Framework/File/Transfer/Adapter/Http.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private function getFilePath($options): string
106106
* @param array $options
107107
* @param string $filepath
108108
*/
109-
protected function prepareResponse($options, string $filepath): void
109+
private function prepareResponse($options, string $filepath): void
110110
{
111111
$mimeType = $this->mime->getMimeType($filepath);
112112
if (is_array($options) && isset($options['headers']) && $options['headers'] instanceof \Zend\Http\Headers) {

0 commit comments

Comments
 (0)