Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / src / Functional / System / ThemeTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\System;
4
5 use Drupal\Core\StreamWrapper\PublicStream;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\Tests\TestFileCreationTrait;
8
9 /**
10  * Tests the theme interface functionality by enabling and switching themes, and
11  * using an administration theme.
12  *
13  * @group system
14  */
15 class ThemeTest extends BrowserTestBase {
16
17   use TestFileCreationTrait {
18     getTestFiles as drupalGetTestFiles;
19   }
20
21   /**
22    * A user with administrative permissions.
23    *
24    * @var \Drupal\user\UserInterface
25    */
26   protected $adminUser;
27
28   /**
29    * Modules to enable.
30    *
31    * @var array
32    */
33   public static $modules = ['node', 'block', 'file'];
34
35   protected function setUp() {
36     parent::setUp();
37
38     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
39
40     $this->adminUser = $this->drupalCreateUser(['access administration pages', 'view the administration theme', 'administer themes', 'bypass node access', 'administer blocks']);
41     $this->drupalLogin($this->adminUser);
42     $this->node = $this->drupalCreateNode();
43     $this->drupalPlaceBlock('local_tasks_block');
44   }
45
46   /**
47    * Test the theme settings form.
48    */
49   public function testThemeSettings() {
50     // Ensure invalid theme settings form URLs return a proper 404.
51     $this->drupalGet('admin/appearance/settings/bartik');
52     $this->assertResponse(404, 'The theme settings form URL for a uninstalled theme could not be found.');
53     $this->drupalGet('admin/appearance/settings/' . $this->randomMachineName());
54     $this->assertResponse(404, 'The theme settings form URL for a non-existent theme could not be found.');
55     $this->assertTrue(\Drupal::service('theme_installer')->install(['stable']));
56     $this->drupalGet('admin/appearance/settings/stable');
57     $this->assertResponse(404, 'The theme settings form URL for a hidden theme is unavailable.');
58
59     // Specify a filesystem path to be used for the logo.
60     $file = current($this->drupalGetTestFiles('image'));
61     $file_relative = strtr($file->uri, ['public:/' => PublicStream::basePath()]);
62     $default_theme_path = 'core/themes/classy';
63
64     $supported_paths = [
65       // Raw stream wrapper URI.
66       $file->uri => [
67         'form' => file_uri_target($file->uri),
68         'src' => file_url_transform_relative(file_create_url($file->uri)),
69       ],
70       // Relative path within the public filesystem.
71       file_uri_target($file->uri) => [
72         'form' => file_uri_target($file->uri),
73         'src' => file_url_transform_relative(file_create_url($file->uri)),
74       ],
75       // Relative path to a public file.
76       $file_relative => [
77         'form' => $file_relative,
78         'src' => file_url_transform_relative(file_create_url($file->uri)),
79       ],
80       // Relative path to an arbitrary file.
81       'core/misc/druplicon.png' => [
82         'form' => 'core/misc/druplicon.png',
83         'src' => base_path() . 'core/misc/druplicon.png',
84       ],
85       // Relative path to a file in a theme.
86       $default_theme_path . '/logo.svg' => [
87         'form' => $default_theme_path . '/logo.svg',
88         'src' => base_path() . $default_theme_path . '/logo.svg',
89       ],
90     ];
91     foreach ($supported_paths as $input => $expected) {
92       $edit = [
93         'default_logo' => FALSE,
94         'logo_path' => $input,
95       ];
96       $this->drupalPostForm('admin/appearance/settings', $edit, t('Save configuration'));
97       $this->assertNoText('The custom logo path is invalid.');
98       $this->assertFieldByName('logo_path', $expected['form']);
99
100       // Verify logo path examples.
101       $elements = $this->xpath('//div[contains(@class, :item)]/div[@class=:description]/code', [
102         ':item' => 'js-form-item-logo-path',
103         ':description' => 'description',
104       ]);
105       // Expected default values (if all else fails).
106       $implicit_public_file = 'logo.svg';
107       $explicit_file = 'public://logo.svg';
108       $local_file = $default_theme_path . '/logo.svg';
109       // Adjust for fully qualified stream wrapper URI in public filesystem.
110       if (file_uri_scheme($input) == 'public') {
111         $implicit_public_file = file_uri_target($input);
112         $explicit_file = $input;
113         $local_file = strtr($input, ['public:/' => PublicStream::basePath()]);
114       }
115       // Adjust for fully qualified stream wrapper URI elsewhere.
116       elseif (file_uri_scheme($input) !== FALSE) {
117         $explicit_file = $input;
118       }
119       // Adjust for relative path within public filesystem.
120       elseif ($input == file_uri_target($file->uri)) {
121         $implicit_public_file = $input;
122         $explicit_file = 'public://' . $input;
123         $local_file = PublicStream::basePath() . '/' . $input;
124       }
125       $this->assertEqual($elements[0]->getText(), $implicit_public_file);
126       $this->assertEqual($elements[1]->getText(), $explicit_file);
127       $this->assertEqual($elements[2]->getText(), $local_file);
128
129       // Verify the actual 'src' attribute of the logo being output in a site
130       // branding block.
131       $this->drupalPlaceBlock('system_branding_block', ['region' => 'header']);
132       $this->drupalGet('');
133       $elements = $this->xpath('//header//a[@rel=:rel]/img', [
134           ':rel' => 'home',
135         ]
136       );
137       $this->assertEqual($elements[0]->getAttribute('src'), $expected['src']);
138     }
139     $unsupported_paths = [
140       // Stream wrapper URI to non-existing file.
141       'public://whatever.png',
142       'private://whatever.png',
143       'temporary://whatever.png',
144       // Bogus stream wrapper URIs.
145       'public:/whatever.png',
146       '://whatever.png',
147       ':whatever.png',
148       'public://',
149       // Relative path within the public filesystem to non-existing file.
150       'whatever.png',
151       // Relative path to non-existing file in public filesystem.
152       PublicStream::basePath() . '/whatever.png',
153       // Semi-absolute path to non-existing file in public filesystem.
154       '/' . PublicStream::basePath() . '/whatever.png',
155       // Relative path to arbitrary non-existing file.
156       'core/misc/whatever.png',
157       // Semi-absolute path to arbitrary non-existing file.
158       '/core/misc/whatever.png',
159       // Absolute paths to any local file (even if it exists).
160       \Drupal::service('file_system')->realpath($file->uri),
161     ];
162     $this->drupalGet('admin/appearance/settings');
163     foreach ($unsupported_paths as $path) {
164       $edit = [
165         'default_logo' => FALSE,
166         'logo_path' => $path,
167       ];
168       $this->drupalPostForm(NULL, $edit, t('Save configuration'));
169       $this->assertText('The custom logo path is invalid.');
170     }
171
172     // Upload a file to use for the logo.
173     $edit = [
174       'default_logo' => FALSE,
175       'logo_path' => '',
176       'files[logo_upload]' => \Drupal::service('file_system')->realpath($file->uri),
177     ];
178     $this->drupalPostForm('admin/appearance/settings', $edit, t('Save configuration'));
179
180     $fields = $this->xpath($this->constructFieldXpath('name', 'logo_path'));
181     $uploaded_filename = 'public://' . $fields[0]->getValue();
182
183     $this->drupalPlaceBlock('system_branding_block', ['region' => 'header']);
184     $this->drupalGet('');
185     $elements = $this->xpath('//header//a[@rel=:rel]/img', [
186         ':rel' => 'home',
187       ]
188     );
189     $this->assertEqual($elements[0]->getAttribute('src'), file_url_transform_relative(file_create_url($uploaded_filename)));
190
191     $this->container->get('theme_handler')->install(['bartik']);
192
193     // Ensure only valid themes are listed in the local tasks.
194     $this->drupalPlaceBlock('local_tasks_block', ['region' => 'header']);
195     $this->drupalGet('admin/appearance/settings');
196     $theme_handler = \Drupal::service('theme_handler');
197     $this->assertLink($theme_handler->getName('classy'));
198     $this->assertLink($theme_handler->getName('bartik'));
199     $this->assertNoLink($theme_handler->getName('stable'));
200
201     // If a hidden theme is an admin theme it should be viewable.
202     \Drupal::configFactory()->getEditable('system.theme')->set('admin', 'stable')->save();
203     \Drupal::service('router.builder')->rebuildIfNeeded();
204     $this->drupalPlaceBlock('local_tasks_block', ['region' => 'header', 'theme' => 'stable']);
205     $this->drupalGet('admin/appearance/settings');
206     $this->assertLink($theme_handler->getName('stable'));
207     $this->drupalGet('admin/appearance/settings/stable');
208     $this->assertResponse(200, 'The theme settings form URL for a hidden theme that is the admin theme is available.');
209
210     // Ensure default logo and favicons are not triggering custom path
211     // validation errors if their custom paths are set on the form.
212     $edit = [
213       'default_logo' => TRUE,
214       'logo_path' => 'public://whatever.png',
215       'default_favicon' => TRUE,
216       'favicon_path' => 'public://whatever.ico',
217     ];
218     $this->drupalPostForm('admin/appearance/settings', $edit, 'Save configuration');
219     $this->assertNoText('The custom logo path is invalid.');
220     $this->assertNoText('The custom favicon path is invalid.');
221   }
222
223   /**
224    * Test the theme settings logo form.
225    */
226   public function testThemeSettingsLogo() {
227     // Visit Bartik's theme settings page to replace the logo.
228     $this->container->get('theme_handler')->install(['bartik']);
229     $this->drupalGet('admin/appearance/settings/bartik');
230     $edit = [
231       'default_logo' => FALSE,
232       'logo_path' => 'core/misc/druplicon.png',
233     ];
234     $this->drupalPostForm('admin/appearance/settings/bartik', $edit, t('Save configuration'));
235     $this->assertFieldByName('default_logo', FALSE);
236     $this->assertFieldByName('logo_path', 'core/misc/druplicon.png');
237
238     // Make sure the logo and favicon settings are not available when the file
239     // module is not enabled.
240     \Drupal::service('module_installer')->uninstall(['file']);
241     $this->drupalGet('admin/appearance/settings');
242     $this->assertNoText('Logo image settings');
243     $this->assertNoText('Shortcut icon settings');
244   }
245
246   /**
247    * Test the administration theme functionality.
248    */
249   public function testAdministrationTheme() {
250     $this->container->get('theme_handler')->install(['seven']);
251
252     // Install an administration theme and show it on the node admin pages.
253     $edit = [
254       'admin_theme' => 'seven',
255       'use_admin_theme' => TRUE,
256     ];
257     $this->drupalPostForm('admin/appearance', $edit, t('Save configuration'));
258
259     $this->drupalGet('admin/config');
260     $this->assertRaw('core/themes/seven', 'Administration theme used on an administration page.');
261
262     $this->drupalGet('node/' . $this->node->id());
263     $this->assertRaw('core/themes/classy', 'Site default theme used on node page.');
264
265     $this->drupalGet('node/add');
266     $this->assertRaw('core/themes/seven', 'Administration theme used on the add content page.');
267
268     $this->drupalGet('node/' . $this->node->id() . '/edit');
269     $this->assertRaw('core/themes/seven', 'Administration theme used on the edit content page.');
270
271     // Disable the admin theme on the node admin pages.
272     $edit = [
273       'use_admin_theme' => FALSE,
274     ];
275     $this->drupalPostForm('admin/appearance', $edit, t('Save configuration'));
276
277     $this->drupalGet('admin/config');
278     $this->assertRaw('core/themes/seven', 'Administration theme used on an administration page.');
279
280     // Ensure that the admin theme is also visible on the 403 page.
281     $normal_user = $this->drupalCreateUser(['view the administration theme']);
282     $this->drupalLogin($normal_user);
283     $this->drupalGet('admin/config');
284     $this->assertResponse(403);
285     $this->assertRaw('core/themes/seven', 'Administration theme used on an administration page.');
286     $this->drupalLogin($this->adminUser);
287
288     $this->drupalGet('node/add');
289     $this->assertRaw('core/themes/classy', 'Site default theme used on the add content page.');
290
291     // Reset to the default theme settings.
292     $edit = [
293       'admin_theme' => '0',
294       'use_admin_theme' => FALSE,
295     ];
296     $this->drupalPostForm('admin/appearance', $edit, t('Save configuration'));
297
298     $this->drupalGet('admin');
299     $this->assertRaw('core/themes/classy', 'Site default theme used on administration page.');
300
301     $this->drupalGet('node/add');
302     $this->assertRaw('core/themes/classy', 'Site default theme used on the add content page.');
303   }
304
305   /**
306    * Test switching the default theme.
307    */
308   public function testSwitchDefaultTheme() {
309     /** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */
310     $theme_handler = \Drupal::service('theme_handler');
311     // First, install Stark and set it as the default theme programmatically.
312     $theme_handler->install(['stark']);
313     $this->config('system.theme')->set('default', 'stark')->save();
314
315     // Install Bartik and set it as the default theme.
316     $theme_handler->install(['bartik']);
317     $this->drupalGet('admin/appearance');
318     $this->clickLink(t('Set as default'));
319     $this->assertEqual($this->config('system.theme')->get('default'), 'bartik');
320
321     // Test the default theme on the secondary links (blocks admin page).
322     $this->drupalGet('admin/structure/block');
323     $this->assertText('Bartik(' . t('active tab') . ')', 'Default local task on blocks admin page is the default theme.');
324     // Switch back to Stark and test again to test that the menu cache is cleared.
325     $this->drupalGet('admin/appearance');
326     // Stark is the first 'Set as default' link.
327     $this->clickLink(t('Set as default'));
328     $this->drupalGet('admin/structure/block');
329     $this->assertText('Stark(' . t('active tab') . ')', 'Default local task on blocks admin page has changed.');
330   }
331
332   /**
333    * Test themes can't be installed when the base theme or engine is missing.
334    *
335    * Include test for themes that have a missing base theme somewhere further up
336    * the chain than the immediate base theme.
337    */
338   public function testInvalidTheme() {
339     // theme_page_test_system_info_alter() un-hides all hidden themes.
340     $this->container->get('module_installer')->install(['theme_page_test']);
341     // Clear the system_list() and theme listing cache to pick up the change.
342     $this->container->get('theme_handler')->reset();
343     $this->drupalGet('admin/appearance');
344     $this->assertText(t('This theme requires the base theme @base_theme to operate correctly.', ['@base_theme' => 'not_real_test_basetheme']));
345     $this->assertText(t('This theme requires the base theme @base_theme to operate correctly.', ['@base_theme' => 'test_invalid_basetheme']));
346     $this->assertText(t('This theme requires the theme engine @theme_engine to operate correctly.', ['@theme_engine' => 'not_real_engine']));
347     // Check for the error text of a theme with the wrong core version.
348     $this->assertText("This theme is not compatible with Drupal 8.x. Check that the .info.yml file contains the correct 'core' value.");
349     // Check for the error text of a theme without a content region.
350     $this->assertText("This theme is missing a 'content' region.");
351   }
352
353   /**
354    * Test uninstalling of themes works.
355    */
356   public function testUninstallingThemes() {
357     // Install Bartik and set it as the default theme.
358     \Drupal::service('theme_handler')->install(['bartik']);
359     // Set up seven as the admin theme.
360     \Drupal::service('theme_handler')->install(['seven']);
361     $edit = [
362       'admin_theme' => 'seven',
363       'use_admin_theme' => TRUE,
364     ];
365     $this->drupalPostForm('admin/appearance', $edit, t('Save configuration'));
366     $this->drupalGet('admin/appearance');
367     $this->clickLink(t('Set as default'));
368
369     // Check that seven cannot be uninstalled as it is the admin theme.
370     $this->assertNoRaw('Uninstall Seven theme', 'A link to uninstall the Seven theme does not appear on the theme settings page.');
371     // Check that bartik cannot be uninstalled as it is the default theme.
372     $this->assertNoRaw('Uninstall Bartik theme', 'A link to uninstall the Bartik theme does not appear on the theme settings page.');
373     // Check that the classy theme cannot be uninstalled as it is a base theme
374     // of seven and bartik.
375     $this->assertNoRaw('Uninstall Classy theme', 'A link to uninstall the Classy theme does not appear on the theme settings page.');
376
377     // Install Stark and set it as the default theme.
378     \Drupal::service('theme_handler')->install(['stark']);
379
380     $edit = [
381       'admin_theme' => 'stark',
382       'use_admin_theme' => TRUE,
383     ];
384     $this->drupalPostForm('admin/appearance', $edit, t('Save configuration'));
385
386     // Check that seven can be uninstalled now.
387     $this->assertRaw('Uninstall Seven theme', 'A link to uninstall the Seven theme does appear on the theme settings page.');
388     // Check that the classy theme still cannot be uninstalled as it is a
389     // base theme of bartik.
390     $this->assertNoRaw('Uninstall Classy theme', 'A link to uninstall the Classy theme does not appear on the theme settings page.');
391
392     // Change the default theme to stark, stark is second in the list.
393     $this->clickLink(t('Set as default'), 1);
394
395     // Check that bartik can be uninstalled now.
396     $this->assertRaw('Uninstall Bartik theme', 'A link to uninstall the Bartik theme does appear on the theme settings page.');
397
398     // Check that the classy theme still can't be uninstalled as neither of its
399     // base themes have been.
400     $this->assertNoRaw('Uninstall Classy theme', 'A link to uninstall the Classy theme does not appear on the theme settings page.');
401
402     // Uninstall each of the three themes starting with Bartik.
403     $this->clickLink(t('Uninstall'));
404     $this->assertRaw('The <em class="placeholder">Bartik</em> theme has been uninstalled');
405     // Seven is the second in the list.
406     $this->clickLink(t('Uninstall'));
407     $this->assertRaw('The <em class="placeholder">Seven</em> theme has been uninstalled');
408
409     // Check that the classy theme still can't be uninstalled as it is hidden.
410     $this->assertNoRaw('Uninstall Classy theme', 'A link to uninstall the Classy theme does not appear on the theme settings page.');
411   }
412
413   /**
414    * Tests installing a theme and setting it as default.
415    */
416   public function testInstallAndSetAsDefault() {
417     $this->drupalGet('admin/appearance');
418     // Bartik is uninstalled in the test profile and has the third "Install and
419     // set as default" link.
420     $this->clickLink(t('Install and set as default'), 2);
421     // Test the confirmation message.
422     $this->assertText('Bartik is now the default theme.');
423     // Make sure Bartik is now set as the default theme in config.
424     $this->assertEqual($this->config('system.theme')->get('default'), 'bartik');
425
426     // This checks for a regression. See https://www.drupal.org/node/2498691.
427     $this->assertNoText('The bartik theme was not found.');
428
429     $themes = \Drupal::service('theme_handler')->rebuildThemeData();
430     $version = $themes['bartik']->info['version'];
431
432     // Confirm Bartik is indicated as the default theme.
433     $out = $this->getSession()->getPage()->getContent();
434     $this->assertTrue((bool) preg_match('/Bartik ' . preg_quote($version) . '\s{2,}\(default theme\)/', $out));
435   }
436
437   /**
438    * Test the theme settings form when logo and favicon features are disabled.
439    */
440   public function testThemeSettingsNoLogoNoFavicon() {
441     // Install theme with no logo and no favicon feature.
442     $this->container->get('theme_handler')->install(['test_theme_settings_features']);
443     // Visit this theme's settings page.
444     $this->drupalGet('admin/appearance/settings/test_theme_settings_features');
445     $edit = [];
446     $this->drupalPostForm('admin/appearance/settings/test_theme_settings_features', $edit, t('Save configuration'));
447     $this->assertText('The configuration options have been saved.');
448   }
449
450 }