You are here

function BlockCacheAlter::testSimpleCacheRefreshments in Block Cache Alter 6

Test simple cache changes with clear cache option (only block or page).

File

./blockcache_alter.test, line 122
Tests for Block Cache Alter

Class

BlockCacheAlter
@file Tests for Block Cache Alter

Code

function testSimpleCacheRefreshments() {
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer blocks',
    'administer nodes',
    'post comments',
    'administer comments',
  ));
  $this
    ->drupalLogin($admin_user);

  // Turn on block caching.
  variable_set('block_cache', CACHE_NORMAL);

  // Create two blocks and assign them to regions.
  $block1 = $this
    ->_createTestBlock();
  $block2 = $this
    ->_createTestBlock();
  db_query("UPDATE {blocks} set status = 1, region = 'left' WHERE module = 'block' AND delta = '%s'", $block1['delta']);
  db_query("UPDATE {blocks} set status = 1, region = 'left' WHERE module = 'block' AND delta = '%s'", $block2['delta']);
  $this
    ->assertEqual($this
    ->_checkBlockSetting($block1['delta'], 'region'), 'left', 'Region is set to left for block 1', t('Cache refreshment change.'));
  $this
    ->assertEqual($this
    ->_checkBlockSetting($block2['delta'], 'region'), 'left', 'Region is set to left for block 2', t('Cache refreshment change.'));

  // Let's change their caching to global.
  $edit['cache_block'] = BLOCK_CACHE_GLOBAL;

  // Set a bc_life if core_patch is applied so cache_clear_all
  // will do it's job in the tests after this.
  $edit2 = $edit;
  $status = _blockcache_alter_core_patch();
  if (empty($status)) {
    variable_set('bca_corepatch', TRUE);
    $edit2['bc_life'] = 4;
    $this
      ->drupalPost('admin/build/block/configure/block/' . $block2['delta'], $edit2, t('Save block'));
  }
  $this
    ->drupalPost('admin/build/block/configure/block/' . $block1['delta'], $edit, t('Save block'));
  $this
    ->assertEqual($this
    ->_checkBlockSetting($block1['delta']), BLOCK_CACHE_GLOBAL, t('Cache set to global.'), t('Cache refreshment change.'));

  // Add lifetime if core patch is applied, expire time is checked
  // in block module and everything will be borked.
  $this
    ->drupalPost('admin/build/block/configure/block/' . $block2['delta'], $edit, t('Save block'));
  $this
    ->assertEqual($this
    ->_checkBlockSetting($block2['delta']), BLOCK_CACHE_GLOBAL, t('Cache set to global.'), t('Cache refreshment change.'));

  // We should now have 2 cached blocks in cache_block table.
  $cache1 = $this
    ->_getRecordFromCacheBlockTable($block1['delta']);
  $cache2 = $this
    ->_getRecordFromCacheBlockTable($block2['delta']);
  $this
    ->assertEqual($cache1->cid, 'block:1:garland', t('Cached block 1 found.'), t('Cache refreshment change.'));
  $this
    ->assertEqual($cache2->cid, 'block:2:garland', t('Cached block 2 found.'), t('Cache refreshment change.'));

  // Sleep one second, because otherwhise created
  // timestamp will be the same as this goes superfast
  sleep(1);

  // Let's clear the cache for block 1, cache for block 2 should stay the same
  // which we can test with the value in the created field.
  $edit['cache_block_clear'] = '1';
  $this
    ->drupalPost('admin/build/block/configure/block/' . $block1['delta'], $edit2, t('Save block'));
  $cache3 = $this
    ->_getRecordFromCacheBlockTable($block1['delta']);
  $cache4 = $this
    ->_getRecordFromCacheBlockTable($block2['delta']);
  $this
    ->assertNotEqual($cache1->created, $cache3->created, t('Block cache updated for block 1'), t('Cache refreshment change.'));
  $this
    ->assertEqual($cache2->created, $cache4->created, t('Block cache not updated for block 2'), t('Cache refreshment change.'));

  // Sleep four seconds, because otherwhise created
  // timestamp will be the same as this goes superfast
  sleep(4);

  // Clear cache again, but now all.
  $edit['cache_block_clear'] = '2';
  $this
    ->drupalPost('admin/build/block/configure/block/' . $block1['delta'], $edit, t('Save block'));
  $cache5 = $this
    ->_getRecordFromCacheBlockTable($block1['delta']);
  $cache6 = $this
    ->_getRecordFromCacheBlockTable($block2['delta']);
  $this
    ->assertNotEqual($cache3->created, $cache5->created, t('Block cache updated for block 1'), t('Cache refreshment change.'));
  $this
    ->assertNotEqual($cache4->created, $cache6->created, t('Block cache updated for block 2'), t('Cache refreshment change.'));
}