You are here

function BlockCacheAlter::testPatchedBlockModule in Block Cache Alter 6

Extra tests when the block module is patched.

File

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

Class

BlockCacheAlter
@file Tests for Block Cache Alter

Code

function testPatchedBlockModule() {
  $status = _blockcache_alter_core_patch();
  if (!empty($status)) {
    $this
      ->assertNotNull($status, t('No blockcache alter patch applied to block module, quitting rest of tests.'));
  }
  else {

    // Create and login user
    $admin_user = $this
      ->drupalCreateUser(array(
      'administer blocks',
      'administer nodes',
      'post comments',
      'administer comments',
    ));
    $this
      ->drupalLogin($admin_user);

    // Core patch applied, set the core patch variable and turn on block caching.
    variable_set('block_cache', CACHE_NORMAL);
    variable_set('bca_corepatch', TRUE);
    $this
      ->assertTrue(variable_get('bca_corepatch', 0), t('Blockcache alter patch applied, running extra tests functionality.'));

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

    ///////////////////////

    // Test node actions //

    ///////////////////////
    $edit['cache_block'] = BLOCK_CACHE_GLOBAL;
    $edit["bc_refresh[node]"] = 'checked';
    $edit["bc_relate[page]"] = 'checked';
    $this
      ->drupalPost('admin/build/block/configure/block/' . $block['delta'], $edit, t('Save block'));
    $cache1 = $this
      ->_getRecordFromCacheBlockTable($block['delta']);

    // Create a page node, cache should be different.
    sleep(1);
    $page = $this
      ->drupalCreateNode(array(
      'type' => 'page',
      'title' => 'page',
      'body' => 'blah',
    ));
    $this
      ->drupalGet('node/' . $page->nid);
    $cache2 = $this
      ->_getRecordFromCacheBlockTable($block['delta']);
    $this
      ->assertNotEqual($cache1->created, $cache2->created, t('Block cache updated for block 1'), t('Extra functionality: node.'));

    // create a story node, cache should be the same.
    sleep(1);
    $story = $this
      ->drupalCreateNode(array(
      'type' => 'story',
      'title' => 'story',
      'body' => 'blah',
    ));
    $this
      ->drupalGet('node/' . $story->nid);
    $cache3 = $this
      ->_getRecordFromCacheBlockTable($block['delta']);
    $this
      ->assertEqual($cache2->created, $cache3->created, t('Block cache not updated for block 1'), t('Extra functionality: node.'));

    // Create new user, we have to surf to another page, but cache should be the same.
    sleep(1);
    $new_user = $this
      ->drupalCreateUser(array(
      'administer site configuration',
    ));
    $this
      ->drupalGet('user');
    $cache4 = $this
      ->_getRecordFromCacheBlockTable($block['delta']);
    $this
      ->assertEqual($cache3->created, $cache4->created, t('Block cache not updated for block 1.'), t('Extra functionality: node.'));

    // Reset
    $edit["bc_refresh[node]"] = FALSE;
    $edit["bc_relate[page]"] = FALSE;

    ////////////////////////////

    // Test user edit actions //

    ////////////////////////////
    $edit["bc_refresh[user]"] = 'checked';
    $this
      ->drupalPost('admin/build/block/configure/block/' . $block['delta'], $edit, t('Save block'));
    $cache1 = $this
      ->_getRecordFromCacheBlockTable($block['delta']);
    sleep(2);

    // Create new user, we have to surf to another page so the block will be cached again.
    $new_user = $this
      ->drupalCreateUser(array(
      'administer site configuration',
    ));
    $this
      ->drupalGet('user');
    $cache2 = $this
      ->_getRecordFromCacheBlockTable($block['delta']);
    $this
      ->assertNotEqual($cache1->created, $cache2->created, t('Block cache updated for block 1.'), t('Extra functionality: user.'));
    sleep(2);

    // Update user, we have to surf to another page so the block will be cached again.
    user_save($new_user, array());
    $this
      ->drupalGet('user');
    $cache3 = $this
      ->_getRecordFromCacheBlockTable($block['delta']);
    $this
      ->assertNotEqual($cache2->created, $cache3->created, t('Block cache updated for block 1.'), t('Extra functionality: user.'));
    sleep(2);

    // Delete user, we have to surf to another page so the block will be cached again.
    user_delete(NULL, $new_user->uid);
    $this
      ->drupalGet('user');
    $cache4 = $this
      ->_getRecordFromCacheBlockTable($block['delta']);
    $this
      ->assertNotEqual($cache3->created, $cache4->created, t('Block cache updated for block 1.'), t('Extra functionality: user.'));

    // create a story node, cache should be the same.
    sleep(1);
    $story = $this
      ->drupalCreateNode(array(
      'type' => 'story',
      'title' => 'story',
      'body' => 'blah',
    ));
    $this
      ->drupalGet('node/' . $story->nid);
    $cache5 = $this
      ->_getRecordFromCacheBlockTable($block['delta']);
    $this
      ->assertEqual($cache4->created, $cache5->created, t('Block cache not updated for block 1'), t('Extra functionality: user.'));

    // Reset
    $edit["bc_refresh[user]"] = FALSE;

    //////////////////////////

    // Test comment actions //

    //////////////////////////
    $edit["bc_refresh[comment]"] = 'checked';
    $edit["bc_relate[story]"] = 'checked';
    $this
      ->drupalPost('admin/build/block/configure/block/' . $block['delta'], $edit, t('Save block'));
    $cache1 = $this
      ->_getRecordFromCacheBlockTable($block['delta']);

    // Create a story and a comment.
    $story = $this
      ->drupalCreateNode(array(
      'type' => 'story',
      'title' => 'story',
      'body' => 'blah',
    ));
    $comment['nid'] = $story->nid;
    $comment['uid'] = '0';
    $comment['cid'] = '';
    $comment['pid'] = '0';
    $comment['format'] = '2';
    $comment['subject'] = 'subject';
    $comment['comment'] = 'comment';
    comment_save($comment);
    sleep(1);
    $out = $this
      ->drupalGet('node/' . $story->nid);
    $cache2 = $this
      ->_getRecordFromCacheBlockTable($block['delta']);
    $this
      ->assertNotEqual($cache1->created, $cache2->created, t('Block cache updated for block 1'), t('Extra functionality: comment.'));

    // Reset
    $edit["bc_refresh[comment]"] = FALSE;
    $edit["bc_relate[story]"] = FALSE;

    ////////////////////////////////////////

    // Test user login and logout actions //

    ////////////////////////////////////////
    $edit["bc_refresh[login]"] = 'checked';
    $this
      ->drupalPost('admin/build/block/configure/block/' . $block['delta'], $edit, t('Save block'));
    $cache1 = $this
      ->_getRecordFromCacheBlockTable($block['delta']);

    // Logout user and cache should be different
    sleep(1);
    $this
      ->drupalLogout();
    $this
      ->drupalGet('user/login');
    $cache2 = $this
      ->_getRecordFromCacheBlockTable($block['delta']);
    $this
      ->assertNotEqual($cache1->created, $cache2->created, t('Block cache updated for block 1'), t('Extra functionality: login.'));

    // Login again and cache should be different
    sleep(1);
    $this
      ->drupalLogin($admin_user);
    $cache2 = $this
      ->_getRecordFromCacheBlockTable($block['delta']);
    $this
      ->assertNotEqual($cache1->created, $cache2->created, t('Block cache updated for block 1'), t('Extra functionality: login.'));

    // Create a page, cache should be the same
    sleep(1);
    $page = $this
      ->drupalCreateNode(array(
      'type' => 'page',
      'title' => 'page',
      'body' => 'blah',
    ));
    $this
      ->drupalGet('node/' . $page->nid);
    $cache3 = $this
      ->_getRecordFromCacheBlockTable($block['delta']);
    $this
      ->assertEqual($cache2->created, $cache3->created, t('Block cache not updated for block 1'), t('Extra functionality: login.'));

    ///////////////////////////////

    // Play with bc_life seconds //

    ///////////////////////////////
    $edit["bc_life"] = '5';
    $this
      ->drupalPost('admin/build/block/configure/block/' . $block['delta'], $edit, t('Save block'));
    $this
      ->drupalGet('user');
    $cache1 = $this
      ->_getRecordFromCacheBlockTable($block['delta']);

    // Sleep 3 seconds, visit user page, cache should be the same
    sleep(3);
    $this
      ->drupalGet('user');
    $cache2 = $this
      ->_getRecordFromCacheBlockTable($block['delta']);
    $this
      ->assertEqual($cache1->created, $cache2->created, t('Block cache not updated for block 1'), t('Extra functionality: lifetime.'));

    // Sleep 3 more seconds, revisit user page and cache should be changed.
    sleep(3);
    $this
      ->drupalGet('user');
    $cache3 = $this
      ->_getRecordFromCacheBlockTable($block['delta']);
    $this
      ->assertNotEqual($cache2->created, $cache3->created, t('Block cache updated for block 1'), t('Extra functionality: lifetime.'));
  }
}