StableTemplateOverrideTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Drupal\KernelTests\Core\Theme;
  3. use Drupal\Core\Theme\Registry;
  4. use Drupal\KernelTests\KernelTestBase;
  5. /**
  6. * Tests Stable's template overrides.
  7. *
  8. * @group Theme
  9. */
  10. class StableTemplateOverrideTest extends KernelTestBase {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public static $modules = ['system', 'user'];
  15. /**
  16. * An array of template names to skip, without the extension.
  17. *
  18. * @var string[]
  19. */
  20. protected $templatesToSkip = [
  21. 'views-form-views-form',
  22. ];
  23. /**
  24. * The theme handler.
  25. *
  26. * @var \Drupal\Core\Extension\ThemeHandlerInterface
  27. */
  28. protected $themeHandler;
  29. /**
  30. * A list of all core modules.
  31. *
  32. * @var string[]
  33. */
  34. protected $allModules;
  35. /**
  36. * {@inheritdoc}
  37. */
  38. protected function setUp() {
  39. parent::setUp();
  40. $this->themeHandler = $this->container->get('theme_handler');
  41. $this->container->get('theme_installer')->install(['stable']);
  42. $this->installAllModules();
  43. }
  44. /**
  45. * Installs all core modules.
  46. */
  47. protected function installAllModules() {
  48. // Enable all core modules.
  49. $all_modules = $this->container->get('extension.list.module')->getList();
  50. $all_modules = array_filter($all_modules, function ($module) {
  51. // Filter contrib, hidden, experimental, already enabled modules, and
  52. // modules in the Testing package.
  53. if ($module->origin !== 'core' || !empty($module->info['hidden']) || $module->status == TRUE || $module->info['package'] == 'Testing' || $module->info['package'] == 'Core (Experimental)') {
  54. return FALSE;
  55. }
  56. return TRUE;
  57. });
  58. $this->allModules = array_keys($all_modules);
  59. sort($this->allModules);
  60. $module_installer = $this->container->get('module_installer');
  61. $module_installer->install($this->allModules);
  62. $this->installConfig(['system', 'user']);
  63. }
  64. /**
  65. * Ensures that Stable overrides all relevant core templates.
  66. */
  67. public function testStableTemplateOverrides() {
  68. $registry = new Registry($this->root, \Drupal::cache(), \Drupal::lock(), \Drupal::moduleHandler(), $this->themeHandler, \Drupal::service('theme.initialization'), 'stable');
  69. $registry->setThemeManager(\Drupal::theme());
  70. $registry_full = $registry->get();
  71. foreach ($registry_full as $hook => $info) {
  72. if (isset($info['template'])) {
  73. // Allow skipping templates.
  74. if (in_array($info['template'], $this->templatesToSkip)) {
  75. continue;
  76. }
  77. $this->assertEquals('core/themes/stable', $info['theme path'], $info['template'] . '.html.twig overridden in Stable.');
  78. }
  79. }
  80. }
  81. }