Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TemplateFilesTest.php
Go to the documentation of this file.
1 <?php
7 
9 {
13  public function testTemplates()
14  {
15  $invalidTemplates = [];
16  foreach ($this->templatesDataProvider() as $template) {
17  list($area, $themeId, $module, $file, $xml) = $template;
18  $params = ['area' => $area, 'themeId' => $themeId, 'module' => $module];
19  try {
20  $templateFilename = \Magento\TestFramework\Helper\Bootstrap::getObjectmanager()
21  ->get(\Magento\Framework\View\FileSystem::class)
22  ->getTemplateFileName($file, $params);
23  $this->assertFileExists($templateFilename);
24  } catch (\PHPUnit\Framework\ExpectationFailedException $e) {
25  $invalidTemplates[] = "File \"{$templateFilename}\" does not exist." .
26  PHP_EOL .
27  "Parameters: {$area}/{$themeId} {$module}::{$file}" .
28  PHP_EOL .
29  'Layout update: ' .
30  $xml;
31  }
32  }
33 
34  $this->assertEmpty(
35  $invalidTemplates,
36  "Invalid templates found:\n\n" . implode("\n-----\n", $invalidTemplates)
37  );
38  }
39 
40  public function templatesDataProvider()
41  {
42  $templates = [];
43 
44  $themes = $this->_getDesignThemes();
45  foreach ($themes as $theme) {
48  \Magento\Framework\View\Layout\ProcessorInterface::class,
49  ['theme' => $theme]
50  );
51  $layoutTemplates = $this->_getLayoutTemplates($layoutUpdate->getFileLayoutUpdatesXml());
52  foreach ($layoutTemplates as $templateData) {
53  $templates[] = array_merge([$theme->getArea(), $theme->getId()], $templateData);
54  }
55  }
56 
57  return $templates;
58  }
59 
67  protected function _getLayoutTemplates($layoutXml)
68  {
69  $templates = [];
70 
71  $blocks = $layoutXml->xpath('//block');
72  foreach ($blocks as $block) {
73  $attributes = $block->attributes();
74  if (isset($attributes['template'])) {
75  $module = $this->_getBlockModule($block);
76  if (!$this->_isTemplateForDisabledModule($module, (string)$attributes['template'])) {
77  $templates[] = [$module, (string)$attributes['template'], $block->asXML()];
78  }
79  }
80  }
81 
82  $layoutTemplates = $layoutXml->xpath('//template');
83  foreach ($layoutTemplates as $template) {
84  $action = $template->xpath("parent::*");
85  $attributes = $action[0]->attributes();
86  switch ($attributes['method']) {
87  case 'setTemplate':
88  $parent = $action[0]->xpath("parent::*");
89  $attributes = $parent[0]->attributes();
90  $referenceName = (string)$attributes['name'];
91  $block = $layoutXml->xpath(
92  "//block[@name='{$referenceName}'] | //referenceBlock[@name='{$referenceName}']"
93  );
94  $module = $this->_getBlockModule($block[0]);
95  if (!$template->attributes() && !$this->_isTemplateForDisabledModule($module, (string)$template)) {
96  $templates[] = [$module, (string)$template, $parent[0]->asXml()];
97  }
98  break;
99  case 'addInformationRenderer':
100  case 'addMergeSettingsBlockType':
101  $blockType = $action[0]->xpath('block');
102  $module = $this->_getBlockModule($blockType[0]);
103  if (!$this->_isTemplateForDisabledModule($module, (string)$template)) {
104  $templates[] = [$module, (string)$template, $action[0]->asXml()];
105  }
106  break;
107  default:
108  break;
109  }
110  }
111  return $templates;
112  }
113 
120  protected function _getBlockModule($xmlNode)
121  {
122  $attributes = $xmlNode->attributes();
123  if (isset($attributes['type'])) {
124  $class = (string)$attributes['type'];
125  } else {
126  $class = (string)$xmlNode;
127  }
128  $blockModule = substr($class, 0, strpos($class, '_Block'));
129  return $blockModule;
130  }
131 
139  protected function _isTemplateForDisabledModule($blockModule, $template)
140  {
141  $enabledModules = $this->_getEnabledModules();
142 
143  if (!isset($enabledModules[$blockModule])) {
144  return true;
145  }
146  return $this->_isFileForDisabledModule($template);
147  }
148 }
$block
Definition: block.php:8
$_option $_optionId $class
Definition: date.phtml:13
$attributes
Definition: matrix.phtml:13
$theme
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$templateData
$template
Definition: export.php:12