You are here

public function AuthcacheBuiltinTestCacheBackend::testKeyExpiry in Authenticated User Page Caching (Authcache) 7.2

Verify that the key cache is set with the proper values.

File

modules/authcache_builtin/tests/authcache_builtin.cache-backend.test, line 429
Test cases for the Authcache Bultin Cache Backend module.

Class

AuthcacheBuiltinTestCacheBackend
Tests update functionality unrelated to the database.

Code

public function testKeyExpiry() {
  global $base_root;
  variable_set('authcache_key_lifetime', 3600);

  // Login and retrieve authcache key.
  $this
    ->drupalLogin($this->member);
  $key_cache_cid = $base_root . ':' . $this->session_id;
  $user_key = $this
    ->drupalGetHeader('X-Authcache-Builtin-Test-Key');
  $this
    ->assertTrue($user_key, 'User key is not empty');
  $this
    ->assertNotEqual(authcache_backend_anonymous_key(), $user_key, 'User key is not same as the anonymous key');

  // Issue a normal page request and ensure that the key cache was populated.
  $now = time();
  $this
    ->drupalGet($this->fcURL . '?q=node', array(), $this
    ->buildRequestHeaders(0x1));
  $cache = cache_get($key_cache_cid, 'cache_authcache_key');
  $this
    ->assertEqual($user_key, $cache->data);
  $this
    ->assertTrue($cache->expire >= $now - 3600 * 0.1);
  $this
    ->assertTrue($cache->expire <= $now + 3600 * 1.1);

  // Logout and ensure that the key cache is empty.
  $this
    ->drupalGet($this->fcURL . '?q=user/logout', array(), $this
    ->buildRequestHeaders(0x1));
  $cache = cache_get($key_cache_cid, 'cache_authcache_key');
  $this
    ->assertFalse($cache);

  // Login again with member and set cache expiry date into the past.
  $this
    ->drupalLogin($this->member);
  $key_cache_cid = $base_root . ':' . $this->session_id;
  cache_set($key_cache_cid, $user_key, 'cache_authcache_key', REQUEST_TIME - 3600);

  // Issue a normal page request and ensure that the cache has been renewed.
  $now = time();
  $this
    ->drupalGet($this->fcURL . '?q=node', array(), $this
    ->buildRequestHeaders(0x1));
  $cache = cache_get($key_cache_cid, 'cache_authcache_key');
  $this
    ->assertEqual($user_key, $cache->data);
  $this
    ->assertTrue($cache->expire >= $now - 3600 * 0.1);
  $this
    ->assertTrue($cache->expire <= $now + 3600 * 1.1);

  // Logout and ensure that the key cache is empty.
  $this
    ->drupalGet($this->fcURL . '?q=user/logout', array(), $this
    ->buildRequestHeaders(0x1));
  $cache = cache_get($key_cache_cid, 'cache_authcache_key');
  $this
    ->assertFalse($cache);

  // Login again with member and set a wrong authcache key but with proper
  // expiry date.
  $this
    ->drupalLogin($this->member);
  $key_cache_cid = $base_root . ':' . $this->session_id;
  cache_set($key_cache_cid, $this
    ->randomName(4), 'cache_authcache_key', REQUEST_TIME + 3600);

  // Issue a normal page request and ensure that the cache has been renewed.
  $now = time();
  $this
    ->drupalGet($this->fcURL . '?q=node', array(), $this
    ->buildRequestHeaders(0x1));
  $cache = cache_get($key_cache_cid, 'cache_authcache_key');
  $this
    ->assertEqual($user_key, $cache->data);
  $this
    ->assertTrue($cache->expire >= $now - 3600 * 0.1);
  $this
    ->assertTrue($cache->expire <= $now + 3600 * 1.1);

  // Logout and ensure that the key cache is empty.
  $this
    ->drupalGet($this->fcURL . '?q=user/logout', array(), $this
    ->buildRequestHeaders(0x1));
  $cache = cache_get($key_cache_cid, 'cache_authcache_key');
  $this
    ->assertFalse($cache);
}