function ThemeTest::testThemeSettings

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/System/ThemeTest.php \Drupal\Tests\system\Functional\System\ThemeTest::testThemeSettings()
  2. 8.9.x core/modules/system/tests/src/Functional/System/ThemeTest.php \Drupal\Tests\system\Functional\System\ThemeTest::testThemeSettings()
  3. 11.x 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 71

Class

ThemeTest
Tests the theme administration user interface.

Namespace

Drupal\Tests\system\Functional\System

Code

public function testThemeSettings() : void {
    // Ensure a disabled theme settings form URL returns 404.
    $this->drupalGet('admin/appearance/settings/olivero');
    $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([
        'stable9',
    ]));
    $this->drupalGet('admin/appearance/settings/stable9');
    $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/starterkit_theme';
    
    /** @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. Try both the test image we've been
    // using so far and an SVG file.
    $upload_uris = [
        $file->uri,
        'core/themes/olivero/logo.svg',
    ];
    $this->drupalPlaceBlock('system_branding_block', [
        'region' => 'header',
    ]);
    foreach ($upload_uris as $upload_uri) {
        $edit = [
            'default_logo' => FALSE,
            'logo_path' => '',
            'files[logo_upload]' => \Drupal::service('file_system')->realpath($upload_uri),
        ];
        $this->drupalGet('admin/appearance/settings');
        $this->submitForm($edit, 'Save configuration');
        $this->assertSession()
            ->pageTextContains('The configuration options have been saved.');
        $uploaded_filename = 'public://' . $this->getSession()
            ->getPage()
            ->findField('logo_path')
            ->getValue();
        $this->drupalGet('');
        $this->assertSession()
            ->elementAttributeContains('xpath', '//header//a[@rel="home"]/img', 'src', $file_url_generator->generateString($uploaded_filename));
        // Clear the logo or it will use previous value.
        $edit = [
            'default_logo' => FALSE,
            'logo_path' => '',
            'files[logo_upload]' => '',
        ];
        $this->drupalGet('admin/appearance/settings');
        $this->submitForm($edit, 'Save configuration');
    }
    $this->container
        ->get('theme_installer')
        ->install([
        'olivero',
    ]);
    // 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('starterkit_theme'));
    $this->assertSession()
        ->linkExists($theme_handler->getName('olivero'));
    $this->assertSession()
        ->linkNotExists($theme_handler->getName('stable9'));
    // If a hidden theme is an admin theme it should be viewable.
    \Drupal::configFactory()->getEditable('system.theme')
        ->set('admin', 'stable9')
        ->save();
    \Drupal::service('router.builder')->rebuildIfNeeded();
    $this->drupalPlaceBlock('local_tasks_block', [
        'region' => 'header',
        'theme' => 'stable9',
    ]);
    $this->drupalGet('admin/appearance/settings');
    $this->assertSession()
        ->linkExists($theme_handler->getName('stable9'));
    $this->drupalGet('admin/appearance/settings/stable9');
    $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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.