diff --git a/core/lib/Drupal/Component/Utility/NestedArray.php b/core/lib/Drupal/Component/Utility/NestedArray.php index 74e631c..995211f 100644 --- a/core/lib/Drupal/Component/Utility/NestedArray.php +++ b/core/lib/Drupal/Component/Utility/NestedArray.php @@ -349,4 +349,26 @@ 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/src/TestDiscovery.php b/core/modules/simpletest/src/TestDiscovery.php index d0a9205..233f05b 100644 --- a/core/modules/simpletest/src/TestDiscovery.php +++ b/core/modules/simpletest/src/TestDiscovery.php @@ -10,6 +10,7 @@ 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; @@ -136,8 +137,8 @@ public function registerTestNamespaces() { * * @param string $extension * (optional) The name of an extension to limit discovery to; e.g., 'node'. - * @param string[] $exclude_groups - * (optional) An array of excluded groups. + * @param string[] $exclude_types + * (optional) An array of excluded test types. * * @return array * An array of tests keyed by the first @group specified in each test's @@ -158,7 +159,7 @@ public function registerTestNamespaces() { * @todo Remove singular grouping; retain list of groups in 'group' key. * @see https://www.drupal.org/node/2296615 */ - public function getTestClasses($extension = NULL, array $exclude_groups = []) { + public function getTestClasses($extension = NULL, array $exclude_types = []) { $reader = new SimpleAnnotationReader(); $reader->addNamespace('Drupal\\simpletest\\Annotation'); @@ -221,8 +222,10 @@ public function getTestClasses($extension = NULL, array $exclude_groups = []) { } } - if ($exclude_groups) { - $list = array_diff_key($list, array_flip($exclude_groups)); + 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; diff --git a/core/modules/simpletest/tests/src/Unit/TestInfoParsingTest.php b/core/modules/simpletest/tests/src/Unit/TestInfoParsingTest.php index 1ff36c7..e7f9f9c 100644 --- a/core/modules/simpletest/tests/src/Unit/TestInfoParsingTest.php +++ b/core/modules/simpletest/tests/src/Unit/TestInfoParsingTest.php @@ -281,7 +281,9 @@ class FunctionalExampleTest {} 'Functional' => [ 'FunctionalExampleTest.php' => $test_file, 'FunctionalExampleTest2.php' => str_replace(['FunctionalExampleTest', '@group example'], ['FunctionalExampleTest2', '@group example2'], $test_file), - 'FunctionalExampleTest3.php' => str_replace(['FunctionalExampleTest', '@group example'], ['FunctionalExampleTest3', '@group example2'], $test_file), + ], + 'Kernel' => [ + 'KernelExampleTest3.php' => str_replace(['FunctionalExampleTest', '@group example'], ['KernelExampleTest3', '@group example2'], $test_file), ], ], ], @@ -312,6 +314,7 @@ public function testGetTestClasses() { 'name' => 'Drupal\Tests\test_module\Functional\FunctionalExampleTest', 'description' => 'Test description', 'group' => 'example', + 'type' => 'PHPUnit-Functional' ], ], 'example2' => [ @@ -319,11 +322,13 @@ public function testGetTestClasses() { 'name' => 'Drupal\Tests\test_module\Functional\FunctionalExampleTest2', 'description' => 'Test description', 'group' => 'example2', + 'type' => 'PHPUnit-Functional' ], - 'Drupal\Tests\test_module\Functional\FunctionalExampleTest3' => [ - 'name' => 'Drupal\Tests\test_module\Functional\FunctionalExampleTest3', + 'Drupal\Tests\test_module\Kernel\KernelExampleTest3' => [ + 'name' => 'Drupal\Tests\test_module\Kernel\KernelExampleTest3', 'description' => 'Test description', 'group' => 'example2', + 'type' => 'PHPUnit-Kernel' ], ], ], $result); @@ -332,7 +337,7 @@ public function testGetTestClasses() { /** * @covers ::getTestClasses */ - public function testGetTestClassesWithExcludedGroups() { + public function testGetTestClassesWithExcludedTypes() { $this->setupVfsWithTestClasses(); $class_loader = $this->prophesize(ClassLoader::class); $module_handler = $this->prophesize(ModuleHandlerInterface::class); @@ -343,14 +348,23 @@ public function testGetTestClassesWithExcludedGroups() { 'test_module' => new Extension('vfs://drupal', 'module', 'modules/test_module/test_module.info.yml'), ]; $test_discovery->setExtensions($extensions); - $result = $test_discovery->getTestClasses(NULL, ['example2']); - $this->assertCount(1, $result); + $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); diff --git a/core/scripts/run-tests.sh b/core/scripts/run-tests.sh index 7b93eb6..926d254 100755 --- a/core/scripts/run-tests.sh +++ b/core/scripts/run-tests.sh @@ -208,9 +208,9 @@ function simpletest_script_help() { Specify the path and the extension (i.e. 'core/modules/user/user.test'). - --exclude-groups + --exclude-types - Runs all tests, but excluding a certain group. Together with --all, + 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. @@ -297,7 +297,7 @@ function simpletest_script_parse_args() { 'module' => NULL, 'class' => FALSE, 'file' => FALSE, - 'exclude-groups' => [], + 'exclude-types' => [], 'directory' => NULL, 'color' => FALSE, 'verbose' => FALSE, @@ -904,7 +904,7 @@ function simpletest_script_get_test_list() { $test_list = array(); if ($args['all'] || $args['module']) { try { - $groups = simpletest_test_get_all($args['module'], $args['exclude-groups']); + $groups = simpletest_test_get_all($args['module'], $args['exclude-types']); } 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 a120e2a..7964298 100644 --- a/core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/NestedArrayTest.php @@ -259,4 +259,30 @@ 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; + } + }