Commit ae4a224e authored by catch's avatar catch

Issue #2433591 by dawehner, plach, YesCT, Wim Leers: Views using pagers should...

Issue #2433591 by dawehner, plach, YesCT, Wim Leers: Views using pagers should specify a cache context
parent 6758f115
...@@ -250,6 +250,7 @@ protected function assertFrontPageViewCacheTags($do_assert_views_caches) { ...@@ -250,6 +250,7 @@ protected function assertFrontPageViewCacheTags($do_assert_views_caches) {
'user.permissions', 'user.permissions',
// Default cache contexts of the renderer. // Default cache contexts of the renderer.
'theme', 'theme',
'url.query_args.pagers:0',
]; ];
// Test before there are any nodes. // Test before there are any nodes.
......
...@@ -229,7 +229,7 @@ public function cacheGet($type) { ...@@ -229,7 +229,7 @@ public function cacheGet($type) {
// Load entities for each result. // Load entities for each result.
$this->view->query->loadEntities($this->view->result); $this->view->query->loadEntities($this->view->result);
$this->view->total_rows = $cache->data['total_rows']; $this->view->total_rows = $cache->data['total_rows'];
$this->view->setCurrentPage($cache->data['current_page']); $this->view->setCurrentPage($cache->data['current_page'], TRUE);
$this->view->execute_time = 0; $this->view->execute_time = 0;
return TRUE; return TRUE;
} }
......
...@@ -388,7 +388,7 @@ public function isCacheable() { ...@@ -388,7 +388,7 @@ public function isCacheable() {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCacheContexts() { public function getCacheContexts() {
$contexts = []; $contexts = ['url.query_args.pagers:' . $this->options['id']];
if ($this->options['expose']['items_per_page']) { if ($this->options['expose']['items_per_page']) {
$contexts[] = 'url.query_args:items_per_page'; $contexts[] = 'url.query_args:items_per_page';
} }
......
...@@ -75,6 +75,7 @@ public function testClickSorting() { ...@@ -75,6 +75,7 @@ public function testClickSorting() {
$expected_contexts = [ $expected_contexts = [
'languages:language_interface', 'languages:language_interface',
'theme', 'theme',
'url.query_args.pagers:0',
'url.query_args:order', 'url.query_args:order',
'url.query_args:sort', 'url.query_args:sort',
]; ];
......
...@@ -206,6 +206,7 @@ public function testExposedSortAndItemsPerPage() { ...@@ -206,6 +206,7 @@ public function testExposedSortAndItemsPerPage() {
'languages:language_interface', 'languages:language_interface',
'entity_test_view_grants', 'entity_test_view_grants',
'theme', 'theme',
'url.query_args.pagers:0',
'url.query_args:items_per_page', 'url.query_args:items_per_page',
'url.query_args:offset', 'url.query_args:offset',
'url.query_args:sort_order', 'url.query_args:sort_order',
......
<?php
/**
* @file
* Contains \Drupal\views\Tests\Plugin\PagerKernelTest.
*/
namespace Drupal\views\Tests\Plugin;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\views\Tests\ViewUnitTestBase;
use Drupal\views\Views;
/**
* Tests pager-related APIs.
*
* @group views
*/
class PagerKernelTest extends ViewUnitTestBase {
/**
* {@inheritdoc}
*/
public static $testViews = ['test_pager_full'];
/**
* {@inheritdoc}
*/
public static $modules = ['user', 'node'];
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this->installEntitySchema('node');
$this->installEntitySchema('user');
}
/**
* Tests pager-related setter methods on ViewExecutable.
*
* @see \Drupal\views\ViewExecutable::setItemsPerPage
* @see \Drupal\views\ViewExecutable::setOffset
* @see \Drupal\views\ViewExecutable::setCurrentPage
*/
public function testSetPagerMethods() {
$view = Views::getView('test_pager_full');
$output = $view->preview();
\Drupal::service('renderer')->renderPlain($output);
$this->assertIdentical(CacheBackendInterface::CACHE_PERMANENT, $output['#cache']['max-age']);
foreach (['setItemsPerPage', 'setOffset', 'setCurrentPage'] as $method) {
// Without $keep_cacheablity.
$view = Views::getView('test_pager_full');
$view->setDisplay('default');
$view->{$method}(1);
$output = $view->preview();
\Drupal::service('renderer')->renderPlain($output);
$this->assertIdentical(0, $output['#cache']['max-age'], 'Max age set to 0 without $keep_cacheablity.');
// With $keep_cacheablity.
$view = Views::getView('test_pager_full');
$view->setDisplay('default');
$view->{$method}(1, TRUE);
$output = $view->preview();
\Drupal::service('renderer')->renderPlain($output);
$this->assertIdentical(CacheBackendInterface::CACHE_PERMANENT, $output['#cache']['max-age'], 'Max age unchanged with $keep_cacheablity.');
}
}
}
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace Drupal\views\Tests\Plugin; namespace Drupal\views\Tests\Plugin;
use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
use Drupal\views\Views; use Drupal\views\Views;
use Drupal\language\Entity\ConfigurableLanguage; use Drupal\language\Entity\ConfigurableLanguage;
...@@ -17,6 +18,8 @@ ...@@ -17,6 +18,8 @@
*/ */
class PagerTest extends PluginTestBase { class PagerTest extends PluginTestBase {
use AssertPageCacheContextsAndTagsTrait;
/** /**
* Views used by this test. * Views used by this test.
* *
...@@ -255,6 +258,10 @@ public function testNormalPager() { ...@@ -255,6 +258,10 @@ public function testNormalPager() {
$this->executeView($view); $this->executeView($view);
$this->assertEqual($view->pager->getItemsPerPage(), 0); $this->assertEqual($view->pager->getItemsPerPage(), 0);
$this->assertEqual(count($view->result), 11); $this->assertEqual(count($view->result), 11);
// Test pager cache contexts.
$this->drupalGet('test_pager_full');
$this->assertCacheContexts(['languages:language_interface', 'theme', 'timezone', 'url.query_args.pagers:0', 'user.node_grants:view']);
} }
/** /**
......
...@@ -222,7 +222,7 @@ public function testViewAddCacheMetadata() { ...@@ -222,7 +222,7 @@ public function testViewAddCacheMetadata() {
$view = View::load('test_display'); $view = View::load('test_display');
$view->save(); $view->save();
$this->assertEqual(['languages:' . LanguageInterface::TYPE_CONTENT, 'languages:' . LanguageInterface::TYPE_INTERFACE, 'user.node_grants:view'], $view->getDisplay('default')['cache_metadata']['contexts']); $this->assertEqual(['languages:' . LanguageInterface::TYPE_CONTENT, 'languages:' . LanguageInterface::TYPE_INTERFACE, 'url.query_args.pagers:0', 'user.node_grants:view'], $view->getDisplay('default')['cache_metadata']['contexts']);
} }
} }
...@@ -491,10 +491,22 @@ public function setArguments(array $args) { ...@@ -491,10 +491,22 @@ public function setArguments(array $args) {
/** /**
* Change/Set the current page for the pager. * Change/Set the current page for the pager.
*
* @param int $page
* The current page.
* @param bool $keep_cacheability
* (optional) Keep the cacheability. By default we mark the view as not
* cacheable. The reason for this parameter is that we do not know what the
* passed in value varies by. For example, it could be per role. Defaults to
* FALSE.
*/ */
public function setCurrentPage($page) { public function setCurrentPage($page, $keep_cacheability = FALSE) {
$this->current_page = $page; $this->current_page = $page;
if (!$keep_cacheability) {
$this->element['#cache']['max-age'] = 0;
}
// If the pager is already initialized, pass it through to the pager. // If the pager is already initialized, pass it through to the pager.
if (!empty($this->pager)) { if (!empty($this->pager)) {
return $this->pager->setCurrentPage($page); return $this->pager->setCurrentPage($page);
...@@ -531,14 +543,26 @@ public function getItemsPerPage() { ...@@ -531,14 +543,26 @@ public function getItemsPerPage() {
/** /**
* Set the items per page on the pager. * Set the items per page on the pager.
*
* @param int $items_per_page
* The items per page.
* @param bool $keep_cacheability
* (optional) Keep the cacheability. By default we mark the view as not
* cacheable. The reason for this parameter is that we do not know what the
* passed in value varies by. For example, it could be per role. Defaults to
* FALSE.
*/ */
public function setItemsPerPage($items_per_page) { public function setItemsPerPage($items_per_page, $keep_cacheability = FALSE) {
$this->items_per_page = $items_per_page; $this->items_per_page = $items_per_page;
// If the pager is already initialized, pass it through to the pager. // If the pager is already initialized, pass it through to the pager.
if (!empty($this->pager)) { if (!empty($this->pager)) {
$this->pager->setItemsPerPage($items_per_page); $this->pager->setItemsPerPage($items_per_page);
} }
if (!$keep_cacheability) {
$this->element['#cache']['max-age'] = 0;
}
} }
/** /**
...@@ -557,14 +581,26 @@ public function getOffset() { ...@@ -557,14 +581,26 @@ public function getOffset() {
/** /**
* Set the offset on the pager. * Set the offset on the pager.
*
* @param int $offset
* The pager offset.
* @param bool $keep_cacheability
* (optional) Keep the cacheability. By default we mark the view as not
* cacheable. The reason for this parameter is that we do not know what the
* passed in value varies by. For example, it could be per role. Defaults to
* FALSE.
*/ */
public function setOffset($offset) { public function setOffset($offset, $keep_cacheability = FALSE) {
$this->offset = $offset; $this->offset = $offset;
// If the pager is already initialized, pass it through to the pager. // If the pager is already initialized, pass it through to the pager.
if (!empty($this->pager)) { if (!empty($this->pager)) {
$this->pager->setOffset($offset); $this->pager->setOffset($offset);
} }
if (!$keep_cacheability) {
$this->element['#cache']['max-age'] = 0;
}
} }
/** /**
...@@ -2363,7 +2399,7 @@ public function unserialize($serialized) { ...@@ -2363,7 +2399,7 @@ public function unserialize($serialized) {
$this->setDisplay($current_display); $this->setDisplay($current_display);
$this->setArguments($args); $this->setArguments($args);
$this->setCurrentPage($current_page); $this->setCurrentPage($current_page, TRUE);
$this->setExposedInput($exposed_input); $this->setExposedInput($exposed_input);
$this->exposed_data = $exposed_data; $this->exposed_data = $exposed_data;
$this->exposed_raw_input = $exposed_raw_input; $this->exposed_raw_input = $exposed_raw_input;
......
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