Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / tests / TestSuites / TestSuiteBase.php
1 <?php
2
3 namespace Drupal\Tests\TestSuites;
4
5 use Drupal\simpletest\TestDiscovery;
6 use PHPUnit\Framework\TestSuite;
7
8 /**
9  * Base class for Drupal test suites.
10  */
11 abstract class TestSuiteBase extends TestSuite {
12
13   /**
14    * Finds extensions in a Drupal installation.
15    *
16    * An extension is defined as a directory with an *.info.yml file in it.
17    *
18    * @param string $root
19    *   Path to the root of the Drupal installation.
20    *
21    * @return string[]
22    *   Associative array of extension paths, with extension name as keys.
23    */
24   protected function findExtensionDirectories($root) {
25     $extension_roots = \drupal_phpunit_contrib_extension_directory_roots($root);
26
27     $extension_directories = array_map('drupal_phpunit_find_extension_directories', $extension_roots);
28     return array_reduce($extension_directories, 'array_merge', []);
29   }
30
31   /**
32    * Find and add tests to the suite for core and any extensions.
33    *
34    * @param string $root
35    *   Path to the root of the Drupal installation.
36    * @param string $suite_namespace
37    *   SubNamespace used to separate test suite. Examples: Unit, Functional.
38    */
39   protected function addTestsBySuiteNamespace($root, $suite_namespace) {
40     // Core's tests are in the namespace Drupal\${suite_namespace}Tests\ and are
41     // always inside of core/tests/Drupal/${suite_namespace}Tests. The exception
42     // to this is Unit tests for historical reasons.
43     if ($suite_namespace == 'Unit') {
44       $tests = TestDiscovery::scanDirectory("Drupal\\Tests\\", "$root/core/tests/Drupal/Tests");
45       $tests = array_flip(array_filter(array_flip($tests), function ($test_class) {
46         // The Listeners directory does not contain tests. Use the class name
47         // to be compatible with all operating systems.
48         return !preg_match('/^Drupal\\\\Tests\\\\Listeners\\\\/', $test_class);
49       }));
50       $this->addTestFiles($tests);
51     }
52     else {
53       $this->addTestFiles(TestDiscovery::scanDirectory("Drupal\\${suite_namespace}Tests\\", "$root/core/tests/Drupal/${suite_namespace}Tests"));
54     }
55
56     // Extensions' tests will always be in the namespace
57     // Drupal\Tests\$extension_name\$suite_namespace\ and be in the
58     // $extension_path/tests/src/$suite_namespace directory. Not all extensions
59     // will have all kinds of tests.
60     foreach ($this->findExtensionDirectories($root) as $extension_name => $dir) {
61       $test_path = "$dir/tests/src/$suite_namespace";
62       if (is_dir($test_path)) {
63         $this->addTestFiles(TestDiscovery::scanDirectory("Drupal\\Tests\\$extension_name\\$suite_namespace\\", $test_path));
64       }
65     }
66   }
67
68 }