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