Skip to content

Commit

Permalink
ENGCOM-4828: [2.3] Database Media Storage - Transactional Emails will…
Browse files Browse the repository at this point in the history
… now extract image from database in Database Media Storage mode magento#21674
  • Loading branch information
p-bystritsky authored Jun 3, 2019
2 parents abbf068 + 3eeb2f0 commit cff969b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
24 changes: 19 additions & 5 deletions app/code/Magento/Email/Model/AbstractTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
use Magento\Store\Model\Information as StoreInformation;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\Store;
use Magento\MediaStorage\Helper\File\Storage\Database;

/**
* Template model class
* Template model class.
*
* phpcs:disable Magento2.Classes.AbstractApi
* @author Magento Core Team <[email protected]>
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyFields)
Expand Down Expand Up @@ -163,6 +165,11 @@ abstract class AbstractTemplate extends AbstractModel implements TemplateTypesIn
*/
private $urlModel;

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

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\View\DesignInterface $design
Expand All @@ -177,6 +184,7 @@ abstract class AbstractTemplate extends AbstractModel implements TemplateTypesIn
* @param \Magento\Framework\Filter\FilterManager $filterManager
* @param \Magento\Framework\UrlInterface $urlModel
* @param array $data
* @param Database $fileStorageDatabase
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand All @@ -193,7 +201,8 @@ public function __construct(
\Magento\Email\Model\TemplateFactory $templateFactory,
\Magento\Framework\Filter\FilterManager $filterManager,
\Magento\Framework\UrlInterface $urlModel,
array $data = []
array $data = [],
Database $fileStorageDatabase = null
) {
$this->design = $design;
$this->area = isset($data['area']) ? $data['area'] : null;
Expand All @@ -207,6 +216,8 @@ public function __construct(
$this->templateFactory = $templateFactory;
$this->filterManager = $filterManager;
$this->urlModel = $urlModel;
$this->fileStorageDatabase = $fileStorageDatabase ?:
\Magento\Framework\App\ObjectManager::getInstance()->get(Database::class);
parent::__construct($context, $registry, null, null, $data);
}

Expand Down Expand Up @@ -394,6 +405,11 @@ protected function getLogoUrl($store)
if ($fileName) {
$uploadDir = \Magento\Email\Model\Design\Backend\Logo::UPLOAD_DIR;
$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
if ($this->fileStorageDatabase->checkDbUsage() &&
!$mediaDirectory->isFile($uploadDir . '/' . $fileName)
) {
$this->fileStorageDatabase->saveFileToFilesystem($uploadDir . '/' . $fileName);
}
if ($mediaDirectory->isFile($uploadDir . '/' . $fileName)) {
return $this->storeManager->getStore()->getBaseUrl(
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
Expand Down Expand Up @@ -490,7 +506,6 @@ protected function addEmailVariables($variables, $storeId)

/**
* Apply design config so that emails are processed within the context of the appropriate area/store/theme.
* Can be called multiple times without issue.
*
* @return bool
*/
Expand Down Expand Up @@ -664,8 +679,7 @@ public function getTemplateFilter()
}

/**
* Save current design config and replace with design config from specified store
* Event is not dispatched.
* Save current design config and replace with design config from specified store. Event is not dispatched.
*
* @param null|bool|int|string $storeId
* @param string $area
Expand Down
23 changes: 21 additions & 2 deletions app/code/Magento/Email/Test/Unit/Model/BackendTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Magento\Email\Model\BackendTemplate;
use Magento\Framework\ObjectManagerInterface;

/**
* Tests for adminhtml email template model.
*/
class BackendTemplateTest extends \PHPUnit\Framework\TestCase
{
/**
Expand Down Expand Up @@ -46,6 +49,11 @@ class BackendTemplateTest extends \PHPUnit\Framework\TestCase
*/
private $serializerMock;

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

protected function setUp()
{
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
Expand All @@ -56,6 +64,7 @@ protected function setUp()
$this->structureMock = $this->createMock(\Magento\Config\Model\Config\Structure::class);
$this->structureMock->expects($this->any())->method('getFieldPathsByAttribute')->willReturn(['path' => 'test']);

$this->databaseHelperMock = $this->createMock(\Magento\MediaStorage\Helper\File\Storage\Database::class);
$this->resourceModelMock = $this->createMock(\Magento\Email\Model\ResourceModel\Template::class);
$this->resourceModelMock->expects($this->any())
->method('getSystemConfigByPathsAndTemplateId')
Expand All @@ -64,8 +73,18 @@ protected function setUp()
$objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
$objectManagerMock->expects($this->any())
->method('get')
->with(\Magento\Email\Model\ResourceModel\Template::class)
->will($this->returnValue($this->resourceModelMock));
->willReturnCallback(
function ($value) {
switch ($value) {
case \Magento\MediaStorage\Helper\File\Storage\Database::class:
return ($this->databaseHelperMock);
case \Magento\Email\Model\ResourceModel\Template::class:
return ($this->resourceModelMock);
default:
return(null);
}
}
);

\Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);

Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Email/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"magento/module-config": "*",
"magento/module-store": "*",
"magento/module-theme": "*",
"magento/module-media-storage": "*",
"magento/module-variable": "*"
},
"suggest": {
Expand Down

0 comments on commit cff969b

Please sign in to comment.