53 const URL =
'http://example.com';
57 require_once
__DIR__ .
'/../_files/download_mock.php';
59 self::$functionExists =
true;
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
69 $this->fileReadFactory = $this->createMock(\
Magento\Framework\
Filesystem\File\ReadFactory::class);
71 $this->_helper = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))->getObject(
72 \
Magento\Downloadable\Helper\Download::class,
74 'downloadableFile' => $this->_downloadableFileMock,
75 'filesystem' => $this->_filesystemMock,
76 'session' => $this->sessionManager,
77 'fileReadFactory' => $this->fileReadFactory,
87 $this->_helper->setResource(
'/some/path/../file', DownloadHelper::LINK_TYPE_FILE);
96 $this->_helper->getFileSize();
105 $this->_helper->setResource(self::FILE_PATH,
'The link type is invalid. Verify and try again.');
106 $this->_helper->getFileSize();
112 $this->assertEquals(self::FILE_SIZE, $this->_helper->getFileSize());
118 $this->assertEquals(self::FILE_SIZE, $this->_helper->getFileSize());
128 $this->_helper->getFileSize();
134 $this->_downloadableFileMock->expects($this->never())->method(
'getFileType');
135 $this->assertEquals(self::MIME_TYPE, $this->_helper->getContentType());
144 self::$functionExists = $functionExistsResult;
145 self::$mimeContentType = $mimeContentTypeResult;
147 $this->_downloadableFileMock->expects(
152 $this->returnValue(self::MIME_TYPE)
155 $this->assertEquals(self::MIME_TYPE, $this->_helper->getContentType());
163 return [[
false,
''], [
true,
false]];
169 $this->assertEquals(self::MIME_TYPE, $this->_helper->getContentType());
174 $baseName =
'base_name.file';
175 $path = TESTS_TEMP_DIR .
'/' . $baseName;
177 $this->assertEquals($baseName, $this->_helper->getFilename());
183 $this->assertEquals(
'example.com', $this->_helper->getFilename());
189 $this->
_setupUrlMocks(self::FILE_SIZE, self::URL, [
'disposition' =>
"inline; filename={$fileName}"]);
190 $this->assertEquals(
$fileName, $this->_helper->getFilename());
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);
215 protected function _setupUrlMocks($size = self::FILE_SIZE,
$url = self::URL, $additionalStatData = [])
217 $this->_handleMock->expects(
222 $this->returnValue(array_merge([
'size' => $size,
'type' => self::MIME_TYPE], $additionalStatData))
225 $this->fileReadFactory->expects(
230 $this->returnValue($this->_handleMock)
233 $this->_helper->setResource(
$url, DownloadHelper::LINK_TYPE_URL);
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();
_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__
testGetFileNameUrlWithContentDisposition()
testGetFileSizeInvalidLinkType()
dataProviderForTestGetContentTypeThroughHelper()
testGetFileSizeNoResource()
testGetContentTypeThroughHelper($functionExistsResult, $mimeContentTypeResult)
testSetResourceInvalidPath()