Skip to content

Commit 3eeb2f0

Browse files
ENGCOM-4828: [2.3] Database Media Storage - Transactional Emails will now extract image from database in Database Media Storage mode #21674
- Merge Pull Request #21674 from gwharton/magento2:2.3-develop-dbmediastorage3 - Merged commits: 1. c764ca3 2. 057c13d 3. 1c05b79
2 parents 211dd25 + 1c05b79 commit 3eeb2f0

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

app/code/Magento/Email/Model/AbstractTemplate.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
use Magento\Store\Model\Information as StoreInformation;
1515
use Magento\Store\Model\ScopeInterface;
1616
use Magento\Store\Model\Store;
17+
use Magento\MediaStorage\Helper\File\Storage\Database;
1718

1819
/**
19-
* Template model class
20+
* Template model class.
2021
*
22+
* phpcs:disable Magento2.Classes.AbstractApi
2123
* @author Magento Core Team <core@magentocommerce.com>
2224
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2325
* @SuppressWarnings(PHPMD.TooManyFields)
@@ -163,6 +165,11 @@ abstract class AbstractTemplate extends AbstractModel implements TemplateTypesIn
163165
*/
164166
private $urlModel;
165167

168+
/**
169+
* @var Database
170+
*/
171+
private $fileStorageDatabase;
172+
166173
/**
167174
* @param \Magento\Framework\Model\Context $context
168175
* @param \Magento\Framework\View\DesignInterface $design
@@ -177,6 +184,7 @@ abstract class AbstractTemplate extends AbstractModel implements TemplateTypesIn
177184
* @param \Magento\Framework\Filter\FilterManager $filterManager
178185
* @param \Magento\Framework\UrlInterface $urlModel
179186
* @param array $data
187+
* @param Database $fileStorageDatabase
180188
*
181189
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
182190
*/
@@ -193,7 +201,8 @@ public function __construct(
193201
\Magento\Email\Model\TemplateFactory $templateFactory,
194202
\Magento\Framework\Filter\FilterManager $filterManager,
195203
\Magento\Framework\UrlInterface $urlModel,
196-
array $data = []
204+
array $data = [],
205+
Database $fileStorageDatabase = null
197206
) {
198207
$this->design = $design;
199208
$this->area = isset($data['area']) ? $data['area'] : null;
@@ -207,6 +216,8 @@ public function __construct(
207216
$this->templateFactory = $templateFactory;
208217
$this->filterManager = $filterManager;
209218
$this->urlModel = $urlModel;
219+
$this->fileStorageDatabase = $fileStorageDatabase ?:
220+
\Magento\Framework\App\ObjectManager::getInstance()->get(Database::class);
210221
parent::__construct($context, $registry, null, null, $data);
211222
}
212223

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

491507
/**
492508
* Apply design config so that emails are processed within the context of the appropriate area/store/theme.
493-
* Can be called multiple times without issue.
494509
*
495510
* @return bool
496511
*/
@@ -664,8 +679,7 @@ public function getTemplateFilter()
664679
}
665680

666681
/**
667-
* Save current design config and replace with design config from specified store
668-
* Event is not dispatched.
682+
* Save current design config and replace with design config from specified store. Event is not dispatched.
669683
*
670684
* @param null|bool|int|string $storeId
671685
* @param string $area

app/code/Magento/Email/Test/Unit/Model/BackendTemplateTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use Magento\Email\Model\BackendTemplate;
1313
use Magento\Framework\ObjectManagerInterface;
1414

15+
/**
16+
* Tests for adminhtml email template model.
17+
*/
1518
class BackendTemplateTest extends \PHPUnit\Framework\TestCase
1619
{
1720
/**
@@ -46,6 +49,11 @@ class BackendTemplateTest extends \PHPUnit\Framework\TestCase
4649
*/
4750
private $serializerMock;
4851

52+
/**
53+
* @var \Magento\MediaStorage\Helper\File\Storage\Database|\PHPUnit_Framework_MockObject_MockObject
54+
*/
55+
private $databaseHelperMock;
56+
4957
protected function setUp()
5058
{
5159
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -56,6 +64,7 @@ protected function setUp()
5664
$this->structureMock = $this->createMock(\Magento\Config\Model\Config\Structure::class);
5765
$this->structureMock->expects($this->any())->method('getFieldPathsByAttribute')->willReturn(['path' => 'test']);
5866

67+
$this->databaseHelperMock = $this->createMock(\Magento\MediaStorage\Helper\File\Storage\Database::class);
5968
$this->resourceModelMock = $this->createMock(\Magento\Email\Model\ResourceModel\Template::class);
6069
$this->resourceModelMock->expects($this->any())
6170
->method('getSystemConfigByPathsAndTemplateId')
@@ -64,8 +73,18 @@ protected function setUp()
6473
$objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
6574
$objectManagerMock->expects($this->any())
6675
->method('get')
67-
->with(\Magento\Email\Model\ResourceModel\Template::class)
68-
->will($this->returnValue($this->resourceModelMock));
76+
->willReturnCallback(
77+
function ($value) {
78+
switch ($value) {
79+
case \Magento\MediaStorage\Helper\File\Storage\Database::class:
80+
return ($this->databaseHelperMock);
81+
case \Magento\Email\Model\ResourceModel\Template::class:
82+
return ($this->resourceModelMock);
83+
default:
84+
return(null);
85+
}
86+
}
87+
);
6988

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

app/code/Magento/Email/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"magento/module-config": "*",
1313
"magento/module-store": "*",
1414
"magento/module-theme": "*",
15+
"magento/module-media-storage": "*",
1516
"magento/module-variable": "*"
1617
},
1718
"suggest": {

0 commit comments

Comments
 (0)