Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I checked out (with svn) the code for Automated Testing. However, I want to run unit tests as a specific user.

Is there a way to do that? That is, is there a way to loggin with php code alone?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted
$user_id = $this->_make_user('author', 'user_login_name', 'password', 'test@test.com');
wp_set_current_user( $user_id );

as long as the class that you define the testcase in inherits from WPTestCase, then you can use the _make_user function. The function is found in this svn repository, and is defined in wp-testlib/base.php line 380.

NEW:

I switched to using wp_insert_user.

the following is in my setUp:

    $this->user_id = wp_insert_user(array('user_login'=>'test_administrator', 'role'=>'administrator'));
    wp_set_current_user( $this->user_id , '');

and the following is in my teardown:

    wp_delete_user($this->user_id);
    $this->user_id = 0;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.