ThemeSettingsTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Drupal\KernelTests\Core\Theme;
  3. use Drupal\Core\Config\InstallStorage;
  4. use Drupal\Core\Extension\ExtensionDiscovery;
  5. use Drupal\KernelTests\KernelTestBase;
  6. /**
  7. * Tests theme settings functionality.
  8. *
  9. * @group Theme
  10. */
  11. class ThemeSettingsTest extends KernelTestBase {
  12. /**
  13. * Modules to enable.
  14. *
  15. * @var array
  16. */
  17. public static $modules = ['system'];
  18. /**
  19. * List of discovered themes.
  20. *
  21. * @var array
  22. */
  23. protected $availableThemes;
  24. protected function setUp() {
  25. parent::setUp();
  26. // Theme settings rely on System module's system.theme.global configuration.
  27. $this->installConfig(['system']);
  28. if (!isset($this->availableThemes)) {
  29. $discovery = new ExtensionDiscovery($this->root);
  30. $this->availableThemes = $discovery->scan('theme');
  31. }
  32. }
  33. /**
  34. * Tests that $theme.settings are imported and used as default theme settings.
  35. */
  36. public function testDefaultConfig() {
  37. $name = 'test_basetheme';
  38. $path = $this->availableThemes[$name]->getPath();
  39. $this->assertFileExists("$path/" . InstallStorage::CONFIG_INSTALL_DIRECTORY . "/$name.settings.yml");
  40. $this->container->get('theme_installer')->install([$name]);
  41. $this->assertIdentical(theme_get_setting('base', $name), 'only');
  42. }
  43. /**
  44. * Tests that the $theme.settings default config file is optional.
  45. */
  46. public function testNoDefaultConfig() {
  47. $name = 'stark';
  48. $path = $this->availableThemes[$name]->getPath();
  49. $this->assertFileNotExists("$path/" . InstallStorage::CONFIG_INSTALL_DIRECTORY . "/$name.settings.yml");
  50. $this->container->get('theme_installer')->install([$name]);
  51. $this->assertNotNull(theme_get_setting('features.favicon', $name));
  52. }
  53. /**
  54. * Tests that the default logo config can be overridden.
  55. */
  56. public function testLogoConfig() {
  57. /** @var \Drupal\Core\Extension\ThemeInstallerInterface $theme_installer */
  58. $theme_installer = $this->container->get('theme_installer');
  59. $theme_installer->install(['stark']);
  60. /** @var \Drupal\Core\Extension\ThemeHandler $theme_handler */
  61. $theme_handler = $this->container->get('theme_handler');
  62. $theme = $theme_handler->getTheme('stark');
  63. // Tests default behavior.
  64. $expected = '/' . $theme->getPath() . '/logo.svg';
  65. $this->assertEquals($expected, theme_get_setting('logo.url', 'stark'));
  66. $config = $this->config('stark.settings');
  67. drupal_static_reset('theme_get_setting');
  68. $values = [
  69. 'default_logo' => FALSE,
  70. 'logo_path' => 'public://logo_with_scheme.png',
  71. ];
  72. theme_settings_convert_to_config($values, $config)->save();
  73. // Tests logo path with scheme.
  74. $expected = file_url_transform_relative(file_create_url('public://logo_with_scheme.png'));
  75. $this->assertEquals($expected, theme_get_setting('logo.url', 'stark'));
  76. $values = [
  77. 'default_logo' => FALSE,
  78. 'logo_path' => $theme->getPath() . '/logo_relative_path.gif',
  79. ];
  80. theme_settings_convert_to_config($values, $config)->save();
  81. drupal_static_reset('theme_get_setting');
  82. // Tests relative path.
  83. $expected = '/' . $theme->getPath() . '/logo_relative_path.gif';
  84. $this->assertEquals($expected, theme_get_setting('logo.url', 'stark'));
  85. $theme_installer->install(['test_theme']);
  86. \Drupal::configFactory()
  87. ->getEditable('system.theme')
  88. ->set('default', 'test_theme')
  89. ->save();
  90. $theme = $theme_handler->getTheme('test_theme');
  91. drupal_static_reset('theme_get_setting');
  92. // Tests logo set in test_theme.info.yml.
  93. $expected = '/' . $theme->getPath() . '/images/logo2.svg';
  94. $this->assertEquals($expected, theme_get_setting('logo.url', 'test_theme'));
  95. }
  96. }