Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.3-develop' into mtf-eol
Browse files Browse the repository at this point in the history
  • Loading branch information
soumyau committed Jun 10, 2019
2 parents 2292712 + b5b66fe commit 9336220
Show file tree
Hide file tree
Showing 18 changed files with 391 additions and 72 deletions.
21 changes: 17 additions & 4 deletions app/code/Magento/Backend/Model/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Class \Magento\Backend\Model\UrlInterface
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
* @api
* @since 100.0.2
*/
Expand Down Expand Up @@ -366,6 +367,19 @@ protected function _getMenu()
return $this->_menu;
}

/**
* Set scope entity
*
* @param mixed $scopeId
* @return \Magento\Framework\UrlInterface
*/
public function setScope($scopeId)
{
parent::setScope($scopeId);
$this->_scope = $this->_scopeResolver->getScope($scopeId);
return $this;
}

/**
* Set custom auth session
*
Expand Down Expand Up @@ -402,13 +416,13 @@ public function getAreaFrontName()
}

/**
* Retrieve action path.
* Add backend area front name as a prefix to action path
* Retrieve action path, add backend area front name as a prefix to action path
*
* @return string
*/
protected function _getActionPath()
{

$path = parent::_getActionPath();
if ($path) {
if ($this->getAreaFrontName()) {
Expand Down Expand Up @@ -448,8 +462,7 @@ protected function _getConfigCacheId($path)
}

/**
* Get config data by path
* Use only global config values for backend
* Get config data by path, use only global config values for backend
*
* @param string $path
* @return null|string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\FileSystemException;
use Magento\Backend\Block\DataProviders\ImageUploadConfig as ImageUploadConfigDataProvider;
use Magento\MediaStorage\Helper\File\Storage\Database;

/**
* Block for gallery content.
Expand Down Expand Up @@ -50,25 +51,34 @@ class Content extends \Magento\Backend\Block\Widget
*/
private $imageUploadConfigDataProvider;

/**
* @var Database
*/
private $fileStorageDatabase;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
* @param \Magento\Catalog\Model\Product\Media\Config $mediaConfig
* @param array $data
* @param ImageUploadConfigDataProvider $imageUploadConfigDataProvider
* @param Database $fileStorageDatabase
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
\Magento\Catalog\Model\Product\Media\Config $mediaConfig,
array $data = [],
ImageUploadConfigDataProvider $imageUploadConfigDataProvider = null
ImageUploadConfigDataProvider $imageUploadConfigDataProvider = null,
Database $fileStorageDatabase = null
) {
$this->_jsonEncoder = $jsonEncoder;
$this->_mediaConfig = $mediaConfig;
parent::__construct($context, $data);
$this->imageUploadConfigDataProvider = $imageUploadConfigDataProvider
?: ObjectManager::getInstance()->get(ImageUploadConfigDataProvider::class);
$this->fileStorageDatabase = $fileStorageDatabase
?: ObjectManager::getInstance()->get(Database::class);
}

/**
Expand Down Expand Up @@ -164,6 +174,13 @@ public function getImagesJson()
$images = $this->sortImagesByPosition($value['images']);
foreach ($images as &$image) {
$image['url'] = $this->_mediaConfig->getMediaUrl($image['file']);
if ($this->fileStorageDatabase->checkDbUsage() &&
!$mediaDir->isFile($this->_mediaConfig->getMediaPath($image['file']))
) {
$this->fileStorageDatabase->saveFileToFilesystem(
$this->_mediaConfig->getMediaPath($image['file'])
);
}
try {
$fileHandler = $mediaDir->stat($this->_mediaConfig->getMediaPath($image['file']));
$image['size'] = $fileHandler['size'];
Expand All @@ -187,9 +204,12 @@ public function getImagesJson()
private function sortImagesByPosition($images)
{
if (is_array($images)) {
usort($images, function ($imageA, $imageB) {
return ($imageA['position'] < $imageB['position']) ? -1 : 1;
});
usort(
$images,
function ($imageA, $imageB) {
return ($imageA['position'] < $imageB['position']) ? -1 : 1;
}
);
}
return $images;
}
Expand Down
35 changes: 31 additions & 4 deletions app/code/Magento/Catalog/Model/Category/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class FileInfo
*/
private $baseDirectory;

/**
* @var ReadInterface
*/
private $pubDirectory;

/**
* @param Filesystem $filesystem
* @param Mime $mime
Expand Down Expand Up @@ -82,6 +87,20 @@ private function getBaseDirectory()
return $this->baseDirectory;
}

/**
* Get Pub Directory read instance
*
* @return ReadInterface
*/
private function getPubDirectory()
{
if (!isset($this->pubDirectory)) {
$this->pubDirectory = $this->filesystem->getDirectoryRead(DirectoryList::PUB);
}

return $this->pubDirectory;
}

/**
* Retrieve MIME type of requested file
*
Expand Down Expand Up @@ -135,7 +154,7 @@ private function getFilePath($fileName)
{
$filePath = ltrim($fileName, '/');

$mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath();
$mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath($filePath);
$isFileNameBeginsWithMediaDirectoryPath = $this->isBeginsWithMediaDirectoryPath($fileName);

// if the file is not using a relative path, it resides in the catalog/category media directory
Expand All @@ -160,7 +179,7 @@ public function isBeginsWithMediaDirectoryPath($fileName)
{
$filePath = ltrim($fileName, '/');

$mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath();
$mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath($filePath);
$isFileNameBeginsWithMediaDirectoryPath = strpos($filePath, $mediaDirectoryRelativeSubpath) === 0;

return $isFileNameBeginsWithMediaDirectoryPath;
Expand All @@ -169,14 +188,22 @@ public function isBeginsWithMediaDirectoryPath($fileName)
/**
* Get media directory subpath relative to base directory path
*
* @param string $filePath
* @return string
*/
private function getMediaDirectoryPathRelativeToBaseDirectoryPath()
private function getMediaDirectoryPathRelativeToBaseDirectoryPath(string $filePath = '')
{
$baseDirectoryPath = $this->getBaseDirectory()->getAbsolutePath();
$baseDirectory = $this->getBaseDirectory();
$baseDirectoryPath = $baseDirectory->getAbsolutePath();
$mediaDirectoryPath = $this->getMediaDirectory()->getAbsolutePath();
$pubDirectoryPath = $this->getPubDirectory()->getAbsolutePath();

$mediaDirectoryRelativeSubpath = substr($mediaDirectoryPath, strlen($baseDirectoryPath));
$pubDirectory = $baseDirectory->getRelativePath($pubDirectoryPath);

if (strpos($mediaDirectoryRelativeSubpath, $pubDirectory) === 0 && strpos($filePath, $pubDirectory) !== 0) {
$mediaDirectoryRelativeSubpath = substr($mediaDirectoryRelativeSubpath, strlen($pubDirectory));
}

return $mediaDirectoryRelativeSubpath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Catalog\Model\Entity\Attribute;
use Magento\Catalog\Model\Product;
use Magento\Framework\Phrase;
use Magento\MediaStorage\Helper\File\Storage\Database;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand Down Expand Up @@ -50,6 +51,11 @@ class ContentTest extends \PHPUnit\Framework\TestCase
*/
protected $imageHelper;

/**
* @var \Magento\MediaStorage\Helper\File\Storage\Database|\PHPUnit_Framework_MockObject_MockObject
*/
protected $databaseMock;

/**
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
*/
Expand All @@ -71,13 +77,18 @@ public function setUp()
->disableOriginalConstructor()
->getMock();

$this->databaseMock = $this->getMockBuilder(Database::class)
->disableOriginalConstructor()
->getMock();

$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->content = $this->objectManager->getObject(
\Magento\Catalog\Block\Adminhtml\Product\Helper\Form\Gallery\Content::class,
[
'mediaConfig' => $this->mediaConfigMock,
'jsonEncoder' => $this->jsonEncoderMock,
'filesystem' => $this->fileSystemMock
'filesystem' => $this->fileSystemMock,
'fileStorageDatabase' => $this->databaseMock
]
);
}
Expand Down Expand Up @@ -143,6 +154,13 @@ public function testGetImagesJson()
$this->readMock->expects($this->any())->method('stat')->willReturnMap($sizeMap);
$this->jsonEncoderMock->expects($this->once())->method('encode')->willReturnCallback('json_encode');

$this->readMock->expects($this->any())
->method('isFile')
->will($this->returnValue(true));
$this->databaseMock->expects($this->any())
->method('checkDbUsage')
->will($this->returnValue(false));

$this->assertSame(json_encode($imagesResult), $this->content->getImagesJson());
}

