Getting WordPress cookies once per test

Cutting the overhead time of WordPress CodeCeption tests.

The problem

While developing a client site I need to verify expectations on the admin side of things. Be it using an Administrator user or any other user each and every test in the suite, a Codeception Cest format test suite, will require the user to login each time. While that’s not a problem I’ve recently added methods to WP Browser allowing for a cut on that overhead price each test pays.
As an example I could have a Cest suite testing the post creation possibilities of the Editor role

<?php
use \AcceptanceTester;

class EditorPostCreationCest {

    /**
     * @before
     */
    protected function login( AcceptanceTester $I ) {
        // before each test login as the editor
        $I->loginAs('editor', 'editor');
    }

    /**
     * @test
     * it should allow inserting a post
     */
    public function it_should_allow_inserting_a_post( AcceptanceTester $I ) {
        $this->login( $I );
        $I->amOnPage( '/wp/wp-admin/post-new.php' );
        $I->seeElement( 'body.wp-admin.post-new-php' );
    }

    /**
     * @test
     * it should allow inserting a page
     */
    public function it_should_allow_inserting_a_page(AcceptanceTester $I) {
        $this->login( $I );
        $I->amOnPage( '/wp/wp-admin/post-new.php?post_type=page' );
        $I->seeElement( 'body.wp-admin.post-new-php' );
    }

    /**
     * @test
     * it should allow inserting a note
     */
    public function it_should_allow_inserting_a_note(AcceptanceTester $I) {
        $this->login( $I );
        //...
    }

    /**
     * @test
     * it should allow inserting a workshop
     */
    public function it_should_allow_inserting_a_workshop(AcceptanceTester $I) {
        $this->login( $I );
        //...
    }

    /**
     * @test
     * it should allow inserting a product
     */
    public function it_should_allow_inserting_a_product(AcceptanceTester $I) {
        $this->login( $I );
        //...
    }

    // many more CPTs here...

}

Each test is logging in the Editor user again as PhpBrowser will not save cookies after each section unless told to do so and that will apply to all the tests using the module in the same suite.
A more granular control, one allowing a per-test setting of WordPress auth cookies, might involve using the recently added grabWordPressAuthCookie and grabWordPressLoginCookie WP Browser methods.

Getting the cookies once, using them when needed

The test case above can be rewritten to leverage the @beforeClass annotation to get the auth cookies, and pay the time price of doing so, only once:

<?php
use \AcceptanceTester;

class EditorPostCreationCest {

    protected static $authCookie;
    protected static $loginCookie;

    /**
     * @beforeClass
     */
    public static function getAuthCookies( AcceptanceTester $I ) {
        $I->loginAs('editor', 'editor');
        self::$authCookie = $I->grabWordPressAuthCookie();
        self::$loginCookie = $I->grabWordPressLoginCookie();
    }

    /**
     * @before
     */
    protected function login( AcceptanceTester $I ) {
        $I->setCookie(self::$authCookie->getName(), self::$authCookie->getValue());
        $I->setCookie(self::$loginCookie->getName(), self::$loginCookie->getValue());
    }

    /**
     * @test
     * it should allow inserting a post
     */
    public function it_should_allow_inserting_a_post( AcceptanceTester $I ) {
        $this->login( $I );
        $I->amOnPage( '/wp/wp-admin/post-new.php' );
        $I->seeElement( 'body.wp-admin.post-new-php' );
    }

    /**
     * @test
     * it should allow inserting a page
     */
    public function it_should_allow_inserting_a_page(AcceptanceTester $I) {
        $this->login( $I );
        $I->amOnPage( '/wp/wp-admin/post-new.php?post_type=page' );
        $I->seeElement( 'body.wp-admin.post-new-php' );
    }

    /**
     * @test
     * it should allow inserting a note
     */
    public function it_should_allow_inserting_a_note(AcceptanceTester $I) {
        $this->login( $I );
        //...
    }

    /**
     * @test
     * it should allow inserting a workshop
     */
    public function it_should_allow_inserting_a_workshop(AcceptanceTester $I) {
        $this->login( $I );
        //...
    }

    /**
     * @test
     * it should allow inserting a product
     */
    public function it_should_allow_inserting_a_product(AcceptanceTester $I) {
        $this->login( $I );
        //...
    }

    // many more CPTs here...

}

and cut the overhead time that logging in the user each time would require. It’s not the only way but it’s a way more.

I appreciate your input