Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FilterTest.php
Go to the documentation of this file.
1 <?php
7 
14 
19 class FilterTest extends \PHPUnit\Framework\TestCase
20 {
24  protected $model;
25 
29  protected $objectManager;
30 
31  protected function setUp()
32  {
34 
35  $this->model = $this->objectManager->create(
36  \Magento\Email\Model\Template\Filter::class
37  );
38  }
39 
45  public function testViewDirective()
46  {
47  $url = $this->model->viewDirective(
48  ['{{view url="Magento_Theme::favicon.ico"}}', 'view', ' url="Magento_Theme::favicon.ico"']
49  );
50  $this->assertStringEndsWith('favicon.ico', $url);
51  }
52 
58  public function testBlockDirective()
59  {
60  $class = Footer::class;
61  $data = [
62  "{{block class='$class' name='test.block' template='Magento_Theme::html/footer.phtml'}}",
63  'block',
64  " class='$class' name='test.block' template='Magento_Theme::html/footer.phtml'",
65  ];
66  $html = $this->model->blockDirective($data);
67  $this->assertContains('<div class="footer-container">', $html);
68  }
69 
74  public function testStoreDirective()
75  {
76  $url = $this->model->storeDirective(
77  ['{{store direct_url="arbitrary_url/"}}', 'store', ' direct_url="arbitrary_url/"']
78  );
79  $this->assertStringMatchesFormat('http://example.com/%sarbitrary_url/', $url);
80 
81  $url = $this->model->storeDirective(
82  ['{{store url="translation/ajax/index"}}', 'store', ' url="translation/ajax/index"']
83  );
84  $this->assertStringMatchesFormat('http://example.com/%stranslation/ajax/index/', $url);
85 
86  $this->model->setStoreId(0);
87  $backendUrlModel = $this->objectManager->create(\Magento\Backend\Model\Url::class);
88  $this->model->setUrlModel($backendUrlModel);
89  $url = $this->model->storeDirective(
90  ['{{store url="translation/ajax/index"}}', 'store', ' url="translation/ajax/index"']
91  );
92  $this->assertStringMatchesFormat('http://example.com/index.php/backend/translation/ajax/index/%A', $url);
93  }
94 
105  public function testLayoutDirective($area, $directiveParams, $expectedOutput)
106  {
108  $registration = $this->objectManager->get(\Magento\Theme\Model\Theme\Registration::class);
109  $registration->register();
110  $this->model = $this->objectManager->create(\Magento\Email\Model\Template\Filter::class);
113  $layout = $this->objectManager->create(\Magento\Framework\View\Layout::class);
114  $this->objectManager->addSharedInstance($layout, \Magento\Framework\View\Layout::class);
115  $this->objectManager->get(\Magento\Framework\View\DesignInterface::class)
116  ->setDesignTheme('Magento_EmailTest/default');
117 
118  $actualOutput = $this->model->layoutDirective(
119  ['{{layout ' . $directiveParams . '}}', 'layout', ' ' . $directiveParams]
120  );
121  $this->assertEquals($expectedOutput, trim($actualOutput));
122  }
123 
127  public function layoutDirectiveDataProvider()
128  {
129  $result = [
130  'area parameter - omitted' => [
131  'adminhtml',
132  'handle="email_template_test_handle"',
133  '<b>Email content for frontend/Magento/default theme</b>',
134  ],
135  'area parameter - frontend' => [
136  'adminhtml',
137  'handle="email_template_test_handle" area="frontend"',
138  '<b>Email content for frontend/Magento/default theme</b>',
139  ],
140  'area parameter - backend' => [
141  'frontend',
142  'handle="email_template_test_handle" area="adminhtml"',
143  '<b>Email content for adminhtml/Magento/default theme</b>',
144  ],
145  'custom parameter' => [
146  'frontend',
147  'handle="email_template_test_handle" template="Magento_Email::sample_email_content_custom.phtml"',
148  '<b>Custom Email content for frontend/Magento/default theme</b>',
149  ],
150  ];
151  return $result;
152  }
153 
161  public function testTransDirective($directive, $translations, $expectedResult)
162  {
163  $renderer = Phrase::getRenderer();
164 
165  $translator = $this->getMockBuilder(\Magento\Framework\Translate::class)
166  ->disableOriginalConstructor()
167  ->setMethods(['getData'])
168  ->getMock();
169 
170  $translator->expects($this->atLeastOnce())
171  ->method('getData')
172  ->will($this->returnValue($translations));
173 
174  $this->objectManager->addSharedInstance($translator, \Magento\Framework\Translate::class);
175  $this->objectManager->removeSharedInstance(\Magento\Framework\Phrase\Renderer\Translate::class);
176  Phrase::setRenderer($this->objectManager->create(\Magento\Framework\Phrase\RendererInterface::class));
177 
178  $this->assertEquals($expectedResult, $this->model->filter($directive));
179 
180  Phrase::setRenderer($renderer);
181  }
182 
186  public function transDirectiveDataProvider()
187  {
188  return [
189  [
190  '{{trans "foobar"}}',
191  [],
192  'foobar',
193  ],
194  [
195  '{{trans "foobar"}}',
196  ['foobar' => 'barfoo'],
197  'barfoo',
198  ]
199  ];
200  }
201 
216  public function testCssDirective($templateType, $directiveParams, $expectedOutput)
217  {
219  $registration = $this->objectManager->get(
220  \Magento\Theme\Model\Theme\Registration::class
221  );
222  $registration->register();
223  $this->setUpDesignParams();
224  $this->model->setStoreId('fixturestore')
225  ->setPlainTemplateMode($templateType == TemplateTypesInterface::TYPE_TEXT);
226 
227  $output = $this->model->cssDirective(['{{css ' . $directiveParams . '}}', 'css', ' ' . $directiveParams]);
228 
229  if ($expectedOutput !== '') {
230  $this->assertContains($expectedOutput, $output);
231  } else {
232  $this->assertSame($expectedOutput, $output);
233  }
234  }
235 
239  public function cssDirectiveDataProvider()
240  {
241  return [
242  'CSS from theme' => [
244  'file="css/email-1.css"',
245  'color: #111'
246  ],
247  'CSS from parent theme' => [
249  'file="css/email-2.css"',
250  'color: #222'
251  ],
252  'CSS from grandparent theme' => [
254  'file="css/email-3.css"',
255  'color: #333'
256  ],
257  'Missing file parameter' => [
259  '',
260  '/* "file" parameter must be specified */'
261  ],
262  'Plain-text template outputs nothing' => [
264  'file="css/email-1.css"',
265  '',
266  ],
267  'Empty or missing file' => [
269  'file="css/non-existent-file.css"',
270  '/* Contents of css/non-existent-file.css could not be loaded or is empty */'
271  ],
272  'File with compilation error results in error message' => [
274  'file="css/file-with-error.css"',
275  'variable @non-existent-variable is undefined',
276  ],
277  ];
278  }
279 
297  public function testInlinecssDirective(
298  $templateText,
299  $expectedOutput,
300  $productionMode = false,
301  $plainTemplateMode = false,
302  $isChildTemplateMode = false
303  ) {
305  $registration = $this->objectManager->get(
306  \Magento\Theme\Model\Theme\Registration::class
307  );
308  $registration->register();
309  $this->setUpDesignParams();
310 
311  $this->model->setPlainTemplateMode($plainTemplateMode);
312  $this->model->setIsChildTemplate($isChildTemplateMode);
313 
314  $appMode = $productionMode ? State::MODE_PRODUCTION : State::MODE_DEVELOPER;
315  $this->objectManager->get(\Magento\Framework\App\State::class)->setMode($appMode);
316 
317  $this->assertContains($expectedOutput, $this->model->filter($templateText));
318  }
319 
324  {
325  return [
326  'CSS from theme' => [
327  '<html><p></p> {{inlinecss file="css/email-inline-1.css"}}</html>',
328  '<p style="color: #111; text-align: left;">',
329  ],
330  'CSS from parent theme' => [
331  '<html><p></p> {{inlinecss file="css/email-inline-2.css"}}</html>',
332  '<p style="color: #222; text-align: left;">',
333  ],
334  'CSS from grandparent theme' => [
335  '<html><p></p> {{inlinecss file="css/email-inline-3.css"}}',
336  '<p style="color: #333; text-align: left;">',
337  ],
338  'Non-existent file results in unmodified markup' => [
339  '<html><p></p> {{inlinecss file="css/non-existent-file.css"}}</html>',
340  '<html><p></p> </html>',
341  ],
342  'Plain template mode results in unmodified markup' => [
343  '<html><p></p> {{inlinecss file="css/email-inline-1.css"}}</html>',
344  '<html><p></p> </html>',
345  false,
346  true,
347  ],
348  'Child template mode results in unmodified directive' => [
349  '<html><p></p> {{inlinecss file="css/email-inline-1.css"}}</html>',
350  '<html><p></p> {{inlinecss file="css/email-inline-1.css"}}</html>',
351  false,
352  false,
353  true,
354  ],
355  'Production mode - File with compilation error results in structurally unmodified markup' => [
356  '<html><p></p> {{inlinecss file="css/file-with-error.css"}}</html>',
357  '<p></p>',
358  true,
359  ],
360  'Developer mode - File with compilation error results in error message' => [
361  '<html><p></p> {{inlinecss file="css/file-with-error.css"}}</html>',
362  'CSS inlining error:',
363  false,
364  ],
365  ];
366  }
367 
377  public function testInlinecssDirectiveThrowsExceptionWhenMissingParameter($templateText)
378  {
380  $registration = $this->objectManager->get(
381  \Magento\Theme\Model\Theme\Registration::class
382  );
383  $registration->register();
384  $this->setUpDesignParams();
385 
386  $this->model->filter($templateText);
387  }
388 
393  {
394  return [
395  'Missing "file" parameter' => [
396  '{{inlinecss}}',
397  ],
398  'Missing "file" parameter value' => [
399  '{{inlinecss file=""}}',
400  ],
401  ];
402  }
403 
407  protected function setUpDesignParams()
408  {
409  $themeCode = 'Vendor_EmailTest/custom_theme';
410  $this->model->setDesignParams([
411  'area' => Area::AREA_FRONTEND,
412  'theme' => $themeCode,
413  'locale' => Locale::DEFAULT_SYSTEM_LOCALE,
414  ]);
415  }
416 }
testTransDirective($directive, $translations, $expectedResult)
Definition: FilterTest.php:161
$templateType
Definition: list.phtml:37
$_option $_optionId $class
Definition: date.phtml:13
static setRenderer(RendererInterface $renderer)
Definition: Phrase.php:46