Skip to content
Snippets Groups Projects
Unverified Commit fd664f78 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2873732 by vijaycs85, GaëlG: Array to string conversion in...

Issue #2873732 by vijaycs85, GaëlG: Array to string conversion in CacheContextsManager::convertTokensToKeys() because of the 'cookies' cache context
parent b9a61af9
No related branches found
No related tags found
No related merge requests found
......@@ -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);
......
<?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'],
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment