FileTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Test\Unit\Model\Config\Backend;
  7. use Magento\Config\Model\Config\Backend\File;
  8. use Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface;
  9. use Magento\Framework\App\Cache\TypeListInterface;
  10. use Magento\Framework\App\Config\ScopeConfigInterface;
  11. use Magento\Framework\App\Filesystem\DirectoryList;
  12. use Magento\Framework\Filesystem;
  13. use Magento\Framework\Filesystem\Directory\WriteInterface;
  14. use Magento\Framework\Model\Context;
  15. use Magento\Framework\Registry;
  16. use Magento\MediaStorage\Model\File\Uploader;
  17. use Magento\MediaStorage\Model\File\UploaderFactory;
  18. /**
  19. * Class FileTest
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. */
  22. class FileTest extends \PHPUnit\Framework\TestCase
  23. {
  24. /** @var File */
  25. protected $model;
  26. /** @var Context|\PHPUnit_Framework_MockObject_MockObject */
  27. protected $contextMock;
  28. /** @var Registry|\PHPUnit_Framework_MockObject_MockObject */
  29. protected $registryMock;
  30. /** @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
  31. protected $scopeConfigMock;
  32. /** @var TypeListInterface|\PHPUnit_Framework_MockObject_MockObject */
  33. protected $typeListMock;
  34. /** @var UploaderFactory|\PHPUnit_Framework_MockObject_MockObject */
  35. protected $uploaderFactoryMock;
  36. /** @var RequestDataInterface|\PHPUnit_Framework_MockObject_MockObject */
  37. protected $requestDataMock;
  38. /** @var Filesystem|\PHPUnit_Framework_MockObject_MockObject */
  39. protected $filesystemMock;
  40. /** @var WriteInterface|\PHPUnit_Framework_MockObject_MockObject */
  41. protected $writeMock;
  42. /** @var Uploader|\PHPUnit_Framework_MockObject_MockObject */
  43. protected $uploaderMock;
  44. protected function setUp()
  45. {
  46. $this->contextMock = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $this->registryMock = $this->getMockBuilder(\Magento\Framework\Registry::class)
  50. ->getMockForAbstractClass();
  51. $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
  52. ->getMockForAbstractClass();
  53. $this->typeListMock = $this->getMockBuilder(\Magento\Framework\App\Cache\TypeListInterface::class)
  54. ->getMockForAbstractClass();
  55. $this->uploaderFactoryMock = $this->getMockBuilder(\Magento\MediaStorage\Model\File\UploaderFactory::class)
  56. ->disableOriginalConstructor()
  57. ->setMethods(['create'])
  58. ->getMock();
  59. $this->requestDataMock
  60. = $this->getMockBuilder(\Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface::class)
  61. ->getMockForAbstractClass();
  62. $this->filesystemMock = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->writeMock = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\WriteInterface::class)
  66. ->getMock();
  67. $this->uploaderMock = $this->getMockBuilder(\Magento\MediaStorage\Model\File\Uploader::class)
  68. ->disableOriginalConstructor()
  69. ->getMock();
  70. $this->uploaderFactoryMock->expects($this->any())
  71. ->method('create')
  72. ->willReturn($this->uploaderMock);
  73. $this->filesystemMock->expects($this->once())
  74. ->method('getDirectoryWrite')
  75. ->with(DirectoryList::MEDIA)
  76. ->willReturn($this->writeMock);
  77. $this->model = new File(
  78. $this->contextMock,
  79. $this->registryMock,
  80. $this->scopeConfigMock,
  81. $this->typeListMock,
  82. $this->uploaderFactoryMock,
  83. $this->requestDataMock,
  84. $this->filesystemMock
  85. );
  86. }
  87. public function testBeforeSave()
  88. {
  89. $value = 'value';
  90. $groupId = 1;
  91. $field = 'field';
  92. $tmpFileName = 'tmp_file_name';
  93. $name = 'name';
  94. $path = 'path';
  95. $scope = 'scope';
  96. $scopeId = 2;
  97. $uploadDir = 'upload_dir';
  98. $uploadDirData = [
  99. 'scope_info' => 1,
  100. 'value' => $uploadDir,
  101. ];
  102. $fieldConfig = [
  103. 'upload_dir' => $uploadDirData,
  104. ];
  105. $fileData = [
  106. 'tmp_name' => $tmpFileName,
  107. 'name' => $name,
  108. ];
  109. $fileName = 'file_name';
  110. $result = [
  111. 'file' => $fileName,
  112. ];
  113. $this->model->setValue($value);
  114. $this->model->setPath($path);
  115. $this->model->setScope($scope);
  116. $this->model->setScopeId($scopeId);
  117. $this->model->setFieldConfig($fieldConfig);
  118. $_FILES['groups']['tmp_name'][$groupId]['fields'][$field]['value'] = $tmpFileName;
  119. $this->requestDataMock->expects($this->once())
  120. ->method('getTmpName')
  121. ->with($path)
  122. ->willReturn($tmpFileName);
  123. $this->requestDataMock->expects($this->once())
  124. ->method('getName')
  125. ->with($path)
  126. ->willReturn($name);
  127. $this->uploaderFactoryMock->expects($this->any())
  128. ->method('create')
  129. ->with(['fileId' => $fileData])
  130. ->willReturn($this->uploaderMock);
  131. $this->uploaderMock->expects($this->once())
  132. ->method('save')
  133. ->with($uploadDir . '/' . $scope . '/' . $scopeId, null)
  134. ->willReturn($result);
  135. $this->assertEquals($this->model, $this->model->beforeSave());
  136. $this->assertEquals($this->model->getValue(), $scope . '/' . $scopeId . '/' . $fileName);
  137. }
  138. public function testBeforeWithoutRequest()
  139. {
  140. $tmpFileName = 'tmp_file_name';
  141. $value = ['tmp_name' => $tmpFileName, 'name' => 'name'];
  142. $name = 'name';
  143. $path = 'path';
  144. $scope = 'scope';
  145. $scopeId = 2;
  146. $uploadDir = 'upload_dir';
  147. $fieldConfig = [
  148. 'upload_dir' => $uploadDir,
  149. ];
  150. $fileData = [
  151. 'tmp_name' => $tmpFileName,
  152. 'name' => $name,
  153. ];
  154. $fileName = 'file_name';
  155. $result = [
  156. 'file' => $fileName,
  157. ];
  158. $this->model->setValue($value);
  159. $this->model->setPath($path);
  160. $this->model->setScope($scope);
  161. $this->model->setScopeId($scopeId);
  162. $this->model->setFieldConfig($fieldConfig);
  163. $this->requestDataMock->expects($this->once())
  164. ->method('getTmpName')
  165. ->with($path)
  166. ->willReturn('');
  167. $this->uploaderFactoryMock->expects($this->any())
  168. ->method('create')
  169. ->with(['fileId' => $fileData])
  170. ->willReturn($this->uploaderMock);
  171. $this->uploaderMock->expects($this->once())
  172. ->method('save')
  173. ->with($uploadDir, null)
  174. ->willReturn($result);
  175. $this->assertEquals($this->model, $this->model->beforeSave());
  176. $this->assertEquals($this->model->getValue(), $fileName);
  177. }
  178. public function testBeforeWithoutFile()
  179. {
  180. $value = ['name' => 'name'];
  181. $path = 'path';
  182. $uploadDir = 'upload_dir';
  183. $uploadDirData = [
  184. 'scope_info' => 1,
  185. 'value' => $uploadDir,
  186. ];
  187. $fieldConfig = [
  188. 'upload_dir' => $uploadDirData,
  189. ];
  190. $this->model->setValue($value);
  191. $this->model->setPath($path);
  192. $this->model->setFieldConfig($fieldConfig);
  193. $this->requestDataMock->expects($this->once())
  194. ->method('getTmpName')
  195. ->with($path)
  196. ->willReturn('');
  197. $this->assertEquals($this->model, $this->model->beforeSave());
  198. $this->assertEquals($this->model->getValue(), null);
  199. }
  200. public function testBeforeWithDelete()
  201. {
  202. $value = ['delete' => 1];
  203. $path = 'path';
  204. $uploadDir = 'upload_dir';
  205. $uploadDirData = [
  206. 'scope_info' => 1,
  207. 'value' => $uploadDir,
  208. ];
  209. $fieldConfig = [
  210. 'upload_dir' => $uploadDirData,
  211. ];
  212. $this->model->setValue($value);
  213. $this->model->setPath($path);
  214. $this->model->setFieldConfig($fieldConfig);
  215. $this->requestDataMock->expects($this->once())
  216. ->method('getTmpName')
  217. ->with($path)
  218. ->willReturn('');
  219. $this->assertEquals($this->model, $this->model->beforeSave());
  220. $this->assertEquals($this->model->getValue(), '');
  221. }
  222. /**
  223. * @expectedException \Magento\Framework\Exception\LocalizedException
  224. * @expectedExceptionMessage Exception!
  225. */
  226. public function testBeforeSaveWithException()
  227. {
  228. $value = 'value';
  229. $groupId = 1;
  230. $field = 'field';
  231. $tmpFileName = 'tmp_file_name';
  232. $name = 'name';
  233. $path = 'path';
  234. $scope = 'scope';
  235. $scopeId = 2;
  236. $uploadDir = 'upload_dir';
  237. $fieldConfig = [
  238. 'upload_dir' => $uploadDir,
  239. ];
  240. $fileData = [
  241. 'tmp_name' => $tmpFileName,
  242. 'name' => $name,
  243. ];
  244. $exception = 'Exception!';
  245. $this->model->setValue($value);
  246. $this->model->setPath($path);
  247. $this->model->setScope($scope);
  248. $this->model->setScopeId($scopeId);
  249. $this->model->setFieldConfig($fieldConfig);
  250. $_FILES['groups']['tmp_name'][$groupId]['fields'][$field]['value'] = $tmpFileName;
  251. $this->requestDataMock->expects($this->once())
  252. ->method('getTmpName')
  253. ->with($path)
  254. ->willReturn($tmpFileName);
  255. $this->requestDataMock->expects($this->once())
  256. ->method('getName')
  257. ->with($path)
  258. ->willReturn($name);
  259. $this->uploaderFactoryMock->expects($this->any())
  260. ->method('create')
  261. ->with(['fileId' => $fileData])
  262. ->willReturn($this->uploaderMock);
  263. $this->uploaderMock->expects($this->once())
  264. ->method('save')
  265. ->with($uploadDir, null)
  266. ->willThrowException(new \Exception($exception));
  267. $this->model->beforeSave();
  268. }
  269. }