Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #385 from magento-frontend/bugs-pr
Bugs MAGETWO-57656 Base url should be processed while sending email
- Loading branch information
Showing
6 changed files
with
296 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/** | ||
* Copyright © 2016 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Email\Model\Template\Css; | ||
|
||
use Magento\Framework\View\Asset\NotationResolver\Variable; | ||
use Magento\Framework\View\Asset\Repository; | ||
|
||
class Processor | ||
{ | ||
/** | ||
* @var Repository | ||
*/ | ||
private $assetRepository; | ||
|
||
/** | ||
* @param Repository $assetRepository | ||
*/ | ||
public function __construct(Repository $assetRepository) | ||
{ | ||
$this->assetRepository = $assetRepository; | ||
} | ||
|
||
/** | ||
* Process css placeholders | ||
* | ||
* @param string $css | ||
* @return string | ||
*/ | ||
public function process($css) | ||
{ | ||
$matches = []; | ||
if (preg_match_all(Variable::VAR_REGEX, $css, $matches, PREG_SET_ORDER)) { | ||
$replacements = []; | ||
foreach ($matches as $match) { | ||
if (!isset($replacements[$match[0]])) { | ||
$replacements[$match[0]] = $this->getPlaceholderValue($match[1]); | ||
} | ||
} | ||
$css = str_replace(array_keys($replacements), $replacements, $css); | ||
} | ||
return $css; | ||
} | ||
|
||
/** | ||
* Retrieve placeholder value | ||
* | ||
* @param string $placeholder | ||
* @return string | ||
*/ | ||
private function getPlaceholderValue($placeholder) | ||
{ | ||
/** @var \Magento\Framework\View\Asset\File\FallbackContext $context */ | ||
$context = $this->assetRepository->getStaticViewFileContext(); | ||
|
||
switch ($placeholder) { | ||
case 'base_url_path': | ||
return $context->getBaseUrl(); | ||
case 'locale': | ||
return $context->getLocale(); | ||
default: | ||
return ''; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
app/code/Magento/Email/Test/Unit/Model/Template/Css/ProcessorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/** | ||
* Copyright © 2016 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Email\Test\Unit\Model\Template\Css; | ||
|
||
use Magento\Email\Model\Template\Css\Processor; | ||
use Magento\Framework\View\Asset\File\FallbackContext; | ||
use Magento\Framework\View\Asset\Repository; | ||
|
||
class ProcessorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var Processor | ||
*/ | ||
protected $processor; | ||
|
||
/** | ||
* @var Repository|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
protected $assetRepository; | ||
|
||
/** | ||
* @var FallbackContext|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
protected $fallbackContext; | ||
|
||
public function setUp() | ||
{ | ||
$this->assetRepository = $this->getMockBuilder(Repository::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$this->fallbackContext = $this->getMockBuilder(FallbackContext::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->processor = new Processor($this->assetRepository); | ||
} | ||
|
||
public function testProcess() | ||
{ | ||
$url = 'http://magento.local/pub/static/'; | ||
$locale = 'en_US'; | ||
$css = '@import url("{{base_url_path}}frontend/_view/{{locale}}/css/email.css");'; | ||
$expectedCss = '@import url("' . $url . 'frontend/_view/' . $locale . '/css/email.css");'; | ||
|
||
$this->assetRepository->expects($this->exactly(2)) | ||
->method('getStaticViewFileContext') | ||
->willReturn($this->fallbackContext); | ||
$this->fallbackContext->expects($this->once()) | ||
->method('getBaseUrl') | ||
->willReturn($url); | ||
$this->fallbackContext->expects($this->once()) | ||
->method('getLocale') | ||
->willReturn($locale); | ||
$this->assertEquals($expectedCss, $this->processor->process($css)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.