Commit ae7ec6c5 authored by effulgentsia's avatar effulgentsia

Issue #2562757 by Wim Leers: Various small cacheability fixes that are blocking SmartCache

parent e3d5340a
......@@ -52,6 +52,11 @@ public static function buildCancelLink(ConfirmFormInterface $form, Request $requ
'#title' => $form->getCancelText(),
'#attributes' => ['class' => ['button']],
'#url' => $url,
'#cache' => [
'contexts' => [
'url.query_args:destination',
],
],
];
}
......
......@@ -16,6 +16,9 @@
/**
* Wraps the caching logic for the render caching system.
*
* @todo Refactor this out into a generic service capable of cache redirects,
* and let RenderCache use that. https://www.drupal.org/node/2551419
*/
class RenderCache implements RenderCacheInterface {
......
......@@ -63,7 +63,7 @@ public static function create(ContainerInterface $container, array $configuratio
* {@inheritdoc}
*/
protected function blockAccess(AccountInterface $account) {
return $this->state->get('test_block_access', FALSE) ? AccessResult::allowed() : AccessResult::forbidden();
return $this->state->get('test_block_access', FALSE) ? AccessResult::allowed()->setCacheMaxAge(0) : AccessResult::forbidden()->setCacheMaxAge(0);
}
/**
......
......@@ -8,6 +8,7 @@
namespace Drupal\book\Tests;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityInterface;
use Drupal\simpletest\WebTestBase;
use Drupal\user\RoleInterface;
......@@ -124,11 +125,12 @@ public function testBookNavigationCacheContext() {
// Enable the debug output.
\Drupal::state()->set('book_test.debug_book_navigation_cache_context', TRUE);
Cache::invalidateTags(['book_test.debug_book_navigation_cache_context']);
$this->drupalLogin($this->bookAuthor);
// On non-node route.
$this->drupalGet('');
$this->drupalGet($this->adminUser->urlInfo());
$this->assertRaw('[route.book_navigation]=book.none');
// On non-book node route.
......
......@@ -15,6 +15,7 @@
* Implements hook_page_attachments().
*/
function book_test_page_attachments(array &$page) {
$page['#cache']['tags'][] = 'book_test.debug_book_navigation_cache_context';
if (\Drupal::state()->get('book_test.debug_book_navigation_cache_context', FALSE)) {
drupal_set_message(\Drupal::service('cache_contexts_manager')->convertTokensToKeys(['route.book_navigation'])->getKeys()[0]);
}
......
......@@ -399,7 +399,7 @@ public function testFormImmutability() {
// that implementation.
\Drupal::state()->set('page_cache_bypass_form_immutability', TRUE);
\Drupal::moduleHandler()->resetImplementations();
\Drupal::cache('render')->deleteAll();
Cache::invalidateTags(['rendered']);
$this->drupalGet('page_cache_form_test_immutability');
......
......@@ -7,6 +7,8 @@
namespace Drupal\path\Tests;
use Drupal\Core\Cache\Cache;
/**
* Add, edit, delete, and change alias and verify its consistency in the
* database.
......@@ -57,6 +59,8 @@ function testPathCache() {
// Visit the alias for the node and confirm a cache entry is created.
\Drupal::cache('data')->deleteAll();
// @todo Remove this once https://www.drupal.org/node/2480077 lands.
Cache::invalidateTags(['rendered']);
$this->drupalGet(trim($edit['alias'], '/'));
$this->assertTrue(\Drupal::cache('data')->get('preload-paths:' . $edit['source']), 'Cache entry was created.');
}
......
......@@ -75,6 +75,7 @@ public function view(Request $request, SearchPageInterface $entity) {
// Build the form first, because it may redirect during the submit,
// and we don't want to build the results based on last time's request.
$build['#cache']['contexts'][] = 'url.query_args:keys';
if ($request->query->has('keys')) {
$keys = trim($request->get('keys'));
$plugin->setSearch($keys, $request->query->all(), $request->attributes->all());
......
......@@ -60,8 +60,8 @@ public function onSave(ConfigCrudEvent $event) {
$this->cacheTagsInvalidator->invalidateTags(['route_match', 'rendered']);
}
// Global theme settings.
if ($event->getConfig()->getName() === 'system.theme.global') {
// Theme configuration and global theme settings.
if (in_array($event->getConfig()->getName(), ['system.theme', 'system.theme.global'])) {
$this->cacheTagsInvalidator->invalidateTags(['rendered']);
}
......
......@@ -40,6 +40,11 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#value' => 'Submit',
);
// This is a POST form with multiple steps that does not transition from one
// step to the next via POST requests, but via GET requests, because it uses
// Batch API to advance through the steps.
$form['#cache']['max-age'] = 0;
return $form;
}
......
......@@ -25,6 +25,8 @@ public function testNodeSetParent(NodeInterface $node, NodeInterface $parent) {
}
public function testEntityLanguage(NodeInterface $node) {
return ['#markup' => $node->label()];
$build = ['#markup' => $node->label()];
\Drupal::service('renderer')->addCacheableDependency($build, $node);
return $build;
}
}
......@@ -33,6 +33,7 @@ public function testCancelLinkTitle() {
$link = ConfirmFormHelper::buildCancelLink($form, new Request());
$this->assertSame($cancel_text, $link['#title']);
$this->assertSame(['contexts' => ['url.query_args:destination']], $link['#cache']);
}
/**
......@@ -49,6 +50,7 @@ public function testCancelLinkRoute() {
->will($this->returnValue($cancel_route));
$link = ConfirmFormHelper::buildCancelLink($form, new Request());
$this->assertEquals(Url::fromRoute($route_name), $link['#url']);
$this->assertSame(['contexts' => ['url.query_args:destination']], $link['#cache']);
}
/**
......@@ -64,6 +66,7 @@ public function testCancelLinkRouteWithParams() {
->will($this->returnValue($expected));
$link = ConfirmFormHelper::buildCancelLink($form, new Request());
$this->assertEquals($expected, $link['#url']);
$this->assertSame(['contexts' => ['url.query_args:destination']], $link['#cache']);
}
/**
......@@ -86,6 +89,7 @@ public function testCancelLinkRouteWithUrl() {
->will($this->returnValue($cancel_route));
$link = ConfirmFormHelper::buildCancelLink($form, new Request());
$this->assertSame($cancel_route, $link['#url']);
$this->assertSame(['contexts' => ['url.query_args:destination']], $link['#cache']);
}
/**
......@@ -112,6 +116,7 @@ public function testCancelLinkDestination($destination) {
$link = ConfirmFormHelper::buildCancelLink($form, new Request($query));
$this->assertSame($url, $link['#url']);
$this->assertSame(['contexts' => ['url.query_args:destination']], $link['#cache']);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment