Blogs, Handlebars, database fixtures

Blogs operations and Handlebars.

Blog operations

I’ve updated the WPDb module in latest wp-browser version to handle operations on the blogs table.
I’m moving toward a testing scenario set up like this one in an acceptance test that uses both the WPDb and the WPWebDriver modules

$I->wantTo('see the latest posts from each blog on the "Network Latest" page');

$I->haveMultisiteInDatabase();
$blogIds = $I->haveManyBlogsInDatabase(3);

foreach($blogIds as $blogId) {
    I->useBlog($blogId);
    $overrides = ['post_title' => 'Post {{n}} from blog with ID ' . $blogId];
    I->haveManyPostsInDatabase(3, $overrides);
}

$I->amOnPage('/network-latest');

foreach($blogIds as $blogId){
    $I->see('Post 2 from blog with ID ' . $blogId);
    $I->dontSee('Post 1 from blog with ID ' . $blogId);
    $I->dontSee('Post 0 from blog with ID ' . $blogId);
}

while this works on database terms I still have to throughly test the interaction between the WPWebDriver module, the WPDb one and WordPress .htaccess defined rules.

Many Handlebars

In the example above I’ve used the {{n}} placeholder to have it replaced with the post index in the series.
I’ve added a section to the package README file detailing the possibilities offered by the underlying Handlebars PHP templating engine but as a snippet the test above might be re-written like this

$I->wantTo('see the latest posts from each blog on the "Network Latest" page');

$I->haveMultisiteInDatabase();
$blogIds = $I->haveManyBlogsInDatabase(3);

foreach($blogIds as $blogId) {
    I->useBlog($blogId);
    $overrides = [
        'post_title' => 'Post {{n}} from blog with ID {{blog_id}}{{#unless visible}} - should not be visible{{/unless}}'],
        'template_data' => [
            'blog_id' => $blogId,
            'visible' => function($n){ return $n == 2;}
        ]
    I->haveManyPostsInDatabase(3, $overrides);
}

$I->amOnPage('/network-latest');

foreach($blogIds as $blogId){
    $I->see('Post 2 from blog with ID ' . $blogId);
    $I->dontSee('should not be visible');
}

Using a template engine here is an overkill but it opens up the possibility to set up complex database fixtures in code and avoid the SQL dump path.

I appreciate your input