71 $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
72 $this->requestMock = $this->createMock(\
Magento\Framework\
App\RequestInterface::class);
73 $this->responseMock = $this->createMock(\
Magento\Framework\
App\ResponseInterface::class);
74 $this->directoryMock = $this->createMock(\
Magento\Framework\
Filesystem\Directory\ReadInterface::class);
75 $this->fileSystemMock = $this->createMock(\
Magento\Framework\Filesystem::class);
76 $this->storage = $this->createMock(\
Magento\MediaStorage\Helper\
File\Storage::class);
77 $this->objectManagerMock = $this->createMock(\
Magento\Framework\ObjectManagerInterface::class);
79 $this->contextMock = $this->createMock(\
Magento\Backend\
App\Action\Context::class);
80 $this->contextMock->expects($this->any())->method(
'getRequest')->willReturn($this->requestMock);
81 $this->contextMock->expects($this->any())->method(
'getResponse')->willReturn($this->responseMock);
82 $this->contextMock->expects($this->any())->method(
'getObjectManager')->willReturn($this->objectManagerMock);
84 $this->urlDecoderMock = $this->createMock(\
Magento\Framework\Url\DecoderInterface::class);
85 $this->resultRawMock = $this->createMock(\
Magento\Framework\Controller\Result\Raw::class);
87 $this->resultRawFactoryMock = $this->createPartialMock(
88 \
Magento\Framework\Controller\Result\RawFactory::class,
97 public function testExecuteNoParamsShouldThrowException()
100 $controller = $this->objectManager->getObject(\
Magento\Customer\Controller\Adminhtml\Index\Viewfile::class);
104 public function testExecuteParamFile()
106 $decodedFile =
'decoded_file';
111 $this->requestMock->expects($this->atLeastOnce())->method(
'getParam')->with(
'file')->willReturn($decodedFile);
113 $this->directoryMock->expects($this->once())->method(
'getAbsolutePath')->with(
$fileName)->willReturn(
$path);
115 $this->fileSystemMock->expects($this->once())->method(
'getDirectoryRead')
117 ->willReturn($this->directoryMock);
119 $this->storage->expects($this->once())->method(
'processStorageFile')->with(
$path)->willReturn(
true);
121 $this->objectManagerMock->expects($this->any())->method(
'get')
124 [\
Magento\Framework\Filesystem::class, $this->fileSystemMock],
125 [\
Magento\MediaStorage\Helper\File\Storage::class, $this->storage]
129 $this->urlDecoderMock->expects($this->once())->method(
'decode')->with($decodedFile)->willReturn($file);
131 $fileResponse = $this->createMock(\
Magento\Framework\
App\ResponseInterface::class);
132 $fileFactoryMock = $this->createMock(\
Magento\Framework\
App\
Response\Http\FileFactory::class);
133 $fileFactoryMock->expects($this->once())->method(
'create')->with(
135 [
'type' =>
'filename',
'value' =>
$fileName],
137 )->willReturn($fileResponse);
141 \
Magento\Customer\Controller\Adminhtml\Index\Viewfile::class,
143 'context' => $this->contextMock,
144 'urlDecoder' => $this->urlDecoderMock,
145 'fileFactory' => $fileFactoryMock
151 public function testExecuteGetParamImage()
153 $decodedFile =
'decoded_file';
157 $stat = [
'size' => 10,
'mtime' => 10];
159 $this->requestMock->expects($this->any())->method(
'getParam')
160 ->willReturnMap([[
'file',
null,
null], [
'image',
null, $decodedFile]]);
162 $this->directoryMock->expects($this->once())->method(
'getAbsolutePath')->with(
$fileName)->willReturn(
$path);
163 $this->directoryMock->expects($this->once())->method(
'stat')->with(
$fileName)->willReturn($stat);
165 $this->fileSystemMock->expects($this->once())->method(
'getDirectoryRead')
167 ->willReturn($this->directoryMock);
169 $this->storage->expects($this->once())->method(
'processStorageFile')->with(
$path)->willReturn(
true);
171 $this->objectManagerMock->expects($this->any())->method(
'get')
174 [\
Magento\Framework\Filesystem::class, $this->fileSystemMock],
175 [\
Magento\MediaStorage\Helper\File\Storage::class, $this->storage]
179 $this->urlDecoderMock->expects($this->once())->method(
'decode')->with($decodedFile)->willReturn($file);
181 $this->resultRawMock->expects($this->once())->method(
'setHttpResponseCode')->with(200)->willReturnSelf();
182 $this->resultRawMock->expects($this->any())->method(
'setHeader')
185 [
'Pragma',
'public',
true, $this->resultRawMock],
186 [
'Content-type',
'application/octet-stream',
true, $this->resultRawMock],
187 [
'Content-Length', $stat[
'size'],
false, $this->resultRawMock],
188 [
'Pragma',
'public',
true, $this->resultRawMock],
192 $this->resultRawFactoryMock = $this->createPartialMock(
193 \
Magento\Framework\Controller\Result\RawFactory::class,
196 $this->resultRawFactoryMock->expects($this->once())->method(
'create')->willReturn($this->resultRawMock);
200 \
Magento\Customer\Controller\Adminhtml\Index\Viewfile::class,
202 'context' => $this->contextMock,
203 'urlDecoder' => $this->urlDecoderMock,
204 'resultRawFactory' => $this->resultRawFactoryMock
207 $this->assertSame($this->resultRawMock,
$controller->execute());
216 $file =
'../../../app/etc/env.php';
217 $decodedFile = base64_encode($file);
221 $this->requestMock->expects($this->atLeastOnce())->method(
'getParam')->with(
'file')->willReturn($decodedFile);
223 $this->directoryMock->expects($this->once())->method(
'getAbsolutePath')->with(
$fileName)->willReturn(
$path);
225 $this->fileSystemMock->expects($this->once())->method(
'getDirectoryRead')
227 ->willReturn($this->directoryMock);
229 $this->storage->expects($this->once())->method(
'processStorageFile')->with(
$path)->willReturn(
false);
231 $this->objectManagerMock->expects($this->any())->method(
'get')
234 [\
Magento\Framework\Filesystem::class, $this->fileSystemMock],
235 [\
Magento\MediaStorage\Helper\
File\Storage::class, $this->storage],
239 $this->urlDecoderMock->expects($this->once())->method(
'decode')->with($decodedFile)->willReturn($file);
240 $fileFactoryMock = $this->createMock(
249 \
Magento\Customer\Controller\Adminhtml\Index\Viewfile::class,
251 'context' => $this->contextMock,
252 'urlDecoder' => $this->urlDecoderMock,
253 'fileFactory' => $fileFactoryMock,