public function testObsoleteViewPaths() { $allowedFiles = ['requirejs-config.js', 'layouts.xml']; $allowedThemeFiles = array_merge($allowedFiles, ['composer.json', 'theme.xml', 'LICENSE.txt', 'LICENSE_EE.txt', 'LICENSE_AFL.txt', 'registration.php']); $areas = '{frontend,adminhtml,base}'; $componentRegistrar = new ComponentRegistrar(); $pathsToCheck = []; foreach ($componentRegistrar->getPaths(ComponentRegistrar::THEME) as $themeDir) { $pathsToCheck[$themeDir . '/*'] = ['allowed_files' => $allowedThemeFiles, 'allowed_dirs' => ['layout', 'page_layout', 'templates', 'web', 'etc', 'i18n', 'media', '\\w+_\\w+']]; $pathsToCheck[$themeDir . '/*_*/*'] = ['allowed_files' => $allowedThemeFiles, 'allowed_dirs' => ['layout', 'page_layout', 'templates', 'web', 'email']]; } foreach ($componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleDir) { $pathsToCheck[$moduleDir . "/view/{$areas}/*"] = ['allowed_files' => $allowedFiles, 'allowed_dirs' => ['layout', 'page_layout', 'templates', 'web', 'ui_component', 'email']]; } $errors = []; foreach ($pathsToCheck as $path => $allowed) { $allowedFiles = $allowed['allowed_files']; $allowedDirs = $allowed['allowed_dirs']; $foundFiles = Glob::glob($path, Glob::GLOB_BRACE); if (!$foundFiles) { continue; } foreach ($foundFiles as $file) { $baseName = basename($file); if (is_dir($file)) { foreach ($allowedDirs as $allowedDir) { if (preg_match("#^{$allowedDir}\$#", $baseName)) { continue 2; } } } if (in_array($baseName, $allowedFiles)) { continue; } $errors[] = $file; } } if (!empty($errors)) { $this->fail('Unexpected files or directories found. Make sure they are not at obsolete locations:' . PHP_EOL . implode(PHP_EOL, $errors)); } }
/** * Get paths by pattern for specified component component * * @param string $componentType * @param string $componentName * @param string $pathPattern * @return array */ private function getPathByComponentPattern($componentType, $componentName, $pathPattern) { $files = []; if ($componentType == '*') { $componentTypes = [ComponentRegistrar::MODULE, ComponentRegistrar::LIBRARY, ComponentRegistrar::THEME, ComponentRegistrar::LANGUAGE]; } else { $componentTypes = [$componentType]; } foreach ($componentTypes as $type) { if ($componentName == '*') { $files = array_merge($files, $this->dirSearch->collectFiles($type, $pathPattern)); } else { $componentDir = $this->componentRegistrar->getPath($type, $componentName); if (!empty($componentDir)) { $files = array_merge($files, Glob::glob($componentDir . '/' . $pathPattern, Glob::GLOB_BRACE)); } } } return $files; }
/** * Copies configuration files from the main code base, so the installation could proceed in the tests directory * * @return void */ private function copyAppConfigFiles() { $globalConfigFiles = Glob::glob($this->_globalConfigDir . '/{di.xml,*/di.xml,vendor_path.php}', Glob::GLOB_BRACE); foreach ($globalConfigFiles as $file) { $targetFile = $this->_configDir . str_replace($this->_globalConfigDir, '', $file); $this->_ensureDirExists(dirname($targetFile)); if ($file !== $targetFile) { copy($file, $targetFile); } } }
/** * Search collect files * * @param string $type * @return array * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function collectItems($type) { $items = []; $rewrites = []; $fallbacks = [['path' => 'tests/app'], ['path' => 'generated']]; while ($fallback = array_pop($fallbacks)) { $path = isset($fallback['path']) ? $fallback['path'] : ''; $ns = isset($fallback['namespace']) ? $fallback['namespace'] : ''; $location = $path . ($ns ? '/' . str_replace('\\', '/', $ns) : ''); $pattern = $this->_getPattern($type, $location); $filesIterator = Glob::glob($pattern, Glob::GLOB_BRACE); foreach ($filesIterator as $filePath) { if (!is_dir($filePath)) { $this->_processItem($items, $rewrites, $filePath, $location, $path); } else { $dirIterator = new \RegexIterator(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($filePath, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS)), '/.php$/i'); foreach ($dirIterator as $info) { /** @var $info \SplFileInfo */ $realPath = $info->getPathname(); if (is_link($realPath)) { $realPath = readlink($realPath); } $this->_processItem($items, $rewrites, $realPath, $location, $path); } } } } return $items; }
/** * Resolve semicolon-separated relative glob pattern(s) to matched absolute paths * * @param string $pattern * @return array */ protected function _resolvePathPattern($pattern) { $result = []; $allPatterns = preg_split('/\\s*;\\s*/', trim($pattern), -1, PREG_SPLIT_NO_EMPTY); foreach ($allPatterns as $onePattern) { $onePattern = $this->_resolvePath($onePattern); $files = Glob::glob($onePattern, Glob::GLOB_BRACE); $result = array_merge($result, $files); } return $result; }
/** * Search paths by given regex * * @param string $pattern * @param string $path * @return string[] * @throws FileSystemException */ public function search($pattern, $path) { clearstatcache(); $globPattern = rtrim($path, '/') . '/' . ltrim($pattern, '/'); $result = Glob::glob($globPattern, Glob::GLOB_BRACE); return is_array($result) ? $result : []; }