Updated the caching framework to the according FLOW3 revision 2616
[Packages/TYPO3.CMS.git] / tests / t3lib / cache / backend / t3lib_cache_backend_filebackendtestcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 // TODO implement autoloading so that we only require stuff we really need
27 require_once(PATH_t3lib . 'class.t3lib_cache.php');
28
29 require_once(PATH_t3lib . 'cache/backend/interfaces/interface.t3lib_cache_backend_backend.php');
30 require_once(PATH_t3lib . 'cache/frontend/interfaces/interface.t3lib_cache_frontend_frontend.php');
31
32 require_once(PATH_t3lib . 'cache/backend/class.t3lib_cache_backend_abstractbackend.php');
33 require_once(PATH_t3lib . 'cache/frontend/class.t3lib_cache_frontend_abstractfrontend.php');
34 require_once(PATH_t3lib . 'cache/class.t3lib_cache_exception.php');
35 require_once(PATH_t3lib . 'cache/class.t3lib_cache_factory.php');
36 require_once(PATH_t3lib . 'cache/class.t3lib_cache_manager.php');
37 require_once(PATH_t3lib . 'cache/frontend/class.t3lib_cache_frontend_variablefrontend.php');
38
39 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_classalreadyloaded.php');
40 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_duplicateidentifier.php');
41 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_invalidbackend.php');
42 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_invalidcache.php');
43 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_invaliddata.php');
44 require_once(PATH_t3lib . 'cache/exception/class.t3lib_cache_exception_nosuchcache.php');
45
46 require_once(PATH_t3lib . 'cache/backend/class.t3lib_cache_backend_filebackend.php');
47
48 /**
49 * Testcase for the File cache backend
50 *
51 * This file is a backport from FLOW3
52 *
53 * @author Ingo Renner <ingo@typo3.org>
54 * @package TYPO3
55 * @subpackage tests
56 * @version $Id$
57 */
58 class t3lib_cache_backend_FileBackendTestCase extends tx_phpunit_testcase {
59
60 /**
61 * If set, the tearDown() method will clean up the cache subdirectory used by this unit test.
62 *
63 * @var t3lib_cache_backend_FileBackend
64 */
65 protected $backend;
66
67 protected $testingCacheDirectory;
68
69 /**
70 * Sets up this testcase
71 *
72 * @return void
73 */
74 public function setUp() {
75 $this->testingCacheDirectory = 'typo3temp/cache/testing/';
76
77 $this->backend = t3lib_div::makeInstance(
78 't3lib_cache_backend_FileBackend',
79 array('cacheDirectory' => $this->testingCacheDirectory)
80 );
81 }
82
83 /**
84 * @test
85 * @author Robert Lemke <robert@typo3.org>
86 * @author Ingo Renner <ingo@typo3.org>
87 */
88 public function defaultCacheDirectoryIsWritable() {
89 $cacheDirectory = $this->backend->getCacheDirectory();
90
91 $this->assertTrue(is_writable($cacheDirectory), 'The default cache directory "' . $cacheDirectory . '" is not writable.');
92 }
93
94 /**
95 * @test
96 * @author Robert Lemke <robert@typo3.org>
97 * @expectedException t3lib_cache_Exception
98 */
99 public function setCacheDirectoryThrowsExceptionOnNonWritableDirectory() {
100 if (DIRECTORY_SEPARATOR == '\\') {
101 $this->markTestSkipped('test not reliable in Windows environment');
102 }
103 $directoryName = '/sbin';
104
105 $this->backend->setCacheDirectory($directoryName);
106 }
107
108 /**
109 * @test
110 * @author Robert Lemke <robert@typo3.org>
111 * @author Ingo Renner <ingo@typo3.org>
112 */
113 public function getCacheDirectoryReturnsThePreviouslySetDirectory() {
114 $directory = $this->testingCacheDirectory;
115 $fullPathToDirectory = t3lib_div::getIndpEnv('TYPO3_DOCUMENT_ROOT') . '/' . $directory;
116
117 $this->backend->setCacheDirectory($directory);
118 $this->assertEquals($fullPathToDirectory, $this->backend->getCacheDirectory(), 'getDirectory() did not return the expected value.');
119 }
120
121 /**
122 * @test
123 * @author Robert Lemke <robert@typo3.org>
124 * @author Ingo Renner <ingo@typo3.org>
125 * @expectedException t3lib_cache_exception_InvalidData
126 */
127 public function setThrowsExceptionIfDataIsNotAString() {
128 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
129 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
130 array(),
131 '',
132 FALSE
133 );
134
135 $data = array('Some data');
136 $entryIdentifier = 'BackendFileTest';
137
138 $this->backend->setCache($cache);
139
140 $this->backend->set($entryIdentifier, $data);
141 }
142
143 /**
144 * @test
145 * @author Robert Lemke <robert@typo3.org>
146 * @author Ingo Renner <ingo@typo3.org>
147 */
148 public function setReallySavesToTheSpecifiedDirectory() {
149 $cacheIdentifier = 'UnitTestCache';
150 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
151 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
152 array(),
153 '',
154 FALSE
155 );
156
157 $cache->expects($this->atLeastOnce())
158 ->method('getIdentifier')
159 ->will($this->returnValue($cacheIdentifier));
160
161 $data = 'some data' . microtime();
162 $entryIdentifier = 'BackendFileTest';
163 $entryIdentifierHash = sha1($entryIdentifier);
164
165 $this->backend->setCache($cache);
166 $this->backend->set($entryIdentifier, $data);
167
168 $cacheDirectory = $this->backend->getCacheDirectory();
169 $pathAndFilename = $cacheDirectory
170 . 'data/'
171 . $cacheIdentifier . '/'
172 . $entryIdentifierHash[0] . '/'
173 . $entryIdentifierHash[1] . '/'
174 . $entryIdentifier;
175 $this->assertTrue(file_exists($pathAndFilename), 'File does not exist.');
176 $retrievedData = file_get_contents($pathAndFilename, NULL, NULL, t3lib_cache_backend_FileBackend::EXPIRYTIME_LENGTH);
177 $this->assertEquals(
178 $data,
179 $retrievedData,
180 'The original and the retrieved data don\'t match.'
181 );
182 }
183
184 /**
185 * @test
186 * @author Robert Lemke <robert@typo3.org>
187 * @author Ingo Renner <ingo@typo3.org>
188 */
189 public function setOverwritesAnAlreadyExistingCacheEntryForTheSameIdentifier() {
190 $cacheIdentifier = 'UnitTestCache';
191 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
192 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
193 array(),
194 '',
195 FALSE
196 );
197 $cache->expects($this->atLeastOnce())
198 ->method('getIdentifier')
199 ->will($this->returnValue($cacheIdentifier));
200
201 $data1 = 'some data' . microtime();
202 $data2 = 'some data' . microtime();
203 $entryIdentifier = 'BackendFileRemoveBeforeSetTest';
204 $entryIdentifierHash = sha1($entryIdentifier);
205
206 $this->backend->setCache($cache);
207 $this->backend->set($entryIdentifier, $data1, array(), 500);
208 // setting a second entry with the same identifier, but different
209 // data, this should _replace_ the existing one we set before
210 $this->backend->set($entryIdentifier, $data2, array(), 200);
211
212 $cacheDirectory = $this->backend->getCacheDirectory();
213 $pathAndFilename = $cacheDirectory
214 . 'data/'
215 . $cacheIdentifier . '/'
216 . $entryIdentifierHash[0] . '/'
217 . $entryIdentifierHash[1] . '/'
218 . $entryIdentifier;
219 $this->assertTrue(file_exists($pathAndFilename), 'File does not exist.');
220 $retrievedData = file_get_contents($pathAndFilename, NULL, NULL, t3lib_cache_backend_FileBackend::EXPIRYTIME_LENGTH);
221 $this->assertEquals($data2, $retrievedData);
222 }
223
224 /**
225 * @test
226 * @author Robert Lemke <robert@typo3.org>
227 * @author Ingo Renner <ingo@typo3.org>
228 */
229 public function setReallySavesSpecifiedTags() {
230 $cacheIdentifier = 'UnitTestCache';
231 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
232 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
233 array(),
234 '',
235 FALSE
236 );
237 $cache->expects($this->atLeastOnce())
238 ->method('getIdentifier')
239 ->will($this->returnValue($cacheIdentifier));
240
241 $data = 'some data' . microtime();
242 $entryIdentifier = 'BackendFileTest';
243 $entryIdentifierHash = sha1($entryIdentifier);
244
245 $this->backend->setCache($cache);
246 $tagsDirectory = $this->backend->getCacheDirectory() . 'tags/';
247
248 $this->backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
249
250 $this->assertTrue(is_dir($tagsDirectory . 'UnitTestTag%tag1'), 'Tag directory UnitTestTag%tag1 does not exist.');
251 $this->assertTrue(is_dir($tagsDirectory . 'UnitTestTag%tag2'), 'Tag directory UnitTestTag%tag2 does not exist.');
252
253 $filename = $tagsDirectory . 'UnitTestTag%tag1/' . $cacheIdentifier . t3lib_cache_backend_FileBackend::SEPARATOR . $entryIdentifier;
254 $this->assertTrue(file_exists($filename), 'File "' . $filename . '" does not exist.');
255
256 $filename = $tagsDirectory . 'UnitTestTag%tag2/' . $cacheIdentifier . t3lib_cache_backend_FileBackend::SEPARATOR . $entryIdentifier;
257 $this->assertTrue(file_exists($filename), 'File "' . $filename . '" does not exist.');
258 }
259
260 /**
261 * @test
262 * @author Robert Lemke <robert@typo3.org>
263 * @author Ingo Renner <ingo@typo3.org>
264 */
265 public function getReturnsContentOfTheCorrectCacheFile() {
266 $cacheIdentifier = 'UnitTestCache';
267 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
268 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
269 array(),
270 '',
271 FALSE
272 );
273 $cache->expects($this->atLeastOnce())
274 ->method('getIdentifier')
275 ->will($this->returnValue($cacheIdentifier));
276
277 $this->backend->setCache($cache);
278
279 $entryIdentifier = 'BackendFileTest';
280
281 $data = 'some data' . microtime();
282 $this->backend->set($entryIdentifier, $data, array(), 500);
283
284 $data = 'some other data' . microtime();
285 $this->backend->set($entryIdentifier, $data, array(), 100);
286
287 $loadedData = $this->backend->get($entryIdentifier);
288
289 $this->assertEquals($data, $loadedData, 'The original and the retrieved data don\'t match.');
290 }
291
292 /**
293 * @test
294 * @author Robert Lemke <robert@typo3.org>
295 * @author Ingo Renner <ingo@typo3.org>
296 */
297 public function hasReturnsTheCorrectResult() {
298 $cacheIdentifier = 'UnitTestCache';
299 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
300 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
301 array(),
302 '',
303 FALSE
304 );
305 $cache->expects($this->atLeastOnce())
306 ->method('getIdentifier')
307 ->will($this->returnValue($cacheIdentifier));
308
309 $this->backend->setCache($cache);
310
311 $entryIdentifier = 'BackendFileTest';
312
313 $data = 'some data' . microtime();
314 $this->backend->set($entryIdentifier, $data);
315
316 $this->assertTrue($this->backend->has($entryIdentifier), 'has() did not return TRUE.');
317 $this->assertFalse($this->backend->has($entryIdentifier . 'Not'), 'has() did not return FALSE.');
318 }
319
320 /**
321 * @test
322 * @author Robert Lemke <robert@typo3.org>
323 * @author Ingo Renner <ingo@typo3.org>
324 */
325 public function removeReallyRemovesACacheEntry() {
326 $cacheIdentifier = 'UnitTestCache';
327 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
328 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
329 array(),
330 '',
331 FALSE
332 );
333 $cache->expects($this->atLeastOnce())
334 ->method('getIdentifier')
335 ->will($this->returnValue($cacheIdentifier));
336
337 $data = 'some data' . microtime();
338 $entryIdentifier = 'BackendFileRemovalTest';
339 $entryIdentifierHash = sha1($entryIdentifier);
340
341 $cacheDirectory = $this->backend->getCacheDirectory();
342 $this->backend->setCache($cache);
343
344 $pathAndFilename = $cacheDirectory
345 . 'data/'
346 . $cacheIdentifier . '/'
347 . $entryIdentifierHash[0] . '/'
348 . $entryIdentifierHash[1] . '/'
349 . $entryIdentifier;
350
351 $this->backend->set($entryIdentifier, $data);
352 $this->assertTrue(file_exists($pathAndFilename), 'The cache entry does not exist.');
353
354 $this->backend->remove($entryIdentifier);
355 $this->assertFalse(file_exists($pathAndFilename), 'The cache entry still exists.');
356 }
357
358 /**
359 * @test
360 * @author Robert Lemke <robert@typo3.org>
361 * @author Karsten Dambekalns <karsten@typo3.org>
362 * @author Ingo Renner <ingo@typo3.org>
363 */
364 public function collectGarbageReallyRemovesAnExpiredCacheEntry() {
365 $cacheIdentifier = 'UnitTestCache';
366 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
367 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
368 array(),
369 '',
370 FALSE
371 );
372 $cache->expects($this->atLeastOnce())
373 ->method('getIdentifier')
374 ->will($this->returnValue($cacheIdentifier));
375
376 $data = 'some data' . microtime();
377 $entryIdentifier = 'BackendFileRemovalTest';
378 $entryIdentifierHash = sha1($entryIdentifier);
379
380 $cacheDirectory = $this->backend->getCacheDirectory();
381 $this->backend->setCache($cache);
382
383 $pathAndFilename = $cacheDirectory
384 . 'data/'
385 . $cacheIdentifier . '/'
386 . $entryIdentifierHash[0] . '/'
387 . $entryIdentifierHash[1] . '/'
388 . $entryIdentifier;
389
390 $this->backend->set($entryIdentifier, $data, array(), 1);
391 $this->assertTrue(file_exists($pathAndFilename), 'The cache entry does not exist.');
392
393 sleep(2);
394
395 $this->backend->collectGarbage();
396 $this->assertFalse(file_exists($pathAndFilename), 'The cache entry still exists.');
397 }
398
399 /**
400 * @test
401 * @author Robert Lemke <robert@typo3.org>
402 * @author Karsten Dambekalns <karsten@typo3.org>
403 * @author Ingo Renner <ingo@typo3.org>
404 */
405 public function collectGarbageReallyRemovesAllExpiredCacheEntries() {
406 $cacheIdentifier = 'UnitTestCache';
407 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
408 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
409 array(),
410 '',
411 FALSE
412 );
413 $cache->expects($this->atLeastOnce())
414 ->method('getIdentifier')
415 ->will($this->returnValue($cacheIdentifier));
416
417 $data = 'some data' . microtime();
418 $entryIdentifier = 'BackendFileRemovalTest';
419
420 $cacheDirectory = $this->backend->getCacheDirectory();
421 $this->backend->setCache($cache);
422
423 // $pattern = $cacheDirectory
424 // . 'data/'
425 // . $cacheIdentifier . '/*/*/'
426 // . t3lib_cache_backend_FileBackend::FILENAME_EXPIRYTIME_GLOB
427 // . t3lib_cache_backend_FileBackend::SEPARATOR
428 // . $entryIdentifier
429 // . '?';
430 $pattern = $cacheDirectory
431 . 'data/'
432 . $cacheIdentifier . '/*/*/'
433 . $entryIdentifier
434 . '?';
435
436 $this->backend->set($entryIdentifier . 'A', $data, array(), 1);
437 $this->backend->set($entryIdentifier . 'B', $data, array(), 1);
438 $this->backend->set($entryIdentifier . 'C', $data, array(), 1);
439 $filesFound = glob($pattern);
440 $this->assertTrue(is_array($filesFound) && count($filesFound) > 0, 'The cache entries do not exist.');
441
442 sleep(2);
443
444 $this->backend->collectGarbage();
445 $filesFound = glob($pattern);
446 $this->assertTrue(count($filesFound) == 0, 'The cache entries still exist.');
447 }
448
449 /**
450 * @test
451 * @author Robert Lemke <robert@typo3.org>
452 * @author Ingo Renner <ingo@typo3.org>
453 */
454 public function removeReallyRemovesTagsOfRemovedEntry() {
455 $cacheIdentifier = 'UnitTestCache';
456 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
457 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
458 array(),
459 '',
460 FALSE
461 );
462 $cache->expects($this->atLeastOnce())
463 ->method('getIdentifier')
464 ->will($this->returnValue($cacheIdentifier));
465
466 $data = 'some data' . microtime();
467 $entryIdentifier = 'BackendFileTest';
468
469 $this->backend->setCache($cache);
470
471 $tagsDirectory = $this->backend->getCacheDirectory() . 'tags/';
472
473 $this->backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
474 $this->backend->remove($entryIdentifier);
475
476 $this->assertTrue(!file_exists($tagsDirectory . 'UnitTestTag%tag1/' . $entryIdentifier), 'File "' . $tagsDirectory . 'UnitTestTag%tag1/' . $entryIdentifier . '" still exists.');
477 $this->assertTrue(!file_exists($tagsDirectory . 'UnitTestTag%tag2/' . $entryIdentifier), 'File "' . $tagsDirectory . 'UnitTestTag%tag2/' . $entryIdentifier . '" still exists.');
478 }
479
480 /**
481 * @test
482 * @author Robert Lemke <robert@typo3.org>
483 * @author Ingo Renner <ingo@typo3.org>
484 */
485 public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag() {
486 $cacheIdentifier = 'UnitTestCache';
487 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
488 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
489 array(),
490 '',
491 FALSE
492 );
493 $cache->expects($this->atLeastOnce())
494 ->method('getIdentifier')
495 ->will($this->returnValue($cacheIdentifier));
496
497 $this->backend->setCache($cache);
498
499 $data = 'some data' . microtime();
500 $this->backend->set('BackendFileTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
501 $this->backend->set('BackendFileTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
502 $this->backend->set('BackendFileTest3', $data, array('UnitTestTag%test'));
503
504 $expectedEntry = 'BackendFileTest2';
505
506 $actualEntries = $this->backend->findIdentifiersByTag('UnitTestTag%special');
507 $this->assertTrue(is_array($actualEntries), 'actualEntries is not an array.');
508
509 $this->assertEquals($expectedEntry, array_pop($actualEntries));
510 }
511
512 /**
513 * @test
514 * @author Robert Lemke <robert@typo3.org>
515 * @author Ingo Renner <ingo@typo3.org>
516 */
517 public function flushRemovesAllCacheEntriesAndRelatedTags() {
518 $cacheIdentifier = 'UnitTestCache';
519 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
520 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
521 array(),
522 '',
523 FALSE
524 );
525 $cache->expects($this->atLeastOnce())
526 ->method('getIdentifier')
527 ->will($this->returnValue($cacheIdentifier));
528
529 $this->backend->setCache($cache);
530
531 $tagsDirectory = $this->backend->getCacheDirectory() . 'tags/';
532 $cacheDirectory = $this->backend->getCacheDirectory() . 'data/' . $cacheIdentifier . '/';
533
534 $data = 'some data' . microtime();
535 $this->backend->set('BackendFileTest1', $data, array('UnitTestTag%test'));
536 $this->backend->set('BackendFileTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
537 $this->backend->set('BackendFileTest3', $data, array('UnitTestTag%test'));
538
539 $this->backend->flush();
540
541 $pattern = $cacheDirectory . '*/*/*';
542 $filesFound = glob($pattern);
543 $this->assertTrue(count($filesFound) == 0, 'Still files in the cache directory');
544
545 $entryIdentifier = 'BackendFileTest1';
546 $this->assertTrue(!file_exists($tagsDirectory . 'UnitTestTag%test/' . $entryIdentifier), 'File "' . $tagsDirectory . 'UnitTestTag%test/' . $entryIdentifier . '" still exists.');
547 $entryIdentifier = 'BackendFileTest2';
548 $this->assertTrue(!file_exists($tagsDirectory . 'UnitTestTag%test/' . $entryIdentifier), 'File "' . $tagsDirectory . 'UnitTestTag%test/' . $entryIdentifier . '" still exists.');
549 $entryIdentifier = 'BackendFileTest3';
550 $this->assertTrue(!file_exists($tagsDirectory . 'UnitTestTag%test/' . $entryIdentifier), 'File "' . $tagsDirectory . 'UnitTestTag%test/' . $entryIdentifier . '" still exists.');
551 $this->assertTrue(!file_exists($tagsDirectory . 'UnitTestTag%special/' . $entryIdentifier), 'File "' . $tagsDirectory . 'UnitTestTag%special/' . $entryIdentifier . '" still exists.');
552 }
553
554 /**
555 * @test
556 * @author Robert Lemke <robert@typo3.org>
557 * @author Ingo Renner <ingo@typo3.org>
558 */
559 public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
560 $cacheIdentifier = 'UnitTestCache';
561 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
562 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
563 array(),
564 '',
565 FALSE
566 );
567 $cache->expects($this->atLeastOnce())
568 ->method('getIdentifier')
569 ->will($this->returnValue($cacheIdentifier));
570
571 $this->backend->setCache($cache);
572
573 $data = 'some data' . microtime();
574 $this->backend->set('BackendFileTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
575 $this->backend->set('BackendFileTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
576 $this->backend->set('BackendFileTest3', $data, array('UnitTestTag%test'));
577
578 $this->backend->flushByTag('UnitTestTag%special');
579
580 $this->assertTrue($this->backend->has('BackendFileTest1'), 'BackendFileTest1');
581 $this->assertFalse($this->backend->has('BackendFileTest2'), 'BackendFileTest2');
582 $this->assertTrue($this->backend->has('BackendFileTest3'), 'BackendFileTest3');
583 }
584
585
586 /**
587 * @test
588 * @author Robert Lemke <robert@typo3.org>
589 * @author Karsten Dambekalns <karsten@typo3.org>
590 * @author Ingo Renner <ingo@typo3.org>
591 */
592 public function hasReturnsTheCorrectResultForEntryWithExceededLifetime() {
593 $cacheIdentifier = 'UnitTestCache';
594 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
595 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
596 array(),
597 '',
598 FALSE
599 );
600 $cache->expects($this->atLeastOnce())
601 ->method('getIdentifier')
602 ->will($this->returnValue($cacheIdentifier));
603
604 $this->backend->setCache($cache);
605
606 $entryIdentifier = 'BackendFileTest';
607 $data = 'some data' . microtime();
608 $this->backend->set($entryIdentifier, $data);
609
610 $expiredEntryIdentifier = 'ExpiredBackendFileTest';
611 $expiredData = 'some old data' . microtime();
612 $this->backend->set($expiredEntryIdentifier, $expiredData, array(), 1);
613
614 sleep(2);
615
616 $this->assertFalse($this->backend->has($expiredEntryIdentifier), 'has() did not return FALSE.');
617 }
618
619 /**
620 * @test
621 * @author Karsten Dambekalns <karsten@typo3.org>
622 * @author Ingo Renner <ingo@typo3.org>
623 */
624 public function getReturnsFalseForEntryWithExceededLifetime() {
625 $cacheIdentifier = 'UnitTestCache';
626 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
627 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
628 array(),
629 '',
630 FALSE
631 );
632 $cache->expects($this->atLeastOnce())
633 ->method('getIdentifier')
634 ->will($this->returnValue($cacheIdentifier));
635
636 $this->backend->setCache($cache);
637
638 $entryIdentifier = 'BackendFileTest';
639 $data = 'some data' . microtime();
640 $this->backend->set($entryIdentifier, $data);
641
642 $expiredEntryIdentifier = 'ExpiredBackendFileTest';
643 $expiredData = 'some old data' . microtime();
644 $this->backend->set($expiredEntryIdentifier, $expiredData, array(), 1);
645
646 sleep(2);
647
648 $this->assertEquals($data, $this->backend->get($entryIdentifier), 'The original and the retrieved data don\'t match.');
649 $this->assertFalse($this->backend->get($expiredEntryIdentifier), 'The expired entry could be loaded.');
650 }
651
652 /**
653 * @test
654 * @author Karsten Dambekalns <karsten@typo3.org>
655 * @author Ingo Renner <ingo@typo3.org>
656 */
657 public function findIdentifiersByTagReturnsEmptyArrayForEntryWithExceededLifetime() {
658 $cacheIdentifier = 'UnitTestCache';
659 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
660 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
661 array(),
662 '',
663 FALSE
664 );
665 $cache->expects($this->atLeastOnce())
666 ->method('getIdentifier')
667 ->will($this->returnValue($cacheIdentifier));
668
669 $this->backend->setCache($cache);
670
671 $this->backend->set('BackendFileTest', 'some data', array('UnitTestTag%special'), 1);
672
673 sleep(2);
674
675 $this->assertEquals(array(), $this->backend->findIdentifiersByTag('UnitTestTag%special'));
676 }
677
678 /**
679 * @test
680 * @author Robert Lemke <robert@typo3.org>
681 * @author Karsten Dambekalns <karsten@typo3.org>
682 * @author Ingo Renner <ingo@typo3.org>
683 */
684 public function setWithUnlimitedLifetimeWritesCorrectEntry() {
685 $cacheIdentifier = 'UnitTestCache';
686 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
687 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
688 array(),
689 '',
690 FALSE
691 );
692 $cache->expects($this->atLeastOnce())
693 ->method('getIdentifier')
694 ->will($this->returnValue($cacheIdentifier));
695
696 $data = 'some data' . microtime();
697 $entryIdentifier = 'BackendFileTest';
698 $entryIdentifierHash = sha1($entryIdentifier);
699
700 $this->backend->setCache($cache);
701 $this->backend->set($entryIdentifier, $data, array(), 0);
702
703 $cacheDirectory = $this->backend->getCacheDirectory();
704
705 $pathAndFilename = $cacheDirectory
706 . 'data/'
707 . $cacheIdentifier . '/'
708 . $entryIdentifierHash[0] . '/'
709 . $entryIdentifierHash[1] . '/'
710 . $entryIdentifier;
711 $this->assertTrue(file_exists($pathAndFilename), 'File not found.');
712
713 $retrievedData = file_get_contents($pathAndFilename, NULL, NULL, t3lib_cache_backend_FileBackend::EXPIRYTIME_LENGTH);
714 $this->assertEquals($data, $retrievedData, 'The original and the retrieved data don\'t match.');
715 }
716
717 /**
718 * @test
719 * @author Robert Lemke <robert@typo3.org>
720 * @author Ingo Renner <ingo@typo3.org>
721 */
722 public function tearDown() {
723 if (is_object($this->backend)) {
724 $directory = $this->backend->getCacheDirectory();
725 if (is_dir($directory)) {
726 t3lib_div::rmdir($directory, true);
727 }
728 }
729 }
730 }
731
732 ?>