DownloadTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Test\Unit\Helper;
  7. use Magento\Downloadable\Helper\Download as DownloadHelper;
  8. use Magento\Downloadable\Helper\File as DownloadableFile;
  9. use Magento\Framework\App\Filesystem\DirectoryList;
  10. use Magento\Framework\Filesystem;
  11. use Magento\Framework\Filesystem\Directory\ReadInterface as DirReadInterface;
  12. use Magento\Framework\Filesystem\File\ReadInterface as FileReadInterface;
  13. /**
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class DownloadTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /** @var DownloadHelper */
  19. protected $_helper;
  20. /** @var Filesystem|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $_filesystemMock;
  22. /** @var FileReadInterface|\PHPUnit_Framework_MockObject_MockObject */
  23. protected $_handleMock;
  24. /** @var DirReadInterface|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $_workingDirectoryMock;
  26. /** @var DownloadableFile|\PHPUnit_Framework_MockObject_MockObject */
  27. protected $_downloadableFileMock;
  28. /** @var \Magento\Framework\Session\SessionManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  29. protected $sessionManager;
  30. /** @var \Magento\Framework\Filesystem\File\ReadFactory|\PHPUnit_Framework_MockObject_MockObject */
  31. protected $fileReadFactory;
  32. /** @var bool Result of function_exists() */
  33. public static $functionExists;
  34. /** @var string Result of mime_content_type() */
  35. public static $mimeContentType;
  36. const FILE_SIZE = 4096;
  37. const FILE_PATH = '/some/path';
  38. const MIME_TYPE = 'image/png';
  39. const URL = 'http://example.com';
  40. protected function setUp()
  41. {
  42. require_once __DIR__ . '/../_files/download_mock.php';
  43. self::$functionExists = true;
  44. self::$mimeContentType = self::MIME_TYPE;
  45. $this->_filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
  46. $this->_handleMock = $this->createMock(\Magento\Framework\Filesystem\File\ReadInterface::class);
  47. $this->_workingDirectoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
  48. $this->_downloadableFileMock = $this->createMock(\Magento\Downloadable\Helper\File::class);
  49. $this->sessionManager = $this->getMockForAbstractClass(
  50. \Magento\Framework\Session\SessionManagerInterface::class
  51. );
  52. $this->fileReadFactory = $this->createMock(\Magento\Framework\Filesystem\File\ReadFactory::class);
  53. $this->_helper = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject(
  54. \Magento\Downloadable\Helper\Download::class,
  55. [
  56. 'downloadableFile' => $this->_downloadableFileMock,
  57. 'filesystem' => $this->_filesystemMock,
  58. 'session' => $this->sessionManager,
  59. 'fileReadFactory' => $this->fileReadFactory,
  60. ]
  61. );
  62. }
  63. /**
  64. * @expectedException \InvalidArgumentException
  65. */
  66. public function testSetResourceInvalidPath()
  67. {
  68. $this->_helper->setResource('/some/path/../file', DownloadHelper::LINK_TYPE_FILE);
  69. }
  70. /**
  71. * @expectedException \Magento\Framework\Exception\LocalizedException
  72. * @expectedExceptionMessage Please set resource file and link type.
  73. */
  74. public function testGetFileSizeNoResource()
  75. {
  76. $this->_helper->getFileSize();
  77. }
  78. /**
  79. * @expectedException \Magento\Framework\Exception\LocalizedException
  80. * @expectedExceptionMessage Invalid download link type.
  81. */
  82. public function testGetFileSizeInvalidLinkType()
  83. {
  84. $this->_helper->setResource(self::FILE_PATH, 'The link type is invalid. Verify and try again.');
  85. $this->_helper->getFileSize();
  86. }
  87. public function testGetFileSizeUrl()
  88. {
  89. $this->_setupUrlMocks();
  90. $this->assertEquals(self::FILE_SIZE, $this->_helper->getFileSize());
  91. }
  92. public function testGetFileSize()
  93. {
  94. $this->_setupFileMocks();
  95. $this->assertEquals(self::FILE_SIZE, $this->_helper->getFileSize());
  96. }
  97. /**
  98. * @expectedException \Magento\Framework\Exception\LocalizedException
  99. * @expectedExceptionMessage Invalid download link type.
  100. */
  101. public function testGetFileSizeNoFile()
  102. {
  103. $this->_setupFileMocks(false);
  104. $this->_helper->getFileSize();
  105. }
  106. public function testGetContentType()
  107. {
  108. $this->_setupFileMocks();
  109. $this->_downloadableFileMock->expects($this->never())->method('getFileType');
  110. $this->assertEquals(self::MIME_TYPE, $this->_helper->getContentType());
  111. }
  112. /**
  113. * @dataProvider dataProviderForTestGetContentTypeThroughHelper
  114. */
  115. public function testGetContentTypeThroughHelper($functionExistsResult, $mimeContentTypeResult)
  116. {
  117. $this->_setupFileMocks();
  118. self::$functionExists = $functionExistsResult;
  119. self::$mimeContentType = $mimeContentTypeResult;
  120. $this->_downloadableFileMock->expects(
  121. $this->once()
  122. )->method(
  123. 'getFileType'
  124. )->will(
  125. $this->returnValue(self::MIME_TYPE)
  126. );
  127. $this->assertEquals(self::MIME_TYPE, $this->_helper->getContentType());
  128. }
  129. /**
  130. * @return array
  131. */
  132. public function dataProviderForTestGetContentTypeThroughHelper()
  133. {
  134. return [[false, ''], [true, false]];
  135. }
  136. public function testGetContentTypeUrl()
  137. {
  138. $this->_setupUrlMocks();
  139. $this->assertEquals(self::MIME_TYPE, $this->_helper->getContentType());
  140. }
  141. public function testGetFilename()
  142. {
  143. $baseName = 'base_name.file';
  144. $path = TESTS_TEMP_DIR . '/' . $baseName;
  145. $this->_setupFileMocks(true, self::FILE_SIZE, $path);
  146. $this->assertEquals($baseName, $this->_helper->getFilename());
  147. }
  148. public function testGetFileNameUrl()
  149. {
  150. $this->_setupUrlMocks();
  151. $this->assertEquals('example.com', $this->_helper->getFilename());
  152. }
  153. public function testGetFileNameUrlWithContentDisposition()
  154. {
  155. $fileName = 'some_other.file';
  156. $this->_setupUrlMocks(self::FILE_SIZE, self::URL, ['disposition' => "inline; filename={$fileName}"]);
  157. $this->assertEquals($fileName, $this->_helper->getFilename());
  158. }
  159. /**
  160. * @param bool $doesExist
  161. * @param int $size
  162. * @param string $path
  163. */
  164. protected function _setupFileMocks($doesExist = true, $size = self::FILE_SIZE, $path = self::FILE_PATH)
  165. {
  166. $this->_handleMock->expects($this->any())->method('stat')->will($this->returnValue(['size' => $size]));
  167. $this->_downloadableFileMock->expects($this->any())->method('ensureFileInFilesystem')->with($path)
  168. ->will($this->returnValue($doesExist));
  169. $this->_workingDirectoryMock->expects($doesExist ? $this->once() : $this->never())->method('openFile')
  170. ->will($this->returnValue($this->_handleMock));
  171. $this->_filesystemMock->expects($this->any())->method('getDirectoryRead')->with(DirectoryList::MEDIA)
  172. ->will($this->returnValue($this->_workingDirectoryMock));
  173. $this->_helper->setResource($path, DownloadHelper::LINK_TYPE_FILE);
  174. }
  175. /**
  176. * @param int $size
  177. * @param string $url
  178. * @param array $additionalStatData
  179. */
  180. protected function _setupUrlMocks($size = self::FILE_SIZE, $url = self::URL, $additionalStatData = [])
  181. {
  182. $this->_handleMock->expects(
  183. $this->any()
  184. )->method(
  185. 'stat'
  186. )->will(
  187. $this->returnValue(array_merge(['size' => $size, 'type' => self::MIME_TYPE], $additionalStatData))
  188. );
  189. $this->fileReadFactory->expects(
  190. $this->once()
  191. )->method(
  192. 'create'
  193. )->will(
  194. $this->returnValue($this->_handleMock)
  195. );
  196. $this->_helper->setResource($url, DownloadHelper::LINK_TYPE_URL);
  197. }
  198. public function testOutput()
  199. {
  200. $this->sessionManager
  201. ->expects($this->once())->method('writeClose');
  202. $this->_setupUrlMocks(self::FILE_SIZE, self::URL, ['disposition' => "inline; filename=test.txt"]);
  203. $this->_helper->output();
  204. }
  205. }