/** * Creates the cache ID for a renderable element. * * Creates the cache ID string based on #cache['keys'] + #cache['contexts']. * * @param array $elements * A renderable array. * * @return string * The cache ID string, or FALSE if the element may not be cached. */ protected function createCacheID(array $elements) { // If the maximum age is zero, then caching is effectively prohibited. if (isset($elements['#cache']['max-age']) && $elements['#cache']['max-age'] === 0) { return FALSE; } if (isset($elements['#cache']['keys'])) { $cid_parts = $elements['#cache']['keys']; if (!empty($elements['#cache']['contexts'])) { $contexts = $this->cacheContextsManager->convertTokensToKeys($elements['#cache']['contexts']); $cid_parts = array_merge($cid_parts, $contexts); } return implode(':', $cid_parts); } return FALSE; }
/** * @covers ::validateTokens * * @dataProvider validateTokensProvider */ public function testValidateContexts(array $contexts, $expected_exception_message) { $container = new ContainerBuilder(); $cache_contexts_manager = new CacheContextsManager($container, ['foo', 'foo.bar', 'baz']); if ($expected_exception_message !== FALSE) { $this->setExpectedException('LogicException', $expected_exception_message); } // If it doesn't throw an exception, validateTokens() returns NULL. $this->assertNull($cache_contexts_manager->validateTokens($contexts)); }