Skip to content

Commit

Permalink
Merge pull request #474 from magento-tango/MAGETWO-38049
Browse files Browse the repository at this point in the history
[Tango-API] Bug fix 38049 backend links in frontend emails
  • Loading branch information
Kovsher, Iurii(ikovsher) committed Jul 15, 2015
2 parents da06802 + 9db1988 commit 35a2d53
Show file tree
Hide file tree
Showing 24 changed files with 116 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
-->
<!--@subject Welcome to {{var store.getFrontendName()}} @-->
<!--@vars {
"var store.getUrl('customer/account/')":"Customer Account URL",
"var this.getUrl($store, 'customer/account/')":"Customer Account URL",
"var customer.email":"Customer Email",
"var customer.name":"Customer Name"
} @-->
Expand All @@ -19,7 +19,7 @@
{{trans
'To sign in to our site, use these credentials during checkout or on the <a href="%customer_url">My Account</a> page:'

customer_url=$store.getUrl('customer/account/')
customer_url=$this.getUrl($store,'customer/account/')
|raw}}
</p>
<ul>
Expand All @@ -30,7 +30,7 @@
{{trans
'Forgot your account password? Click <a href="%reset_url">here</a> to reset it.'

reset_url="$store.getUrl('customer/account/createPassword/', [_query:[id:$customer.id, token:$customer.rp_token]])"
reset_url="$this.getUrl($store,'customer/account/createPassword/',[_query:[id:$customer.id,token:$customer.rp_token]])"
|raw}}
</p>
<p>{{trans "When you sign in to your account, you will be able to:"}}</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
-->
<!--@subject Please confirm your {{var store.getFrontendName()}} account @-->
<!--@vars {
"var store.getUrl('customer/account/confirm/', [_query:[id:$customer.id, key:$customer.confirmation, back_url:$back_url]])":"Account Confirmation URL",
"var store.getUrl('customer/account/')":"Customer Account URL",
"var this.getUrl($store, 'customer/account/confirm/', [_query:[id:$customer.id, key:$customer.confirmation, back_url:$back_url]])":"Account Confirmation URL",
"var this.getUrl($store, 'customer/account/')":"Customer Account URL",
"var customer.email":"Customer Email",
"var customer.name":"Customer Name"
} @-->
Expand All @@ -23,7 +23,7 @@
<table class="inner-wrapper" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td align="center">
<a href="{{var store.getUrl('customer/account/confirm/', [_query:[id:$customer.id, key:$customer.confirmation, back_url:$back_url]])}}" target="_blank">{{trans "Confirm Your Account"}}</a>
<a href="{{var this.getUrl($store,'customer/account/confirm/',[_query:[id:$customer.id,key:$customer.confirmation,back_url:$back_url]])}}" target="_blank">{{trans "Confirm Your Account"}}</a>
</td>
</tr>
</table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
-->
<!--@subject Welcome to {{var store.getFrontendName()}} @-->
<!--@vars {
"var store.getUrl('customer/account/')":"Customer Account URL",
"var this.getUrl($store, 'customer/account/')":"Customer Account URL",
"var customer.email":"Customer Email",
"var customer.name":"Customer Name"
} @-->
Expand All @@ -18,7 +18,7 @@
{{trans
'To sign in to our site, use these credentials during checkout or on the <a href="%customer_url">My Account</a> page:'

customer_url=$store.getUrl('customer/account/')
customer_url=$this.getUrl($store,'customer/account/')
|raw}}
</p>
<ul>
Expand All @@ -29,7 +29,7 @@
{{trans
'Forgot your account password? Click <a href="%reset_url">here</a> to reset it.'

reset_url="$store.getUrl('customer/account/createPassword/', [_query:[id:$customer.id, token:$customer.rp_token]])"
reset_url="$this.getUrl($store,'customer/account/createPassword/',[_query:[id:$customer.id,token:$customer.rp_token]])"
|raw}}
</p>
<p>{{trans "When you sign in to your account, you will be able to:"}}</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
-->
<!--@subject Reset your {{var store.getFrontendName()}} password @-->
<!--@vars {
"var store.getUrl('customer/account/')":"Customer Account URL",
"var this.getUrl(store, 'customer/account/')":"Customer Account URL",
"var customer.name":"Customer Name"
} @-->
{{template config_path="design/email/header_template"}}
Expand All @@ -21,7 +21,7 @@
<table class="inner-wrapper" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td align="center">
<a href="{{var store.getUrl('customer/account/createPassword', [_query:[id:$customer.id, token:$customer.rp_token]])}}" target="_blank">{{trans "Reset Password"}}</a>
<a href="{{var this.getUrl($store,'customer/account/createPassword',[_query:[id:$customer.id,token:$customer.rp_token]])}}" target="_blank">{{trans "Reset Password"}}</a>
</td>
</tr>
</table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<!--@subject Reset your {{var store.getFrontendName()}} password @-->
<!--@vars {
"var customer.name":"Customer Name",
"var store.getUrl('customer/account/createPassword/', [_query:[id:$customer.id, token:$customer.rp_token]])":"Reset Password URL"
"var this.getUrl($store, 'customer/account/createPassword/', [_query:[id:$customer.id, token:$customer.rp_token]])":"Reset Password URL"
} @-->
{{template config_path="design/email/header_template"}}

