[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/blockcache_alter/ -> blockcache_alter.test (source)

   1  <?php
   2  // $Id: blockcache_alter.test,v 1.1.2.11 2009/03/31 18:32:58 swentel Exp $
   3  
   4  /**
   5   * @file
   6   * Tests for Block Cache Alter
   7   */
   8  
   9  class BlockCacheAlter extends DrupalWebTestCase {
  10    /**
  11     * Implementation of getInfo().
  12     */
  13    function getInfo() {
  14      return array(
  15        'name' => t('Caching functionality'),
  16        'description' => t('Test block cache alter functionality. The tests will check if one of the pathes is applied too to test further functionality.'),
  17        'group' => t('Block Cache Alter'),
  18      );
  19    }
  20  
  21    /**
  22     * Implementation of setUp().
  23     */
  24    function setUp() {
  25      parent::setUp('blockcache_alter');
  26    }
  27  
  28    /**
  29     * Debug helper function. Writes values away to a text file in the files directory.
  30     */
  31    function _debugHelper($value, $writetype = 'a+') {
  32      $debug = fopen($this->originalFileDirectory .'/simpletestdebug.txt', 'a+');
  33      fwrite($debug, print_r($value, TRUE) ."\n");
  34      fclose($debug);
  35    }
  36  
  37    /**
  38     * Helper function to create a test block.
  39     * @return array $block All properties of testblock.
  40     */
  41    function _createTestBlock() {
  42      // Add a new box by filling out the input form on the admin/build/block/add page.
  43      $box = array();
  44      $box['info'] = $this->randomName(8);
  45      $box['title'] = $this->randomName(8);
  46      $box['body'] = $this->randomName(32);
  47      $this->drupalPost('admin/build/block/add', $box, t('Save block'));
  48  
  49      // Confirm that the box has been created, and then query the created bid.
  50      $this->assertText(t('The block has been created.'), t('Box successfully created.'));
  51      $bid = db_result(db_query("SELECT bid FROM {boxes} WHERE info = '%s'", array($box['info'])));
  52      $delta = db_result(db_query("SELECT bid FROM {boxes} WHERE bid = %d", $bid));
  53  
  54      // Check to see if the box was created by checking that it's in the database..
  55      $this->assertNotNull($bid, t('Box found in database'));
  56  
  57      $box['bid'] = $bid;
  58      $box['delta'] = $delta;
  59      return $box;
  60    }
  61  
  62    /**
  63     * Helper function to check a setting of a block directly from database.
  64     *
  65     * @param int $delta The delta of the block.
  66     * @param string $field The name of the field to retrieve.
  67     * @return int $cache The setting of the cache.
  68     */
  69    function _checkBlockSetting($delta, $field = 'cache') {
  70      $setting = db_result(db_query("SELECT $field FROM {blocks} WHERE module = 'block' AND delta = '%s'", $delta));
  71      return $setting;
  72    }
  73  
  74    /**
  75     * Helper function to get a record from cache_block table.
  76     *
  77     * @param int $delta The delta of the block.
  78     * @return stdClass $cache A complete cache object.
  79     */
  80    function _getRecordFromCacheBlockTable($delta) {
  81      $cache = db_fetch_object(db_query("SELECT * FROM {cache_block} WHERE cid = '%s'", 'block:'. $delta .':garland'));
  82      return $cache;
  83    }
  84  
  85    /**
  86     * Helper function to return all block cache options.
  87     * @return array $block_cache_options.
  88     */
  89    function _returnBlockCacheOptions() {
  90        $block_cache_options = array(
  91        BLOCK_CACHE_GLOBAL => t('Cache once for everything (global)'),
  92        BLOCK_CACHE_PER_PAGE => t('Per page'),
  93        BLOCK_CACHE_PER_ROLE => t('Per role'),
  94        BLOCK_CACHE_PER_ROLE | BLOCK_CACHE_PER_PAGE => t('Per role per page'),
  95        BLOCK_CACHE_PER_USER => t('Per user'),
  96        BLOCK_CACHE_PER_USER | BLOCK_CACHE_PER_PAGE => t('Per user per page'),
  97        BLOCK_NO_CACHE => t('Do not cache'),
  98      );
  99      return $block_cache_options;
 100    }
 101  
 102    /**
 103     * Test simple cache changes.
 104     */
 105    function testSimpleCacheChanges() {
 106      $admin_user = $this->drupalCreateUser(array('administer blocks', 'administer nodes', 'post comments', 'administer comments'));
 107      $this->drupalLogin($admin_user);
 108  
 109      $edit = array();
 110      $block = $this->_createTestBlock();
 111  
 112      foreach ($this->_returnBlockCacheOptions() as $key => $value) {
 113        $edit['cache_block'] = $key;
 114        $this->drupalPost('admin/build/block/configure/block/'. $block['delta'], $edit, t('Save block'));
 115        $this->assertText(t('The block configuration has been saved.'), t('Block successfully updated.'), t('Simple cache change.'));
 116        $this->assertEqual($this->_checkBlockSetting($block['delta']), $key, $value, t('Simple cache change.'));
 117      }
 118    }
 119  
 120    /**
 121     * Test simple cache changes with clear cache option (only block or page).
 122     */
 123    function testSimpleCacheRefreshments() {
 124      $admin_user = $this->drupalCreateUser(array('administer blocks', 'administer nodes', 'post comments', 'administer comments'));
 125      $this->drupalLogin($admin_user);
 126  
 127      // Turn on block caching.
 128      variable_set('block_cache', CACHE_NORMAL);
 129  
 130      // Create two blocks and assign them to regions.
 131      $block1 = $this->_createTestBlock();
 132      $block2 = $this->_createTestBlock();
 133      db_query("UPDATE {blocks} set status = 1, region = 'left' WHERE module = 'block' AND delta = '%s'", $block1['delta']);
 134      db_query("UPDATE {blocks} set status = 1, region = 'left' WHERE module = 'block' AND delta = '%s'", $block2['delta']);
 135      $this->assertEqual($this->_checkBlockSetting($block1['delta'], 'region'), 'left', 'Region is set to left for block 1', t('Cache refreshment change.'));
 136      $this->assertEqual($this->_checkBlockSetting($block2['delta'], 'region'), 'left', 'Region is set to left for block 2', t('Cache refreshment change.'));
 137  
 138      // Let's change their caching to global.
 139      $edit['cache_block'] = BLOCK_CACHE_GLOBAL;
 140  
 141      // Set a bc_life if core_patch is applied so cache_clear_all
 142      // will do it's job in the tests after this.
 143      $edit2 = $edit;
 144      $status = _blockcache_alter_core_patch();
 145      if (empty($status)) {
 146        variable_set('bca_corepatch', TRUE);
 147        $edit2['bc_life'] = 4;
 148        $this->drupalPost('admin/build/block/configure/block/'. $block2['delta'], $edit2, t('Save block'));
 149      }
 150  
 151      $this->drupalPost('admin/build/block/configure/block/'. $block1['delta'], $edit, t('Save block'));
 152      $this->assertEqual($this->_checkBlockSetting($block1['delta']), BLOCK_CACHE_GLOBAL, t('Cache set to global.'), t('Cache refreshment change.'));
 153      // Add lifetime if core patch is applied, expire time is checked
 154      // in block module and everything will be borked.
 155      $this->drupalPost('admin/build/block/configure/block/'. $block2['delta'], $edit, t('Save block'));
 156      $this->assertEqual($this->_checkBlockSetting($block2['delta']), BLOCK_CACHE_GLOBAL, t('Cache set to global.'), t('Cache refreshment change.'));
 157  
 158      // We should now have 2 cached blocks in cache_block table.
 159      $cache1 = $this->_getRecordFromCacheBlockTable($block1['delta']);
 160      $cache2 = $this->_getRecordFromCacheBlockTable($block2['delta']);
 161      $this->assertEqual($cache1->cid, 'block:1:garland', t('Cached block 1 found.'), t('Cache refreshment change.'));
 162      $this->assertEqual($cache2->cid, 'block:2:garland', t('Cached block 2 found.'), t('Cache refreshment change.'));
 163  
 164      // Sleep one second, because otherwhise created
 165      // timestamp will be the same as this goes superfast
 166      sleep(1);
 167  
 168      // Let's clear the cache for block 1, cache for block 2 should stay the same
 169      // which we can test with the value in the created field.
 170      $edit['cache_block_clear'] = '1';
 171      $this->drupalPost('admin/build/block/configure/block/'. $block1['delta'], $edit2, t('Save block'));
 172      $cache3 = $this->_getRecordFromCacheBlockTable($block1['delta']);
 173      $cache4 = $this->_getRecordFromCacheBlockTable($block2['delta']);
 174      $this->assertNotEqual($cache1->created, $cache3->created, t('Block cache updated for block 1'), t('Cache refreshment change.'));
 175      $this->assertEqual($cache2->created, $cache4->created, t('Block cache not updated for block 2'), t('Cache refreshment change.'));
 176  
 177      // Sleep four seconds, because otherwhise created
 178      // timestamp will be the same as this goes superfast
 179      sleep(4);
 180  
 181      // Clear cache again, but now all.
 182      $edit['cache_block_clear'] = '2';
 183      $this->drupalPost('admin/build/block/configure/block/'. $block1['delta'], $edit, t('Save block'));
 184      $cache5 = $this->_getRecordFromCacheBlockTable($block1['delta']);
 185      $cache6 = $this->_getRecordFromCacheBlockTable($block2['delta']);
 186      $this->assertNotEqual($cache3->created, $cache5->created, t('Block cache updated for block 1'), t('Cache refreshment change.'));
 187      $this->assertNotEqual($cache4->created, $cache6->created, t('Block cache updated for block 2'), t('Cache refreshment change.'));
 188  
 189    }
 190  
 191    /**
 192     * Extra tests when the block module is patched.
 193     */
 194    function testPatchedBlockModule() {
 195  
 196      $status = _blockcache_alter_core_patch();
 197      if (!empty($status)) {
 198        $this->assertNotNull($status, t('No blockcache alter patch applied to block module, quitting rest of tests.'));
 199      }
 200      else {
 201        // Create and login user
 202        $admin_user = $this->drupalCreateUser(array('administer blocks', 'administer nodes', 'post comments', 'administer comments'));
 203        $this->drupalLogin($admin_user);
 204  
 205        // Core patch applied, set the core patch variable and turn on block caching.
 206        variable_set('block_cache', CACHE_NORMAL);
 207        variable_set('bca_corepatch', TRUE);
 208        $this->assertTrue(variable_get('bca_corepatch', 0), t('Blockcache alter patch applied, running extra tests functionality.'));
 209  
 210        // Create two blocks and assign them to regions.
 211        $block = $this->_createTestBlock();
 212        db_query("UPDATE {blocks} set status = 1, region = 'left' WHERE module = 'block' AND delta = '%s'", $block['delta']);
 213        $this->assertEqual($this->_checkBlockSetting($block['delta'], 'region'), 'left', 'Region is set to left for block 1', t('Extra functionality.'));
 214  
 215        ///////////////////////
 216        // Test node actions //
 217        ///////////////////////
 218        $edit['cache_block'] = BLOCK_CACHE_GLOBAL;
 219        $edit["bc_refresh[node]"] = 'checked';
 220        $edit["bc_relate[page]"] = 'checked';
 221        $this->drupalPost('admin/build/block/configure/block/'. $block['delta'], $edit, t('Save block'));
 222        $cache1 = $this->_getRecordFromCacheBlockTable($block['delta']);
 223  
 224        // Create a page node, cache should be different.
 225        sleep(1);
 226        $page = $this->drupalCreateNode(array('type' => 'page', 'title' => 'page', 'body' => 'blah'));
 227        $this->drupalGet('node/'. $page->nid);
 228        $cache2 = $this->_getRecordFromCacheBlockTable($block['delta']);
 229        $this->assertNotEqual($cache1->created, $cache2->created, t('Block cache updated for block 1'), t('Extra functionality: node.'));
 230  
 231        // create a story node, cache should be the same.
 232        sleep(1);
 233        $story = $this->drupalCreateNode(array('type' => 'story', 'title' => 'story', 'body' => 'blah'));
 234        $this->drupalGet('node/'. $story->nid);
 235        $cache3 = $this->_getRecordFromCacheBlockTable($block['delta']);
 236        $this->assertEqual($cache2->created, $cache3->created, t('Block cache not updated for block 1'), t('Extra functionality: node.'));
 237  
 238        // Create new user, we have to surf to another page, but cache should be the same.
 239        sleep(1);
 240        $new_user = $this->drupalCreateUser(array('administer site configuration'));
 241        $this->drupalGet('user');
 242        $cache4 = $this->_getRecordFromCacheBlockTable($block['delta']);
 243        $this->assertEqual($cache3->created, $cache4->created, t('Block cache not updated for block 1.'), t('Extra functionality: node.'));
 244  
 245        // Reset
 246        $edit["bc_refresh[node]"] = FALSE;
 247        $edit["bc_relate[page]"] = FALSE;
 248  
 249        ////////////////////////////
 250        // Test user edit actions //
 251        ////////////////////////////
 252        $edit["bc_refresh[user]"] = 'checked';
 253        $this->drupalPost('admin/build/block/configure/block/'. $block['delta'], $edit, t('Save block'));
 254        $cache1 = $this->_getRecordFromCacheBlockTable($block['delta']);
 255  
 256        sleep(2);
 257  
 258        // Create new user, we have to surf to another page so the block will be cached again.
 259        $new_user = $this->drupalCreateUser(array('administer site configuration'));
 260        $this->drupalGet('user');
 261        $cache2 = $this->_getRecordFromCacheBlockTable($block['delta']);
 262        $this->assertNotEqual($cache1->created, $cache2->created, t('Block cache updated for block 1.'), t('Extra functionality: user.'));
 263  
 264        sleep(2);
 265  
 266        // Update user, we have to surf to another page so the block will be cached again.
 267        user_save($new_user, array());
 268        $this->drupalGet('user');
 269        $cache3 = $this->_getRecordFromCacheBlockTable($block['delta']);
 270        $this->assertNotEqual($cache2->created, $cache3->created, t('Block cache updated for block 1.'), t('Extra functionality: user.'));
 271  
 272        sleep(2);
 273  
 274        // Delete user, we have to surf to another page so the block will be cached again.
 275        user_delete(NULL, $new_user->uid);
 276        $this->drupalGet('user');
 277        $cache4 = $this->_getRecordFromCacheBlockTable($block['delta']);
 278        $this->assertNotEqual($cache3->created, $cache4->created, t('Block cache updated for block 1.'), t('Extra functionality: user.'));
 279  
 280        // create a story node, cache should be the same.
 281        sleep(1);
 282        $story = $this->drupalCreateNode(array('type' => 'story', 'title' => 'story', 'body' => 'blah'));
 283        $this->drupalGet('node/'. $story->nid);
 284        $cache5 = $this->_getRecordFromCacheBlockTable($block['delta']);
 285        $this->assertEqual($cache4->created, $cache5->created, t('Block cache not updated for block 1'), t('Extra functionality: user.'));
 286  
 287        // Reset
 288        $edit["bc_refresh[user]"] = FALSE;
 289  
 290        //////////////////////////
 291        // Test comment actions //
 292        //////////////////////////
 293        $edit["bc_refresh[comment]"] = 'checked';
 294        $edit["bc_relate[story]"] = 'checked';
 295        $this->drupalPost('admin/build/block/configure/block/'. $block['delta'], $edit, t('Save block'));
 296        $cache1 = $this->_getRecordFromCacheBlockTable($block['delta']);
 297  
 298        // Create a story and a comment.
 299        $story = $this->drupalCreateNode(array('type' => 'story', 'title' => 'story', 'body' => 'blah'));
 300        $comment['nid'] = $story->nid;
 301        $comment['uid'] = '0';
 302        $comment['cid'] = '';
 303        $comment['pid'] = '0';
 304        $comment['format'] = '2';
 305        $comment['subject'] = 'subject';
 306        $comment['comment'] = 'comment';
 307        comment_save($comment);
 308  
 309        sleep(1);
 310  
 311        $out = $this->drupalGet('node/'. $story->nid);
 312        $cache2 = $this->_getRecordFromCacheBlockTable($block['delta']);
 313        $this->assertNotEqual($cache1->created, $cache2->created, t('Block cache updated for block 1'), t('Extra functionality: comment.'));
 314  
 315        // Reset
 316        $edit["bc_refresh[comment]"] = FALSE;
 317        $edit["bc_relate[story]"] = FALSE;
 318  
 319        ////////////////////////////////////////
 320        // Test user login and logout actions //
 321        ////////////////////////////////////////
 322        $edit["bc_refresh[login]"] = 'checked';
 323        $this->drupalPost('admin/build/block/configure/block/'. $block['delta'], $edit, t('Save block'));
 324        $cache1 = $this->_getRecordFromCacheBlockTable($block['delta']);
 325  
 326        // Logout user and cache should be different
 327        sleep(1);
 328        $this->drupalLogout();
 329        $this->drupalGet('user/login');
 330        $cache2 = $this->_getRecordFromCacheBlockTable($block['delta']);
 331        $this->assertNotEqual($cache1->created, $cache2->created, t('Block cache updated for block 1'), t('Extra functionality: login.'));
 332  
 333        // Login again and cache should be different
 334        sleep(1);
 335        $this->drupalLogin($admin_user);
 336        $cache2 = $this->_getRecordFromCacheBlockTable($block['delta']);
 337        $this->assertNotEqual($cache1->created, $cache2->created, t('Block cache updated for block 1'), t('Extra functionality: login.'));
 338  
 339        // Create a page, cache should be the same
 340        sleep(1);
 341        $page = $this->drupalCreateNode(array('type' => 'page', 'title' => 'page', 'body' => 'blah'));
 342        $this->drupalGet('node/'. $page->nid);
 343        $cache3 = $this->_getRecordFromCacheBlockTable($block['delta']);
 344        $this->assertEqual($cache2->created, $cache3->created, t('Block cache not updated for block 1'), t('Extra functionality: login.'));
 345  
 346        ///////////////////////////////
 347        // Play with bc_life seconds //
 348        ///////////////////////////////
 349        $edit["bc_life"] = '5';
 350        $this->drupalPost('admin/build/block/configure/block/'. $block['delta'], $edit, t('Save block'));
 351        $this->drupalGet('user');
 352        $cache1 = $this->_getRecordFromCacheBlockTable($block['delta']);
 353  
 354        // Sleep 3 seconds, visit user page, cache should be the same
 355        sleep(3);
 356        $this->drupalGet('user');
 357        $cache2 = $this->_getRecordFromCacheBlockTable($block['delta']);
 358        $this->assertEqual($cache1->created, $cache2->created, t('Block cache not updated for block 1'), t('Extra functionality: lifetime.'));
 359  
 360  
 361        // Sleep 3 more seconds, revisit user page and cache should be changed.
 362        sleep(3);
 363        $this->drupalGet('user');
 364        $cache3 = $this->_getRecordFromCacheBlockTable($block['delta']);
 365        $this->assertNotEqual($cache2->created, $cache3->created, t('Block cache updated for block 1'), t('Extra functionality: lifetime.'));
 366  
 367      }
 368    }
 369  
 370  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7