[BUGFIX] Fallback to empty array if ExportController receives no input
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Cache / Backend / PdoBackendTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Cache\Backend;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Cache\Backend\PdoBackend;
19 use TYPO3\CMS\Core\Cache\Exception;
20 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23
24 /**
25 * Testcase for the PDO cache backend
26 *
27 * @requires extension pdo_sqlite
28 */
29 class PdoBackendTest extends UnitTestCase
30 {
31 /**
32 * @var bool Reset singletons created by subject
33 */
34 protected $resetSingletonInstances = true;
35
36 /**
37 * @test
38 */
39 public function setThrowsExceptionIfNoFrontEndHasBeenSet()
40 {
41 $this->expectException(Exception::class);
42 $this->expectExceptionCode(1259515600);
43
44 $backend = new PdoBackend('Testing');
45 $data = 'Some data';
46 $identifier = 'MyIdentifier';
47 $backend->set($identifier, $data);
48 }
49
50 /**
51 * @test
52 */
53 public function itIsPossibleToSetAndCheckExistenceInCache()
54 {
55 $backend = $this->setUpBackend();
56 $data = 'Some data';
57 $identifier = 'MyIdentifier';
58 $backend->set($identifier, $data);
59 $this->assertTrue($backend->has($identifier));
60 }
61
62 /**
63 * @test
64 */
65 public function itIsPossibleToSetAndGetEntry()
66 {
67 $backend = $this->setUpBackend();
68 $data = 'Some data';
69 $identifier = 'MyIdentifier';
70 $backend->set($identifier, $data);
71 $fetchedData = $backend->get($identifier);
72 $this->assertEquals($data, $fetchedData);
73 }
74
75 /**
76 * @test
77 */
78 public function itIsPossibleToRemoveEntryFromCache()
79 {
80 $backend = $this->setUpBackend();
81 $data = 'Some data';
82 $identifier = 'MyIdentifier';
83 $backend->set($identifier, $data);
84 $backend->remove($identifier);
85 $this->assertFalse($backend->has($identifier));
86 }
87
88 /**
89 * @test
90 */
91 public function itIsPossibleToOverwriteAnEntryInTheCache()
92 {
93 $backend = $this->setUpBackend();
94 $data = 'Some data';
95 $identifier = 'MyIdentifier';
96 $backend->set($identifier, $data);
97 $otherData = 'some other data';
98 $backend->set($identifier, $otherData);
99 $fetchedData = $backend->get($identifier);
100 $this->assertEquals($otherData, $fetchedData);
101 }
102
103 /**
104 * @test
105 */
106 public function findIdentifiersByTagFindsSetEntries()
107 {
108 $backend = $this->setUpBackend();
109 $data = 'Some data';
110 $entryIdentifier = 'MyIdentifier';
111 $backend->set($entryIdentifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
112 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag1');
113 $this->assertEquals($entryIdentifier, $retrieved[0]);
114 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
115 $this->assertEquals($entryIdentifier, $retrieved[0]);
116 }
117
118 /**
119 * @test
120 */
121 public function setRemovesTagsFromPreviousSet()
122 {
123 $backend = $this->setUpBackend();
124 $data = 'Some data';
125 $entryIdentifier = 'MyIdentifier';
126 $backend->set($entryIdentifier, $data, ['UnitTestTag%tag1', 'UnitTestTag%tag2']);
127 $backend->set($entryIdentifier, $data, ['UnitTestTag%tag3']);
128 $retrieved = $backend->findIdentifiersByTag('UnitTestTag%tag2');
129 $this->assertEquals([], $retrieved);
130 }
131
132 /**
133 * @test
134 */
135 public function setOverwritesExistingEntryThatExceededItsLifetimeWithNewData()
136 {
137 $backend = $this->setUpBackend();
138 $data1 = 'data1';
139 $entryIdentifier = $this->getUniqueId('test');
140 $backend->set($entryIdentifier, $data1, [], 1);
141 $data2 = 'data2';
142 $GLOBALS['EXEC_TIME'] += 2;
143 $backend->set($entryIdentifier, $data2, [], 10);
144 $this->assertEquals($data2, $backend->get($entryIdentifier));
145 }
146
147 /**
148 * @test
149 */
150 public function hasReturnsFalseIfTheEntryDoesntExist()
151 {
152 $backend = $this->setUpBackend();
153 $identifier = 'NonExistingIdentifier';
154 $this->assertFalse($backend->has($identifier));
155 }
156
157 /**
158 * @test
159 */
160 public function removeReturnsFalseIfTheEntryDoesntExist()
161 {
162 $backend = $this->setUpBackend();
163 $identifier = 'NonExistingIdentifier';
164 $this->assertFalse($backend->remove($identifier));
165 }
166
167 /**
168 * @test
169 */
170 public function flushByTagRemovesCacheEntriesWithSpecifiedTag()
171 {
172 $backend = $this->setUpBackend();
173 $data = 'some data' . microtime();
174 $backend->set('PdoBackendTest1', $data, ['UnitTestTag%test', 'UnitTestTag%boring']);
175 $backend->set('PdoBackendTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
176 $backend->set('PdoBackendTest3', $data, ['UnitTestTag%test']);
177 $backend->flushByTag('UnitTestTag%special');
178 $this->assertTrue($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
179 $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
180 $this->assertTrue($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
181 }
182
183 /**
184 * @test
185 */
186 public function flushByTagsRemovesCacheEntriesWithSpecifiedTags()
187 {
188 $backend = $this->setUpBackend();
189 $data = 'some data' . microtime();
190 $backend->set('PdoBackendTest1', $data, ['UnitTestTag%test', 'UnitTestTags%boring']);
191 $backend->set('PdoBackendTest2', $data, ['UnitTestTag%test', 'UnitTestTag%special']);
192 $backend->set('PdoBackendTest3', $data, ['UnitTestTag%test']);
193 $backend->flushByTags(['UnitTestTag%special', 'UnitTestTags%boring']);
194 $this->assertFalse($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
195 $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
196 $this->assertTrue($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
197 }
198
199 /**
200 * @test
201 */
202 public function flushRemovesAllCacheEntries()
203 {
204 $backend = $this->setUpBackend();
205 $data = 'some data' . microtime();
206 $backend->set('PdoBackendTest1', $data);
207 $backend->set('PdoBackendTest2', $data);
208 $backend->set('PdoBackendTest3', $data);
209 $backend->flush();
210 $this->assertFalse($backend->has('PdoBackendTest1'), 'PdoBackendTest1');
211 $this->assertFalse($backend->has('PdoBackendTest2'), 'PdoBackendTest2');
212 $this->assertFalse($backend->has('PdoBackendTest3'), 'PdoBackendTest3');
213 }
214
215 /**
216 * @test
217 */
218 public function flushRemovesOnlyOwnEntries()
219 {
220 $thisCache = $this->createMock(FrontendInterface::class);
221 $thisCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thisCache'));
222 $thisBackend = $this->setUpBackend();
223 $thisBackend->setCache($thisCache);
224 $thatCache = $this->createMock(FrontendInterface::class);
225 $thatCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('thatCache'));
226 $thatBackend = $this->setUpBackend();
227 $thatBackend->setCache($thatCache);
228 $thisBackend->set('thisEntry', 'Hello');
229 $thatBackend->set('thatEntry', 'World!');
230 $thatBackend->flush();
231 $this->assertEquals('Hello', $thisBackend->get('thisEntry'));
232 $this->assertFalse($thatBackend->has('thatEntry'));
233 }
234
235 /**
236 * @test
237 */
238 public function collectGarbageReallyRemovesAnExpiredCacheEntry()
239 {
240 $backend = $this->setUpBackend();
241 $data = 'some data' . microtime();
242 $entryIdentifier = 'BackendPDORemovalTest';
243 $backend->set($entryIdentifier, $data, [], 1);
244 $this->assertTrue($backend->has($entryIdentifier));
245 $GLOBALS['EXEC_TIME'] += 2;
246 $backend->collectGarbage();
247 $this->assertFalse($backend->has($entryIdentifier));
248 }
249
250 /**
251 * @test
252 */
253 public function collectGarbageReallyRemovesAllExpiredCacheEntries()
254 {
255 $backend = $this->setUpBackend();
256 $data = 'some data' . microtime();
257 $entryIdentifier = 'BackendPDORemovalTest';
258 $backend->set($entryIdentifier . 'A', $data, [], null);
259 $backend->set($entryIdentifier . 'B', $data, [], 10);
260 $backend->set($entryIdentifier . 'C', $data, [], 1);
261 $backend->set($entryIdentifier . 'D', $data, [], 1);
262 $this->assertTrue($backend->has($entryIdentifier . 'A'));
263 $this->assertTrue($backend->has($entryIdentifier . 'B'));
264 $this->assertTrue($backend->has($entryIdentifier . 'C'));
265 $this->assertTrue($backend->has($entryIdentifier . 'D'));
266 $GLOBALS['EXEC_TIME'] += 2;
267 $backend->collectGarbage();
268 $this->assertTrue($backend->has($entryIdentifier . 'A'));
269 $this->assertTrue($backend->has($entryIdentifier . 'B'));
270 $this->assertFalse($backend->has($entryIdentifier . 'C'));
271 $this->assertFalse($backend->has($entryIdentifier . 'D'));
272 }
273
274 /**
275 * Sets up the PDO backend used for testing
276 *
277 * @return \TYPO3\CMS\Core\Cache\Backend\PdoBackend
278 */
279 protected function setUpBackend()
280 {
281 $mockCache = $this->createMock(FrontendInterface::class);
282 $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('TestCache'));
283 $backend = GeneralUtility::makeInstance(PdoBackend::class, 'Testing');
284 $backend->setCache($mockCache);
285 $backend->setDataSourceName('sqlite::memory:');
286 $backend->initializeObject();
287 return $backend;
288 }
289 }