/** * Verifies render cache handling of the block being tested. * * @see ::testBlockViewBuilderCache() */ protected function verifyRenderCacheHandling() { // Force a request via GET so we can test the render cache. $request = \Drupal::request(); $request_method = $request->server->get('REQUEST_METHOD'); $request->setMethod('GET'); // Test that a cache entry is created. $build = $this->getBlockRenderArray(); $cid = 'entity_view:block:test_block:en:core'; $this->renderer->renderRoot($build); $this->assertTrue($this->container->get('cache.render')->get($cid), 'The block render element has been cached.'); // Re-save the block and check that the cache entry has been deleted. $this->block->save(); $this->assertFalse($this->container->get('cache.render')->get($cid), 'The block render cache entry has been cleared when the block was saved.'); // Rebuild the render array (creating a new cache entry in the process) and // delete the block to check the cache entry is deleted. unset($build['#printed']); // Re-add the block because \Drupal\block\BlockViewBuilder::buildBlock() // removes it. $build['#block'] = $this->block; $this->renderer->renderRoot($build); $this->assertTrue($this->container->get('cache.render')->get($cid), 'The block render element has been cached.'); $this->block->delete(); $this->assertFalse($this->container->get('cache.render')->get($cid), 'The block render cache entry has been cleared when the block was deleted.'); // Restore the previous request method. $request->setMethod($request_method); }
/** * Verifies render cache handling of the block being tested. * * @see ::testBlockViewBuilderCache() */ protected function verifyRenderCacheHandling() { // Force a request via GET so we can test the render cache. $request = \Drupal::request(); $request_method = $request->server->get('REQUEST_METHOD'); $request->setMethod('GET'); // Test that a cache entry is created. $build = $this->getBlockRenderArray(); $cid = 'entity_view:block:test_block:' . implode(':', \Drupal::service('cache_contexts_manager')->convertTokensToKeys(['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'user.permissions'])->getKeys()); $this->renderer->renderRoot($build); $this->assertTrue($this->container->get('cache.render')->get($cid), 'The block render element has been cached.'); // Re-save the block and check that the cache entry has been deleted. $this->block->save(); $this->assertFalse($this->container->get('cache.render')->get($cid), 'The block render cache entry has been cleared when the block was saved.'); // Rebuild the render array (creating a new cache entry in the process) and // delete the block to check the cache entry is deleted. unset($build['#printed']); // Re-add the block because \Drupal\block\BlockViewBuilder::buildBlock() // removes it. $build['#block'] = $this->block; $this->renderer->renderRoot($build); $this->assertTrue($this->container->get('cache.render')->get($cid), 'The block render element has been cached.'); $this->block->delete(); $this->assertFalse($this->container->get('cache.render')->get($cid), 'The block render cache entry has been cleared when the block was deleted.'); // Restore the previous request method. $request->setMethod($request_method); }
/** * Verifies render cache handling of the block being tested. * * @see ::testBlockViewBuilderCache() */ protected function verifyRenderCacheHandling() { // Force a request via GET so we can get drupal_render() cache working. $request = \Drupal::request(); $request_method = $request->server->get('REQUEST_METHOD'); $request->setMethod('GET'); // Test that entities with caching disabled do not generate a cache entry. $build = $this->getBlockRenderArray(); $this->assertTrue(isset($build['#cache']) && array_keys($build['#cache']) == array('tags'), 'The render array element of uncacheable blocks is not cached, but does have cache tags set.'); // Enable block caching. $this->setBlockCacheConfig(array('max_age' => 600)); // Test that a cache entry is created. $build = $this->getBlockRenderArray(); $cid = drupal_render_cid_create($build); drupal_render($build); $this->assertTrue($this->container->get('cache.render')->get($cid), 'The block render element has been cached.'); // Re-save the block and check that the cache entry has been deleted. $this->block->save(); $this->assertFalse($this->container->get('cache.render')->get($cid), 'The block render cache entry has been cleared when the block was saved.'); // Rebuild the render array (creating a new cache entry in the process) and // delete the block to check the cache entry is deleted. unset($build['#printed']); // Re-add the block because \Drupal\block\BlockViewBuilder::buildBlock() // removes it. $build['#block'] = $this->block; drupal_render($build); $this->assertTrue($this->container->get('cache.render')->get($cid), 'The block render element has been cached.'); $this->block->delete(); $this->assertFalse($this->container->get('cache.render')->get($cid), 'The block render cache entry has been cleared when the block was deleted.'); // Restore the previous request method. $request->setMethod($request_method); }