public function testContextPlaceholdersAreReplaced()
 {
     $container = $this->getMockContainer();
     $container->expects($this->once())->method("get")->with("cache_context.foo")->will($this->returnValue(new FooCacheContext()));
     $cache_contexts = new CacheContexts($container, $this->getContextsFixture());
     $new_keys = $cache_contexts->convertTokensToKeys(array("non-cache-context", "cache_context.foo"));
     $expected = array("non-cache-context", "bar");
     $this->assertEquals($expected, $new_keys);
 }
Ejemplo n.º 2
0
 /**
  * Creates the cache ID for a renderable element.
  *
  * This creates the cache ID string, either by returning the #cache['cid']
  * property if present or by building the cache ID out of the #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']['cid'])) {
         return $elements['#cache']['cid'];
     } elseif (isset($elements['#cache']['keys'])) {
         $cid_parts = $elements['#cache']['keys'];
         if (isset($elements['#cache']['contexts'])) {
             $contexts = $this->cacheContexts->convertTokensToKeys($elements['#cache']['contexts']);
             $cid_parts = array_merge($cid_parts, $contexts);
         }
         return implode(':', $cid_parts);
     }
     return FALSE;
 }
Ejemplo n.º 3
0
 /**
  * @covers ::convertTokensToKeys
  *
  * @expectedException \Exception
  *
  * @dataProvider providerTestInvalidCalculatedContext
  */
 public function testInvalidCalculatedContext($context_token)
 {
     $container = $this->getMockContainer();
     $cache_contexts = new CacheContexts($container, $this->getContextsFixture());
     $cache_contexts->convertTokensToKeys([$context_token]);
 }