Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions
StaticFilesTest Class Reference
Inheritance diagram for StaticFilesTest:

Public Member Functions

 testReferencesFromStaticFiles ($area, $themePath, $locale, $module, $filePath, $absolutePath)
 
 referencesFromStaticFilesDataProvider ()
 
 testLessNotConfusedWithCss ($area, $themePath, $locale, $module, $filePath)
 
 lessNotConfusedWithCssDataProvider ()
 
 testReferencesFromPhtmlFiles ($phtmlFile, $area, $themePath, $fileId)
 
 referencesFromPhtmlFilesDataProvider ()
 
 testReferencesFromLayoutFiles ($layoutFile, $area, $themePath, $fileId)
 
 referencesFromLayoutFilesDataProvider ()
 

Protected Member Functions

 setUp ()
 

Detailed Description

An integrity test that searches for references to static files and asserts that they are resolved via fallback @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 15 of file StaticFilesTest.php.

Member Function Documentation

◆ lessNotConfusedWithCssDataProvider()

lessNotConfusedWithCssDataProvider ( )
Returns
array

Definition at line 228 of file StaticFilesTest.php.

229  {
230  return \Magento\Framework\App\Utility\Files::init()->getStaticPreProcessingFiles('*.{less,css}');
231  }

◆ referencesFromLayoutFilesDataProvider()

referencesFromLayoutFilesDataProvider ( )
Returns
array

Definition at line 302 of file StaticFilesTest.php.

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  }
foreach($appDirs as $dir) $files

◆ referencesFromPhtmlFilesDataProvider()

referencesFromPhtmlFilesDataProvider ( )
Returns
array

Definition at line 254 of file StaticFilesTest.php.

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  }
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52

◆ referencesFromStaticFilesDataProvider()

referencesFromStaticFilesDataProvider ( )
Returns
array

Definition at line 188 of file StaticFilesTest.php.

189  {
190  return \Magento\Framework\App\Utility\Files::init()->getStaticPreProcessingFiles('*.{less,css}');
191  }

◆ setUp()

setUp ( )
protected

Definition at line 59 of file StaticFilesTest.php.

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  }
$om

◆ testLessNotConfusedWithCss()

testLessNotConfusedWithCss (   $area,
  $themePath,
  $locale,
  $module,
  $filePath 
)

There must be either .css or .less file, because if there are both, then .less will not be found by fallback

Parameters
string$area
string$themePath
string$locale
string$module
string$filePath@dataProvider lessNotConfusedWithCssDataProvider

Definition at line 203 of file StaticFilesTest.php.

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  }
$fileName
Definition: translate.phtml:15

◆ testReferencesFromLayoutFiles()

testReferencesFromLayoutFiles (   $layoutFile,
  $area,
  $themePath,
  $fileId 
)
Parameters
string$layoutFile
string$area
string$themePath
string$fileId@dataProvider referencesFromLayoutFilesDataProvider

Definition at line 290 of file StaticFilesTest.php.

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  }

◆ testReferencesFromPhtmlFiles()

testReferencesFromPhtmlFiles (   $phtmlFile,
  $area,
  $themePath,
  $fileId 
)

Test if references $this->getViewFileUrl() in .phtml-files are correct

Parameters
string$phtmlFile
string$area
string$themePath
string$fileId@dataProvider referencesFromPhtmlFilesDataProvider

Definition at line 242 of file StaticFilesTest.php.

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  }

◆ testReferencesFromStaticFiles()

testReferencesFromStaticFiles (   $area,
  $themePath,
  $locale,
  $module,
  $filePath,
  $absolutePath 
)

Scan references to files from other static files and assert they are correct

The CSS or LESS files may refer to other resources using @import or url() notation We want to check integrity of all these references Note that the references may have syntax specific to the Magento preprocessing subsystem

Parameters
string$area
string$themePath
string$locale
string$module
string$filePath
string$absolutePath@dataProvider referencesFromStaticFilesDataProvider

The LESS library treats the related resources with relative links not in the same way as CSS: when another LESS file is included, it is embedded directly into the resulting document, but the relative paths of related resources are not adjusted accordingly to the new root file. Probably it is a bug of the LESS library.

Definition at line 91 of file StaticFilesTest.php.

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  }
$contents
Definition: website.php:14
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
static getRelatedPath($relativeTo, $path)
Definition: FileSystem.php:230

The documentation for this class was generated from the following file: