Loading core/lib/Drupal/Core/Cache/Context/CookiesCacheContext.php +6 −1 Changes for core/lib/Drupal/Core/Cache/Context/CookiesCacheContext.php: 6 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -25,7 +25,12 @@ public static function getLabel() { */ public function getContext($cookie = NULL) { if ($cookie === NULL) { return $this->requestStack->getCurrentRequest()->cookies->all(); $cookies = $this->requestStack->getCurrentRequest()->cookies->all(); // Sort the cookies by names, to always set the same context if the cookies // are the same but in a different order. ksort($cookies); // Use http_build_query() to get a short string from the cookies array. return http_build_query($cookies); } else { return $this->requestStack->getCurrentRequest()->cookies->get($cookie); Loading core/tests/Drupal/Tests/Core/Cache/Context/CookieCacheContextTest.php 0 → 100644 +43 −0 Changes for core/tests/Drupal/Tests/Core/Cache/Context/CookieCacheContextTest.php: 43 added lines, 0 removed lines. Original line number Diff line number Diff line <?php namespace Drupal\Tests\Core\Cache\Context; use Drupal\Core\Cache\Context\CookiesCacheContext; use Drupal\Tests\UnitTestCase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; /** * @coversDefaultClass \Drupal\Core\Cache\Context\CookiesCacheContext * @group Cache */ class CookieCacheContextTest extends UnitTestCase { /** * @covers ::getContext * * @dataProvider providerTestGetContext */ public function testGetContext($cookies, $cookie_name, $context) { $request_stack = new RequestStack(); $request = Request::create('/', 'GET'); foreach ($cookies as $cookie => $value) { $request->cookies->set($cookie, $value); } $request_stack->push($request); $cache_context = new CookiesCacheContext($request_stack); $this->assertSame($cache_context->getContext($cookie_name), $context); } /** * Provides a list of cookies and expected cache contexts. */ public function providerTestGetContext() { return [ [['foo' => 1, 'bar' => 2, 'baz' => 3], 'foo', 1], // Context is ordered by cookie name. [['foo' => 1, 'bar' => 2, 'baz' => 3], NULL, 'bar=2&baz=3&foo=1'], ]; } } Loading
core/lib/Drupal/Core/Cache/Context/CookiesCacheContext.php +6 −1 Changes for core/lib/Drupal/Core/Cache/Context/CookiesCacheContext.php: 6 added lines, 1 removed line. Original line number Diff line number Diff line Loading @@ -25,7 +25,12 @@ public static function getLabel() { */ public function getContext($cookie = NULL) { if ($cookie === NULL) { return $this->requestStack->getCurrentRequest()->cookies->all(); $cookies = $this->requestStack->getCurrentRequest()->cookies->all(); // Sort the cookies by names, to always set the same context if the cookies // are the same but in a different order. ksort($cookies); // Use http_build_query() to get a short string from the cookies array. return http_build_query($cookies); } else { return $this->requestStack->getCurrentRequest()->cookies->get($cookie); Loading
core/tests/Drupal/Tests/Core/Cache/Context/CookieCacheContextTest.php 0 → 100644 +43 −0 Changes for core/tests/Drupal/Tests/Core/Cache/Context/CookieCacheContextTest.php: 43 added lines, 0 removed lines. Original line number Diff line number Diff line <?php namespace Drupal\Tests\Core\Cache\Context; use Drupal\Core\Cache\Context\CookiesCacheContext; use Drupal\Tests\UnitTestCase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; /** * @coversDefaultClass \Drupal\Core\Cache\Context\CookiesCacheContext * @group Cache */ class CookieCacheContextTest extends UnitTestCase { /** * @covers ::getContext * * @dataProvider providerTestGetContext */ public function testGetContext($cookies, $cookie_name, $context) { $request_stack = new RequestStack(); $request = Request::create('/', 'GET'); foreach ($cookies as $cookie => $value) { $request->cookies->set($cookie, $value); } $request_stack->push($request); $cache_context = new CookiesCacheContext($request_stack); $this->assertSame($cache_context->getContext($cookie_name), $context); } /** * Provides a list of cookies and expected cache contexts. */ public function providerTestGetContext() { return [ [['foo' => 1, 'bar' => 2, 'baz' => 3], 'foo', 1], // Context is ordered by cookie name. [['foo' => 1, 'bar' => 2, 'baz' => 3], NULL, 'bar=2&baz=3&foo=1'], ]; } }