Unverified Commit 163191cf 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

(cherry picked from commit fd664f78)
parent 3a8f5c16
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -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);
+43 −0
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'],
    ];
  }

}