More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / modules / rest / tests / src / Functional / EntityResource / EntityResourceRestTestCoverageTest.php
1 <?php
2
3 namespace Drupal\Tests\rest\Functional\EntityResource;
4
5 use Drupal\Core\Entity\EntityTypeInterface;
6 use Drupal\Tests\BrowserTestBase;
7
8 /**
9  * Checks that all core content/config entity types have REST test coverage.
10  *
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)
14  *
15  * @group rest
16  */
17 class EntityResourceRestTestCoverageTest extends BrowserTestBase {
18
19   /**
20    * Entity definitions array.
21    *
22    * @var array
23    */
24   protected $definitions;
25
26   /**
27    * {@inheritdoc}
28    */
29   protected function setUp() {
30     parent::setUp();
31
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.
36       return
37         $module->origin === 'core' &&
38         empty($module->info['hidden']) &&
39         $module->status == FALSE &&
40         $module->info['package'] !== 'Testing' &&
41         $module->info['package'] !== 'Core (Experimental)';
42     });
43
44     $this->container->get('module_installer')->install(array_keys($stable_core_modules));
45     $this->rebuildContainer();
46
47     $this->definitions = $this->container->get('entity_type.manager')->getDefinitions();
48
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();
53     });
54   }
55
56   /**
57    * Tests that all core content/config entity types have REST test coverage.
58    */
59   public function testEntityTypeRestTestCoverage() {
60     $default_test_locations = [
61       // Test coverage for formats provided by the 'serialization' module.
62       'serialization' => [
63         'possible paths' => [
64           '\Drupal\Tests\rest\Functional\EntityResource\CLASS\CLASS',
65         ],
66         'class suffix' => [
67           'JsonAnonTest',
68           'JsonBasicAuthTest',
69           'JsonCookieTest',
70           'XmlAnonTest',
71           'XmlBasicAuthTest',
72           'XmlCookieTest',
73         ],
74       ],
75       // Test coverage for formats provided by the 'hal' module.
76       'hal' => [
77         'possible paths' => [
78           '\Drupal\Tests\hal\Functional\EntityResource\CLASS\CLASS',
79         ],
80         'class suffix' => [
81           'HalJsonAnonTest',
82           'HalJsonBasicAuthTest',
83           'HalJsonCookieTest',
84         ],
85       ],
86     ];
87
88     $problems = [];
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];
94
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';
100
101       foreach ($tests as $module => $info) {
102         $possible_paths = $info['possible paths'];
103         $missing_tests = [];
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)) {
108               continue 2;
109             }
110           }
111           $missing_tests[] = $postfix;
112         }
113         if (!empty($missing_tests)) {
114           $missing_tests_list = implode(', ', array_map(function ($missing_test) use ($class_name) {
115             return $class_name . $missing_test;
116           }, $missing_tests));
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)";
119         }
120       }
121     }
122     $all = count($this->definitions);
123     $good = $all - count($problems);
124     $this->assertSame([], $problems, $this->getLlamaMessage($good, $all));
125   }
126
127   /**
128    * Message from Llama.
129    *
130    * @param int $g
131    *   A count of entities with test coverage.
132    * @param int $a
133    *   A count of all entities.
134    *
135    * @return string
136    *   An information about progress of REST test coverage.
137    */
138   protected function getLlamaMessage($g, $a) {
139     return "
140
141       ________________________
142      /           Hi!          \\
143     |  It's llame to not have  |
144     |   complete REST tests!   |
145     |                          |
146     |     Progress: $g/$a.     |
147     | ________________________/
148     |/
149 //  o
150 l'>
151 ll
152 llama
153 || ||
154 '' ''
155 ";
156   }
157
158 }