Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StorageTest.php
Go to the documentation of this file.
1 <?php
11 
13 
17 class StorageTest extends \PHPUnit\Framework\TestCase
18 {
22  protected $filesystem;
23 
27  protected $session;
28 
32  protected $themeFactory;
33 
37  protected $request;
38 
42  protected $helper;
43 
47  protected $customizationPath;
48 
52  protected $directoryWrite;
53 
57  protected $contextHelper;
58 
62  protected $theme;
63 
67  protected $customization;
68 
72  protected $urlEncoder;
73 
77  protected $urlDecoder;
78 
79  protected $requestParams;
80 
81  protected function setUp()
82  {
83  $this->customizationPath = '/' . implode('/', ['var', 'theme']);
84 
85  $this->request = $this->createMock(\Magento\Framework\App\Request\Http::class);
86  $this->filesystem = $this->createMock(\Magento\Framework\Filesystem::class);
87  $this->session = $this->createMock(\Magento\Backend\Model\Session::class);
88  $this->contextHelper = $this->createMock(\Magento\Framework\App\Helper\Context::class);
89  $this->directoryWrite = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
90  $this->themeFactory = $this->createMock(\Magento\Framework\View\Design\Theme\FlyweightFactory::class);
91  $this->theme = $this->createMock(\Magento\Theme\Model\Theme::class);
92  $this->customization = $this->createMock(\Magento\Framework\View\Design\Theme\Customization::class);
93 
94  $this->filesystem->expects($this->any())
95  ->method('getDirectoryWrite')
96  ->will($this->returnValue($this->directoryWrite));
97  $this->urlEncoder = $this->getMockBuilder(\Magento\Framework\Url\EncoderInterface::class)->getMock();
98  $this->urlDecoder = $this->getMockBuilder(\Magento\Framework\Url\DecoderInterface::class)->getMock();
99 
100  $this->directoryWrite->expects($this->any())->method('create')->willReturn(true);
101  $this->contextHelper->expects($this->any())->method('getRequest')->willReturn($this->request);
102  $this->contextHelper->expects($this->any())->method('getUrlEncoder')->willReturn($this->urlEncoder);
103  $this->contextHelper->expects($this->any())->method('getUrlDecoder')->willReturn($this->urlDecoder);
104  $this->themeFactory->expects($this->any())->method('create')->willReturn($this->theme);
105 
106  $this->theme->expects($this->any())
107  ->method('getCustomization')
108  ->will($this->returnValue($this->customization));
109 
110  $this->request->expects($this->at(0))
111  ->method('getParam')
112  ->with(\Magento\Theme\Helper\Storage::PARAM_THEME_ID)
113  ->will($this->returnValue(6));
114  $this->request->expects($this->at(1))
115  ->method('getParam')
116  ->with(\Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE)
117  ->will($this->returnValue(\Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE));
118 
119  $this->helper = new \Magento\Theme\Helper\Storage(
120  $this->contextHelper,
121  $this->filesystem,
122  $this->session,
123  $this->themeFactory
124  );
125  }
126 
127  protected function tearDown()
128  {
129  $this->request = null;
130  $this->filesystem = null;
131  $this->session = null;
132  $this->contextHelper = null;
133  $this->directoryWrite = null;
134  $this->themeFactory = null;
135  $this->theme = null;
136  $this->customization = null;
137  }
138 
143  public function testGetShortFilename()
144  {
145  $longFileName = 'veryLongFileNameMoreThanTwenty';
146  $expectedFileName = 'veryLongFileNameMore...';
147  $this->assertEquals($expectedFileName, $this->helper->getShortFilename($longFileName, 20));
148  }
149 
150  public function testGetStorageRoot()
151  {
152  $expectedStorageRoot = '/' . \Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE;
153  $this->assertEquals($expectedStorageRoot, $this->helper->getStorageRoot());
154  }
155 
156  public function testGetThumbnailDirectory()
157  {
158  $imagePath = implode('/', ['root', 'image', 'image_name.jpg']);
159  $thumbnailDir = implode(
160  '/',
161  ['root', 'image', \Magento\Theme\Model\Wysiwyg\Storage::THUMBNAIL_DIRECTORY]
162  );
163 
164  $this->assertEquals($thumbnailDir, $this->helper->getThumbnailDirectory($imagePath));
165  }
166 
167  public function testGetThumbnailPath()
168  {
169  $image = 'image_name.jpg';
170  $thumbnailPath = '/' . implode(
171  '/',
172  [
173  \Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE,
174  \Magento\Theme\Model\Wysiwyg\Storage::THUMBNAIL_DIRECTORY,
175  $image
176  ]
177  );
178 
179  $this->customization->expects(
180  $this->any()
181  )->method(
182  'getCustomizationPath'
183  )->will(
184  $this->returnValue($this->customizationPath)
185  );
186 
187  $this->directoryWrite->expects($this->any())->method('isExist')->will($this->returnValue(true));
188 
189  $this->assertEquals($thumbnailPath, $this->helper->getThumbnailPath($image));
190  }
191 
192  public function testGetRequestParams()
193  {
194  $this->request->expects(
195  $this->at(0)
196  )->method(
197  'getParam'
198  )->with(
199  \Magento\Theme\Helper\Storage::PARAM_THEME_ID
200  )->will(
201  $this->returnValue(6)
202  );
203  $this->request->expects(
204  $this->at(1)
205  )->method(
206  'getParam'
207  )->with(
209  )->will(
210  $this->returnValue('image')
211  );
212  $this->request->expects(
213  $this->at(2)
214  )->method(
215  'getParam'
216  )->with(
217  \Magento\Theme\Helper\Storage::PARAM_NODE
218  )->will(
219  $this->returnValue('node')
220  );
221 
222  $expectedResult = [
226  ];
227  $this->assertEquals($expectedResult, $this->helper->getRequestParams());
228  }
229 
231  {
232  $this->request->expects(
233  $this->at(0)
234  )->method(
235  'getParam'
236  )->with(
238  )->will(
239  $this->returnValue(\Magento\Theme\Model\Wysiwyg\Storage::TYPE_FONT)
240  );
241 
242  $this->request->expects(
243  $this->at(1)
244  )->method(
245  'getParam'
246  )->with(
248  )->will(
249  $this->returnValue(\Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE)
250  );
251 
252  $fontTypes = $this->helper->getAllowedExtensionsByType();
253  $this->assertEquals(['ttf', 'otf', 'eot', 'svg', 'woff'], $fontTypes);
254 
255  $imagesTypes = $this->helper->getAllowedExtensionsByType();
256  $this->assertEquals(['jpg', 'jpeg', 'gif', 'png', 'xbm', 'wbmp'], $imagesTypes);
257  }
258 
266  {
267  $image = 'notFoundImage.png';
268  $root = '/image';
269  $sourceNode = '/not/a/root';
270  $node = base64_encode($sourceNode);
271  $this->request->expects($this->at(0))
272  ->method('getParam')
273  ->willReturnMap(
274  [
275  [
276  \Magento\Theme\Helper\Storage::PARAM_THEME_ID,
277  null,
278  6,
279  ],
280  [
281  \Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE,
282  null,
283  \Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE
284  ],
285  [
286  \Magento\Theme\Helper\Storage::PARAM_NODE,
287  null,
288  $node
289  ],
290  ]
291  );
292  $this->urlDecoder->expects($this->once())
293  ->method('decode')
294  ->with($node)
295  ->willReturnCallback(function ($path) {
296  return base64_decode($path);
297  });
298  $this->directoryWrite->expects($this->once())
299  ->method('isDirectory')
300  ->with($root . $sourceNode)
301  ->willReturn(true);
302  $this->directoryWrite->expects($this->once())
303  ->method('getRelativePath')
304  ->with($root . $sourceNode)
305  ->willReturn($sourceNode);
306  $this->directoryWrite->expects($this->once())
307  ->method('isExist')
308  ->with($sourceNode . '/' . $image);
309 
310  $this->helper->getThumbnailPath($image);
311  }
312 
318  {
319  $path = '/image/path/to';
320  $this->urlEncoder->expects($this->once())
321  ->method('encode')
322  ->with('/path/to')
323  ->willReturnCallback(function ($path) {
324  return base64_encode($path);
325  });
326  $this->urlDecoder->expects($this->once())
327  ->method('decode')
328  ->with(base64_encode('/path/to'))
329  ->willReturnCallback(function ($path) {
330  return base64_decode($path);
331  });
332 
333  $value = $this->helper->convertPathToId($path);
334  $this->assertEquals(base64_encode('/path/to'), $value);
335  $this->assertEquals($path, $this->helper->convertIdToPath($value));
336  }
337 
338  public function testGetSession()
339  {
340  $this->assertInstanceOf(\Magento\Backend\Model\Session::class, $this->helper->getSession());
341  }
342 
343  public function testGetRelativeUrl()
344  {
345  $filename = base64_encode('filename.ext');
346  $notRoot = base64_encode('not/a/root');
347  $this->request->expects($this->any())
348  ->method('getParam')
349  ->willReturnMap(
350  [
351  'type' => [
352  \Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE,
353  null,
354  \Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE,
355  ],
356  'node' => [
357  \Magento\Theme\Helper\Storage::PARAM_NODE,
358  null,
359  $notRoot,
360  ],
361  'filenaem' => [
362  \Magento\Theme\Helper\Storage::PARAM_FILENAME,
363  null,
364  $filename,
365  ],
366  ]
367  );
368  $decode = function ($value) {
369  return base64_decode($value);
370  };
371  $this->urlDecoder->expects($this->at(0))
372  ->method('decode')
373  ->with($notRoot)
374  ->willReturnCallback($decode);
375  $this->urlDecoder->expects($this->at(1))
376  ->method('decode')
377  ->with($filename)
378  ->willReturnCallback($decode);
379 
380  $this->assertEquals(
381  '../image/not/a/root/filename.ext',
382  $this->helper->getRelativeUrl()
383  );
384  }
385 
390  {
391  return [
394  ];
395  }
396 
405  {
406  $this->request->expects($this->once())
407  ->method('getParam')
408  ->with(\Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE)
409  ->willReturn($type);
410 
411  $this->assertEquals($name, $this->helper->getStorageTypeName());
412  }
413 
421  {
422  $this->helper->getStorageTypeName();
423  }
424 
431  public function testGetThemeNotFound()
432  {
433  $this->themeFactory->expects($this->once())
434  ->method('create')
435  ->willReturn(null);
436  $helper = new \Magento\Theme\Helper\Storage(
437  $this->contextHelper,
438  $this->filesystem,
439  $this->session,
440  $this->themeFactory
441  );
442  $helper->getStorageRoot();
443  }
444 }
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14