3 namespace Drupal\Tests\Core\Cache;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Core\Cache\CacheableMetadata;
7 use Drupal\Tests\Core\Render\TestCacheableDependency;
8 use Drupal\Tests\UnitTestCase;
9 use Symfony\Component\DependencyInjection\ContainerBuilder;
12 * @coversDefaultClass \Drupal\Core\Cache\CacheableMetadata
15 class CacheableMetadataTest extends UnitTestCase {
19 * @dataProvider providerTestMerge
21 * This only tests at a high level, because it reuses existing logic. Detailed
22 * tests exist for the existing logic:
24 * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeTags()
25 * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeMaxAges()
26 * @see \Drupal\Tests\Core\Cache\CacheContextsTest
28 public function testMerge(CacheableMetadata $a, CacheableMetadata $b, CacheableMetadata $expected) {
29 $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
30 ->disableOriginalConstructor()
32 $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
34 $container = new ContainerBuilder();
35 $container->set('cache_contexts_manager', $cache_contexts_manager);
36 \Drupal::setContainer($container);
38 $this->assertEquals($expected, $a->merge($b));
42 * @covers ::addCacheableDependency
43 * @dataProvider providerTestMerge
45 * This only tests at a high level, because it reuses existing logic. Detailed
46 * tests exist for the existing logic:
48 * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeTags()
49 * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeMaxAges()
50 * @see \Drupal\Tests\Core\Cache\CacheContextsTest
52 public function testAddCacheableDependency(CacheableMetadata $a, CacheableMetadata $b, CacheableMetadata $expected) {
53 $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
54 ->disableOriginalConstructor()
56 $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
57 $container = new ContainerBuilder();
58 $container->set('cache_contexts_manager', $cache_contexts_manager);
59 \Drupal::setContainer($container);
61 $this->assertEquals($expected, $a->addCacheableDependency($b));
65 * Provides test data for testMerge().
69 public function providerTestMerge() {
72 [(new CacheableMetadata()), (new CacheableMetadata()), (new CacheableMetadata())],
74 [(new CacheableMetadata())->setCacheContexts(['foo']), (new CacheableMetadata())->setCacheContexts(['bar']), (new CacheableMetadata())->setCacheContexts(['bar', 'foo'])],
76 [(new CacheableMetadata())->setCacheTags(['foo']), (new CacheableMetadata())->setCacheTags(['bar']), (new CacheableMetadata())->setCacheTags(['bar', 'foo'])],
78 [(new CacheableMetadata())->setCacheMaxAge(60), (new CacheableMetadata())->setCacheMaxAge(Cache::PERMANENT), (new CacheableMetadata())->setCacheMaxAge(60)],
83 * This delegates to Cache::mergeTags(), so just a basic test.
85 * @covers ::addCacheTags
87 public function testAddCacheTags() {
88 $metadata = new CacheableMetadata();
91 [['foo:bar'], ['foo:bar']],
92 [['foo:baz'], ['foo:bar', 'foo:baz']],
93 [['axx:first', 'foo:baz'], ['axx:first', 'foo:bar', 'foo:baz']],
94 [[], ['axx:first', 'foo:bar', 'foo:baz']],
95 [['axx:first'], ['axx:first', 'foo:bar', 'foo:baz']],
98 foreach ($add_expected as $data) {
99 list($add, $expected) = $data;
100 $metadata->addCacheTags($add);
101 $this->assertEquals($expected, $metadata->getCacheTags());
106 * Test valid and invalid values as max age.
108 * @covers ::setCacheMaxAge
109 * @dataProvider providerSetCacheMaxAge
111 public function testSetCacheMaxAge($data, $expect_exception) {
112 $metadata = new CacheableMetadata();
113 if ($expect_exception) {
114 $this->setExpectedException('\InvalidArgumentException');
116 $metadata->setCacheMaxAge($data);
117 $this->assertEquals($data, $metadata->getCacheMaxAge());
121 * Data provider for testSetCacheMaxAge.
123 public function providerSetCacheMaxAge() {
128 [new \stdClass(), TRUE],
136 * @covers ::createFromRenderArray
137 * @dataProvider providerTestCreateFromRenderArray
139 public function testCreateFromRenderArray(array $render_array, CacheableMetadata $expected) {
140 $this->assertEquals($expected, CacheableMetadata::createFromRenderArray($render_array));
144 * Provides test data for createFromRenderArray().
148 public function providerTestCreateFromRenderArray() {
151 $empty_metadata = new CacheableMetadata();
152 $nonempty_metadata = new CacheableMetadata();
153 $nonempty_metadata->setCacheContexts(['qux'])
154 ->setCacheTags(['foo:bar']);
156 $empty_render_array = [];
157 $nonempty_render_array = [
159 'contexts' => ['qux'],
160 'tags' => ['foo:bar'],
161 'max-age' => Cache::PERMANENT,
165 $data[] = [$empty_render_array, $empty_metadata];
166 $data[] = [$nonempty_render_array, $nonempty_metadata];
172 * @covers ::createFromObject
173 * @dataProvider providerTestCreateFromObject
175 public function testCreateFromObject($object, CacheableMetadata $expected) {
176 $this->assertEquals($expected, CacheableMetadata::createFromObject($object));
180 * Provides test data for createFromObject().
184 public function providerTestCreateFromObject() {
187 $empty_metadata = new CacheableMetadata();
188 $nonempty_metadata = new CacheableMetadata();
189 $nonempty_metadata->setCacheContexts(['qux'])
190 ->setCacheTags(['foo:bar'])
191 ->setCacheMaxAge(600);
192 $uncacheable_metadata = new CacheableMetadata();
193 $uncacheable_metadata->setCacheMaxAge(0);
195 $empty_cacheable_object = new TestCacheableDependency([], [], Cache::PERMANENT);
196 $nonempty_cacheable_object = new TestCacheableDependency(['qux'], ['foo:bar'], 600);
197 $uncacheable_object = new \stdClass();
199 $data[] = [$empty_cacheable_object, $empty_metadata];
200 $data[] = [$nonempty_cacheable_object, $nonempty_metadata];
201 $data[] = [$uncacheable_object, $uncacheable_metadata];