28 class ViewFileReferenceTest
extends \PHPUnit\Framework\TestCase
33 protected static $_fallbackRule;
38 protected static $_viewFilesFallback;
43 protected static $_filesFallback;
48 protected static $_checkThemeLocales = [];
53 protected static $_themeCollection;
58 protected static $_componentRegistrar;
60 public static function setUpBeforeClass()
64 [
'preferences' => [\
Magento\Theme\Model\Theme::class => \
Magento\Theme\Model\Theme\Data::class]]
67 self::$_componentRegistrar =
$objectManager->get(\
Magento\Framework\Component\ComponentRegistrar::class);
71 self::$_fallbackRule = $fallbackPool->getRule(
72 $fallbackPool::TYPE_STATIC_FILE
76 \
Magento\Framework\View\Design\FileResolution\Fallback\StaticFile::class
78 self::$_filesFallback =
$objectManager->get(\
Magento\Framework\View\Design\FileResolution\Fallback\File::class);
84 self::$_checkThemeLocales = [];
85 foreach (self::$_themeCollection as
$theme) {
86 $themeLocales = self::_getThemeLocales(
$theme);
87 $themeLocales[] =
null;
89 self::$_checkThemeLocales[
$theme->getFullPath()] = $themeLocales;
99 protected static function _getThemeLocales(\
Magento\Framework\View\Design\ThemeInterface
$theme)
102 $patternDir = self::_getLocalePatternDir(
$theme);
103 foreach (\ResourceBundle::getLocales(
'') as $locale) {
104 $dir = str_replace(
'<locale_placeholder>', $locale, $patternDir);
119 protected static function _getLocalePatternDir(\
Magento\Framework\View\Design\ThemeInterface
$theme)
121 $localePlaceholder =
'<locale_placeholder>';
122 $params = [
'area' =>
$theme->getArea(),
'theme' =>
$theme,
'locale' => $localePlaceholder];
123 $patternDirs = self::$_fallbackRule->getPatternDirs(
$params);
124 $themePath = self::$_componentRegistrar->getPath(
128 foreach ($patternDirs as $patternDir) {
129 $patternPath = $patternDir .
'/';
130 if ((strpos($patternPath, $themePath) !==
false)
131 && (strpos($patternPath, $localePlaceholder) !==
false)
136 throw new \Exception(
'Unable to determine theme locale path');
145 public function testModularFallback($modularCall, array $usages, $area)
147 list(, $file) = explode(\
Magento\Framework\View\Asset\Repository::FILE_ID_SEPARATOR, $modularCall);
149 $wrongResolutions = [];
150 foreach (self::$_themeCollection as
$theme) {
151 if ($area &&
$theme->getArea() != $area) {
155 $found = $this->_getFileResolutions(
$theme, $file);
156 $wrongResolutions = array_merge($wrongResolutions, $found);
159 if ($wrongResolutions) {
162 "Found modular call:\n %s in\n %s\n which may resolve to non-modular location(s):\n %s",
164 implode(
', ', $usages),
165 implode(
', ', $wrongResolutions)
178 protected function _getFileResolutions(\
Magento\Framework\View\Design\ThemeInterface
$theme, $file)
181 $fileResolved = self::$_filesFallback->getFile(
$theme->getArea(),
$theme, $file);
182 if (file_exists($fileResolved)) {
183 $found[$fileResolved] = $fileResolved;
186 foreach (self::$_checkThemeLocales[
$theme->getFullPath()] as $locale) {
187 $fileResolved = self::$_viewFilesFallback->getFile(
$theme->getArea(),
$theme, $locale, $file);
188 if (file_exists($fileResolved)) {
189 $found[$fileResolved] = $fileResolved;
198 public static function modularFallbackDataProvider()
201 foreach (self::_getFilesToProcess() as $file) {
202 $file = (string)$file;
204 $modulePattern =
'[A-Z][a-z]+_[A-Z][a-z]+';
205 $filePattern =
'[[:alnum:]_/-]+\\.[[:alnum:]_./-]+';
207 . preg_quote(\
Magento\Framework\View\Asset\Repository::FILE_ID_SEPARATOR)
208 . $filePattern .
'#S';
213 $area = self::_getArea($file);
215 foreach ($matches[0] as $modularCall) {
216 $dataSetKey = $modularCall .
' @ ' . ($area ?:
'any area');
218 if (!isset(
$result[$dataSetKey])) {
219 $result[$dataSetKey] = [
'modularCall' => $modularCall,
'usages' => [],
'area' => $area];
221 $result[$dataSetKey][
'usages'][$file] = $file;
232 protected static function _getFilesToProcess()
240 foreach ($dirs as $dir) {
241 $iterator = new \RecursiveIteratorIterator(
242 new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS)
258 protected static function _getArea($file)
260 $file = str_replace(
'\\',
'/', $file);
264 $areaPatterns[] =
'#' . $themeDir .
'/([^/]+)/#S';
267 $areaPatterns[] =
'#' . $moduleDir .
'/view/([^/]+)/#S';
269 foreach ($areaPatterns as
$pattern) {
270 if (preg_match(
$pattern, $file, $matches)) {
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
static getObjectManager()