123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * Test for uploader service
- */
- namespace Magento\Theme\Test\Unit\Model\Uploader;
- use Magento\Framework\Convert\DataSize;
- class ServiceTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Theme\Model\Uploader\Service
- */
- protected $_service;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\MediaStorage\Model\File\Uploader
- */
- protected $_uploader;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\MediaStorage\Model\File\UploaderFactory
- */
- protected $_uploaderFactory;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\File\Size
- */
- protected $_fileSizeMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Convert\DataSize
- */
- protected $dataSize;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Filesystem
- */
- protected $_filesystemMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Filesystem\Directory\Read
- */
- protected $_directoryMock;
- /**
- * @var int
- */
- const MB_MULTIPLIER = 1048576;
- protected function setUp()
- {
- $this->_uploader = $this->createMock(\Magento\MediaStorage\Model\File\Uploader::class);
- $this->dataSize = new DataSize();
- $this->_uploaderFactory = $this->createPartialMock(
- \Magento\MediaStorage\Model\File\UploaderFactory::class,
- ['create']
- );
- $this->_uploaderFactory->expects($this->any())->method('create')->will($this->returnValue($this->_uploader));
- $this->_directoryMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Read::class);
- $this->_filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
- $this->_filesystemMock->expects(
- $this->any()
- )->method(
- 'getDirectoryRead'
- )->will(
- $this->returnValue($this->_directoryMock)
- );
- /** @var $service \Magento\Theme\Model\Uploader\Service */
- $this->_fileSizeMock = $this->getMockBuilder(
- \Magento\Framework\File\Size::class
- )->setMethods(
- ['getMaxFileSize']
- )->disableOriginalConstructor()->getMock();
- $this->_fileSizeMock->expects(
- $this->any()
- )->method(
- 'getMaxFileSize'
- )->will(
- $this->returnValue(600 * self::MB_MULTIPLIER)
- );
- }
- protected function tearDown()
- {
- $this->_service = null;
- $this->_uploader = null;
- $this->_fileSizeMock = null;
- $this->_filesystemMock = null;
- $this->_uploaderFactory = null;
- }
- public function testUploadLimitNotConfigured()
- {
- $this->_service = new \Magento\Theme\Model\Uploader\Service(
- $this->_filesystemMock,
- $this->_fileSizeMock,
- $this->dataSize,
- $this->_uploaderFactory
- );
- $this->assertEquals(600 * self::MB_MULTIPLIER, $this->_service->getJsUploadMaxSize());
- $this->assertEquals(600 * self::MB_MULTIPLIER, $this->_service->getCssUploadMaxSize());
- }
- public function testGetCssUploadMaxSize()
- {
- $this->_service = new \Magento\Theme\Model\Uploader\Service(
- $this->_filesystemMock,
- $this->_fileSizeMock,
- $this->dataSize,
- $this->_uploaderFactory,
- ['css' => '5M']
- );
- $this->assertEquals(5 * self::MB_MULTIPLIER, $this->_service->getCssUploadMaxSize());
- }
- public function testGetJsUploadMaxSize()
- {
- $this->_service = new \Magento\Theme\Model\Uploader\Service(
- $this->_filesystemMock,
- $this->_fileSizeMock,
- $this->dataSize,
- $this->_uploaderFactory,
- ['js' => '3M']
- );
- $this->assertEquals(3 * self::MB_MULTIPLIER, $this->_service->getJsUploadMaxSize());
- }
- public function testGetFileContent()
- {
- $fileName = 'file.name';
- $this->_directoryMock->expects(
- $this->once()
- )->method(
- 'getRelativePath'
- )->with(
- $fileName
- )->will(
- $this->returnValue($fileName)
- );
- $this->_directoryMock->expects(
- $this->once()
- )->method(
- 'readFile'
- )->with(
- $fileName
- )->will(
- $this->returnValue('content from my file')
- );
- $this->_service = new \Magento\Theme\Model\Uploader\Service(
- $this->_filesystemMock,
- $this->_fileSizeMock,
- $this->dataSize,
- $this->_uploaderFactory,
- ['js' => '3M']
- );
- $this->assertEquals('content from my file', $this->_service->getFileContent($fileName));
- }
- public function testUploadCssFile()
- {
- $fileName = 'file.name';
- $this->_service = new \Magento\Theme\Model\Uploader\Service(
- $this->_filesystemMock,
- $this->_fileSizeMock,
- $this->dataSize,
- $this->_uploaderFactory,
- ['css' => '3M']
- );
- $this->_directoryMock->expects(
- $this->once()
- )->method(
- 'getRelativePath'
- )->with(
- $fileName
- )->will(
- $this->returnValue($fileName)
- );
- $this->_directoryMock->expects(
- $this->once()
- )->method(
- 'readFile'
- )->with(
- $fileName
- )->will(
- $this->returnValue('content')
- );
- $this->_uploader->expects(
- $this->once()
- )->method(
- 'validateFile'
- )->will(
- $this->returnValue(['name' => $fileName, 'tmp_name' => $fileName])
- );
- $this->assertEquals(
- ['content' => 'content', 'filename' => $fileName],
- $this->_service->uploadCssFile($fileName)
- );
- }
- /**
- * @expectedException \Magento\Framework\Exception\LocalizedException
- */
- public function testUploadInvalidCssFile()
- {
- $fileName = 'file.name';
- $this->_uploader->expects(
- $this->once()
- )->method(
- 'getFileSize'
- )->will(
- $this->returnValue(30 * self::MB_MULTIPLIER)
- );
- $this->_service = new \Magento\Theme\Model\Uploader\Service(
- $this->_filesystemMock,
- $this->_fileSizeMock,
- $this->dataSize,
- $this->_uploaderFactory,
- ['css' => '10M']
- );
- $this->_service->uploadCssFile($fileName);
- }
- public function testUploadJsFile()
- {
- $fileName = 'file.name';
- $this->_fileSizeMock->expects(
- $this->once()
- )->method(
- 'getMaxFileSize'
- )->will(
- $this->returnValue(600 * self::MB_MULTIPLIER)
- );
- $this->_service = new \Magento\Theme\Model\Uploader\Service(
- $this->_filesystemMock,
- $this->_fileSizeMock,
- $this->dataSize,
- $this->_uploaderFactory,
- ['js' => '500M']
- );
- $this->_directoryMock->expects(
- $this->once()
- )->method(
- 'getRelativePath'
- )->with(
- $fileName
- )->will(
- $this->returnValue($fileName)
- );
- $this->_directoryMock->expects(
- $this->once()
- )->method(
- 'readFile'
- )->with(
- $fileName
- )->will(
- $this->returnValue('content')
- );
- $this->_uploader->expects(
- $this->once()
- )->method(
- 'validateFile'
- )->will(
- $this->returnValue(['name' => $fileName, 'tmp_name' => $fileName])
- );
- $this->_uploader->expects($this->once())->method('getFileSize')->will($this->returnValue('499'));
- $this->assertEquals(
- ['content' => 'content', 'filename' => $fileName],
- $this->_service->uploadJsFile($fileName)
- );
- }
- /**
- * @expectedException \Magento\Framework\Exception\LocalizedException
- */
- public function testUploadInvalidJsFile()
- {
- $fileName = 'file.name';
- $this->_service = new \Magento\Theme\Model\Uploader\Service(
- $this->_filesystemMock,
- $this->_fileSizeMock,
- $this->dataSize,
- $this->_uploaderFactory,
- ['js' => '100M']
- );
- $this->_uploader->expects(
- $this->once()
- )->method(
- 'getFileSize'
- )->will(
- $this->returnValue(499 * self::MB_MULTIPLIER)
- );
- $this->_service->uploadJsFile($fileName);
- }
- }
|