Skip to content

Commit 84a9cf2

Browse files
author
Joan He
committed
Merge remote-tracking branch 'arcticfoxes/MC-18115' into 2.3.3-develop-pr
2 parents 06ddcd8 + d713dac commit 84a9cf2

File tree

10 files changed

+268
-58
lines changed

10 files changed

+268
-58
lines changed

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

7-
/**
8-
* Adminhtml system template preview block
9-
*
10-
* @author Magento Core Team <core@magentocommerce.com>
11-
*/
128
namespace Magento\Email\Block\Adminhtml\Template;
139

1410
/**
@@ -55,16 +51,12 @@ public function __construct(
5551
* Prepare html output
5652
*
5753
* @return string
58-
* @throws \Magento\Framework\Exception\LocalizedException
54+
* @throws \Exception
5955
*/
6056
protected function _toHtml()
6157
{
6258
$request = $this->getRequest();
6359

64-
if (!$request instanceof \Magento\Framework\App\RequestSafetyInterface || !$request->isSafeMethod()) {
65-
throw new \Magento\Framework\Exception\LocalizedException(__('Wrong request.'));
66-
}
67-
6860
$storeId = $this->getAnyStoreView()->getId();
6961
/** @var $template \Magento\Email\Model\Template */
7062
$template = $this->_emailFactory->create();

app/code/Magento/Email/Controller/Adminhtml/Email/Template/Popup.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
namespace Magento\Email\Controller\Adminhtml\Email\Template;
99

10-
use Magento\Framework\App\Action\HttpGetActionInterface;
10+
use Magento\Framework\App\Action\HttpPostActionInterface;
1111

1212
/**
1313
* Rendering popup email template.
1414
*/
15-
class Popup extends \Magento\Backend\App\Action implements HttpGetActionInterface
15+
class Popup extends \Magento\Backend\App\Action implements HttpPostActionInterface
1616
{
1717
/**
1818
* @var \Magento\Framework\View\Result\PageFactory

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,28 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
declare(strict_types=1);
8+
79
namespace Magento\Email\Controller\Adminhtml\Email\Template;
810

11+
use Magento\Email\Controller\Adminhtml\Email\Template;
912
use Magento\Framework\App\Action\HttpGetActionInterface;
13+
use Magento\Framework\App\Action\HttpPostActionInterface;
1014

1115
/**
1216
* Rendering email template preview.
1317
*/
14-
class Preview extends \Magento\Email\Controller\Adminhtml\Email\Template implements HttpGetActionInterface
18+
class Preview extends Template implements HttpGetActionInterface, HttpPostActionInterface
1519
{
1620
/**
1721
* Preview transactional email action.
18-
*
19-
* @return void
2022
*/
2123
public function execute()
2224
{
2325
try {
2426
$this->_view->loadLayout();
2527
$this->_view->getPage()->getConfig()->getTitle()->prepend(__('Email Preview'));
2628
$this->_view->renderLayout();
27-
$this->getResponse()->setHeader('Content-Security-Policy', "script-src 'self'");
2829
} catch (\Exception $e) {
2930
$this->messageManager->addErrorMessage(
3031
__('An error occurred. The email template can not be opened for preview.')

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,6 @@ protected function setUp()
152152
*/
153153
public function testToHtml($requestParamMap)
154154
{
155-
$this->request->expects($this->atLeastOnce())
156-
->method('isSafeMethod')
157-
->willReturn(true);
158155
$this->request->expects($this->any())
159156
->method('getParam')
160157
->willReturnMap($requestParamMap);
@@ -171,24 +168,6 @@ public function testToHtml($requestParamMap)
171168
$this->assertEquals(self::MALICIOUS_TEXT, $this->preview->toHtml());
172169
}
173170

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();
190-
}
191-
192171
/**
193172
* Data provider
194173
*

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ class PreviewTest extends \PHPUnit\Framework\TestCase
5454
*/
5555
protected $pageTitleMock;
5656

57-
/**
58-
* @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
59-
*/
60-
protected $responseMock;
61-
6257
protected function setUp()
6358
{
6459
$objectManager = new ObjectManager($this);
@@ -84,16 +79,11 @@ protected function setUp()
8479
->disableOriginalConstructor()
8580
->getMock();
8681

87-
$this->responseMock = $this->getMockBuilder(\Magento\Framework\App\ResponseInterface::class)
88-
->setMethods(['setHeader'])
89-
->getMockForAbstractClass();
90-
9182
$this->context = $objectManager->getObject(
9283
\Magento\Backend\App\Action\Context::class,
9384
[
9485
'request' => $this->requestMock,
95-
'view' => $this->viewMock,
96-
'response' => $this->responseMock
86+
'view' => $this->viewMock
9787
]
9888
);
9989
$this->object = $objectManager->getObject(
@@ -118,9 +108,6 @@ public function testExecute()
118108
$this->pageTitleMock->expects($this->once())
119109
->method('prepend')
120110
->willReturnSelf();
121-
$this->responseMock->expects($this->once())
122-
->method('setHeader')
123-
->with('Content-Security-Policy', "script-src 'self'");
124111

125112
$this->assertNull($this->object->execute());
126113
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Email\Test\Unit\ViewModel\Template\Preview;
8+
9+
use Magento\Email\ViewModel\Template\Preview\Form;
10+
use Magento\Framework\App\Request\Http;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
14+
/**
15+
* Class FormTest
16+
*
17+
* @covers \Magento\Email\ViewModel\Template\Preview\Form
18+
*/
19+
class FormTest extends \PHPUnit\Framework\TestCase
20+
{
21+
/** @var Form */
22+
protected $form;
23+
24+
/** @var Http|\PHPUnit_Framework_MockObject_MockObject */
25+
protected $requestMock;
26+
27+
protected function setUp()
28+
{
29+
$this->requestMock = $this->createPartialMock(
30+
Http::class,
31+
['getParam', 'getMethod']
32+
);
33+
34+
$objectManagerHelper = new ObjectManager($this);
35+
36+
$this->form = $objectManagerHelper->getObject(
37+
Form::class,
38+
['request'=> $this->requestMock]
39+
);
40+
}
41+
42+
/**
43+
* Tests that the form is created with the expected fields based on the request type.
44+
*
45+
* @dataProvider getFormFieldsDataProvider
46+
* @param string $httpMethod
47+
* @param array $httpParams
48+
* @param array $expectedFields
49+
* @throws LocalizedException
50+
*/
51+
public function testGetFormFields(string $httpMethod, array $httpParams, array $expectedFields)
52+
{
53+
$this->requestMock->expects($this->once())
54+
->method('getMethod')
55+
->willReturn($httpMethod);
56+
57+
$this->requestMock->expects($this->any())
58+
->method('getParam')
59+
->willReturnMap($httpParams);
60+
61+
$actualFields = $this->form->getFormFields();
62+
63+
$this->assertEquals($expectedFields, $actualFields);
64+
}
65+
66+
/**
67+
* Tests that an exception is thrown when a required parameter is missing for the request type.
68+
*
69+
* @dataProvider getFormFieldsInvalidDataProvider
70+
* @expectedException \Magento\Framework\Exception\LocalizedException
71+
* @expectedExceptionMessage Missing expected parameter
72+
* @param string $httpMethod
73+
* @param array $httpParams
74+
*/
75+
public function testGetFormFieldsMissingParameter(string $httpMethod, array $httpParams)
76+
{
77+
$this->requestMock->expects($this->once())
78+
->method('getMethod')
79+
->willReturn($httpMethod);
80+
81+
$this->requestMock->expects($this->once())
82+
->method('getParam')
83+
->willReturnMap($httpParams);
84+
85+
$this->form->getFormFields();
86+
}
87+
88+
/**
89+
* @return array
90+
*/
91+
public function getFormFieldsDataProvider()
92+
{
93+
return [
94+
'get_request_valid' => [
95+
'httpMethod' => 'GET',
96+
'httpParams' => [
97+
['id', null, 1]
98+
],
99+
'expectedFields' => [
100+
'id' => 1
101+
]
102+
],
103+
'get_request_valid_ignore_params' => [
104+
'httpMethod' => 'GET',
105+
'httpParams' => [
106+
['id', null, 1],
107+
['text', null, 'Hello World'],
108+
['type', null, 2],
109+
['styles', null, '']
110+
],
111+
'expectedFields' => [
112+
'id' => 1
113+
]
114+
],
115+
'post_request_valid' => [
116+
'httpMethod' => 'POST',
117+
'httpParams' => [
118+
['text', null, 'Hello World'],
119+
['type', null, 2],
120+
['styles', null, '']
121+
],
122+
'expectedFields' => [
123+
'text' => 'Hello World',
124+
'type' => 2,
125+
'styles' => ''
126+
]
127+
]
128+
];
129+
}
130+
131+
/**
132+
* @return array
133+
*/
134+
public function getFormFieldsInvalidDataProvider()
135+
{
136+
return [
137+
'get_request_missing_id' => [
138+
'httpMethod' => 'GET',
139+
'httpParams' => [
140+
['text', null, 'Hello World'],
141+
['type', null, 2],
142+
['styles', null, '']
143+
]
144+
],
145+
'post_request_missing_text' => [
146+
'httpMethod' => 'POST',
147+
'httpParams' => [
148+
['type', null, 2],
149+
['styles', null, '']
150+
]
151+
]
152+
];
153+
}
154+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Email\ViewModel\Template\Preview;
9+
10+
use Magento\Framework\App\RequestInterface;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\View\Element\Block\ArgumentInterface;
13+
14+
/**
15+
* Class Form
16+
*/
17+
class Form implements ArgumentInterface
18+
{
19+
private $expectedParamsGetRequest = [
20+
'id'
21+
];
22+
23+
private $expectedParamsPostRequest = [
24+
'text',
25+
'type',
26+
'styles'
27+
];
28+
29+
/**
30+
* @var RequestInterface
31+
*/
32+
private $request;
33+
34+
/**
35+
* @param RequestInterface $request
36+
*/
37+
public function __construct(RequestInterface $request)
38+
{
39+
$this->request = $request;
40+
}
41+
42+
/**
43+
* Gets the fields to be included in the email preview form.
44+
*
45+
* @return array
46+
* @throws LocalizedException
47+
*/
48+
public function getFormFields()
49+
{
50+
$params = $fields = [];
51+
$method = $this->request->getMethod();
52+
53+
if ($method === 'GET') {
54+
$params = $this->expectedParamsGetRequest;
55+
} elseif ($method === 'POST') {
56+
$params = $this->expectedParamsPostRequest;
57+
}
58+
59+
foreach ($params as $paramName) {
60+
$fieldValue = $this->request->getParam($paramName);
61+
if ($fieldValue === null) {
62+
throw new LocalizedException(
63+
__("Missing expected parameter \"$paramName\" while attempting to generate template preview.")
64+
);
65+
}
66+
$fields[$paramName] = $fieldValue;
67+
}
68+
69+
return $fields;
70+
}
71+
}

app/code/Magento/Email/view/adminhtml/layout/adminhtml_email_template_preview.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
<referenceContainer name="backend.page" remove="true"/>
1313
<referenceContainer name="menu.wrapper" remove="true"/>
1414
<referenceContainer name="root">
15-
<block name="preview.page.content" class="Magento\Backend\Block\Page" template="Magento_Email::preview/iframeswitcher.phtml"/>
15+
<block name="preview.page.content" class="Magento\Backend\Block\Page" template="Magento_Email::preview/iframeswitcher.phtml">
16+
<arguments>
17+
<argument name="preview_form_view_model" xsi:type="object">Magento\Email\ViewModel\Template\Preview\Form</argument>
18+
</arguments>
19+
</block>
1620
</referenceContainer>
1721
</body>
1822
</page>

0 commit comments

Comments
 (0)