drupal

Some simple debugging tips for Drupal

It was a funny week when it comes to squashing bugs in a few projects I'm involved with. Less is more and this is also true when it comes to debugging I guess, always look in common places if you encouter strange things. So here's a small list I'm really going to add on my checklist of debugging tips:

1: Always look in watchdog first. Almost everything within Drupal comes from the database and quite often when data is (partially) missing, it's because a query goes wrong. My use case: developer 1 defines some fields in the views ui and than implemented a views_query_alter hook in a module. Two days later, developer 2 decided to change the fields and poof, the query_alter completely freaked out, no content was shown and developer 1 didn't even knew about the views change. After 3 hours, he noticed the watchdog error and saw a field was missing.

2: Export your views to files and install journal. Exporting the views into a file gives you history if you use a code repository. You can easily switch back to a state where you were absolutely certain your site worked flawlessly. When someone else decides to override your default view, journal comes in handy since it adds a required textarea on the views UI (and also all other settings pages) where one has to fill in something about the changes. And even if he or she is very cryptic about it, you at least know who might have borked up your installation.

3: Never trust other people's commits. I learned that the hard way this week. I was confident my module worked fine, even the simple tests I wrote were passing and suddenly watchdog (remember rule 1) was growing with error messages. A colleague added some code which had absolutely nothing todo with my functionality so I figured something was still wrong with mine. A few hours of frustration later, I noticed a small word in an array was changed from 'field' to 'a' which caused the error messages appearing in the log. I had no idea where this came from, so I looked at the commit history (please, use a code repository) and I found the exact moment where this (desastrous) change occured - nobody knows why and how. Of course, these things happen and I'm pretty sure it probably happened to me too where I accidently changed some code without knowing so.

4: Only 16 chars! If you add your own watchdog messages, always remember the type is limited 16 characters. This is actually one I have to blame myself for. I decided to add a lot of watchdog messages for every action that happened into my module, so I could track down any errors that occured. However, all my types looked like modulename_action. The irony here lies in the fact that the name of the module was 16 chars long and when the errors started to popup, the action suffix was cut off when inserted into the database, so I had no idea where to look first. A quick look at the table structure of watchdog triggered a loud ironic laughter in the room and another hard lesson learned.

Topics 

drupal, bughunt, planet

Nerd night IV

One of my tasks on the Nerd Night yesterday was importing a custom php written blog into a Drupal installation. A simple import script querying basic info like title, date & body from the custom tables and doing a node_save($node) was a piece of cake, so after one hour, a good 500 entries were imported into Drupal. There was one small thing I couldn't fix immediately: those damn MS Word single & double quotes. For some reason, trying to import those made the rest of the entry disappear, so we had to manually copy/paste some of them as I didn't found an immediate solution. If anyone has some tips to fix this, let me know.

Also Drupal related, I got a mail this morning from an (old new - don't ask) colleague for an invitation to a party. To my big suprise, the Druplicon is on the leaflet but I honestly have no freaking idea why they used it. Not sure if I'm going to attend too ..

And to end with: I'm looking for new maintainers for PicLens and User types, I simply don't have enough time and/or interest to keep maintaining them fully, so anyone interested, file an issue in the webmasters queue and it's yours!

Topics 

drupal, nerd nights

Szeged and the future of MediaRSS

For the first time I gave a presentation at a Drupalcon. I was a bit nervous at first, but all ended up well with some nice reactions from other people. It also convinced me completely that I should separate all MediaRSS functions in the PicLens module into a separate contrib called MediaRSS API. Not sure when I'll get to this, but it's the most logical thing todo, but I first need to clean up my issue queue since I didn't had a lot of time this week to work on my existing projects.

On the contrary, I had a good time networking at Szeged Drupalcon meeting a lot of new people and catching up with the ones I knew. I also started working on Views UI Permissions after some brainstorming with old colleagues after the last session on thursday. The first version was online friday morning, so in a way I did my own code sprint. And I was one of the winners of the Testing Party, hurray! And a first little test ended up in MediaRSS support for Kris Vanderwater's Imagefield Gallery project.

Anyway, back in Belgium now, allready counting off to the next Drupalcon. Big thanks to the Drupal community, you guys so rock!

Imagecache javascript crop has a scaling option!

It took me some time to realize I would not find a decent solution with Jquery Interface to implement some sort of scaling function in my Imagecache javascript crop (formerly imagecrop) module. After some thinking, I found a solution which satisfies me enough to release this for drupal 5. It's now possible to create an imagecache preset with only the javascript action and use the original image to crop - instead of first adding a scaling function, which would result in frustration. On the crop page you can scale the image now. It's a select box at this point, I might change it to a textfield if I get many request for this. For now, it does what I and some clients want. Submit a patch if you want otherwhise (think: a setting on the imagecrop settings page). Anyway, there's an issue about this feature at http://drupal.org/node/281867. Use that page to report problems if you find them. Oh - and I can't stress this enough - *only* about the scaling problem in D5, thanks :) So, if you're interested, go to http://drupal.org/project/imagecrop and test out the development version. When the issue above is closed in two weeks, I'll roll out a new release containing some other small, but important fixes! I'll start porting the project to D6 after the drupalcon and - good news - it will use JQuery UI instead of Interface which gives me the opportunity to actually scale the image easier with jquery instead of a page refresh. Update Aug 3, 20:34: A cool patch by swortis now supports the thickbox module. Instead of a window popup, the thickbox overlay is used. Coolness! Trivial stuff to mention:

  • I renamed the project to Imagecache javascript crop instead of imagecrop. Makes more sense, really.
  • Yhager, ping-pong me on irc next week, we really need to have that talk!

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

Pages

Subscribe to RSS - drupal

You are here