View source
<?php
namespace Drupal\Tests\styleswitcher\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
class MultiThemeTest extends BrowserTestBase {
use AssertPageCacheContextsAndTagsTrait;
use AssertTrait;
use HelperTrait;
protected static $modules = [
'styleswitcher',
'styleswitcher_test_theme_per_page',
'block',
'dynamic_page_cache',
'page_cache',
];
protected $defaultTheme = 'classy';
protected $themeStyles;
protected function setUp() {
parent::setUp();
$this->themeStyles = [
'styleswitcher_test_theme' => [
'back' => [
'Default',
'Yellow on Purple',
'Green text',
'Blue back',
],
'front' => [
'Yellow on Purple',
'Green text',
'Blue back',
],
],
'styleswitcher_test_base_theme' => [
'back' => [
'Default',
'Yellow on Purple',
],
'front' => [
'Default',
'Yellow on Purple',
],
],
'stark' => [
'back' => [
'Default',
],
'front' => [],
],
];
$this
->enablePageCaching();
$this
->drupalPlaceBlock('local_tasks_block', [
'id' => 'local_tasks_block',
]);
\Drupal::service('theme_installer')
->install(array_keys($this->themeStyles));
foreach (array_keys($this->themeStyles) as $theme) {
$this
->drupalPlaceBlock('styleswitcher_styleswitcher', [
'id' => 'styleswitcher_' . $theme,
'theme' => $theme,
]);
}
}
public function testConfigThemeForms() {
$assert = $this
->assertSession();
$user = $this
->drupalCreateUser([
'administer styleswitcher',
]);
$this
->drupalLogin($user);
foreach ($this->themeStyles as $theme => $styles) {
$styles = $styles['back'];
$this
->drupalGet('admin/config/user-interface/styleswitcher');
$tabs = $this
->getSession()
->getPage()
->find('css', '#block-local-tasks-block');
$theme_config_path = '/admin/config/user-interface/styleswitcher/settings/' . $theme;
$tab = $assert
->elementExists('css', 'a[href$="' . $theme_config_path . '"]', $tabs);
$tab
->click();
$form = $assert
->elementExists('css', 'form#styleswitcher-config-theme');
$rows = $form
->findAll('css', '#styleswitcher-styles-table tbody tr');
$this
->assertCount(count($styles), $rows);
foreach ($styles as $i => $label) {
$label_cell = $rows[$i]
->find('css', 'td:first-child');
if (floatval(\Drupal::VERSION) >= 8.800000000000001) {
$this
->assertStringContainsString($label, $label_cell
->getText());
}
else {
$this
->assertContains($label, $label_cell
->getText());
}
}
}
}
public function testBlockLinks() {
foreach ($this->themeStyles as $theme => $styles) {
$page_path = 'styleswitcher-test-theme-per-page/' . $theme;
$block_selector = '#block-styleswitcher-' . strtr($theme, '_', '-');
$this
->assertStylesList($styles['front'], $block_selector, $page_path);
}
}
public function testDynamicLink() {
$assert = $this
->assertSession();
foreach (array_keys($this->themeStyles) as $theme) {
$this
->drupalGet('styleswitcher-test-theme-per-page/' . $theme);
$link = $assert
->elementExists('css', 'head > link:last-child[rel="stylesheet"]');
if (floatval(\Drupal::VERSION) >= 8.800000000000001) {
$this
->assertStringContainsString('/styleswitcher/css/' . $theme, $link
->getAttribute('href'));
}
else {
$this
->assertContains('/styleswitcher/css/' . $theme, $link
->getAttribute('href'));
}
}
}
public function testNoJsStyleSwitching() {
$assert = $this
->assertSession();
$this
->drupalGet('styleswitcher-test-theme-per-page/styleswitcher_test_theme');
$this
->checkStyleFullyApplied('blue_back', 'styleswitcher_test_theme', TRUE);
$this
->click('a.style-switcher.style-green-text');
$assert
->addressEquals('styleswitcher-test-theme-per-page/styleswitcher_test_theme');
$this
->checkStyleFullyApplied('green_text', 'styleswitcher_test_theme');
$this
->click('a.style-switcher.style-yellow-on-purple');
$this
->checkStyleFullyApplied('yellow_on_purple', 'styleswitcher_test_theme');
$this
->drupalGet('styleswitcher-test-theme-per-page/styleswitcher_test_base_theme');
$this
->checkStyleFullyApplied('default', 'styleswitcher_test_base_theme', TRUE);
$this
->click('a.style-switcher.style-yellow-on-purple');
$assert
->addressEquals('styleswitcher-test-theme-per-page/styleswitcher_test_base_theme');
$this
->checkStyleFullyApplied('yellow_on_purple', 'styleswitcher_test_base_theme');
$this
->click('a.style-switcher.style-default');
$this
->checkStyleFullyApplied('default', 'styleswitcher_test_base_theme');
$this
->drupalGet('styleswitcher-test-theme-per-page/styleswitcher_test_theme');
$this
->checkStyleFullyApplied('yellow_on_purple', 'styleswitcher_test_theme');
}
protected function checkStyleFullyApplied(string $name, string $theme, bool $no_cookie = FALSE) {
$styles = [
'default' => [
'type' => 'custom',
'css_suffix' => 'styleswitcher/css/' . $theme,
],
'yellow_on_purple' => [
'type' => 'theme',
'css_suffix' => '/yellow-on-purple.css',
],
'green_text' => [
'type' => 'theme',
'css_suffix' => '/green-fg.css',
],
'blue_back' => [
'type' => 'theme',
'css_suffix' => '/blue-bg.css',
],
];
$session = $this
->getSession();
$cookie = $no_cookie ? NULL : $styles[$name]['type'] . '/' . $name;
$this
->assertSame($cookie, $session
->getCookie('styleswitcher[' . $theme . ']'));
$this
->assertActiveStylePath($styles[$name]['css_suffix'], $theme);
$this
->drupalGet('styleswitcher-test-theme-per-page/' . $theme);
}
}