The problem is that the tests run in command line mode, and, of course, the current user is not authenticated there.
There are several alternatives on how to make it work. One option is to mute either the check_admin_referer() authentication function or its base wp_verify_nonce() authentication function. But this is not the best approach, since the tests have an integration flavor, while stubbing is a more approach for unit tests.
A good solution is to authenticate the user to pass the tests. You can do this relatively easily:
public function testValidateSettings() { $_REQUEST['security'] = wp_create_nonce($this->plugin_admin->plugin_name); $this->assertTrue($this->plugin_admin->sc_validate_settings()); }
I am not sure if $this->plugin_admin->plugin_name will work, as it may be private property. That way you can just pass it as a string with hard code:
public function testValidateSettings() { $_REQUEST['security'] = wp_create_nonce('whatever your plugin name is'); $this->assertTrue($this->plugin_admin->sc_validate_settings()); }
It would also be great to clean up after your test, so you should definitely do this in your test case:
public function tearDown() { unset($_REQUEST['security']); }
I donβt think you need to clear the newly created nonce after the tests, because the original WP tests do not clear this either.
That is pretty.
Improvement
If all your tests in the test case require authentication, you can put the creation of nonce in setUp() - this will make the test case more beautiful, since the break code will correspond to the setting:
public function setUp() { $_REQUEST['security'] = wp_create_nonce('whatever your plugin name is'); } public function tearDown() { unset($_REQUEST['security']); } public function testValidateSettings() { $this->assertTrue($this->plugin_admin->sc_validate_settings()); }
Sentence
Install Xdebug and connect to it through your IDE - this will help you go through the code step by step and see what does not work the way you expected. This is better than blindly fighting all these noses, http referees, etc.
Note
WP code is pretty sad. A huge list of functions in the global namespace, the lack of OOP, a complete request processing cycle (for example, unexpected die() ) really disappoints, because it significantly expands the code base and testing.
So get ready for big fights with the code :)