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. 10 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()

Test 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';
    $supported_paths = [
        // Raw stream wrapper URI.
$file->uri => [
            'form' => StreamWrapperManager::getTarget($file->uri),
            'src' => file_url_transform_relative(file_create_url($file->uri)),
        ],
        // Relative path within the public filesystem.
StreamWrapperManager::getTarget($file->uri) => [
            'form' => StreamWrapperManager::getTarget($file->uri),
            'src' => file_url_transform_relative(file_create_url($file->uri)),
        ],
        // Relative path to a public file.
$file_relative => [
            'form' => $file_relative,
            'src' => file_url_transform_relative(file_create_url($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->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', [
            ':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 (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;
        }
        $this->assertEqual($elements[0]->getText(), $implicit_public_file);
        $this->assertEqual($elements[1]->getText(), $explicit_file);
        $this->assertEqual($elements[2]->getText(), $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', [
            ':rel' => 'home',
        ]);
        $this->assertEqual($elements[0]->getAttribute('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->drupalPostForm(NULL, $edit, t('Save configuration'));
        $this->assertText('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->drupalPostForm('admin/appearance/settings', $edit, t('Save configuration'));
    $fields = $this->xpath($this->constructFieldXpath('name', 'logo_path'));
    $uploaded_filename = 'public://' . $fields[0]->getValue();
    $this->drupalPlaceBlock('system_branding_block', [
        'region' => 'header',
    ]);
    $this->drupalGet('');
    $elements = $this->xpath('//header//a[@rel=:rel]/img', [
        ':rel' => 'home',
    ]);
    $this->assertEqual($elements[0]->getAttribute('src'), file_url_transform_relative(file_create_url($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->drupalPostForm('admin/appearance/settings', $edit, 'Save configuration');
    $this->assertNoText('The custom logo path is invalid.');
    $this->assertNoText('The custom favicon path is invalid.');
}

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