Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Theme / ThemeInstallerTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Theme;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\Extension\ExtensionNameLengthException;
7 use Drupal\KernelTests\KernelTestBase;
8
9 /**
10  * Tests installing and uninstalling of themes.
11  *
12  * @group Extension
13  */
14 class ThemeInstallerTest extends KernelTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['system'];
22
23   /**
24    * {@inheritdoc}
25    */
26   public function register(ContainerBuilder $container) {
27     parent::register($container);
28     // Some test methods involve ModuleHandler operations, which attempt to
29     // rebuild and dump routes.
30     $container
31       ->register('router.dumper', 'Drupal\Core\Routing\NullMatcherDumper');
32   }
33
34   protected function setUp() {
35     parent::setUp();
36     $this->installConfig(['system']);
37   }
38
39   /**
40    * Verifies that no themes are installed by default.
41    */
42   public function testEmpty() {
43     $this->assertFalse($this->extensionConfig()->get('theme'));
44
45     $this->assertFalse(array_keys($this->themeHandler()->listInfo()));
46     $this->assertFalse(array_keys(system_list('theme')));
47
48     // Rebuilding available themes should always yield results though.
49     $this->assertTrue($this->themeHandler()->rebuildThemeData()['stark'], 'ThemeHandler::rebuildThemeData() yields all available themes.');
50
51     // theme_get_setting() should return global default theme settings.
52     $this->assertIdentical(theme_get_setting('features.favicon'), TRUE);
53   }
54
55   /**
56    * Tests installing a theme.
57    */
58   public function testInstall() {
59     $name = 'test_basetheme';
60
61     $themes = $this->themeHandler()->listInfo();
62     $this->assertFalse(isset($themes[$name]));
63
64     $this->themeInstaller()->install([$name]);
65
66     $this->assertIdentical($this->extensionConfig()->get("theme.$name"), 0);
67
68     $themes = $this->themeHandler()->listInfo();
69     $this->assertTrue(isset($themes[$name]));
70     $this->assertEqual($themes[$name]->getName(), $name);
71
72     $this->assertEqual(array_keys(system_list('theme')), array_keys($themes));
73
74     // Verify that test_basetheme.settings is active.
75     $this->assertIdentical(theme_get_setting('features.favicon', $name), FALSE);
76     $this->assertEqual(theme_get_setting('base', $name), 'only');
77     $this->assertEqual(theme_get_setting('override', $name), 'base');
78   }
79
80   /**
81    * Tests installing a sub-theme.
82    */
83   public function testInstallSubTheme() {
84     $name = 'test_subtheme';
85     $base_name = 'test_basetheme';
86
87     $themes = $this->themeHandler()->listInfo();
88     $this->assertFalse(array_keys($themes));
89
90     $this->themeInstaller()->install([$name]);
91
92     $themes = $this->themeHandler()->listInfo();
93     $this->assertTrue(isset($themes[$name]));
94     $this->assertTrue(isset($themes[$base_name]));
95
96     $this->themeInstaller()->uninstall([$name]);
97
98     $themes = $this->themeHandler()->listInfo();
99     $this->assertFalse(isset($themes[$name]));
100     $this->assertTrue(isset($themes[$base_name]));
101   }
102
103   /**
104    * Tests installing a non-existing theme.
105    */
106   public function testInstallNonExisting() {
107     $name = 'non_existing_theme';
108
109     $themes = $this->themeHandler()->listInfo();
110     $this->assertFalse(array_keys($themes));
111
112     try {
113       $message = 'ThemeHandler::install() throws InvalidArgumentException upon installing a non-existing theme.';
114       $this->themeInstaller()->install([$name]);
115       $this->fail($message);
116     }
117     catch (\InvalidArgumentException $e) {
118       $this->pass(get_class($e) . ': ' . $e->getMessage());
119     }
120
121     $themes = $this->themeHandler()->listInfo();
122     $this->assertFalse(array_keys($themes));
123   }
124
125   /**
126    * Tests installing a theme with a too long name.
127    */
128   public function testInstallNameTooLong() {
129     $name = 'test_theme_having_veery_long_name_which_is_too_long';
130
131     try {
132       $message = 'ThemeHandler::install() throws ExtensionNameLengthException upon installing a theme with a too long name.';
133       $this->themeInstaller()->install([$name]);
134       $this->fail($message);
135     }
136     catch (ExtensionNameLengthException $e) {
137       $this->pass(get_class($e) . ': ' . $e->getMessage());
138     }
139   }
140
141   /**
142    * Tests uninstalling the default theme.
143    */
144   public function testUninstallDefault() {
145     $name = 'stark';
146     $other_name = 'bartik';
147     $this->themeInstaller()->install([$name, $other_name]);
148     $this->config('system.theme')->set('default', $name)->save();
149
150     $themes = $this->themeHandler()->listInfo();
151     $this->assertTrue(isset($themes[$name]));
152     $this->assertTrue(isset($themes[$other_name]));
153
154     try {
155       $message = 'ThemeHandler::uninstall() throws InvalidArgumentException upon disabling default theme.';
156       $this->themeHandler()->uninstall([$name]);
157       $this->fail($message);
158     }
159     catch (\InvalidArgumentException $e) {
160       $this->pass(get_class($e) . ': ' . $e->getMessage());
161     }
162
163     $themes = $this->themeHandler()->listInfo();
164     $this->assertTrue(isset($themes[$name]));
165     $this->assertTrue(isset($themes[$other_name]));
166   }
167
168   /**
169    * Tests uninstalling the admin theme.
170    */
171   public function testUninstallAdmin() {
172     $name = 'stark';
173     $other_name = 'bartik';
174     $this->themeInstaller()->install([$name, $other_name]);
175     $this->config('system.theme')->set('admin', $name)->save();
176
177     $themes = $this->themeHandler()->listInfo();
178     $this->assertTrue(isset($themes[$name]));
179     $this->assertTrue(isset($themes[$other_name]));
180
181     try {
182       $message = 'ThemeHandler::uninstall() throws InvalidArgumentException upon disabling admin theme.';
183       $this->themeHandler()->uninstall([$name]);
184       $this->fail($message);
185     }
186     catch (\InvalidArgumentException $e) {
187       $this->pass(get_class($e) . ': ' . $e->getMessage());
188     }
189
190     $themes = $this->themeHandler()->listInfo();
191     $this->assertTrue(isset($themes[$name]));
192     $this->assertTrue(isset($themes[$other_name]));
193   }
194
195   /**
196    * Tests uninstalling a sub-theme.
197    */
198   public function testUninstallSubTheme() {
199     $name = 'test_subtheme';
200     $base_name = 'test_basetheme';
201
202     $this->themeInstaller()->install([$name]);
203     $this->themeInstaller()->uninstall([$name]);
204
205     $themes = $this->themeHandler()->listInfo();
206     $this->assertFalse(isset($themes[$name]));
207     $this->assertTrue(isset($themes[$base_name]));
208   }
209
210   /**
211    * Tests uninstalling a base theme before its sub-theme.
212    */
213   public function testUninstallBaseBeforeSubTheme() {
214     $name = 'test_basetheme';
215     $sub_name = 'test_subtheme';
216
217     $this->themeInstaller()->install([$sub_name]);
218
219     try {
220       $message = 'ThemeHandler::install() throws InvalidArgumentException upon uninstalling base theme before sub theme.';
221       $this->themeInstaller()->uninstall([$name]);
222       $this->fail($message);
223     }
224     catch (\InvalidArgumentException $e) {
225       $this->pass(get_class($e) . ': ' . $e->getMessage());
226     }
227
228     $themes = $this->themeHandler()->listInfo();
229     $this->assertTrue(isset($themes[$name]));
230     $this->assertTrue(isset($themes[$sub_name]));
231
232     // Verify that uninstalling both at the same time works.
233     $this->themeInstaller()->uninstall([$name, $sub_name]);
234
235     $themes = $this->themeHandler()->listInfo();
236     $this->assertFalse(isset($themes[$name]));
237     $this->assertFalse(isset($themes[$sub_name]));
238   }
239
240   /**
241    * Tests uninstalling a non-existing theme.
242    */
243   public function testUninstallNonExisting() {
244     $name = 'non_existing_theme';
245
246     $themes = $this->themeHandler()->listInfo();
247     $this->assertFalse(array_keys($themes));
248
249     try {
250       $message = 'ThemeHandler::uninstall() throws InvalidArgumentException upon uninstalling a non-existing theme.';
251       $this->themeInstaller()->uninstall([$name]);
252       $this->fail($message);
253     }
254     catch (\InvalidArgumentException $e) {
255       $this->pass(get_class($e) . ': ' . $e->getMessage());
256     }
257
258     $themes = $this->themeHandler()->listInfo();
259     $this->assertFalse(array_keys($themes));
260   }
261
262   /**
263    * Tests uninstalling a theme.
264    */
265   public function testUninstall() {
266     $name = 'test_basetheme';
267
268     $this->themeInstaller()->install([$name]);
269     $this->assertTrue($this->config("$name.settings")->get());
270
271     $this->themeInstaller()->uninstall([$name]);
272
273     $this->assertFalse(array_keys($this->themeHandler()->listInfo()));
274     $this->assertFalse(array_keys(system_list('theme')));
275
276     $this->assertFalse($this->config("$name.settings")->get());
277
278     // Ensure that the uninstalled theme can be installed again.
279     $this->themeInstaller()->install([$name]);
280     $themes = $this->themeHandler()->listInfo();
281     $this->assertTrue(isset($themes[$name]));
282     $this->assertEqual($themes[$name]->getName(), $name);
283     $this->assertEqual(array_keys(system_list('theme')), array_keys($themes));
284     $this->assertTrue($this->config("$name.settings")->get());
285   }
286
287   /**
288    * Tests uninstalling a theme that is not installed.
289    */
290   public function testUninstallNotInstalled() {
291     $name = 'test_basetheme';
292
293     try {
294       $message = 'ThemeHandler::uninstall() throws InvalidArgumentException upon uninstalling a theme that is not installed.';
295       $this->themeInstaller()->uninstall([$name]);
296       $this->fail($message);
297     }
298     catch (\InvalidArgumentException $e) {
299       $this->pass(get_class($e) . ': ' . $e->getMessage());
300     }
301   }
302
303   /**
304    * Tests that theme info can be altered by a module.
305    *
306    * @see module_test_system_info_alter()
307    */
308   public function testThemeInfoAlter() {
309     $name = 'seven';
310     $this->container->get('state')->set('module_test.hook_system_info_alter', TRUE);
311
312     $this->themeInstaller()->install([$name]);
313
314     $themes = $this->themeHandler()->listInfo();
315     $this->assertFalse(isset($themes[$name]->info['regions']['test_region']));
316
317     // Rebuild module data so we know where module_test is located.
318     // @todo Remove as part of https://www.drupal.org/node/2186491
319     system_rebuild_module_data();
320     $this->moduleInstaller()->install(['module_test'], FALSE);
321     $this->assertTrue($this->moduleHandler()->moduleExists('module_test'));
322
323     $themes = $this->themeHandler()->listInfo();
324     $this->assertTrue(isset($themes[$name]->info['regions']['test_region']));
325
326     // Legacy assertions.
327     // @todo Remove once theme initialization/info has been modernized.
328     // @see https://www.drupal.org/node/2228093
329     $info = system_get_info('theme', $name);
330     $this->assertTrue(isset($info['regions']['test_region']));
331     $regions = system_region_list($name);
332     $this->assertTrue(isset($regions['test_region']));
333     $system_list = system_list('theme');
334     $this->assertTrue(isset($system_list[$name]->info['regions']['test_region']));
335
336     $this->moduleInstaller()->uninstall(['module_test']);
337     $this->assertFalse($this->moduleHandler()->moduleExists('module_test'));
338
339     $themes = $this->themeHandler()->listInfo();
340     $this->assertFalse(isset($themes[$name]->info['regions']['test_region']));
341
342     // Legacy assertions.
343     // @todo Remove once theme initialization/info has been modernized.
344     // @see https://www.drupal.org/node/2228093
345     $info = system_get_info('theme', $name);
346     $this->assertFalse(isset($info['regions']['test_region']));
347     $regions = system_region_list($name);
348     $this->assertFalse(isset($regions['test_region']));
349     $system_list = system_list('theme');
350     $this->assertFalse(isset($system_list[$name]->info['regions']['test_region']));
351   }
352
353   /**
354    * Returns the theme handler service.
355    *
356    * @return \Drupal\Core\Extension\ThemeHandlerInterface
357    */
358   protected function themeHandler() {
359     return $this->container->get('theme_handler');
360   }
361
362   /**
363    * Returns the theme installer service.
364    *
365    * @return \Drupal\Core\Extension\ThemeInstallerInterface
366    */
367   protected function themeInstaller() {
368     return $this->container->get('theme_installer');
369   }
370
371   /**
372    * Returns the system.theme config object.
373    *
374    * @return \Drupal\Core\Config\Config
375    */
376   protected function extensionConfig() {
377     return $this->config('core.extension');
378   }
379
380   /**
381    * Returns the ModuleHandler.
382    *
383    * @return \Drupal\Core\Extension\ModuleHandlerInterface
384    */
385   protected function moduleHandler() {
386     return $this->container->get('module_handler');
387   }
388
389   /**
390    * Returns the ModuleInstaller.
391    *
392    * @return \Drupal\Core\Extension\ModuleInstallerInterface
393    */
394   protected function moduleInstaller() {
395     return $this->container->get('module_installer');
396   }
397
398 }