Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DownloadTest.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Downloadable\Helper\Download as DownloadHelper;
9 use Magento\Downloadable\Helper\File as DownloadableFile;
14 
18 class DownloadTest extends \PHPUnit\Framework\TestCase
19 {
21  protected $_helper;
22 
24  protected $_filesystemMock;
25 
27  protected $_handleMock;
28 
31 
34 
36  protected $sessionManager;
37 
39  protected $fileReadFactory;
40 
42  public static $functionExists;
43 
45  public static $mimeContentType;
46 
47  const FILE_SIZE = 4096;
48 
49  const FILE_PATH = '/some/path';
50 
51  const MIME_TYPE = 'image/png';
52 
53  const URL = 'http://example.com';
54 
55  protected function setUp()
56  {
57  require_once __DIR__ . '/../_files/download_mock.php';
58 
59  self::$functionExists = true;
60  self::$mimeContentType = self::MIME_TYPE;
61 
62  $this->_filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
63  $this->_handleMock = $this->createMock(\Magento\Framework\Filesystem\File\ReadInterface::class);
64  $this->_workingDirectoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
65  $this->_downloadableFileMock = $this->createMock(\Magento\Downloadable\Helper\File::class);
66  $this->sessionManager = $this->getMockForAbstractClass(
67  \Magento\Framework\Session\SessionManagerInterface::class
68  );
69  $this->fileReadFactory = $this->createMock(\Magento\Framework\Filesystem\File\ReadFactory::class);
70 
71  $this->_helper = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject(
72  \Magento\Downloadable\Helper\Download::class,
73  [
74  'downloadableFile' => $this->_downloadableFileMock,
75  'filesystem' => $this->_filesystemMock,
76  'session' => $this->sessionManager,
77  'fileReadFactory' => $this->fileReadFactory,
78  ]
79  );
80  }
81 
85  public function testSetResourceInvalidPath()
86  {
87  $this->_helper->setResource('/some/path/../file', DownloadHelper::LINK_TYPE_FILE);
88  }
89 
94  public function testGetFileSizeNoResource()
95  {
96  $this->_helper->getFileSize();
97  }
98 
104  {
105  $this->_helper->setResource(self::FILE_PATH, 'The link type is invalid. Verify and try again.');
106  $this->_helper->getFileSize();
107  }
108 
109  public function testGetFileSizeUrl()
110  {
111  $this->_setupUrlMocks();
112  $this->assertEquals(self::FILE_SIZE, $this->_helper->getFileSize());
113  }
114 
115  public function testGetFileSize()
116  {
117  $this->_setupFileMocks();
118  $this->assertEquals(self::FILE_SIZE, $this->_helper->getFileSize());
119  }
120 
125  public function testGetFileSizeNoFile()
126  {
127  $this->_setupFileMocks(false);
128  $this->_helper->getFileSize();
129  }
130 
131  public function testGetContentType()
132  {
133  $this->_setupFileMocks();
134  $this->_downloadableFileMock->expects($this->never())->method('getFileType');
135  $this->assertEquals(self::MIME_TYPE, $this->_helper->getContentType());
136  }
137 
141  public function testGetContentTypeThroughHelper($functionExistsResult, $mimeContentTypeResult)
142  {
143  $this->_setupFileMocks();
144  self::$functionExists = $functionExistsResult;
145  self::$mimeContentType = $mimeContentTypeResult;
146 
147  $this->_downloadableFileMock->expects(
148  $this->once()
149  )->method(
150  'getFileType'
151  )->will(
152  $this->returnValue(self::MIME_TYPE)
153  );
154 
155  $this->assertEquals(self::MIME_TYPE, $this->_helper->getContentType());
156  }
157 
162  {
163  return [[false, ''], [true, false]];
164  }
165 
166  public function testGetContentTypeUrl()
167  {
168  $this->_setupUrlMocks();
169  $this->assertEquals(self::MIME_TYPE, $this->_helper->getContentType());
170  }
171 
172  public function testGetFilename()
173  {
174  $baseName = 'base_name.file';
175  $path = TESTS_TEMP_DIR . '/' . $baseName;
176  $this->_setupFileMocks(true, self::FILE_SIZE, $path);
177  $this->assertEquals($baseName, $this->_helper->getFilename());
178  }
179 
180  public function testGetFileNameUrl()
181  {
182  $this->_setupUrlMocks();
183  $this->assertEquals('example.com', $this->_helper->getFilename());
184  }
185 
187  {
188  $fileName = 'some_other.file';
189  $this->_setupUrlMocks(self::FILE_SIZE, self::URL, ['disposition' => "inline; filename={$fileName}"]);
190  $this->assertEquals($fileName, $this->_helper->getFilename());
191  }
192 
198  protected function _setupFileMocks($doesExist = true, $size = self::FILE_SIZE, $path = self::FILE_PATH)
199  {
200  $this->_handleMock->expects($this->any())->method('stat')->will($this->returnValue(['size' => $size]));
201  $this->_downloadableFileMock->expects($this->any())->method('ensureFileInFilesystem')->with($path)
202  ->will($this->returnValue($doesExist));
203  $this->_workingDirectoryMock->expects($doesExist ? $this->once() : $this->never())->method('openFile')
204  ->will($this->returnValue($this->_handleMock));
205  $this->_filesystemMock->expects($this->any())->method('getDirectoryRead')->with(DirectoryList::MEDIA)
206  ->will($this->returnValue($this->_workingDirectoryMock));
207  $this->_helper->setResource($path, DownloadHelper::LINK_TYPE_FILE);
208  }
209 
215  protected function _setupUrlMocks($size = self::FILE_SIZE, $url = self::URL, $additionalStatData = [])
216  {
217  $this->_handleMock->expects(
218  $this->any()
219  )->method(
220  'stat'
221  )->will(
222  $this->returnValue(array_merge(['size' => $size, 'type' => self::MIME_TYPE], $additionalStatData))
223  );
224 
225  $this->fileReadFactory->expects(
226  $this->once()
227  )->method(
228  'create'
229  )->will(
230  $this->returnValue($this->_handleMock)
231  );
232 
233  $this->_helper->setResource($url, DownloadHelper::LINK_TYPE_URL);
234  }
235 
236  public function testOutput()
237  {
238  $this->sessionManager
239  ->expects($this->once())->method('writeClose');
240  $this->_setupUrlMocks(self::FILE_SIZE, self::URL, ['disposition' => "inline; filename=test.txt"]);
241  $this->_helper->output();
242  }
243 }
_setupUrlMocks($size=self::FILE_SIZE, $url=self::URL, $additionalStatData=[])
_setupFileMocks($doesExist=true, $size=self::FILE_SIZE, $path=self::FILE_PATH)
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$fileName
Definition: translate.phtml:15
testGetContentTypeThroughHelper($functionExistsResult, $mimeContentTypeResult)