Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions app/code/Magento/Email/Model/Template/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\State;
use Magento\Framework\Css\PreProcessor\Adapter\CssInliner;
use Magento\Framework\DataObject;
use Magento\Framework\Escaper;
use Magento\Framework\Exception\MailException;
use Magento\Framework\Exception\NoSuchEntityException;
Expand Down Expand Up @@ -192,6 +193,11 @@ class Filter extends Template
*/
private $storeInformation;

/**
* @var DataObject[]
*/
private $storeInformationObjects = [];

/**
* @var StateInterface
*/
Expand Down Expand Up @@ -855,26 +861,43 @@ public function configDirective($construction)
$configValue = '';
$params = $this->getParameters($construction[2]);
$storeId = $this->getStoreId();
$store = $this->_storeManager->getStore($storeId);
$storeInformationObj = $this->storeInformation
->getStoreInformationObject($store);
if (isset($params['path']) && $this->isAvailableConfigVariable($params['path'])) {
$configValue = $this->_scopeConfig->getValue(
$params['path'],
ScopeInterface::SCOPE_STORE,
$storeId
);
if ($params['path'] == $this->storeInformation::XML_PATH_STORE_INFO_COUNTRY_CODE) {
$configValue = $storeInformationObj->getData('country');
$configValue = $this->getStoreInformationObject($storeId)->getData('country');
} elseif ($params['path'] == $this->storeInformation::XML_PATH_STORE_INFO_REGION_CODE) {
$configValue = $storeInformationObj->getData('region') ?
$storeInformationObj->getData('region') :
$configValue;
$region = $this->getStoreInformationObject($storeId)->getData('region');
$configValue = $region ?: $configValue;
}
}
return $configValue;
}

/**
* Retrieve store information object, resolving it at most once per store.
*
* Store information resolution loads country and region entities from the database, so it must only happen
* for the config paths that actually need it.
*
* @param int|string|null $storeId
* @return DataObject
* @throws NoSuchEntityException
*/
private function getStoreInformationObject($storeId): DataObject
{
$cacheKey = (string)$storeId;
if (!isset($this->storeInformationObjects[$cacheKey])) {
$this->storeInformationObjects[$cacheKey] = $this->storeInformation
->getStoreInformationObject($this->_storeManager->getStore($storeId));
}

return $this->storeInformationObjects[$cacheKey];
}

/**
* Check if given variable is available for directive "Config"
*
Expand Down
45 changes: 39 additions & 6 deletions app/code/Magento/Email/Test/Unit/Model/Template/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,8 @@ public function testConfigDirectiveAvailable()
->method('getValue')
->willReturn($scopeConfigValue);

$this->storeInformation->expects($this->once())
->method('getStoreInformationObject')
->willReturn(new DataObject([]));
$this->storeInformation->expects($this->never())
->method('getStoreInformationObject');

$this->assertEquals($scopeConfigValue, $this->getModel()->configDirective($construction));
}
Expand All @@ -467,9 +466,8 @@ public function testConfigDirectiveUnavailable()
->method('getValue')
->willReturn($scopeConfigValue);

$this->storeInformation->expects($this->once())
->method('getStoreInformationObject')
->willReturn(new DataObject([]));
$this->storeInformation->expects($this->never())
->method('getStoreInformationObject');

$this->assertEquals($scopeConfigValue, $this->getModel()->configDirective($construction));
}
Expand Down Expand Up @@ -526,6 +524,41 @@ public function testConfigDirectiveGetRegion()
$this->assertEquals($expectedRegion, $this->getModel()->configDirective($construction));
}

/**
* Store information must be resolved only once, no matter how many store information directives are rendered.
*
* @throws NoSuchEntityException
*/
public function testConfigDirectiveResolvesStoreInformationOnce()
{
$countryPath = 'general/store_information/country_id';
$regionPath = 'general/store_information/region_id';

$this->storeManager->expects($this->any())
->method('getStore')
->willReturn($this->store);
$this->store->expects($this->any())->method('getId')->willReturn(1);

$this->configVariables->expects($this->any())
->method('getAvailableVars')
->willReturn(['country' => $countryPath, 'region' => $regionPath]);

$this->storeInformation->expects($this->once())
->method('getStoreInformationObject')
->willReturn(new DataObject(['country' => 'United States', 'region' => 'Texas']));

$filter = $this->getModel();

$this->assertEquals(
'United States',
$filter->configDirective(["{{config path={$countryPath}}}", 'config', " path={$countryPath}"])
);
$this->assertEquals(
'Texas',
$filter->configDirective(["{{config path={$regionPath}}}", 'config', " path={$regionPath}"])
);
}

/**
* @throws MailException
* @throws NoSuchEntityException
Expand Down