Version 1
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Cache / Context / QueryArgsCacheContextTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Cache\Context;
4
5 use Drupal\Core\Cache\Context\QueryArgsCacheContext;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\RequestStack;
9
10 /**
11  * @coversDefaultClass \Drupal\Core\Cache\Context\QueryArgsCacheContext
12  * @group Cache
13  */
14 class QueryArgsCacheContextTest extends UnitTestCase {
15
16   /**
17    * @covers ::getContext
18    *
19    * @dataProvider providerTestGetContext
20    */
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);
27   }
28
29   /**
30    * Provides a list of query arguments and expected cache contexts.
31    */
32   public function providerTestGetContext() {
33     return [
34       [[], NULL, ''],
35       [[], 'foo', ''],
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'],
46     ];
47   }
48
49 }