Skip to content

Commit f99403a

Browse files
author
vpaladiychuk
committed
MAGETWO-2204: "HEADERS ALREADY SENT" When Controller Action Outputs Directly
1 parent 96d5e1b commit f99403a

File tree

2 files changed

+92
-7
lines changed
  • app/code/Magento/Customer/Controller/Adminhtml/Index
  • dev/tests/unit/testsuite/Magento/Customer/Controller/Adminhtml/Index

2 files changed

+92
-7
lines changed

app/code/Magento/Customer/Controller/Adminhtml/Index/Viewfile.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,11 @@ public function execute()
181181
$contentLength = $stat['size'];
182182
$contentModify = $stat['mtime'];
183183

184-
$this->getResponse()
185-
->setHttpResponseCode(200)
184+
$resultRaw->setHttpResponseCode(200)
186185
->setHeader('Pragma', 'public', true)
187186
->setHeader('Content-type', $contentType, true)
188187
->setHeader('Content-Length', $contentLength)
189-
->setHeader('Last-Modified', date('r', $contentModify))
190-
->clearBody();
191-
$this->getResponse()->sendHeaders();
192-
188+
->setHeader('Last-Modified', date('r', $contentModify));
193189
$resultRaw->setContents($directory->readFile($fileName));
194190
} else {
195191
$name = pathinfo($path, PATHINFO_BASENAME);

dev/tests/unit/testsuite/Magento/Customer/Controller/Adminhtml/Index/ViewfileTest.php

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,99 @@ public function testExecuteParamFile()
6767
);
6868
$resultRawFactoryMock->expects($this->once())->method('create')->willReturn($resultRawMock);
6969

70+
$fileResponse = $this->getMock('Magento\Framework\App\ResponseInterface', [], [], '', false);
71+
$fileFactoryMock = $this->getMock('Magento\Framework\App\Response\Http\FileFactory', [], [], '', false);
72+
$fileFactoryMock->expects($this->once())->method('create')->with(
73+
$path,
74+
['type' => 'filename', 'value' => $fileName],
75+
\Magento\Framework\App\Filesystem\DirectoryList::MEDIA
76+
)->willReturn($fileResponse);
77+
78+
/** @var \Magento\Customer\Controller\Adminhtml\Index\Viewfile $controller */
79+
$controller = (new \Magento\TestFramework\Helper\ObjectManager($this))->getObject(
80+
'Magento\Customer\Controller\Adminhtml\Index\Viewfile',
81+
[
82+
'context' => $contextMock,
83+
'urlDecoder' => $urlDecoderMock,
84+
'resultRawFactory' => $resultRawFactoryMock,
85+
'fileFactory' => $fileFactoryMock
86+
]
87+
);
88+
$this->assertSame($resultRawMock, $controller->execute());
89+
$this->assertSame($fileResponse, $controller->getResponse());
90+
}
91+
92+
public function testExecuteGetParamImage()
93+
{
94+
$decodedFile = 'decoded_file';
95+
$file = 'file';
96+
$fileName = 'customer/' . $file;
97+
$path = 'path';
98+
$stat = ['size' => 10, 'mtime' => 10];
99+
100+
$requestMock = $this->getMock('Magento\Framework\App\RequestInterface', [], [], '', false);
101+
$requestMock->expects($this->at(0))->method('getParam')->with('file')->willReturn(null);
102+
$requestMock->expects($this->at(1))->method('getParam')->with('image')->willReturn($decodedFile);
103+
$requestMock->expects($this->at(2))->method('getParam')->with('image')->willReturn($decodedFile);
104+
105+
$responseMock = $this->getMock('Magento\Framework\App\ResponseInterface', [], [], '', false);
106+
107+
$directoryMock = $this->getMock('Magento\Framework\Filesystem\Directory\ReadInterface', [], [], '', false);
108+
$directoryMock->expects($this->once())->method('getAbsolutePath')->with($fileName)->willReturn($path);
109+
$directoryMock->expects($this->once())->method('stat')->with($path)->willReturn($stat);
110+
111+
$fileSystemMock = $this->getMock('Magento\Framework\Filesystem', [], [], '', false);
112+
$fileSystemMock->expects($this->once())->method('getDirectoryRead')
113+
->with(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)
114+
->willReturn($directoryMock);
115+
116+
$storage = $this->getMock('Magento\Core\Helper\File\Storage', [], [], '', false);
117+
$storage->expects($this->once())->method('processStorageFile')->with($path)->willReturn(true);
118+
119+
$objectManager = $this->getMock('Magento\Framework\ObjectManagerInterface', [], [], '', false);
120+
$objectManager->expects($this->at(0))->method('get')->with('Magento\Framework\Filesystem')
121+
->willReturn($fileSystemMock);
122+
$objectManager->expects($this->at(1))->method('get')->with('Magento\Core\Helper\File\Storage')
123+
->willReturn($storage);
124+
125+
$contextMock = $this->getMock('Magento\Backend\App\Action\Context', [], [], '', false);
126+
$contextMock->expects($this->once())->method('getRequest')->willReturn($requestMock);
127+
$contextMock->expects($this->once())->method('getResponse')->willReturn($responseMock);
128+
$contextMock->expects($this->once())->method('getObjectManager')->willReturn($objectManager);
129+
130+
$urlDecoderMock = $this->getMock('Magento\Framework\Url\DecoderInterface', [], [], '', false);
131+
$urlDecoderMock->expects($this->once())->method('decode')->with($decodedFile)->willReturn($file);
132+
133+
$resultRawMock = $this->getMock('Magento\Framework\Controller\Result\Raw', [], [], '', false);
134+
$resultRawMock->expects($this->once())->method('setHttpResponseCode')->with(200)->willReturnSelf();
135+
$resultRawMock->expects($this->at(1))->method('setHeader')->with('Pragma', 'public', true)->willReturnSelf();
136+
$resultRawMock->expects($this->at(2))->method('setHeader')
137+
->with('Content-type', 'application/octet-stream', true)
138+
->willReturnSelf();
139+
$resultRawMock->expects($this->at(3))->method('setHeader')
140+
->with('Content-Length', $stat['size'])
141+
->willReturnSelf();
142+
$resultRawMock->expects($this->at(4))->method('setHeader')
143+
->with('Last-Modified', date('r', $stat['mtime']))
144+
->willReturnSelf();
145+
146+
$resultRawFactoryMock = $this->getMock(
147+
'Magento\Framework\Controller\Result\RawFactory',
148+
['create'],
149+
[],
150+
'',
151+
false
152+
);
153+
$resultRawFactoryMock->expects($this->once())->method('create')->willReturn($resultRawMock);
154+
70155
/** @var \Magento\Customer\Controller\Adminhtml\Index\Viewfile $controller */
71156
$controller = (new \Magento\TestFramework\Helper\ObjectManager($this))->getObject(
72157
'Magento\Customer\Controller\Adminhtml\Index\Viewfile',
73-
['context' => $contextMock, 'urlDecoder' => $urlDecoderMock, 'resultRawFactory' => $resultRawFactoryMock]
158+
[
159+
'context' => $contextMock,
160+
'urlDecoder' => $urlDecoderMock,
161+
'resultRawFactory' => $resultRawFactoryMock
162+
]
74163
);
75164
$this->assertSame($resultRawMock, $controller->execute());
76165
}

0 commit comments

Comments
 (0)