Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StaticFilesTest.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Test\Integrity;
8 
10 
15 class StaticFilesTest extends \PHPUnit\Framework\TestCase
16 {
20  private $fallback;
21 
25  private $explicitFallback;
26 
30  private $themeRepo;
31 
35  private $design;
36 
40  private $baseTheme;
41 
45  private $alternativeResolver;
46 
52  private $simpleFactory;
53 
57  private $filesystem;
58 
59  protected function setUp()
60  {
61  $om = \Magento\TestFramework\Helper\Bootstrap::getObjectmanager();
62  $this->fallback = $om->get(\Magento\Framework\View\Design\FileResolution\Fallback\StaticFile::class);
63  $this->explicitFallback = $om->get(
64  \Magento\Framework\View\Design\FileResolution\Fallback\Resolver\Simple::class
65  );
66  $this->themeRepo = $om->get(\Magento\Framework\View\Design\Theme\FlyweightFactory::class);
67  $this->design = $om->get(\Magento\Framework\View\DesignInterface::class);
68  $this->baseTheme = $om->get(\Magento\Framework\View\Design\ThemeInterface::class);
69  $this->alternativeResolver = $om->get(
70  \Magento\Framework\View\Design\FileResolution\Fallback\Resolver\Alternative::class
71  );
72  $this->simpleFactory = $om->get(\Magento\Framework\View\Design\Fallback\Rule\SimpleFactory::class);
73  $this->filesystem = $om->get(\Magento\Framework\Filesystem::class);
74  }
75 
91  public function testReferencesFromStaticFiles($area, $themePath, $locale, $module, $filePath, $absolutePath)
92  {
93  $contents = file_get_contents($absolutePath);
94  preg_match_all(
95  \Magento\Framework\View\Url\CssResolver::REGEX_CSS_RELATIVE_URLS,
96  $contents,
97  $matches
98  );
99  foreach ($matches[1] as $relatedResource) {
100  if (false !== strpos($relatedResource, '@')) { // unable to parse paths with LESS variables/mixins
101  continue;
102  }
103  list($relatedModule, $relatedPath) =
104  \Magento\Framework\View\Asset\Repository::extractModule($relatedResource);
105  if ($relatedModule) {
106  $fallbackModule = $relatedModule;
107  } else {
108  if ('less' == pathinfo($filePath, PATHINFO_EXTENSION)) {
115  $this->markTestSkipped("Due to LESS library specifics, the '{$relatedResource}' cannot be tested.");
116  }
117  $fallbackModule = $module;
118  $relatedPath = \Magento\Framework\View\FileSystem::getRelatedPath($filePath, $relatedResource);
119  }
120  // the $relatedPath will be suitable for feeding to the fallback system
121  $staticFile = $this->getStaticFile($area, $themePath, $locale, $relatedPath, $fallbackModule);
122  if (empty($staticFile) && substr($relatedPath, 0, 2) === '..') {
123  //check if static file exists on lib level
124  $path = substr($relatedPath, 2);
125  $libDir = rtrim($this->filesystem->getDirectoryRead(DirectoryList::LIB_WEB)->getAbsolutePath(), '/');
126  $rule = $this->simpleFactory->create(['pattern' => $libDir]);
127  $params = ['area' => $area, 'theme' => $themePath, 'locale' => $locale];
128  $staticFile = $this->alternativeResolver->resolveFile($rule, $path, $params);
129  }
130  $this->assertNotEmpty(
131  $staticFile,
132  "The related resource cannot be resolved through fallback: '{$relatedResource}'"
133  );
134  }
135  }
136 
144  private function getDefaultThemePath($area)
145  {
146  switch ($area) {
147  case 'frontend':
148  return $this->design->getConfigurationDesignTheme($area);
149  case 'adminhtml':
150  return 'Magento/backend';
151  case 'doc':
152  return 'Magento/blank';
153  default:
154  throw new \LogicException('Unable to determine theme path');
155  }
156  }
157 
169  private function getStaticFile($area, $theme, $locale, $filePath, $module = null, $isExplicit = false)
170  {
171  if ($area == 'base') {
172  $theme = $this->baseTheme;
173  }
174  if (!is_object($theme)) {
175  $themePath = $theme ?: $this->getDefaultThemePath($area);
176  $theme = $this->themeRepo->create($themePath, $area);
177  }
178  if ($isExplicit) {
180  return $this->explicitFallback->resolve($type, $filePath, $area, $theme, $locale, $module);
181  }
182  return $this->fallback->getFile($area, $theme, $locale, $filePath, $module);
183  }
184 
189  {
190  return \Magento\Framework\App\Utility\Files::init()->getStaticPreProcessingFiles('*.{less,css}');
191  }
192 
203  public function testLessNotConfusedWithCss($area, $themePath, $locale, $module, $filePath)
204  {
205  if (false !== strpos($filePath, 'widgets.css')) {
206  $filePath .= '';
207  }
208  $fileName = pathinfo($filePath, PATHINFO_FILENAME);
209  $dirName = dirname($filePath);
210  if ('.' == $dirName) {
211  $dirName = '';
212  } else {
213  $dirName .= '/';
214  }
215  $cssPath = $dirName . $fileName . '.css';
216  $lessPath = $dirName . $fileName . '.less';
217  $cssFile = $this->getStaticFile($area, $themePath, $locale, $cssPath, $module, true);
218  $lessFile = $this->getStaticFile($area, $themePath, $locale, $lessPath, $module, true);
219  $this->assertFalse(
220  $cssFile && $lessFile,
221  "A resource file of only one type must exist. Both found: '$cssFile' and '$lessFile'"
222  );
223  }
224 
229  {
230  return \Magento\Framework\App\Utility\Files::init()->getStaticPreProcessingFiles('*.{less,css}');
231  }
232 
242  public function testReferencesFromPhtmlFiles($phtmlFile, $area, $themePath, $fileId)
243  {
244  list($module, $filePath) = \Magento\Framework\View\Asset\Repository::extractModule($fileId);
245  $this->assertNotEmpty(
246  $this->getStaticFile($area, $themePath, 'en_US', $filePath, $module),
247  "Unable to locate '{$fileId}' reference from {$phtmlFile}"
248  );
249  }
250 
255  {
256  $result = [];
257  foreach (\Magento\Framework\App\Utility\Files::init()->getPhtmlFiles(true, false) as $info) {
258  list($area, $themePath, , , $file) = $info;
259  foreach ($this->collectGetViewFileUrl($file) as $fileId) {
260  $result[] = [$file, $area, $themePath, $fileId];
261  }
262  }
263  return $result;
264  }
265 
272  private function collectGetViewFileUrl($file)
273  {
274  $result = [];
275  if (preg_match_all('/\$block->getViewFileUrl\(\'([^\']+?)\'\)/', file_get_contents($file), $matches)) {
276  foreach ($matches[1] as $fileId) {
277  $result[] = $fileId;
278  }
279  }
280  return $result;
281  }
282 
290  public function testReferencesFromLayoutFiles($layoutFile, $area, $themePath, $fileId)
291  {
292  list($module, $filePath) = \Magento\Framework\View\Asset\Repository::extractModule($fileId);
293  $this->assertNotEmpty(
294  $this->getStaticFile($area, $themePath, 'en_US', $filePath, $module),
295  "Unable to locate '{$fileId}' reference from layout XML in {$layoutFile}"
296  );
297  }
298 
303  {
304  $result = [];
305  $files = \Magento\Framework\App\Utility\Files::init()->getLayoutFiles(['with_metainfo' => true], false);
306  foreach ($files as $metaInfo) {
307  list($area, $themePath, , , $file) = $metaInfo;
308  foreach ($this->collectFileIdsFromLayout($file) as $fileId) {
309  $result[] = [$file, $area, $themePath, $fileId];
310  }
311  }
312  return $result;
313  }
314 
321  private function collectFileIdsFromLayout($file)
322  {
323  $xml = simplexml_load_file($file);
324  $elements = $xml->xpath('//head/css|link|script');
325  $result = [];
326  if ($elements) {
327  foreach ($elements as $node) {
328  $result[] = (string)$node;
329  }
330  }
331  return $result;
332  }
333 }
$contents
Definition: website.php:14
testReferencesFromStaticFiles($area, $themePath, $locale, $module, $filePath, $absolutePath)
testReferencesFromLayoutFiles($layoutFile, $area, $themePath, $fileId)
testReferencesFromPhtmlFiles($phtmlFile, $area, $themePath, $fileId)
$type
Definition: item.phtml:13
$fileName
Definition: translate.phtml:15
$theme
$om
testLessNotConfusedWithCss($area, $themePath, $locale, $module, $filePath)
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
foreach($appDirs as $dir) $files
static getRelatedPath($relativeTo, $path)
Definition: FileSystem.php:230