e1c9fd5d0c7975d4fff43473e9772cd0db58e6d2
[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(), 'getCacheDirectory() 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 . $entryIdentifier
427 . '?';
428
429 $this->backend->set($entryIdentifier . 'A', $data, array(), 1);
430 $this->backend->set($entryIdentifier . 'B', $data, array(), 1);
431 $this->backend->set($entryIdentifier . 'C', $data, array(), 1);
432 $filesFound = glob($pattern);
433 $this->assertTrue(is_array($filesFound) && count($filesFound) > 0, 'The cache entries do not exist.');
434
435 sleep(2);
436
437 $this->backend->collectGarbage();
438 $filesFound = is_array(glob($pattern)) ? glob($pattern) : array();
439 $this->assertTrue(count($filesFound) === 0, 'The cache entries still exist.');
440 }
441
442 /**
443 * @test
444 * @author Robert Lemke <robert@typo3.org>
445 * @author Ingo Renner <ingo@typo3.org>
446 */
447 public function removeReallyRemovesTagsOfRemovedEntry() {
448 $cacheIdentifier = 'UnitTestCache';
449 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
450 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
451 array(),
452 '',
453 FALSE
454 );
455 $cache->expects($this->atLeastOnce())
456 ->method('getIdentifier')
457 ->will($this->returnValue($cacheIdentifier));
458
459 $data = 'some data' . microtime();
460 $entryIdentifier = 'BackendFileTest';
461
462 $this->backend->setCache($cache);
463
464 $tagsDirectory = $this->backend->getCacheDirectory() . 'tags/';
465
466 $this->backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
467 $this->backend->remove($entryIdentifier);
468
469 $this->assertTrue(!file_exists($tagsDirectory . 'UnitTestTag%tag1/' . $entryIdentifier), 'File "' . $tagsDirectory . 'UnitTestTag%tag1/' . $entryIdentifier . '" still exists.');
470 $this->assertTrue(!file_exists($tagsDirectory . 'UnitTestTag%tag2/' . $entryIdentifier), 'File "' . $tagsDirectory . 'UnitTestTag%tag2/' . $entryIdentifier . '" still exists.');
471 }
472
473 /**
474 * @test
475 * @author Robert Lemke <robert@typo3.org>
476 * @author Ingo Renner <ingo@typo3.org>
477 */
478 public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag() {
479 $cacheIdentifier = 'UnitTestCache';
480 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
481 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
482 array(),
483 '',
484 FALSE
485 );
486 $cache->expects($this->atLeastOnce())
487 ->method('getIdentifier')
488 ->will($this->returnValue($cacheIdentifier));
489
490 $this->backend->setCache($cache);
491
492 $data = 'some data' . microtime();
493 $this->backend->set('BackendFileTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
494 $this->backend->set('BackendFileTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
495 $this->backend->set('BackendFileTest3', $data, array('UnitTestTag%test'));
496
497 $expectedEntry = 'BackendFileTest2';
498
499 $actualEntries = $this->backend->findIdentifiersByTag('UnitTestTag%special');
500 $this->assertTrue(is_array($actualEntries), 'actualEntries is not an array.');
501
502 $this->assertEquals($expectedEntry, array_pop($actualEntries));
503 }
504
505 /**
506 * @test
507 * @author Robert Lemke <robert@typo3.org>
508 * @author Ingo Renner <ingo@typo3.org>
509 */
510 public function flushRemovesAllCacheEntriesAndRelatedTags() {
511 $cacheIdentifier = 'UnitTestCache';
512 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
513 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
514 array(),
515 '',
516 FALSE
517 );
518 $cache->expects($this->atLeastOnce())
519 ->method('getIdentifier')
520 ->will($this->returnValue($cacheIdentifier));
521
522 $this->backend->setCache($cache);
523
524 $tagsDirectory = $this->backend->getCacheDirectory() . 'tags/';
525 $cacheDirectory = $this->backend->getCacheDirectory() . 'data/' . $cacheIdentifier . '/';
526
527 $data = 'some data' . microtime();
528 $this->backend->set('BackendFileTest1', $data, array('UnitTestTag%test'));
529 $this->backend->set('BackendFileTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
530 $this->backend->set('BackendFileTest3', $data, array('UnitTestTag%test'));
531
532 $this->backend->flush();
533
534 $pattern = $cacheDirectory . '*/*/*';
535 $filesFound = is_array(glob($pattern)) ? glob($pattern) : array();
536 $this->assertTrue(count($filesFound) === 0, 'Still files in the cache directory');
537
538 $tagPrefixTest = $tagsDirectory . 'UnitTestTag%test/' . $cacheIdentifier . '^';
539 $tagPrefixSpecial = $tagsDirectory . 'UnitTestTag%special/' . $cacheIdentifier . '^';
540 $entryIdentifier = 'BackendFileTest1';
541 $this->assertTrue(!file_exists($tagPrefixTest . $entryIdentifier), 'File "' . $tagPrefixTest . $entryIdentifier . '" still exists.');
542 $entryIdentifier = 'BackendFileTest2';
543 $this->assertTrue(!file_exists($tagPrefixTest . $entryIdentifier), 'File "' . $tagPrefixTest . $entryIdentifier . '" still exists.');
544 $this->assertTrue(!file_exists($tagPrefixSpecial . $entryIdentifier), 'File "' . $tagPrefixSpecial . $entryIdentifier . '" still exists.');
545 $entryIdentifier = 'BackendFileTest3';
546 $this->assertTrue(!file_exists($tagPrefixTest . $entryIdentifier), 'File "' . $tagPrefixTest . $entryIdentifier . '" still exists.');
547 }
548
549 /**
550 * @test
551 * @author Robert Lemke <robert@typo3.org>
552 * @author Ingo Renner <ingo@typo3.org>
553 */
554 public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
555 $cacheIdentifier = 'UnitTestCache';
556 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
557 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
558 array(),
559 '',
560 FALSE
561 );
562 $cache->expects($this->atLeastOnce())
563 ->method('getIdentifier')
564 ->will($this->returnValue($cacheIdentifier));
565
566 $this->backend->setCache($cache);
567
568 $data = 'some data' . microtime();
569 $this->backend->set('BackendFileTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
570 $this->backend->set('BackendFileTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
571 $this->backend->set('BackendFileTest3', $data, array('UnitTestTag%test'));
572
573 $this->backend->flushByTag('UnitTestTag%special');
574
575 $this->assertTrue($this->backend->has('BackendFileTest1'), 'BackendFileTest1');
576 $this->assertFalse($this->backend->has('BackendFileTest2'), 'BackendFileTest2');
577 $this->assertTrue($this->backend->has('BackendFileTest3'), 'BackendFileTest3');
578 }
579
580
581 /**
582 * @test
583 * @author Robert Lemke <robert@typo3.org>
584 * @author Karsten Dambekalns <karsten@typo3.org>
585 * @author Ingo Renner <ingo@typo3.org>
586 */
587 public function hasReturnsTheCorrectResultForEntryWithExceededLifetime() {
588 $cacheIdentifier = 'UnitTestCache';
589 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
590 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
591 array(),
592 '',
593 FALSE
594 );
595 $cache->expects($this->atLeastOnce())
596 ->method('getIdentifier')
597 ->will($this->returnValue($cacheIdentifier));
598
599 $this->backend->setCache($cache);
600
601 $entryIdentifier = 'BackendFileTest';
602 $data = 'some data' . microtime();
603 $this->backend->set($entryIdentifier, $data);
604
605 $expiredEntryIdentifier = 'ExpiredBackendFileTest';
606 $expiredData = 'some old data' . microtime();
607 $this->backend->set($expiredEntryIdentifier, $expiredData, array(), 1);
608
609 sleep(2);
610
611 $this->assertFalse($this->backend->has($expiredEntryIdentifier), 'has() did not return FALSE.');
612 }
613
614 /**
615 * @test
616 * @author Karsten Dambekalns <karsten@typo3.org>
617 * @author Ingo Renner <ingo@typo3.org>
618 */
619 public function getReturnsFalseForEntryWithExceededLifetime() {
620 $cacheIdentifier = 'UnitTestCache';
621 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
622 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
623 array(),
624 '',
625 FALSE
626 );
627 $cache->expects($this->atLeastOnce())
628 ->method('getIdentifier')
629 ->will($this->returnValue($cacheIdentifier));
630
631 $this->backend->setCache($cache);
632
633 $entryIdentifier = 'BackendFileTest';
634 $data = 'some data' . microtime();
635 $this->backend->set($entryIdentifier, $data);
636
637 $expiredEntryIdentifier = 'ExpiredBackendFileTest';
638 $expiredData = 'some old data' . microtime();
639 $this->backend->set($expiredEntryIdentifier, $expiredData, array(), 1);
640
641 sleep(2);
642
643 $this->assertEquals($data, $this->backend->get($entryIdentifier), 'The original and the retrieved data don\'t match.');
644 $this->assertFalse($this->backend->get($expiredEntryIdentifier), 'The expired entry could be loaded.');
645 }
646
647 /**
648 * @test
649 * @author Karsten Dambekalns <karsten@typo3.org>
650 * @author Ingo Renner <ingo@typo3.org>
651 */
652 public function findIdentifiersByTagReturnsEmptyArrayForEntryWithExceededLifetime() {
653 $cacheIdentifier = 'UnitTestCache';
654 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
655 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
656 array(),
657 '',
658 FALSE
659 );
660 $cache->expects($this->atLeastOnce())
661 ->method('getIdentifier')
662 ->will($this->returnValue($cacheIdentifier));
663
664 $this->backend->setCache($cache);
665
666 $this->backend->set('BackendFileTest', 'some data', array('UnitTestTag%special'), 1);
667
668 sleep(2);
669
670 $this->assertEquals(array(), $this->backend->findIdentifiersByTag('UnitTestTag%special'));
671 }
672
673 /**
674 * @test
675 * @author Robert Lemke <robert@typo3.org>
676 * @author Karsten Dambekalns <karsten@typo3.org>
677 * @author Ingo Renner <ingo@typo3.org>
678 */
679 public function setWithUnlimitedLifetimeWritesCorrectEntry() {
680 $cacheIdentifier = 'UnitTestCache';
681 $cache = $this->getMock('t3lib_cache_frontend_AbstractFrontend',
682 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove'),
683 array(),
684 '',
685 FALSE
686 );
687 $cache->expects($this->atLeastOnce())
688 ->method('getIdentifier')
689 ->will($this->returnValue($cacheIdentifier));
690
691 $data = 'some data' . microtime();
692 $entryIdentifier = 'BackendFileTest';
693 $entryIdentifierHash = sha1($entryIdentifier);
694
695 $this->backend->setCache($cache);
696 $this->backend->set($entryIdentifier, $data, array(), 0);
697
698 $cacheDirectory = $this->backend->getCacheDirectory();
699
700 $pathAndFilename = $cacheDirectory
701 . 'data/'
702 . $cacheIdentifier . '/'
703 . $entryIdentifierHash[0] . '/'
704 . $entryIdentifierHash[1] . '/'
705 . $entryIdentifier;
706 $this->assertTrue(file_exists($pathAndFilename), 'File not found.');
707
708 $retrievedData = file_get_contents($pathAndFilename, NULL, NULL, t3lib_cache_backend_FileBackend::EXPIRYTIME_LENGTH);
709 $this->assertEquals($data, $retrievedData, 'The original and the retrieved data don\'t match.');
710 }
711
712 /**
713 * @author Robert Lemke <robert@typo3.org>
714 * @author Ingo Renner <ingo@typo3.org>
715 */
716 public function tearDown() {
717 if (is_object($this->backend)) {
718 $directory = $this->backend->getCacheDirectory();
719 if (is_dir($directory)) {
720 t3lib_div::rmdir($directory, true);
721 }
722 }
723 }
724 }
725
726 ?>