Version 1
[yaffs-website] / web / core / modules / color / tests / src / Functional / ColorTest.php
1 <?php
2
3 namespace Drupal\Tests\color\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Modify the Bartik theme colors and make sure the changes are reflected on the
9  * frontend.
10  *
11  * @group color
12  */
13 class ColorTest extends BrowserTestBase {
14
15   /**
16    * Modules to install.
17    *
18    * @var array
19    */
20   public static $modules = ['color', 'color_test', 'block', 'file'];
21
22   /**
23    * A user with administrative permissions.
24    *
25    * @var \Drupal\user\UserInterface
26    */
27   protected $bigUser;
28
29   /**
30    * An associative array of settings for themes.
31    *
32    * @var array
33    */
34   protected $themes;
35
36   /**
37    * Associative array of hex color strings to test.
38    *
39    * Keys are the color string and values are a Boolean set to TRUE for valid
40    * colors.
41    *
42    * @var array
43    */
44   protected $colorTests;
45
46   /**
47    * {@inheritdoc}
48    */
49   protected function setUp() {
50     parent::setUp();
51
52     // Create user.
53     $this->bigUser = $this->drupalCreateUser(['administer themes']);
54
55     // This tests the color module in Bartik.
56     $this->themes = [
57       'bartik' => [
58         'palette_input' => 'palette[bg]',
59         'scheme' => 'slate',
60         'scheme_color' => '#3b3b3b',
61       ],
62       'color_test_theme' => [
63         'palette_input' => 'palette[bg]',
64         'scheme' => 'custom',
65         'scheme_color' => '#3b3b3b',
66       ],
67     ];
68     \Drupal::service('theme_handler')->install(array_keys($this->themes));
69
70     // Array filled with valid and not valid color values.
71     $this->colorTests = [
72       '#000' => TRUE,
73       '#123456' => TRUE,
74       '#abcdef' => TRUE,
75       '#0' => FALSE,
76       '#00' => FALSE,
77       '#0000' => FALSE,
78       '#00000' => FALSE,
79       '123456' => FALSE,
80       '#00000g' => FALSE,
81     ];
82   }
83
84   /**
85    * Tests the Color module functionality.
86    */
87   public function testColor() {
88     foreach ($this->themes as $theme => $test_values) {
89       $this->_testColor($theme, $test_values);
90     }
91   }
92
93   /**
94    * Tests the Color module functionality using the given theme.
95    *
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.
101    */
102   public function _testColor($theme, $test_values) {
103     $this->config('system.theme')
104       ->set('default', $theme)
105       ->save();
106     $settings_path = 'admin/appearance/settings/' . $theme;
107
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'));
115
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 . ')');
122     }
123
124     $this->drupalGet($settings_path);
125     $this->assertResponse(200);
126     $edit['scheme'] = $test_values['scheme'];
127     $this->drupalPostForm($settings_path, $edit, t('Save configuration'));
128
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 . ')');
134     }
135
136     // Test with aggregated CSS turned on.
137     $config = $this->config('system.performance');
138     $config->set('css.preprocess', 1);
139     $config->save();
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)));
145     }
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);
148     $config->save();
149   }
150
151   /**
152    * Tests whether the provided color is valid.
153    */
154   public function testValidColor() {
155     $this->config('system.theme')
156       ->set('default', 'bartik')
157       ->save();
158     $settings_path = 'admin/appearance/settings/bartik';
159
160     $this->drupalLogin($this->bigUser);
161     $edit['scheme'] = '';
162
163     foreach ($this->colorTests as $color => $is_valid) {
164       $edit['palette[bg]'] = $color;
165       $this->drupalPostForm($settings_path, $edit, t('Save configuration'));
166
167       if ($is_valid) {
168         $this->assertText('The configuration options have been saved.');
169       }
170       else {
171         $this->assertText('You must enter a valid hexadecimal color value for Main background.');
172       }
173     }
174   }
175
176   /**
177    * Test whether the custom logo is used in the color preview.
178    */
179   public function testLogoSettingOverride() {
180     $this->drupalLogin($this->bigUser);
181     $edit = [
182       'default_logo' => FALSE,
183       'logo_path' => 'core/misc/druplicon.png',
184     ];
185     $this->drupalPostForm('admin/appearance/settings', $edit, t('Save configuration'));
186
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']);
190   }
191
192   /**
193    * Test whether the scheme can be set, viewed anonymously and reset.
194    */
195   public function testOverrideAndResetScheme() {
196     $settings_path = 'admin/appearance/settings/bartik';
197     $this->config('system.theme')
198       ->set('default', 'bartik')
199       ->save();
200
201     // Place branding block with site name and slogan into header region.
202     $this->drupalPlaceBlock('system_branding_block', ['region' => 'header']);
203
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.');
207
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'));
212
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.');
218
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'));
223
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.');
229   }
230
231 }