You are here

function ThemeTest::testThemeSettings in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/system/src/Tests/System/ThemeTest.php \Drupal\system\Tests\System\ThemeTest::testThemeSettings()

Test the theme settings form.

File

core/modules/system/src/Tests/System/ThemeTest.php, line 49
Contains \Drupal\system\Tests\System\ThemeTest.

Class

ThemeTest
Tests the theme interface functionality by enabling and switching themes, and using an administration theme.

Namespace

Drupal\system\Tests\System

Code

function testThemeSettings() {

  // Ensure invalid theme settings form URLs return a proper 404.
  $this
    ->drupalGet('admin/appearance/settings/bartik');
  $this
    ->assertResponse(404, 'The theme settings form URL for a uninstalled theme could not be found.');
  $this
    ->drupalGet('admin/appearance/settings/' . $this
    ->randomMachineName());
  $this
    ->assertResponse(404, 'The theme settings form URL for a non-existent theme could not be found.');
  $this
    ->assertTrue(\Drupal::service('theme_installer')
    ->install([
    'stable',
  ]));
  $this
    ->drupalGet('admin/appearance/settings/stable');
  $this
    ->assertResponse(404, 'The theme settings form URL for a hidden theme is unavailable.');

  // Specify a filesystem path to be used for the logo.
  $file = current($this
    ->drupalGetTestFiles('image'));
  $file_relative = strtr($file->uri, array(
    'public:/' => PublicStream::basePath(),
  ));
  $default_theme_path = 'core/themes/classy';
  $supported_paths = array(
    // Raw stream wrapper URI.
    $file->uri => array(
      'form' => file_uri_target($file->uri),
      'src' => file_create_url($file->uri),
    ),
    // Relative path within the public filesystem.
    file_uri_target($file->uri) => array(
      'form' => file_uri_target($file->uri),
      'src' => file_create_url($file->uri),
    ),
    // Relative path to a public file.
    $file_relative => array(
      'form' => $file_relative,
      'src' => file_create_url($file->uri),
    ),
    // Relative path to an arbitrary file.
    'core/misc/druplicon.png' => array(
      'form' => 'core/misc/druplicon.png',
      'src' => $GLOBALS['base_url'] . '/' . 'core/misc/druplicon.png',
    ),
    // Relative path to a file in a theme.
    $default_theme_path . '/logo.svg' => array(
      'form' => $default_theme_path . '/logo.svg',
      'src' => $GLOBALS['base_url'] . '/' . $default_theme_path . '/logo.svg',
    ),
  );
  foreach ($supported_paths as $input => $expected) {
    $edit = array(
      'default_logo' => FALSE,
      'logo_path' => $input,
    );
    $this
      ->drupalPostForm('admin/appearance/settings', $edit, t('Save configuration'));
    $this
      ->assertNoText('The custom logo path is invalid.');
    $this
      ->assertFieldByName('logo_path', $expected['form']);

    // Verify logo path examples.
    $elements = $this
      ->xpath('//div[contains(@class, :item)]/div[@class=:description]/code', array(
      ':item' => 'js-form-item-logo-path',
      ':description' => 'description',
    ));

    // Expected default values (if all else fails).
    $implicit_public_file = 'logo.svg';
    $explicit_file = 'public://logo.svg';
    $local_file = $default_theme_path . '/logo.svg';

    // Adjust for fully qualified stream wrapper URI in public filesystem.
    if (file_uri_scheme($input) == 'public') {
      $implicit_public_file = file_uri_target($input);
      $explicit_file = $input;
      $local_file = strtr($input, array(
        'public:/' => PublicStream::basePath(),
      ));
    }
    elseif (file_uri_scheme($input) !== FALSE) {
      $explicit_file = $input;
    }
    elseif ($input == file_uri_target($file->uri)) {
      $implicit_public_file = $input;
      $explicit_file = 'public://' . $input;
      $local_file = PublicStream::basePath() . '/' . $input;
    }
    $this
      ->assertEqual((string) $elements[0], $implicit_public_file);
    $this
      ->assertEqual((string) $elements[1], $explicit_file);
    $this
      ->assertEqual((string) $elements[2], $local_file);

    // Verify the actual 'src' attribute of the logo being output in a site
    // branding block.
    $this
      ->drupalPlaceBlock('system_branding_block', [
      'region' => 'header',
    ]);
    $this
      ->drupalGet('');
    $elements = $this
      ->xpath('//header//a[@rel=:rel]/img', array(
      ':rel' => 'home',
    ));
    $this
      ->assertEqual((string) $elements[0]['src'], $expected['src']);
  }
  $unsupported_paths = array(
    // Stream wrapper URI to non-existing file.
    'public://whatever.png',
    'private://whatever.png',
    'temporary://whatever.png',
    // Bogus stream wrapper URIs.
    'public:/whatever.png',
    '://whatever.png',
    ':whatever.png',
    'public://',
    // Relative path within the public filesystem to non-existing file.
    'whatever.png',
    // Relative path to non-existing file in public filesystem.
    PublicStream::basePath() . '/whatever.png',
    // Semi-absolute path to non-existing file in public filesystem.
    '/' . PublicStream::basePath() . '/whatever.png',
    // Relative path to arbitrary non-existing file.
    'core/misc/whatever.png',
    // Semi-absolute path to arbitrary non-existing file.
    '/core/misc/whatever.png',
    // Absolute paths to any local file (even if it exists).
    drupal_realpath($file->uri),
  );
  $this
    ->drupalGet('admin/appearance/settings');
  foreach ($unsupported_paths as $path) {
    $edit = array(
      'default_logo' => FALSE,
      'logo_path' => $path,
    );
    $this
      ->drupalPostForm(NULL, $edit, t('Save configuration'));
    $this
      ->assertText('The custom logo path is invalid.');
  }

  // Upload a file to use for the logo.
  $edit = array(
    'default_logo' => FALSE,
    'logo_path' => '',
    'files[logo_upload]' => drupal_realpath($file->uri),
  );
  $this
    ->drupalPostForm('admin/appearance/settings', $edit, t('Save configuration'));
  $fields = $this
    ->xpath($this
    ->constructFieldXpath('name', 'logo_path'));
  $uploaded_filename = 'public://' . $fields[0]['value'];
  $this
    ->drupalPlaceBlock('system_branding_block', [
    'region' => 'header',
  ]);
  $this
    ->drupalGet('');
  $elements = $this
    ->xpath('//header//a[@rel=:rel]/img', array(
    ':rel' => 'home',
  ));
  $this
    ->assertEqual($elements[0]['src'], file_create_url($uploaded_filename));
  $this->container
    ->get('theme_handler')
    ->install(array(
    'bartik',
  ));
  $this
    ->drupalGet('admin/appearance/settings/bartik');

  // The logo field should only be present on the global theme settings form.
  $this
    ->assertNoFieldByName('logo_path');
  $this
    ->drupalPostForm(NULL, [], t('Save configuration'));

  // Ensure only valid themes are listed in the local tasks.
  $this
    ->drupalPlaceBlock('local_tasks_block', [
    'region' => 'header',
  ]);
  $this
    ->drupalGet('admin/appearance/settings');
  $theme_handler = \Drupal::service('theme_handler');
  $this
    ->assertLink($theme_handler
    ->getName('classy'));
  $this
    ->assertLink($theme_handler
    ->getName('bartik'));
  $this
    ->assertNoLink($theme_handler
    ->getName('stable'));

  // If a hidden theme is an admin theme it should be viewable.
  \Drupal::configFactory()
    ->getEditable('system.theme')
    ->set('admin', 'stable')
    ->save();
  \Drupal::service('router.builder')
    ->rebuildIfNeeded();
  $this
    ->drupalPlaceBlock('local_tasks_block', [
    'region' => 'header',
    'theme' => 'stable',
  ]);
  $this
    ->drupalGet('admin/appearance/settings');
  $this
    ->assertLink($theme_handler
    ->getName('stable'));
  $this
    ->drupalGet('admin/appearance/settings/stable');
  $this
    ->assertResponse(200, 'The theme settings form URL for a hidden theme that is the admin theme is available.');
}