-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
161 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
/** | ||
* Class Friends_AccessControlTest | ||
* | ||
* @package Friends | ||
*/ | ||
|
||
namespace Friends; | ||
|
||
/** | ||
* Test the AccessControl class. | ||
*/ | ||
class AccessControlTest extends \WP_UnitTestCase { | ||
private $user_id, $friend_id; | ||
|
||
/** | ||
* Setup the unit tests. | ||
*/ | ||
public function set_up() { | ||
parent::set_up(); | ||
|
||
$this->user_id = $this->factory->user->create( | ||
array( | ||
'user_login' => 'me.local', | ||
'user_email' => '[email protected]', | ||
'role' => 'administrator', | ||
'user_url' => 'https://me.local', | ||
) | ||
); | ||
|
||
$this->friend_id = $this->factory->user->create( | ||
array( | ||
'user_login' => 'friend.local', | ||
'user_email' => '[email protected]', | ||
'role' => 'friend', | ||
'user_url' => 'https://friend.local', | ||
) | ||
); | ||
|
||
$friends = Friends::get_instance(); | ||
update_option( 'home', 'http://me.local' ); | ||
|
||
$this->friends_in_token = wp_generate_password( 128, false ); | ||
if ( update_user_option( $this->friend_id, 'friends_in_token', $this->friends_in_token ) ) { | ||
update_option( 'friends_in_token_' . $this->friends_in_token, $this->friend_id ); | ||
} | ||
|
||
// We're using the same in and out token here since we're faking this on a single install. | ||
update_user_option( $this->friend_id, 'friends_out_token', $this->friends_in_token ); | ||
} | ||
|
||
/** | ||
* Check that the friend auth works. | ||
*/ | ||
public function test_friend_auth() { | ||
$friends = Friends::get_instance(); | ||
|
||
update_option( 'home', 'http://friend.local' ); | ||
$friend_user = new User( $this->friend_id ); | ||
$friend_auth = $friend_user->get_friend_auth(); | ||
$this->assertNotEmpty( $friend_auth ); | ||
|
||
update_option( 'home', 'http://me.local' ); | ||
$user_id = $friends->access_control->check_friend_auth( $friend_auth ); | ||
$this->assertEquals( $user_id, $this->friend_id ); | ||
} | ||
|
||
/** | ||
* Check that the friend auth works with a different friend username. | ||
*/ | ||
public function test_friend_auth_different_username() { | ||
$friends = Friends::get_instance(); | ||
|
||
update_option( 'home', 'http://friend.local' ); | ||
$friend_user = new User( $this->friend_id ); | ||
|
||
// Let's set a different username. | ||
$userdata = get_userdata( $this->friend_id ); | ||
$last_user_login = $userdata->user_login; | ||
$userdata->user_login = 'hugo'; | ||
wp_update_user( $userdata ); | ||
|
||
$friend_auth = $friend_user->get_friend_auth(); | ||
$this->assertNotEmpty( $friend_auth ); | ||
|
||
update_option( 'home', 'http://me.local' ); | ||
$user_id = $friends->access_control->check_friend_auth( $friend_auth ); | ||
$this->assertEquals( $user_id, $this->friend_id ); | ||
|
||
$userdata = get_userdata( $this->friend_id ); | ||
$userdata->user_login = $last_user_login; | ||
wp_update_user( $userdata ); | ||
} | ||
|
||
|
||
/** | ||
* Check that the friend auth works. | ||
*/ | ||
public function test_private_feed_url() { | ||
$friends = Friends::get_instance(); | ||
add_filter( 'wp_doing_cron', '__return_true' ); | ||
update_option( 'home', 'http://friend.local' ); | ||
$friend_user = new User( $this->friend_id ); | ||
|
||
$term = new \WP_Term( | ||
(object) array( | ||
'name' => $friend_user->user_url . '/feed/', | ||
'url' => $friend_user->user_url . '/feed/', | ||
) | ||
); | ||
$user_feed = new User_Feed( $term, $friend_user ); | ||
|
||
$url = $user_feed->get_private_url(); | ||
$this->assertNotEmpty( $url ); | ||
|
||
parse_str( parse_url( $url, PHP_URL_QUERY ), $get ); | ||
$this->assertArrayHasKey( 'me', $get ); | ||
$this->assertArrayHasKey( 'auth', $get ); | ||
|
||
update_option( 'home', 'http://me.local' ); | ||
$_GET = $get; | ||
$user_id = $friends->access_control->authenticate( 0 ); | ||
$this->assertEquals( $user_id, $this->friend_id ); | ||
$_GET = array(); | ||
remove_filter( 'wp_doing_cron', '__return_true' ); | ||
} | ||
|
||
} |