Commit c735b827 authored by catch's avatar catch

Issue #2674480 by twistor: Views does not properly ignore the query cache during preview

parent 69901dfd
......@@ -8,12 +8,12 @@
namespace Drupal\views;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Tags;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Form\FormState;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\views\Plugin\views\display\DisplayRouterInterface;
use Drupal\Component\Utility\Tags;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
......@@ -1371,8 +1371,9 @@ public function execute($display_id = NULL) {
$module_handler->invokeAll('views_pre_execute', array($this));
// Check for already-cached results.
/** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache */
if (!empty($this->live_preview)) {
$cache = $this->display_handler->getPlugin('cache', 'none');
$cache = Views::pluginManager('cache')->createInstance('none');
}
else {
$cache = $this->display_handler->getPlugin('cache');
......
......@@ -7,10 +7,16 @@
namespace Drupal\Tests\views\Unit;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Url;
use Drupal\Tests\UnitTestCase;
use Drupal\views\Entity\View;
use Drupal\views\Plugin\views\cache\CachePluginBase;
use Drupal\views\Plugin\views\cache\None as NoneCache;
use Drupal\views\Plugin\views\pager\None as NonePager;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;
use Symfony\Component\Routing\Route;
......@@ -76,6 +82,20 @@ class ViewExecutableTest extends UnitTestCase {
*/
protected $routeProvider;
/**
* The mocked none cache plugin.
*
* @var \Drupal\views\Plugin\views\cache\None|\PHPUnit_Framework_MockObject_MockObject
*/
protected $noneCache;
/**
* The mocked cache plugin that returns a successful result.
*
* @var \Drupal\views\Plugin\views\cache\None|\PHPUnit_Framework_MockObject_MockObject
*/
protected $successCache;
/**
* {@inheritdoc}
*/
......@@ -103,10 +123,26 @@ protected function setUp() {
->disableOriginalConstructor()
->getMock();
$module_handler = $this->getMockBuilder(ModuleHandlerInterface::class)
->getMock();
$this->noneCache = $this->getMockBuilder(NoneCache::class)
->disableOriginalConstructor()
->getMock();
$success_cache = $this->prophesize(CachePluginBase::class);
$success_cache->cacheGet('results')->willReturn(TRUE);
$this->successCache = $success_cache->reveal();
$cache_manager = $this->prophesize(PluginManagerInterface::class);
$cache_manager->createInstance('none')->willReturn($this->noneCache);
$translation = $this->getStringTranslationStub();
$container = new ContainerBuilder();
$container->set('string_translation', $translation);
$container->set('views.executable', $this->viewExecutableFactory);
$container->set('module_handler', $module_handler);
$container->set('plugin.manager.views.cache', $cache_manager->reveal());
\Drupal::setContainer($container);
}
......@@ -452,6 +488,11 @@ protected function setupBaseViewAndDisplay() {
$display = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase')
->disableOriginalConstructor()
->getMock();
$display->expects($this->any())
->method('getPlugin')
->with($this->equalTo('cache'))
->willReturn($this->successCache);
$display->display = $config['display']['default'];
$view->current_display = 'default';
......@@ -466,6 +507,10 @@ protected function setupBaseViewAndDisplay() {
->with('default')
->willReturn(TRUE);
foreach (array_keys($view->getHandlerTypes()) as $type) {
$view->$type = [];
}
return array($view, $display);
}
......@@ -565,4 +610,31 @@ public function testSetCurrentPageDuringPreRender() {
$this->assertNotContains('page:12', $view->element['#cache']['keys']);
}
/**
* @covers ::execute
*/
public function testCacheIsIgnoredDuringPreview() {
/** @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject $view */
/** @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit_Framework_MockObject_MockObject $display */
list($view, $display) = $this->setupBaseViewAndDisplay();
// Pager needs to be set to avoid false test failures.
$view->pager = $this->getMockBuilder(NonePager::class)
->disableOriginalConstructor()
->getMock();
$query = $this->getMockBuilder(QueryPluginBase::class)
->disableOriginalConstructor()
->getMock();
$view->query = $query;
$view->built = TRUE;
$view->live_preview = TRUE;
$this->noneCache->expects($this->once())->method('cacheGet');
$query->expects($this->once())->method('execute');
$view->execute();
}
}
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