@@ -18,105 +18,175 @@ class PreviewTest extends \PHPUnit\Framework\TestCase
18
18
19
19
const MALICIOUS_TEXT = 'test malicious ' ;
20
20
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
+
21
46
/**
22
47
* Init data
23
48
*/
24
49
protected function setUp ()
25
50
{
26
51
$ this ->objectManagerHelper = new \Magento \Framework \TestFramework \Unit \Helper \ObjectManager ($ this );
27
- }
28
52
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
- {
39
53
$ 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)
49
71
->disableOriginalConstructor ()
50
72
->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 ())
52
82
->method ('getProcessedTemplate ' )
53
83
->with ($ this ->equalTo ([]))
54
84
->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
+
61
89
$ emailFactory = $ this ->createPartialMock (\Magento \Email \Model \TemplateFactory::class, ['create ' ]);
62
90
$ emailFactory ->expects ($ this ->any ())
63
91
->method ('create ' )
64
- ->willReturn ($ template );
92
+ ->willReturn ($ this -> template );
65
93
66
- $ request = $ this ->createMock (\Magento \Framework \App \RequestInterface::class);
67
- $ request ->expects ($ this ->any ())->method ('getParam ' )->willReturnMap ($ requestParamMap );
68
94
$ eventManage = $ this ->createMock (\Magento \Framework \Event \ManagerInterface::class);
69
95
$ scopeConfig = $ this ->createMock (\Magento \Framework \App \Config \ScopeConfigInterface::class);
70
96
$ design = $ this ->createMock (\Magento \Framework \View \DesignInterface::class);
71
97
$ 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 ' )
78
104
->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 ]);
81
108
$ appState = $ this ->getMockBuilder (\Magento \Framework \App \State::class)
82
- ->setConstructorArgs ([
83
- $ scopeConfig
84
- ])
109
+ ->setConstructorArgs (
110
+ [
111
+ $ scopeConfig
112
+ ]
113
+ )
85
114
->setMethods (['emulateAreaCode ' ])
86
115
->disableOriginalConstructor ()
87
116
->getMock ();
88
117
$ appState ->expects ($ this ->any ())
89
118
->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 ());
92
124
93
125
$ context = $ this ->createPartialMock (
94
126
\Magento \Backend \Block \Template \Context::class,
95
127
['getRequest ' , 'getEventManager ' , 'getScopeConfig ' , 'getDesignPackage ' , 'getStoreManager ' , 'getAppState ' ]
96
128
);
97
- $ context ->expects ($ this ->any ())->method ('getRequest ' )->willReturn ($ request );
129
+ $ context ->expects ($ this ->any ())->method ('getRequest ' )->willReturn ($ this -> request );
98
130
$ context ->expects ($ this ->any ())->method ('getEventManager ' )->willReturn ($ eventManage );
99
131
$ context ->expects ($ this ->any ())->method ('getScopeConfig ' )->willReturn ($ scopeConfig );
100
132
$ 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 );
102
134
$ context ->expects ($ this ->once ())->method ('getAppState ' )->willReturn ($ appState );
103
135
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
-
110
136
/** @var \Magento\Email\Block\Adminhtml\Template\Preview $preview */
111
- $ preview = $ this ->objectManagerHelper ->getObject (
137
+ $ this -> preview = $ this ->objectManagerHelper ->getObject (
112
138
\Magento \Email \Block \Adminhtml \Template \Preview::class,
113
139
[
114
140
'context ' => $ context ,
115
- 'maliciousCode ' => $ maliciousCode ,
141
+ 'maliciousCode ' => $ this -> maliciousCode ,
116
142
'emailFactory ' => $ emailFactory
117
143
]
118
144
);
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 ();
120
190
}
121
191
122
192
/**
0 commit comments