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.


drupal, bughunt, planet

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!

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:

// 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)) {
    elseif (
$token != false && empty($formelement)) {

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

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.

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).


drupal, simpletest, planet

RTBF on Drupal

After 3 months of heavy development, RTBF (Radio-Télévision Belge de la Communauté française) was launched yesterday on Drupal. And this is only the first of a few phases still to come. The first launch consists of 3 major parts:

  1. RTBF Info: a news information site with lots of multimedia like images, videos and songs available page- or article-based. In the background, a couple of xmlrpc services are set up to get info for the lotto, belganews, weather and so on. The editors at RTBF can easily add new articles and connect multimedia in every possible way they might think of.
  2. RTBF Media: simply a pool of all multimedia files. Most of them are imported with xmlrpc services making radio emissions, journals etc created at RTBF, available on the web. The Info site (and in the future all other sites from RTBF) can use items from this pool to display in multimedia blocks or an individual article.
  3. RTBF iPlayer: a major flex application which connects to the MediaServer through the amfphp and services module. You can browse all on demand multimedia and soon all streaming channels will be made available too. If you are on the site, you can launch this player by clicking on 'Plus de medias' in the video block on the homepage.

A lot of custom modules were written to get this rolling, but I made the most generic ones available on the d.o. website:

  1. Imagecrop: This module makes a javascript toolbox action available thanks to the power of Imagecache 2.
  2. Blockclone: Adds a new operation to duplicate an existing block defined by other modules.
  3. CCK comment reference: defines a cck field type for referencing a comment.

This is definitely the biggest setup I've done so far and at the same time the last one at my current work. I'm starting at a new place within two weeks, more news on that later, let's keep it as a suprise!


drupal, work, planet


Subscribe to RSS - planet

You are here