Skip to content

Commit 7d30371

Browse files
authored
Merge pull request #4268 from magento-obsessive-owls/MC-15977
[Owls] MC-15977: Email template preview
2 parents a4a5f34 + 6931e81 commit 7d30371

File tree

2 files changed

+135
-58
lines changed

2 files changed

+135
-58
lines changed

app/code/Magento/Email/Block/Adminhtml/Template/Preview.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,26 @@ public function __construct(
5555
* Prepare html output
5656
*
5757
* @return string
58+
* @throws \Magento\Framework\Exception\LocalizedException
5859
*/
5960
protected function _toHtml()
6061
{
62+
$request = $this->getRequest();
63+
64+
if (!$request instanceof \Magento\Framework\App\RequestSafetyInterface || !$request->isSafeMethod()) {
65+
throw new \Magento\Framework\Exception\LocalizedException(__('Wrong request.'));
66+
}
67+
6168
$storeId = $this->getAnyStoreView()->getId();
6269
/** @var $template \Magento\Email\Model\Template */
6370
$template = $this->_emailFactory->create();
6471

65-
if ($id = (int)$this->getRequest()->getParam('id')) {
72+
if ($id = (int)$request->getParam('id')) {
6673
$template->load($id);
6774
} else {
68-
$template->setTemplateType($this->getRequest()->getParam('type'));
69-
$template->setTemplateText($this->getRequest()->getParam('text'));
70-
$template->setTemplateStyles($this->getRequest()->getParam('styles'));
75+
$template->setTemplateType($request->getParam('type'));
76+
$template->setTemplateText($request->getParam('text'));
77+
$template->setTemplateStyles($request->getParam('styles'));
7178
}
7279

7380
\Magento\Framework\Profiler::start($this->profilerName);

app/code/Magento/Email/Test/Unit/Block/Adminhtml/Template/PreviewTest.php

Lines changed: 124 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,105 +18,175 @@ class PreviewTest extends \PHPUnit\Framework\TestCase
1818

1919
const MALICIOUS_TEXT = 'test malicious';
2020

21+
/**
22+
* @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
23+
*/
24+
protected $request;
25+
26+
/**
27+
* @var \Magento\Email\Block\Adminhtml\Template\Preview
28+
*/
29+
protected $preview;
30+
31+
/**
32+
* @var \Magento\Framework\Filter\Input\MaliciousCode|\PHPUnit_Framework_MockObject_MockObject
33+
*/
34+
protected $maliciousCode;
35+
36+
/**
37+
* @var \Magento\Email\Model\Template|\PHPUnit_Framework_MockObject_MockObject
38+
*/
39+
protected $template;
40+
41+
/**
42+
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
43+
*/
44+
protected $storeManager;
45+
2146
/**
2247
* Init data
2348
*/
2449
protected function setUp()
2550
{
2651
$this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
27-
}
2852

29-
/**
30-
* Check of processing email templates
31-
*
32-
* @param array $requestParamMap
33-
*
34-
* @dataProvider toHtmlDataProvider
35-
* @param $requestParamMap
36-
*/
37-
public function testToHtml($requestParamMap)
38-
{
3953
$storeId = 1;
40-
$template = $this->getMockBuilder(\Magento\Email\Model\Template::class)
41-
->setMethods([
42-
'setDesignConfig',
43-
'getDesignConfig',
44-
'__wakeup',
45-
'getProcessedTemplate',
46-
'getAppState',
47-
'revertDesign'
48-
])
54+
$designConfigData = [];
55+
56+
$this->template = $this->getMockBuilder(\Magento\Email\Model\Template::class)
57+
->setMethods(
58+
[
59+
'setDesignConfig',
60+
'getDesignConfig',
61+
'__wakeup',
62+
'getProcessedTemplate',
63+
'getAppState',
64+
'revertDesign'
65+
]
66+
)
67+
->disableOriginalConstructor()
68+
->getMock();
69+
70+
$this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
4971
->disableOriginalConstructor()
5072
->getMock();
51-
$template->expects($this->once())
73+
74+
$this->request = $this->createMock(\Magento\Framework\App\Request\Http::class);
75+
76+
$this->maliciousCode = $this->createPartialMock(
77+
\Magento\Framework\Filter\Input\MaliciousCode::class,
78+
['filter']
79+
);
80+
81+
$this->template->expects($this->once())
5282
->method('getProcessedTemplate')
5383
->with($this->equalTo([]))
5484
->willReturn(self::MALICIOUS_TEXT);
55-
$designConfigData = [];
56-
$template->expects($this->atLeastOnce())
57-
->method('getDesignConfig')
58-
->willReturn(new \Magento\Framework\DataObject(
59-
$designConfigData
60-
));
85+
86+
$this->template->method('getDesignConfig')
87+
->willReturn(new \Magento\Framework\DataObject($designConfigData));
88+
6189
$emailFactory = $this->createPartialMock(\Magento\Email\Model\TemplateFactory::class, ['create']);
6290
$emailFactory->expects($this->any())
6391
->method('create')
64-
->willReturn($template);
92+
->willReturn($this->template);
6593

66-
$request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
67-
$request->expects($this->any())->method('getParam')->willReturnMap($requestParamMap);
6894
$eventManage = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
6995
$scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
7096
$design = $this->createMock(\Magento\Framework\View\DesignInterface::class);
7197
$store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['getId', '__wakeup']);
72-
$store->expects($this->any())->method('getId')->willReturn($storeId);
73-
$storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
74-
->disableOriginalConstructor()
75-
->getMock();
76-
$storeManager->expects($this->atLeastOnce())
77-
->method('getDefaultStoreView')
98+
99+
$store->expects($this->any())
100+
->method('getId')
101+
->willReturn($storeId);
102+
103+
$this->storeManager->method('getDefaultStoreView')
78104
->willReturn($store);
79-
$storeManager->expects($this->any())->method('getDefaultStoreView')->willReturn(null);
80-
$storeManager->expects($this->any())->method('getStores')->willReturn([$store]);
105+
106+
$this->storeManager->expects($this->any())->method('getDefaultStoreView')->willReturn(null);
107+
$this->storeManager->expects($this->any())->method('getStores')->willReturn([$store]);
81108
$appState = $this->getMockBuilder(\Magento\Framework\App\State::class)
82-
->setConstructorArgs([
83-
$scopeConfig
84-
])
109+
->setConstructorArgs(
110+
[
111+
$scopeConfig
112+
]
113+
)
85114
->setMethods(['emulateAreaCode'])
86115
->disableOriginalConstructor()
87116
->getMock();
88117
$appState->expects($this->any())
89118
->method('emulateAreaCode')
90-
->with(\Magento\Email\Model\AbstractTemplate::DEFAULT_DESIGN_AREA, [$template, 'getProcessedTemplate'])
91-
->willReturn($template->getProcessedTemplate());
119+
->with(
120+
\Magento\Email\Model\AbstractTemplate::DEFAULT_DESIGN_AREA,
121+
[$this->template, 'getProcessedTemplate']
122+
)
123+
->willReturn($this->template->getProcessedTemplate());
92124

93125
$context = $this->createPartialMock(
94126
\Magento\Backend\Block\Template\Context::class,
95127
['getRequest', 'getEventManager', 'getScopeConfig', 'getDesignPackage', 'getStoreManager', 'getAppState']
96128
);
97-
$context->expects($this->any())->method('getRequest')->willReturn($request);
129+
$context->expects($this->any())->method('getRequest')->willReturn($this->request);
98130
$context->expects($this->any())->method('getEventManager')->willReturn($eventManage);
99131
$context->expects($this->any())->method('getScopeConfig')->willReturn($scopeConfig);
100132
$context->expects($this->any())->method('getDesignPackage')->willReturn($design);
101-
$context->expects($this->any())->method('getStoreManager')->willReturn($storeManager);
133+
$context->expects($this->any())->method('getStoreManager')->willReturn($this->storeManager);
102134
$context->expects($this->once())->method('getAppState')->willReturn($appState);
103135

104-
$maliciousCode = $this->createPartialMock(\Magento\Framework\Filter\Input\MaliciousCode::class, ['filter']);
105-
$maliciousCode->expects($this->once())
106-
->method('filter')
107-
->with($this->equalTo($requestParamMap[1][2]))
108-
->willReturn(self::MALICIOUS_TEXT);
109-
110136
/** @var \Magento\Email\Block\Adminhtml\Template\Preview $preview */
111-
$preview = $this->objectManagerHelper->getObject(
137+
$this->preview = $this->objectManagerHelper->getObject(
112138
\Magento\Email\Block\Adminhtml\Template\Preview::class,
113139
[
114140
'context' => $context,
115-
'maliciousCode' => $maliciousCode,
141+
'maliciousCode' => $this->maliciousCode,
116142
'emailFactory' => $emailFactory
117143
]
118144
);
119-
$this->assertEquals(self::MALICIOUS_TEXT, $preview->toHtml());
145+
}
146+
147+
/**
148+
* Check of processing email templates
149+
*
150+
* @param array $requestParamMap
151+
* @dataProvider toHtmlDataProvider
152+
*/
153+
public function testToHtml($requestParamMap)
154+
{
155+
$this->request->expects($this->atLeastOnce())
156+
->method('isSafeMethod')
157+
->willReturn(true);
158+
$this->request->expects($this->any())
159+
->method('getParam')
160+
->willReturnMap($requestParamMap);
161+
$this->template
162+
->expects($this->atLeastOnce())
163+
->method('getDesignConfig');
164+
$this->storeManager->expects($this->atLeastOnce())
165+
->method('getDefaultStoreView');
166+
$this->maliciousCode->expects($this->once())
167+
->method('filter')
168+
->with($this->equalTo($requestParamMap[1][2]))
169+
->willReturn(self::MALICIOUS_TEXT);
170+
171+
$this->assertEquals(self::MALICIOUS_TEXT, $this->preview->toHtml());
172+
}
173+
174+
/**
175+
* @expectedException \Magento\Framework\Exception\LocalizedException
176+
*/
177+
public function testToHtmlWithException()
178+
{
179+
$this->request->expects($this->atLeastOnce())
180+
->method('isSafeMethod')
181+
->willReturn(false);
182+
$this->template
183+
->expects($this->never())
184+
->method('getDesignConfig');
185+
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
186+
$this->expectExceptionMessage(
187+
(string)__('Wrong request.')
188+
);
189+
$this->preview->toHtml();
120190
}
121191

122192
/**

0 commit comments

Comments
 (0)