simpletest

Block cache alter just started rocking!

A few months ago I released block cache alter which did nothing more than adding a fieldset on the block configuration page enabling you to change the cache setting of a block. A small first step making block cache rock a bit more in Drupal 6. I wasn't sure how to take it a step further, but I'm happy I'm not alone on this case.

With a tremendous patch submitted by batbug2 (see #354080), the module got much smarter. You are now able to set expire values for each block or have it refreshed on certain actions (nodeapi, comment and user). In fact, a lot of the patch by batbug came from the excellent Blockcache module which never got ported to D6. After some thinking I decided to add the patch into the module because in the end, they really have some differences:

  • You need to apply a patch to the block module if you want to have all possibilities. 2 patches are available, take a look at the documentation that comes with the download which one suits you best. Note: you can still simply run without a patch, you simply won't have that much control when the cache is cleared.
  • If you choose to refresh on say a node action (insert, update, delete), you can also specify which node type.
  • 206 passes, 0 fails, and 0 exceptions. That's right, it's fully tested, if that ain't nice ..

If you're interested, please help out testing. The development release is available at http://drupal.org/node/395422. The sooner I get feedback - good or bad, post comments and follow up patches at #354080 - the faster I can release a new version and start writing patches to fix block caching for good in D7!

Never be to sure without Simpletest

It took me some time to start playing around with simpletest in Drupal which really is a shame. A few hours ago, I decided to start digging into it trying to write my first tests. I'm a real happy man now, because 4 lines failed on me testing a simple validation function I wrote. At first, I thought my test code was wrong, but looking at my validation function again made me realize I had a logical flaw in this code. This was my original validation function:

<?php
// formelement will be empty when called from outside a form validation
function _cjp_validate_token($string, $formelement = '') {
  if (!empty(
$string)) {
   
$token = db_fetch_object(db_query("SELECT cjp_tokens_token_expire FROM {cjp_tokens_token} WHERE cjp_tokens_token_string = '%s'", $string));
    if (
$token == false && !empty($formelement)) {
     
form_set_error($formelement, t('Je toegangscode is niet geldig.'));
    }
    elseif (
$token != false && !empty($formelement) && $token->cjp_tokens_token_expire < time()) {
     
form_set_error($formelement, t('Je toegangscode is vervallen.'));
    }
    elseif (
$token == false && empty($formelement)) {
      return
false;
    }
    elseif (
$token != false && empty($formelement)) {
      return
true;
    }
  }
}
?>

The simple test I wrote was testing if an existing token wasn't expired yet:

<?php
function testInvalidToken() {
 
$token = _cjp_generate_token("SimpleTest", 1, time()-3600, 1, true);
 
$result = _cjp_validate_token($token);
 
$this->assertFalse($result, 'Token is expired');
 
db_query("DELETE FROM {cjp_tokens_token} WHERE cjp_tokens_token_string = '%s'", $token);
}
?>

Since I didn't pass a second parameter in the validate function, the first two statements are ignored. Normally it should enter the third one returning false but since I did not test on the cjp_tokens_token_expire field, it went on to the last statement returning true. The test failed so I needed to fix code which I otherwhise never would have looked at again since I was quite sure it was working. Never be to sure. Anyway, changing the third statement as you can see underneath made the simpletest pass and me a very happy man.

<?php
 
elseif ($token != false && empty($formelement) && $token->cjp_tokens_token_expire < time()) {
?>

So for now, I promise never to write any code again without tests (if applicable of course).

Topics 

drupal, simpletest, planet
Subscribe to RSS - simpletest

You are here