Skip to content

Commit

Permalink
When Saving design config, ensure that temporary file in database is …
Browse files Browse the repository at this point in the history
…renamed aswell as the local file in pub/media.

Initialise Database Helper from Object Manager if not passed by DI. Increases backwards compatability.
  • Loading branch information
Graham Wharton committed Apr 29, 2019
1 parent 114bcec commit 354a714
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
15 changes: 14 additions & 1 deletion app/code/Magento/Theme/Model/Design/Backend/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Magento\Framework\UrlInterface;
use Magento\MediaStorage\Model\File\UploaderFactory;
use Magento\Theme\Model\Design\Config\FileUploader\FileProcessor;
use Magento\MediaStorage\Helper\File\Storage\Database;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Expand All @@ -36,6 +37,11 @@ class File extends BackendFile
*/
private $mime;

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

/**
* @param Context $context
* @param Registry $registry
Expand All @@ -48,6 +54,7 @@ class File extends BackendFile
* @param AbstractResource|null $resource
* @param AbstractDb|null $resourceCollection
* @param array $data
* @param Database $databaseHelper
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -61,7 +68,8 @@ public function __construct(
UrlInterface $urlBuilder,
AbstractResource $resource = null,
AbstractDb $resourceCollection = null,
array $data = []
array $data = [],
Database $databaseHelper = null
) {
parent::__construct(
$context,
Expand All @@ -76,6 +84,7 @@ public function __construct(
$data
);
$this->urlBuilder = $urlBuilder;
$this->databaseHelper = $databaseHelper ?: ObjectManager::getInstance()->get(Database::class);
}

/**
Expand Down Expand Up @@ -103,6 +112,10 @@ public function beforeSave()
$this->getTmpMediaPath($filename),
$this->_getUploadDir() . '/' . $filename
);
$this->databaseHelper->renameFile(
$this->getTmpMediaPath($filename),
$this->_getUploadDir() . '/' . $filename
);
if ($result) {
$this->_mediaDirectory->delete($this->getTmpMediaPath($filename));
if ($this->_addWhetherScopeInfo()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class FileTest extends \PHPUnit\Framework\TestCase
*/
private $mime;

/**
* @var \Magento\MediaStorage\Helper\File\Storage\Database|\PHPUnit_Framework_MockObject_MockObject
*/
private $databaseHelper;

public function setUp()
{
$context = $this->getMockObject(\Magento\Framework\Model\Context::class);
Expand Down Expand Up @@ -55,6 +60,17 @@ public function setUp()
->disableOriginalConstructor()
->getMock();

$this->databaseHelper = $this->getMockBuilder(\Magento\MediaStorage\Helper\File\Storage\Database::class)
->disableOriginalConstructor()
->getMock();

$abstractResource = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\AbstractResource::class)
->getMockForAbstractClass();

$abstractDb = $this->getMockBuilder(\Magento\Framework\Data\Collection\AbstractDb::class)
->disableOriginalConstructor()
->getMockForAbstractClass();

$this->fileBackend = new File(
$context,
$registry,
Expand All @@ -63,7 +79,11 @@ public function setUp()
$uploaderFactory,
$requestData,
$filesystem,
$this->urlBuilder
$this->urlBuilder,
$abstractResource,
$abstractDb,
[],
$this->databaseHelper
);

$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
Expand Down Expand Up @@ -196,6 +216,11 @@ public function testBeforeSave($fileName)
]
);

$this->databaseHelper->expects($this->once())
->method('renameFile')
->with($expectedTmpMediaPath, '/' . $expectedFileName)
->willReturn(true);

$this->mediaDirectory->expects($this->once())
->method('copyFile')
->with($expectedTmpMediaPath, '/' . $expectedFileName)
Expand Down

0 comments on commit 354a714

Please sign in to comment.