Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Cache / CacheTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Cache;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\Core\Cache\Cache
10  * @group Cache
11  */
12 class CacheTest extends UnitTestCase {
13
14   /**
15    * Provides a list of cache tags arrays.
16    *
17    * @return array
18    */
19   public function validateTagsProvider() {
20     return [
21       [[], FALSE],
22       [['foo'], FALSE],
23       [['foo', 'bar'], FALSE],
24       [['foo', 'bar', 'llama:2001988', 'baz', 'llama:14031991'], FALSE],
25       // Invalid.
26       [[FALSE], 'Cache tags must be strings, boolean given.'],
27       [[TRUE], 'Cache tags must be strings, boolean given.'],
28       [['foo', FALSE], 'Cache tags must be strings, boolean given.'],
29       [[NULL], 'Cache tags must be strings, NULL given.'],
30       [['foo', NULL], 'Cache tags must be strings, NULL given.'],
31       [[1337], 'Cache tags must be strings, integer given.'],
32       [['foo', 1337], 'Cache tags must be strings, integer given.'],
33       [[3.14], 'Cache tags must be strings, double given.'],
34       [['foo', 3.14], 'Cache tags must be strings, double given.'],
35       [[[]], 'Cache tags must be strings, array given.'],
36       [['foo', []], 'Cache tags must be strings, array given.'],
37       [['foo', ['bar']], 'Cache tags must be strings, array given.'],
38       [[new \stdClass()], 'Cache tags must be strings, object given.'],
39       [['foo', new \stdClass()], 'Cache tags must be strings, object given.'],
40     ];
41   }
42
43   /**
44    * @covers ::validateTags
45    *
46    * @dataProvider validateTagsProvider
47    */
48   public function testValidateTags(array $tags, $expected_exception_message) {
49     if ($expected_exception_message !== FALSE) {
50       $this->setExpectedException('LogicException', $expected_exception_message);
51     }
52     // If it doesn't throw an exception, validateTags() returns NULL.
53     $this->assertNull(Cache::validateTags($tags));
54   }
55
56
57   /**
58    * Provides a list of pairs of cache tags arrays to be merged.
59    *
60    * @return array
61    */
62   public function mergeTagsProvider() {
63     return [
64       [[], [], []],
65       [['bar'], ['foo'], ['bar', 'foo']],
66       [['foo'], ['bar'], ['bar', 'foo']],
67       [['foo'], ['bar', 'foo'], ['bar', 'foo']],
68       [['foo'], ['foo', 'bar'], ['bar', 'foo']],
69       [['bar', 'foo'], ['foo', 'bar'], ['bar', 'foo']],
70       [['foo', 'bar'], ['foo', 'bar'], ['bar', 'foo']],
71     ];
72   }
73
74   /**
75    * @covers ::mergeTags
76    *
77    * @dataProvider mergeTagsProvider
78    */
79   public function testMergeTags(array $a, array $b, array $expected) {
80     $this->assertEquals($expected, Cache::mergeTags($a, $b));
81   }
82
83   /**
84    * Provides a list of pairs of cache tags arrays to be merged.
85    *
86    * @return array
87    */
88   public function mergeMaxAgesProvider() {
89     return [
90       [Cache::PERMANENT, Cache::PERMANENT, Cache::PERMANENT],
91       [60, 60, 60],
92       [0, 0, 0],
93
94       [60, 0, 0],
95       [0, 60, 0],
96
97       [Cache::PERMANENT, 0, 0],
98       [0, Cache::PERMANENT, 0],
99
100       [Cache::PERMANENT, 60, 60],
101       [60, Cache::PERMANENT, 60],
102     ];
103   }
104
105
106   /**
107    * @covers ::mergeMaxAges
108    *
109    * @dataProvider mergeMaxAgesProvider
110    */
111   public function testMergeMaxAges($a, $b, $expected) {
112     $this->assertSame($expected, Cache::mergeMaxAges($a, $b));
113   }
114
115   /**
116    * Provides a list of pairs of (prefix, suffixes) to build cache tags from.
117    *
118    * @return array
119    */
120   public function buildTagsProvider() {
121     return [
122       ['node', [1], ['node:1']],
123       ['node', [1, 2, 3], ['node:1', 'node:2', 'node:3']],
124       ['node', [3, 2, 1], ['node:3', 'node:2', 'node:1']],
125       ['node', [NULL], ['node:']],
126       ['node', [TRUE, FALSE], ['node:1', 'node:']],
127       ['node', ['a', 'z', 'b'], ['node:a', 'node:z', 'node:b']],
128       // No suffixes, no cache tags.
129       ['', [], []],
130       ['node', [], []],
131       // Only suffix values matter, not keys: verify that expectation.
132       ['node', [5 => 145, 4545 => 3], ['node:145', 'node:3']],
133       ['node', [5 => TRUE], ['node:1']],
134       ['node', [5 => NULL], ['node:']],
135       ['node', ['a' => NULL], ['node:']],
136       ['node', ['a' => TRUE], ['node:1']],
137       // Test the $glue parameter.
138       ['config:system.menu', ['menu_name'], ['config:system.menu.menu_name'], '.'],
139     ];
140   }
141
142   /**
143    * @covers ::buildTags
144    *
145    * @dataProvider buildTagsProvider
146    */
147   public function testBuildTags($prefix, array $suffixes, array $expected, $glue = ':') {
148     $this->assertEquals($expected, Cache::buildTags($prefix, $suffixes, $glue));
149   }
150
151 }