3 namespace Drupal\Tests\color\Functional;
5 use Drupal\Tests\BrowserTestBase;
8 * Modify the Bartik theme colors and make sure the changes are reflected on the
13 class ColorTest extends BrowserTestBase {
20 public static $modules = ['color', 'color_test', 'block', 'file'];
23 * A user with administrative permissions.
25 * @var \Drupal\user\UserInterface
30 * An associative array of settings for themes.
37 * Associative array of hex color strings to test.
39 * Keys are the color string and values are a Boolean set to TRUE for valid
44 protected $colorTests;
49 protected function setUp() {
53 $this->bigUser = $this->drupalCreateUser(['administer themes']);
55 // This tests the color module in Bartik.
58 'palette_input' => 'palette[bg]',
60 'scheme_color' => '#3b3b3b',
62 'color_test_theme' => [
63 'palette_input' => 'palette[bg]',
65 'scheme_color' => '#3b3b3b',
68 \Drupal::service('theme_handler')->install(array_keys($this->themes));
70 // Array filled with valid and not valid color values.
85 * Tests the Color module functionality.
87 public function testColor() {
88 foreach ($this->themes as $theme => $test_values) {
89 $this->_testColor($theme, $test_values);
94 * Tests the Color module functionality using the given theme.
96 * @param string $theme
97 * The machine name of the theme being tested.
98 * @param array $test_values
99 * An associative array of test settings (i.e. 'Main background', 'Text
100 * color', 'Color set', etc) for the theme which being tested.
102 public function _testColor($theme, $test_values) {
103 $this->config('system.theme')
104 ->set('default', $theme)
106 $settings_path = 'admin/appearance/settings/' . $theme;
108 $this->drupalLogin($this->bigUser);
109 $this->drupalGet($settings_path);
110 $this->assertResponse(200);
111 $this->assertUniqueText('Color set');
112 $edit['scheme'] = '';
113 $edit[$test_values['palette_input']] = '#123456';
114 $this->drupalPostForm($settings_path, $edit, t('Save configuration'));
116 $this->drupalGet('<front>');
117 $stylesheets = $this->config('color.theme.' . $theme)->get('stylesheets');
118 foreach ($stylesheets as $stylesheet) {
119 $this->assertPattern('|' . file_url_transform_relative(file_create_url($stylesheet)) . '|', 'Make sure the color stylesheet is included in the content. (' . $theme . ')');
120 $stylesheet_content = join("\n", file($stylesheet));
121 $this->assertTrue(strpos($stylesheet_content, 'color: #123456') !== FALSE, 'Make sure the color we changed is in the color stylesheet. (' . $theme . ')');
124 $this->drupalGet($settings_path);
125 $this->assertResponse(200);
126 $edit['scheme'] = $test_values['scheme'];
127 $this->drupalPostForm($settings_path, $edit, t('Save configuration'));
129 $this->drupalGet('<front>');
130 $stylesheets = $this->config('color.theme.' . $theme)->get('stylesheets');
131 foreach ($stylesheets as $stylesheet) {
132 $stylesheet_content = join("\n", file($stylesheet));
133 $this->assertTrue(strpos($stylesheet_content, 'color: ' . $test_values['scheme_color']) !== FALSE, 'Make sure the color we changed is in the color stylesheet. (' . $theme . ')');
136 // Test with aggregated CSS turned on.
137 $config = $this->config('system.performance');
138 $config->set('css.preprocess', 1);
140 $this->drupalGet('<front>');
141 $stylesheets = \Drupal::state()->get('drupal_css_cache_files') ?: [];
142 $stylesheet_content = '';
143 foreach ($stylesheets as $uri) {
144 $stylesheet_content .= join("\n", file(drupal_realpath($uri)));
146 $this->assertTrue(strpos($stylesheet_content, 'public://') === FALSE, 'Make sure the color paths have been translated to local paths. (' . $theme . ')');
147 $config->set('css.preprocess', 0);
152 * Tests whether the provided color is valid.
154 public function testValidColor() {
155 $this->config('system.theme')
156 ->set('default', 'bartik')
158 $settings_path = 'admin/appearance/settings/bartik';
160 $this->drupalLogin($this->bigUser);
161 $edit['scheme'] = '';
163 foreach ($this->colorTests as $color => $is_valid) {
164 $edit['palette[bg]'] = $color;
165 $this->drupalPostForm($settings_path, $edit, t('Save configuration'));
168 $this->assertText('The configuration options have been saved.');
171 $this->assertText('You must enter a valid hexadecimal color value for Main background.');
177 * Test whether the custom logo is used in the color preview.
179 public function testLogoSettingOverride() {
180 $this->drupalLogin($this->bigUser);
182 'default_logo' => FALSE,
183 'logo_path' => 'core/misc/druplicon.png',
185 $this->drupalPostForm('admin/appearance/settings', $edit, t('Save configuration'));
187 // Ensure that the overridden logo is present in Bartik, which is colorable.
188 $this->drupalGet('admin/appearance/settings/bartik');
189 $this->assertIdentical($GLOBALS['base_path'] . 'core/misc/druplicon.png', $this->getDrupalSettings()['color']['logo']);
193 * Test whether the scheme can be set, viewed anonymously and reset.
195 public function testOverrideAndResetScheme() {
196 $settings_path = 'admin/appearance/settings/bartik';
197 $this->config('system.theme')
198 ->set('default', 'bartik')
201 // Place branding block with site name and slogan into header region.
202 $this->drupalPlaceBlock('system_branding_block', ['region' => 'header']);
204 $this->drupalGet('');
205 $this->assertNoRaw('files/color/bartik-', 'Make sure the color logo is not being used.');
206 $this->assertRaw('bartik/logo.svg', 'Make sure the original bartik logo exists.');
208 // Log in and set the color scheme to 'slate'.
209 $this->drupalLogin($this->bigUser);
210 $edit['scheme'] = 'slate';
211 $this->drupalPostForm($settings_path, $edit, t('Save configuration'));
213 // Visit the homepage and ensure color changes.
214 $this->drupalLogout();
215 $this->drupalGet('');
216 $this->assertRaw('files/color/bartik-', 'Make sure the color logo is being used.');
217 $this->assertNoRaw('bartik/logo.svg', 'Make sure the original bartik logo does not exist.');
219 // Log in and set the color scheme back to default (delete config).
220 $this->drupalLogin($this->bigUser);
221 $edit['scheme'] = 'default';
222 $this->drupalPostForm($settings_path, $edit, t('Save configuration'));
224 // Log out and ensure there is no color and we have the original logo.
225 $this->drupalLogout();
226 $this->drupalGet('');
227 $this->assertNoRaw('files/color/bartik-', 'Make sure the color logo is not being used.');
228 $this->assertRaw('bartik/logo.svg', 'Make sure the original bartik logo exists.');