diff --git a/app/code/Magento/Catalog/etc/di.xml b/app/code/Magento/Catalog/etc/di.xml index 65e14be97edd1e665caa49615319724f8b62fecf..7a0a9602aa0ec9d7939f17f415a10c811fc3a0ac 100644 --- a/app/code/Magento/Catalog/etc/di.xml +++ b/app/code/Magento/Catalog/etc/di.xml @@ -635,7 +635,7 @@ <item name="watermark_image_image" xsi:type="array"> <item name="path" xsi:type="string">design/watermark/image_image</item> <item name="fieldset" xsi:type="string">other_settings/watermark/image</item> - <item name="backend_model" xsi:type="string">Magento\Theme\Model\Design\Backend\Image</item> + <item name="backend_model" xsi:type="string">Magento\Theme\Model\Design\Backend\File</item> <item name="upload_dir" xsi:type="array"> <item name="config" xsi:type="string">system/filesystem/media</item> <item name="scope_info" xsi:type="string">1</item> @@ -663,7 +663,7 @@ <item name="watermark_small_image_image" xsi:type="array"> <item name="path" xsi:type="string">design/watermark/small_image_image</item> <item name="fieldset" xsi:type="string">other_settings/watermark/small_image</item> - <item name="backend_model" xsi:type="string">Magento\Theme\Model\Design\Backend\Image</item> + <item name="backend_model" xsi:type="string">Magento\Theme\Model\Design\Backend\File</item> <item name="upload_dir" xsi:type="array"> <item name="config" xsi:type="string">system/filesystem/media</item> <item name="scope_info" xsi:type="string">1</item> @@ -691,7 +691,7 @@ <item name="watermark_thumbnail_image" xsi:type="array"> <item name="path" xsi:type="string">design/watermark/thumbnail_image</item> <item name="fieldset" xsi:type="string">other_settings/watermark/thumbnail</item> - <item name="backend_model" xsi:type="string">Magento\Theme\Model\Design\Backend\Image</item> + <item name="backend_model" xsi:type="string">Magento\Theme\Model\Design\Backend\File</item> <item name="upload_dir" xsi:type="array"> <item name="config" xsi:type="string">system/filesystem/media</item> <item name="scope_info" xsi:type="string">1</item> diff --git a/app/code/Magento/Swatches/etc/di.xml b/app/code/Magento/Swatches/etc/di.xml index f73753acfb71422589a2c5ea483b3badabc194a9..c9d963f1ccb5f41b35027d422f8072e188d57233 100644 --- a/app/code/Magento/Swatches/etc/di.xml +++ b/app/code/Magento/Swatches/etc/di.xml @@ -43,7 +43,7 @@ <item name="watermark_swatch_image_image" xsi:type="array"> <item name="path" xsi:type="string">design/watermark/swatch_image_image</item> <item name="fieldset" xsi:type="string">other_settings/watermark/swatch_image</item> - <item name="backend_model" xsi:type="string">Magento\Theme\Model\Design\Backend\Image</item> + <item name="backend_model" xsi:type="string">Magento\Theme\Model\Design\Backend\File</item> <item name="upload_dir" xsi:type="array"> <item name="config" xsi:type="string">system/filesystem/media</item> <item name="scope_info" xsi:type="string">1</item> diff --git a/app/code/Magento/Theme/Model/Design/Backend/Favicon.php b/app/code/Magento/Theme/Model/Design/Backend/Favicon.php index 46496287caf88a2c2d76fbfaf931642b75932801..e7067aae785efa0468f85019473854806490141e 100644 --- a/app/code/Magento/Theme/Model/Design/Backend/Favicon.php +++ b/app/code/Magento/Theme/Model/Design/Backend/Favicon.php @@ -5,19 +5,43 @@ */ namespace Magento\Theme\Model\Design\Backend; -class Favicon extends Image +use Magento\Framework\Filesystem; + +class Favicon extends File { /** - * @var string + * The tail part of directory path for uploading + * + */ + const UPLOAD_DIR = 'favicon'; + + /** + * Return path to directory for upload file + * + * @return string + * @throw \Magento\Framework\Exception\LocalizedException + */ + protected function _getUploadDir() + { + return $this->_mediaDirectory->getRelativePath($this->_appendScopeInfo(self::UPLOAD_DIR)); + } + + /** + * Makes a decision about whether to add info about the scope. + * + * @return boolean */ - protected $uploadDir = 'favicon'; + protected function _addWhetherScopeInfo() + { + return true; + } /** * Getter for allowed extensions of uploaded files. * * @return string[] */ - public function getAllowedExtensions() + protected function _getAllowedExtensions() { return ['ico', 'png', 'gif', 'jpg', 'jpeg', 'apng', 'svg']; } diff --git a/app/code/Magento/Theme/Model/Design/Backend/Image.php b/app/code/Magento/Theme/Model/Design/Backend/File.php similarity index 66% rename from app/code/Magento/Theme/Model/Design/Backend/Image.php rename to app/code/Magento/Theme/Model/Design/Backend/File.php index 8f7dbbca100d4588aaae35e6cb1361ff4fccfcaf..48eb89bae2cefbd24a6ba85f47a7a5d0b2fa4840 100644 --- a/app/code/Magento/Theme/Model/Design/Backend/Image.php +++ b/app/code/Magento/Theme/Model/Design/Backend/File.php @@ -6,7 +6,7 @@ namespace Magento\Theme\Model\Design\Backend; use Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface; -use \Magento\Config\Model\Config\Backend\File as File; +use \Magento\Config\Model\Config\Backend\File as BackendFile; use Magento\Framework\App\Cache\TypeListInterface; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Data\Collection\AbstractDb; @@ -18,17 +18,12 @@ use Magento\Framework\Registry; use Magento\MediaStorage\Model\File\UploaderFactory; use Magento\Theme\Model\Design\Config\FileUploader\Config; -class Image extends File +class File extends BackendFile { - /** - * @var string - */ - protected $uploadDir = 'image'; - /** * @var Config */ - protected $imageConfig; + protected $fileConfig; /** * @param Context $context @@ -38,7 +33,7 @@ class Image extends File * @param UploaderFactory $uploaderFactory * @param RequestDataInterface $requestData * @param Filesystem $filesystem - * @param Config $imageConfig + * @param Config $fileConfig * @param AbstractResource|null $resource * @param AbstractDb|null $resourceCollection * @param array $data @@ -52,7 +47,7 @@ class Image extends File UploaderFactory $uploaderFactory, RequestDataInterface $requestData, Filesystem $filesystem, - Config $imageConfig, + Config $fileConfig, AbstractResource $resource = null, AbstractDb $resourceCollection = null, array $data = [] @@ -69,7 +64,7 @@ class Image extends File $resourceCollection, $data ); - $this->imageConfig = $imageConfig; + $this->fileConfig = $fileConfig; } /** @@ -93,12 +88,14 @@ class Image extends File } $filename = $value['file']; $result = $this->_mediaDirectory->copyFile( - $this->imageConfig->getTmpMediaPath($filename), - $this->_appendScopeInfo($this->uploadDir) . '/' . $filename + $this->fileConfig->getTmpMediaPath($filename), + $this->_getUploadDir() . '/' . $filename ); if ($result) { - $this->_mediaDirectory->delete($this->imageConfig->getTmpMediaPath($filename)); - $filename = $this->_prependScopeInfo($filename); + $this->_mediaDirectory->delete($this->fileConfig->getTmpMediaPath($filename)); + if ($this->_addWhetherScopeInfo()) { + $filename = $this->_prependScopeInfo($filename); + } $this->setValue($filename); } else { $this->unsValue(); @@ -114,11 +111,11 @@ class Image extends File { $value = $this->getValue(); if ($value && !is_array($value)) { - $fileName = '/' . $this->uploadDir . '/' . $value; + $fileName = $this->_getUploadDir() . '/' . $value; $stat = $this->_mediaDirectory->stat($fileName); $this->setValue([ [ - 'url' => $this->imageConfig->getStoreMediaUrl() . $fileName, + 'url' => $this->fileConfig->getStoreMediaUrl() . $fileName, 'file' => $value, 'size' => is_array($stat) ? $stat['size'] : 0, 'exists' => true @@ -128,6 +125,40 @@ class Image extends File return $this; } + /** + * Return path to directory for upload file + * + * @return string + * @throws \Magento\Framework\Exception\LocalizedException + */ + protected function _getUploadDir() + { + $fieldConfig = $this->getFieldConfig(); + /* @var $fieldConfig \Magento\Framework\Simplexml\Element */ + + if (!array_key_exists('upload_dir', $fieldConfig)) { + throw new \Magento\Framework\Exception\LocalizedException( + __('The base directory to upload file is not specified.') + ); + } + + if (is_array($fieldConfig['upload_dir'])) { + $uploadDir = $fieldConfig['upload_dir']['value']; + if (array_key_exists('scope_info', $fieldConfig['upload_dir']) && $fieldConfig['upload_dir']['scope_info'] + ) { + $uploadDir = $this->_appendScopeInfo($uploadDir); + } + + if (array_key_exists('config', $fieldConfig['upload_dir'])) { + $uploadDir = $this->_mediaDirectory->getRelativePath($uploadDir); + } + } else { + $uploadDir = (string)$fieldConfig['upload_dir']; + } + + return $uploadDir; + } + /** * Getter for allowed extensions of uploaded files * diff --git a/app/code/Magento/Theme/Model/Design/Backend/Logo.php b/app/code/Magento/Theme/Model/Design/Backend/Logo.php index b658bfefa312d3c4484b148b9bf94c30f2b33305..55e5a5b62ce4c01dd612a6ff00bb723b1b40e108 100644 --- a/app/code/Magento/Theme/Model/Design/Backend/Logo.php +++ b/app/code/Magento/Theme/Model/Design/Backend/Logo.php @@ -5,19 +5,41 @@ */ namespace Magento\Theme\Model\Design\Backend; -class Logo extends Image +class Logo extends File { /** - * @var string + * The tail part of directory path for uploading + * */ - protected $uploadDir = 'logo'; + const UPLOAD_DIR = 'logo'; + + /** + * Return path to directory for upload file + * + * @return string + * @throw \Magento\Framework\Exception\LocalizedException + */ + protected function _getUploadDir() + { + return $this->_mediaDirectory->getRelativePath($this->_appendScopeInfo(self::UPLOAD_DIR)); + } + + /** + * Makes a decision about whether to add info about the scope. + * + * @return boolean + */ + protected function _addWhetherScopeInfo() + { + return true; + } /** * Getter for allowed extensions of uploaded files. * * @return string[] */ - public function getAllowedExtensions() + protected function _getAllowedExtensions() { return ['jpg', 'jpeg', 'gif', 'png', 'svg']; } diff --git a/app/code/Magento/Theme/Model/Design/Config/FileUploader/ImageProcessor.php b/app/code/Magento/Theme/Model/Design/Config/FileUploader/ImageProcessor.php index f8eaa277b157469ef1a95f0f189b5a34d3dceb05..d9d216cf01906fbe123588af7def378d4ff78902 100644 --- a/app/code/Magento/Theme/Model/Design/Config/FileUploader/ImageProcessor.php +++ b/app/code/Magento/Theme/Model/Design/Config/FileUploader/ImageProcessor.php @@ -7,7 +7,7 @@ namespace Magento\Theme\Model\Design\Config\FileUploader; use Magento\Framework\Exception\LocalizedException; use Magento\MediaStorage\Model\File\UploaderFactory; -use Magento\Theme\Model\Design\Backend\Image; +use Magento\Theme\Model\Design\Backend\File; use Magento\Theme\Model\Design\BackendModelFactory; use Magento\Theme\Model\Design\Config\MetadataProvider; @@ -80,7 +80,7 @@ class ImageProcessor protected function save($fileId, $destination) { $result = ['file' => '', 'size' => '']; - /** @var Image $backendModel */ + /** @var File $backendModel */ $backendModel = $this->getBackendModel($fileId); $uploader = $this->uploaderFactory->create(['fileId' => $fileId]); $uploader->setAllowRenameFiles(true); @@ -94,7 +94,7 @@ class ImageProcessor * Retrieve backend model by field code * * @param string $code - * @return Image + * @return File * @throws LocalizedException */ protected function getBackendModel($code) diff --git a/app/code/Magento/Theme/Model/Design/Config/Storage.php b/app/code/Magento/Theme/Model/Design/Config/Storage.php index aa518a085a54805556110be7cb96d1eb16e11e35..eb121698682248837ed98861f6eccb80bcaae92c 100644 --- a/app/code/Magento/Theme/Model/Design/Config/Storage.php +++ b/app/code/Magento/Theme/Model/Design/Config/Storage.php @@ -77,7 +77,8 @@ class Storage foreach ($fieldsData as &$fieldData) { $value = $this->valueProcessor->process( $this->scopeConfig->getValue($fieldData->getPath(), $scope, $scopeId), - $fieldData->getPath() + $fieldData->getPath(), + $fieldData->getFieldConfig() ); if ($value !== null) { $fieldData->setValue($value); diff --git a/app/code/Magento/Theme/Model/Design/Config/ValueProcessor.php b/app/code/Magento/Theme/Model/Design/Config/ValueProcessor.php index 7ce660f625f09e4e3426eba6a52e810d9336355c..f52101d91d1848706caf43909cd23051cdc9a3b2 100644 --- a/app/code/Magento/Theme/Model/Design/Config/ValueProcessor.php +++ b/app/code/Magento/Theme/Model/Design/Config/ValueProcessor.php @@ -28,11 +28,18 @@ class ValueProcessor * * @param string $value * @param string $path + * @param array $fieldConfig * @return mixed */ - public function process($value, $path) + public function process($value, $path, array $fieldConfig = []) { - $backendModel = $this->backendModelFactory->createByPath($path, ['value' => $value]); + $backendModel = $this->backendModelFactory->createByPath( + $path, + [ + 'value' => $value, + 'field_config' => $fieldConfig, + ] + ); $backendModel->afterLoad(); return $backendModel->getValue(); } diff --git a/app/code/Magento/Theme/Test/Unit/Model/Design/Backend/ImageTest.php b/app/code/Magento/Theme/Test/Unit/Model/Design/Backend/FileTest.php similarity index 73% rename from app/code/Magento/Theme/Test/Unit/Model/Design/Backend/ImageTest.php rename to app/code/Magento/Theme/Test/Unit/Model/Design/Backend/FileTest.php index ce2a8c47419e79487940f5b79451fe2287e833b0..bd7307bd3c2a0f8d63c7b42c779bc547961a1b05 100644 --- a/app/code/Magento/Theme/Test/Unit/Model/Design/Backend/ImageTest.php +++ b/app/code/Magento/Theme/Test/Unit/Model/Design/Backend/FileTest.php @@ -5,20 +5,20 @@ */ namespace Magento\Theme\Test\Unit\Model\Design\Backend; -use Magento\Theme\Model\Design\Backend\Image; +use Magento\Theme\Model\Design\Backend\File; use Magento\Framework\App\Filesystem\DirectoryList; -class ImageTest extends \PHPUnit_Framework_TestCase +class FileTest extends \PHPUnit_Framework_TestCase { /** @var \Magento\Framework\Filesystem\Directory\WriteInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $mediaDirectory; /** @var \Magento\Theme\Model\Design\Config\FileUploader\Config|\PHPUnit_Framework_MockObject_MockObject */ - protected $imageConfig; + protected $fileConfig; - /** @var Image */ - protected $imageBackend; + /** @var File */ + protected $fileBackend; public function setUp() { @@ -31,7 +31,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase 'Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface' ); $filesystem = $this->getMockObject('Magento\Framework\Filesystem'); - $this->imageConfig = $this->getMockObject('Magento\Theme\Model\Design\Config\FileUploader\Config'); + $this->fileConfig = $this->getMockObject('Magento\Theme\Model\Design\Config\FileUploader\Config'); $this->mediaDirectory = $this->getMockObjectForAbstractClass( 'Magento\Framework\Filesystem\Directory\WriteInterface' @@ -42,7 +42,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase ->with(DirectoryList::MEDIA) ->willReturn($this->mediaDirectory); - $this->imageBackend = new Image( + $this->fileBackend = new File( $context, $registry, $config, @@ -50,13 +50,13 @@ class ImageTest extends \PHPUnit_Framework_TestCase $uploaderFactory, $requestData, $filesystem, - $this->imageConfig + $this->fileConfig ); } public function tearDown() { - unset($this->imageBackend); + unset($this->fileBackend); } /** @@ -87,25 +87,33 @@ class ImageTest extends \PHPUnit_Framework_TestCase public function testAfterLoad() { $value = 'store/1/filename.jpg'; - $this->imageBackend->setValue($value); + $this->fileBackend->setValue($value); + $this->fileBackend->setFieldConfig( + [ + 'upload_dir' => [ + 'value' => 'value', + 'config' => 'system/filesystem/media', + ], + ] + ); $this->mediaDirectory->expects($this->once()) ->method('stat') - ->with('/image/' . $value) + ->with('/' . $value) ->willReturn(['size' => 234234]); - $this->imageConfig->expects($this->once()) + $this->fileConfig->expects($this->once()) ->method('getStoreMediaUrl') ->willReturn('http://magento2.com/pub/media'); - $this->imageBackend->afterLoad(); + $this->fileBackend->afterLoad(); $this->assertEquals( [ [ - 'url' => 'http://magento2.com/pub/media/image/' . $value, + 'url' => 'http://magento2.com/pub/media/' . $value, 'file' => $value, 'size' => 234234, 'exists' => true, ] ], - $this->imageBackend->getValue() + $this->fileBackend->getValue() ); } @@ -113,9 +121,9 @@ class ImageTest extends \PHPUnit_Framework_TestCase { $value = 'filename.jpg'; $tmpMediaPath = 'tmp/image/' . $value; - $this->imageBackend->setScope('store'); - $this->imageBackend->setScopeId(1); - $this->imageBackend->setValue( + $this->fileBackend->setScope('store'); + $this->fileBackend->setScopeId(1); + $this->fileBackend->setValue( [ [ 'url' => 'http://magento2.com/pub/media/tmp/image/' . $value, @@ -124,24 +132,29 @@ class ImageTest extends \PHPUnit_Framework_TestCase ] ] ); - $this->imageConfig->expects($this->exactly(2)) + $this->fileBackend->setFieldConfig( + [ + 'upload_dir' => [ + 'value' => 'value', + 'config' => 'system/filesystem/media', + ], + ] + ); + $this->fileConfig->expects($this->exactly(2)) ->method('getTmpMediaPath') ->with($value) ->willReturn($tmpMediaPath); $this->mediaDirectory->expects($this->once()) ->method('copyFile') - ->with( - $tmpMediaPath, - 'image/store/1/' . $value - ) + ->with($tmpMediaPath, '/' . $value) ->willReturn(true); $this->mediaDirectory->expects($this->once()) ->method('delete') ->with($tmpMediaPath); - $this->imageBackend->beforeSave(); - $this->assertEquals('store/1/filename.jpg', $this->imageBackend->getValue()); + $this->fileBackend->beforeSave(); + $this->assertEquals('filename.jpg', $this->fileBackend->getValue()); } /** @@ -150,7 +163,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase */ public function testBeforeSaveWithoutFile() { - $this->imageBackend->setData( + $this->fileBackend->setData( [ 'value' => [ 'test' => '' @@ -160,13 +173,13 @@ class ImageTest extends \PHPUnit_Framework_TestCase ], ] ); - $this->imageBackend->beforeSave(); + $this->fileBackend->beforeSave(); } public function testBeforeSaveWithExistingFile() { $value = 'filename.jpg'; - $this->imageBackend->setValue( + $this->fileBackend->setValue( [ [ 'url' => 'http://magento2.com/pub/media/tmp/image/' . $value, @@ -176,10 +189,10 @@ class ImageTest extends \PHPUnit_Framework_TestCase ] ] ); - $this->imageBackend->beforeSave(); + $this->fileBackend->beforeSave(); $this->assertEquals( $value, - $this->imageBackend->getValue() + $this->fileBackend->getValue() ); } } diff --git a/app/code/Magento/Theme/Test/Unit/Model/Design/Config/FileUploader/ImageProcessorTest.php b/app/code/Magento/Theme/Test/Unit/Model/Design/Config/FileUploader/ImageProcessorTest.php index 4449104fdd76148cf97de19a831df4808e474a5e..b38c7c8ec178346e3291edf3e2d8ccce1979979f 100644 --- a/app/code/Magento/Theme/Test/Unit/Model/Design/Config/FileUploader/ImageProcessorTest.php +++ b/app/code/Magento/Theme/Test/Unit/Model/Design/Config/FileUploader/ImageProcessorTest.php @@ -21,7 +21,7 @@ class ImageProcessorTest extends \PHPUnit_Framework_TestCase /** @var \Magento\Theme\Model\Design\BackendModelFactory|\PHPUnit_Framework_MockObject_MockObject */ protected $backendModelFactory; - /** @var \Magento\Theme\Model\Design\Backend\Image|\PHPUnit_Framework_MockObject_MockObject */ + /** @var \Magento\Theme\Model\Design\Backend\File|\PHPUnit_Framework_MockObject_MockObject */ protected $backendModel; /** @var \Magento\Theme\Model\Design\Config\MetadataProvider|\PHPUnit_Framework_MockObject_MockObject */ @@ -45,7 +45,7 @@ class ImageProcessorTest extends \PHPUnit_Framework_TestCase $this->backendModelFactory = $this->getMockBuilder('Magento\Theme\Model\Design\BackendModelFactory') ->disableOriginalConstructor() ->getMock(); - $this->backendModel = $this->getMockBuilder('Magento\Theme\Model\Design\Backend\Image') + $this->backendModel = $this->getMockBuilder('Magento\Theme\Model\Design\Backend\File') ->disableOriginalConstructor() ->getMock(); $this->metadataProvider = $this->getMockBuilder('Magento\Theme\Model\Design\Config\MetadataProvider') @@ -66,7 +66,7 @@ class ImageProcessorTest extends \PHPUnit_Framework_TestCase $metadata = [ $fieldCode => [ 'path' => $path, - 'backend_model' => 'Magento\Theme\Model\Design\Backend\Image' + 'backend_model' => 'Magento\Theme\Model\Design\Backend\File' ], ]; diff --git a/app/code/Magento/Theme/Test/Unit/Model/Design/Config/StorageTest.php b/app/code/Magento/Theme/Test/Unit/Model/Design/Config/StorageTest.php index 77c1b2b7ca81161e98fdcc62185f65c4192fadf8..ffbb9335a5522cebbd15ce73f2632de3a420e819 100644 --- a/app/code/Magento/Theme/Test/Unit/Model/Design/Config/StorageTest.php +++ b/app/code/Magento/Theme/Test/Unit/Model/Design/Config/StorageTest.php @@ -175,13 +175,16 @@ class StorageTest extends \PHPUnit_Framework_TestCase $this->designConfigData->expects($this->atLeastOnce()) ->method('getPath') ->willReturn('path'); + $this->designConfigData->expects($this->atLeastOnce()) + ->method('getFieldConfig') + ->willReturn([]); $this->scopeConfig->expects($this->once()) ->method('getValue') ->with('path', $scope, $scopeId) ->willReturn('value'); $this->valueProcessor->expects($this->once()) ->method('process') - ->with('value', 'path') + ->with('value', 'path', []) ->willReturnArgument(0); $this->designConfigData->expects($this->once()) ->method('setValue') diff --git a/app/code/Magento/Theme/Test/Unit/Model/Design/Config/ValueProcessorTest.php b/app/code/Magento/Theme/Test/Unit/Model/Design/Config/ValueProcessorTest.php index ac8097a28fa271f0589bdee59bb912732e5d34d3..3121a9e2b6c76818023b8ed7c7e6f56fc859f5dd 100644 --- a/app/code/Magento/Theme/Test/Unit/Model/Design/Config/ValueProcessorTest.php +++ b/app/code/Magento/Theme/Test/Unit/Model/Design/Config/ValueProcessorTest.php @@ -38,7 +38,13 @@ class ValueProcessorTest extends \PHPUnit_Framework_TestCase $this->backendModelFactory->expects($this->once()) ->method('createByPath') - ->with($path, ['value' => $value]) + ->with( + $path, + [ + 'value' => $value, + 'field_config' => [], + ] + ) ->willReturn($this->backendModel); $this->backendModel->expects($this->once()) ->method('afterLoad');