Chris@0: drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); Chris@0: Chris@0: $this->adminUser = $this->drupalCreateUser(['access administration pages', 'view the administration theme', 'administer themes', 'bypass node access', 'administer blocks']); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: $this->node = $this->drupalCreateNode(); Chris@0: $this->drupalPlaceBlock('local_tasks_block'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test the theme settings form. Chris@0: */ Chris@0: public function testThemeSettings() { Chris@0: // Ensure invalid theme settings form URLs return a proper 404. Chris@0: $this->drupalGet('admin/appearance/settings/bartik'); Chris@0: $this->assertResponse(404, 'The theme settings form URL for a uninstalled theme could not be found.'); Chris@0: $this->drupalGet('admin/appearance/settings/' . $this->randomMachineName()); Chris@0: $this->assertResponse(404, 'The theme settings form URL for a non-existent theme could not be found.'); Chris@0: $this->assertTrue(\Drupal::service('theme_installer')->install(['stable'])); Chris@0: $this->drupalGet('admin/appearance/settings/stable'); Chris@0: $this->assertResponse(404, 'The theme settings form URL for a hidden theme is unavailable.'); Chris@0: Chris@0: // Specify a filesystem path to be used for the logo. Chris@0: $file = current($this->drupalGetTestFiles('image')); Chris@0: $file_relative = strtr($file->uri, ['public:/' => PublicStream::basePath()]); Chris@0: $default_theme_path = 'core/themes/classy'; Chris@0: Chris@0: $supported_paths = [ Chris@0: // Raw stream wrapper URI. Chris@0: $file->uri => [ Chris@0: 'form' => file_uri_target($file->uri), Chris@0: 'src' => file_url_transform_relative(file_create_url($file->uri)), Chris@0: ], Chris@0: // Relative path within the public filesystem. Chris@0: file_uri_target($file->uri) => [ Chris@0: 'form' => file_uri_target($file->uri), Chris@0: 'src' => file_url_transform_relative(file_create_url($file->uri)), Chris@0: ], Chris@0: // Relative path to a public file. Chris@0: $file_relative => [ Chris@0: 'form' => $file_relative, Chris@0: 'src' => file_url_transform_relative(file_create_url($file->uri)), Chris@0: ], Chris@0: // Relative path to an arbitrary file. Chris@0: 'core/misc/druplicon.png' => [ Chris@0: 'form' => 'core/misc/druplicon.png', Chris@0: 'src' => base_path() . 'core/misc/druplicon.png', Chris@0: ], Chris@0: // Relative path to a file in a theme. Chris@0: $default_theme_path . '/logo.svg' => [ Chris@0: 'form' => $default_theme_path . '/logo.svg', Chris@0: 'src' => base_path() . $default_theme_path . '/logo.svg', Chris@0: ], Chris@0: ]; Chris@0: foreach ($supported_paths as $input => $expected) { Chris@0: $edit = [ Chris@0: 'default_logo' => FALSE, Chris@0: 'logo_path' => $input, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/appearance/settings', $edit, t('Save configuration')); Chris@0: $this->assertNoText('The custom logo path is invalid.'); Chris@0: $this->assertFieldByName('logo_path', $expected['form']); Chris@0: Chris@0: // Verify logo path examples. Chris@0: $elements = $this->xpath('//div[contains(@class, :item)]/div[@class=:description]/code', [ Chris@0: ':item' => 'js-form-item-logo-path', Chris@0: ':description' => 'description', Chris@0: ]); Chris@0: // Expected default values (if all else fails). Chris@0: $implicit_public_file = 'logo.svg'; Chris@0: $explicit_file = 'public://logo.svg'; Chris@0: $local_file = $default_theme_path . '/logo.svg'; Chris@0: // Adjust for fully qualified stream wrapper URI in public filesystem. Chris@0: if (file_uri_scheme($input) == 'public') { Chris@0: $implicit_public_file = file_uri_target($input); Chris@0: $explicit_file = $input; Chris@0: $local_file = strtr($input, ['public:/' => PublicStream::basePath()]); Chris@0: } Chris@0: // Adjust for fully qualified stream wrapper URI elsewhere. Chris@0: elseif (file_uri_scheme($input) !== FALSE) { Chris@0: $explicit_file = $input; Chris@0: } Chris@0: // Adjust for relative path within public filesystem. Chris@0: elseif ($input == file_uri_target($file->uri)) { Chris@0: $implicit_public_file = $input; Chris@0: $explicit_file = 'public://' . $input; Chris@0: $local_file = PublicStream::basePath() . '/' . $input; Chris@0: } Chris@0: $this->assertEqual((string) $elements[0], $implicit_public_file); Chris@0: $this->assertEqual((string) $elements[1], $explicit_file); Chris@0: $this->assertEqual((string) $elements[2], $local_file); Chris@0: Chris@0: // Verify the actual 'src' attribute of the logo being output in a site Chris@0: // branding block. Chris@0: $this->drupalPlaceBlock('system_branding_block', ['region' => 'header']); Chris@0: $this->drupalGet(''); Chris@0: $elements = $this->xpath('//header//a[@rel=:rel]/img', [ Chris@0: ':rel' => 'home', Chris@0: ] Chris@0: ); Chris@0: $this->assertEqual((string) $elements[0]['src'], $expected['src']); Chris@0: } Chris@0: $unsupported_paths = [ Chris@0: // Stream wrapper URI to non-existing file. Chris@0: 'public://whatever.png', Chris@0: 'private://whatever.png', Chris@0: 'temporary://whatever.png', Chris@0: // Bogus stream wrapper URIs. Chris@0: 'public:/whatever.png', Chris@0: '://whatever.png', Chris@0: ':whatever.png', Chris@0: 'public://', Chris@0: // Relative path within the public filesystem to non-existing file. Chris@0: 'whatever.png', Chris@0: // Relative path to non-existing file in public filesystem. Chris@0: PublicStream::basePath() . '/whatever.png', Chris@0: // Semi-absolute path to non-existing file in public filesystem. Chris@0: '/' . PublicStream::basePath() . '/whatever.png', Chris@0: // Relative path to arbitrary non-existing file. Chris@0: 'core/misc/whatever.png', Chris@0: // Semi-absolute path to arbitrary non-existing file. Chris@0: '/core/misc/whatever.png', Chris@0: // Absolute paths to any local file (even if it exists). Chris@0: drupal_realpath($file->uri), Chris@0: ]; Chris@0: $this->drupalGet('admin/appearance/settings'); Chris@0: foreach ($unsupported_paths as $path) { Chris@0: $edit = [ Chris@0: 'default_logo' => FALSE, Chris@0: 'logo_path' => $path, Chris@0: ]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save configuration')); Chris@0: $this->assertText('The custom logo path is invalid.'); Chris@0: } Chris@0: Chris@0: // Upload a file to use for the logo. Chris@0: $edit = [ Chris@0: 'default_logo' => FALSE, Chris@0: 'logo_path' => '', Chris@0: 'files[logo_upload]' => drupal_realpath($file->uri), Chris@0: ]; Chris@0: $this->drupalPostForm('admin/appearance/settings', $edit, t('Save configuration')); Chris@0: Chris@0: $fields = $this->xpath($this->constructFieldXpath('name', 'logo_path')); Chris@0: $uploaded_filename = 'public://' . $fields[0]['value']; Chris@0: Chris@0: $this->drupalPlaceBlock('system_branding_block', ['region' => 'header']); Chris@0: $this->drupalGet(''); Chris@0: $elements = $this->xpath('//header//a[@rel=:rel]/img', [ Chris@0: ':rel' => 'home', Chris@0: ] Chris@0: ); Chris@0: $this->assertEqual($elements[0]['src'], file_url_transform_relative(file_create_url($uploaded_filename))); Chris@0: Chris@0: $this->container->get('theme_handler')->install(['bartik']); Chris@0: Chris@0: // Ensure only valid themes are listed in the local tasks. Chris@0: $this->drupalPlaceBlock('local_tasks_block', ['region' => 'header']); Chris@0: $this->drupalGet('admin/appearance/settings'); Chris@0: $theme_handler = \Drupal::service('theme_handler'); Chris@0: $this->assertLink($theme_handler->getName('classy')); Chris@0: $this->assertLink($theme_handler->getName('bartik')); Chris@0: $this->assertNoLink($theme_handler->getName('stable')); Chris@0: Chris@0: // If a hidden theme is an admin theme it should be viewable. Chris@0: \Drupal::configFactory()->getEditable('system.theme')->set('admin', 'stable')->save(); Chris@0: \Drupal::service('router.builder')->rebuildIfNeeded(); Chris@0: $this->drupalPlaceBlock('local_tasks_block', ['region' => 'header', 'theme' => 'stable']); Chris@0: $this->drupalGet('admin/appearance/settings'); Chris@0: $this->assertLink($theme_handler->getName('stable')); Chris@0: $this->drupalGet('admin/appearance/settings/stable'); Chris@0: $this->assertResponse(200, 'The theme settings form URL for a hidden theme that is the admin theme is available.'); Chris@0: Chris@0: // Ensure default logo and favicons are not triggering custom path Chris@0: // validation errors if their custom paths are set on the form. Chris@0: $edit = [ Chris@0: 'default_logo' => TRUE, Chris@0: 'logo_path' => 'public://whatever.png', Chris@0: 'default_favicon' => TRUE, Chris@0: 'favicon_path' => 'public://whatever.ico', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/appearance/settings', $edit, 'Save configuration'); Chris@0: $this->assertNoText('The custom logo path is invalid.'); Chris@0: $this->assertNoText('The custom favicon path is invalid.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test the theme settings logo form. Chris@0: */ Chris@0: public function testThemeSettingsLogo() { Chris@0: // Visit Bartik's theme settings page to replace the logo. Chris@0: $this->container->get('theme_handler')->install(['bartik']); Chris@0: $this->drupalGet('admin/appearance/settings/bartik'); Chris@0: $edit = [ Chris@0: 'default_logo' => FALSE, Chris@0: 'logo_path' => 'core/misc/druplicon.png', Chris@0: ]; Chris@0: $this->drupalPostForm('admin/appearance/settings/bartik', $edit, t('Save configuration')); Chris@0: $this->assertFieldByName('default_logo', FALSE); Chris@0: $this->assertFieldByName('logo_path', 'core/misc/druplicon.png'); Chris@0: Chris@0: // Make sure the logo and favicon settings are not available when the file Chris@0: // module is not enabled. Chris@0: \Drupal::service('module_installer')->uninstall(['file']); Chris@0: $this->drupalGet('admin/appearance/settings'); Chris@0: $this->assertNoText('Logo image settings'); Chris@0: $this->assertNoText('Shortcut icon settings'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test the administration theme functionality. Chris@0: */ Chris@0: public function testAdministrationTheme() { Chris@0: $this->container->get('theme_handler')->install(['seven']); Chris@0: Chris@0: // Install an administration theme and show it on the node admin pages. Chris@0: $edit = [ Chris@0: 'admin_theme' => 'seven', Chris@0: 'use_admin_theme' => TRUE, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/appearance', $edit, t('Save configuration')); Chris@0: Chris@0: $this->drupalGet('admin/config'); Chris@0: $this->assertRaw('core/themes/seven', 'Administration theme used on an administration page.'); Chris@0: Chris@0: $this->drupalGet('node/' . $this->node->id()); Chris@0: $this->assertRaw('core/themes/classy', 'Site default theme used on node page.'); Chris@0: Chris@0: $this->drupalGet('node/add'); Chris@0: $this->assertRaw('core/themes/seven', 'Administration theme used on the add content page.'); Chris@0: Chris@0: $this->drupalGet('node/' . $this->node->id() . '/edit'); Chris@0: $this->assertRaw('core/themes/seven', 'Administration theme used on the edit content page.'); Chris@0: Chris@0: // Disable the admin theme on the node admin pages. Chris@0: $edit = [ Chris@0: 'use_admin_theme' => FALSE, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/appearance', $edit, t('Save configuration')); Chris@0: Chris@0: $this->drupalGet('admin/config'); Chris@0: $this->assertRaw('core/themes/seven', 'Administration theme used on an administration page.'); Chris@0: Chris@0: // Ensure that the admin theme is also visible on the 403 page. Chris@0: $normal_user = $this->drupalCreateUser(['view the administration theme']); Chris@0: $this->drupalLogin($normal_user); Chris@0: $this->drupalGet('admin/config'); Chris@0: $this->assertResponse(403); Chris@0: $this->assertRaw('core/themes/seven', 'Administration theme used on an administration page.'); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: Chris@0: $this->drupalGet('node/add'); Chris@0: $this->assertRaw('core/themes/classy', 'Site default theme used on the add content page.'); Chris@0: Chris@0: // Reset to the default theme settings. Chris@0: $edit = [ Chris@0: 'admin_theme' => '0', Chris@0: 'use_admin_theme' => FALSE, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/appearance', $edit, t('Save configuration')); Chris@0: Chris@0: $this->drupalGet('admin'); Chris@0: $this->assertRaw('core/themes/classy', 'Site default theme used on administration page.'); Chris@0: Chris@0: $this->drupalGet('node/add'); Chris@0: $this->assertRaw('core/themes/classy', 'Site default theme used on the add content page.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test switching the default theme. Chris@0: */ Chris@0: public function testSwitchDefaultTheme() { Chris@0: /** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler */ Chris@0: $theme_handler = \Drupal::service('theme_handler'); Chris@0: // First, install Stark and set it as the default theme programmatically. Chris@0: $theme_handler->install(['stark']); Chris@0: $this->config('system.theme')->set('default', 'stark')->save(); Chris@0: Chris@0: // Install Bartik and set it as the default theme. Chris@0: $theme_handler->install(['bartik']); Chris@0: $this->drupalGet('admin/appearance'); Chris@0: $this->clickLink(t('Set as default')); Chris@0: $this->assertEqual($this->config('system.theme')->get('default'), 'bartik'); Chris@0: Chris@0: // Test the default theme on the secondary links (blocks admin page). Chris@0: $this->drupalGet('admin/structure/block'); Chris@0: $this->assertText('Bartik(' . t('active tab') . ')', 'Default local task on blocks admin page is the default theme.'); Chris@0: // Switch back to Stark and test again to test that the menu cache is cleared. Chris@0: $this->drupalGet('admin/appearance'); Chris@0: // Stark is the first 'Set as default' link. Chris@0: $this->clickLink(t('Set as default')); Chris@0: $this->drupalGet('admin/structure/block'); Chris@0: $this->assertText('Stark(' . t('active tab') . ')', 'Default local task on blocks admin page has changed.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test themes can't be installed when the base theme or engine is missing. Chris@0: * Chris@0: * Include test for themes that have a missing base theme somewhere further up Chris@0: * the chain than the immediate base theme. Chris@0: */ Chris@0: public function testInvalidTheme() { Chris@0: // theme_page_test_system_info_alter() un-hides all hidden themes. Chris@0: $this->container->get('module_installer')->install(['theme_page_test']); Chris@0: // Clear the system_list() and theme listing cache to pick up the change. Chris@0: $this->container->get('theme_handler')->reset(); Chris@0: $this->drupalGet('admin/appearance'); Chris@0: $this->assertText(t('This theme requires the base theme @base_theme to operate correctly.', ['@base_theme' => 'not_real_test_basetheme'])); Chris@0: $this->assertText(t('This theme requires the base theme @base_theme to operate correctly.', ['@base_theme' => 'test_invalid_basetheme'])); Chris@0: $this->assertText(t('This theme requires the theme engine @theme_engine to operate correctly.', ['@theme_engine' => 'not_real_engine'])); Chris@0: // Check for the error text of a theme with the wrong core version. Chris@0: $this->assertText("This theme is not compatible with Drupal 8.x. Check that the .info.yml file contains the correct 'core' value."); Chris@0: // Check for the error text of a theme without a content region. Chris@0: $this->assertText("This theme is missing a 'content' region."); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test uninstalling of themes works. Chris@0: */ Chris@0: public function testUninstallingThemes() { Chris@0: // Install Bartik and set it as the default theme. Chris@0: \Drupal::service('theme_handler')->install(['bartik']); Chris@0: // Set up seven as the admin theme. Chris@0: \Drupal::service('theme_handler')->install(['seven']); Chris@0: $edit = [ Chris@0: 'admin_theme' => 'seven', Chris@0: 'use_admin_theme' => TRUE, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/appearance', $edit, t('Save configuration')); Chris@0: $this->drupalGet('admin/appearance'); Chris@0: $this->clickLink(t('Set as default')); Chris@0: Chris@0: // Check that seven cannot be uninstalled as it is the admin theme. Chris@0: $this->assertNoRaw('Uninstall Seven theme', 'A link to uninstall the Seven theme does not appear on the theme settings page.'); Chris@0: // Check that bartik cannot be uninstalled as it is the default theme. Chris@0: $this->assertNoRaw('Uninstall Bartik theme', 'A link to uninstall the Bartik theme does not appear on the theme settings page.'); Chris@0: // Check that the classy theme cannot be uninstalled as it is a base theme Chris@0: // of seven and bartik. Chris@0: $this->assertNoRaw('Uninstall Classy theme', 'A link to uninstall the Classy theme does not appear on the theme settings page.'); Chris@0: Chris@0: // Install Stark and set it as the default theme. Chris@0: \Drupal::service('theme_handler')->install(['stark']); Chris@0: Chris@0: $edit = [ Chris@0: 'admin_theme' => 'stark', Chris@0: 'use_admin_theme' => TRUE, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/appearance', $edit, t('Save configuration')); Chris@0: Chris@0: // Check that seven can be uninstalled now. Chris@0: $this->assertRaw('Uninstall Seven theme', 'A link to uninstall the Seven theme does appear on the theme settings page.'); Chris@0: // Check that the classy theme still cannot be uninstalled as it is a Chris@0: // base theme of bartik. Chris@0: $this->assertNoRaw('Uninstall Classy theme', 'A link to uninstall the Classy theme does not appear on the theme settings page.'); Chris@0: Chris@0: // Change the default theme to stark, stark is second in the list. Chris@0: $this->clickLink(t('Set as default'), 1); Chris@0: Chris@0: // Check that bartik can be uninstalled now. Chris@0: $this->assertRaw('Uninstall Bartik theme', 'A link to uninstall the Bartik theme does appear on the theme settings page.'); Chris@0: Chris@0: // Check that the classy theme still can't be uninstalled as neither of it's Chris@0: // base themes have been. Chris@0: $this->assertNoRaw('Uninstall Classy theme', 'A link to uninstall the Classy theme does not appear on the theme settings page.'); Chris@0: Chris@0: // Uninstall each of the three themes starting with Bartik. Chris@0: $this->clickLink(t('Uninstall')); Chris@0: $this->assertRaw('The Bartik theme has been uninstalled'); Chris@0: // Seven is the second in the list. Chris@0: $this->clickLink(t('Uninstall')); Chris@0: $this->assertRaw('The Seven theme has been uninstalled'); Chris@0: Chris@0: // Check that the classy theme still can't be uninstalled as it is hidden. Chris@0: $this->assertNoRaw('Uninstall Classy theme', 'A link to uninstall the Classy theme does not appear on the theme settings page.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests installing a theme and setting it as default. Chris@0: */ Chris@0: public function testInstallAndSetAsDefault() { Chris@0: $this->drupalGet('admin/appearance'); Chris@0: // Bartik is uninstalled in the test profile and has the third "Install and Chris@0: // set as default" link. Chris@0: $this->clickLink(t('Install and set as default'), 2); Chris@0: // Test the confirmation message. Chris@0: $this->assertText('Bartik is now the default theme.'); Chris@0: // Make sure Bartik is now set as the default theme in config. Chris@0: $this->assertEqual($this->config('system.theme')->get('default'), 'bartik'); Chris@0: Chris@0: // This checks for a regression. See https://www.drupal.org/node/2498691. Chris@0: $this->assertNoText('The bartik theme was not found.'); Chris@0: Chris@0: $themes = \Drupal::service('theme_handler')->rebuildThemeData(); Chris@0: $version = $themes['bartik']->info['version']; Chris@0: Chris@0: // Confirm Bartik is indicated as the default theme. Chris@0: $this->assertTextPattern('/Bartik ' . preg_quote($version) . '\s{2,}\(default theme\)/'); Chris@0: } Chris@0: Chris@0: }