Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Component / FileCache / FileCacheTest.php
1 <?php
2
3 namespace Drupal\Tests\Component\FileCache;
4
5 use Drupal\Component\FileCache\FileCache;
6 use PHPUnit\Framework\TestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\Component\FileCache\FileCache
10  * @group FileCache
11  */
12 class FileCacheTest extends TestCase {
13
14   /**
15    * FileCache object used for the tests.
16    *
17    * @var \Drupal\Component\FileCache\FileCacheInterface
18    */
19   protected $fileCache;
20
21   /**
22    * Static FileCache object used for verification of tests.
23    *
24    * @var \Drupal\Component\FileCache\FileCacheBackendInterface
25    */
26   protected $staticFileCache;
27
28   /**
29    * {@inheritdoc}
30    */
31   protected function setUp() {
32     parent::setUp();
33
34     $this->fileCache = new FileCache('prefix', 'test', '\Drupal\Tests\Component\FileCache\StaticFileCacheBackend', ['bin' => 'llama']);
35     $this->staticFileCache = new StaticFileCacheBackend(['bin' => 'llama']);
36   }
37
38   /**
39    * @covers ::get
40    * @covers ::__construct
41    */
42   public function testGet() {
43     // Test a cache miss.
44     $result = $this->fileCache->get(__DIR__ . '/Fixtures/no-llama-42.yml');
45     $this->assertNull($result);
46
47     // Test a cache hit.
48     $filename = __DIR__ . '/Fixtures/llama-42.txt';
49     $realpath = realpath($filename);
50     $cid = 'prefix:test:' . $realpath;
51     $data = [
52       'mtime' => filemtime($realpath),
53       'filepath' => $realpath,
54       'data' => 42,
55     ];
56
57     $this->staticFileCache->store($cid, $data);
58
59     $result = $this->fileCache->get($filename);
60     $this->assertEquals(42, $result);
61
62     // Cleanup static caches.
63     $this->fileCache->delete($filename);
64   }
65
66   /**
67    * @covers ::getMultiple
68    */
69   public function testGetMultiple() {
70     // Test a cache miss.
71     $result = $this->fileCache->getMultiple([__DIR__ . '/Fixtures/no-llama-42.yml']);
72     $this->assertEmpty($result);
73
74     // Test a cache hit.
75     $filename = __DIR__ . '/Fixtures/llama-42.txt';
76     $realpath = realpath($filename);
77     $cid = 'prefix:test:' . $realpath;
78     $data = [
79       'mtime' => filemtime($realpath),
80       'filepath' => $realpath,
81       'data' => 42,
82     ];
83
84     $this->staticFileCache->store($cid, $data);
85
86     $result = $this->fileCache->getMultiple([$filename]);
87     $this->assertEquals([$filename => 42], $result);
88
89     // Test a static cache hit.
90     $file2 = __DIR__ . '/Fixtures/llama-23.txt';
91     $this->fileCache->set($file2, 23);
92
93     $result = $this->fileCache->getMultiple([$filename, $file2]);
94     $this->assertEquals([$filename => 42, $file2 => 23], $result);
95
96     // Cleanup static caches.
97     $this->fileCache->delete($filename);
98     $this->fileCache->delete($file2);
99   }
100
101   /**
102    * @covers ::set
103    */
104   public function testSet() {
105     $filename = __DIR__ . '/Fixtures/llama-23.txt';
106     $realpath = realpath($filename);
107     $cid = 'prefix:test:' . $realpath;
108     $data = [
109       'mtime' => filemtime($realpath),
110       'filepath' => $realpath,
111       'data' => 23,
112     ];
113
114     $this->fileCache->set($filename, 23);
115     $result = $this->staticFileCache->fetch([$cid]);
116     $this->assertEquals([$cid => $data], $result);
117
118     // Cleanup static caches.
119     $this->fileCache->delete($filename);
120   }
121
122   /**
123    * @covers ::delete
124    */
125   public function testDelete() {
126     $filename = __DIR__ . '/Fixtures/llama-23.txt';
127     $realpath = realpath($filename);
128     $cid = 'prefix:test:' . $realpath;
129
130     $this->fileCache->set($filename, 23);
131
132     // Ensure data is removed after deletion.
133     $this->fileCache->delete($filename);
134
135     $result = $this->staticFileCache->fetch([$cid]);
136     $this->assertEquals([], $result);
137
138     $result = $this->fileCache->get($filename);
139     $this->assertNull($result);
140   }
141
142 }