From 78b3c6d0421c66b317ce11393ed6e934d60aa2c5 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 14 Sep 2021 17:11:01 -0700 Subject: [PATCH] ExampleDataLoader - Fix loading data-sets from `tests/phpunit/*` from web-env Before: If you add example-data under `tests/phpunit/`, then it works in headless env. In web-env, it will be scanned, but the `include $file` step will fail. After: The `include $file` step will succeed. --- Civi/Test/ExampleDataLoader.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Civi/Test/ExampleDataLoader.php b/Civi/Test/ExampleDataLoader.php index 68a0443bc7..8b78d82353 100644 --- a/Civi/Test/ExampleDataLoader.php +++ b/Civi/Test/ExampleDataLoader.php @@ -112,17 +112,24 @@ class ExampleDataLoader { * @param $classDelim * Namespace separator, eg underscore or backslash. * @return array - * Array(string $relativeFileName => string $className). + * Array(string $includeFile => string $className). */ private function scanExampleClasses($classRoot, $classDir, $classDelim): array { + $civiRoot = \Civi::paths()->getPath('[civicrm.root]/'); + $classRoot = \CRM_Utils_File::addTrailingSlash($classRoot, '/'); + // Prefer include-paths relative to civiRoot - eg make tests/phpunit/* loadable at runtime. + $includeRoot = \CRM_Utils_File::isChildPath($civiRoot, $classRoot) ? $civiRoot : $classRoot; + $r = []; $exDirs = (array) glob($classRoot . $classDir); foreach ($exDirs as $exDir) { foreach (\CRM_Utils_File::findFiles($exDir, '*.ex.php') as $file) { $file = str_replace(DIRECTORY_SEPARATOR, '/', $file); - $file = \CRM_Utils_File::relativize($file, \CRM_Utils_File::addTrailingSlash($classRoot, '/')); - $class = str_replace('/', $classDelim, preg_replace('/\.ex\.php$/', '', $file)); - $r[$file] = $class; + $includeFile = \CRM_Utils_File::relativize($file, $includeRoot); + $classFile = \CRM_Utils_File::relativize($file, $classRoot); + $class = str_replace('/', $classDelim, preg_replace('/\.ex\.php$/', '', + $classFile)); + $r[$includeFile] = $class; } } return $r; -- 2.25.1