3 namespace Drupal\Tests\Core\Cache\Context;
5 use Drupal\Core\Cache\Context\QueryArgsCacheContext;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\RequestStack;
11 * @coversDefaultClass \Drupal\Core\Cache\Context\QueryArgsCacheContext
14 class QueryArgsCacheContextTest extends UnitTestCase {
17 * @covers ::getContext
19 * @dataProvider providerTestGetContext
21 public function testGetContext(array $query_args, $cache_context_parameter, $context) {
22 $request_stack = new RequestStack();
23 $request = Request::create('/', 'GET', $query_args);
24 $request_stack->push($request);
25 $cache_context = new QueryArgsCacheContext($request_stack);
26 $this->assertSame($cache_context->getContext($cache_context_parameter), $context);
30 * Provides a list of query arguments and expected cache contexts.
32 public function providerTestGetContext() {
36 // Non-empty query arguments.
37 [['llama' => 'rocks', 'alpaca' => '', 'panda' => 'drools', 'z' => '0'], NULL, 'alpaca=&llama=rocks&panda=drools&z=0'],
38 [['llama' => 'rocks', 'alpaca' => '', 'panda' => 'drools', 'z' => '0'], 'llama', 'rocks'],
39 [['llama' => 'rocks', 'alpaca' => '', 'panda' => 'drools', 'z' => '0'], 'alpaca', '?valueless?'],
40 [['llama' => 'rocks', 'alpaca' => '', 'panda' => 'drools', 'z' => '0'], 'panda', 'drools'],
41 [['llama' => 'rocks', 'alpaca' => '', 'panda' => 'drools', 'z' => '0'], 'z', '0'],
42 [['llama' => 'rocks', 'alpaca' => '', 'panda' => 'drools', 'z' => '0'], 'chicken', ''],
43 [['llama' => ['rocks', 'kitty']], 'llama', '0=rocks&1=kitty'],
44 [['llama' => ['rocks' => 'fuzzball', 'monkey' => 'patch']], 'llama', 'rocks=fuzzball&monkey=patch'],
45 [['llama' => ['rocks' => ['nested', 'bonobo']]], 'llama', 'rocks%5B0%5D=nested&rocks%5B1%5D=bonobo'],