3 namespace Drupal\Tests\Core\Cache;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Tests\UnitTestCase;
9 * @coversDefaultClass \Drupal\Core\Cache\Cache
12 class CacheTest extends UnitTestCase {
15 * Provides a list of cache tags arrays.
19 public function validateTagsProvider() {
23 [['foo', 'bar'], FALSE],
24 [['foo', 'bar', 'llama:2001988', 'baz', 'llama:14031991'], FALSE],
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.'],
44 * @covers ::validateTags
46 * @dataProvider validateTagsProvider
48 public function testValidateTags(array $tags, $expected_exception_message) {
49 if ($expected_exception_message !== FALSE) {
50 $this->setExpectedException('LogicException', $expected_exception_message);
52 // If it doesn't throw an exception, validateTags() returns NULL.
53 $this->assertNull(Cache::validateTags($tags));
58 * Provides a list of pairs of cache tags arrays to be merged.
62 public function mergeTagsProvider() {
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']],
77 * @dataProvider mergeTagsProvider
79 public function testMergeTags(array $a, array $b, array $expected) {
80 $this->assertEquals($expected, Cache::mergeTags($a, $b));
84 * Provides a list of pairs of cache tags arrays to be merged.
88 public function mergeMaxAgesProvider() {
90 [Cache::PERMANENT, Cache::PERMANENT, Cache::PERMANENT],
97 [Cache::PERMANENT, 0, 0],
98 [0, Cache::PERMANENT, 0],
100 [Cache::PERMANENT, 60, 60],
101 [60, Cache::PERMANENT, 60],
107 * @covers ::mergeMaxAges
109 * @dataProvider mergeMaxAgesProvider
111 public function testMergeMaxAges($a, $b, $expected) {
112 $this->assertSame($expected, Cache::mergeMaxAges($a, $b));
116 * Provides a list of pairs of (prefix, suffixes) to build cache tags from.
120 public function buildTagsProvider() {
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.
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'], '.'],
143 * @covers ::buildTags
145 * @dataProvider buildTagsProvider
147 public function testBuildTags($prefix, array $suffixes, array $expected, $glue = ':') {
148 $this->assertEquals($expected, Cache::buildTags($prefix, $suffixes, $glue));