Esempio n. 1
0
 /**
  * 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');
     // First access as user without access.
     $build = DisplayPluginBase::buildBasicRenderable('test_access_perm', 'default');
     $account_switcher->switchTo($this->normalUser);
     $result = $renderer->renderPlain($build);
     $this->assertNotEqual($result, '');
     // Then with access.
     $build = DisplayPluginBase::buildBasicRenderable('test_access_perm', 'default');
     $account_switcher->switchTo($this->webUser);
     $result = $renderer->renderPlain($build);
     $this->assertEqual($result, '');
 }
Esempio n. 2
0
 /**
  * Tests the output caching on an actual page.
  */
 public function testCacheOutputOnPage()
 {
     $view = Views::getView('test_display');
     $view->storage->setStatus(TRUE);
     $view->setDisplay('page_1');
     $view->display_handler->overrideOption('cache', array('type' => 'time', 'options' => array('results_lifespan' => '3600', 'output_lifespan' => '3600')));
     $view->save();
     $this->container->get('router.builder')->rebuildIfNeeded();
     /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */
     $render_cache = \Drupal::service('render_cache');
     $cache_element = DisplayPluginBase::buildBasicRenderable('test_display', 'page_1');
     $cache_element['#cache'] += ['contexts' => $this->container->getParameter('renderer.config')['required_cache_contexts']];
     $this->assertFalse($render_cache->get($cache_element));
     $this->drupalGet('test-display');
     $this->assertResponse(200);
     $this->assertTrue($render_cache->get($cache_element));
     $cache_tags = ['config:user.role.anonymous', 'config:views.view.test_display', 'node_list', 'rendered'];
     $this->assertCacheTags($cache_tags);
     $this->drupalGet('test-display');
     $this->assertResponse(200);
     $this->assertTrue($render_cache->get($cache_element));
     $this->assertCacheTags($cache_tags);
 }
Esempio n. 3
0
 /**
  * Tests REST export with views render caching enabled.
  */
 public function testRestRenderCaching()
 {
     $this->drupalLogin($this->adminUser);
     /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */
     $render_cache = \Drupal::service('render_cache');
     // Enable render caching for the views.
     /** @var \Drupal\views\ViewEntityInterface $storage */
     $storage = View::load('test_serializer_display_entity');
     $options =& $storage->getDisplay('default');
     $options['display_options']['cache'] = ['type' => 'tag'];
     $storage->save();
     $original = DisplayPluginBase::buildBasicRenderable('test_serializer_display_entity', 'rest_export_1');
     // Ensure that there is no corresponding render cache item yet.
     $original['#cache'] += ['contexts' => []];
     $original['#cache']['contexts'] = Cache::mergeContexts($original['#cache']['contexts'], $this->container->getParameter('renderer.config')['required_cache_contexts']);
     $cache_tags = ['config:views.view.test_serializer_display_entity', 'entity_test:1', 'entity_test:10', 'entity_test:2', 'entity_test:3', 'entity_test:4', 'entity_test:5', 'entity_test:6', 'entity_test:7', 'entity_test:8', 'entity_test:9', 'entity_test_list'];
     $cache_contexts = ['entity_test_view_grants', 'languages:language_interface', 'theme', 'request_format'];
     $this->assertFalse($render_cache->get($original));
     // Request the page, once in XML and once in JSON to ensure that the caching
     // varies by it.
     $result1 = $this->drupalGetJSON('test/serialize/entity');
     $this->addRequestWithFormat('json');
     $this->assertHeader('content-type', 'application/json');
     $this->assertCacheContexts($cache_contexts);
     $this->assertCacheTags($cache_tags);
     $this->assertTrue($render_cache->get($original));
     $result_xml = $this->drupalGetWithFormat('test/serialize/entity', 'xml');
     $this->addRequestWithFormat('xml');
     $this->assertHeader('content-type', 'text/xml; charset=UTF-8');
     $this->assertCacheContexts($cache_contexts);
     $this->assertCacheTags($cache_tags);
     $this->assertTrue($render_cache->get($original));
     // Ensure that the XML output is different from the JSON one.
     $this->assertNotEqual($result1, $result_xml);
     // Ensure that the cached page works.
     $result2 = $this->drupalGetJSON('test/serialize/entity');
     $this->addRequestWithFormat('json');
     $this->assertHeader('content-type', 'application/json');
     $this->assertEqual($result2, $result1);
     $this->assertCacheContexts($cache_contexts);
     $this->assertCacheTags($cache_tags);
     $this->assertTrue($render_cache->get($original));
     // Create a new entity and ensure that the cache tags are taken over.
     EntityTest::create(['name' => 'test_11', 'user_id' => $this->adminUser->id()])->save();
     $result3 = $this->drupalGetJSON('test/serialize/entity');
     $this->addRequestWithFormat('json');
     $this->assertHeader('content-type', 'application/json');
     $this->assertNotEqual($result3, $result2);
     // Add the new entity cache tag and remove the first one, because we just
     // show 10 items in total.
     $cache_tags[] = 'entity_test:11';
     unset($cache_tags[array_search('entity_test:1', $cache_tags)]);
     $this->assertCacheContexts($cache_contexts);
     $this->assertCacheTags($cache_tags);
     $this->assertTrue($render_cache->get($original));
 }
