You are here

function MemCacheClearCase::testClearCacheLifetime in Zircon Profile 8

Same name in this branch
  1. 8 modules/memcache/tests/memcache.test \MemCacheClearCase::testClearCacheLifetime()
  2. 8 modules/memcache/tests/memcache6.test \MemCacheClearCase::testClearCacheLifetime()
Same name and namespace in other branches
  1. 8.0 modules/memcache/tests/memcache.test \MemCacheClearCase::testClearCacheLifetime()
  2. 8.0 modules/memcache/tests/memcache6.test \MemCacheClearCase::testClearCacheLifetime()

Test full bin flushes with cache lifetime.

File

modules/memcache/tests/memcache.test, line 302

Class

MemCacheClearCase
Test cache clearing methods.

Code

function testClearCacheLifetime() {
  variable_set('cache_lifetime', 600);
  $this
    ->resetVariables();

  // Set a cache item with an expiry.
  cache_set('test_cid', $this->default_value, $this->default_bin, time() + 3600);
  $this
    ->assertTrue($this
    ->checkCacheExists('test_cid', $this->default_value), 'Cache item was created successfully.');

  // Set a permanent cache item.
  cache_set('test_cid_2', $this->default_value, $this->default_bin);

  // Clear the page and block caches.
  cache_clear_all(MEMCACHE_CONTENT_CLEAR, $this->default_bin);

  // Since the cache was cleared within the current session, cache_get()
  // should return false.
  $this
    ->assertFalse($this
    ->checkCacheExists('test_cid', $this->default_value), 'Cache item was cleared successfully.');

  // However permament items should stay in place.
  $this
    ->assertTrue($this
    ->checkCacheExists('test_cid_2', $this->default_value), 'Cache item was not cleared');

  // If $_SESSION['cache_flush'] is not set, then the expired item should be returned.
  unset($_SESSION['cache_flush']);
  $this
    ->assertTrue($this
    ->checkCacheExists('test_cid', $this->default_value), 'Cache item is still returned due to minimum cache lifetime.');

  // Set a much shorter cache lifetime.
  variable_set('cache_content_flush_' . $this->default_bin, 0);
  variable_set('cache_lifetime', 1);
  cache_set('test_cid_1', $this->default_value, $this->default_bin, time() + 6000);
  $this
    ->assertTrue($this
    ->checkCacheExists('test_cid', $this->default_value), 'Cache item was created successfully.');
  sleep(2);
  cache_clear_all(MEMCACHE_CONTENT_CLEAR, $this->default_bin);
  $this
    ->assertFalse($this
    ->checkCacheExists('test_cid', $this->default_value), 'Cache item is not returned once minimum cache lifetime has expired.');

  // Reset the cache clear variables.
  variable_set('cache_content_flush_' . $this->default_bin, 0);
  variable_set('cache_lifetime', 6000);
  $this
    ->resetVariables();
  sleep(1);

  // Confirm that cache_lifetime does not take effect for full bin flushes.
  cache_set('test_cid', $this->default_value, $this->default_bin, time() + 6000);
  $this
    ->assertTrue($this
    ->checkCacheExists('test_cid', $this->default_value), 'Cache item was created successfully.');
  cache_set('test_cid_2', $this->default_value, $this->default_bin);
  $this
    ->assertTrue($this
    ->checkCacheExists('test_cid_2', $this->default_value), 'Cache item was created successfully.');

  // Now flush the bin.
  cache_clear_all('*', $this->default_bin, TRUE);
  $this
    ->assertFalse($this
    ->checkCacheExists('test_cid', $this->default_value), 'Cache item was cleared successfully.');
  $this
    ->assertFalse($this
    ->checkCacheExists('test_cid_2', $this->default_value), 'Cache item was cleared successfully.');
}