/web/core/tests/Drupal/Tests/Core/Extension/ExtensionDiscoveryTest.php

https://gitlab.com/mohamed_hussein/prodt · PHP · 195 lines · 125 code · 22 blank · 48 comment · 3 complexity · ad41eece45f3802b27bdb751d4f0d7e0 MD5 · raw file

  1. <?php
  2. namespace Drupal\Tests\Core\Extension;
  3. use Drupal\Component\FileCache\FileCacheFactory;
  4. use Drupal\Core\Extension\Extension;
  5. use Drupal\Core\Extension\ExtensionDiscovery;
  6. use Drupal\Tests\UnitTestCase;
  7. use org\bovigo\vfs\vfsStream;
  8. use Symfony\Component\Yaml\Yaml;
  9. /**
  10. * Tests discovery of extensions.
  11. *
  12. * @coversDefaultClass \Drupal\Core\Extension\ExtensionDiscovery
  13. * @group Extension
  14. */
  15. class ExtensionDiscoveryTest extends UnitTestCase {
  16. /**
  17. * Tests extension discovery in a virtual filesystem with vfsStream.
  18. *
  19. * @covers ::scan
  20. */
  21. public function testExtensionDiscoveryVfs() {
  22. // Set up the file system.
  23. $filesystem = [];
  24. $files_by_type_and_name_expected = $this->populateFilesystemStructure($filesystem);
  25. $vfs = vfsStream::setup('root', NULL, $filesystem);
  26. $root = $vfs->url();
  27. $this->assertFileExists($root . '/core/modules/system/system.module');
  28. $this->assertFileExists($root . '/core/modules/system/system.info.yml');
  29. // Create an ExtensionDiscovery with $root.
  30. $extension_discovery = new ExtensionDiscovery($root, FALSE, NULL, 'sites/default');
  31. /** @var \Drupal\Core\Extension\Extension[][] $extensions_by_type */
  32. $extensions_by_type = [];
  33. $files_by_type_and_name = [];
  34. foreach (['profile', 'module', 'theme', 'theme_engine'] as $type) {
  35. $extensions_by_type[$type] = $extension_discovery->scan($type, FALSE);
  36. foreach ($extensions_by_type[$type] as $name => $extension) {
  37. $files_by_type_and_name[$type][$name] = $extension->getPathname();
  38. }
  39. if ($type === 'profile') {
  40. // Set profile directories for discovery of the other extension types.
  41. $extension_discovery->setProfileDirectories(['my_profile' => 'profiles/my_profile']);
  42. }
  43. }
  44. $this->assertEquals($files_by_type_and_name_expected, $files_by_type_and_name);
  45. $extension_expected = new Extension($root, 'module', 'core/modules/system/system.info.yml', 'system.module');
  46. $extension_expected->subpath = 'modules/system';
  47. $extension_expected->origin = 'core';
  48. $this->assertEquals($extension_expected, $extensions_by_type['module']['system'], 'system');
  49. $extension_expected = new Extension($root, 'theme_engine', 'core/themes/engines/twig/twig.info.yml', 'twig.engine');
  50. $extension_expected->subpath = 'themes/engines/twig';
  51. $extension_expected->origin = 'core';
  52. $this->assertEquals($extension_expected, $extensions_by_type['theme_engine']['twig'], 'twig');
  53. }
  54. /**
  55. * Tests changing extension discovery file cache objects to arrays.
  56. *
  57. * @covers ::scan
  58. * @runInSeparateProcess
  59. */
  60. public function testExtensionDiscoveryCache() {
  61. // Set up an extension object in the cache to mimic site prior to changing
  62. // \Drupal\Core\Extension\ExtensionDiscovery::scanDirectory() to cache an
  63. // array instead of an object. Note we cannot use the VFS file system
  64. // because FileCache does not support stream wrappers.
  65. $extension = new Extension($this->root, 'module', 'core/modules/user/user.info.yml', 'user.module');
  66. $extension->subpath = 'modules/user';
  67. $extension->origin = 'core';
  68. // Undo \Drupal\Tests\UnitTestCase::setUp() so FileCache works.
  69. FileCacheFactory::setConfiguration([]);
  70. $file_cache = FileCacheFactory::get('extension_discovery');
  71. $file_cache->set($this->root . '/core/modules/user/user.info.yml', $extension);
  72. // Create an ExtensionDiscovery object to test.
  73. $extension_discovery = new ExtensionDiscovery($this->root, TRUE, [], 'sites/default');
  74. $modules = $extension_discovery->scan('module', FALSE);
  75. $this->assertArrayHasKey('user', $modules);
  76. $this->assertEquals((array) $extension, (array) $modules['user']);
  77. $this->assertNotSame($extension, $modules['user']);
  78. // FileCache item should now be an array.
  79. $this->assertSame([
  80. 'type' => 'module',
  81. 'pathname' => 'core/modules/user/user.info.yml',
  82. 'filename' => 'user.module',
  83. 'subpath' => 'modules/user',
  84. ], $file_cache->get($this->root . '/core/modules/user/user.info.yml'));
  85. }
  86. /**
  87. * Adds example files to the filesystem structure.
  88. *
  89. * @param array $filesystem_structure
  90. * An associative array where each key represents a directory.
  91. *
  92. * @return string[][]
  93. * Format: $[$type][$name] = $yml_file
  94. * E.g. $['module']['system'] = 'system.info.yml'
  95. */
  96. protected function populateFilesystemStructure(array &$filesystem_structure) {
  97. $info_by_file = [
  98. 'core/profiles/standard/standard.info.yml' => [
  99. 'type' => 'profile',
  100. ],
  101. 'core/profiles/minimal/minimal.info.yml' => [
  102. 'type' => 'profile',
  103. ],
  104. // Override the core instance of the 'minimal' profile.
  105. 'sites/default/profiles/minimal/minimal.info.yml' => [
  106. 'type' => 'profile',
  107. ],
  108. 'profiles/my_profile/my_profile.info.yml' => [
  109. 'type' => 'profile',
  110. ],
  111. 'profiles/my_profile/modules/my_profile_nested_module/my_profile_nested_module.info.yml' => [],
  112. 'profiles/other_profile/other_profile.info.yml' => [
  113. 'type' => 'profile',
  114. ],
  115. 'core/modules/user/user.info.yml' => [],
  116. 'profiles/other_profile/modules/other_profile_nested_module/other_profile_nested_module.info.yml' => [],
  117. 'core/modules/system/system.info.yml' => [],
  118. 'core/themes/seven/seven.info.yml' => [
  119. 'type' => 'theme',
  120. ],
  121. // Override the core instance of the 'seven' theme.
  122. 'sites/default/themes/seven/seven.info.yml' => [
  123. 'type' => 'theme',
  124. ],
  125. 'modules/devel/devel.info.yml' => [],
  126. 'modules/poorly_placed_theme/poorly_placed_theme.info.yml' => [
  127. 'type' => 'theme',
  128. ],
  129. 'core/themes/engines/twig/twig.info.yml' => [
  130. 'type' => 'theme_engine',
  131. ],
  132. ];
  133. $files_by_type_and_name_expected = [];
  134. $content_by_file = [];
  135. foreach ($info_by_file as $file => $info) {
  136. $name = basename($file, '.info.yml');
  137. $info += [
  138. 'type' => 'module',
  139. 'name' => "Name of ($name)",
  140. 'core' => '8.x',
  141. ];
  142. $type = $info['type'];
  143. $content_by_file[$file] = Yaml::dump($info);
  144. $files_by_type_and_name_expected[$type][$name] = $file;
  145. }
  146. $content_by_file['core/modules/system/system.module'] = '<?php';
  147. $content_by_file['core/themes/engines/twig/twig.engine'] = '<?php';
  148. foreach ($content_by_file as $file => $content) {
  149. $pieces = explode('/', $file);
  150. $this->addFileToFilesystemStructure($filesystem_structure, $pieces, $content);
  151. }
  152. unset($files_by_type_and_name_expected['module']['other_profile_nested_module']);
  153. return $files_by_type_and_name_expected;
  154. }
  155. /**
  156. * @param array $filesystem_structure
  157. * An associative array where each key represents a directory.
  158. * @param string[] $pieces
  159. * Fragments of the file path.
  160. * @param string $content
  161. * The contents of the file.
  162. */
  163. protected function addFileToFilesystemStructure(array &$filesystem_structure, array $pieces, $content) {
  164. $piece = array_shift($pieces);
  165. if ($pieces !== []) {
  166. $filesystem_structure += [$piece => []];
  167. $this->addFileToFilesystemStructure($filesystem_structure[$piece], $pieces, $content);
  168. }
  169. else {
  170. $filesystem_structure[$piece] = $content;
  171. }
  172. }
  173. }