commit 1879ce8bae1c68b3e26d50aee0baee0c4d2b9f38 Author: Joel Pittet Date: Mon Nov 30 23:33:04 2015 -0800 tests diff --git a/core/modules/color/color.module b/core/modules/color/color.module index 1cab4b0..c992b2a 100644 --- a/core/modules/color/color.module +++ b/core/modules/color/color.module @@ -9,7 +9,7 @@ use Drupal\Component\Utility\Bytes; use Drupal\Component\Utility\Environment; use Drupal\Core\Block\BlockPluginInterface; -use Drupal\Core\Cache\Cache; +use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Render\Element\Textfield; @@ -118,10 +118,13 @@ function color_block_view_system_branding_block_alter(array &$build, BlockPlugin */ function color_block_view_pre_render(array $build) { $theme_key = \Drupal::theme()->getActiveTheme()->getName(); - $build['#cache']['tags'][] = 'config:color.theme.' . $theme_key; + $config = \Drupal::config('color.theme.' . $theme_key); + $cacheable_metadata = CacheableMetadata::createFromRenderArray($build); + $cacheable_metadata->addCacheableDependency($config); + $cacheable_metadata->applyTo($build); // Override logo. - $logo = \Drupal::config('color.theme.' . $theme_key)->get('logo'); + $logo = $config->get('logo'); if ($logo && $build['content']['site_logo'] && preg_match('!' . $theme_key . '/logo.svg$!', $build['content']['site_logo']['#uri'])) { $build['content']['site_logo']['#uri'] = file_create_url($logo); } @@ -429,8 +432,6 @@ function color_scheme_form_submit($form, FormStateInterface $form_state) { // No change in color config, use the standard theme from color.inc. if (implode(',', color_get_palette($theme, TRUE)) == implode(',', $palette)) { $config->delete(); - // Clear the library cache. - Cache::invalidateTags(['library_info']); return; } @@ -502,9 +503,6 @@ function color_scheme_form_submit($form, FormStateInterface $form_state) { ->set('stylesheets', $css) ->set('files', $paths['files']) ->save(); - - // Clear the library cache. - Cache::invalidateTags(['library_info']); } /** diff --git a/core/modules/color/color.services.yml b/core/modules/color/color.services.yml new file mode 100644 index 0000000..03f96e7 --- /dev/null +++ b/core/modules/color/color.services.yml @@ -0,0 +1,6 @@ +services: + color.config_cache_tag: + class: Drupal\color\EventSubscriber\ConfigCacheTag + arguments: ['@cache_tags.invalidator'] + tags: + - { name: event_subscriber } diff --git a/core/modules/color/src/EventSubscriber/ConfigCacheTag.php b/core/modules/color/src/EventSubscriber/ConfigCacheTag.php new file mode 100644 index 0000000..2bddbb1 --- /dev/null +++ b/core/modules/color/src/EventSubscriber/ConfigCacheTag.php @@ -0,0 +1,61 @@ +cacheTagsInvalidator = $cache_tags_invalidator; + } + + /** + * Invalidate cache tags when a color theme config object changes. + * + * @param \Drupal\Core\Config\ConfigCrudEvent $event + * The Event to process. + */ + public function onChange(ConfigCrudEvent $event) { + // Changing the color theme settings may mean a different CSS or JavaScript + // library asset file. + if (strpos($event->getConfig()->getName(), 'color.theme.') === 0) { + $this->cacheTagsInvalidator->invalidateTags(['library_info']); + } + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + $events[ConfigEvents::SAVE][] = ['onChange']; + $events[ConfigEvents::DELETE][] = ['onChange']; + + return $events; + } + +} diff --git a/core/modules/color/src/Tests/ColorTest.php b/core/modules/color/src/Tests/ColorTest.php index 1d1b72e..750b2e8 100644 --- a/core/modules/color/src/Tests/ColorTest.php +++ b/core/modules/color/src/Tests/ColorTest.php @@ -22,7 +22,7 @@ class ColorTest extends WebTestBase { * * @var array */ - public static $modules = array('color', 'color_test'); + public static $modules = array('color', 'color_test', 'block'); /** * A user with administrative permissions. @@ -194,4 +194,43 @@ function testLogoSettingOverride() { $this->assertIdentical($GLOBALS['base_url'] . '/' . 'core/misc/druplicon.png', $this->getDrupalSettings()['color']['logo']); } + /** + * Test whether the scheme can be set, viewed anonymously and reset. + */ + function testOverrideAndResetScheme() { + $settings_path = 'admin/appearance/settings/bartik'; + $this->config('system.theme') + ->set('default', 'bartik') + ->save(); + + // Place branding block with site name and slogan into header region. + $this->drupalPlaceBlock('system_branding_block', ['region' => 'header']); + + $this->drupalGet(''); + $this->assertNoRaw('files/color/bartik-', 'Make sure the color logo is not being used.'); + $this->assertRaw('bartik/logo.svg', 'Make sure the original bartik logo exists.'); + + // Login and set the scheme to slate. + $this->drupalLogin($this->bigUser); + $edit['scheme'] = 'slate'; + $this->drupalPostForm($settings_path, $edit, t('Save configuration')); + + // Visit the bartik homepage and ensure color changes. + $this->drupalLogout(); + $this->drupalGet(''); + $this->assertRaw('files/color/bartik-', 'Make sure the color logo is being used.'); + $this->assertNoRaw('bartik/logo.svg', 'Make sure the original bartik logo does not exist.'); + + // Login and set the scheme back to default (delete config). + $this->drupalLogin($this->bigUser); + $edit['scheme'] = 'default'; + $this->drupalPostForm($settings_path, $edit, t('Save configuration')); + + // Logout and ensure there is no color and we have the original logo. + $this->drupalLogout(); + $this->drupalGet(''); + $this->assertNoRaw('files/color/bartik-', 'Make sure the color logo is not being used.'); + $this->assertRaw('bartik/logo.svg', 'Make sure the original bartik logo exists.'); + } + }