comparison core/modules/jsonapi/tests/src/Functional/TestCoverageTest.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
1 <?php
2
3 namespace Drupal\Tests\jsonapi\Functional;
4
5 use Drupal\Core\Entity\EntityTypeInterface;
6 use Drupal\Tests\BrowserTestBase;
7
8 /**
9 * Checks that all core content/config entity types have JSON:API test coverage.
10 *
11 * @group jsonapi
12 */
13 class TestCoverageTest extends BrowserTestBase {
14
15 /**
16 * Entity definitions array.
17 *
18 * @var array
19 */
20 protected $definitions;
21
22 /**
23 * {@inheritdoc}
24 */
25 protected function setUp() {
26 parent::setUp();
27
28 $all_modules = system_rebuild_module_data();
29 $stable_core_modules = array_filter($all_modules, function ($module) {
30 // Filter out contrib, hidden, testing, and experimental modules. We also
31 // don't need to enable modules that are already enabled.
32 return $module->origin === 'core'
33 && empty($module->info['hidden'])
34 && $module->status == FALSE
35 && $module->info['package'] !== 'Testing'
36 && $module->info['package'] !== 'Core (Experimental)';
37 });
38
39 $this->container->get('module_installer')->install(array_keys($stable_core_modules));
40 $this->rebuildContainer();
41
42 $this->definitions = $this->container->get('entity_type.manager')->getDefinitions();
43
44 // Entity types marked as "internal" are not exposed by JSON:API and hence
45 // also don't need test coverage.
46 $this->definitions = array_filter($this->definitions, function (EntityTypeInterface $entity_type) {
47 return !$entity_type->isInternal();
48 });
49 }
50
51 /**
52 * Tests that all core entity types have JSON:API test coverage.
53 */
54 public function testEntityTypeRestTestCoverage() {
55 $problems = [];
56 foreach ($this->definitions as $entity_type_id => $info) {
57 $class_name_full = $info->getClass();
58 $parts = explode('\\', $class_name_full);
59 $class_name = end($parts);
60 $module_name = $parts[1];
61
62 $possible_paths = [
63 'Drupal\Tests\jsonapi\Functional\CLASSTest',
64 '\Drupal\Tests\\' . $module_name . '\Functional\Jsonapi\CLASSTest',
65 ];
66 foreach ($possible_paths as $path) {
67 $missing_tests = [];
68 $class = str_replace('CLASS', $class_name, $path);
69 if (class_exists($class)) {
70 continue 2;
71 }
72 $missing_tests[] = $class;
73 }
74 if (!empty($missing_tests)) {
75 $missing_tests_list = implode(', ', $missing_tests);
76 $problems[] = "$entity_type_id: $class_name ($class_name_full) (expected tests: $missing_tests_list)";
77 }
78 }
79
80 $all = count($this->definitions);
81 $good = $all - count($problems);
82 $this->assertSame([], $problems, $this->getLlamaMessage($good, $all));
83 }
84
85 /**
86 * Message from Llama.
87 *
88 * @param int $g
89 * A count of entities with test coverage.
90 * @param int $a
91 * A count of all entities.
92 *
93 * @return string
94 * An information about progress of REST test coverage.
95 */
96 protected function getLlamaMessage($g, $a) {
97 return "
98
99 _________________________
100 / Hi! \\
101 | It's llame to not have |
102 | complete JSON:API tests! |
103 | |
104 | Progress: $g/$a. |
105 | _________________________/
106 |/
107 // o
108 l'>
109 ll
110 llama
111 || ||
112 '' ''
113 ";
114 }
115
116 }