3 namespace Drupal\Tests\rest\Functional\EntityResource;
5 use Drupal\Core\Entity\EntityTypeInterface;
6 use Drupal\Tests\BrowserTestBase;
9 * Checks that all core content/config entity types have REST test coverage.
11 * Every entity type must have test coverage for:
12 * - every format in core (json + xml + hal_json)
13 * - every authentication provider in core (anon, cookie, basic_auth)
17 class EntityResourceRestTestCoverageTest extends BrowserTestBase {
20 * Entity definitions array.
24 protected $definitions;
29 protected function setUp() {
32 $all_modules = system_rebuild_module_data();
33 $stable_core_modules = array_filter($all_modules, function ($module) {
34 // Filter out contrib, hidden, testing, and experimental modules. We also
35 // don't need to enable modules that are already enabled.
37 $module->origin === 'core' &&
38 empty($module->info['hidden']) &&
39 $module->status == FALSE &&
40 $module->info['package'] !== 'Testing' &&
41 $module->info['package'] !== 'Core (Experimental)';
44 $this->container->get('module_installer')->install(array_keys($stable_core_modules));
45 $this->rebuildContainer();
47 $this->definitions = $this->container->get('entity_type.manager')->getDefinitions();
49 // Entity types marked as "internal" are not exposed by the entity REST
50 // resource plugin and hence also don't need test coverage.
51 $this->definitions = array_filter($this->definitions, function (EntityTypeInterface $entity_type) {
52 return !$entity_type->isInternal();
57 * Tests that all core content/config entity types have REST test coverage.
59 public function testEntityTypeRestTestCoverage() {
60 $default_test_locations = [
61 // Test coverage for formats provided by the 'serialization' module.
64 '\Drupal\Tests\rest\Functional\EntityResource\CLASS\CLASS',
75 // Test coverage for formats provided by the 'hal' module.
78 '\Drupal\Tests\hal\Functional\EntityResource\CLASS\CLASS',
82 'HalJsonBasicAuthTest',
89 foreach ($this->definitions as $entity_type_id => $info) {
90 $class_name_full = $info->getClass();
91 $parts = explode('\\', $class_name_full);
92 $class_name = end($parts);
93 $module_name = $parts[1];
95 // The test class can live either in the REST/HAL module, or in the module
96 // providing the entity type.
97 $tests = $default_test_locations;
98 $tests['serialization']['possible paths'][] = '\Drupal\Tests\\' . $module_name . '\Functional\Rest\CLASS';
99 $tests['hal']['possible paths'][] = '\Drupal\Tests\\' . $module_name . '\Functional\Hal\CLASS';
101 foreach ($tests as $module => $info) {
102 $possible_paths = $info['possible paths'];
104 foreach ($info['class suffix'] as $postfix) {
105 foreach ($possible_paths as $path) {
106 $class = str_replace('CLASS', $class_name, $path . $postfix);
107 if (class_exists($class)) {
111 $missing_tests[] = $postfix;
113 if (!empty($missing_tests)) {
114 $missing_tests_list = implode(', ', array_map(function ($missing_test) use ($class_name) {
115 return $class_name . $missing_test;
117 $which_normalization = $module === 'serialization' ? 'default' : $module;
118 $problems[] = "$entity_type_id: $class_name ($class_name_full), $which_normalization normalization (expected tests: $missing_tests_list)";
122 $all = count($this->definitions);
123 $good = $all - count($problems);
124 $this->assertSame([], $problems, $this->getLlamaMessage($good, $all));
128 * Message from Llama.
131 * A count of entities with test coverage.
133 * A count of all entities.
136 * An information about progress of REST test coverage.
138 protected function getLlamaMessage($g, $a) {
141 ________________________
143 | It's llame to not have |
144 | complete REST tests! |
147 | ________________________/