Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ViewfileTest.php
Go to the documentation of this file.
1 <?php
8 
12 class ViewfileTest extends \PHPUnit\Framework\TestCase
13 {
18 
22  protected $resultRawMock;
23 
27  protected $urlDecoderMock;
28 
32  protected $contextMock;
33 
37  protected $objectManager;
38 
42  protected $objectManagerMock;
43 
47  protected $storage;
48 
52  protected $fileSystemMock;
53 
57  protected $responseMock;
58 
62  protected $directoryMock;
63 
67  protected $requestMock;
68 
69  protected function setUp()
70  {
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);
78 
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);
83 
84  $this->urlDecoderMock = $this->createMock(\Magento\Framework\Url\DecoderInterface::class);
85  $this->resultRawMock = $this->createMock(\Magento\Framework\Controller\Result\Raw::class);
86 
87  $this->resultRawFactoryMock = $this->createPartialMock(
88  \Magento\Framework\Controller\Result\RawFactory::class,
89  ['create']
90  );
91  }
92 
97  public function testExecuteNoParamsShouldThrowException()
98  {
100  $controller = $this->objectManager->getObject(\Magento\Customer\Controller\Adminhtml\Index\Viewfile::class);
101  $controller->execute();
102  }
103 
104  public function testExecuteParamFile()
105  {
106  $decodedFile = 'decoded_file';
107  $file = 'file';
108  $fileName = 'customer/' . $file;
109  $path = 'path';
110 
111  $this->requestMock->expects($this->atLeastOnce())->method('getParam')->with('file')->willReturn($decodedFile);
112 
113  $this->directoryMock->expects($this->once())->method('getAbsolutePath')->with($fileName)->willReturn($path);
114 
115  $this->fileSystemMock->expects($this->once())->method('getDirectoryRead')
116  ->with(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)
117  ->willReturn($this->directoryMock);
118 
119  $this->storage->expects($this->once())->method('processStorageFile')->with($path)->willReturn(true);
120 
121  $this->objectManagerMock->expects($this->any())->method('get')
122  ->willReturnMap(
123  [
124  [\Magento\Framework\Filesystem::class, $this->fileSystemMock],
125  [\Magento\MediaStorage\Helper\File\Storage::class, $this->storage]
126  ]
127  );
128 
129  $this->urlDecoderMock->expects($this->once())->method('decode')->with($decodedFile)->willReturn($file);
130 
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(
134  $path,
135  ['type' => 'filename', 'value' => $fileName],
136  \Magento\Framework\App\Filesystem\DirectoryList::MEDIA
137  )->willReturn($fileResponse);
138 
140  $controller = $this->objectManager->getObject(
141  \Magento\Customer\Controller\Adminhtml\Index\Viewfile::class,
142  [
143  'context' => $this->contextMock,
144  'urlDecoder' => $this->urlDecoderMock,
145  'fileFactory' => $fileFactoryMock
146  ]
147  );
148  $controller->execute();
149  }
150 
151  public function testExecuteGetParamImage()
152  {
153  $decodedFile = 'decoded_file';
154  $file = 'file';
155  $fileName = 'customer/' . $file;
156  $path = 'path';
157  $stat = ['size' => 10, 'mtime' => 10];
158 
159  $this->requestMock->expects($this->any())->method('getParam')
160  ->willReturnMap([['file', null, null], ['image', null, $decodedFile]]);
161 
162  $this->directoryMock->expects($this->once())->method('getAbsolutePath')->with($fileName)->willReturn($path);
163  $this->directoryMock->expects($this->once())->method('stat')->with($fileName)->willReturn($stat);
164 
165  $this->fileSystemMock->expects($this->once())->method('getDirectoryRead')
166  ->with(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)
167  ->willReturn($this->directoryMock);
168 
169  $this->storage->expects($this->once())->method('processStorageFile')->with($path)->willReturn(true);
170 
171  $this->objectManagerMock->expects($this->any())->method('get')
172  ->willReturnMap(
173  [
174  [\Magento\Framework\Filesystem::class, $this->fileSystemMock],
175  [\Magento\MediaStorage\Helper\File\Storage::class, $this->storage]
176  ]
177  );
178 
179  $this->urlDecoderMock->expects($this->once())->method('decode')->with($decodedFile)->willReturn($file);
180 
181  $this->resultRawMock->expects($this->once())->method('setHttpResponseCode')->with(200)->willReturnSelf();
182  $this->resultRawMock->expects($this->any())->method('setHeader')
183  ->willReturnMap(
184  [
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],
189  ]
190  );
191 
192  $this->resultRawFactoryMock = $this->createPartialMock(
193  \Magento\Framework\Controller\Result\RawFactory::class,
194  ['create']
195  );
196  $this->resultRawFactoryMock->expects($this->once())->method('create')->willReturn($this->resultRawMock);
197 
199  $controller = $this->objectManager->getObject(
200  \Magento\Customer\Controller\Adminhtml\Index\Viewfile::class,
201  [
202  'context' => $this->contextMock,
203  'urlDecoder' => $this->urlDecoderMock,
204  'resultRawFactory' => $this->resultRawFactoryMock
205  ]
206  );
207  $this->assertSame($this->resultRawMock, $controller->execute());
208  }
209 
214  public function testExecuteInvalidFile()
215  {
216  $file = '../../../app/etc/env.php';
217  $decodedFile = base64_encode($file);
218  $fileName = 'customer/' . $file;
219  $path = 'path';
220 
221  $this->requestMock->expects($this->atLeastOnce())->method('getParam')->with('file')->willReturn($decodedFile);
222 
223  $this->directoryMock->expects($this->once())->method('getAbsolutePath')->with($fileName)->willReturn($path);
224 
225  $this->fileSystemMock->expects($this->once())->method('getDirectoryRead')
226  ->with(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)
227  ->willReturn($this->directoryMock);
228 
229  $this->storage->expects($this->once())->method('processStorageFile')->with($path)->willReturn(false);
230 
231  $this->objectManagerMock->expects($this->any())->method('get')
232  ->willReturnMap(
233  [
234  [\Magento\Framework\Filesystem::class, $this->fileSystemMock],
235  [\Magento\MediaStorage\Helper\File\Storage::class, $this->storage],
236  ]
237  );
238 
239  $this->urlDecoderMock->expects($this->once())->method('decode')->with($decodedFile)->willReturn($file);
240  $fileFactoryMock = $this->createMock(
241  \Magento\Framework\App\Response\Http\FileFactory::class,
242  [],
243  [],
244  '',
245  false
246  );
247 
248  $controller = $this->objectManager->getObject(
249  \Magento\Customer\Controller\Adminhtml\Index\Viewfile::class,
250  [
251  'context' => $this->contextMock,
252  'urlDecoder' => $this->urlDecoderMock,
253  'fileFactory' => $fileFactoryMock,
254  ]
255  );
256  $controller->execute();
257  }
258 }
$fileName
Definition: translate.phtml:15
$controller
Definition: info.phtml:14