Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Cache / ChainedFastBackendTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Cache;
4
5 use Drupal\Core\Cache\ChainedFastBackend;
6 use Drupal\Core\Cache\MemoryBackend;
7 use Drupal\Tests\UnitTestCase;
8
9 /**
10  * @coversDefaultClass \Drupal\Core\Cache\ChainedFastBackend
11  * @group Cache
12  */
13 class ChainedFastBackendTest extends UnitTestCase {
14
15   /**
16    * The consistent cache backend.
17    *
18    * @var \Drupal\Core\Cache\CacheBackendInterface
19    */
20   protected $consistentCache;
21
22   /**
23    * The fast cache backend.
24    *
25    * @var \Drupal\Core\Cache\CacheBackendInterface
26    */
27   protected $fastCache;
28
29   /**
30    * The cache bin.
31    *
32    * @var string
33    */
34   protected $bin;
35
36   /**
37    * Tests a get() on the fast backend, with no hit on the consistent backend.
38    */
39   public function testGetDoesntHitConsistentBackend() {
40     $consistent_cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
41     $timestamp_cid = ChainedFastBackend::LAST_WRITE_TIMESTAMP_PREFIX . 'cache_foo';
42     // Use the request time because that is what we will be comparing against.
43     $timestamp_item = (object) ['cid' => $timestamp_cid, 'data' => (int) $_SERVER['REQUEST_TIME'] - 60];
44     $consistent_cache->expects($this->once())
45       ->method('get')->with($timestamp_cid)
46       ->will($this->returnValue($timestamp_item));
47     $consistent_cache->expects($this->never())
48       ->method('getMultiple');
49
50     $fast_cache = new MemoryBackend();
51     $fast_cache->set('foo', 'baz');
52
53     $chained_fast_backend = new ChainedFastBackend(
54       $consistent_cache,
55       $fast_cache,
56       'foo'
57     );
58     $this->assertEquals('baz', $chained_fast_backend->get('foo')->data);
59   }
60
61   /**
62    * Tests a fast cache miss gets data from the consistent cache backend.
63    */
64   public function testFallThroughToConsistentCache() {
65     $timestamp_item = (object) [
66       'cid' => ChainedFastBackend::LAST_WRITE_TIMESTAMP_PREFIX . 'cache_foo',
67       // Time travel is easy.
68       'data' => time() + 60,
69     ];
70     $cache_item = (object) [
71       'cid' => 'foo',
72       'data' => 'baz',
73       'created' => time(),
74       'expire' => time() + 3600,
75       'tags' => ['tag'],
76     ];
77
78     $consistent_cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
79     $fast_cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
80
81     // We should get a call for the timestamp on the consistent backend.
82     $consistent_cache->expects($this->once())
83       ->method('get')
84       ->with($timestamp_item->cid)
85       ->will($this->returnValue($timestamp_item));
86
87     // We should get a call for the cache item on the consistent backend.
88     $consistent_cache->expects($this->once())
89       ->method('getMultiple')
90       ->with([$cache_item->cid])
91       ->will($this->returnValue([$cache_item->cid => $cache_item]));
92
93     // We should get a call for the cache item on the fast backend.
94     $fast_cache->expects($this->once())
95       ->method('getMultiple')
96       ->with([$cache_item->cid])
97       ->will($this->returnValue([$cache_item->cid => $cache_item]));
98
99     // We should get a call to set the cache item on the fast backend.
100     $fast_cache->expects($this->once())
101       ->method('set')
102       ->with($cache_item->cid, $cache_item->data, $cache_item->expire);
103
104     $chained_fast_backend = new ChainedFastBackend(
105       $consistent_cache,
106       $fast_cache,
107       'foo'
108     );
109     $this->assertEquals('baz', $chained_fast_backend->get('foo')->data);
110   }
111
112 }