TYPO3 CMS  TYPO3_6-2
Typo3DatabaseBackendTest.php
Go to the documentation of this file.
1 <?php
3 
22 
29  protected function setUpMockFrontendOfBackend(\TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend $backend) {
30  $mockCache = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend', array(), array(), '', FALSE);
31  $mockCache->expects($this->any())->method('getIdentifier')->will($this->returnValue('Testing'));
32  $backend->setCache($mockCache);
33  return $mockCache;
34  }
35 
39  public function setCacheCalculatesCacheTableName() {
41  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
43  $this->assertEquals('cf_Testing', $backend->getCacheTable());
44  }
45 
49  public function setCacheCalculatesTagsTableName() {
51  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
53  $this->assertEquals('cf_Testing_tags', $backend->getTagsTable());
54  }
55 
60  public function setThrowsExceptionIfFrontendWasNotSet() {
62  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
63  $backend->set('identifier', 'data');
64  }
65 
70  public function setThrowsExceptionIfDataIsNotAString() {
72  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
74  $data = array('Some data');
75  $entryIdentifier = 'BackendDbTest';
76  $backend->set($entryIdentifier, $data);
77  }
78 
82  public function setInsertsEntryInTable() {
84  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
86  $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
87  $GLOBALS['TYPO3_DB']
88  ->expects($this->once())
89  ->method('exec_INSERTquery')
90  ->with('cf_Testing', $this->callback(function (array $data) {
91  if ($data['content'] !== 'someData') {
92  return FALSE;
93  }
94  if ($data['identifier'] !== 'anIdentifier') {
95  return FALSE;
96  }
97  return TRUE;
98  }));
99  $backend->set('anIdentifier', 'someData');
100  }
101 
105  public function setRemovesAnAlreadyExistingCacheEntryForTheSameIdentifier() {
107  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('remove'), array('Testing'));
109  $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
110 
111  $backend->expects($this->once())->method('remove');
112  $data = $this->getUniqueId('someData');
113  $entryIdentifier = 'anIdentifier';
114  $backend->set($entryIdentifier, $data, array(), 500);
115  }
116 
120  public function setReallySavesSpecifiedTags() {
122  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
124  $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
125  $GLOBALS['TYPO3_DB']
126  ->expects($this->once())
127  ->method('exec_INSERTmultipleRows')
128  ->with(
129  'cf_Testing_tags',
130  $this->callback(function (array $data) {
131  if ($data[0] === 'identifier' && $data[1] === 'tag') {
132  return TRUE;
133  }
134  return FALSE;
135  }),
136  $this->callback(function (array $data) {
137  if ($data[0][0] !== 'anIdentifier' || $data[0][1] !== 'UnitTestTag%tag1') {
138  return FALSE;
139  }
140  if ($data[1][0] !== 'anIdentifier' || $data[1][1] !== 'UnitTestTag%tag2') {
141  return FALSE;
142  }
143  return TRUE;
144  })
145  );
146  $backend->set('anIdentifier', 'someData', array('UnitTestTag%tag1', 'UnitTestTag%tag2'));
147  }
148 
152  public function setSavesCompressedDataWithEnabledCompression() {
153  $backendOptions = array(
154  'compression' => TRUE
155  );
157  $backend = $this->getMock(
158  'TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend',
159  array('dummy'),
160  array('Testing', $backendOptions)
161  );
163 
164  $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
165  $GLOBALS['TYPO3_DB']
166  ->expects($this->once())
167  ->method('exec_INSERTquery')
168  ->with(
169  'cf_Testing',
170  $this->callback(function (array $data) {
171  if (@gzuncompress($data['content']) === 'someData') {
172  return TRUE;
173  }
174  return FALSE;
175  }
176  ));
177 
178  $backend->set('anIdentifier', 'someData');
179  }
180 
184  public function setWithUnlimitedLifetimeWritesCorrectEntry() {
186  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
188 
189  $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
190  $GLOBALS['TYPO3_DB']
191  ->expects($this->once())
192  ->method('exec_INSERTquery')
193  ->with(
194  'cf_Testing',
195  $this->callback(function (array $data) {
196  $lifetime = $data['expires'];
197  if ($lifetime > 2000000000) {
198  return TRUE;
199  }
200  return FALSE;
201  }
202  ));
203 
204  $backend->set('aIdentifier', 'someData', array(), 0);
205  }
206 
211  public function getThrowsExceptionIfFrontendWasNotSet() {
213  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
214  $backend->get('identifier');
215  }
216 
220  public function getReturnsContentOfTheCorrectCacheEntry() {
222  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
224 
225  $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
226  $GLOBALS['TYPO3_DB']
227  ->expects($this->once())
228  ->method('exec_SELECTgetSingleRow')
229  ->with('content', 'cf_Testing', $this->anything())
230  ->will($this->returnValue(array('content' => 'someData')));
231 
232  $loadedData = $backend->get('aIdentifier');
233  $this->assertEquals('someData', $loadedData);
234  }
235 
239  public function getSetsExceededLifetimeQueryPart() {
241  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
243 
244  $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
245  $GLOBALS['TYPO3_DB']
246  ->expects($this->once())
247  ->method('exec_SELECTgetSingleRow')
248  ->with(
249  'content',
250  'cf_Testing',
251  $this->stringContains('identifier = AND cf_Testing.expires >=')
252  );
253 
254  $backend->get('aIdentifier');
255  }
256 
261  public function hasThrowsExceptionIfFrontendWasNotSet() {
263  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
264  $backend->has('identifier');
265  }
266 
270  public function hasReturnsTrueForExistingEntry() {
272  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
274 
275  $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
276  $GLOBALS['TYPO3_DB']
277  ->expects($this->once())
278  ->method('exec_SELECTcountRows')
279  ->with('*', 'cf_Testing', $this->anything())
280  ->will($this->returnValue(1));
281 
282  $this->assertTrue($backend->has('aIdentifier'));
283  }
284 
288  public function hasSetsExceededLifetimeQueryPart() {
290  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
292 
293  $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
294  $GLOBALS['TYPO3_DB']
295  ->expects($this->once())
296  ->method('exec_SELECTcountRows')
297  ->with(
298  '*',
299  'cf_Testing',
300  $this->stringContains('identifier = AND cf_Testing.expires >='))
301  ->will($this->returnValue(1));
302 
303  $this->assertTrue($backend->has('aIdentifier'));
304  }
305 
310  public function removeThrowsExceptionIfFrontendWasNotSet() {
312  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
313  $backend->remove('identifier');
314  }
315 
320  public function collectGarbageThrowsExceptionIfFrontendWasNotSet() {
322  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
323  $backend->collectGarbage();
324  }
325 
329  public function collectGarbageDeletesTagsFromExpiredEntries() {
331  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
333 
334  $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
335  $GLOBALS['TYPO3_DB']
336  ->expects($this->at(1))
337  ->method('sql_fetch_assoc')
338  ->will($this->returnValue(array('identifier' => 'aIdentifier')));
339  $GLOBALS['TYPO3_DB']
340  ->expects($this->at(2))
341  ->method('fullQuoteStr')
342  ->will($this->returnValue('aIdentifier'));
343  $GLOBALS['TYPO3_DB']
344  ->expects($this->at(3))
345  ->method('sql_fetch_assoc')
346  ->will($this->returnValue(FALSE));
347  $GLOBALS['TYPO3_DB']
348  ->expects($this->at(5))
349  ->method('exec_DELETEquery')
350  ->with('cf_Testing_tags', 'identifier IN (aIdentifier)');
351 
352  $backend->collectGarbage();
353  }
354 
359  public function findIdentifiersByTagThrowsExceptionIfFrontendWasNotSet() {
361  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
362  $backend->findIdentifiersByTag('identifier');
363  }
364 
368  public function findIdentifiersByTagFindsCacheEntriesWithSpecifiedTag() {
370  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
372 
373  $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
374  $GLOBALS['TYPO3_DB']
375  ->expects($this->at(0))
376  ->method('fullQuoteStr')
377  ->will($this->returnValue('cf_Testing_tags'));
378  $GLOBALS['TYPO3_DB']
379  ->expects($this->at(1))
380  ->method('exec_SELECTgetRows')
381  ->with(
382  'cf_Testing.identifier',
383  'cf_Testing, cf_Testing_tags',
384  $this->stringContains('cf_Testing_tags.tag = cf_Testing_tags AND cf_Testing.identifier = cf_Testing_tags.identifier AND cf_Testing.expires >= '),
385  'cf_Testing.identifier'
386  )
387  ->will($this->returnValue(array(array('identifier' => 'aIdentifier'))));
388  $this->assertSame(array('aIdentifier' => 'aIdentifier'), $backend->findIdentifiersByTag('aTag'));
389  }
390 
395  public function flushThrowsExceptionIfFrontendWasNotSet() {
397  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
398  $backend->flush();
399  }
400 
404  public function flushRemovesAllCacheEntries() {
406  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
408 
409  $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
410  $GLOBALS['TYPO3_DB']
411  ->expects($this->at(0))
412  ->method('exec_TRUNCATEquery')
413  ->with('cf_Testing');
414  $GLOBALS['TYPO3_DB']
415  ->expects($this->at(1))
416  ->method('exec_TRUNCATEquery')
417  ->with('cf_Testing_tags');
418 
419  $backend->flush();
420  }
421 
426  public function flushByTagThrowsExceptionIfFrontendWasNotSet() {
428  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
429  $backend->flushByTag(array());
430  }
431 
435  public function flushByTagRemovesCacheEntriesWithSpecifiedTag() {
437  $backend = $this->getMock('TYPO3\\CMS\\Core\\Cache\\Backend\\Typo3DatabaseBackend', array('dummy'), array('Testing'));
439 
440  $GLOBALS['TYPO3_DB'] = $this->getMock('TYPO3\\CMS\\Core\\Database\\DatabaseConnection', array(), array(), '', FALSE);
441  $GLOBALS['TYPO3_DB']
442  ->expects($this->at(0))
443  ->method('fullQuoteStr')
444  ->will($this->returnValue('UnitTestTag%special'));
445  $GLOBALS['TYPO3_DB']
446  ->expects($this->at(1))
447  ->method('exec_SELECTquery')
448  ->with(
449  'DISTINCT identifier',
450  'cf_Testing_tags',
451  'cf_Testing_tags.tag = UnitTestTag%special'
452  );
453  $GLOBALS['TYPO3_DB']
454  ->expects($this->at(2))
455  ->method('sql_fetch_assoc')
456  ->will($this->returnValue(array('identifier' => 'BackendDbTest1')));
457  $GLOBALS['TYPO3_DB']
458  ->expects($this->at(3))
459  ->method('fullQuoteStr')
460  ->with('BackendDbTest1', 'cf_Testing')
461  ->will($this->returnValue('BackendDbTest1'));
462  $GLOBALS['TYPO3_DB']
463  ->expects($this->at(4))
464  ->method('sql_fetch_assoc')
465  ->will($this->returnValue(array('identifier' => 'BackendDbTest2')));
466  $GLOBALS['TYPO3_DB']
467  ->expects($this->at(5))
468  ->method('fullQuoteStr')
469  ->with('BackendDbTest2', 'cf_Testing')
470  ->will($this->returnValue('BackendDbTest2'));
471  $GLOBALS['TYPO3_DB']
472  ->expects($this->at(6))
473  ->method('sql_fetch_assoc')
474  ->will($this->returnValue(FALSE));
475  $GLOBALS['TYPO3_DB']
476  ->expects($this->at(7))
477  ->method('sql_free_result')
478  ->will($this->returnValue(TRUE));
479  $GLOBALS['TYPO3_DB']
480  ->expects($this->at(8))
481  ->method('exec_DELETEquery')
482  ->with('cf_Testing', 'identifier IN (BackendDbTest1, BackendDbTest2)');
483  $GLOBALS['TYPO3_DB']
484  ->expects($this->at(9))
485  ->method('exec_DELETEquery')
486  ->with('cf_Testing_tags', 'identifier IN (BackendDbTest1, BackendDbTest2)');
487 
488  $backend->flushByTag('UnitTestTag%special');
489  }
490 }
setUpMockFrontendOfBackend(\TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend $backend)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]