WPDb multisite capabilities demo 02

Writing the first acceptance tests.

An assertion pivot

Having to test the site header id attribute means I have to set a baseline for such a verification to pass.
Looking at the summary user stories I wrote in the previous article I’m going to translate the first one in an acceptance test; since I’m extending the Twentysixteen theme I’ve modified the story to match the theme markup:

Given WordPress is installed as single site And there is a menu registered in the primary location When I visit the site index page Then I should see a div#site-header-menu > nav#main-header element.

I’m targeting, with the test, the elements I can control and this means HTML attributes and markup structures in place of content.
I add the first Codeception cept test using Codeception CLI

codecept generate:cept acceptance MainHeader

and move it, for organizational purposes, in the /tests/acceptance/_blogs/SingleSite/PrimaryMenu folder. Codeception will look recursively in the folders and the cept test will run without problems.

Setting up a menu

The test fixture is this case consists in the following steps:

  1. set the _Blogs theme as the current theme
  2. add a nav menu
  3. set the theme options to use said menu in the primary menu location
  4. add some elements to the nav menu

Starting from an empty, and hence passing, acceptance test

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

point 1 is covered setting some options in the database

// 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.local'
foreach ( $meta as $key => $value ) {
    $I->havePostmetaInDatabase( $menu_item_id, $key, $value );

and finally make some assertions

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

At this point the test will fail on the last assertion: no surprise here as I have to modify the primary menu classes yet. MainSiteHeader failing acceptance test


The code is on GitHub to follow along.


I will make this first acceptance test pass modifying the code to then move to other user stories.