Expand All @@ -21,7 +21,7 @@
<table class="inner-wrapper" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td align="center">
<a href="{{var store.getUrl('customer/account/createPassword/', [_query:[id:$customer.id, token:$customer.rp_token]])}}" target="_blank">{{trans "Reset Password"}}</a>
<a href="{{var this.getUrl($store,'customer/account/createPassword/',[_query:[id:$customer.id,token:$customer.rp_token]])}}" target="_blank">{{trans "Reset Password"}}</a>
</td>
</tr>
</table>
Expand Down
33 changes: 28 additions & 5 deletions app/code/Magento/Email/Model/AbstractTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ abstract class AbstractTemplate extends AbstractModel implements TemplateTypesIn
*/
protected $emailConfig;

/**
* @var \Magento\Framework\UrlInterface
*/
private $urlModel;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\View\DesignInterface $design
Expand All @@ -157,6 +162,7 @@ abstract class AbstractTemplate extends AbstractModel implements TemplateTypesIn
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param Template\Config $emailConfig
* @param \Magento\Email\Model\TemplateFactory $templateFactory
* @param \Magento\Framework\Url $urlModel
* @param array $data
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
Expand All @@ -172,6 +178,7 @@ public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
Template\Config $emailConfig,
TemplateFactory $templateFactory,
\Magento\Framework\UrlInterface $urlModel,
array $data = []
) {
$this->design = $design;
Expand All @@ -184,6 +191,7 @@ public function __construct(
$this->scopeConfig = $scopeConfig;
$this->emailConfig = $emailConfig;
$this->templateFactory = $templateFactory;
$this->urlModel = $urlModel;
parent::__construct($context, $registry, null, null, $data);
}

Expand Down Expand Up @@ -645,11 +653,9 @@ public function getTemplateFilter()
{
if (empty($this->templateFilter)) {
$this->templateFilter = $this->getFilterFactory()->create();
$this->templateFilter->setUseAbsoluteLinks(
$this->getUseAbsoluteLinks()
)->setStoreId(
$this->getDesignConfig()->getStore()
);
$this->templateFilter->setUseAbsoluteLinks($this->getUseAbsoluteLinks())
->setStoreId($this->getDesignConfig()->getStore())
->setUrlModel($this->urlModel);
}
return $this->templateFilter;
}
Expand Down Expand Up @@ -716,4 +722,21 @@ abstract protected function getFilterFactory();
* @return int|string
*/
abstract public function getType();

/**
* Generate URL for the specified store.
*
* @param \Magento\Store\Model\Store $store
* @param string $route
* @param array $params
* @return string
*/
public function getUrl(\Magento\Store\Model\Store $store, $route = '', $params = [])
{
$url = $this->urlModel->setScope($store);
if ($this->storeManager->getStore()->getId() != $store->getId()) {
$params['_scope_to_url'] = true;
}
return $url->getUrl($route, $params);
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/Email/Model/BackendTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class BackendTemplate extends Template
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Email\Model\Template\Config $emailConfig
* @param \Magento\Email\Model\TemplateFactory $templateFactory
* @param \Magento\Framework\UrlInterface $urlModel
* @param \Magento\Email\Model\Template\FilterFactory $filterFactory
* @param \Magento\Config\Model\Config\Structure $structure
* @param array $data
Expand All @@ -47,6 +48,7 @@ public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Email\Model\Template\Config $emailConfig,
\Magento\Email\Model\TemplateFactory $templateFactory,
\Magento\Framework\UrlInterface $urlModel,
\Magento\Email\Model\Template\FilterFactory $filterFactory,
\Magento\Config\Model\Config\Structure $structure,
array $data = []
Expand All @@ -63,6 +65,7 @@ public function __construct(
$scopeConfig,
$emailConfig,
$templateFactory,
$urlModel,
$filterFactory,
$data
);
Expand Down
7 changes: 5 additions & 2 deletions app/code/Magento/Email/Model/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ class Template extends AbstractTemplate implements \Magento\Framework\Mail\Templ
protected $_sendingException = null;

/**
* Constructor
*
* Email filter factory
*
* @var \Magento\Email\Model\Template\FilterFactory
*/
private $filterFactory;

/**
* Initialize dependencies
*
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\View\DesignInterface $design
* @param \Magento\Framework\Registry $registry
Expand All @@ -117,6 +117,7 @@ class Template extends AbstractTemplate implements \Magento\Framework\Mail\Templ
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param Template\Config $emailConfig
* @param \Magento\Email\Model\TemplateFactory $templateFactory
* @param \Magento\Framework\UrlInterface $urlModel
* @param \Magento\Email\Model\Template\FilterFactory $filterFactory
* @param array $data
*
Expand All @@ -133,6 +134,7 @@ public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Email\Model\Template\Config $emailConfig,
\Magento\Email\Model\TemplateFactory $templateFactory,
\Magento\Framework\UrlInterface $urlModel,
\Magento\Email\Model\Template\FilterFactory $filterFactory,
array $data = []
) {
Expand All @@ -148,6 +150,7 @@ public function __construct(
$scopeConfig,
$emailConfig,
$templateFactory,
$urlModel,
$data
);
}
Expand Down
29 changes: 13 additions & 16 deletions app/code/Magento/Email/Model/Template/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ class Filter extends \Magento\Framework\Filter\Template
protected $_appState;

/**
* @var \Magento\Backend\Model\UrlInterface
* @var \Magento\Framework\UrlInterface
*/
protected $backendUrlBuilder;
protected $urlModel;

/**
* @var \Pelago\Emogrifier
Expand All @@ -156,7 +156,7 @@ class Filter extends \Magento\Framework\Filter\Template
* @param \Magento\Framework\View\LayoutInterface $layout
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
* @param \Magento\Framework\App\State $appState
* @param \Magento\Backend\Model\UrlInterface $backendUrlBuilder
* @param \Magento\Framework\UrlInterface $urlModel
* @param \Pelago\Emogrifier $emogrifier
* @param array $variables
*
Expand All @@ -173,7 +173,7 @@ public function __construct(
\Magento\Framework\View\LayoutInterface $layout,
\Magento\Framework\View\LayoutFactory $layoutFactory,
\Magento\Framework\App\State $appState,
\Magento\Backend\Model\UrlInterface $backendUrlBuilder,
\Magento\Framework\UrlInterface $urlModel,
\Pelago\Emogrifier $emogrifier,
$variables = []
) {
Expand All @@ -187,7 +187,7 @@ public function __construct(
$this->_layout = $layout;
$this->_layoutFactory = $layoutFactory;
$this->_appState = $appState;
$this->backendUrlBuilder = $backendUrlBuilder;
$this->urlModel = $urlModel;
$this->emogrifier = $emogrifier;
parent::__construct($string, $variables);
}
Expand Down Expand Up @@ -495,22 +495,19 @@ public function storeDirective($construction)
unset($params['url']);
}

return $this->getUrl($path, $params);
return $this->urlModel->getUrl($path, $params);
}

/**
* @param string $path
* @param array $params
* @return string
* Set current URL model, which will be used for URLs generation.
*
* @param \Magento\Framework\UrlInterface $urlModel
* @return $this
*/
protected function getUrl($path, $params)
public function setUrlModel(\Magento\Framework\UrlInterface $urlModel)
{
$isBackendStore = \Magento\Store\Model\Store::DEFAULT_STORE_ID === $this->getStoreId()
|| \Magento\Store\Model\Store::ADMIN_CODE === $this->getStoreId();

return $isBackendStore
? $this->backendUrlBuilder->getUrl($path, $params)
: $this->_storeManager->getStore($this->getStoreId())->getUrl($path, $params);
$this->urlModel = $urlModel;
return $this;
}

/**
Expand Down
12 changes: 11 additions & 1 deletion app/code/Magento/Email/Test/Unit/Model/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class TemplateTest extends \PHPUnit_Framework_TestCase
*/
private $filterFactory;

/**
* @var \Magento\Framework\Url|\PHPUnit_Framework_MockObject_MockObject
*/
private $urlModel;

/**
* @var \Magento\Email\Model\Template\Config|\PHPUnit_Framework_MockObject_MockObject
*/
Expand Down Expand Up @@ -103,6 +108,9 @@ public function setUp()
$this->templateFactory = $this->getMockBuilder('Magento\Email\Model\TemplateFactory')
->disableOriginalConstructor()
->getMock();
$this->urlModel = $this->getMockBuilder('Magento\Framework\Url')
->disableOriginalConstructor()
->getMock();
$this->filterFactory = $this->getMockBuilder('Magento\Email\Model\Template\FilterFactory')
->setMethods(['create'])
->disableOriginalConstructor()
Expand Down Expand Up @@ -131,6 +139,7 @@ protected function getModelMock(array $mockedMethods = [])
$this->scopeConfig,
$this->emailConfig,
$this->templateFactory,
$this->urlModel,
$this->filterFactory
]
)
Expand Down Expand Up @@ -160,7 +169,7 @@ public function testSetAndGetTemplateFilter()
public function testGetTemplateFilterWithEmptyValue()
{
$filterTemplate = $this->getMockBuilder('Magento\Framework\Filter\Template')
->setMethods(['setUseAbsoluteLinks', 'setStoreId'])
->setMethods(['setUseAbsoluteLinks', 'setStoreId', 'setUrlModel'])
->disableOriginalConstructor()
->getMock();
$filterTemplate->expects($this->once())
Expand Down Expand Up @@ -737,6 +746,7 @@ public function testGetType($templateType, $expectedResult)
$this->getMock('Magento\Framework\App\Config\ScopeConfigInterface'),
$emailConfig,
$this->getMock('Magento\Email\Model\TemplateFactory', [], [], '', false),
$this->getMock('Magento\Framework\Url', [], [], '', false),
$this->getMock('Magento\Email\Model\Template\FilterFactory', [], [], '', false),
['template_id' => 10],
]
Expand Down
10 changes: 10 additions & 0 deletions app/code/Magento/Email/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@
<preference for="Magento\Framework\Mail\TransportInterface" type="Magento\Framework\Mail\Transport" />
<preference for="Magento\Framework\Mail\MessageInterface" type="Magento\Framework\Mail\Message" />
<preference for="Magento\Framework\Mail\Template\SenderResolverInterface" type="Magento\Email\Model\Template\SenderResolver" />
<type name="Magento\Email\Model\BackendTemplate">
<arguments>
<argument name="urlModel" xsi:type="object" shared="false">Magento\Backend\Model\Url</argument>
</arguments>
</type>
<type name="Magento\Email\Model\Template">
<arguments>
<argument name="urlModel" xsi:type="object" shared="false">Magento\Framework\Url</argument>
</arguments>
</type>
</config>
Loading

0 comments on commit 35a2d53

Please sign in to comment.