View source
<?php
namespace Drupal\Tests\color\Functional;
use Drupal\Tests\BrowserTestBase;
class ColorTest extends BrowserTestBase {
protected static $modules = [
'color',
'color_test',
'block',
'file',
];
protected $defaultTheme = 'stark';
protected $bigUser;
protected $themes;
protected $colorTests;
protected function setUp() : void {
parent::setUp();
$this->bigUser = $this
->drupalCreateUser([
'administer themes',
]);
$this->themes = [
'bartik' => [
'palette_input' => 'palette[bg]',
'scheme' => 'slate',
'scheme_color' => '#3b3b3b',
],
'color_test_theme' => [
'palette_input' => 'palette[bg]',
'scheme' => 'custom',
'scheme_color' => '#3b3b3b',
],
];
\Drupal::service('theme_installer')
->install(array_keys($this->themes));
$this->colorTests = [
'#000' => TRUE,
'#123456' => TRUE,
'#abcdef' => TRUE,
'#0' => FALSE,
'#00' => FALSE,
'#0000' => FALSE,
'#00000' => FALSE,
'123456' => FALSE,
'#00000g' => FALSE,
];
}
public function testColor() {
foreach ($this->themes as $theme => $test_values) {
$this
->_testColor($theme, $test_values);
}
}
public function _testColor($theme, $test_values) {
$this
->config('system.theme')
->set('default', $theme)
->save();
$settings_path = 'admin/appearance/settings/' . $theme;
$this
->drupalLogin($this->bigUser);
$this
->drupalGet($settings_path);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContainsOnce('Color set');
$edit['scheme'] = '';
$edit[$test_values['palette_input']] = '#123456';
$this
->drupalGet($settings_path);
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('<front>');
$stylesheets = $this
->config('color.theme.' . $theme)
->get('stylesheets');
$file_url_generator = \Drupal::service('file_url_generator');
foreach ($stylesheets as $stylesheet) {
$this
->assertSession()
->responseMatches('|' . $file_url_generator
->generateString($stylesheet) . '|');
$stylesheet_content = implode("\n", file($stylesheet));
$this
->assertStringContainsString('color: #123456', $stylesheet_content, 'Make sure the color we changed is in the color stylesheet. (' . $theme . ')');
}
$this
->drupalGet($settings_path);
$this
->assertSession()
->statusCodeEquals(200);
$edit['scheme'] = $test_values['scheme'];
$this
->drupalGet($settings_path);
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('<front>');
$stylesheets = $this
->config('color.theme.' . $theme)
->get('stylesheets');
foreach ($stylesheets as $stylesheet) {
$stylesheet_content = implode("\n", file($stylesheet));
$this
->assertStringContainsString('color: ' . $test_values['scheme_color'], $stylesheet_content, 'Make sure the color we changed is in the color stylesheet. (' . $theme . ')');
}
$config = $this
->config('system.performance');
$config
->set('css.preprocess', 1);
$config
->save();
$this
->drupalGet('<front>');
$stylesheets = \Drupal::state()
->get('drupal_css_cache_files', []);
$stylesheet_content = '';
foreach ($stylesheets as $uri) {
$stylesheet_content .= implode("\n", file(\Drupal::service('file_system')
->realpath($uri)));
}
$this
->assertStringNotContainsString('public://', $stylesheet_content, 'Make sure the color paths have been translated to local paths. (' . $theme . ')');
$config
->set('css.preprocess', 0);
$config
->save();
}
public function testValidColor() {
$this
->config('system.theme')
->set('default', 'bartik')
->save();
$settings_path = 'admin/appearance/settings/bartik';
$this
->drupalLogin($this->bigUser);
$edit['scheme'] = '';
foreach ($this->colorTests as $color => $is_valid) {
$edit['palette[bg]'] = $color;
$this
->drupalGet($settings_path);
$this
->submitForm($edit, 'Save configuration');
if ($is_valid) {
$this
->assertSession()
->pageTextContains('The configuration options have been saved.');
}
else {
$this
->assertSession()
->pageTextContains('You must enter a valid hexadecimal color value for Main background.');
}
}
}
public function testLogoSettingOverride() {
$this
->drupalLogin($this->bigUser);
$edit = [
'default_logo' => FALSE,
'logo_path' => 'core/misc/druplicon.png',
];
$this
->drupalGet('admin/appearance/settings');
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('admin/appearance/settings/bartik');
$this
->assertSame($GLOBALS['base_path'] . 'core/misc/druplicon.png', $this
->getDrupalSettings()['color']['logo']);
}
public function testOverrideAndResetScheme() {
$settings_path = 'admin/appearance/settings/bartik';
$this
->config('system.theme')
->set('default', 'bartik')
->save();
$this
->drupalPlaceBlock('system_branding_block', [
'region' => 'header',
]);
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains('files/color/bartik-');
$this
->assertSession()
->responseContains('bartik/logo.svg');
$this
->drupalLogin($this->bigUser);
$edit['scheme'] = 'slate';
$this
->drupalGet($settings_path);
$this
->submitForm($edit, 'Save configuration');
$this
->drupalLogout();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('files/color/bartik-');
$this
->assertSession()
->responseNotContains('bartik/logo.svg');
$this
->drupalLogin($this->bigUser);
$edit['scheme'] = 'default';
$this
->drupalGet($settings_path);
$this
->submitForm($edit, 'Save configuration');
$this
->drupalLogout();
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains('files/color/bartik-');
$this
->assertSession()
->responseContains('bartik/logo.svg');
}
}