.../Drupal/Tests/Core/Cache/CacheContextsTest.php | 95 ++++++++++++++++------ 1 file changed, 72 insertions(+), 23 deletions(-) diff --git a/core/tests/Drupal/Tests/Core/Cache/CacheContextsTest.php b/core/tests/Drupal/Tests/Core/Cache/CacheContextsTest.php index fff9df2..87b7798 100644 --- a/core/tests/Drupal/Tests/Core/Cache/CacheContextsTest.php +++ b/core/tests/Drupal/Tests/Core/Cache/CacheContextsTest.php @@ -9,7 +9,9 @@ use Drupal\Core\Cache\CacheContexts; use Drupal\Core\Cache\CacheContextInterface; +use Drupal\Core\Cache\CalculatedCacheContextInterface; use Drupal\Tests\UnitTestCase; +use Symfony\Component\DependencyInjection\Container; /** * @coversDefaultClass \Drupal\Core\Cache\CacheContexts @@ -17,20 +19,19 @@ */ class CacheContextsTest extends UnitTestCase { - public function testContextPlaceholdersAreReplaced() { + /** + * @covers ::convertTokensToKeys + */ + public function testConvertTokensToKeys() { $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( - ['foo'] - ); + $new_keys = $cache_contexts->convertTokensToKeys([ + 'foo', + 'baz:parameter', + ]); - $expected = ['bar']; + $expected = ['bar', 'baz.cnenzrgre']; $this->assertEquals($expected, $new_keys); } @@ -44,24 +45,41 @@ public function testInvalidContext() { $container = $this->getMockContainer(); $cache_contexts = new CacheContexts($container, $this->getContextsFixture()); - $cache_contexts->convertTokensToKeys( - ["non-cache-context"] - ); + $cache_contexts->convertTokensToKeys(["non-cache-context"]); + } + + /** + * @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]); + } + + /** + * Provides a list of invalid 'baz' cache contexts: the parameter is missing. + */ + public function providerTestInvalidCalculatedContext() { + return [ + ['baz'], + ['baz:'], + ]; } public function testAvailableContextStrings() { $cache_contexts = new CacheContexts($this->getMockContainer(), $this->getContextsFixture()); $contexts = $cache_contexts->getAll(); - $this->assertEquals(array("foo"), $contexts); + $this->assertEquals(array("foo", "baz"), $contexts); } public function testAvailableContextLabels() { $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()); $labels = $cache_contexts->getLabels(); $expected = array("foo" => "Foo"); @@ -69,14 +87,22 @@ public function testAvailableContextLabels() { } protected function getContextsFixture() { - return array('foo'); + return array('foo', 'baz'); } protected function getMockContainer() { - return $this->getMockBuilder('Drupal\Core\DependencyInjection\Container') - ->disableOriginalConstructor() - ->getMock(); + $container = $this->getMockBuilder('Drupal\Core\DependencyInjection\Container') + ->disableOriginalConstructor() + ->getMock(); + $container->expects($this->any()) + ->method('get') + ->will($this->returnValueMap([ + ['cache_context.foo', Container::EXCEPTION_ON_INVALID_REFERENCE, new FooCacheContext()], + ['cache_context.baz', Container::EXCEPTION_ON_INVALID_REFERENCE, new BazCacheContext()], + ])); + return $container; } + } /** @@ -100,3 +126,26 @@ public function getContext() { } +/** + * Fake calculated cache context class. + */ +class BazCacheContext implements CalculatedCacheContextInterface { + + /** + * {@inheritdoc} + */ + public static function getLabel() { + return 'Baz'; + } + + /** + * {@inheritdoc} + */ + public function getContext($parameter) { + if (!is_string($parameter) || strlen($parameter) === 0) { + throw new \Exception; + } + return 'baz.' . str_rot13($parameter); + } + +}