Expand Down Expand Up @@ -210,6 +228,14 @@ public function testGetImagesJsonWithException()
$this->fileSystemMock->expects($this->any())->method('getDirectoryRead')->willReturn($this->readMock);
$this->mediaConfigMock->expects($this->any())->method('getMediaUrl');
$this->mediaConfigMock->expects($this->any())->method('getMediaPath');

$this->readMock->expects($this->any())
->method('isFile')
->will($this->returnValue(true));
$this->databaseMock->expects($this->any())
->method('checkDbUsage')
->will($this->returnValue(false));

$this->readMock->expects($this->any())->method('stat')->willReturnOnConsecutiveCalls(
$this->throwException(
new \Magento\Framework\Exception\FileSystemException(new Phrase('test'))
Expand Down Expand Up @@ -365,4 +391,52 @@ private function getMediaAttribute(string $label, string $attributeCode)

return $mediaAttribute;
}

/**
* Test GetImagesJson() calls MediaStorage functions to obtain image from DB prior to stat call
*
* @return void
*/
public function testGetImagesJsonMediaStorageMode()
{
$images = [
'images' => [
[
'value_id' => '0',
'file' => 'file_1.jpg',
'media_type' => 'image',
'position' => '0'
]
]
];

$mediaPath = [
['file_1.jpg', 'catalog/product/image_1.jpg']
];

$this->content->setElement($this->galleryMock);

$this->galleryMock->expects($this->once())
->method('getImages')
->willReturn($images);
$this->fileSystemMock->expects($this->once())
->method('getDirectoryRead')
->willReturn($this->readMock);
$this->mediaConfigMock->expects($this->any())
->method('getMediaPath')
->willReturnMap($mediaPath);

$this->readMock->expects($this->any())
->method('isFile')
->will($this->returnValue(false));
$this->databaseMock->expects($this->any())
->method('checkDbUsage')
->will($this->returnValue(true));

$this->databaseMock->expects($this->once())
->method('saveFileToFilesystem')
->with('catalog/product/image_1.jpg');

$this->content->getImagesJson();
}
}
Loading

0 comments on commit 9336220

Please sign in to comment.