Grabbing a WordPress cookie

Where I outline the underway implementation of an easy way to get hold of WordPress cookies for easier acceptance testing.

The problem

In an earlier article I’ve been using a WP Browser sugar method to get hold of session cookies that would allow me to get to WordPress admin area; one of the method reads like this

/** * @test * it should redirect to the admin area path when accessing /admin with rights */ public function it_should_redirect_to_the_admin_area_when_accessing_admin_with_rights(AcceptanceTester $I) { $I->wantTo( "go to the wp admin area using the /admin path" ); $I->loginAsAdmin(); $I->amOnPage( '/admin' ); $I->seeElement( 'body.wp-admin' ); } 

inside of a Cest format test case.
What I’m doing in the $I->loginAsAdmin() step is not functional to the test but it’s just a way to get hold of WordPress access cookies.

A first solution

The Codeception cest format test cases will allow for _before and _after methods to be set up in a way similar to PHPUnit setUp and tearDown methods; as a first step I can move the method in the _before method of my test case

public function _before( AcceptanceTester $I ) { $I->loginAsAdmin(); } 

and then proceed as usual, giving the access rights for granted, in each test

/** * @test * it should redirect to the admin area path when accessing /admin with rights */ public function it_should_redirect_to_the_admin_area_when_accessing_admin_with_rights(AcceptanceTester $I) { $I->wantTo( "go to the wp admin area using the /admin path" ); $I->amOnPage( '/admin' ); $I->seeElement( 'body.wp-admin' ); } 

while that works I’d like to implement a more flexible solution.

Actually PhpBrowser allows to get a cookie using the $I->grabCookie($name) method. Sadly that’s a total match and WordPress default auth cookie name, something like wordpress_6351ac2f7406b4de5fda8c4296b96bb3, will not match using the method like

$I->grabCookie('wordpress_'); 

I will be adding a grabCookieWithPattern method to the WP Browser package to solve this problem.

Saving and setting cookies

Another need I’d like to solve, keeping in mind that WP Browser is a WordPress-oriented Codeception module library, is the one dealing with retrieving, storing and setting WordPress auth cookies using sugar methods in the test methods.
Ideally I’d like to be able to do something like this

public function _before( AcceptanceTester $I ) { // grab, store but not set $I->storeWpAuthCookies(); // grab, store and set $I->setWpAuthCookies(); } /** * @test * it should redirect to the admin area path when accessing /admin with rights */ public function it_should_redirect_to_the_admin_area_when_accessing_admin_with_rights(AcceptanceTester $I) { $I->wantTo( "go to the wp admin area using the /admin path" ); // new sugar method: either set from stored or no op // if set already $I->setWpAuthCookies(); $I->amOnPage( '/admin' ); $I->seeElement( 'body.wp-admin' ); } /** * @test * it should redirect to the login when accessing /admin w/o rights */ public function it_should_redirect_to_the_login_when_accessing_admin_w_o_rights(AcceptanceTester $I) { $I->wantTo( "get redirected to the login when accessing /admin without rights" ); // new sugar method: if cookies are set unset them $I->resetCookies() $I->amOnPage( '/admin' ); $I->seeElement( 'body.login' ); } 

Next

Idea to code!

Probably related

I appreciate your input