Skip to content
Snippets Groups Projects

Issue #3458387: Avoid YamlValidationTest::provideYamls() returns .yml files that are not part of the Examples project

Merged Issue #3458387: Avoid YamlValidationTest::provideYamls() returns .yml files that are not part of the Examples project
@@ -6,45 +6,65 @@ use PHPUnit\Framework\TestCase;
use Symfony\Component\Yaml\Yaml;
/**
* Validate requirements for config YAML.
* Verifies the configuration files are valid.
*
* YAML in modules' config/ directory should not have a uuid: key. We'll use
* this test to check whether that's the case.
* Configuration files are considered valid if they do not contain a uuid key.
*
* @group examples
*/
class YamlValidationTest extends TestCase {
/**
* Find all the config YAML files and provide them to the test.
* Provides test data for testNoUuidsInConfig().
*
* @return array[]
* An array of arrays of strings, suitable as a data provider. Strings are
* paths to YAML files in config directories.
* @return array
* The test data.
*/
public function provideYamls() {
$yaml_paths = [];
// We cannot check all the files in all the directories contained in the
// project directory because GitLab CI could add its own directories,
// including the server document root directory.
// That is why $paths contains a list of directories to check.
// @todo Checks this needs to be changed once
// https://www.drupal.org/project/gitlab_templates/issues/3425971
// introduces changes in the setup process.
$paths = [realpath(__DIR__ . '/../../..') . '/modules'];
$yaml_files = [];
$examples_project_path = realpath(__DIR__ . '/../../..');
$paths = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($examples_project_path, \RecursiveDirectoryIterator::FOLLOW_SYMLINKS));
foreach ($paths as $path) {
$pathname = $path->getPathname();
if (strpos($pathname, '.yml') !== FALSE) {
if (strpos($pathname, '/config/') !== FALSE) {
$yaml_paths[] = [$pathname];
$directory = new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS);
$filter = new \RecursiveCallbackFilterIterator($directory, function ($current, $key, $iterator) {
if ($current->isFile()) {
// Only accept files whose extension is .yml which are contained in a
// config directory.
if ($current->getExtension() === 'yml' && strpos($current->getPathName(), '/config/') !== FALSE) {
return TRUE;
}
return FALSE;
}
// Always accept a directory.
return TRUE;
});
$iterator = new \RecursiveIteratorIterator($filter);
foreach ($iterator as $info) {
$yaml_files[] = [$info->getPathname()];
}
}
return $yaml_paths;
return $yaml_files;
}
/**
* Tests that the configuration files do not contain a uuid key.
*
* @dataProvider provideYamls
*/
public function testNoUuidsInConfig($yaml_path) {
$yaml = Yaml::parse(file_get_contents($yaml_path));
$this->assertArrayNotHasKey('uuid', $yaml, "YAML in this file contains a uuid key: $yaml_path");
public function testNoUuidsInConfig($yaml_file) {
$yaml = Yaml::parse(file_get_contents($yaml_file));
$this->assertArrayNotHasKey('uuid', $yaml, "$yaml_file contains a uuid key.");
}
}
Loading