You are here

public function ThemeTest::testThemeSettings in Drupal 9

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

Tests the theme settings form.

File

core/modules/system/tests/src/Functional/System/ThemeTest.php, line 61

Class

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

Namespace

Drupal\Tests\system\Functional\System

Code

public function testThemeSettings() {

  // Ensure a disabled theme settings form URL returns 404.
  $this
    ->drupalGet('admin/appearance/settings/bartik');
  $this
    ->assertSession()
    ->statusCodeEquals(404);

  // Ensure a non existent theme settings form URL returns 404.
  $this
    ->drupalGet('admin/appearance/settings/' . $this
    ->randomMachineName());
  $this
    ->assertSession()
    ->statusCodeEquals(404);

  // Ensure a hidden theme settings form URL returns 404.
  $this
    ->assertTrue(\Drupal::service('theme_installer')
    ->install([
    'stable',
  ]));
  $this
    ->drupalGet('admin/appearance/settings/stable');
  $this
    ->assertSession()
    ->statusCodeEquals(404);

  // Specify a filesystem path to be used for the logo.
  $file = current($this
    ->drupalGetTestFiles('image'));
  $file_relative = strtr($file->uri, [
    'public:/' => PublicStream::basePath(),
  ]);
  $default_theme_path = 'core/themes/classy';

  /** @var \Drupal\Core\File\FileUrlGeneratorInterface $file_url_generator */
  $file_url_generator = \Drupal::service('file_url_generator');
  $supported_paths = [
    // Raw stream wrapper URI.
    $file->uri => [
      'form' => StreamWrapperManager::getTarget($file->uri),
      'src' => $file_url_generator
        ->generateString($file->uri),
    ],
    // Relative path within the public filesystem.
    StreamWrapperManager::getTarget($file->uri) => [
      'form' => StreamWrapperManager::getTarget($file->uri),
      'src' => $file_url_generator
        ->generateString($file->uri),
    ],
    // Relative path to a public file.
    $file_relative => [
      'form' => $file_relative,
      'src' => $file_url_generator
        ->generateString($file->uri),
    ],
    // Relative path to an arbitrary file.
    'core/misc/druplicon.png' => [
      'form' => 'core/misc/druplicon.png',
      'src' => base_path() . 'core/misc/druplicon.png',
    ],
    // Relative path to a file in a theme.
    $default_theme_path . '/logo.svg' => [
      'form' => $default_theme_path . '/logo.svg',
      'src' => base_path() . $default_theme_path . '/logo.svg',
    ],
  ];
  foreach ($supported_paths as $input => $expected) {
    $edit = [
      'default_logo' => FALSE,
      'logo_path' => $input,
    ];
    $this
      ->drupalGet('admin/appearance/settings');
    $this
      ->submitForm($edit, 'Save configuration');
    $this
      ->assertSession()
      ->pageTextNotContains('The custom logo path is invalid.');
    $this
      ->assertSession()
      ->fieldValueEquals('logo_path', $expected['form']);

    // Verify logo path examples.
    // 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 (StreamWrapperManager::getScheme($input) == 'public') {
      $implicit_public_file = StreamWrapperManager::getTarget($input);
      $explicit_file = $input;
      $local_file = strtr($input, [
        'public:/' => PublicStream::basePath(),
      ]);
    }
    elseif (StreamWrapperManager::getScheme($input) !== FALSE) {
      $explicit_file = $input;
    }
    elseif ($input == StreamWrapperManager::getTarget($file->uri)) {
      $implicit_public_file = $input;
      $explicit_file = 'public://' . $input;
      $local_file = PublicStream::basePath() . '/' . $input;
    }
    $xpath = "//div[contains(@class, 'js-form-item-logo-path')]/div[@class='description']/code";
    $this
      ->assertSession()
      ->elementTextEquals('xpath', "{$xpath}[1]", $implicit_public_file);
    $this
      ->assertSession()
      ->elementTextEquals('xpath', "{$xpath}[2]", $explicit_file);
    $this
      ->assertSession()
      ->elementTextEquals('xpath', "{$xpath}[3]", $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('');
    $this
      ->assertSession()
      ->elementAttributeContains('xpath', '//header//a[@rel="home"]/img', 'src', $expected['src']);
  }
  $unsupported_paths = [
    // 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::service('file_system')
      ->realpath($file->uri),
  ];
  $this
    ->drupalGet('admin/appearance/settings');
  foreach ($unsupported_paths as $path) {
    $edit = [
      'default_logo' => FALSE,
      'logo_path' => $path,
    ];
    $this
      ->submitForm($edit, 'Save configuration');
    $this
      ->assertSession()
      ->pageTextContains('The custom logo path is invalid.');
  }

  // Upload a file to use for the logo.
  $edit = [
    'default_logo' => FALSE,
    'logo_path' => '',
    'files[logo_upload]' => \Drupal::service('file_system')
      ->realpath($file->uri),
  ];
  $this
    ->drupalGet('admin/appearance/settings');
  $this
    ->submitForm($edit, 'Save configuration');
  $uploaded_filename = 'public://' . $this
    ->getSession()
    ->getPage()
    ->findField('logo_path')
    ->getValue();
  $this
    ->drupalPlaceBlock('system_branding_block', [
    'region' => 'header',
  ]);
  $this
    ->drupalGet('');
  $this
    ->assertSession()
    ->elementAttributeContains('xpath', '//header//a[@rel="home"]/img', 'src', $file_url_generator
    ->generateString($uploaded_filename));
  $this->container
    ->get('theme_installer')
    ->install([
    'bartik',
  ]);

  // 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
    ->assertSession()
    ->linkExists($theme_handler
    ->getName('classy'));
  $this
    ->assertSession()
    ->linkExists($theme_handler
    ->getName('bartik'));
  $this
    ->assertSession()
    ->linkNotExists($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
    ->assertSession()
    ->linkExists($theme_handler
    ->getName('stable'));
  $this
    ->drupalGet('admin/appearance/settings/stable');
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Ensure default logo and favicons are not triggering custom path
  // validation errors if their custom paths are set on the form.
  $edit = [
    'default_logo' => TRUE,
    'logo_path' => 'public://whatever.png',
    'default_favicon' => TRUE,
    'favicon_path' => 'public://whatever.ico',
  ];
  $this
    ->drupalGet('admin/appearance/settings');
  $this
    ->submitForm($edit, 'Save configuration');
  $this
    ->assertSession()
    ->pageTextNotContains('The custom logo path is invalid.');
  $this
    ->assertSession()
    ->pageTextNotContains('The custom favicon path is invalid.');
}