Ejemplo n.º 1
0
 public function testReadListsCorruptedFile()
 {
     $result = Files::init()->readLists(__DIR__ . '/_files/list_corrupted_file.txt');
     foreach ($result as $path) {
         $this->assertNotContains('unknown.txt', $path);
     }
 }
Ejemplo n.º 2
0
 /**
  * Gets alien dependencies information for current module by analyzing file's contents
  *
  * @param string $currentModule
  * @param string $fileType
  * @param string $file
  * @param string $contents
  * @return array
  */
 public function getDependencyInfo($currentModule, $fileType, $file, &$contents)
 {
     if (!in_array($fileType, ['php', 'template'])) {
         return [];
     }
     $pattern = '~\\b(?<class>(?<module>(' . implode('_|', \Magento\Framework\App\Utility\Files::init()->getNamespaces()) . '[_\\\\])[a-zA-Z0-9]+)[a-zA-Z0-9_\\\\]*)\\b~';
     $dependenciesInfo = [];
     if (preg_match_all($pattern, $contents, $matches)) {
         $matches['module'] = array_unique($matches['module']);
         foreach ($matches['module'] as $i => $referenceModule) {
             $referenceModule = str_replace('_', '\\', $referenceModule);
             if ($currentModule == $referenceModule) {
                 continue;
             }
             $dependenciesInfo[] = ['module' => $referenceModule, 'type' => \Magento\TestFramework\Dependency\RuleInterface::TYPE_HARD, 'source' => trim($matches['class'][$i])];
         }
     }
     $result = $this->_caseGetUrl($currentModule, $contents);
     if (count($result)) {
         $dependenciesInfo = array_merge($dependenciesInfo, $result);
     }
     $result = $this->_caseLayoutBlock($currentModule, $fileType, $file, $contents);
     if (count($result)) {
         $dependenciesInfo = array_merge($dependenciesInfo, $result);
     }
     return $dependenciesInfo;
 }
 /**
  * Build Framework dependencies report
  *
  * @param string $outputPath
  * @return void
  */
 protected function buildReport($outputPath)
 {
     $filePaths = $this->registrar->getPaths(ComponentRegistrar::MODULE);
     $filesForParse = Files::init()->getFiles($filePaths, '*');
     $configFiles = Files::init()->getConfigFiles('module.xml', [], false);
     ServiceLocator::getFrameworkDependenciesReportBuilder()->build(['parse' => ['files_for_parse' => $filesForParse, 'config_files' => $configFiles, 'declared_namespaces' => Files::init()->getNamespaces()], 'write' => ['report_filename' => $outputPath]]);
 }
 public function testObsoleteJavascriptAttributeType()
 {
     $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
     $invoker(function ($file) {
         $this->assertNotRegexp('/type="text\\/javascript"/', file_get_contents($file), 'Please do not use "text/javascript" type attribute.');
     }, \Magento\Framework\App\Utility\Files::init()->getPhtmlFiles());
 }
Ejemplo n.º 5
0
 /**
  * Constructor
  *
  * @param array $mapRouters
  * @param array $mapLayoutBlocks
  * @param array $mapLayoutHandles
  */
 public function __construct(array $mapRouters, array $mapLayoutBlocks, array $mapLayoutHandles)
 {
     $this->_mapRouters = $mapRouters;
     $this->_mapLayoutBlocks = $mapLayoutBlocks;
     $this->_mapLayoutHandles = $mapLayoutHandles;
     $this->_namespaces = implode('|', \Magento\Framework\App\Utility\Files::init()->getNamespaces());
 }
