November 11, 2010

Bulk view

Drupal, SimpleTest, and the node access API

Setting up Simpletest and Drush on Drupal 6.x:

  1. Download and enable Simpletest with drush dl simpletest; drush en -y simpletest
  2. Download to your ~/.drush/drush_extras directory. This version allows you to run a single test from the command-line.
  3. Create a custom module with a tests/ subdirectory, and write your tests in it. (See this Lullabot Simpletest tutorial.)

We’re starting another Drupal project. While the IT architect is working on clarifying the requirements, I volunteered to implement the risky parts so that we could get a better sense of what we needed to do.

The first major chunk of risk was fine-grained access control. Some users needed to be able to edit the nodes associated with other users, and some users needed to have partial access to nodes depending on how they were referenced by the node. Because there were many cases, I decided to start by writing unit tests.

SimpleTest was not as straightforward in Drupal 6.x as it was in Drupal 5.x. There were a few things that confused me before I figured things out.

I wondered why my queries were running off different table prefixes. I didn’t have some of the data I expected to have. It turns out that Simpletest now works on a separate Drupal instance by default, using a unique table prefix so that it doesn’t mess around with your regular database. I’m doing this on a test server and I want to be able to easily look up details using SQL, so I needed to add this to my test case:

class ExampleTestCase extends DrupalWebTestCase {
  function setUp() {
    global $base_url;
    $this->originalPrefix = $GLOBALS['db_prefix'];
  function tearDown() { }

I also didn’t like how the built-in $this->drupalCreateUser took permissions instead of roles, and how it created custom roles each time. I created a function that looked up the role IDs using the {role} table, then added the role IDs and roles to the $edit['roles'] array before creating the user.

Lastly, I needed to add the Content Profile operations to my custom user creation function. I based this code on content_profile.test.

// create a content_profile node
$edit = array(
  'title' => $account->name,
  'body'  => $this->randomName(),
$this->drupalPost('node/add/' . str_replace(' ', '-', $role), $edit, t('Save'));

It would’ve been even better to do this without going through the web interface, but it was fine for a quick hack.

I had the setup I wanted for writing test cases that checked user permissions. I wrote functions for checking if the user could accept an invitation (must be invited, must not already have accepted, and must be able to fit). SimpleTest made it easy to test each of the functions, allowing me to build and test blocks that I could then put together.

The code in content_permission.module turned out to be a good starting point for my field-level permissions, while the Drupal node access API made it easy to handle the user-association-related permissions even though I used node references instead of user references.

It was a good day of hacking. I wrote tests, then I wrote code, then I argued with the computer until my tests passed. ;) It was fun seeing my progress and knowing I wasn’t screwing up things I’d already solved.

If you’re writing Drupal code, I strongly recommend giving SimpleTest a try. Implementing hook_node_access_records and hook_node_grants is much easier when you can write a test to make sure the right records are showing up. (With the occasional use of node_access_acquire_grants to recalculate…) Otherwise-invisible Drupal code becomes easy to verify. The time you invest into writing tests will pay off throughout the project, and during future work as well. Have fun!

Reinvention: virtual storytelling summit Nov 11 – 22, 2010

Update: May 12 2012: Hmm, files are missing. Sorry!

UPDATE: Here are my sketchnotes from the first day. Click on each one to view the full-sized version. Want to share this post with others? Short URL: . (Follow me on Twitter: @sachac)

New: Added three more sketchnotes:

That Resonates With Me! How to Change the World, One Story at a Time, Nancy Duarte


Shift Your Story Arc: Creating the Trajectory of Your Life, Julie Ann Turner


Screw Your Career Path, Live Your Story! Jason Seiden


Previously posted:

Why You Need to Tell a Bigger Story, Get Storied


Spend Shift: How the Post-Crisis Values Revolution is Changing the Way We Buy, Sell, and Live, John Gerzema


Just Enough to Make a Story: Creating a Narrative from an Anecdote, Sean Buvala


Social Movements as Participatory Storytelling, Andy Goodman and Lily McCombs


Want to share this post with others? Short URL: . (Follow me on Twitter: @sachac)


I normally gloss over marketing e-mails, particularly the ones that ask me to promote something to the readers of “sacha chua :: tech evangelist, storyteller, geek”. It was a good thing Anthony Marques reached out to me again about the Reinvention Summit, which turned out to be a virtual summit on storytelling with some pretty good speakers. The sessions will run from Nov 11 (today!) to Nov 22, and the basic Explorer’s Pass is currently $33.33 – which you can get down to $8.33 if my math is right, using this $25 coupon code: REINVENTION. – the REINVENTION coupon doesn’t seem to apply, but oh well! Not a bad price for attending sessions by Nancy Duarte, Steve Denning, and other storytelling gurus.

Of the 32 sessions planned, here are a few I’m particularly interested in:

Just Enough to Make A Story: Creating a Narrative from an Anecdote
Sean Buvala, Thu, November 11, 4pm – 5pm

In most business and nonprofit settings, there is plenty of anecdotal content for just about any point you would like to illustrate. However, these single-reference-point remnants of story need to be filled out and supported in order to make their biggest impact. In this workshop, you will learn some methods for helping you create impactful stories from these story-starters.

That Resonates with Me! How to Change the World One Presentation at a Time
Nancy Duarte, Thursday, November 18, 2pm – 3pm

If you say “I have an idea for something”, what you really mean is “I want to change the world in some way.” You might not be able to change the entire world, but what is “the world” anyway? It is simply all of the ideas of all our ancestors. Look around you. Your clothes, language, furniture, house, city, and nation all began as visions in other minds.

Humans love to create. And creating starts with an idea that can change the world.

“The only reason to give a speech is to change the world.” – John F. Kennedy

Presentations are the lingua franca of business and those who master communicating with them rise faster than their peers, reach more customers than their competitors, and turn causes into a groundswell.

Pioneering presentation innovator Nancy Duarte, CEO of Duarte Design, will demonstrate how to apply the methods in her book resonate: Presenting Visual Stories That Transform Audiences, to build meaningful connections with audiences that compel them to action. Her groundbreaking work details a new way of structuring a presentation and connecting with an audience – helping the presenter create a human connection.

Changing the world starts with transforming an audience and an audience will only change if you resonate with them.

In this session, you will learn to:

  • Leverage the hidden story structures inherent in great communication
  • Connect with your audience empathetically
  • Create captivating content
  • Craft ideas that get repeated
  • Inspire enthusiasm and support for your vision

This session is for leaders who are tasked with communicating clearly and persuading through verbal communications.

Telling Taller Tales
Andrew Melville, Monday, November 15, 5pm – 6pm

I run through a model of three different levels of story; interesting, through memorable to compelling. I build on journalistic and script writing story principles to discuss people’s Intention behind storytelling, and look at observation, juxtaposition and transformation as components of powerful storytelling. I talk about experiences working with the Maori tribes of New Zealand, and their oral traditions, and metaphors from nature. Telling Taller Tales talks about building authentic and honest stories in the workplace, melding a brand story connecting marketing messages, vision and values and corridor conversations.

Why Great Storytelling Initiatives Fail, and What Can Be Done About It
Steve Denning, Friday, November 19, 4pm – 5pm

Why do great leadership storytelling initiatives tend to fail? These world-class initiatives in established organizations seem to flourish for a while, with strong top management support and demonstrated results; but then something happens, and the initiative is sidelined or downsized or undermined in some indirect fashion, Why do managers act in this way? Why don’t they recognize that storytelling is central to leadership and key to their organizations future? What can be done to sustain storytelling initiatives? Steve Denning draws on the findings from his new book, The Leader’s Guide to Radical Management, and shows what kind of changes are needed to have storytelling take its rightful place as a key management and leadership tool in 21st Century organizations. Come learn seven principles to enable storytelling in organizations.


Even if Anthony didn’t offer me a free press pass and discount coupon for sharing (code: REINVENTION), I’d probably pay for it anyway – it looks like it will be worth it. I might not have come across it without his nudging, though, so thanks, Anthony! =)

I’ll post sketchnotes for the sessions I do manage to make it to. Check it out!