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