Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TemplateTest.php
Go to the documentation of this file.
1 <?php
7 
13 
19 class TemplateTest extends \PHPUnit\Framework\TestCase
20 {
24  private $context;
25 
29  private $design;
30 
35  private $registry;
36 
40  private $appEmulation;
41 
45  private $storeManager;
46 
50  private $filesystem;
51 
55  private $assetRepo;
56 
60  private $scopeConfig;
61 
65  private $filterFactory;
66 
70  private $filterManager;
71 
75  private $urlModel;
76 
80  private $emailConfig;
81 
85  private $templateFactory;
86 
90  private $serializerMock;
91 
92  protected function setUp()
93  {
94  $this->context = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
95  ->disableOriginalConstructor()
96  ->getMock();
97 
98  $this->design = $this->getMockBuilder(\Magento\Framework\View\DesignInterface::class)
99  ->disableOriginalConstructor()
100  ->getMock();
101 
102  $this->registry = $this->getMockBuilder(\Magento\Framework\Registry::class)
103  ->disableOriginalConstructor()
104  ->getMock();
105 
106  $this->appEmulation = $this->getMockBuilder(\Magento\Store\Model\App\Emulation::class)
107  ->disableOriginalConstructor()
108  ->getMock();
109 
110  $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
111  ->disableOriginalConstructor()
112  ->getMock();
113 
114  $this->assetRepo = $this->getMockBuilder(\Magento\Framework\View\Asset\Repository::class)
115  ->disableOriginalConstructor()
116  ->getMock();
117 
118  $this->filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
119  ->disableOriginalConstructor()
120  ->getMock();
121 
122  $this->scopeConfig = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class)
123  ->disableOriginalConstructor()
124  ->getMock();
125 
126  $this->emailConfig = $this->getMockBuilder(\Magento\Email\Model\Template\Config::class)
127  ->disableOriginalConstructor()
128  ->getMock();
129 
130  $this->templateFactory = $this->getMockBuilder(\Magento\Email\Model\TemplateFactory::class)
131  ->disableOriginalConstructor()
132  ->getMock();
133 
134  $this->filterManager = $this->getMockBuilder(\Magento\Framework\Filter\FilterManager::class)
135  ->disableOriginalConstructor()
136  ->getMock();
137 
138  $this->urlModel = $this->getMockBuilder(\Magento\Framework\Url::class)
139  ->disableOriginalConstructor()
140  ->getMock();
141 
142  $this->filterFactory = $this->getMockBuilder(\Magento\Email\Model\Template\FilterFactory::class)
143  ->setMethods(['create'])
144  ->disableOriginalConstructor()
145  ->getMock();
146 
147  $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();
148  }
149 
156  protected function getModelMock(array $mockedMethods = [])
157  {
158  return $this->getMockBuilder(\Magento\Email\Model\Template::class)
159  ->setMethods(array_merge($mockedMethods, ['__wakeup', '__sleep', '_init']))
160  ->setConstructorArgs([
161  $this->context,
162  $this->design,
163  $this->registry,
164  $this->appEmulation,
165  $this->storeManager,
166  $this->assetRepo,
167  $this->filesystem,
168  $this->scopeConfig,
169  $this->emailConfig,
170  $this->templateFactory,
171  $this->filterManager,
172  $this->urlModel,
173  $this->filterFactory,
174  [],
175  $this->serializerMock
176  ])
177  ->getMock();
178  }
179 
181  {
182  $model = $this->getModelMock();
183  $model->setIsChildTemplate(true);
184  $this->assertSame(true, $model->isChildTemplate());
185 
186  $model->setIsChildTemplate(false);
187  $this->assertSame(false, $model->isChildTemplate());
188  }
189 
190  public function testSetAndGetTemplateFilter()
191  {
192  $model = $this->getModelMock();
193  $filterTemplate = $this->getMockBuilder(\Magento\Email\Model\Template\Filter::class)
194  ->disableOriginalConstructor()
195  ->getMock();
196  $model->setTemplateFilter($filterTemplate);
197  $this->assertSame($filterTemplate, $model->getTemplateFilter());
198  }
199 
201  {
202  $filterTemplate = $this->getMockBuilder(\Magento\Framework\Filter\Template::class)
203  ->setMethods(['setUseAbsoluteLinks', 'setStoreId', 'setUrlModel'])
204  ->disableOriginalConstructor()
205  ->getMock();
206  $filterTemplate->expects($this->once())
207  ->method('setUseAbsoluteLinks')
208  ->will($this->returnSelf());
209  $filterTemplate->expects($this->once())
210  ->method('setStoreId')
211  ->will($this->returnSelf());
212  $this->filterFactory->method('create')
213  ->will($this->returnValue($filterTemplate));
214  $designConfig = $this->getMockBuilder(\Magento\Framework\DataObject::class)
215  ->setMethods(['getStore'])
216  ->disableOriginalConstructor()
217  ->getMock();
218 
219  $model = $this->getModelMock(['getUseAbsoluteLinks', 'getDesignConfig']);
220  $model->expects($this->once())
221  ->method('getDesignConfig')
222  ->will($this->returnValue($designConfig));
223 
224  $this->assertSame($filterTemplate, $model->getTemplateFilter());
225  }
226 
236  public function testLoadDefault(
238  $templateText,
239  $parsedTemplateText,
240  $expectedTemplateSubject,
241  $expectedOrigTemplateVariables,
242  $expectedTemplateStyles
243  ) {
244  $model = $this->getModelMock([
245  'getDesignParams'
246  ]);
247 
248  $designParams = [
249  'area' => Area::AREA_FRONTEND,
250  'theme' => 'Magento/blank',
251  'locale' => Locale::DEFAULT_SYSTEM_LOCALE,
252  ];
253 
254  $model->expects($this->once())
255  ->method('getDesignParams')
256  ->will($this->returnValue($designParams));
257 
258  $templateId = 'templateId';
259 
260  $templateFile = 'templateFile';
261  $this->emailConfig->expects($this->once())
262  ->method('getTemplateFilename')
263  ->with($templateId)
264  ->will($this->returnValue($templateFile));
265  $this->emailConfig->expects($this->once())
266  ->method('getTemplateType')
267  ->with($templateId)
268  ->will($this->returnValue($templateType));
269 
270  $modulesDir = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\ReadInterface::class)
271  ->setMethods(['readFile', 'getRelativePath'])
272  ->getMockForAbstractClass();
273 
274  $relativePath = 'relativePath';
275  $modulesDir->expects($this->once())
276  ->method('getRelativePath')
277  ->with($templateFile)
278  ->will($this->returnValue($relativePath));
279  $modulesDir->expects($this->once())
280  ->method('readFile')
281  ->will($this->returnValue($templateText));
282 
283  $this->filesystem->expects($this->once())
284  ->method('getDirectoryRead')
285  ->with(DirectoryList::ROOT)
286  ->will($this->returnValue($modulesDir));
287 
288  $model->loadDefault($templateId);
289 
290  if ($templateType === 'html') {
291  $this->assertEquals(TemplateTypesInterface::TYPE_HTML, $model->getTemplateType());
292  } else {
293  $this->assertEquals(TemplateTypesInterface::TYPE_TEXT, $model->getTemplateType());
294  }
295  $this->assertEquals($templateId, $model->getId());
296  $this->assertEquals($parsedTemplateText, $model->getTemplateText());
297  $this->assertEquals($expectedTemplateSubject, $model->getTemplateSubject());
298  $this->assertEquals($expectedOrigTemplateVariables, $model->getData('orig_template_variables'));
299  $this->assertEquals($expectedTemplateStyles, $model->getTemplateStyles());
300  }
301 
305  public function loadDefaultDataProvider()
306  {
307  return [
308  'empty' => [
309  'templateType' => 'html',
310  'templateText' => '',
311  'parsedTemplateText' => '',
312  'expectedTemplateSubject' => null,
313  'expectedOrigTemplateVariables' => null,
314  'expectedTemplateStyles' => null,
315  ],
316  'copyright in Plain Text Removed' => [
317  'templateType' => 'text',
318  'templateText' => '<!-- Copyright © Magento, Inc. All rights reserved. -->',
319  'parsedTemplateText' => '',
320  'expectedTemplateSubject' => null,
321  'expectedOrigTemplateVariables' => null,
322  'expectedTemplateStyles' => null,
323  ],
324  'copyright in HTML Removed' => [
325  'templateType' => 'html',
326  'templateText' => '<!-- Copyright © Magento, Inc. All rights reserved. -->',
327  'parsedTemplateText' => '',
328  'expectedTemplateSubject' => null,
329  'expectedOrigTemplateVariables' => null,
330  'expectedTemplateStyles' => null,
331  ],
332  'subject set' => [
333  'templateType' => 'html',
334  'templateText' => '<!--@subject Email Subject @-->',
335  'parsedTemplateText' => '',
336  'expectedTemplateSubject' => 'Email Subject',
337  'expectedOrigTemplateVariables' => null,
338  'expectedTemplateStyles' => null,
339  ],
340  'orig_template_variables set' => [
341  'templateType' => 'html',
342  'templateText' => '<!--@vars {"store url=\"\"":"Store Url"} @-->Some Other Text',
343  'parsedTemplateText' => 'Some Other Text',
344  'expectedTemplateSubject' => null,
345  'expectedOrigTemplateVariables' => '{"store url=\"\"":"Store Url"}',
346  'expectedTemplateStyles' => null,
347  ],
348  'styles' => [
349  'templateType' => 'html',
350  'templateText' => '<!--@styles p { color: #000; } @-->Some Other Text',
351  'parsedTemplateText' => 'Some Other Text',
352  'expectedTemplateSubject' => null,
353  'expectedOrigTemplateVariables' => null,
354  'expectedTemplateStyles' => 'p { color: #000; }',
355  ],
356  ];
357  }
358 
366  public function testLoadByConfigPath($loadFromDatabase)
367  {
368  $configPath = 'design/email/header_template';
369  $model = $this->getModelMock([
370  'getDesignConfig',
371  'loadDefault',
372  'load',
373  'getTemplateText',
374  'setTemplateText',
375  ]);
376 
377  $designConfig = $this->getMockBuilder(\Magento\Framework\DataObject::class)
378  ->setMethods(['getStore'])
379  ->disableOriginalConstructor()
380  ->getMock();
381 
382  $storeId = 'storeId';
383  $designConfig->expects($this->once())
384  ->method('getStore')
385  ->will($this->returnValue($storeId));
386  $model->expects($this->once())
387  ->method('getDesignConfig')
388  ->will($this->returnValue($designConfig));
389 
390  if ($loadFromDatabase) {
391  $templateId = '1';
392  $model->expects($this->once())
393  ->method('load')
394  ->with($templateId)
395  ->will($this->returnSelf());
396  } else {
397  $templateId = 'design_email_header_template';
398  $model->expects($this->once())
399  ->method('loadDefault')
400  ->with($templateId)
401  ->will($this->returnSelf());
402  }
403 
404  $this->scopeConfig->expects($this->once())
405  ->method('getValue')
406  ->with($configPath, ScopeInterface::SCOPE_STORE, $storeId)
407  ->will($this->returnValue($templateId));
408 
409  $model->loadByConfigPath($configPath);
410  }
411 
416  {
417  return [
418  'Load from filesystem' => [
419  false,
420  'Test template content',
421  'Test template content',
422  ],
423  'Load from database' => [
424  true,
425  'Test template content',
426  'Test template content',
427  ],
428  ];
429  }
430 
431  public function testGetAndSetId()
432  {
433  $model = $this->getModelMock();
434  $templateId = 'templateId';
435  $this->assertEquals($model, $model->setId($templateId));
436  $this->assertEquals($templateId, $model->getId());
437  }
438 
445  public function testIsValidForSend($senderName, $senderEmail, $templateSubject, $expectedValue)
446  {
447  $model = $this->getModelMock(['getSenderName', 'getSenderEmail', 'getTemplateSubject']);
448  $model->expects($this->any())
449  ->method('getSenderName')
450  ->will($this->returnValue($senderName));
451  $model->expects($this->any())
452  ->method('getSenderEmail')
453  ->will($this->returnValue($senderEmail));
454  $model->expects($this->any())
455  ->method('getTemplateSubject')
456  ->will($this->returnValue($templateSubject));
457  $this->assertEquals($expectedValue, $model->isValidForSend());
458  }
459 
463  public function isValidForSendDataProvider()
464  {
465  return [
466  'should be valid' => [
467  'senderName' => 'sender name',
468  'senderEmail' => '[email protected]',
469  'templateSubject' => 'template subject',
470  'expectedValue' => true
471  ],
472  'no sender name so not valid' => [
473  'senderName' => '',
474  'senderEmail' => '[email protected]',
475  'templateSubject' => 'template subject',
476  'expectedValue' => false
477  ],
478  'no sender email so not valid' => [
479  'senderName' => 'sender name',
480  'senderEmail' => '',
481  'templateSubject' => 'template subject',
482  'expectedValue' => false
483  ],
484  'no subject so not valid' => [
485  'senderName' => 'sender name',
486  'senderEmail' => '[email protected]',
487  'templateSubject' => '',
488  'expectedValue' => false
489  ],
490  ];
491  }
492 
494  {
495  $model = $this->getModelMock(['getTemplateFilter', 'getDesignConfig', 'applyDesignConfig']);
496 
497  $templateSubject = 'templateSubject';
498  $model->setTemplateSubject($templateSubject);
499 
500  $filterTemplate = $this->getMockBuilder(\Magento\Framework\Filter\Template::class)
501  ->setMethods(['setVariables', 'setStoreId', 'filter'])
502  ->disableOriginalConstructor()
503  ->getMock();
504  $model->expects($this->once())
505  ->method('getTemplateFilter')
506  ->will($this->returnValue($filterTemplate));
507 
508  $model->expects($this->once())
509  ->method('applyDesignConfig');
510 
511  $designConfig = $this->getMockBuilder(\Magento\Framework\DataObject::class)
512  ->setMethods(['getStore'])
513  ->disableOriginalConstructor()
514  ->getMock();
515  $storeId = 'storeId';
516  $designConfig->expects($this->once())
517  ->method('getStore')
518  ->will($this->returnValue($storeId));
519  $model->expects($this->once())
520  ->method('getDesignConfig')
521  ->will($this->returnValue($designConfig));
522 
523  $filterTemplate->expects($this->once())
524  ->method('setStoreId')
525  ->with($storeId)
526  ->will($this->returnSelf());
527  $expectedResult = 'expected';
528  $filterTemplate->expects($this->once())
529  ->method('filter')
530  ->with($templateSubject)
531  ->will($this->returnValue($expectedResult));
532 
533  $variables = [ 'key' => 'value' ];
534  $filterTemplate->expects($this->once())
535  ->method('setVariables')
536  ->with(array_merge($variables, ['this' => $model]));
537  $this->assertEquals($expectedResult, $model->getProcessedTemplateSubject($variables));
538  }
539 
546  public function testGetVariablesOptionArray($withGroup, $templateVariables, $expectedResult)
547  {
548  $model = $this->getModelMock();
549  $model->setData('orig_template_variables', $templateVariables);
550 
551  $this->serializerMock->expects($this->any())->method('unserialize')
552  ->willReturn(
553  json_decode($templateVariables, true)
554  );
555  $this->assertEquals($expectedResult, $model->getVariablesOptionArray($withGroup));
556  }
557 
562  {
563  return [
564  'empty variables' => [
565  'withGroup' => false,
566  'templateVariables' => '',
567  'expectedResult' => [],
568  ],
569  'empty variables with grouped option' => [
570  'withGroup' => true,
571  'templateVariables' => '',
572  'expectedResult' => [],
573  ],
574  'customer account new variables' => [
575  'withGroup' => false,
576  'templateVariables' => '{"store url=\"\"":"Store Url","var logo_url":"Email Logo Image Url",'
577  . '"var customer.name":"Customer Name"}',
578  'expectedResult' => [
579  [
580  'value' => '{{store url=""}}',
581  'label' => __('%1', 'Store Url'),
582  ],
583  [
584  'value' => '{{var logo_url}}',
585  'label' => __('%1', 'Email Logo Image Url'),
586  ],
587  [
588  'value' => '{{var customer.name}}',
589  'label' => __('%1', 'Customer Name'),
590  ],
591  ],
592  ],
593  'customer account new variables with grouped option' => [
594  'withGroup' => true,
595  'templateVariables' => '{"store url=\"\"":"Store Url","var logo_url":"Email Logo Image Url",'
596  . '"var customer.name":"Customer Name"}',
597  'expectedResult' => [
598  'label' => __('Template Variables'),
599  'value' => [
600  [
601  'value' => '{{store url=""}}',
602  'label' => __('%1', 'Store Url'),
603  ],
604  [
605  'value' => '{{var logo_url}}',
606  'label' => __('%1', 'Email Logo Image Url'),
607  ],
608  [
609  'value' => '{{var customer.name}}',
610  'label' => __('%1', 'Customer Name'),
611  ],
612  ],
613  ],
614  ],
615  ];
616  }
617 
623  public function testProcessTemplate($templateId, $expectedResult)
624  {
625  $model = $this->getModelMock([
626  'load',
627  'loadDefault',
628  'getProcessedTemplate',
629  'applyDesignConfig',
630  'cancelDesignConfig',
631  ]);
632  $model->setId($templateId);
633  if (is_numeric($templateId)) {
634  $model->expects($this->once())
635  ->method('load')
636  ->with($templateId);
637  } else {
638  $model->expects($this->once())
639  ->method('loadDefault')
640  ->with($templateId);
641  }
642 
643  $model->expects($this->once())
644  ->method('applyDesignConfig')
645  ->will($this->returnValue(true));
646  $model->expects($this->once())
647  ->method('cancelDesignConfig')
648  ->will($this->returnValue(true));
649 
650  $vars = [ 'key' => 'value' ];
651  $model->setVars($vars);
652  $model->expects($this->once())
653  ->method('getProcessedTemplate')
654  ->with($vars)
655  ->will($this->returnValue($expectedResult));
656 
657  $this->assertEquals($expectedResult, $model->processTemplate());
658  $this->assertTrue($model->getUseAbsoluteLinks());
659  }
660 
664  public function processTemplateVariable()
665  {
666  return [
667  'numeric id' => [
668  'templateId' => 1,
669  'expectedResult' => 'expected result',
670  ],
671  'string id' => [
672  'templateId' => 'my id',
673  'expectedResult' => 'expected result',
674  ],
675  ];
676  }
677 
682  {
683  $model = $this->getModelMock([
684  'loadDefault',
685  'applyDesignConfig',
686  ]);
687  $model->expects($this->once())
688  ->method('loadDefault')
689  ->will($this->returnValue(true));
690 
691  $model->expects($this->once())
692  ->method('applyDesignConfig')
693  ->will($this->returnValue(true));
694 
695  $model->processTemplate();
696  }
697 
698  public function testGetSubject()
699  {
700  $variables = ['key' => 'value'];
701  $model = $this->getModelMock(['getProcessedTemplateSubject']);
702  $model->setVars($variables);
703  $expectedResult = 'result';
704  $model->expects($this->once())
705  ->method('getProcessedTemplateSubject')
706  ->with($variables)
707  ->will($this->returnValue($expectedResult));
708  $this->assertEquals($expectedResult, $model->getSubject());
709  }
710 
711  public function testSetOptions()
712  {
713  $options = ['someOption' => 'someValue'];
714  $model = $this->getModelMock(['setDesignConfig']);
715  $model->expects($this->once())
716  ->method('setDesignConfig')
717  ->with($options);
718  $model->setOptions($options);
719  }
720 
726  public function testGetType($templateType, $expectedResult)
727  {
728  $emailConfig = $this->getMockBuilder(\Magento\Email\Model\Template\Config::class)
729  ->setMethods(['getTemplateType'])
730  ->disableOriginalConstructor()
731  ->getMock();
732 
733  $emailConfig->expects($this->once())->method('getTemplateType')->will($this->returnValue($templateType));
734 
736  $model = $this->getMockBuilder(\Magento\Email\Model\Template::class)
737  ->setMethods(['_init'])
738  ->setConstructorArgs([
739  $this->createMock(\Magento\Framework\Model\Context::class),
740  $this->createMock(\Magento\Theme\Model\View\Design::class),
741  $this->createMock(\Magento\Framework\Registry::class),
742  $this->createMock(\Magento\Store\Model\App\Emulation::class),
743  $this->createMock(\Magento\Store\Model\StoreManager::class),
744  $this->createMock(\Magento\Framework\View\Asset\Repository::class),
745  $this->createMock(\Magento\Framework\Filesystem::class),
746  $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class),
747  $emailConfig,
748  $this->createMock(\Magento\Email\Model\TemplateFactory::class),
749  $this->createMock(\Magento\Framework\Filter\FilterManager::class),
750  $this->createMock(\Magento\Framework\Url::class),
751  $this->createMock(\Magento\Email\Model\Template\FilterFactory::class),
752  ])
753  ->getMock();
754 
755  $model->setTemplateId(10);
756 
757  $this->assertEquals($expectedResult, $model->getType());
758  }
759 
763  public function getTypeDataProvider()
764  {
765  return [['text', 1], ['html', 2]];
766  }
767 }
return false
Definition: gallery.phtml:36
__()
Definition: __.php:13
$templateType
Definition: list.phtml:37
testGetVariablesOptionArray($withGroup, $templateVariables, $expectedResult)
$templateId
Definition: queue.php:15
testLoadDefault( $templateType, $templateText, $parsedTemplateText, $expectedTemplateSubject, $expectedOrigTemplateVariables, $expectedTemplateStyles)
testProcessTemplate($templateId, $expectedResult)
testIsValidForSend($senderName, $senderEmail, $templateSubject, $expectedValue)
$relativePath
Definition: get.php:35