Easier menu fixtures in acceptance testing

Have menus in database!

A not so long test

The last code I had written while trying to walk through the new possibilities of the WPDb module (part of wp-browser, a Codeception add-on), was not “long” but could be made better.

$I = new AcceptanceTester( $scenario );
$I->wantTo( 'see the main-header id on the main site header on a multi site' );

// setup multisite installation
$I->haveMultisiteInDatabase( true, true );
$I->useMainBlog();

// set the theme to `_blogs`
$I->haveOptionInDatabase( 'template', 'Twentysixteen' );
$I->haveOptionInDatabase( 'stylesheet', '_blogs' );
$I->haveOptionInDatabase( 'current_theme', '_Blogs' );

// add a nav menu
$menu_id = $I->haveTermInDatabase( 'Menu 1', 'nav_menu', [ 'slug' => 'menu1' ] );

// set theme options to use the `primary` location
$I->haveOptionInDatabase( 'theme_mods__blogs', [ 'nav_menu_locations' => [ 'primary' => $menu_id ] ] );

// add one element to the menu
$menu_item_id = $I->havePostInDatabase( [
    'post_title' => 'theAverageDev',
    'menu_order' => 1,
    'post_type'  => 'nav_menu_item'
] );
$meta         = [
    '_menu_item_type'             => 'custom',
    '_menu_item_object_id'        => $menu_item_id,
    '_menu_item_object'           => 'custom',
    '_menu_item_url'              => 'http://theaveragedev.com'
];
foreach ( $meta as $key => $value ) {
    $I->havePostmetaInDatabase( $menu_item_id, $key, $value );
}

$I->amOnPage('/');
$I->seeElement('div#site-header-menu > nav.main-navigation');
$I->seeElement('div#site-header-menu > nav.main-header');

In a coding effort I’ve pushed version 1.9.2 of WPBrowser to GitHub and that allows rewriting the code above to this

$I = new AcceptanceTester( $scenario );
$I->wantTo( 'see the main-header id on the main site header on a multi site' );

// setup multisite installation
$I->haveMultisiteInDatabase( true, true );
$I->useMainBlog();

// set the theme to `_blogs`
$I->useTheme('_blogs', 'twentysixteen');

// add a nav menu
$I->haveMenuInDatabase('menu1', 'primary');

// add one element to the menu
$I->haveMenuItemInDatabase('menu1', 'Link 1');

$I->amOnPage('/');
$I->seeElement('div#site-header-menu > nav.main-navigation');
$I->seeElement('div#site-header-menu > nav.main-header');

Menus have always been a pain to set up in fixtures due to their terms and posts based structure but this first implementation eases the burden a little.

I appreciate your input