Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractTemplateTest.php
Go to the documentation of this file.
1 <?php
11 
15 class AbstractTemplateTest extends \PHPUnit\Framework\TestCase
16 {
20  private $design;
21 
25  private $appEmulation;
26 
30  private $storeManager;
31 
35  private $store;
36 
40  private $filesystem;
41 
45  private $assetRepo;
46 
50  private $scopeConfig;
51 
55  private $filterFactory;
56 
60  private $emailConfig;
61 
65  private $templateFactory;
66 
67  protected function setUp()
68  {
69  $this->design = $this->getMockBuilder(\Magento\Framework\View\DesignInterface::class)
70  ->disableOriginalConstructor()
71  ->getMock();
72  $this->registry = $this->getMockBuilder(\Magento\Framework\Registry::class)
73  ->disableOriginalConstructor()
74  ->getMock();
75  $this->appEmulation = $this->getMockBuilder(\Magento\Store\Model\App\Emulation::class)
76  ->disableOriginalConstructor()
77  ->getMock();
78  $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
79  ->disableOriginalConstructor()
80  ->getMock();
81 
82  $this->store = $this->getMockBuilder(\Magento\Store\Model\Store::class)
83  ->setMethods(['getFrontendName', 'getId'])
84  ->disableOriginalConstructor()
85  ->getMock();
86  $this->store->expects($this->any())
87  ->method('getFrontendName')
88  ->will($this->returnValue('frontendName'));
89  $this->store->expects($this->any())
90  ->method('getFrontendName')
91  ->will($this->returnValue('storeId'));
92  $this->storeManager->expects($this->any())
93  ->method('getStore')
94  ->will($this->returnValue($this->store));
95 
96  $this->filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
97  ->disableOriginalConstructor()
98  ->getMock();
99  $this->assetRepo = $this->getMockBuilder(\Magento\Framework\View\Asset\Repository::class)
100  ->disableOriginalConstructor()
101  ->getMock();
102  $this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
103  ->disableOriginalConstructor()
104  ->getMock();
105  $this->emailConfig = $this->getMockBuilder(\Magento\Email\Model\Template\Config::class)
106  ->disableOriginalConstructor()
107  ->getMock();
108  $this->filterFactory = $this->getMockBuilder(\Magento\Email\Model\Template\FilterFactory::class)
109  ->setMethods(['create'])
110  ->disableOriginalConstructor()
111  ->getMock();
112  $this->templateFactory = $this->getMockBuilder(\Magento\Email\Model\TemplateFactory::class)
113  ->disableOriginalConstructor()
114  ->getMock();
115  }
116 
124  protected function getModelMock(array $mockedMethods = [], array $data = [])
125  {
126  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
127  return $this->getMockForAbstractClass(
128  \Magento\Email\Model\AbstractTemplate::class,
129  $helper->getConstructArguments(
130  \Magento\Email\Model\AbstractTemplate::class,
131  [
132  'design' => $this->design,
133  'appEmulation' => $this->appEmulation,
134  'storeManager' => $this->storeManager,
135  'filesystem' => $this->filesystem,
136  'assetRepo' => $this->assetRepo,
137  'scopeConfig' => $this->scopeConfig,
138  'emailConfig' => $this->emailConfig,
139  'filterFactory' => $this->filterFactory,
140  'templateFactory' => $this->templateFactory,
141  'data' => $data,
142  ]
143  ),
144  '',
145  true,
146  true,
147  true,
148  array_merge($mockedMethods, ['__wakeup', '__sleep', '_init'])
149  );
150  }
151 
160  public function testGetProcessedTemplate($variables, $templateType, $storeId, $expectedVariables, $expectedResult)
161  {
162  $filterTemplate = $this->getMockBuilder(\Magento\Email\Model\Template\Filter::class)
163  ->setMethods([
164  'setUseSessionInUrl',
165  'setPlainTemplateMode',
166  'setIsChildTemplate',
167  'setDesignParams',
168  'setVariables',
169  'setStoreId',
170  'filter',
171  'getStoreId',
172  'getInlineCssFiles',
173  ])
174  ->disableOriginalConstructor()
175  ->getMock();
176 
177  $filterTemplate->expects($this->once())
178  ->method('setUseSessionInUrl')
179  ->with(false)
180  ->will($this->returnSelf());
181  $filterTemplate->expects($this->once())
182  ->method('setPlainTemplateMode')
184  ->will($this->returnSelf());
185  $filterTemplate->expects($this->once())
186  ->method('setIsChildTemplate')
187  ->will($this->returnSelf());
188  $filterTemplate->expects($this->once())
189  ->method('setDesignParams')
190  ->will($this->returnSelf());
191  $filterTemplate->expects($this->any())
192  ->method('setStoreId')
193  ->will($this->returnSelf());
194  $filterTemplate->expects($this->any())
195  ->method('getStoreId')
196  ->will($this->returnValue($storeId));
197 
198  $expectedVariables['store'] = $this->store;
199 
200  $model = $this->getModelMock([
201  'getDesignParams',
202  'applyDesignConfig',
203  'getTemplateText',
204  'isPlain',
205  ]);
206  $filterTemplate->expects($this->any())
207  ->method('setVariables')
208  ->with(array_merge(['this' => $model], $expectedVariables));
209  $model->setTemplateFilter($filterTemplate);
210  $model->setTemplateType($templateType);
211 
212  $designParams = [
214  'theme' => 'themeId',
215  'locale' => 'localeId',
216  ];
217  $model->expects($this->any())
218  ->method('getDesignParams')
219  ->will($this->returnValue($designParams));
220 
221  $model->expects($this->atLeastOnce())
222  ->method('isPlain')
223  ->will($this->returnValue($templateType === \Magento\Framework\App\TemplateTypesInterface::TYPE_TEXT));
224 
225  $preparedTemplateText = $expectedResult; //'prepared text';
226  $model->expects($this->once())
227  ->method('getTemplateText')
228  ->will($this->returnValue($preparedTemplateText));
229 
230  $filterTemplate->expects($this->once())
231  ->method('filter')
232  ->with($preparedTemplateText)
233  ->will($this->returnValue($expectedResult));
234 
235  $this->assertEquals($expectedResult, $model->getProcessedTemplate($variables));
236  }
237 
242  {
243  $filterTemplate = $this->getMockBuilder(\Magento\Email\Model\Template\Filter::class)
244  ->setMethods([
245  'setUseSessionInUrl',
246  'setPlainTemplateMode',
247  'setIsChildTemplate',
248  'setDesignParams',
249  'setVariables',
250  'setStoreId',
251  'filter',
252  'getStoreId',
253  'getInlineCssFiles',
254  ])
255  ->disableOriginalConstructor()
256  ->getMock();
257  $filterTemplate->expects($this->once())
258  ->method('setUseSessionInUrl')
259  ->will($this->returnSelf());
260  $filterTemplate->expects($this->once())
261  ->method('setPlainTemplateMode')
262  ->will($this->returnSelf());
263  $filterTemplate->expects($this->once())
264  ->method('setIsChildTemplate')
265  ->will($this->returnSelf());
266  $filterTemplate->expects($this->once())
267  ->method('setDesignParams')
268  ->will($this->returnSelf());
269  $filterTemplate->expects($this->any())
270  ->method('setStoreId')
271  ->will($this->returnSelf());
272  $filterTemplate->expects($this->any())
273  ->method('getStoreId')
274  ->will($this->returnValue(1));
275 
276  $model = $this->getModelMock([
277  'getDesignParams',
278  'applyDesignConfig',
279  'getTemplateText',
280  'isPlain',
281  ]);
282 
283  $designParams = [
285  'theme' => 'themeId',
286  'locale' => 'localeId',
287  ];
288  $model->expects($this->any())
289  ->method('getDesignParams')
290  ->will($this->returnValue($designParams));
291  $model->setTemplateFilter($filterTemplate);
292  $model->setTemplateType(\Magento\Framework\App\TemplateTypesInterface::TYPE_TEXT);
293 
294  $filterTemplate->expects($this->once())
295  ->method('filter')
296  ->will($this->throwException(new \Exception));
297  $model->getProcessedTemplate([]);
298  }
299 
304  {
305  return [
306  'default' => [
307  'variables' => [],
309  'storeId' => 1,
310  'expectedVariables' => [
311  'logo_url' => null,
312  'logo_alt' => 'frontendName',
313  'store' => null,
314  'logo_width' => null,
315  'logo_height' => null,
316  'store_phone' => null,
317  'store_hours' => null,
318  'store_email' => null,
319  ],
320  'expectedResult' => 'expected result',
321  ],
322  'logo variables set' => [
323  'variables' => [
324  'logo_url' => 'http://example.com/logo',
325  'logo_alt' => 'Logo Alt',
326  ],
328  'storeId' => 1,
329  'expectedVariables' => [
330  'logo_url' => 'http://example.com/logo',
331  'logo_alt' => 'Logo Alt',
332  'store' => null,
333  'logo_width' => null,
334  'logo_height' => null,
335  'store_phone' => null,
336  'store_hours' => null,
337  'store_email' => null,
338  'template_styles' => null,
339  ],
340  'expectedResult' => 'expected result',
341  ],
342  ];
343  }
344 
345  public function testGetDefaultEmailLogo()
346  {
347  $model = $this->getModelMock(['getDesignParams']);
348  $value = 'urlWithParamsValue';
349  $designParams = [
351  'theme' => 'themeId',
352  'locale' => 'localeId',
353  ];
354  $model->expects($this->once())
355  ->method('getDesignParams')
356  ->will($this->returnValue($designParams));
357  $this->assetRepo->method('getUrlWithParams')
358  ->with(\Magento\Email\Model\AbstractTemplate::DEFAULT_LOGO_FILE_ID, $designParams)
359  ->will($this->returnValue($value));
360  $this->assertEquals($value, $model->getDefaultEmailLogo());
361  }
362 
369  {
370  $this->getModelMock()->setDesignConfig($config);
371  }
372 
374  {
375  $config = ['area' => 'some_area', 'store' => 1];
376  $model = $this->getModelMock();
377  $model->setDesignConfig($config);
378  $this->assertEquals($config, $model->getDesignConfig()->getData());
379  }
380 
385  {
386  return [[[]], [['area' => 'some_area']], [['store' => 'any_store']]];
387  }
388 
390  {
391  $model = $this->getModelMock();
392  $originalConfig = ['area' => 'some_area', 'store' => 1];
393  $model->setDesignConfig($originalConfig);
394 
395  $expectedConfigs = [
396  ['in' => ['area' => 'frontend', 'store' => null], 'out' => $originalConfig],
397  ['in' => ['area' => 'frontend', 'store' => false], 'out' => $originalConfig],
398  ['in' => ['area' => 'frontend', 'store' => 0], 'out' => ['area' => 'frontend', 'store' => 0]],
399  ['in' => ['area' => 'frontend', 'store' => 1], 'out' => ['area' => 'frontend', 'store' => 1]],
400  ['in' => ['area' => 'frontend', 'store' => 2], 'out' => ['area' => 'frontend', 'store' => 2]],
401  ];
402  foreach ($expectedConfigs as $set) {
403  $model->emulateDesign($set['in']['store'], $set['in']['area']);
404  // assert config data has been emulated
405  $this->assertEquals($set['out'], $model->getDesignConfig()->getData());
406 
407  $model->revertDesign();
408  // assert config data has been reverted to the original state
409  $this->assertEquals($originalConfig, $model->getDesignConfig()->getData());
410  }
411  }
412 
413  public function testGetDesignConfig()
414  {
415  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
416 
417  $designMock = $this->createMock(\Magento\Framework\View\DesignInterface::class);
418  $designMock->expects($this->any())->method('getArea')->willReturn('test_area');
419 
420  $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
421  $storeMock->expects($this->any())->method('getId')->willReturn(2);
422  $storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
423  $storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
424 
425  $model = $this->getMockForAbstractClass(
426  \Magento\Email\Model\AbstractTemplate::class,
427  $helper->getConstructArguments(
428  \Magento\Email\Model\AbstractTemplate::class,
429  [
430  'design' => $designMock,
431  'storeManager' => $storeManagerMock
432  ]
433  )
434  );
435 
436  $expectedConfig = ['area' => 'test_area', 'store' => 2];
437  $this->assertEquals($expectedConfig, $model->getDesignConfig()->getData());
438  }
439 
443  public function testSetForcedAreaWhenAreaIsNotSet(): void
444  {
445  $templateId = 'test_template';
446  $model = $this->getModelMock([], ['area' => null]);
447 
448  $this->emailConfig->expects($this->once())
449  ->method('getTemplateArea')
450  ->with($templateId);
451 
452  $model->setForcedArea($templateId);
453  }
454 
458  public function testSetForcedAreaWhenAreaIsSet(): void
459  {
460  $templateId = 'test_template';
461  $model = $this->getModelMock([], ['area' => 'frontend']);
462 
463  $this->emailConfig->expects($this->never())
464  ->method('getTemplateArea');
465 
466  $model->setForcedArea($templateId);
467  }
468 }
$helper
Definition: iframe.phtml:13
$config
Definition: fraud_order.php:17
$templateType
Definition: list.phtml:37
testGetProcessedTemplate($variables, $templateType, $storeId, $expectedVariables, $expectedResult)
$templateId
Definition: queue.php:15
$value
Definition: gender.phtml:16
getModelMock(array $mockedMethods=[], array $data=[])