My local WordPress development environment includes two different databases. The “regular” one, that I use when manually testing the plugins I develop and the “testing” one for acceptance tests.

As part of the acceptance tests for the Daily Writing Habit plugin I needed to insert new data in the database and then check the effect of the new data on the plugin report page and dashboard widget.

To keep the tests replicable, I had to insert this data on the test database so that I could control the expected results. This meant to switch from the regular to the test database just before launching the acceptance tests and going back to the regular one for further manual explorations.

Thanks to Luca Temedei I now know how to configure this behaviour automatically. You can read the full conversation here but the summary is the following:

  1. Add a condition to wp-config.php depending on the presence of a specific HTTP Header
    
    
    if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'Codeception'){
    	define('DB_NAME', 'wptests' );
    } else {
    	define('DB_NAME', 'wordpress'); 
    }
    
  2. Make sure you send this HTTP Header when accessing the localhost during acceptance testing. To do so, call the haveHTTPHeader function in the initialization (_before section) of the test:
    
     public function _before(AcceptanceTester $I)
        {
    	    $I->haveHttpHeader('X-Requested-With', 'Codeception');
        }
    
    
Share This