From c764ca310cdfa890caf6fc144da2a3aa5861cbee Mon Sep 17 00:00:00 2001 From: Graham Wharton Date: Sun, 10 Mar 2019 13:59:49 +0000 Subject: [PATCH 1/3] Transactional emails are now database aware with regard to email logo image. --- .../Magento/Email/Model/AbstractTemplate.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Email/Model/AbstractTemplate.php b/app/code/Magento/Email/Model/AbstractTemplate.php index a6ecdaf24ebbb..7f9bda7ab0cdd 100644 --- a/app/code/Magento/Email/Model/AbstractTemplate.php +++ b/app/code/Magento/Email/Model/AbstractTemplate.php @@ -14,6 +14,7 @@ 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 @@ -163,6 +164,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 @@ -177,6 +183,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) */ @@ -193,7 +200,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; @@ -207,6 +215,7 @@ 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); } @@ -394,6 +403,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 From 057c13d66dc67c8291662ec5be77b87d26db781f Mon Sep 17 00:00:00 2001 From: Graham Wharton Date: Sun, 10 Mar 2019 18:38:22 +0000 Subject: [PATCH 2/3] Resolved test failures --- .../Magento/Email/Model/AbstractTemplate.php | 3 ++- .../Test/Unit/Model/BackendTemplateTest.php | 20 +++++++++++++++++-- app/code/Magento/Email/composer.json | 1 + 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Email/Model/AbstractTemplate.php b/app/code/Magento/Email/Model/AbstractTemplate.php index 7f9bda7ab0cdd..db4a5f1d8c58b 100644 --- a/app/code/Magento/Email/Model/AbstractTemplate.php +++ b/app/code/Magento/Email/Model/AbstractTemplate.php @@ -215,7 +215,8 @@ public function __construct( $this->templateFactory = $templateFactory; $this->filterManager = $filterManager; $this->urlModel = $urlModel; - $this->fileStorageDatabase = $fileStorageDatabase ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Database::class); + $this->fileStorageDatabase = $fileStorageDatabase ?: + \Magento\Framework\App\ObjectManager::getInstance()->get(Database::class); parent::__construct($context, $registry, null, null, $data); } diff --git a/app/code/Magento/Email/Test/Unit/Model/BackendTemplateTest.php b/app/code/Magento/Email/Test/Unit/Model/BackendTemplateTest.php index 31a04b0b2bbd0..bfe5005ee7351 100644 --- a/app/code/Magento/Email/Test/Unit/Model/BackendTemplateTest.php +++ b/app/code/Magento/Email/Test/Unit/Model/BackendTemplateTest.php @@ -46,6 +46,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); @@ -56,6 +61,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') @@ -64,8 +70,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); diff --git a/app/code/Magento/Email/composer.json b/app/code/Magento/Email/composer.json index 1011b16f8537d..e887adef1fbc9 100644 --- a/app/code/Magento/Email/composer.json +++ b/app/code/Magento/Email/composer.json @@ -12,6 +12,7 @@ "magento/module-config": "*", "magento/module-store": "*", "magento/module-theme": "*", + "magento/module-media-storage": "*", "magento/module-variable": "*" }, "suggest": { From 1c05b797c32d2df6b32f1baf332132e8b7e47e23 Mon Sep 17 00:00:00 2001 From: Nazarn96 Date: Tue, 30 Apr 2019 10:40:50 +0300 Subject: [PATCH 3/3] magento/magento2#21674 static-test-fix --- app/code/Magento/Email/Model/AbstractTemplate.php | 7 +++---- .../Magento/Email/Test/Unit/Model/BackendTemplateTest.php | 7 +++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/Email/Model/AbstractTemplate.php b/app/code/Magento/Email/Model/AbstractTemplate.php index db4a5f1d8c58b..5eae1d1462184 100644 --- a/app/code/Magento/Email/Model/AbstractTemplate.php +++ b/app/code/Magento/Email/Model/AbstractTemplate.php @@ -17,8 +17,9 @@ use Magento\MediaStorage\Helper\File\Storage\Database; /** - * Template model class + * Template model class. * + * phpcs:disable Magento2.Classes.AbstractApi * @author Magento Core Team * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.TooManyFields) @@ -505,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 */ @@ -679,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 diff --git a/app/code/Magento/Email/Test/Unit/Model/BackendTemplateTest.php b/app/code/Magento/Email/Test/Unit/Model/BackendTemplateTest.php index bfe5005ee7351..1ceccd4414cc0 100644 --- a/app/code/Magento/Email/Test/Unit/Model/BackendTemplateTest.php +++ b/app/code/Magento/Email/Test/Unit/Model/BackendTemplateTest.php @@ -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 { /** @@ -72,13 +75,13 @@ protected function setUp() ->method('get') ->willReturnCallback( function ($value) { - switch($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); + return(null); } } );