123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Config\Test\Unit\Model\Config\Backend;
- use Magento\Config\Model\Config\Backend\File;
- use Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface;
- use Magento\Framework\App\Cache\TypeListInterface;
- use Magento\Framework\App\Config\ScopeConfigInterface;
- use Magento\Framework\App\Filesystem\DirectoryList;
- use Magento\Framework\Filesystem;
- use Magento\Framework\Filesystem\Directory\WriteInterface;
- use Magento\Framework\Model\Context;
- use Magento\Framework\Registry;
- use Magento\MediaStorage\Model\File\Uploader;
- use Magento\MediaStorage\Model\File\UploaderFactory;
- /**
- * Class FileTest
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class FileTest extends \PHPUnit\Framework\TestCase
- {
- /** @var File */
- protected $model;
- /** @var Context|\PHPUnit_Framework_MockObject_MockObject */
- protected $contextMock;
- /** @var Registry|\PHPUnit_Framework_MockObject_MockObject */
- protected $registryMock;
- /** @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
- protected $scopeConfigMock;
- /** @var TypeListInterface|\PHPUnit_Framework_MockObject_MockObject */
- protected $typeListMock;
- /** @var UploaderFactory|\PHPUnit_Framework_MockObject_MockObject */
- protected $uploaderFactoryMock;
- /** @var RequestDataInterface|\PHPUnit_Framework_MockObject_MockObject */
- protected $requestDataMock;
- /** @var Filesystem|\PHPUnit_Framework_MockObject_MockObject */
- protected $filesystemMock;
- /** @var WriteInterface|\PHPUnit_Framework_MockObject_MockObject */
- protected $writeMock;
- /** @var Uploader|\PHPUnit_Framework_MockObject_MockObject */
- protected $uploaderMock;
- protected function setUp()
- {
- $this->contextMock = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->registryMock = $this->getMockBuilder(\Magento\Framework\Registry::class)
- ->getMockForAbstractClass();
- $this->scopeConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
- ->getMockForAbstractClass();
- $this->typeListMock = $this->getMockBuilder(\Magento\Framework\App\Cache\TypeListInterface::class)
- ->getMockForAbstractClass();
- $this->uploaderFactoryMock = $this->getMockBuilder(\Magento\MediaStorage\Model\File\UploaderFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->requestDataMock
- = $this->getMockBuilder(\Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface::class)
- ->getMockForAbstractClass();
- $this->filesystemMock = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->writeMock = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\WriteInterface::class)
- ->getMock();
- $this->uploaderMock = $this->getMockBuilder(\Magento\MediaStorage\Model\File\Uploader::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->uploaderFactoryMock->expects($this->any())
- ->method('create')
- ->willReturn($this->uploaderMock);
- $this->filesystemMock->expects($this->once())
- ->method('getDirectoryWrite')
- ->with(DirectoryList::MEDIA)
- ->willReturn($this->writeMock);
- $this->model = new File(
- $this->contextMock,
- $this->registryMock,
- $this->scopeConfigMock,
- $this->typeListMock,
- $this->uploaderFactoryMock,
- $this->requestDataMock,
- $this->filesystemMock
- );
- }
- public function testBeforeSave()
- {
- $value = 'value';
- $groupId = 1;
- $field = 'field';
- $tmpFileName = 'tmp_file_name';
- $name = 'name';
- $path = 'path';
- $scope = 'scope';
- $scopeId = 2;
- $uploadDir = 'upload_dir';
- $uploadDirData = [
- 'scope_info' => 1,
- 'value' => $uploadDir,
- ];
- $fieldConfig = [
- 'upload_dir' => $uploadDirData,
- ];
- $fileData = [
- 'tmp_name' => $tmpFileName,
- 'name' => $name,
- ];
- $fileName = 'file_name';
- $result = [
- 'file' => $fileName,
- ];
- $this->model->setValue($value);
- $this->model->setPath($path);
- $this->model->setScope($scope);
- $this->model->setScopeId($scopeId);
- $this->model->setFieldConfig($fieldConfig);
- $_FILES['groups']['tmp_name'][$groupId]['fields'][$field]['value'] = $tmpFileName;
- $this->requestDataMock->expects($this->once())
- ->method('getTmpName')
- ->with($path)
- ->willReturn($tmpFileName);
- $this->requestDataMock->expects($this->once())
- ->method('getName')
- ->with($path)
- ->willReturn($name);
- $this->uploaderFactoryMock->expects($this->any())
- ->method('create')
- ->with(['fileId' => $fileData])
- ->willReturn($this->uploaderMock);
- $this->uploaderMock->expects($this->once())
- ->method('save')
- ->with($uploadDir . '/' . $scope . '/' . $scopeId, null)
- ->willReturn($result);
- $this->assertEquals($this->model, $this->model->beforeSave());
- $this->assertEquals($this->model->getValue(), $scope . '/' . $scopeId . '/' . $fileName);
- }
- public function testBeforeWithoutRequest()
- {
- $tmpFileName = 'tmp_file_name';
- $value = ['tmp_name' => $tmpFileName, 'name' => 'name'];
- $name = 'name';
- $path = 'path';
- $scope = 'scope';
- $scopeId = 2;
- $uploadDir = 'upload_dir';
- $fieldConfig = [
- 'upload_dir' => $uploadDir,
- ];
- $fileData = [
- 'tmp_name' => $tmpFileName,
- 'name' => $name,
- ];
- $fileName = 'file_name';
- $result = [
- 'file' => $fileName,
- ];
- $this->model->setValue($value);
- $this->model->setPath($path);
- $this->model->setScope($scope);
- $this->model->setScopeId($scopeId);
- $this->model->setFieldConfig($fieldConfig);
- $this->requestDataMock->expects($this->once())
- ->method('getTmpName')
- ->with($path)
- ->willReturn('');
- $this->uploaderFactoryMock->expects($this->any())
- ->method('create')
- ->with(['fileId' => $fileData])
- ->willReturn($this->uploaderMock);
- $this->uploaderMock->expects($this->once())
- ->method('save')
- ->with($uploadDir, null)
- ->willReturn($result);
- $this->assertEquals($this->model, $this->model->beforeSave());
- $this->assertEquals($this->model->getValue(), $fileName);
- }
- public function testBeforeWithoutFile()
- {
- $value = ['name' => 'name'];
- $path = 'path';
- $uploadDir = 'upload_dir';
- $uploadDirData = [
- 'scope_info' => 1,
- 'value' => $uploadDir,
- ];
- $fieldConfig = [
- 'upload_dir' => $uploadDirData,
- ];
- $this->model->setValue($value);
- $this->model->setPath($path);
- $this->model->setFieldConfig($fieldConfig);
- $this->requestDataMock->expects($this->once())
- ->method('getTmpName')
- ->with($path)
- ->willReturn('');
- $this->assertEquals($this->model, $this->model->beforeSave());
- $this->assertEquals($this->model->getValue(), null);
- }
- public function testBeforeWithDelete()
- {
- $value = ['delete' => 1];
- $path = 'path';
- $uploadDir = 'upload_dir';
- $uploadDirData = [
- 'scope_info' => 1,
- 'value' => $uploadDir,
- ];
- $fieldConfig = [
- 'upload_dir' => $uploadDirData,
- ];
- $this->model->setValue($value);
- $this->model->setPath($path);
- $this->model->setFieldConfig($fieldConfig);
- $this->requestDataMock->expects($this->once())
- ->method('getTmpName')
- ->with($path)
- ->willReturn('');
- $this->assertEquals($this->model, $this->model->beforeSave());
- $this->assertEquals($this->model->getValue(), '');
- }
- /**
- * @expectedException \Magento\Framework\Exception\LocalizedException
- * @expectedExceptionMessage Exception!
- */
- public function testBeforeSaveWithException()
- {
- $value = 'value';
- $groupId = 1;
- $field = 'field';
- $tmpFileName = 'tmp_file_name';
- $name = 'name';
- $path = 'path';
- $scope = 'scope';
- $scopeId = 2;
- $uploadDir = 'upload_dir';
- $fieldConfig = [
- 'upload_dir' => $uploadDir,
- ];
- $fileData = [
- 'tmp_name' => $tmpFileName,
- 'name' => $name,
- ];
- $exception = 'Exception!';
- $this->model->setValue($value);
- $this->model->setPath($path);
- $this->model->setScope($scope);
- $this->model->setScopeId($scopeId);
- $this->model->setFieldConfig($fieldConfig);
- $_FILES['groups']['tmp_name'][$groupId]['fields'][$field]['value'] = $tmpFileName;
- $this->requestDataMock->expects($this->once())
- ->method('getTmpName')
- ->with($path)
- ->willReturn($tmpFileName);
- $this->requestDataMock->expects($this->once())
- ->method('getName')
- ->with($path)
- ->willReturn($name);
- $this->uploaderFactoryMock->expects($this->any())
- ->method('create')
- ->with(['fileId' => $fileData])
- ->willReturn($this->uploaderMock);
- $this->uploaderMock->expects($this->once())
- ->method('save')
- ->with($uploadDir, null)
- ->willThrowException(new \Exception($exception));
- $this->model->beforeSave();
- }
- }
|