[TASK] Prefix of caching framework tables is hardcoded
[Packages/TYPO3.CMS.git] / tests / t3lib / cache / backend / class.t3lib_cache_backend_dbbackendTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 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 * Testcase for the DB cache backend
27 *
28 * @author Ingo Renner <ingo@typo3.org>
29 * @package TYPO3
30 * @subpackage tests
31 */
32 class t3lib_cache_backend_DbBackendTest extends tx_phpunit_testcase {
33
34 /**
35 * Enable backup of global and system variables
36 *
37 * @var boolean
38 */
39 protected $backupGlobals = TRUE;
40
41 /**
42 * Exclude TYPO3_DB from backup/ restore of $GLOBALS
43 * because resource types cannot be handled during serializing
44 *
45 * @var array
46 */
47 protected $backupGlobalsBlacklist = array('TYPO3_DB');
48
49 /**
50 * @var t3lib_DB Backup of original TYPO3_DB instance
51 */
52 protected $typo3DbBackup;
53
54 /**
55 * @var string Name of the testing data table
56 */
57 protected $testingCacheTable;
58
59 /**
60 * @var string Name of the testing tags table
61 */
62 protected $testingTagsTable;
63
64 /**
65 * Set up testcases
66 */
67 public function setUp() {
68 $this->typo3DbBackup = $GLOBALS['TYPO3_DB'];
69 $tablePrefix = $GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['tablePrefix'];
70 $this->testingCacheTable = $tablePrefix . 'Testing';
71 $this->testingTagsTable = $tablePrefix . 'Testing_tags';
72 }
73
74 /**
75 * Sets up the backend used for testing
76 *
77 * @return void
78 * @author Ingo Renner <ingo@typo3.org>
79 * @author Christian Kuhn <lolli@schwarzbu.ch>
80 */
81 protected function setUpBackend(array $backendOptions = array()) {
82 $GLOBALS['TYPO3_DB']->sql_query('CREATE TABLE ' . $this->testingCacheTable . ' (
83 id int(11) unsigned NOT NULL auto_increment,
84 identifier varchar(250) DEFAULT \'\' NOT NULL,
85 expires int(11) unsigned DEFAULT \'0\' NOT NULL,
86 content mediumblob,
87 PRIMARY KEY (id),
88 KEY cache_id (identifier, expires)
89 ) ENGINE=InnoDB;
90 ');
91
92 $GLOBALS['TYPO3_DB']->sql_query('CREATE TABLE ' . $this->testingTagsTable . ' (
93 id int(11) unsigned NOT NULL auto_increment,
94 identifier varchar(250) DEFAULT \'\' NOT NULL,
95 tag varchar(250) DEFAULT \'\' NOT NULL,
96 PRIMARY KEY (id),
97 KEY cache_id (identifier),
98 KEY cache_tag (tag)
99 ) ENGINE=InnoDB;
100 ');
101
102 $backend = t3lib_div::makeInstance(
103 't3lib_cache_backend_DbBackend',
104 'Testing',
105 $backendOptions
106 );
107
108 return $backend;
109 }
110
111 /**
112 * Helper method to inject a mock frontend to backend instance
113 *
114 * @param t3lib_cache_backend_DbBackend $backend Current backend instance
115 * @return t3lib_cache_frontend_Frontend Mock frontend
116 */
117 protected function setUpMockFrontendOfBackend(t3lib_cache_backend_DbBackend $backend) {
118 $mockCache = $this->getMock('t3lib_cache_frontend_AbstractFrontend', array(), array(), '', FALSE);
119 $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('Testing'));
120 $backend->setCache($mockCache);
121
122 return $mockCache;
123 }
124
125 /**
126 * @author Ingo Renner <ingo@typo3.org>
127 */
128 public function tearDown() {
129 $GLOBALS['TYPO3_DB'] = $this->typo3DbBackup;
130
131 $GLOBALS['TYPO3_DB']->sql_query(
132 'DROP TABLE IF EXISTS ' . $this->testingCacheTable . ';'
133 );
134
135 $GLOBALS['TYPO3_DB']->sql_query(
136 'DROP TABLE IF EXISTS ' . $this->testingTagsTable . ';'
137 );
138 }
139
140 /**
141 * @test
142 * @author Ingo Renner <ingo@typo3.org>
143 */
144 public function setCacheCalculatesCacheTableName() {
145 $backend = $this->setUpBackend();
146 $this->setUpMockFrontendOfBackend($backend);
147 $this->assertEquals($this->testingCacheTable, $backend->getCacheTable());
148 }
149
150 /**
151 * @test
152 */
153 public function setCacheCalculatesTagsTableName() {
154 $backend = $this->setUpBackend();
155 $this->setUpMockFrontendOfBackend($backend);
156
157 $this->assertEquals($this->testingTagsTable, $backend->getTagsTable());
158 }
159
160 /**
161 * @test
162 * @expectedException t3lib_cache_Exception
163 */
164 public function setThrowsExceptionIfFrontendWasNotSet() {
165 $backend = $this->setUpBackend();
166 $backend->set('identifier', 'data');
167 }
168
169 /**
170 * @test
171 * @expectedException t3lib_cache_exception_InvalidData
172 * @author Ingo Renner <ingo@typo3.org>
173 */
174 public function setThrowsExceptionIfDataIsNotAString() {
175 $backend = $this->setUpBackend();
176 $this->setUpMockFrontendOfBackend($backend);
177
178 $data = array('Some data');
179 $entryIdentifier = 'BackendDbTest';
180
181 $backend->set($entryIdentifier, $data);
182 }
183
184 /**
185 * @test
186 * @author Ingo Renner <ingo@typo3.org>
187 */
188 public function setInsertsEntryInTable() {
189 $backend = $this->setUpBackend();
190 $this->setUpMockFrontendOfBackend($backend);
191
192 $data = 'some data' . microtime();
193 $entryIdentifier = 'BackendDbTest';
194
195 $backend->set($entryIdentifier, $data);
196
197 $entryFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
198 '*',
199 $this->testingCacheTable,
200 'identifier = \'' . $entryIdentifier . '\''
201 );
202
203 $this->assertEquals($data, $entryFound['content']);
204 }
205
206 /**
207 * @test
208 * @author Ingo Renner <ingo@typo3.org>
209 */
210 public function setRemovesAnAlreadyExistingCacheEntryForTheSameIdentifier() {
211 $backend = $this->setUpBackend();
212 $this->setUpMockFrontendOfBackend($backend);
213
214 $data1 = 'some data' . microtime();
215 $data2 = $data1 . '_different';
216 $entryIdentifier = 'BackendDbRemoveBeforeSetTest';
217
218 $backend->set($entryIdentifier, $data1, array(), 500);
219 $backend->set($entryIdentifier, $data2, array(), 200);
220
221 $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
222 '*',
223 $this->testingCacheTable,
224 'identifier = \'' . $entryIdentifier . '\''
225 );
226
227 $this->assertEquals(1, count($entriesFound));
228 }
229
230 /**
231 * @test
232 * @author Ingo Renner <ingo@typo3.org>
233 */
234 public function setReallySavesSpecifiedTags() {
235 $backend = $this->setUpBackend();
236 $this->setUpMockFrontendOfBackend($backend);
237
238 $data = 'some data' . microtime();
239 $entryIdentifier = 'BackendDbTest';
240
241 $backend->set($entryIdentifier, $data, array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
242
243 $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
244 '*',
245 $this->testingTagsTable,
246 'identifier = \'' . $entryIdentifier . '\''
247 );
248
249 $tags = array();
250
251 foreach ($entriesFound as $entry) {
252 $tags[] = $entry['tag'];
253 }
254
255 $this->assertTrue(count($tags) > 0);
256 $this->assertTrue(in_array('UnitTestTag%tag1', $tags));
257 $this->assertTrue(in_array('UnitTestTag%tag2', $tags));
258 }
259
260 /**
261 * @test
262 * @author Christian Kuhn <lolli@schwarzbu.ch>
263 */
264 public function setSavesCompressedDataWithEnabledCompression() {
265 $backend = $this->setUpBackend(
266 array(
267 'compression' => TRUE,
268 )
269 );
270 $this->setUpMockFrontendOfBackend($backend);
271
272 $data = 'some data ' . microtime();
273 $entryIdentifier = 'BackendDbTest';
274
275 $backend->set($entryIdentifier, $data);
276
277 $entry = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
278 'content',
279 $this->testingCacheTable,
280 'identifier = \'' . $entryIdentifier . '\''
281 );
282
283 $this->assertEquals($data, @gzuncompress($entry['content']));
284 }
285
286 /**
287 * @test
288 * @author Christian Kuhn <lolli@schwarzbu.ch>
289 */
290 public function setSavesPlaintextDataWithEnabledCompressionAndCompressionLevel0() {
291 $backend = $this->setUpBackend(
292 array(
293 'compression' => TRUE,
294 'compressionLevel' => 0,
295 )
296 );
297 $this->setUpMockFrontendOfBackend($backend);
298
299 $data = 'some data ' . microtime();
300 $entryIdentifier = 'BackendDbTest';
301
302 $backend->set($entryIdentifier, $data);
303
304 $entry = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
305 'content',
306 $this->testingCacheTable,
307 'identifier = \'' . $entryIdentifier . '\''
308 );
309
310 $this->assertGreaterThan(0, substr_count($entry['content'], $data));
311 }
312
313 /**
314 * @test
315 * @expectedException t3lib_cache_Exception
316 */
317 public function getThrowsExceptionIfFrontendWasNotSet() {
318 $backend = $this->setUpBackend();
319 $backend->get('identifier');
320 }
321
322 /**
323 * @test
324 * @author Ingo Renner <ingo@typo3.org>
325 */
326 public function getReturnsContentOfTheCorrectCacheEntry() {
327 $backend = $this->setUpBackend();
328 $this->setUpMockFrontendOfBackend($backend);
329
330 $data = 'some data' . microtime();
331 $entryIdentifier = 'BackendDbTest';
332
333 $backend->set($entryIdentifier, $data, array(), 500);
334
335 $data = 'some other data' . microtime();
336 $backend->set($entryIdentifier, $data, array(), 100);
337
338 $loadedData = $backend->get($entryIdentifier);
339
340 $this->assertEquals($data, $loadedData);
341 }
342
343 /**
344 * @test
345 * @expectedException t3lib_cache_Exception
346 */
347 public function hasThrowsExceptionIfFrontendWasNotSet() {
348 $backend = $this->setUpBackend();
349 $backend->has('identifier');
350 }
351
352 /**
353 * @test
354 * @author Ingo Renner <ingo@typo3.org>
355 */
356 public function hasReturnsTheCorrectResult() {
357 $backend = $this->setUpBackend();
358 $this->setUpMockFrontendOfBackend($backend);
359
360 $data = 'some data' . microtime();
361 $entryIdentifier = 'BackendDbTest';
362
363 $backend->set($entryIdentifier, $data);
364
365 $this->assertTrue($backend->has($entryIdentifier));
366 $this->assertFalse($backend->has($entryIdentifier . 'Not'));
367 }
368
369 /**
370 * @test
371 * @expectedException t3lib_cache_Exception
372 */
373 public function removeThrowsExceptionIfFrontendWasNotSet() {
374 $backend = $this->setUpBackend();
375 $backend->remove('identifier');
376 }
377
378 /**
379 * @test
380 * @author Ingo Renner <ingo@typo3.org>
381 */
382 public function removeReallyRemovesACacheEntry() {
383 $backend = $this->setUpBackend();
384 $this->setUpMockFrontendOfBackend($backend);
385
386 $data = 'some data' . microtime();
387 $entryIdentifier = 'BackendDbRemovalTest';
388
389 $backend->set($entryIdentifier, $data);
390
391 $backend->remove($entryIdentifier);
392
393 $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
394 '*',
395 $this->testingCacheTable,
396 'identifier = \'' . $entryIdentifier . '\''
397 );
398
399 $this->assertTrue(count($entriesFound) == 0);
400 }
401
402 /**
403 * @test
404 * @expectedException t3lib_cache_Exception
405 */
406 public function collectGarbageThrowsExceptionIfFrontendWasNotSet() {
407 $backend = $this->setUpBackend();
408 $backend->collectGarbage();
409 }
410
411 /**
412 * @test
413 * @author Ingo Renner <ingo@typo3.org>
414 */
415 public function collectGarbageReallyRemovesAnExpiredCacheEntry() {
416 $backend = $this->setUpBackend();
417 $mockCache = $this->setUpMockFrontendOfBackend($backend);
418
419 $data = 'some data' . microtime();
420 $entryIdentifier = 'BackendDbRemovalTest';
421
422 $backend->set($entryIdentifier, $data, array(), 1);
423
424 $GLOBALS['EXEC_TIME'] += 2;
425 // setCache calls initializeCommonReferences which recalculate expire statement
426 // needed after manual $GLOBALS['EXEC_TIME'] manipulation
427 $backend->setCache($mockCache);
428
429 $backend->collectGarbage();
430
431 $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
432 '*',
433 $this->testingCacheTable,
434 'identifier = \'' . $entryIdentifier . '\''
435 );
436
437 $this->assertTrue(count($entriesFound) == 0);
438 }
439
440 /**
441 * @test
442 * @author Ingo Renner <ingo@typo3.org>
443 */
444 public function collectGarbageReallyRemovesAllExpiredCacheEntries() {
445 $backend = $this->setUpBackend();
446 $mockCache = $this->setUpMockFrontendOfBackend($backend);
447
448 $data = 'some data' . microtime();
449 $entryIdentifier = 'BackendDbRemovalTest';
450
451 $backend->set($entryIdentifier . 'A', $data, array(), 1);
452 $backend->set($entryIdentifier . 'B', $data, array(), 1);
453 $backend->set($entryIdentifier . 'C', $data, array(), 1);
454
455 $GLOBALS['EXEC_TIME'] += 2;
456 // setCache calls initializeCommonReferences which recalculate expire statement
457 // needed after manual $GLOBALS['EXEC_TIME'] manipulation
458 $backend->setCache($mockCache);
459
460 $backend->collectGarbage();
461
462 $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
463 '*',
464 $this->testingCacheTable,
465 ''
466 );
467
468 $this->assertTrue(count($entriesFound) == 0);
469 }
470
471 /**
472 * @test
473 * @author Ingo Renner <ingo@typo3.org>
474 */
475 public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag() {
476 $backend = $this->setUpBackend();
477 $this->setUpMockFrontendOfBackend($backend);
478
479 $data = 'some data' . microtime();
480 $backend->set('BackendDbTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
481 $backend->set('BackendDbTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
482 $backend->set('BackendDbTest3', $data, array('UnitTestTag%test'));
483
484 $expectedEntry = 'BackendDbTest2';
485
486 $actualEntries = $backend->findIdentifiersByTag('UnitTestTag%special');
487 $this->assertTrue(is_array($actualEntries));
488 $this->assertEquals($expectedEntry, array_pop($actualEntries));
489 }
490
491 /**
492 * @test
493 * @expectedException t3lib_cache_Exception
494 */
495 public function flushThrowsExceptionIfFrontendWasNotSet() {
496 $backend = $this->setUpBackend();
497 $backend->flush();
498 }
499
500 /**
501 * @test
502 * @author Ingo Renner <ingo@typo3.org>
503 */
504 public function flushRemovesAllCacheEntries() {
505 $backend = $this->setUpBackend();
506 $this->setUpMockFrontendOfBackend($backend);
507
508 $data = 'some data' . microtime();
509 $backend->set('BackendDbTest1', $data, array('UnitTestTag%test'));
510 $backend->set('BackendDbTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
511 $backend->set('BackendDbTest3', $data, array('UnitTestTag%test'));
512
513 $backend->flush();
514
515 $entriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
516 '*',
517 $this->testingCacheTable,
518 ''
519 );
520
521 $this->assertTrue(count($entriesFound) == 0);
522 }
523
524 /**
525 * @test
526 */
527 public function flushDropsDataTable() {
528 $backend = $this->setUpBackend();
529 $this->setUpMockFrontendOfBackend($backend);
530
531 $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB', array('exec_TRUNCATEquery', 'INSERTquery', 'sql_query', 'admin_query'));
532 $GLOBALS['TYPO3_DB']->expects($this->at(2))
533 ->method('admin_query')
534 ->with('DROP TABLE IF EXISTS ' . $this->testingCacheTable);
535
536 $backend->flush();
537 }
538
539 /**
540 * @test
541 */
542 public function flushDropsTagsTable() {
543 $backend = $this->setUpBackend();
544 $this->setUpMockFrontendOfBackend($backend);
545
546 $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB', array('exec_TRUNCATEquery', 'INSERTquery', 'sql_query', 'admin_query'));
547 $GLOBALS['TYPO3_DB']->expects($this->at(3))
548 ->method('admin_query')
549 ->with('DROP TABLE IF EXISTS ' . $this->testingTagsTable);
550
551 $backend->flush();
552 }
553
554 /**
555 * @test
556 */
557 public function flushCreatesDataTable() {
558 $backend = $this->setUpBackend();
559 $this->setUpMockFrontendOfBackend($backend);
560
561 $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB', array('exec_TRUNCATEquery', 'INSERTquery', 'sql_query', 'admin_query'));
562 $GLOBALS['TYPO3_DB']->expects($this->at(4))
563 ->method('admin_query')
564 ->will($this->returnCallback(array($this, flushCreatesDataTableCallback)));
565
566 $backend->flush();
567 }
568
569 /**
570 * Callback of flushCreatesDataTable to check if data table is created
571 *
572 * @param string $sql SQL of admin_query
573 * @return void
574 */
575 public function flushCreatesDataTableCallback($sql) {
576 $startOfStatement = 'CREATE TABLE ' . $this->testingCacheTable . ' (';
577 $this->assertEquals($startOfStatement, substr($sql, 0, strlen($startOfStatement)));
578 }
579
580 /**
581 * @test
582 */
583 public function flushCreatesTagsTable() {
584 $backend = $this->setUpBackend();
585 $this->setUpMockFrontendOfBackend($backend);
586
587 $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB', array('exec_TRUNCATEquery', 'INSERTquery', 'sql_query', 'admin_query'));
588 $GLOBALS['TYPO3_DB']->expects($this->at(5))
589 ->method('admin_query')
590 ->will($this->returnCallback(array($this, flushCreatesTagsTableCallback)));
591
592 $backend->flush();
593 }
594
595 /**
596 * Callback of flushCreatesTagsTable to check if tags table is created
597 *
598 * @param string $sql SQL of admin_query
599 * @return void
600 */
601 public function flushCreatesTagsTableCallback($sql) {
602 $startOfStatement = 'CREATE TABLE ' . $this->testingTagsTable . ' (';
603 $this->assertEquals($startOfStatement, substr($sql, 0, strlen($startOfStatement)));
604 }
605
606 /**
607 * @test
608 * @expectedException t3lib_cache_Exception
609 */
610 public function flushByTagThrowsExceptionIfFrontendWasNotSet() {
611 $backend = $this->setUpBackend();
612 $backend->flushByTag(array());
613 }
614
615 /**
616 * @test
617 * @author Ingo Renner <ingo@typo3.org>
618 */
619 public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
620 $backend = $this->setUpBackend();
621 $this->setUpMockFrontendOfBackend($backend);
622
623 $data = 'some data' . microtime();
624 $backend->set('BackendDbTest1', $data, array('UnitTestTag%test', 'UnitTestTag%boring'));
625 $backend->set('BackendDbTest2', $data, array('UnitTestTag%test', 'UnitTestTag%special'));
626 $backend->set('BackendDbTest3', $data, array('UnitTestTag%test'));
627
628 $backend->flushByTag('UnitTestTag%special');
629
630 $this->assertTrue($backend->has('BackendDbTest1'), 'BackendDbTest1 does not exist anymore.');
631 $this->assertFalse($backend->has('BackendDbTest2'), 'BackendDbTest2 still exists.');
632 $this->assertTrue($backend->has('BackendDbTest3'), 'BackendDbTest3 does not exist anymore.');
633
634 $tagEntriesFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
635 '*',
636 $this->testingTagsTable,
637 'tag = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr('UnitTestTag%special', $this->testingTagsTable)
638 );
639 $this->assertEquals(0, count($tagEntriesFound));
640 }
641
642
643 /**
644 * @test
645 * @author Ingo Renner <ingo@typo3.org>
646 */
647 public function hasReturnsTheCorrectResultForEntryWithExceededLifetime() {
648 $backend = $this->setUpBackend();
649 $mockCache = $this->setUpMockFrontendOfBackend($backend);
650
651 $data = 'some data' . microtime();
652 $entryIdentifier = 'BackendDbTest';
653
654 $backend->set($entryIdentifier, $data);
655
656 $expiredEntryIdentifier = 'ExpiredBackendDbTest';
657 $expiredData = 'some old data' . microtime();
658 $backend->set($expiredEntryIdentifier, $expiredData, array(), 1);
659
660 $GLOBALS['EXEC_TIME'] += 2;
661 // setCache calls initializeCommonReferences which recalculate expire statement
662 // needed after manual $GLOBALS['EXEC_TIME'] manipulation
663 $backend->setCache($mockCache);
664
665 $this->assertFalse($backend->has($expiredEntryIdentifier));
666 }
667
668 /**
669 * @test
670 * @author Christian Kuhn <lolli@schwarzbu.ch>
671 */
672 public function hasReturnsTrueForEntryWithUnlimitedLifetime() {
673 $backend = $this->setUpBackend();
674 $mockCache = $this->setUpMockFrontendOfBackend($backend);
675
676 $entryIdentifier = 'BackendDbTest';
677
678 $backend->set($entryIdentifier, 'data', array(), 0);
679
680 $GLOBALS['EXEC_TIME'] += 1;
681 // setCache calls initializeCommonReferences which recalculate expire statement
682 // needed after manual $GLOBALS['EXEC_TIME'] manipulation
683 $backend->setCache($mockCache);
684
685 $this->assertTrue($backend->has($entryIdentifier));
686 }
687
688 /**
689 * @test
690 * @author Ingo Renner <ingo@typo3.org>
691 */
692 public function getReturnsFalseForEntryWithExceededLifetime() {
693 $backend = $this->setUpBackend();
694 $mockCache = $this->setUpMockFrontendOfBackend($backend);
695
696 $data = 'some data' . microtime();
697 $entryIdentifier = 'BackendDbTest';
698
699 $backend->set($entryIdentifier, $data);
700
701 $expiredEntryIdentifier = 'ExpiredBackendDbTest';
702 $expiredData = 'some old data' . microtime();
703 $backend->set($expiredEntryIdentifier, $expiredData, array(), 1);
704
705 $GLOBALS['EXEC_TIME'] += 2;
706 // setCache calls initializeCommonReferences which recalculate expire statement
707 // needed after manual $GLOBALS['EXEC_TIME'] manipulation
708 $backend->setCache($mockCache);
709
710 $this->assertEquals($data, $backend->get($entryIdentifier));
711 $this->assertFalse($backend->get($expiredEntryIdentifier));
712 }
713
714 /**
715 * @test
716 * @expectedException t3lib_cache_Exception
717 */
718 public function findIdentifiersByTagThrowsExceptionIfFrontendWasNotSet() {
719 $backend = $this->setUpBackend();
720 $backend->findIdentifiersByTag('identifier');
721 }
722
723 /**
724 * @test
725 * @author Ingo Renner <ingo@typo3.org>
726 */
727 public function findIdentifiersByTagReturnsEmptyArrayForEntryWithExceededLifetime() {
728 $backend = $this->setUpBackend();
729 $mockCache = $this->setUpMockFrontendOfBackend($backend);
730
731 $backend->set('BackendDbTest', 'some data', array('UnitTestTag%special'), 1);
732
733 $GLOBALS['EXEC_TIME'] += 2;
734 // setCache calls initializeCommonReferences which recalculate expire statement
735 // needed after manual $GLOBALS['EXEC_TIME'] manipulation
736 $backend->setCache($mockCache);
737
738 $this->assertEquals(array(), $backend->findIdentifiersByTag('UnitTestTag%special'));
739 }
740
741 /**
742 * @test
743 * @author Ingo Renner <ingo@typo3.org>
744 */
745 public function setWithUnlimitedLifetimeWritesCorrectEntry() {
746 $backend = $this->setUpBackend();
747 $this->setUpMockFrontendOfBackend($backend);
748
749 $data = 'some data' . microtime();
750 $entryIdentifier = 'BackendFileTest';
751
752 $backend->set($entryIdentifier, $data, array(), 0);
753
754 $entryFound = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
755 '*',
756 $this->testingCacheTable,
757 ''
758 );
759
760 $this->assertTrue(is_array($entryFound));
761
762 $retrievedData = $entryFound['content'];
763 $this->assertEquals($data, $retrievedData);
764 }
765 }
766
767 ?>