Just few notes:
Looks like you’re doing functional/integration tests rather than unit tests in isolation.
Here you’re creating a user, but not setting it as the current one:
$user = $this->factory->user->create();
wp_set_current_user( 1 );
You probably want:
$user_id = $this->factory->user->create();
wp_set_current_user( $user_id );
Note that cookies aren’t set here, because of:
tests_add_filter( 'send_auth_cookies', '__return_false' );
If you use e.g.
add_filter( 'send_auth_cookies', '__return_true' );
then you will get an error when setcookie()
is called from wp_set_auth_cookie()
:
Cannot modify header information - headers already sent by (
The wp_get_session_token()
is a wrapper of wp_parse_auth_cookie()
.
Parsing a cookie with wp_parse_auth_cookie
assumes $_COOKIE
is set.
Wonder if a fake token string would work in your tests?
Else one could try:
$expiration = time() + DAY_IN_SECONDS;
$manager = WP_Session_Tokens::get_instance( $user_id );
$token = $manager->create( $expiration );
with a verification:
$this->manager->verify( $token );
and then a cleanup:
$this->manager->destroy( $token );