Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\system\Tests\Cache;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Cache\Cache;
|
Chris@0
|
6 use Drupal\Core\Cache\CacheBackendInterface;
|
Chris@0
|
7 use Drupal\simpletest\KernelTestBase;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Tests any cache backend.
|
Chris@0
|
11 *
|
Chris@0
|
12 * Full generic unit test suite for any cache backend. In order to use it for a
|
Chris@0
|
13 * cache backend implementation, extend this class and override the
|
Chris@0
|
14 * createBackendInstance() method to return an object.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @see DatabaseBackendUnitTestCase
|
Chris@0
|
17 * For a full working implementation.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @deprecated as of Drupal 8.2.x, will be removed before Drupal 9.0.0. Use
|
Chris@0
|
20 * \Drupal\KernelTests\Core\Cache\GenericCacheBackendUnitTestBase instead.
|
Chris@0
|
21 */
|
Chris@0
|
22 abstract class GenericCacheBackendUnitTestBase extends KernelTestBase {
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * Array of objects implementing Drupal\Core\Cache\CacheBackendInterface.
|
Chris@0
|
26 *
|
Chris@0
|
27 * @var array
|
Chris@0
|
28 */
|
Chris@0
|
29 protected $cachebackends;
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * Cache bin to use for testing.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @var string
|
Chris@0
|
35 */
|
Chris@0
|
36 protected $testBin;
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Random value to use in tests.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @var string
|
Chris@0
|
42 */
|
Chris@0
|
43 protected $defaultValue;
|
Chris@0
|
44
|
Chris@0
|
45 /**
|
Chris@0
|
46 * Gets the testing bin.
|
Chris@0
|
47 *
|
Chris@0
|
48 * Override this method if you want to work on a different bin than the
|
Chris@0
|
49 * default one.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @return string
|
Chris@0
|
52 * Bin name.
|
Chris@0
|
53 */
|
Chris@0
|
54 protected function getTestBin() {
|
Chris@0
|
55 if (!isset($this->testBin)) {
|
Chris@0
|
56 $this->testBin = 'page';
|
Chris@0
|
57 }
|
Chris@0
|
58 return $this->testBin;
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * Creates a cache backend to test.
|
Chris@0
|
63 *
|
Chris@0
|
64 * Override this method to test a CacheBackend.
|
Chris@0
|
65 *
|
Chris@0
|
66 * @param string $bin
|
Chris@0
|
67 * Bin name to use for this backend instance.
|
Chris@0
|
68 *
|
Chris@0
|
69 * @return \Drupal\Core\Cache\CacheBackendInterface
|
Chris@0
|
70 * Cache backend to test.
|
Chris@0
|
71 */
|
Chris@0
|
72 abstract protected function createCacheBackend($bin);
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * Allows specific implementation to change the environment before a test run.
|
Chris@0
|
76 */
|
Chris@0
|
77 public function setUpCacheBackend() {
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * Allows alteration of environment after a test run but before tear down.
|
Chris@0
|
82 *
|
Chris@0
|
83 * Used before the real tear down because the tear down will change things
|
Chris@0
|
84 * such as the database prefix.
|
Chris@0
|
85 */
|
Chris@0
|
86 public function tearDownCacheBackend() {
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 /**
|
Chris@0
|
90 * Gets a backend to test; this will get a shared instance set in the object.
|
Chris@0
|
91 *
|
Chris@0
|
92 * @return \Drupal\Core\Cache\CacheBackendInterface
|
Chris@0
|
93 * Cache backend to test.
|
Chris@0
|
94 */
|
Chris@0
|
95 protected function getCacheBackend($bin = NULL) {
|
Chris@0
|
96 if (!isset($bin)) {
|
Chris@0
|
97 $bin = $this->getTestBin();
|
Chris@0
|
98 }
|
Chris@0
|
99 if (!isset($this->cachebackends[$bin])) {
|
Chris@0
|
100 $this->cachebackends[$bin] = $this->createCacheBackend($bin);
|
Chris@0
|
101 // Ensure the backend is empty.
|
Chris@0
|
102 $this->cachebackends[$bin]->deleteAll();
|
Chris@0
|
103 }
|
Chris@0
|
104 return $this->cachebackends[$bin];
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@0
|
107 protected function setUp() {
|
Chris@0
|
108 $this->cachebackends = [];
|
Chris@0
|
109 $this->defaultValue = $this->randomMachineName(10);
|
Chris@0
|
110
|
Chris@0
|
111 parent::setUp();
|
Chris@0
|
112
|
Chris@0
|
113 $this->setUpCacheBackend();
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 protected function tearDown() {
|
Chris@0
|
117 // Destruct the registered backend, each test will get a fresh instance,
|
Chris@0
|
118 // properly emptying it here ensure that on persistent data backends they
|
Chris@0
|
119 // will come up empty the next test.
|
Chris@0
|
120 foreach ($this->cachebackends as $bin => $cachebackend) {
|
Chris@0
|
121 $this->cachebackends[$bin]->deleteAll();
|
Chris@0
|
122 }
|
Chris@0
|
123 unset($this->cachebackends);
|
Chris@0
|
124
|
Chris@0
|
125 $this->tearDownCacheBackend();
|
Chris@0
|
126
|
Chris@0
|
127 parent::tearDown();
|
Chris@0
|
128 }
|
Chris@0
|
129
|
Chris@0
|
130 /**
|
Chris@0
|
131 * Tests the get and set methods of Drupal\Core\Cache\CacheBackendInterface.
|
Chris@0
|
132 */
|
Chris@0
|
133 public function testSetGet() {
|
Chris@0
|
134 $backend = $this->getCacheBackend();
|
Chris@0
|
135
|
Chris@0
|
136 $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1.");
|
Chris@0
|
137 $with_backslash = ['foo' => '\Drupal\foo\Bar'];
|
Chris@0
|
138 $backend->set('test1', $with_backslash);
|
Chris@0
|
139 $cached = $backend->get('test1');
|
Chris@0
|
140 $this->assert(is_object($cached), "Backend returned an object for cache id test1.");
|
Chris@0
|
141 $this->assertIdentical($with_backslash, $cached->data);
|
Chris@0
|
142 $this->assertTrue($cached->valid, 'Item is marked as valid.');
|
Chris@0
|
143 // We need to round because microtime may be rounded up in the backend.
|
Chris@0
|
144 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
|
Chris@0
|
145 $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');
|
Chris@0
|
146
|
Chris@0
|
147 $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2.");
|
Chris@0
|
148 $backend->set('test2', ['value' => 3], REQUEST_TIME + 3);
|
Chris@0
|
149 $cached = $backend->get('test2');
|
Chris@0
|
150 $this->assert(is_object($cached), "Backend returned an object for cache id test2.");
|
Chris@0
|
151 $this->assertIdentical(['value' => 3], $cached->data);
|
Chris@0
|
152 $this->assertTrue($cached->valid, 'Item is marked as valid.');
|
Chris@0
|
153 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
|
Chris@0
|
154 $this->assertEqual($cached->expire, REQUEST_TIME + 3, 'Expire time is correct.');
|
Chris@0
|
155
|
Chris@0
|
156 $backend->set('test3', 'foobar', REQUEST_TIME - 3);
|
Chris@0
|
157 $this->assertFalse($backend->get('test3'), 'Invalid item not returned.');
|
Chris@0
|
158 $cached = $backend->get('test3', TRUE);
|
Chris@0
|
159 $this->assert(is_object($cached), 'Backend returned an object for cache id test3.');
|
Chris@0
|
160 $this->assertFalse($cached->valid, 'Item is marked as valid.');
|
Chris@0
|
161 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
|
Chris@0
|
162 $this->assertEqual($cached->expire, REQUEST_TIME - 3, 'Expire time is correct.');
|
Chris@0
|
163
|
Chris@0
|
164 $this->assertIdentical(FALSE, $backend->get('test4'), "Backend does not contain data for cache id test4.");
|
Chris@0
|
165 $with_eof = ['foo' => "\nEOF\ndata"];
|
Chris@0
|
166 $backend->set('test4', $with_eof);
|
Chris@0
|
167 $cached = $backend->get('test4');
|
Chris@0
|
168 $this->assert(is_object($cached), "Backend returned an object for cache id test4.");
|
Chris@0
|
169 $this->assertIdentical($with_eof, $cached->data);
|
Chris@0
|
170 $this->assertTrue($cached->valid, 'Item is marked as valid.');
|
Chris@0
|
171 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
|
Chris@0
|
172 $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');
|
Chris@0
|
173
|
Chris@0
|
174 $this->assertIdentical(FALSE, $backend->get('test5'), "Backend does not contain data for cache id test5.");
|
Chris@0
|
175 $with_eof_and_semicolon = ['foo' => "\nEOF;\ndata"];
|
Chris@0
|
176 $backend->set('test5', $with_eof_and_semicolon);
|
Chris@0
|
177 $cached = $backend->get('test5');
|
Chris@0
|
178 $this->assert(is_object($cached), "Backend returned an object for cache id test5.");
|
Chris@0
|
179 $this->assertIdentical($with_eof_and_semicolon, $cached->data);
|
Chris@0
|
180 $this->assertTrue($cached->valid, 'Item is marked as valid.');
|
Chris@0
|
181 $this->assertTrue($cached->created >= REQUEST_TIME && $cached->created <= round(microtime(TRUE), 3), 'Created time is correct.');
|
Chris@0
|
182 $this->assertEqual($cached->expire, Cache::PERMANENT, 'Expire time is correct.');
|
Chris@0
|
183
|
Chris@0
|
184 $with_variable = ['foo' => '$bar'];
|
Chris@0
|
185 $backend->set('test6', $with_variable);
|
Chris@0
|
186 $cached = $backend->get('test6');
|
Chris@0
|
187 $this->assert(is_object($cached), "Backend returned an object for cache id test6.");
|
Chris@0
|
188 $this->assertIdentical($with_variable, $cached->data);
|
Chris@0
|
189
|
Chris@0
|
190 // Make sure that a cached object is not affected by changing the original.
|
Chris@0
|
191 $data = new \stdClass();
|
Chris@0
|
192 $data->value = 1;
|
Chris@0
|
193 $data->obj = new \stdClass();
|
Chris@0
|
194 $data->obj->value = 2;
|
Chris@0
|
195 $backend->set('test7', $data);
|
Chris@0
|
196 $expected_data = clone $data;
|
Chris@0
|
197 // Add a property to the original. It should not appear in the cached data.
|
Chris@0
|
198 $data->this_should_not_be_in_the_cache = TRUE;
|
Chris@0
|
199 $cached = $backend->get('test7');
|
Chris@0
|
200 $this->assert(is_object($cached), "Backend returned an object for cache id test7.");
|
Chris@0
|
201 $this->assertEqual($expected_data, $cached->data);
|
Chris@0
|
202 $this->assertFalse(isset($cached->data->this_should_not_be_in_the_cache));
|
Chris@0
|
203 // Add a property to the cache data. It should not appear when we fetch
|
Chris@0
|
204 // the data from cache again.
|
Chris@0
|
205 $cached->data->this_should_not_be_in_the_cache = TRUE;
|
Chris@0
|
206 $fresh_cached = $backend->get('test7');
|
Chris@0
|
207 $this->assertFalse(isset($fresh_cached->data->this_should_not_be_in_the_cache));
|
Chris@0
|
208
|
Chris@0
|
209 // Check with a long key.
|
Chris@0
|
210 $cid = str_repeat('a', 300);
|
Chris@0
|
211 $backend->set($cid, 'test');
|
Chris@0
|
212 $this->assertEqual('test', $backend->get($cid)->data);
|
Chris@0
|
213
|
Chris@0
|
214 // Check that the cache key is case sensitive.
|
Chris@0
|
215 $backend->set('TEST8', 'value');
|
Chris@0
|
216 $this->assertEqual('value', $backend->get('TEST8')->data);
|
Chris@0
|
217 $this->assertFalse($backend->get('test8'));
|
Chris@0
|
218
|
Chris@0
|
219 // Calling ::set() with invalid cache tags. This should fail an assertion.
|
Chris@0
|
220 try {
|
Chris@0
|
221 $backend->set('assertion_test', 'value', Cache::PERMANENT, ['node' => [3, 5, 7]]);
|
Chris@0
|
222 $this->fail('::set() was called with invalid cache tags, runtime assertion did not fail.');
|
Chris@0
|
223 }
|
Chris@0
|
224 catch (\AssertionError $e) {
|
Chris@0
|
225 $this->pass('::set() was called with invalid cache tags, runtime assertion failed.');
|
Chris@0
|
226 }
|
Chris@0
|
227 }
|
Chris@0
|
228
|
Chris@0
|
229 /**
|
Chris@0
|
230 * Tests Drupal\Core\Cache\CacheBackendInterface::delete().
|
Chris@0
|
231 */
|
Chris@0
|
232 public function testDelete() {
|
Chris@0
|
233 $backend = $this->getCacheBackend();
|
Chris@0
|
234
|
Chris@0
|
235 $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1.");
|
Chris@0
|
236 $backend->set('test1', 7);
|
Chris@0
|
237 $this->assert(is_object($backend->get('test1')), "Backend returned an object for cache id test1.");
|
Chris@0
|
238
|
Chris@0
|
239 $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2.");
|
Chris@0
|
240 $backend->set('test2', 3);
|
Chris@0
|
241 $this->assert(is_object($backend->get('test2')), "Backend returned an object for cache id %cid.");
|
Chris@0
|
242
|
Chris@0
|
243 $backend->delete('test1');
|
Chris@0
|
244 $this->assertIdentical(FALSE, $backend->get('test1'), "Backend does not contain data for cache id test1 after deletion.");
|
Chris@0
|
245
|
Chris@0
|
246 $this->assert(is_object($backend->get('test2')), "Backend still has an object for cache id test2.");
|
Chris@0
|
247
|
Chris@0
|
248 $backend->delete('test2');
|
Chris@0
|
249 $this->assertIdentical(FALSE, $backend->get('test2'), "Backend does not contain data for cache id test2 after deletion.");
|
Chris@0
|
250
|
Chris@0
|
251 $long_cid = str_repeat('a', 300);
|
Chris@0
|
252 $backend->set($long_cid, 'test');
|
Chris@0
|
253 $backend->delete($long_cid);
|
Chris@0
|
254 $this->assertIdentical(FALSE, $backend->get($long_cid), "Backend does not contain data for long cache id after deletion.");
|
Chris@0
|
255 }
|
Chris@0
|
256
|
Chris@0
|
257 /**
|
Chris@0
|
258 * Tests data type preservation.
|
Chris@0
|
259 */
|
Chris@0
|
260 public function testValueTypeIsKept() {
|
Chris@0
|
261 $backend = $this->getCacheBackend();
|
Chris@0
|
262
|
Chris@0
|
263 $variables = [
|
Chris@0
|
264 'test1' => 1,
|
Chris@0
|
265 'test2' => '0',
|
Chris@0
|
266 'test3' => '',
|
Chris@0
|
267 'test4' => 12.64,
|
Chris@0
|
268 'test5' => FALSE,
|
Chris@0
|
269 'test6' => [1, 2, 3],
|
Chris@0
|
270 ];
|
Chris@0
|
271
|
Chris@0
|
272 // Create cache entries.
|
Chris@0
|
273 foreach ($variables as $cid => $data) {
|
Chris@0
|
274 $backend->set($cid, $data);
|
Chris@0
|
275 }
|
Chris@0
|
276
|
Chris@0
|
277 // Retrieve and test cache objects.
|
Chris@0
|
278 foreach ($variables as $cid => $value) {
|
Chris@0
|
279 $object = $backend->get($cid);
|
Chris@0
|
280 $this->assert(is_object($object), sprintf("Backend returned an object for cache id %s.", $cid));
|
Chris@0
|
281 $this->assertIdentical($value, $object->data, sprintf("Data of cached id %s kept is identical in type and value", $cid));
|
Chris@0
|
282 }
|
Chris@0
|
283 }
|
Chris@0
|
284
|
Chris@0
|
285 /**
|
Chris@0
|
286 * Tests Drupal\Core\Cache\CacheBackendInterface::getMultiple().
|
Chris@0
|
287 */
|
Chris@0
|
288 public function testGetMultiple() {
|
Chris@0
|
289 $backend = $this->getCacheBackend();
|
Chris@0
|
290
|
Chris@0
|
291 // Set numerous testing keys.
|
Chris@0
|
292 $long_cid = str_repeat('a', 300);
|
Chris@0
|
293 $backend->set('test1', 1);
|
Chris@0
|
294 $backend->set('test2', 3);
|
Chris@0
|
295 $backend->set('test3', 5);
|
Chris@0
|
296 $backend->set('test4', 7);
|
Chris@0
|
297 $backend->set('test5', 11);
|
Chris@0
|
298 $backend->set('test6', 13);
|
Chris@0
|
299 $backend->set('test7', 17);
|
Chris@0
|
300 $backend->set($long_cid, 300);
|
Chris@0
|
301
|
Chris@0
|
302 // Mismatch order for harder testing.
|
Chris@0
|
303 $reference = [
|
Chris@0
|
304 'test3',
|
Chris@0
|
305 'test7',
|
Chris@0
|
306 // Cid does not exist.
|
Chris@0
|
307 'test21',
|
Chris@0
|
308 'test6',
|
Chris@0
|
309 // Cid does not exist until added before second getMultiple().
|
Chris@0
|
310 'test19',
|
Chris@0
|
311 'test2',
|
Chris@0
|
312 ];
|
Chris@0
|
313
|
Chris@0
|
314 $cids = $reference;
|
Chris@0
|
315 $ret = $backend->getMultiple($cids);
|
Chris@0
|
316 // Test return - ensure it contains existing cache ids.
|
Chris@0
|
317 $this->assert(isset($ret['test2']), "Existing cache id test2 is set.");
|
Chris@0
|
318 $this->assert(isset($ret['test3']), "Existing cache id test3 is set.");
|
Chris@0
|
319 $this->assert(isset($ret['test6']), "Existing cache id test6 is set.");
|
Chris@0
|
320 $this->assert(isset($ret['test7']), "Existing cache id test7 is set.");
|
Chris@0
|
321 // Test return - ensure that objects has expected properties.
|
Chris@0
|
322 $this->assertTrue($ret['test2']->valid, 'Item is marked as valid.');
|
Chris@0
|
323 $this->assertTrue($ret['test2']->created >= REQUEST_TIME && $ret['test2']->created <= round(microtime(TRUE), 3), 'Created time is correct.');
|
Chris@0
|
324 $this->assertEqual($ret['test2']->expire, Cache::PERMANENT, 'Expire time is correct.');
|
Chris@0
|
325 // Test return - ensure it does not contain nonexistent cache ids.
|
Chris@0
|
326 $this->assertFalse(isset($ret['test19']), "Nonexistent cache id test19 is not set.");
|
Chris@0
|
327 $this->assertFalse(isset($ret['test21']), "Nonexistent cache id test21 is not set.");
|
Chris@0
|
328 // Test values.
|
Chris@0
|
329 $this->assertIdentical($ret['test2']->data, 3, "Existing cache id test2 has the correct value.");
|
Chris@0
|
330 $this->assertIdentical($ret['test3']->data, 5, "Existing cache id test3 has the correct value.");
|
Chris@0
|
331 $this->assertIdentical($ret['test6']->data, 13, "Existing cache id test6 has the correct value.");
|
Chris@0
|
332 $this->assertIdentical($ret['test7']->data, 17, "Existing cache id test7 has the correct value.");
|
Chris@0
|
333 // Test $cids array - ensure it contains cache id's that do not exist.
|
Chris@0
|
334 $this->assert(in_array('test19', $cids), "Nonexistent cache id test19 is in cids array.");
|
Chris@0
|
335 $this->assert(in_array('test21', $cids), "Nonexistent cache id test21 is in cids array.");
|
Chris@0
|
336 // Test $cids array - ensure it does not contain cache id's that exist.
|
Chris@0
|
337 $this->assertFalse(in_array('test2', $cids), "Existing cache id test2 is not in cids array.");
|
Chris@0
|
338 $this->assertFalse(in_array('test3', $cids), "Existing cache id test3 is not in cids array.");
|
Chris@0
|
339 $this->assertFalse(in_array('test6', $cids), "Existing cache id test6 is not in cids array.");
|
Chris@0
|
340 $this->assertFalse(in_array('test7', $cids), "Existing cache id test7 is not in cids array.");
|
Chris@0
|
341
|
Chris@0
|
342 // Test a second time after deleting and setting new keys which ensures that
|
Chris@0
|
343 // if the backend uses statics it does not cause unexpected results.
|
Chris@0
|
344 $backend->delete('test3');
|
Chris@0
|
345 $backend->delete('test6');
|
Chris@0
|
346 $backend->set('test19', 57);
|
Chris@0
|
347
|
Chris@0
|
348 $cids = $reference;
|
Chris@0
|
349 $ret = $backend->getMultiple($cids);
|
Chris@0
|
350 // Test return - ensure it contains existing cache ids.
|
Chris@0
|
351 $this->assert(isset($ret['test2']), "Existing cache id test2 is set");
|
Chris@0
|
352 $this->assert(isset($ret['test7']), "Existing cache id test7 is set");
|
Chris@0
|
353 $this->assert(isset($ret['test19']), "Added cache id test19 is set");
|
Chris@0
|
354 // Test return - ensure it does not contain nonexistent cache ids.
|
Chris@0
|
355 $this->assertFalse(isset($ret['test3']), "Deleted cache id test3 is not set");
|
Chris@0
|
356 $this->assertFalse(isset($ret['test6']), "Deleted cache id test6 is not set");
|
Chris@0
|
357 $this->assertFalse(isset($ret['test21']), "Nonexistent cache id test21 is not set");
|
Chris@0
|
358 // Test values.
|
Chris@0
|
359 $this->assertIdentical($ret['test2']->data, 3, "Existing cache id test2 has the correct value.");
|
Chris@0
|
360 $this->assertIdentical($ret['test7']->data, 17, "Existing cache id test7 has the correct value.");
|
Chris@0
|
361 $this->assertIdentical($ret['test19']->data, 57, "Added cache id test19 has the correct value.");
|
Chris@0
|
362 // Test $cids array - ensure it contains cache id's that do not exist.
|
Chris@0
|
363 $this->assert(in_array('test3', $cids), "Deleted cache id test3 is in cids array.");
|
Chris@0
|
364 $this->assert(in_array('test6', $cids), "Deleted cache id test6 is in cids array.");
|
Chris@0
|
365 $this->assert(in_array('test21', $cids), "Nonexistent cache id test21 is in cids array.");
|
Chris@0
|
366 // Test $cids array - ensure it does not contain cache id's that exist.
|
Chris@0
|
367 $this->assertFalse(in_array('test2', $cids), "Existing cache id test2 is not in cids array.");
|
Chris@0
|
368 $this->assertFalse(in_array('test7', $cids), "Existing cache id test7 is not in cids array.");
|
Chris@0
|
369 $this->assertFalse(in_array('test19', $cids), "Added cache id test19 is not in cids array.");
|
Chris@0
|
370
|
Chris@0
|
371 // Test with a long $cid and non-numeric array key.
|
Chris@0
|
372 $cids = ['key:key' => $long_cid];
|
Chris@0
|
373 $return = $backend->getMultiple($cids);
|
Chris@0
|
374 $this->assertEqual(300, $return[$long_cid]->data);
|
Chris@0
|
375 $this->assertTrue(empty($cids));
|
Chris@0
|
376 }
|
Chris@0
|
377
|
Chris@0
|
378 /**
|
Chris@0
|
379 * Tests \Drupal\Core\Cache\CacheBackendInterface::setMultiple().
|
Chris@0
|
380 */
|
Chris@0
|
381 public function testSetMultiple() {
|
Chris@0
|
382 $backend = $this->getCacheBackend();
|
Chris@0
|
383
|
Chris@0
|
384 $future_expiration = REQUEST_TIME + 100;
|
Chris@0
|
385
|
Chris@0
|
386 // Set multiple testing keys.
|
Chris@0
|
387 $backend->set('cid_1', 'Some other value');
|
Chris@0
|
388 $items = [
|
Chris@0
|
389 'cid_1' => ['data' => 1],
|
Chris@0
|
390 'cid_2' => ['data' => 2],
|
Chris@0
|
391 'cid_3' => ['data' => [1, 2]],
|
Chris@0
|
392 'cid_4' => ['data' => 1, 'expire' => $future_expiration],
|
Chris@0
|
393 'cid_5' => ['data' => 1, 'tags' => ['test:a', 'test:b']],
|
Chris@0
|
394 ];
|
Chris@0
|
395 $backend->setMultiple($items);
|
Chris@0
|
396 $cids = array_keys($items);
|
Chris@0
|
397 $cached = $backend->getMultiple($cids);
|
Chris@0
|
398
|
Chris@0
|
399 $this->assertEqual($cached['cid_1']->data, $items['cid_1']['data'], 'Over-written cache item set correctly.');
|
Chris@0
|
400 $this->assertTrue($cached['cid_1']->valid, 'Item is marked as valid.');
|
Chris@0
|
401 $this->assertTrue($cached['cid_1']->created >= REQUEST_TIME && $cached['cid_1']->created <= round(microtime(TRUE), 3), 'Created time is correct.');
|
Chris@0
|
402 $this->assertEqual($cached['cid_1']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');
|
Chris@0
|
403
|
Chris@0
|
404 $this->assertEqual($cached['cid_2']->data, $items['cid_2']['data'], 'New cache item set correctly.');
|
Chris@0
|
405 $this->assertEqual($cached['cid_2']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');
|
Chris@0
|
406
|
Chris@0
|
407 $this->assertEqual($cached['cid_3']->data, $items['cid_3']['data'], 'New cache item with serialized data set correctly.');
|
Chris@0
|
408 $this->assertEqual($cached['cid_3']->expire, CacheBackendInterface::CACHE_PERMANENT, 'Cache expiration defaults to permanent.');
|
Chris@0
|
409
|
Chris@0
|
410 $this->assertEqual($cached['cid_4']->data, $items['cid_4']['data'], 'New cache item set correctly.');
|
Chris@0
|
411 $this->assertEqual($cached['cid_4']->expire, $future_expiration, 'Cache expiration has been correctly set.');
|
Chris@0
|
412
|
Chris@0
|
413 $this->assertEqual($cached['cid_5']->data, $items['cid_5']['data'], 'New cache item set correctly.');
|
Chris@0
|
414
|
Chris@0
|
415 // Calling ::setMultiple() with invalid cache tags. This should fail an
|
Chris@0
|
416 // assertion.
|
Chris@0
|
417 try {
|
Chris@0
|
418 $items = [
|
Chris@0
|
419 'exception_test_1' => ['data' => 1, 'tags' => []],
|
Chris@0
|
420 'exception_test_2' => ['data' => 2, 'tags' => ['valid']],
|
Chris@0
|
421 'exception_test_3' => ['data' => 3, 'tags' => ['node' => [3, 5, 7]]],
|
Chris@0
|
422 ];
|
Chris@0
|
423 $backend->setMultiple($items);
|
Chris@0
|
424 $this->fail('::setMultiple() was called with invalid cache tags, runtime assertion did not fail.');
|
Chris@0
|
425 }
|
Chris@0
|
426 catch (\AssertionError $e) {
|
Chris@0
|
427 $this->pass('::setMultiple() was called with invalid cache tags, runtime assertion failed.');
|
Chris@0
|
428 }
|
Chris@0
|
429 }
|
Chris@0
|
430
|
Chris@0
|
431 /**
|
Chris@0
|
432 * Test Drupal\Core\Cache\CacheBackendInterface::delete() and
|
Chris@0
|
433 * Drupal\Core\Cache\CacheBackendInterface::deleteMultiple().
|
Chris@0
|
434 */
|
Chris@0
|
435 public function testDeleteMultiple() {
|
Chris@0
|
436 $backend = $this->getCacheBackend();
|
Chris@0
|
437
|
Chris@0
|
438 // Set numerous testing keys.
|
Chris@0
|
439 $backend->set('test1', 1);
|
Chris@0
|
440 $backend->set('test2', 3);
|
Chris@0
|
441 $backend->set('test3', 5);
|
Chris@0
|
442 $backend->set('test4', 7);
|
Chris@0
|
443 $backend->set('test5', 11);
|
Chris@0
|
444 $backend->set('test6', 13);
|
Chris@0
|
445 $backend->set('test7', 17);
|
Chris@0
|
446
|
Chris@0
|
447 $backend->delete('test1');
|
Chris@0
|
448 // Nonexistent key should not cause an error.
|
Chris@0
|
449 $backend->delete('test23');
|
Chris@0
|
450 $backend->deleteMultiple([
|
Chris@0
|
451 'test3',
|
Chris@0
|
452 'test5',
|
Chris@0
|
453 'test7',
|
Chris@0
|
454 // Nonexistent key should not cause an error.
|
Chris@0
|
455 'test19',
|
Chris@0
|
456 // Nonexistent key should not cause an error.
|
Chris@0
|
457 'test21',
|
Chris@0
|
458 ]);
|
Chris@0
|
459
|
Chris@0
|
460 // Test if expected keys have been deleted.
|
Chris@0
|
461 $this->assertIdentical(FALSE, $backend->get('test1'), "Cache id test1 deleted.");
|
Chris@0
|
462 $this->assertIdentical(FALSE, $backend->get('test3'), "Cache id test3 deleted.");
|
Chris@0
|
463 $this->assertIdentical(FALSE, $backend->get('test5'), "Cache id test5 deleted.");
|
Chris@0
|
464 $this->assertIdentical(FALSE, $backend->get('test7'), "Cache id test7 deleted.");
|
Chris@0
|
465
|
Chris@0
|
466 // Test if expected keys exist.
|
Chris@0
|
467 $this->assertNotIdentical(FALSE, $backend->get('test2'), "Cache id test2 exists.");
|
Chris@0
|
468 $this->assertNotIdentical(FALSE, $backend->get('test4'), "Cache id test4 exists.");
|
Chris@0
|
469 $this->assertNotIdentical(FALSE, $backend->get('test6'), "Cache id test6 exists.");
|
Chris@0
|
470
|
Chris@0
|
471 // Test if that expected keys do not exist.
|
Chris@0
|
472 $this->assertIdentical(FALSE, $backend->get('test19'), "Cache id test19 does not exist.");
|
Chris@0
|
473 $this->assertIdentical(FALSE, $backend->get('test21'), "Cache id test21 does not exist.");
|
Chris@0
|
474
|
Chris@0
|
475 // Calling deleteMultiple() with an empty array should not cause an error.
|
Chris@0
|
476 $this->assertFalse($backend->deleteMultiple([]));
|
Chris@0
|
477 }
|
Chris@0
|
478
|
Chris@0
|
479 /**
|
Chris@0
|
480 * Test Drupal\Core\Cache\CacheBackendInterface::deleteAll().
|
Chris@0
|
481 */
|
Chris@0
|
482 public function testDeleteAll() {
|
Chris@0
|
483 $backend_a = $this->getCacheBackend();
|
Chris@0
|
484 $backend_b = $this->getCacheBackend('bootstrap');
|
Chris@0
|
485
|
Chris@0
|
486 // Set both expiring and permanent keys.
|
Chris@0
|
487 $backend_a->set('test1', 1, Cache::PERMANENT);
|
Chris@0
|
488 $backend_a->set('test2', 3, time() + 1000);
|
Chris@0
|
489 $backend_b->set('test3', 4, Cache::PERMANENT);
|
Chris@0
|
490
|
Chris@0
|
491 $backend_a->deleteAll();
|
Chris@0
|
492
|
Chris@0
|
493 $this->assertFalse($backend_a->get('test1'), 'First key has been deleted.');
|
Chris@0
|
494 $this->assertFalse($backend_a->get('test2'), 'Second key has been deleted.');
|
Chris@0
|
495 $this->assertTrue($backend_b->get('test3'), 'Item in other bin is preserved.');
|
Chris@0
|
496 }
|
Chris@0
|
497
|
Chris@0
|
498 /**
|
Chris@0
|
499 * Test Drupal\Core\Cache\CacheBackendInterface::invalidate() and
|
Chris@0
|
500 * Drupal\Core\Cache\CacheBackendInterface::invalidateMultiple().
|
Chris@0
|
501 */
|
Chris@0
|
502 public function testInvalidate() {
|
Chris@0
|
503 $backend = $this->getCacheBackend();
|
Chris@0
|
504 $backend->set('test1', 1);
|
Chris@0
|
505 $backend->set('test2', 2);
|
Chris@0
|
506 $backend->set('test3', 2);
|
Chris@0
|
507 $backend->set('test4', 2);
|
Chris@0
|
508
|
Chris@0
|
509 $reference = ['test1', 'test2', 'test3', 'test4'];
|
Chris@0
|
510
|
Chris@0
|
511 $cids = $reference;
|
Chris@0
|
512 $ret = $backend->getMultiple($cids);
|
Chris@0
|
513 $this->assertEqual(count($ret), 4, 'Four items returned.');
|
Chris@0
|
514
|
Chris@0
|
515 $backend->invalidate('test1');
|
Chris@0
|
516 $backend->invalidateMultiple(['test2', 'test3']);
|
Chris@0
|
517
|
Chris@0
|
518 $cids = $reference;
|
Chris@0
|
519 $ret = $backend->getMultiple($cids);
|
Chris@0
|
520 $this->assertEqual(count($ret), 1, 'Only one item element returned.');
|
Chris@0
|
521
|
Chris@0
|
522 $cids = $reference;
|
Chris@0
|
523 $ret = $backend->getMultiple($cids, TRUE);
|
Chris@0
|
524 $this->assertEqual(count($ret), 4, 'Four items returned.');
|
Chris@0
|
525
|
Chris@0
|
526 // Calling invalidateMultiple() with an empty array should not cause an
|
Chris@0
|
527 // error.
|
Chris@0
|
528 $this->assertFalse($backend->invalidateMultiple([]));
|
Chris@0
|
529 }
|
Chris@0
|
530
|
Chris@0
|
531 /**
|
Chris@0
|
532 * Tests Drupal\Core\Cache\CacheBackendInterface::invalidateTags().
|
Chris@0
|
533 */
|
Chris@0
|
534 public function testInvalidateTags() {
|
Chris@0
|
535 $backend = $this->getCacheBackend();
|
Chris@0
|
536
|
Chris@0
|
537 // Create two cache entries with the same tag and tag value.
|
Chris@0
|
538 $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, ['test_tag:2']);
|
Chris@0
|
539 $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, ['test_tag:2']);
|
Chris@0
|
540 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Two cache items were created.');
|
Chris@0
|
541
|
Chris@0
|
542 // Invalidate test_tag of value 1. This should invalidate both entries.
|
Chris@0
|
543 Cache::invalidateTags(['test_tag:2']);
|
Chris@0
|
544 $this->assertFalse($backend->get('test_cid_invalidate1') || $backend->get('test_cid_invalidate2'), 'Two cache items invalidated after invalidating a cache tag.');
|
Chris@0
|
545 $this->assertTrue($backend->get('test_cid_invalidate1', TRUE) && $backend->get('test_cid_invalidate2', TRUE), 'Cache items not deleted after invalidating a cache tag.');
|
Chris@0
|
546
|
Chris@0
|
547 // Create two cache entries with the same tag and an array tag value.
|
Chris@0
|
548 $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, ['test_tag:1']);
|
Chris@0
|
549 $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, ['test_tag:1']);
|
Chris@0
|
550 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Two cache items were created.');
|
Chris@0
|
551
|
Chris@0
|
552 // Invalidate test_tag of value 1. This should invalidate both entries.
|
Chris@0
|
553 Cache::invalidateTags(['test_tag:1']);
|
Chris@0
|
554 $this->assertFalse($backend->get('test_cid_invalidate1') || $backend->get('test_cid_invalidate2'), 'Two caches removed after invalidating a cache tag.');
|
Chris@0
|
555 $this->assertTrue($backend->get('test_cid_invalidate1', TRUE) && $backend->get('test_cid_invalidate2', TRUE), 'Cache items not deleted after invalidating a cache tag.');
|
Chris@0
|
556
|
Chris@0
|
557 // Create three cache entries with a mix of tags and tag values.
|
Chris@0
|
558 $backend->set('test_cid_invalidate1', $this->defaultValue, Cache::PERMANENT, ['test_tag:1']);
|
Chris@0
|
559 $backend->set('test_cid_invalidate2', $this->defaultValue, Cache::PERMANENT, ['test_tag:2']);
|
Chris@0
|
560 $backend->set('test_cid_invalidate3', $this->defaultValue, Cache::PERMANENT, ['test_tag_foo:3']);
|
Chris@0
|
561 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2') && $backend->get('test_cid_invalidate3'), 'Three cached items were created.');
|
Chris@0
|
562 Cache::invalidateTags(['test_tag_foo:3']);
|
Chris@0
|
563 $this->assertTrue($backend->get('test_cid_invalidate1') && $backend->get('test_cid_invalidate2'), 'Cache items not matching the tag were not invalidated.');
|
Chris@0
|
564 $this->assertFalse($backend->get('test_cid_invalidated3'), 'Cached item matching the tag was removed.');
|
Chris@0
|
565
|
Chris@0
|
566 // Create cache entry in multiple bins. Two cache entries
|
Chris@0
|
567 // (test_cid_invalidate1 and test_cid_invalidate2) still exist from previous
|
Chris@0
|
568 // tests.
|
Chris@0
|
569 $tags = ['test_tag:1', 'test_tag:2', 'test_tag:3'];
|
Chris@0
|
570 $bins = ['path', 'bootstrap', 'page'];
|
Chris@0
|
571 foreach ($bins as $bin) {
|
Chris@0
|
572 $this->getCacheBackend($bin)->set('test', $this->defaultValue, Cache::PERMANENT, $tags);
|
Chris@0
|
573 $this->assertTrue($this->getCacheBackend($bin)->get('test'), 'Cache item was set in bin.');
|
Chris@0
|
574 }
|
Chris@0
|
575
|
Chris@0
|
576 Cache::invalidateTags(['test_tag:2']);
|
Chris@0
|
577
|
Chris@0
|
578 // Test that the cache entry has been invalidated in multiple bins.
|
Chris@0
|
579 foreach ($bins as $bin) {
|
Chris@0
|
580 $this->assertFalse($this->getCacheBackend($bin)->get('test'), 'Tag invalidation affected item in bin.');
|
Chris@0
|
581 }
|
Chris@0
|
582 // Test that the cache entry with a matching tag has been invalidated.
|
Chris@0
|
583 $this->assertFalse($this->getCacheBackend($bin)->get('test_cid_invalidate2'), 'Cache items matching tag were invalidated.');
|
Chris@0
|
584 // Test that the cache entry with without a matching tag still exists.
|
Chris@0
|
585 $this->assertTrue($this->getCacheBackend($bin)->get('test_cid_invalidate1'), 'Cache items not matching tag were not invalidated.');
|
Chris@0
|
586 }
|
Chris@0
|
587
|
Chris@0
|
588 /**
|
Chris@0
|
589 * Test Drupal\Core\Cache\CacheBackendInterface::invalidateAll().
|
Chris@0
|
590 */
|
Chris@0
|
591 public function testInvalidateAll() {
|
Chris@0
|
592 $backend_a = $this->getCacheBackend();
|
Chris@0
|
593 $backend_b = $this->getCacheBackend('bootstrap');
|
Chris@0
|
594
|
Chris@0
|
595 // Set both expiring and permanent keys.
|
Chris@0
|
596 $backend_a->set('test1', 1, Cache::PERMANENT);
|
Chris@0
|
597 $backend_a->set('test2', 3, time() + 1000);
|
Chris@0
|
598 $backend_b->set('test3', 4, Cache::PERMANENT);
|
Chris@0
|
599
|
Chris@0
|
600 $backend_a->invalidateAll();
|
Chris@0
|
601
|
Chris@0
|
602 $this->assertFalse($backend_a->get('test1'), 'First key has been invalidated.');
|
Chris@0
|
603 $this->assertFalse($backend_a->get('test2'), 'Second key has been invalidated.');
|
Chris@0
|
604 $this->assertTrue($backend_b->get('test3'), 'Item in other bin is preserved.');
|
Chris@0
|
605 $this->assertTrue($backend_a->get('test1', TRUE), 'First key has not been deleted.');
|
Chris@0
|
606 $this->assertTrue($backend_a->get('test2', TRUE), 'Second key has not been deleted.');
|
Chris@0
|
607 }
|
Chris@0
|
608
|
Chris@0
|
609 /**
|
Chris@0
|
610 * Tests Drupal\Core\Cache\CacheBackendInterface::removeBin().
|
Chris@0
|
611 */
|
Chris@0
|
612 public function testRemoveBin() {
|
Chris@0
|
613 $backend_a = $this->getCacheBackend();
|
Chris@0
|
614 $backend_b = $this->getCacheBackend('bootstrap');
|
Chris@0
|
615
|
Chris@0
|
616 // Set both expiring and permanent keys.
|
Chris@0
|
617 $backend_a->set('test1', 1, Cache::PERMANENT);
|
Chris@0
|
618 $backend_a->set('test2', 3, time() + 1000);
|
Chris@0
|
619 $backend_b->set('test3', 4, Cache::PERMANENT);
|
Chris@0
|
620
|
Chris@0
|
621 $backend_a->removeBin();
|
Chris@0
|
622
|
Chris@0
|
623 $this->assertFalse($backend_a->get('test1'), 'First key has been deleted.');
|
Chris@0
|
624 $this->assertFalse($backend_a->get('test2', TRUE), 'Second key has been deleted.');
|
Chris@0
|
625 $this->assertTrue($backend_b->get('test3'), 'Item in other bin is preserved.');
|
Chris@0
|
626 }
|
Chris@0
|
627
|
Chris@0
|
628 }
|