diff --git a/core/modules/user/src/Plugin/views/access/Permission.php b/core/modules/user/src/Plugin/views/access/Permission.php index b40b767..18f0275 100644 --- a/core/modules/user/src/Plugin/views/access/Permission.php +++ b/core/modules/user/src/Plugin/views/access/Permission.php @@ -144,7 +144,6 @@ public function isCacheable() { * {@inheritdoc} */ public function getCacheContexts() { - // @todo Write explicit test coverage that the variation works. return ['user.permissions']; } diff --git a/core/modules/user/src/Plugin/views/access/Role.php b/core/modules/user/src/Plugin/views/access/Role.php index ac61a1a..9686d08 100644 --- a/core/modules/user/src/Plugin/views/access/Role.php +++ b/core/modules/user/src/Plugin/views/access/Role.php @@ -157,7 +157,6 @@ public function isCacheable() { * {@inheritdoc} */ public function getCacheContexts() { - // @todo Write explicit test coverage that the variation works. return ['user.roles']; } diff --git a/core/modules/user/src/Tests/Views/AccessPermissionTest.php b/core/modules/user/src/Tests/Views/AccessPermissionTest.php index aaa1daf..04b665a 100644 --- a/core/modules/user/src/Tests/Views/AccessPermissionTest.php +++ b/core/modules/user/src/Tests/Views/AccessPermissionTest.php @@ -8,6 +8,7 @@ namespace Drupal\user\Tests\Views; use Drupal\user\Plugin\views\access\Permission; +use Drupal\views\Plugin\views\display\DisplayPluginBase; use Drupal\views\Views; /** @@ -40,4 +41,32 @@ function testAccessPerm() { $this->assertTrue($view->display_handler->access($this->normalUser)); } + /** + * Tests access on render caching. + */ + public function testRenderCaching() { + $view = Views::getView('test_access_perm'); + $display = &$view->storage->getDisplay('default'); + $display['display_options']['cache'] = [ + 'type' => 'tag', + ]; + + /** @var \Drupal\Core\Render\RendererInterface $renderer */ + $renderer = \Drupal::service('renderer'); + /** @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */ + $account_switcher = \Drupal::service('account_switcher'); + + $build = DisplayPluginBase::buildBasicRenderable('test_access_perm', 'default'); + + // First access as user without access, then with access. + $account_switcher->switchTo($this->normalUser); + $result = $renderer->renderPlain($build); + $this->assertNotEqual($result, ''); + + $build = DisplayPluginBase::buildBasicRenderable('test_access_perm', 'default'); + $account_switcher->switchTo($this->webUser); + $result = $renderer->renderPlain($build); + $this->assertEqual($result, ''); + } + } diff --git a/core/modules/user/src/Tests/Views/AccessRoleTest.php b/core/modules/user/src/Tests/Views/AccessRoleTest.php index f79e86e..ae7af21 100644 --- a/core/modules/user/src/Tests/Views/AccessRoleTest.php +++ b/core/modules/user/src/Tests/Views/AccessRoleTest.php @@ -8,6 +8,7 @@ namespace Drupal\user\Tests\Views; use Drupal\user\Plugin\views\access\Role; +use Drupal\views\Plugin\views\display\DisplayPluginBase; use Drupal\views\Views; use Symfony\Component\HttpFoundation\Request; @@ -92,4 +93,36 @@ function testAccessRole() { $this->assertResponse(200); } + /** + * Tests access on render caching. + */ + public function testRenderCaching() { + $view = Views::getView('test_access_role'); + $display = &$view->storage->getDisplay('default'); + $display['display_options']['cache'] = [ + 'type' => 'tag', + ]; + $display['display_options']['access']['options']['role'] = array( + $this->normalRole => $this->normalRole, + ); + $view->save(); + + /** @var \Drupal\Core\Render\RendererInterface $renderer */ + $renderer = \Drupal::service('renderer'); + /** @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */ + $account_switcher = \Drupal::service('account_switcher'); + + $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default'); + + // First access as user without access, then with access. + $account_switcher->switchTo($this->normalUser); + $result = $renderer->renderPlain($build); + $this->assertNotEqual($result, ''); + + $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default'); + $account_switcher->switchTo($this->webUser); + $result = $renderer->renderPlain($build); + $this->assertEqual($result, ''); + } + } diff --git a/core/modules/views/src/Plugin/views/sort/Random.php b/core/modules/views/src/Plugin/views/sort/Random.php index e966909..434a325 100644 --- a/core/modules/views/src/Plugin/views/sort/Random.php +++ b/core/modules/views/src/Plugin/views/sort/Random.php @@ -26,6 +26,7 @@ public function usesGroupBy() { public function query() { $this->query->addOrderBy('rand'); + $this->view->element['#cache']['max-age'] = 0; } public function buildOptionsForm(&$form, FormStateInterface $form_state) { @@ -37,7 +38,6 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { * {@inheritdoc} */ public function isCacheable() { - // @todo Write test coverage to ensure that opt out of caching works. return FALSE; } diff --git a/core/modules/views/src/Tests/Handler/SortRandomTest.php b/core/modules/views/src/Tests/Handler/SortRandomTest.php index 843019b..aafa0a9 100644 --- a/core/modules/views/src/Tests/Handler/SortRandomTest.php +++ b/core/modules/views/src/Tests/Handler/SortRandomTest.php @@ -7,6 +7,8 @@ namespace Drupal\views\Tests\Handler; +use Drupal\Core\Cache\Cache; +use Drupal\views\Plugin\views\display\DisplayPluginBase; use Drupal\views\Tests\ViewUnitTestBase; use Drupal\views\Views; @@ -26,10 +28,17 @@ class SortRandomTest extends ViewUnitTestBase { /** * Add more items to the test set, to make the order tests more robust. + * + * In total we have then 60 entries, which makes a probability of a collision + * of 1/60!, which is around 1/1E80, which is higher than the estimated amount + * of protons / electrons in the observable universe, also called the eddington + * number. + * + * @see http://en.wikipedia.org/wiki/Eddington_number */ protected function dataSet() { $data = parent::dataSet(); - for ($i = 0; $i < 50; $i++) { + for ($i = 0; $i < 55; $i++) { $data[] = array( 'name' => 'name_' . $i, 'age' => $i, @@ -96,4 +105,39 @@ public function testRandomOrdering() { )); } + /** + * Tests random ordering with tags based caching. + * + * The random sorting should opt out of caching by defining a max age 0. + */ + public function testRandomOrderingWithRenderCaching() { + $view_random = $this->getBasicRandomView(); + + $display = &$view_random->storage->getDisplay('default'); + $display['display_options']['cache'] = [ + 'type' => 'tag', + ]; + + $view_random->storage->save(); + + /** @var \Drupal\Core\Render\RendererInterface $renderer */ + $renderer = \Drupal::service('renderer'); + /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */ + $render_cache = \Drupal::service('render_cache'); + + $original = $build = DisplayPluginBase::buildBasicRenderable($view_random->id(), 'default'); + $result = $renderer->renderPlain($build); + + $original['#cache'] += ['contexts' => []]; + $original['#cache']['contexts'] = Cache::mergeContexts($original['#cache']['contexts'], $this->container->getParameter('renderer.config')['required_cache_contexts']); + + $this->assertFalse($render_cache->get($original), 'Ensure there is no render cache entry.'); + + $build = DisplayPluginBase::buildBasicRenderable($view_random->id(), 'default'); + $result2 = $renderer->renderPlain($build); + + // Ensure that the random ordering works and don't produce the same result. + $this->assertNotEqual($result, $result2); + } + }