diff --git a/core/lib/Drupal/Component/Utility/NestedArray.php b/core/lib/Drupal/Component/Utility/NestedArray.php index 995211f..74e631c 100644 --- a/core/lib/Drupal/Component/Utility/NestedArray.php +++ b/core/lib/Drupal/Component/Utility/NestedArray.php @@ -349,26 +349,4 @@ public static function mergeDeepArray(array $arrays, $preserve_integer_keys = FA return $result; } - /** - * Filters a nested array recursively. - * - * @param array $array - * The filtered nested array - * @param callable|NULL $callable - * The callable to apply for filtering. - * - * @return array - * The filtered array. - */ - public static function filter(array $array, callable $callable = NULL) { - $array = is_callable($callable) ? array_filter($array, $callable) : array_filter($array); - foreach ($array as &$element) { - if (is_array($element)) { - $element = static::filter($element, $callable); - } - } - - return $array; - } - } diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module index 34b9596..74d348e 100644 --- a/core/modules/simpletest/simpletest.module +++ b/core/modules/simpletest/simpletest.module @@ -489,12 +489,9 @@ function simpletest_log_read($test_id, $database_prefix, $test_class) { * each module for files matching the PSR-0 standard. Once loaded the test list * is cached and stored in a static variable. * - * @param string $extension - * (optional) The name of an extension to limit discovery to; e.g., 'node'. - * @param string[] $types - * An array of included test types. - * @param string[] $exclude_types - * (optional) An array of excluded test types. + * @param string $module + * Name of a module. If set then only tests belonging to this module are + * returned. * * @return array[] * An array of tests keyed with the groups, and then keyed by test classes. @@ -509,8 +506,8 @@ function simpletest_log_read($test_id, $database_prefix, $test_class) { * ); * @endcode */ -function simpletest_test_get_all($extension = NULL, array $types = [], array $exclude_types = []) { - return \Drupal::service('test_discovery')->getTestClasses($extension, $types, $exclude_types); +function simpletest_test_get_all($module = NULL) { + return \Drupal::service('test_discovery')->getTestClasses($module); } /** diff --git a/core/modules/simpletest/simpletest.services.yml b/core/modules/simpletest/simpletest.services.yml index 8b645de..56d48ca 100644 --- a/core/modules/simpletest/simpletest.services.yml +++ b/core/modules/simpletest/simpletest.services.yml @@ -1,4 +1,4 @@ services: test_discovery: class: Drupal\simpletest\TestDiscovery - arguments: ['@app.root', '@class_loader', '@module_handler', '@?cache.discovery'] + arguments: ['@class_loader', '@?cache.discovery'] diff --git a/core/modules/simpletest/src/TestDiscovery.php b/core/modules/simpletest/src/TestDiscovery.php index 056e84d..d2c49d7 100644 --- a/core/modules/simpletest/src/TestDiscovery.php +++ b/core/modules/simpletest/src/TestDiscovery.php @@ -10,11 +10,9 @@ use Doctrine\Common\Annotations\SimpleAnnotationReader; use Doctrine\Common\Reflection\StaticReflectionParser; use Drupal\Component\Annotation\Reflection\MockFileFinder; -use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\Unicode; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ExtensionDiscovery; -use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\simpletest\Exception\MissingGroupException; use PHPUnit_Util_Test; @@ -52,37 +50,17 @@ class TestDiscovery { protected $availableExtensions; /** - * The app root. - * - * @var string - */ - protected $root; - - /** - * The module handler. - * - * @var \Drupal\Core\Extension\ModuleHandlerInterface - */ - protected $moduleHandler; - - /** * Constructs a new test discovery. * - * @param string $root - * The app root. * @param $class_loader * The class loader. Normally Composer's ClassLoader, as included by the * front controller, but may also be decorated; e.g., * \Symfony\Component\ClassLoader\ApcClassLoader. - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend * (optional) Backend for caching discovery results. */ - public function __construct($root, $class_loader, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend = NULL) { - $this->root = $root; + public function __construct($class_loader, CacheBackendInterface $cache_backend = NULL) { $this->classLoader = $class_loader; - $this->moduleHandler = $module_handler; $this->cacheBackend = $cache_backend; } @@ -102,15 +80,15 @@ public function registerTestNamespaces() { $existing = $this->classLoader->getPrefixesPsr4(); // Add PHPUnit test namespaces of Drupal core. - $this->testNamespaces['Drupal\\Tests\\'] = [$this->root . '/core/tests/Drupal/Tests']; - $this->testNamespaces['Drupal\\KernelTests\\'] = [$this->root . '/core/tests/Drupal/KernelTests']; - $this->testNamespaces['Drupal\\FunctionalTests\\'] = [$this->root . '/core/tests/Drupal/FunctionalTests']; + $this->testNamespaces['Drupal\\Tests\\'] = [DRUPAL_ROOT . '/core/tests/Drupal/Tests']; + $this->testNamespaces['Drupal\\KernelTests\\'] = [DRUPAL_ROOT . '/core/tests/Drupal/KernelTests']; + $this->testNamespaces['Drupal\\FunctionalTests\\'] = [DRUPAL_ROOT . '/core/tests/Drupal/FunctionalTests']; $this->availableExtensions = array(); foreach ($this->getExtensions() as $name => $extension) { $this->availableExtensions[$extension->getType()][$name] = $name; - $base_path = $this->root . '/' . $extension->getPath(); + $base_path = DRUPAL_ROOT . '/' . $extension->getPath(); // Add namespace of disabled/uninstalled extensions. if (!isset($existing["Drupal\\$name\\"])) { @@ -137,13 +115,11 @@ public function registerTestNamespaces() { * * @param string $extension * (optional) The name of an extension to limit discovery to; e.g., 'node'. - * @param string[] $types - * An array of included test types. - * @param string[] $exclude_types - * (optional) An array of excluded test types. * - * @return array An array of tests keyed by the first - * @code + * @return array + * An array of tests keyed by the first @group specified in each test's + * PHPDoc comment block, and then keyed by class names. For example: + * @code * $groups['block'] => array( * 'Drupal\block\Tests\BlockTest' => array( * 'name' => 'Drupal\block\Tests\BlockTest', @@ -151,12 +127,15 @@ public function registerTestNamespaces() { * 'group' => 'block', * ), * ); - * @endcode + * @endcode + * + * @throws \ReflectionException + * If a discovered test class does not match the expected class name. * * @todo Remove singular grouping; retain list of groups in 'group' key. * @see https://www.drupal.org/node/2296615 */ - public function getTestClasses($extension = NULL, array $types = [], array $exclude_types = []) { + public function getTestClasses($extension = NULL) { $reader = new SimpleAnnotationReader(); $reader->addNamespace('Drupal\\simpletest\\Annotation'); @@ -211,25 +190,13 @@ public function getTestClasses($extension = NULL, array $types = [], array $excl } // Allow modules extending core tests to disable originals. - $this->moduleHandler->alter('simpletest', $list); + \Drupal::moduleHandler()->alter('simpletest', $list); if (!isset($extension)) { if ($this->cacheBackend) { $this->cacheBackend->set('simpletest:discovery:classes', $list); } } - - if ($types) { - $list = NestedArray::filter($list, function ($element) use ($types) { - return !(is_array($element) && isset($element['type']) && !in_array($element['type'], $types)); - }); - } - if ($exclude_types) { - $list = NestedArray::filter($list, function ($element) use ($exclude_types) { - return !(is_array($element) && isset($element['type']) && in_array($element['type'], $exclude_types)); - }); - } - return $list; } @@ -483,7 +450,7 @@ public static function getPhpunitTestSuite($classname) { * An array of Extension objects, keyed by extension name. */ protected function getExtensions() { - $listing = new ExtensionDiscovery($this->root); + $listing = new ExtensionDiscovery(DRUPAL_ROOT); // Ensure that tests in all profiles are discovered. $listing->setProfileDirectories(array()); $extensions = $listing->scan('module', TRUE); diff --git a/core/modules/simpletest/tests/src/Unit/TestInfoParsingTest.php b/core/modules/simpletest/tests/src/Unit/TestInfoParsingTest.php index 8746f42..509b9ab 100644 --- a/core/modules/simpletest/tests/src/Unit/TestInfoParsingTest.php +++ b/core/modules/simpletest/tests/src/Unit/TestInfoParsingTest.php @@ -7,12 +7,8 @@ namespace Drupal\Tests\simpletest\Unit; -use Composer\Autoload\ClassLoader; -use Drupal\Core\Extension\Extension; -use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\simpletest\TestDiscovery; use Drupal\Tests\UnitTestCase; -use org\bovigo\vfs\vfsStream; /** * @coversDefaultClass \Drupal\simpletest\TestDiscovery @@ -260,166 +256,6 @@ public function testTestInfoParserMissingSummary() { $this->assertEmpty($info['description']); } - protected function setupVfsWithTestClasses() { - vfsStream::setup('drupal'); - - $test_file = << [ - 'test_module' => [ - 'tests' => [ - 'src' => [ - 'Functional' => [ - 'FunctionalExampleTest.php' => $test_file, - 'FunctionalExampleTest2.php' => str_replace(['FunctionalExampleTest', '@group example'], ['FunctionalExampleTest2', '@group example2'], $test_file), - ], - 'Kernel' => [ - 'KernelExampleTest3.php' => str_replace(['FunctionalExampleTest', '@group example'], ['KernelExampleTest3', '@group example2'], $test_file), - ], - ], - ], - ], - ], - ]); - } - - /** - * @covers ::getTestClasses - */ - public function testGetTestClasses() { - $this->setupVfsWithTestClasses(); - $class_loader = $this->prophesize(ClassLoader::class); - $module_handler = $this->prophesize(ModuleHandlerInterface::class); - - $test_discovery = new TestTestDiscovery('vfs://drupal', $class_loader->reveal(), $module_handler->reveal()); - - $extensions = [ - 'test_module' => new Extension('vfs://drupal', 'module', 'modules/test_module/test_module.info.yml'), - ]; - $test_discovery->setExtensions($extensions); - $result = $test_discovery->getTestClasses(); - $this->assertCount(2, $result); - $this->assertEquals([ - 'example' => [ - 'Drupal\Tests\test_module\Functional\FunctionalExampleTest' => [ - 'name' => 'Drupal\Tests\test_module\Functional\FunctionalExampleTest', - 'description' => 'Test description', - 'group' => 'example', - 'type' => 'PHPUnit-Functional' - ], - ], - 'example2' => [ - 'Drupal\Tests\test_module\Functional\FunctionalExampleTest2' => [ - 'name' => 'Drupal\Tests\test_module\Functional\FunctionalExampleTest2', - 'description' => 'Test description', - 'group' => 'example2', - 'type' => 'PHPUnit-Functional' - ], - 'Drupal\Tests\test_module\Kernel\KernelExampleTest3' => [ - 'name' => 'Drupal\Tests\test_module\Kernel\KernelExampleTest3', - 'description' => 'Test description', - 'group' => 'example2', - 'type' => 'PHPUnit-Kernel' - ], - ], - ], $result); - } - - /** - * @covers ::getTestClasses - */ - public function testGetTestClassesWithExcludedTypes() { - $this->setupVfsWithTestClasses(); - $class_loader = $this->prophesize(ClassLoader::class); - $module_handler = $this->prophesize(ModuleHandlerInterface::class); - - $test_discovery = new TestTestDiscovery('vfs://drupal', $class_loader->reveal(), $module_handler->reveal()); - - $extensions = [ - 'test_module' => new Extension('vfs://drupal', 'module', 'modules/test_module/test_module.info.yml'), - ]; - $test_discovery->setExtensions($extensions); - $result = $test_discovery->getTestClasses(NULL, [], ['PHPUnit-Kernel']); - $this->assertCount(2, $result); - $this->assertEquals([ - 'example' => [ - 'Drupal\Tests\test_module\Functional\FunctionalExampleTest' => [ - 'name' => 'Drupal\Tests\test_module\Functional\FunctionalExampleTest', - 'description' => 'Test description', - 'group' => 'example', - 'type' => 'PHPUnit-Functional' - ], - ], - 'example2' => [ - 'Drupal\Tests\test_module\Functional\FunctionalExampleTest2' => [ - 'name' => 'Drupal\Tests\test_module\Functional\FunctionalExampleTest2', - 'description' => 'Test description', - 'group' => 'example2', - 'type' => 'PHPUnit-Functional' - ], - ], - ], $result); - } - - /** - * @covers ::getTestClasses - */ - public function testGetTestClassesWithIncludedTypes() { - $this->setupVfsWithTestClasses(); - $class_loader = $this->prophesize(ClassLoader::class); - $module_handler = $this->prophesize(ModuleHandlerInterface::class); - - $test_discovery = new TestTestDiscovery('vfs://drupal', $class_loader->reveal(), $module_handler->reveal()); - - $extensions = [ - 'test_module' => new Extension('vfs://drupal', 'module', 'modules/test_module/test_module.info.yml'), - ]; - $test_discovery->setExtensions($extensions); - $result = $test_discovery->getTestClasses(NULL, ['PHPUnit-Kernel']); - $this->assertCount(2, $result); - $this->assertEquals([ - 'example' => [ - ], - 'example2' => [ - 'Drupal\Tests\test_module\Kernel\KernelExampleTest3' => [ - 'name' => 'Drupal\Tests\test_module\Kernel\KernelExampleTest3', - 'description' => 'Test description', - 'group' => 'example2', - 'type' => 'PHPUnit-Kernel' - ], - ], - ], $result); - } - -} - -class TestTestDiscovery extends TestDiscovery { - - /** - * @var \Drupal\Core\Extension\Extension[] - */ - protected $extensions = []; - - public function setExtensions(array $extensions) { - $this->extensions = $extensions; - } - - /** - * {@inheritdoc} - */ - protected function getExtensions() { - return $this->extensions; - } - /** * @covers ::getPhpunitTestSuite * @dataProvider providerTestGetPhpunitTestSuite diff --git a/core/scripts/run-tests.sh b/core/scripts/run-tests.sh index dd46347..b9c7a01 100755 --- a/core/scripts/run-tests.sh +++ b/core/scripts/run-tests.sh @@ -208,11 +208,6 @@ function simpletest_script_help() { Specify the path and the extension (i.e. 'core/modules/user/user.test'). - --exclude-types - - Runs all tests, but excluding a certain type. Together with --all, - its for example to exclude javascript tests from being executed. - --directory Run all tests found within the specified file directory. --xml @@ -297,8 +292,6 @@ function simpletest_script_parse_args() { 'module' => NULL, 'class' => FALSE, 'file' => FALSE, - 'types' => [], - 'exclude-types' => [], 'directory' => NULL, 'color' => FALSE, 'verbose' => FALSE, @@ -327,10 +320,6 @@ function simpletest_script_parse_args() { if (is_bool($args[$previous_arg])) { $args[$matches[1]] = TRUE; } - elseif (is_array($args[$previous_arg])) { - $value = array_shift($_SERVER['argv']); - $args[$matches[1]] = array_map('trim', explode(',', $value)); - } else { $args[$matches[1]] = array_shift($_SERVER['argv']); } @@ -905,7 +894,7 @@ function simpletest_script_get_test_list() { $test_list = array(); if ($args['all'] || $args['module']) { try { - $groups = simpletest_test_get_all($args['module'], $args['types'], $args['exclude-types']); + $groups = simpletest_test_get_all($args['module']); } catch (Exception $e) { echo (string) $e; diff --git a/core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php b/core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php index 7964298..a120e2a 100644 --- a/core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php @@ -259,30 +259,4 @@ public function testMergeOutOfSequenceKeys() { $this->assertSame($expected, $actual, 'drupal_array_merge_deep() ignores numeric key order when merging.'); } - /** - * @covers ::filter - * @dataProvider providerTestFilter - */ - public function testFilter($array, $callable, $expected) { - $this->assertEquals($expected, NestedArray::filter($array, $callable)); - } - - public function providerTestFilter() { - $data = []; - $data['1d-array'] = [ - [0, 1, '', TRUE], NULL, [1 => 1, 3 => TRUE] - ]; - $data['1d-array-callable'] = [ - [0, 1, '', TRUE], function ($element) { return $element === ''; }, [2 => ''] - ]; - $data['2d-array'] = [ - [[0, 1, '', TRUE], [0, 1, 2, 3]], NULL, [0 => [1 => 1, 3 => TRUE], 1 => [1 => 1, 2 => 2, 3 => 3]], - ]; - $data['2d-array-callable'] = [ - [[0, 1, '', TRUE], [0, 1, 2, 3]], function ($element) { return is_array($element) || $element === 3; }, [0 => [], 1 => [3 => 3]], - ]; - - return $data; - } - }