FileTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Test\Unit\Model\Design\Backend;
  7. use Magento\Framework\UrlInterface;
  8. use Magento\Theme\Model\Design\Backend\File;
  9. use Magento\Framework\App\Filesystem\DirectoryList;
  10. /**
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class FileTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /** @var \Magento\Framework\Filesystem\Directory\WriteInterface|\PHPUnit_Framework_MockObject_MockObject */
  16. protected $mediaDirectory;
  17. /** @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject */
  18. protected $urlBuilder;
  19. /** @var File */
  20. protected $fileBackend;
  21. /**
  22. * @var \Magento\Framework\File\Mime|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $mime;
  25. public function setUp()
  26. {
  27. $context = $this->getMockObject(\Magento\Framework\Model\Context::class);
  28. $registry = $this->getMockObject(\Magento\Framework\Registry::class);
  29. $config = $this->getMockObjectForAbstractClass(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  30. $cacheTypeList = $this->getMockObjectForAbstractClass(\Magento\Framework\App\Cache\TypeListInterface::class);
  31. $uploaderFactory = $this->getMockObject(\Magento\MediaStorage\Model\File\UploaderFactory::class, ['create']);
  32. $requestData = $this->getMockObjectForAbstractClass(
  33. \Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface::class
  34. );
  35. $filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->mediaDirectory = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\WriteInterface::class)
  39. ->getMockForAbstractClass();
  40. $filesystem->expects($this->once())
  41. ->method('getDirectoryWrite')
  42. ->with(DirectoryList::MEDIA)
  43. ->willReturn($this->mediaDirectory);
  44. $this->urlBuilder = $this->getMockBuilder(\Magento\Framework\UrlInterface::class)
  45. ->getMockForAbstractClass();
  46. $this->mime = $this->getMockBuilder(\Magento\Framework\File\Mime::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->fileBackend = new File(
  50. $context,
  51. $registry,
  52. $config,
  53. $cacheTypeList,
  54. $uploaderFactory,
  55. $requestData,
  56. $filesystem,
  57. $this->urlBuilder
  58. );
  59. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  60. $objectManager->setBackwardCompatibleProperty(
  61. $this->fileBackend,
  62. 'mime',
  63. $this->mime
  64. );
  65. }
  66. public function tearDown()
  67. {
  68. unset($this->fileBackend);
  69. }
  70. /**
  71. * @param string $class
  72. * @param array $methods
  73. * @return \PHPUnit_Framework_MockObject_MockObject
  74. */
  75. protected function getMockObject($class, $methods = [])
  76. {
  77. $builder = $this->getMockBuilder($class)
  78. ->disableOriginalConstructor();
  79. if (count($methods)) {
  80. $builder->setMethods($methods);
  81. }
  82. return $builder->getMock();
  83. }
  84. /**
  85. * @param string $class
  86. * @return \PHPUnit_Framework_MockObject_MockObject
  87. */
  88. protected function getMockObjectForAbstractClass($class)
  89. {
  90. return $this->getMockBuilder($class)
  91. ->getMockForAbstractClass();
  92. }
  93. public function testAfterLoad()
  94. {
  95. $value = 'filename.jpg';
  96. $mime = 'image/jpg';
  97. $absoluteFilePath = '/absolute_path/' . $value;
  98. $this->fileBackend->setValue($value);
  99. $this->fileBackend->setFieldConfig(
  100. [
  101. 'upload_dir' => [
  102. 'value' => 'value',
  103. 'config' => 'system/filesystem/media',
  104. ],
  105. 'base_url' => [
  106. 'type' => 'media',
  107. 'value' => 'design/file'
  108. ],
  109. ]
  110. );
  111. $this->mediaDirectory->expects($this->once())
  112. ->method('isExist')
  113. ->with('value/' . $value)
  114. ->willReturn(true);
  115. $this->mediaDirectory->expects($this->once())
  116. ->method('getAbsolutePath')
  117. ->with('value/' . $value)
  118. ->willReturn($absoluteFilePath);
  119. $this->urlBuilder->expects($this->once())
  120. ->method('getBaseUrl')
  121. ->with(['_type' => UrlInterface::URL_TYPE_MEDIA])
  122. ->willReturn('http://magento2.com/pub/media/');
  123. $this->mediaDirectory->expects($this->once())
  124. ->method('getRelativePath')
  125. ->with('value')
  126. ->willReturn('value');
  127. $this->mediaDirectory->expects($this->once())
  128. ->method('stat')
  129. ->with('value/' . $value)
  130. ->willReturn(['size' => 234234]);
  131. $this->mime->expects($this->once())
  132. ->method('getMimeType')
  133. ->with($absoluteFilePath)
  134. ->willReturn($mime);
  135. $this->fileBackend->afterLoad();
  136. $this->assertEquals(
  137. [
  138. [
  139. 'url' => 'http://magento2.com/pub/media/design/file/' . $value,
  140. 'file' => $value,
  141. 'size' => 234234,
  142. 'exists' => true,
  143. 'name' => $value,
  144. 'type' => $mime,
  145. ]
  146. ],
  147. $this->fileBackend->getValue()
  148. );
  149. }
  150. /**
  151. * @dataProvider beforeSaveDataProvider
  152. * @param string $fileName
  153. */
  154. public function testBeforeSave($fileName)
  155. {
  156. $expectedFileName = basename($fileName);
  157. $expectedTmpMediaPath = 'tmp/design/file/' . $expectedFileName;
  158. $this->fileBackend->setScope('store');
  159. $this->fileBackend->setScopeId(1);
  160. $this->fileBackend->setValue(
  161. [
  162. [
  163. 'url' => 'http://magento2.com/pub/media/tmp/image/' . $fileName,
  164. 'file' => $fileName,
  165. 'size' => 234234,
  166. ]
  167. ]
  168. );
  169. $this->fileBackend->setFieldConfig(
  170. [
  171. 'upload_dir' => [
  172. 'value' => 'value',
  173. 'config' => 'system/filesystem/media',
  174. ],
  175. ]
  176. );
  177. $this->mediaDirectory->expects($this->once())
  178. ->method('copyFile')
  179. ->with($expectedTmpMediaPath, '/' . $expectedFileName)
  180. ->willReturn(true);
  181. $this->mediaDirectory->expects($this->once())
  182. ->method('delete')
  183. ->with($expectedTmpMediaPath);
  184. $this->fileBackend->beforeSave();
  185. $this->assertEquals($expectedFileName, $this->fileBackend->getValue());
  186. }
  187. /**
  188. * @return array
  189. */
  190. public function beforeSaveDataProvider()
  191. {
  192. return [
  193. 'Normal file name' => ['filename.jpg'],
  194. 'Vulnerable file name' => ['../../../../../../../../etc/passwd'],
  195. ];
  196. }
  197. /**
  198. * @expectedException \Magento\Framework\Exception\LocalizedException
  199. * @expectedExceptionMessage header_logo_src does not contain field 'file'
  200. */
  201. public function testBeforeSaveWithoutFile()
  202. {
  203. $this->fileBackend->setData(
  204. [
  205. 'value' => [
  206. 'test' => ''
  207. ],
  208. 'field_config' => [
  209. 'field' => 'header_logo_src'
  210. ],
  211. ]
  212. );
  213. $this->fileBackend->beforeSave();
  214. }
  215. public function testBeforeSaveWithExistingFile()
  216. {
  217. $value = 'filename.jpg';
  218. $this->fileBackend->setValue(
  219. [
  220. [
  221. 'url' => 'http://magento2.com/pub/media/tmp/image/' . $value,
  222. 'file' => $value,
  223. 'size' => 234234,
  224. 'exists' => true
  225. ]
  226. ]
  227. );
  228. $this->fileBackend->beforeSave();
  229. $this->assertEquals(
  230. $value,
  231. $this->fileBackend->getValue()
  232. );
  233. }
  234. }