Esempio n. 4
0
 /**
  * Tests random ordering with tags based caching.
  *
  * The random sorting should opt out of caching by defining a max age of 0.
  * At the same time, the row render caching still works.
  */
 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);
 }
 /**
  * Asserts a view's result & render cache items' cache tags.
  *
  * This method starts with a pre bubbling basic render array.
  *
  * @param \Drupal\views\ViewExecutable $view
  *   The view.
  * @param string[] $expected_render_array_cache_tags
  *   The expected render cache tags.
  * @param bool $views_caching_is_enabled
  *   Defines whether views output / render caching is enabled.
  *
  * @return array
  *   The render array.
  */
 protected function assertViewsCacheTagsFromStaticRenderArray(ViewExecutable $view, array $expected_render_array_cache_tags, $views_caching_is_enabled)
 {
     $original = $build = DisplayPluginBase::buildBasicRenderable($view->id(), $view->current_display ?: 'default', $view->args);
     /** @var \Drupal\Core\Render\RendererInterface $renderer */
     $renderer = \Drupal::service('renderer');
     /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */
     $render_cache = \Drupal::service('render_cache');
     // Ensure the current request is a GET request so that render caching is
     // active for direct rendering of views, just like for actual requests.
     /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */
     $request_stack = \Drupal::service('request_stack');
     $request = new Request();
     $request->server->set('REQUEST_TIME', REQUEST_TIME);
     $request_stack->push($request);
     $renderer->renderRoot($build);
     // Render array cache tags.
     $this->pass('Checking render array cache tags.');
     sort($expected_render_array_cache_tags);
     $this->assertEqual($build['#cache']['tags'], $expected_render_array_cache_tags);
     $this->debugCacheTags($build['#cache']['tags'], $expected_render_array_cache_tags);
     $this->pass('Checking Views render cache item cache tags.');
     $original['#cache'] += ['contexts' => []];
     $original['#cache']['contexts'] = Cache::mergeContexts($original['#cache']['contexts'], $this->container->getParameter('renderer.config')['required_cache_contexts']);
     $render_cache_item = $render_cache->get($original);
     if ($views_caching_is_enabled) {
         $this->assertTrue(!empty($render_cache_item), 'Render cache item found.');
         if ($render_cache_item) {
             $this->assertEqual($render_cache_item['#cache']['tags'], $expected_render_array_cache_tags);
             $this->debugCacheTags($render_cache_item['#cache']['tags'], $expected_render_array_cache_tags);
         }
     } else {
         $this->assertFalse($render_cache_item, 'Render cache item not found.');
     }
     return $build;
 }
Esempio n. 6
0
 /**
  * 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');
     // First access as user with access.
     $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default');
     $account_switcher->switchTo($this->normalUser);
     $result = $renderer->renderPlain($build);
     $this->assertTrue(in_array('user.roles', $build['#cache']['contexts']));
     $this->assertEqual(['config:views.view.test_access_role'], $build['#cache']['tags']);
     $this->assertEqual(Cache::PERMANENT, $build['#cache']['max-age']);
     $this->assertNotEqual($result, '');
     // Then without access.
     $build = DisplayPluginBase::buildBasicRenderable('test_access_role', 'default');
     $account_switcher->switchTo($this->webUser);
     $result = $renderer->renderPlain($build);
     // @todo Fix this in https://www.drupal.org/node/2551037,
     // DisplayPluginBase::applyDisplayCachablityMetadata() is not invoked when
     // using buildBasicRenderable() and a Views access plugin returns FALSE.
     //$this->assertTrue(in_array('user.roles', $build['#cache']['contexts']));
     //$this->assertEqual([], $build['#cache']['tags']);
     $this->assertEqual(Cache::PERMANENT, $build['#cache']['max-age']);
     $this->assertEqual($result, '');
 }