From 773945306779a77baeab1863424598db058ee0c5 Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole Date: Thu, 16 Apr 2015 14:39:27 +0100 Subject: [PATCH] Issue #2442769 by keyral, jcnventura, dimaro, pec: Views result cache ignores query arguments --- .../Plugin/views/cache/CachePluginBase.php | 5 +- .../views/src/Tests/Plugin/CacheTest.php | 97 +++++++++++++++++-- 2 files changed, 92 insertions(+), 10 deletions(-) diff --git a/core/modules/views/src/Plugin/views/cache/CachePluginBase.php b/core/modules/views/src/Plugin/views/cache/CachePluginBase.php index cafb6131881..21e666f2b21 100644 --- a/core/modules/views/src/Plugin/views/cache/CachePluginBase.php +++ b/core/modules/views/src/Plugin/views/cache/CachePluginBase.php @@ -287,7 +287,10 @@ abstract class CachePluginBase extends PluginBase { if ($build_info[$index] instanceof Select) { $query = clone $build_info[$index]; $query->preExecute(); - $build_info[$index] = (string)$query; + $build_info[$index] = array( + 'query' => (string)$query, + 'arguments' => $query->getArguments(), + ); } } diff --git a/core/modules/views/src/Tests/Plugin/CacheTest.php b/core/modules/views/src/Tests/Plugin/CacheTest.php index a1c2b9c6a42..679a7644ef9 100644 --- a/core/modules/views/src/Tests/Plugin/CacheTest.php +++ b/core/modules/views/src/Tests/Plugin/CacheTest.php @@ -10,6 +10,7 @@ namespace Drupal\views\Tests\Plugin; use Drupal\node\Entity\Node; use Drupal\views\Tests\ViewUnitTestBase; use Drupal\views\Views; +use Drupal\views_test_data\Plugin\views\filter\FilterTest as FilterPlugin; /** * Tests pluggable caching for views. @@ -24,7 +25,7 @@ class CacheTest extends ViewUnitTestBase { * * @var array */ - public static $testViews = array('test_view', 'test_cache', 'test_groupwise_term_ui', 'test_display'); + public static $testViews = array('test_view', 'test_cache', 'test_groupwise_term_ui', 'test_display', 'test_filter'); /** * Modules to enable. @@ -74,7 +75,7 @@ class CacheTest extends ViewUnitTestBase { 'type' => 'time', 'options' => array( 'results_lifespan' => '3600', - 'output_lifespan' => '3600' + 'output_lifespan' => '3600', ) )); @@ -100,6 +101,84 @@ class CacheTest extends ViewUnitTestBase { $this->assertEqual(5, count($view->result), 'The number of returned rows match.'); } + /** + * Tests result caching with filters. + * + * @see views_plugin_cache_time + */ + public function testTimeResultCachingWithFilter() { + // Check that we can find the test filter plugin. + $plugin = $this->container->get('plugin.manager.views.filter')->createInstance('test_filter'); + $this->assertTrue($plugin instanceof FilterPlugin, 'Test filter plugin found.'); + + $view = Views::getView('test_filter'); + $view->initDisplay(); + $view->display_handler->overrideOption('cache', array( + 'type' => 'time', + 'options' => array( + 'results_lifespan' => '3600', + 'output_lifespan' => '3600', + ), + )); + + // Change the filtering. + $view->displayHandlers->get('default')->overrideOption('filters', array( + 'test_filter' => array( + 'id' => 'test_filter', + 'table' => 'views_test_data', + 'field' => 'name', + 'operator' => '=', + 'value' => 'John', + 'group' => 0, + ), + )); + + $this->executeView($view); + + // Get the cache item. + $cid1 = $view->display_handler->getPlugin('cache')->generateResultsKey(); + + // Build the expected result. + $dataset = array(array('name' => 'John')); + + // Verify the result. + $this->assertEqual(1, count($view->result), 'The number of returned rows match.'); + $this->assertIdenticalResultSet($view, $dataset, array( + 'views_test_data_name' => 'name', + )); + + $view->destroy(); + + $view->initDisplay(); + + // Change the filtering. + $view->displayHandlers->get('default')->overrideOption('filters', array( + 'test_filter' => array( + 'id' => 'test_filter', + 'table' => 'views_test_data', + 'field' => 'name', + 'operator' => '=', + 'value' => 'Ringo', + 'group' => 0, + ), + )); + + $this->executeView($view); + + // Get the cache item. + $cid2 = $view->display_handler->getPlugin('cache')->generateResultsKey(); + $this->assertNotEqual($cid1, $cid2, "Results keys are different."); + + // Build the expected result. + $dataset = array(array('name' => 'Ringo')); + + // Verify the result. + $this->assertEqual(1, count($view->result), 'The number of returned rows match.'); + $this->assertIdenticalResultSet($view, $dataset, array( + 'views_test_data_name' => 'name', + )); + } + /** * Tests result caching with a pager. */ @@ -110,7 +189,7 @@ class CacheTest extends ViewUnitTestBase { 'type' => 'time', 'options' => array( 'results_lifespan' => '3600', - 'output_lifespan' => '3600' + 'output_lifespan' => '3600', ) )); @@ -152,7 +231,7 @@ class CacheTest extends ViewUnitTestBase { $view->setDisplay(); $view->display_handler->overrideOption('cache', array( 'type' => 'none', - 'options' => array() + 'options' => array(), )); $this->executeView($view); @@ -172,7 +251,7 @@ class CacheTest extends ViewUnitTestBase { $view->setDisplay(); $view->display_handler->overrideOption('cache', array( 'type' => 'none', - 'options' => array() + 'options' => array(), )); $this->executeView($view); @@ -193,18 +272,18 @@ class CacheTest extends ViewUnitTestBase { $view->display_handler->overrideOption('cache', array( 'type' => 'time', 'options' => array( - 'output_lifespan' => '3600' + 'output_lifespan' => '3600', ) )); $output = $view->preview(); - drupal_render($output); + \Drupal::service('renderer')->render($output); unset($view->pre_render_called); $view->destroy(); $view->setDisplay(); $output = $view->preview(); - drupal_render($output); + \Drupal::service('renderer')->render($output); $this->assertTrue(in_array('views_test_data/test', $output['#attached']['library']), 'Make sure libraries are added for cached views.'); $this->assertEqual(['foo' => 'bar'], $output['#attached']['drupalSettings'], 'Make sure drupalSettings are added for cached views.'); // Note: views_test_data_views_pre_render() adds some cache tags. @@ -244,7 +323,7 @@ class CacheTest extends ViewUnitTestBase { 'type' => 'time', 'options' => array( 'results_lifespan' => '3600', - 'output_lifespan' => '3600' + 'output_lifespan' => '3600', ) )); $this->executeView($view);