Ejemplo n.º 6
0
 /**
  * Returns whitelist based on blacklist and git changed files
  *
  * @param array $fileTypes
  * @param string $changedFilesBaseDir
  * @param string $baseFilesFolder
  * @return array
  */
 public static function getWhitelist($fileTypes = ['php'], $changedFilesBaseDir = '', $baseFilesFolder = '')
 {
     $globPatternsFolder = self::getBaseFilesFolder();
     if ('' !== $baseFilesFolder) {
         $globPatternsFolder = $baseFilesFolder;
     }
     $directoriesToCheck = Files::init()->readLists($globPatternsFolder . '/_files/whitelist/common.txt');
     $changedFiles = [];
     $globFilesListPattern = ($changedFilesBaseDir ?: self::getChangedFilesBaseDir()) . '/_files/changed_files*';
     foreach (glob($globFilesListPattern) as $listFile) {
         $changedFiles = array_merge($changedFiles, file($listFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
     }
     array_walk($changedFiles, function (&$file) {
         $file = BP . '/' . $file;
     });
     $changedFiles = array_filter($changedFiles, function ($path) use($directoriesToCheck, $fileTypes) {
         if (!file_exists($path)) {
             return false;
         }
         $path = realpath($path);
         foreach ($directoriesToCheck as $directory) {
             $directory = realpath($directory);
             if (strpos($path, $directory) === 0) {
                 if (!empty($fileTypes)) {
                     return in_array(pathinfo($path, PATHINFO_EXTENSION), $fileTypes);
                 }
                 return true;
             }
         }
         return false;
     });
     return $changedFiles;
 }
 public function testLocalXmlFilesAbsent()
 {
     $area = '*';
     $package = '*';
     $theme = '*';
     $this->assertEmpty(glob(\Magento\Framework\App\Utility\Files::init()->getPathToSource() . "/app/design/{$area}/{$package}/{$theme}/local.xml"));
 }
Ejemplo n.º 8
0
 public function testObsoleteDirectives()
 {
     $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
     $invoker(function ($file) {
         $this->assertNotRegExp('/\\{\\{htmlescape.*?\\}\\}/i', file_get_contents($file), 'Directive {{htmlescape}} is obsolete. Use {{escapehtml}} instead.');
     }, \Magento\Framework\App\Utility\Files::init()->getEmailTemplates());
 }
Ejemplo n.º 9
0
 /**
  * Return layout handles that are declared in the base layouts for frontend
  *
  * @return array
  */
 protected function _getBaseFrontendHandles()
 {
     if ($this->_baseFrontendHandles === null) {
         $files = \Magento\Framework\App\Utility\Files::init()->getLayoutFiles(['include_design' => false, 'area' => 'frontend'], false);
         $this->_baseFrontendHandles = $this->_extractLayoutHandles($files);
     }
     return $this->_baseFrontendHandles;
 }
 /**
  * Helper method to setup the black and white lists
  *
  * @param string $type
  * @return void
  */
 public static function setupFileLists($type = '')
 {
     if ($type != '' && !preg_match('/\\/$/', $type)) {
         $type = $type . '/';
     }
     self::$whiteList = Files::init()->readLists(__DIR__ . '/_files/' . $type . 'whitelist/*.txt');
     self::$blackList = Files::init()->readLists(__DIR__ . '/_files/' . $type . 'blacklist/*.txt');
 }
Ejemplo n.º 11
0
 /**
  * Skip blacklisted files
  *
  * @param array $fileList
  * @return array
  * @throws \Exception
  */
 private function filterFiles(array $fileList)
 {
     $blackListFiles = Files::init()->readLists(__DIR__ . '/_files/blacklist/*.txt');
     $filter = function ($value) use($blackListFiles) {
         return !in_array($value, $blackListFiles);
     };
     return array_filter($fileList, $filter);
 }
 public function testXmlFiles()
 {
     $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
     $invoker(function ($configFile) {
         $schema = $this->urnResolver->getRealPath('urn:magento:module:Magento_Widget:etc/widget.xsd');
         $this->_validateFileExpectSuccess($configFile, $schema);
     }, array_merge(\Magento\Framework\App\Utility\Files::init()->getConfigFiles('widget.xml'), \Magento\Framework\App\Utility\Files::init()->getLayoutConfigFiles('widget.xml')));
 }
Ejemplo n.º 13
0
 /**
  * @return array
  */
 public function declaredConsistentlyDataProvider()
 {
     $result = [];
     $root = \Magento\Framework\App\Utility\Files::init()->getPathToSource();
     foreach (Package::readDeclarationFiles($root) as $row) {
         $result[] = $row;
     }
     return $result;
 }
Ejemplo n.º 14
0
 public function testBlocksIntoContainers()
 {
     $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
     $invoker(function ($file) {
         $xml = simplexml_load_file($file);
         $this->assertSame([], $xml->xpath('/widgets/*/supported_blocks'), 'Obsolete node: <supported_blocks>. To be replaced with <supported_containers>');
         $this->assertSame([], $xml->xpath('/widgets/*/*/*/block_name'), 'Obsolete node: <block_name>. To be replaced with <container_name>');
     }, \Magento\Framework\App\Utility\Files::init()->getConfigFiles('widget.xml'));
 }
 /**
  * @return void
  */
 public function testXssSensitiveOutput()
 {
     $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
     $xssOutputValidator = new XssOutputValidator();
     $invoker(function ($file) use($xssOutputValidator) {
         $lines = $xssOutputValidator->getLinesWithXssSensitiveOutput($file);
         $this->assertEmpty($lines, "Potentially XSS vulnerability. " . "Please verify that output is escaped at lines " . $lines);
     }, Files::init()->getPhtmlFiles());
 }
Ejemplo n.º 16
0
 public function testGetLayoutConfigFiles()
 {
     $this->dirSearch->expects($this->once())->method('collectFiles')->with(ComponentRegistrar::THEME, '/etc/some.file')->willReturn(['/one/some.file', '/two/some.file']);
     $expected = ['/one/some.file', '/two/some.file'];
     $actual = Files::init()->getLayoutConfigFiles('some.file', false);
     $this->assertSame($expected, $actual);
     // Check that the result is cached (collectFiles() is called only once)
     $this->assertSame($expected, $actual);
 }
 /**
  * @param array $deniedDirectives
  * @return string
  */
 protected function createMessage($deniedDirectives)
 {
     $rootPath = Files::init()->getPathToSource();
     $message = 'HHVM-incompatible ini_get/ini_set options were found:';
     foreach ($deniedDirectives as $file => $fileDeniedDirectives) {
         $message .= "\n" . str_replace($rootPath, '', $file) . ': [' . implode(', ', $fileDeniedDirectives) . ']';
     }
     return $message;
 }
 public function testSystemConfigurationDeclaration()
 {
     $fileList = \Magento\Framework\App\Utility\Files::init()->getConfigFiles('system.xml', ['wsdl.xml', 'wsdl2.xml', 'wsi.xml'], false);
     foreach ($fileList as $configFile) {
         $configXml = simplexml_load_file($configFile);
         $xpath = '/config/tabs|/config/sections';
         $this->assertEmpty($configXml->xpath($xpath), 'Obsolete system configuration structure detected in file ' . $configFile . '.');
     }
 }
Ejemplo n.º 19
0
 public function testAclDeclarations()
 {
     $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
     $invoker(function ($aclFile) {
         $aclXml = simplexml_load_file($aclFile);
         $xpath = '/config/acl/*[boolean(./children) or boolean(./title)]';
         $this->assertEmpty($aclXml->xpath($xpath), 'Obsolete acl structure detected in file ' . $aclFile . '.');
     }, \Magento\Framework\App\Utility\Files::init()->getMainConfigFiles());
 }
Ejemplo n.º 20
0
 public function testXmlFiles()
 {
     if (null === $this->_getXmlName()) {
         $this->markTestSkipped('No XML validation of files requested');
     }
     $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
     $invoker(function ($configFile) {
         $this->_validateFileExpectSuccess($configFile, $this->_getXsd(), $this->_getFileXsd());
     }, \Magento\Framework\App\Utility\Files::init()->getConfigFiles($this->_getXmlName()));
 }
 public function testGetChildHtml()
 {
     $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
     $invoker(function ($file) {
         $result = \Magento\Framework\App\Utility\Classes::getAllMatches(file_get_contents($file), "/(->getChildHtml\\([^,()]+, ?[^,()]+,)/i");
         $this->assertEmpty($result, "3rd parameter is not needed anymore for getChildHtml() in '{$file}': " . print_r($result, true));
         $result = \Magento\Framework\App\Utility\Classes::getAllMatches(file_get_contents($file), "/(->getChildChildHtml\\([^,()]+, ?[^,()]+, ?[^,()]+,)/i");
         $this->assertEmpty($result, "4th parameter is not needed anymore for getChildChildHtml() in '{$file}': " . print_r($result, true));
     }, Files::init()->getPhpFiles(Files::INCLUDE_APP_CODE | Files::INCLUDE_PUB_CODE | Files::INCLUDE_LIBS | Files::INCLUDE_TEMPLATES | Files::INCLUDE_TESTS | Files::AS_DATA_SET | Files::INCLUDE_NON_CLASSES));
 }
Ejemplo n.º 22
0
 /**
  * Setup basics for all tests
  *
  * @return void
  */
 public static function setUpBeforeClass()
 {
     self::$pathToSource = \Magento\Framework\App\Utility\Files::init()->getPathToSource();
     self::$reportDir = self::$pathToSource . '/tests/static/report';
     self::$magentoDir = (require __DIR__ . '/../../../../../etc/magento_path.php');
     if (!is_dir(self::$reportDir)) {
         mkdir(self::$reportDir, 0777);
     }
     self::setupFileLists();
 }
Ejemplo n.º 23
0
 public function testSchemaUsingPartialXml($expectedErrors = null)
 {
     $xmlFile = $this->_getKnownValidPartialXml();
     if ($xmlFile === null) {
         $this->markTestSkipped('No Partial File');
         return;
     }
     $schema = \Magento\Framework\App\Utility\Files::init()->getPathToSource() . $this->_getXsd();
     $this->_validateFileExpectFailure($xmlFile, $schema, $expectedErrors);
 }
Ejemplo n.º 24
0
 public function testWords()
 {
     $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
     $invoker(function ($file) {
         $words = self::$_wordsFinder->findWords(realpath($file));
         if ($words) {
             $this->fail("Found words: '" . implode("', '", $words) . "' in '{$file}' file");
         }
     }, \Magento\Framework\App\Utility\Files::init()->getAllFiles());
 }
Ejemplo n.º 25
0
 public function testGetChildHtml()
 {
     $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
     $invoker(function ($file) {
         $result = \Magento\Framework\App\Utility\Classes::getAllMatches(file_get_contents($file), "/(->getChildHtml\\([^,()]+, ?[^,()]+,)/i");
         $this->assertEmpty($result, "3rd parameter is not needed anymore for getChildHtml() in '{$file}': " . print_r($result, true));
         $result = \Magento\Framework\App\Utility\Classes::getAllMatches(file_get_contents($file), "/(->getChildChildHtml\\([^,()]+, ?[^,()]+, ?[^,()]+,)/i");
         $this->assertEmpty($result, "4th parameter is not needed anymore for getChildChildHtml() in '{$file}': " . print_r($result, true));
     }, \Magento\Framework\App\Utility\Files::init()->getPhpFiles());
 }
Ejemplo n.º 26
0
 public function testFileSchemaUsingInvalidXml()
 {
     $xmlFile = __DIR__ . '/_files/invalid_fieldset.xml';
     $dom = new \DOMDocument();
     $dom->loadXML(file_get_contents($xmlFile));
     $schema = \Magento\Framework\App\Utility\Files::init()->getPathToSource() . '/lib/internal/Magento/Framework/Object/etc/fieldset_file.xsd';
     $errors = \Magento\Framework\Config\Dom::validateDomDocument($dom, $schema);
     if (!$errors) {
         $this->fail('There is a problem with the schema.  A known bad XML file passed validation');
     }
 }
Ejemplo n.º 27
0
 /**
  * Get list of configuration files associated with modules
  *
  * @return array
  */
 protected function _getConfigFilesPerModule()
 {
     $configFiles = \Magento\Framework\App\Utility\Files::init()->getConfigFiles('config.xml', [], false);
     $data = [];
     foreach ($configFiles as $configFile) {
         preg_match('#/([^/]+?/[^/]+?)/etc/config\\.xml$#', $configFile, $moduleName);
         $moduleName = str_replace('/', '_', $moduleName[1]);
         $data[$configFile] = $moduleName;
     }
     return $data;
 }
 /**
  * Return refactored files
  *
  * @return array
  */
 public function modulesFilesDataProvider()
 {
     $filesList = [];
     foreach ($this->getFilesData('whitelist/refactored_modules*') as $refactoredFolder) {
         $files = \Magento\Framework\App\Utility\Files::init()->getFiles([$this->appPath . $refactoredFolder], '*.php');
         $filesList = array_merge($filesList, $files);
     }
     $result = array_map('realpath', $filesList);
     $result = array_diff($result, $this->filesBlackList);
     return \Magento\Framework\App\Utility\Files::composeDataSets($result);
 }
    /**
     * Build circular dependencies between modules report
     *
     * @param string $outputPath
     * @return void
     */
    protected function buildReport($outputPath)
    {
        $filesForParse = Files::init()->getComposerFiles('code', false);

        ServiceLocator::getDependenciesReportBuilder()->build(
            [
                'parse' => ['files_for_parse' => $filesForParse],
                'write' => ['report_filename' => $outputPath],
            ]
        );
    }
Ejemplo n.º 30
0
 /**
  * Get paths basing on the file with patterns
  *
  * @param string $patternsFile
  * @return array
  */
 private function getPaths($patternsFile)
 {
     $result = [];
     $files = Files::init()->readLists($patternsFile);
     foreach ($files as $file) {
         if (is_dir($file)) {
             $result[] = rtrim($file, '/');
         }
     }
     return $result;
 }