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.
  • Loading branch information
Graham Wharton committed Apr 24, 2019
1 parent 18dc53f commit 76e74ca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
13 changes: 13 additions & 0 deletions app/code/Magento/Theme/Model/Design/Backend/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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;

/**
* File Backend
Expand All @@ -39,6 +40,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 RequestDataInterface $requestData
* @param Filesystem $filesystem
* @param UrlInterface $urlBuilder
* @param Database $databaseHelper
* @param AbstractResource|null $resource
* @param AbstractDb|null $resourceCollection
* @param array $data
Expand All @@ -62,6 +69,7 @@ public function __construct(
RequestDataInterface $requestData,
Filesystem $filesystem,
UrlInterface $urlBuilder,
Database $databaseHelper,
AbstractResource $resource = null,
AbstractDb $resourceCollection = null,
array $data = []
Expand All @@ -79,6 +87,7 @@ public function __construct(
$data
);
$this->urlBuilder = $urlBuilder;
$this->databaseHelper = $databaseHelper;
}

/**
Expand Down Expand Up @@ -258,6 +267,10 @@ private function updateMediaDirectory(string $filename, string $url)
$mediaPath,
$destinationMediaPath
);
$this->databaseHelper->renameFile(
$mediaPath,
$destinationMediaPath
);
}
if ($result) {
if ($mediaPath === $tmpMediaPath) {
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,10 @@ public function setUp()
->disableOriginalConstructor()
->getMock();

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

$this->fileBackend = new File(
$context,
$registry,
Expand All @@ -63,7 +72,8 @@ public function setUp()
$uploaderFactory,
$requestData,
$filesystem,
$this->urlBuilder
$this->urlBuilder,
$this->databaseHelper
);

$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
Expand Down Expand Up @@ -196,6 +206,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 76e74ca

Please sign in to comment.