diff --git a/core/lib/Drupal/Core/Access/AccessResult.php b/core/lib/Drupal/Core/Access/AccessResult.php index cd2b958..efbb835 100644 --- a/core/lib/Drupal/Core/Access/AccessResult.php +++ b/core/lib/Drupal/Core/Access/AccessResult.php @@ -322,6 +322,11 @@ public function cachePerUser() { */ public function cacheUntilEntityChanges(EntityInterface $entity) { $this->addCacheTags($entity->getCacheTags()); + // @discuss Currently only the cache tags are copied from the entity, but + // the contexts are ignored. Shouldn't we include these, since we might + // vary on a parameter that affects accessibility? A good example would be + // block visibility. + // $this->addCacheContexts($entity->getCacheContexts()); return $this; } diff --git a/core/lib/Drupal/Core/Config/ConfigFactory.php b/core/lib/Drupal/Core/Config/ConfigFactory.php index f347bee..6148a7b 100644 --- a/core/lib/Drupal/Core/Config/ConfigFactory.php +++ b/core/lib/Drupal/Core/Config/ConfigFactory.php @@ -228,7 +228,7 @@ protected function propagateCacheableDependencyOverrides($cache_key) { $this->cache[$cache_key] ->addCacheContexts($override->getCacheContexts()) ->addCacheTags($override->getCacheTags()) - ->setCacheMaxAgeIfLower($override->getCacheMaxAge()); + ->mergeCacheMaxAge($override->getCacheMaxAge()); } } diff --git a/core/modules/block/src/BlockViewBuilder.php b/core/modules/block/src/BlockViewBuilder.php index a68b6eb..8a9cb35 100644 --- a/core/modules/block/src/BlockViewBuilder.php +++ b/core/modules/block/src/BlockViewBuilder.php @@ -67,7 +67,10 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la '#id' => $entity->id(), '#cache' => [ 'keys' => ['entity_view', 'block', $entity->id()], - 'contexts' => $plugin->getCacheContexts(), + 'contexts' => Cache::mergeContexts( + $entity->getCacheContexts(), + $plugin->getCacheContexts() + ), 'tags' => Cache::mergeTags( $this->getCacheTags(), // Block view builder cache tag. $entity->getCacheTags(), // Block entity cache tag. diff --git a/core/modules/config/src/Tests/CacheabilityMetadataConfigOverrideIntegrationTest.php b/core/modules/config/src/Tests/CacheabilityMetadataConfigOverrideIntegrationTest.php new file mode 100644 index 0000000..a74254b --- /dev/null +++ b/core/modules/config/src/Tests/CacheabilityMetadataConfigOverrideIntegrationTest.php @@ -0,0 +1,65 @@ +set('block_test.content', 'Needs to have some content'); + + $this->drupalLogin($this->drupalCreateUser()); + } + + /** + * Tests if config overrides correctly set cacheability metadata. + */ + public function testConfigOverride() { + // Check the default (disabled) state of the cache context. The block label + // should not be overridden. + $this->drupalGet(''); + $this->assertNoText('Overridden block label'); + + // Both the cache context and tag should be present. + $this->assertCacheContext('config_override_integration_test'); + $this->assertCacheTag('config_override_integration_test_tag'); + + // Flip the state of the cache context. The block label should now be + // overridden. + \Drupal::state()->set('config_override_integration_test.enabled', 'yes'); + $this->drupalGet(''); + $this->assertText('Overridden block label'); + + // Both the cache context and tag should still be present. + $this->assertCacheContext('config_override_integration_test'); + $this->assertCacheTag('config_override_integration_test_tag'); + } + +} diff --git a/core/modules/config/tests/config_override_integration_test/config/install/block.block.config_override_test.yml b/core/modules/config/tests/config_override_integration_test/config/install/block.block.config_override_test.yml new file mode 100644 index 0000000..be0616f --- /dev/null +++ b/core/modules/config/tests/config_override_integration_test/config/install/block.block.config_override_test.yml @@ -0,0 +1,24 @@ +id: config_override_test +theme: classy +weight: 0 +status: true +langcode: en +region: content +plugin: test_cache +settings: + label: 'Test HTML block' + provider: block_test + label_display: visible + status: true + info: '' + view_mode: default +dependencies: + module: + - block_test + theme: + - classy +visibility: + request_path: + id: request_path + pages: '' + negate: false diff --git a/core/modules/config/tests/config_override_integration_test/config_override_integration_test.info.yml b/core/modules/config/tests/config_override_integration_test/config_override_integration_test.info.yml new file mode 100644 index 0000000..22b1e94 --- /dev/null +++ b/core/modules/config/tests/config_override_integration_test/config_override_integration_test.info.yml @@ -0,0 +1,9 @@ +name: 'Configuration override integration test' +type: module +package: Testing +version: VERSION +core: 8.x + +dependencies: + - block + - block_test diff --git a/core/modules/config/tests/config_override_integration_test/config_override_integration_test.services.yml b/core/modules/config/tests/config_override_integration_test/config_override_integration_test.services.yml new file mode 100644 index 0000000..147874e --- /dev/null +++ b/core/modules/config/tests/config_override_integration_test/config_override_integration_test.services.yml @@ -0,0 +1,9 @@ +services: + cache_context.config_override_integration_test: + class: Drupal\config_override_integration_test\Cache\ConfigOverrideIntegrationTestCacheContext + tags: + - { name: cache.context } + config_override_integration_test.config_override: + class: Drupal\config_override_integration_test\CacheabilityMetadataConfigOverride + tags: + - { name: config.factory.override } diff --git a/core/modules/config/tests/config_override_integration_test/src/Cache/ConfigOverrideIntegrationTestCacheContext.php b/core/modules/config/tests/config_override_integration_test/src/Cache/ConfigOverrideIntegrationTestCacheContext.php new file mode 100644 index 0000000..23e228b --- /dev/null +++ b/core/modules/config/tests/config_override_integration_test/src/Cache/ConfigOverrideIntegrationTestCacheContext.php @@ -0,0 +1,35 @@ +get('config_override_integration_test.enabled', 'no'); + return 'config_override_integration_test.' . $state; + } + +} diff --git a/core/modules/config/tests/config_override_integration_test/src/CacheabilityMetadataConfigOverride.php b/core/modules/config/tests/config_override_integration_test/src/CacheabilityMetadataConfigOverride.php new file mode 100644 index 0000000..e80eeec --- /dev/null +++ b/core/modules/config/tests/config_override_integration_test/src/CacheabilityMetadataConfigOverride.php @@ -0,0 +1,73 @@ +get('config_override_integration_test.enabled', 'no'); + if (in_array('block.block.config_override_test', $names) && $state !== 'no') { + $overrides = $overrides + [ + 'block.block.config_override_test' => [ + 'settings' => ['label' => 'Overridden block label'], + ], + ]; + } + + return $overrides; + } + + /** + * {@inheritdoc} + */ + public function getCacheSuffix() { + return 'config_override_integration_test'; + } + + /** + * {@inheritdoc} + */ + public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) { + return NULL; + } + + /** + * {@inheritdoc} + */ + public function getCacheContexts() { + return ['config_override_integration_test']; + } + + /** + * {@inheritdoc} + */ + public function getCacheTags() { + return ['config_override_integration_test_tag']; + } + + /** + * {@inheritdoc} + */ + public function getCacheMaxAge() { + return Cache::PERMANENT; + } + +} diff --git a/core/modules/config/tests/config_override_test/config_override_test.info.yml b/core/modules/config/tests/config_override_test/config_override_test.info.yml index f1f1109..051729d 100644 --- a/core/modules/config/tests/config_override_test/config_override_test.info.yml +++ b/core/modules/config/tests/config_override_test/config_override_test.info.yml @@ -3,3 +3,7 @@ type: module package: Testing version: VERSION core: 8.x + +dependencies: + - block + - block_content diff --git a/core/modules/config/tests/config_override_test/config_override_test.services.yml b/core/modules/config/tests/config_override_test/config_override_test.services.yml index 4dba664..1a07412 100644 --- a/core/modules/config/tests/config_override_test/config_override_test.services.yml +++ b/core/modules/config/tests/config_override_test/config_override_test.services.yml @@ -11,7 +11,7 @@ services: class: Drupal\config_override_test\ConfigOverriderLowPriority tags: - { name: config.factory.override, priority: -100 } - config_override_test.pirate_day_cache_context_override: + config_override_test.pirate_day_cacheability_metadata_override: class: Drupal\config_override_test\PirateDayCacheabilityMetadataConfigOverride tags: - { name: config.factory.override }