ColorTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. namespace Drupal\Tests\color\Functional;
  3. use Drupal\Tests\BrowserTestBase;
  4. /**
  5. * Modify the Bartik theme colors and make sure the changes are reflected on the
  6. * frontend.
  7. *
  8. * @group color
  9. */
  10. class ColorTest extends BrowserTestBase {
  11. /**
  12. * Modules to install.
  13. *
  14. * @var array
  15. */
  16. public static $modules = ['color', 'color_test', 'block', 'file'];
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected $defaultTheme = 'stark';
  21. /**
  22. * A user with administrative permissions.
  23. *
  24. * @var \Drupal\user\UserInterface
  25. */
  26. protected $bigUser;
  27. /**
  28. * An associative array of settings for themes.
  29. *
  30. * @var array
  31. */
  32. protected $themes;
  33. /**
  34. * Associative array of hex color strings to test.
  35. *
  36. * Keys are the color string and values are a Boolean set to TRUE for valid
  37. * colors.
  38. *
  39. * @var array
  40. */
  41. protected $colorTests;
  42. /**
  43. * {@inheritdoc}
  44. */
  45. protected function setUp() {
  46. parent::setUp();
  47. // Create user.
  48. $this->bigUser = $this->drupalCreateUser(['administer themes']);
  49. // This tests the color module in Bartik.
  50. $this->themes = [
  51. 'bartik' => [
  52. 'palette_input' => 'palette[bg]',
  53. 'scheme' => 'slate',
  54. 'scheme_color' => '#3b3b3b',
  55. ],
  56. 'color_test_theme' => [
  57. 'palette_input' => 'palette[bg]',
  58. 'scheme' => 'custom',
  59. 'scheme_color' => '#3b3b3b',
  60. ],
  61. ];
  62. \Drupal::service('theme_installer')->install(array_keys($this->themes));
  63. // Array filled with valid and not valid color values.
  64. $this->colorTests = [
  65. '#000' => TRUE,
  66. '#123456' => TRUE,
  67. '#abcdef' => TRUE,
  68. '#0' => FALSE,
  69. '#00' => FALSE,
  70. '#0000' => FALSE,
  71. '#00000' => FALSE,
  72. '123456' => FALSE,
  73. '#00000g' => FALSE,
  74. ];
  75. }
  76. /**
  77. * Tests the Color module functionality.
  78. */
  79. public function testColor() {
  80. foreach ($this->themes as $theme => $test_values) {
  81. $this->_testColor($theme, $test_values);
  82. }
  83. }
  84. /**
  85. * Tests the Color module functionality using the given theme.
  86. *
  87. * @param string $theme
  88. * The machine name of the theme being tested.
  89. * @param array $test_values
  90. * An associative array of test settings (i.e. 'Main background', 'Text
  91. * color', 'Color set', etc) for the theme which being tested.
  92. */
  93. public function _testColor($theme, $test_values) {
  94. $this->config('system.theme')
  95. ->set('default', $theme)
  96. ->save();
  97. $settings_path = 'admin/appearance/settings/' . $theme;
  98. $this->drupalLogin($this->bigUser);
  99. $this->drupalGet($settings_path);
  100. $this->assertSession()->statusCodeEquals(200);
  101. $this->assertUniqueText('Color set');
  102. $edit['scheme'] = '';
  103. $edit[$test_values['palette_input']] = '#123456';
  104. $this->drupalPostForm($settings_path, $edit, t('Save configuration'));
  105. $this->drupalGet('<front>');
  106. $stylesheets = $this->config('color.theme.' . $theme)->get('stylesheets');
  107. // Make sure the color stylesheet is included in the content.
  108. foreach ($stylesheets as $stylesheet) {
  109. $this->assertPattern('|' . file_url_transform_relative(file_create_url($stylesheet)) . '|');
  110. $stylesheet_content = implode("\n", file($stylesheet));
  111. $this->assertStringContainsString('color: #123456', $stylesheet_content, 'Make sure the color we changed is in the color stylesheet. (' . $theme . ')');
  112. }
  113. $this->drupalGet($settings_path);
  114. $this->assertSession()->statusCodeEquals(200);
  115. $edit['scheme'] = $test_values['scheme'];
  116. $this->drupalPostForm($settings_path, $edit, t('Save configuration'));
  117. $this->drupalGet('<front>');
  118. $stylesheets = $this->config('color.theme.' . $theme)->get('stylesheets');
  119. foreach ($stylesheets as $stylesheet) {
  120. $stylesheet_content = implode("\n", file($stylesheet));
  121. $this->assertStringContainsString('color: ' . $test_values['scheme_color'], $stylesheet_content, 'Make sure the color we changed is in the color stylesheet. (' . $theme . ')');
  122. }
  123. // Test with aggregated CSS turned on.
  124. $config = $this->config('system.performance');
  125. $config->set('css.preprocess', 1);
  126. $config->save();
  127. $this->drupalGet('<front>');
  128. $stylesheets = \Drupal::state()->get('drupal_css_cache_files') ?: [];
  129. $stylesheet_content = '';
  130. foreach ($stylesheets as $uri) {
  131. $stylesheet_content .= implode("\n", file(\Drupal::service('file_system')->realpath($uri)));
  132. }
  133. $this->assertStringNotContainsString('public://', $stylesheet_content, 'Make sure the color paths have been translated to local paths. (' . $theme . ')');
  134. $config->set('css.preprocess', 0);
  135. $config->save();
  136. }
  137. /**
  138. * Tests whether the provided color is valid.
  139. */
  140. public function testValidColor() {
  141. $this->config('system.theme')
  142. ->set('default', 'bartik')
  143. ->save();
  144. $settings_path = 'admin/appearance/settings/bartik';
  145. $this->drupalLogin($this->bigUser);
  146. $edit['scheme'] = '';
  147. foreach ($this->colorTests as $color => $is_valid) {
  148. $edit['palette[bg]'] = $color;
  149. $this->drupalPostForm($settings_path, $edit, t('Save configuration'));
  150. if ($is_valid) {
  151. $this->assertText('The configuration options have been saved.');
  152. }
  153. else {
  154. $this->assertText('You must enter a valid hexadecimal color value for Main background.');
  155. }
  156. }
  157. }
  158. /**
  159. * Test whether the custom logo is used in the color preview.
  160. */
  161. public function testLogoSettingOverride() {
  162. $this->drupalLogin($this->bigUser);
  163. $edit = [
  164. 'default_logo' => FALSE,
  165. 'logo_path' => 'core/misc/druplicon.png',
  166. ];
  167. $this->drupalPostForm('admin/appearance/settings', $edit, t('Save configuration'));
  168. // Ensure that the overridden logo is present in Bartik, which is colorable.
  169. $this->drupalGet('admin/appearance/settings/bartik');
  170. $this->assertIdentical($GLOBALS['base_path'] . 'core/misc/druplicon.png', $this->getDrupalSettings()['color']['logo']);
  171. }
  172. /**
  173. * Test whether the scheme can be set, viewed anonymously and reset.
  174. */
  175. public function testOverrideAndResetScheme() {
  176. $settings_path = 'admin/appearance/settings/bartik';
  177. $this->config('system.theme')
  178. ->set('default', 'bartik')
  179. ->save();
  180. // Place branding block with site name and slogan into header region.
  181. $this->drupalPlaceBlock('system_branding_block', ['region' => 'header']);
  182. $this->drupalGet('');
  183. $this->assertNoRaw('files/color/bartik-', 'Make sure the color logo is not being used.');
  184. $this->assertRaw('bartik/logo.svg', 'Make sure the original bartik logo exists.');
  185. // Log in and set the color scheme to 'slate'.
  186. $this->drupalLogin($this->bigUser);
  187. $edit['scheme'] = 'slate';
  188. $this->drupalPostForm($settings_path, $edit, t('Save configuration'));
  189. // Visit the homepage and ensure color changes.
  190. $this->drupalLogout();
  191. $this->drupalGet('');
  192. $this->assertRaw('files/color/bartik-', 'Make sure the color logo is being used.');
  193. $this->assertNoRaw('bartik/logo.svg', 'Make sure the original bartik logo does not exist.');
  194. // Log in and set the color scheme back to default (delete config).
  195. $this->drupalLogin($this->bigUser);
  196. $edit['scheme'] = 'default';
  197. $this->drupalPostForm($settings_path, $edit, t('Save configuration'));
  198. // Log out and ensure there is no color and we have the original logo.
  199. $this->drupalLogout();
  200. $this->drupalGet('');
  201. $this->assertNoRaw('files/color/bartik-', 'Make sure the color logo is not being used.');
  202. $this->assertRaw('bartik/logo.svg', 'Make sure the original bartik logo exists.');
  203. }
  204. }