f3824e8d5009b4d8ce40a3fb44e6ed277d244da0
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Cache / CacheableMetadataTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Cache;
4
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;
10
11 /**
12  * @coversDefaultClass \Drupal\Core\Cache\CacheableMetadata
13  * @group Cache
14  */
15 class CacheableMetadataTest extends UnitTestCase {
16
17   /**
18    * @covers ::merge
19    * @dataProvider providerTestMerge
20    *
21    * This only tests at a high level, because it reuses existing logic. Detailed
22    * tests exist for the existing logic:
23    *
24    * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeTags()
25    * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeMaxAges()
26    * @see \Drupal\Tests\Core\Cache\CacheContextsTest
27    */
28   public function testMerge(CacheableMetadata $a, CacheableMetadata $b, CacheableMetadata $expected) {
29     $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
30       ->disableOriginalConstructor()
31       ->getMock();
32     $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
33
34     $container = new ContainerBuilder();
35     $container->set('cache_contexts_manager', $cache_contexts_manager);
36     \Drupal::setContainer($container);
37
38     $this->assertEquals($expected, $a->merge($b));
39   }
40
41   /**
42    * @covers ::addCacheableDependency
43    * @dataProvider providerTestMerge
44    *
45    * This only tests at a high level, because it reuses existing logic. Detailed
46    * tests exist for the existing logic:
47    *
48    * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeTags()
49    * @see \Drupal\Tests\Core\Cache\CacheTest::testMergeMaxAges()
50    * @see \Drupal\Tests\Core\Cache\CacheContextsTest
51    */
52   public function testAddCacheableDependency(CacheableMetadata $a, CacheableMetadata $b, CacheableMetadata $expected) {
53     $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
54       ->disableOriginalConstructor()
55       ->getMock();
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);
60
61     $this->assertEquals($expected, $a->addCacheableDependency($b));
62   }
63
64   /**
65    * Provides test data for testMerge().
66    *
67    * @return array
68    */
69   public function providerTestMerge() {
70     return [
71       // All empty.
72       [(new CacheableMetadata()), (new CacheableMetadata()), (new CacheableMetadata())],
73       // Cache contexts.
74       [(new CacheableMetadata())->setCacheContexts(['foo']), (new CacheableMetadata())->setCacheContexts(['bar']), (new CacheableMetadata())->setCacheContexts(['bar', 'foo'])],
75       // Cache tags.
76       [(new CacheableMetadata())->setCacheTags(['foo']), (new CacheableMetadata())->setCacheTags(['bar']), (new CacheableMetadata())->setCacheTags(['bar', 'foo'])],
77       // Cache max-ages.
78       [(new CacheableMetadata())->setCacheMaxAge(60), (new CacheableMetadata())->setCacheMaxAge(Cache::PERMANENT), (new CacheableMetadata())->setCacheMaxAge(60)],
79     ];
80   }
81
82   /**
83    * This delegates to Cache::mergeTags(), so just a basic test.
84    *
85    * @covers ::addCacheTags
86    */
87   public function testAddCacheTags() {
88     $metadata = new CacheableMetadata();
89     $add_expected = [
90       [[], []],
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']],
96     ];
97
98     foreach ($add_expected as $data) {
99       list($add, $expected) = $data;
100       $metadata->addCacheTags($add);
101       $this->assertEquals($expected, $metadata->getCacheTags());
102     }
103   }
104
105   /**
106    * Test valid and invalid values as max age.
107    *
108    * @covers ::setCacheMaxAge
109    * @dataProvider providerSetCacheMaxAge
110    */
111   public function testSetCacheMaxAge($data, $expect_exception) {
112     $metadata = new CacheableMetadata();
113     if ($expect_exception) {
114       $this->setExpectedException('\InvalidArgumentException');
115     }
116     $metadata->setCacheMaxAge($data);
117     $this->assertEquals($data, $metadata->getCacheMaxAge());
118   }
119
120   /**
121    * Data provider for testSetCacheMaxAge.
122    */
123   public function providerSetCacheMaxAge() {
124     return [
125       [0 , FALSE],
126       ['http', TRUE],
127       ['0', TRUE],
128       [new \stdClass(), TRUE],
129       [300, FALSE],
130       [[], TRUE],
131       [8.0, TRUE]
132    ];
133   }
134
135   /**
136    * @covers ::createFromRenderArray
137    * @dataProvider providerTestCreateFromRenderArray
138    */
139   public function testCreateFromRenderArray(array $render_array, CacheableMetadata $expected) {
140     $this->assertEquals($expected, CacheableMetadata::createFromRenderArray($render_array));
141   }
142
143   /**
144    * Provides test data for createFromRenderArray().
145    *
146    * @return array
147    */
148   public function providerTestCreateFromRenderArray() {
149     $data = [];
150
151     $empty_metadata = new CacheableMetadata();
152     $nonempty_metadata = new CacheableMetadata();
153     $nonempty_metadata->setCacheContexts(['qux'])
154       ->setCacheTags(['foo:bar']);
155
156     $empty_render_array = [];
157     $nonempty_render_array = [
158       '#cache' => [
159         'contexts' => ['qux'],
160         'tags' => ['foo:bar'],
161         'max-age' => Cache::PERMANENT,
162       ],
163     ];
164
165     $data[] = [$empty_render_array, $empty_metadata];
166     $data[] = [$nonempty_render_array, $nonempty_metadata];
167
168     return $data;
169   }
170
171   /**
172    * @covers ::createFromObject
173    * @dataProvider providerTestCreateFromObject
174    */
175   public function testCreateFromObject($object, CacheableMetadata $expected) {
176     $this->assertEquals($expected, CacheableMetadata::createFromObject($object));
177   }
178
179   /**
180    * Provides test data for createFromObject().
181    *
182    * @return array
183    */
184   public function providerTestCreateFromObject() {
185     $data = [];
186
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);
194
195     $empty_cacheable_object = new TestCacheableDependency([], [], Cache::PERMANENT);
196     $nonempty_cacheable_object = new TestCacheableDependency(['qux'], ['foo:bar'], 600);
197     $uncacheable_object = new \stdClass();
198
199     $data[] = [$empty_cacheable_object, $empty_metadata];
200     $data[] = [$nonempty_cacheable_object, $nonempty_metadata];
201     $data[] = [$uncacheable_object, $uncacheable_metadata];
202
203     return $data;
204   }
205
206 }