Unit Tests: create more fixtures for Tests_User
. When using a factory to create ad hoc users, use the inherited static prop $static_factory
instead of the instance prop, $factory
. If 2 factories are used out of sync, the generator sequences diverge and dupes can be created, causing an untold number of unforeseen errors. Yay.
See #30017, #33968. git-svn-id: https://develop.svn.wordpress.org/trunk@35188 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
80178d8dc8
commit
742082c042
@ -5,13 +5,14 @@
|
|||||||
* @group user
|
* @group user
|
||||||
*/
|
*/
|
||||||
class Tests_User extends WP_UnitTestCase {
|
class Tests_User extends WP_UnitTestCase {
|
||||||
|
protected static $test_id;
|
||||||
|
protected static $author_id;
|
||||||
|
protected static $_author;
|
||||||
|
protected $author;
|
||||||
protected $user_data;
|
protected $user_data;
|
||||||
|
|
||||||
function setUp() {
|
public static function wpSetUpBeforeClass( $factory ) {
|
||||||
parent::setUp();
|
self::$test_id = $factory->user->create( array(
|
||||||
|
|
||||||
$this->user_data = array(
|
|
||||||
'user_login' => 'user1',
|
'user_login' => 'user1',
|
||||||
'user_nicename' => 'userone',
|
'user_nicename' => 'userone',
|
||||||
'user_pass' => 'password',
|
'user_pass' => 'password',
|
||||||
@ -20,14 +21,33 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
'display_name' => 'John Doe',
|
'display_name' => 'John Doe',
|
||||||
'user_email' => 'blackburn@battlefield3.com',
|
'user_email' => 'blackburn@battlefield3.com',
|
||||||
'user_url' => 'http://tacos.com'
|
'user_url' => 'http://tacos.com'
|
||||||
);
|
) );
|
||||||
|
self::$author_id = $factory->user->create( array( 'role' => 'author' ) );
|
||||||
|
self::$_author = get_user_by( 'ID', self::$author_id );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function wpTearDownAfterClass() {
|
||||||
|
$ids = array( self::$test_id, self::$author_id );
|
||||||
|
foreach ( $ids as $id ) {
|
||||||
|
if ( is_multisite() ) {
|
||||||
|
wpmu_delete_user( $id );
|
||||||
|
} else {
|
||||||
|
wp_delete_user( $id );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function setUp() {
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->author = clone self::$_author;
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_get_users_of_blog() {
|
function test_get_users_of_blog() {
|
||||||
// add one of each user role
|
// add one of each user role
|
||||||
$nusers = array();
|
$nusers = array();
|
||||||
foreach ( array('administrator', 'editor', 'author', 'contributor', 'subscriber' ) as $role ) {
|
foreach ( array('administrator', 'editor', 'author', 'contributor', 'subscriber' ) as $role ) {
|
||||||
$id = $this->factory->user->create( array( 'role' => $role ) );
|
$id = self::$static_factory->user->create( array( 'role' => $role ) );
|
||||||
$nusers[ $id ] = $id;
|
$nusers[ $id ] = $id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,54 +71,48 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
$key = rand_str();
|
$key = rand_str();
|
||||||
$val = rand_str();
|
$val = rand_str();
|
||||||
|
|
||||||
$user_id = $this->factory->user->create( array( 'role' => 'author' ) );
|
|
||||||
|
|
||||||
// get an option that doesn't exist
|
// get an option that doesn't exist
|
||||||
$this->assertFalse(get_user_option($key, $user_id));
|
$this->assertFalse( get_user_option( $key, self::$author_id ) );
|
||||||
|
|
||||||
// set and get
|
// set and get
|
||||||
update_user_option( $user_id, $key, $val );
|
update_user_option( self::$author_id, $key, $val );
|
||||||
$this->assertEquals( $val, get_user_option($key, $user_id) );
|
$this->assertEquals( $val, get_user_option( $key, self::$author_id ) );
|
||||||
|
|
||||||
// change and get again
|
// change and get again
|
||||||
$val2 = rand_str();
|
$val2 = rand_str();
|
||||||
update_user_option( $user_id, $key, $val2 );
|
update_user_option( self::$author_id, $key, $val2 );
|
||||||
$this->assertEquals( $val2, get_user_option($key, $user_id) );
|
$this->assertEquals( $val2, get_user_option( $key, self::$author_id ) );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// simple tests for usermeta functions
|
// simple tests for usermeta functions
|
||||||
function test_usermeta() {
|
function test_usermeta() {
|
||||||
|
|
||||||
$key = rand_str();
|
$key = rand_str();
|
||||||
$val = rand_str();
|
$val = rand_str();
|
||||||
|
|
||||||
$user_id = $this->factory->user->create( array( 'role' => 'author' ) );
|
|
||||||
|
|
||||||
// get a meta key that doesn't exist
|
// get a meta key that doesn't exist
|
||||||
$this->assertEquals( '', get_user_meta($user_id, $key, true));
|
$this->assertEquals( '', get_user_meta( self::$author_id, $key, true ) );
|
||||||
|
|
||||||
// set and get
|
// set and get
|
||||||
update_user_meta( $user_id, $key, $val );
|
update_user_meta( self::$author_id, $key, $val );
|
||||||
$this->assertEquals( $val, get_user_meta($user_id, $key, true) );
|
$this->assertEquals( $val, get_user_meta( self::$author_id, $key, true ) );
|
||||||
|
|
||||||
// change and get again
|
// change and get again
|
||||||
$val2 = rand_str();
|
$val2 = rand_str();
|
||||||
update_user_meta( $user_id, $key, $val2 );
|
update_user_meta( self::$author_id, $key, $val2 );
|
||||||
$this->assertEquals( $val2, get_user_meta($user_id, $key, true) );
|
$this->assertEquals( $val2, get_user_meta( self::$author_id, $key, true ) );
|
||||||
|
|
||||||
// delete and get
|
// delete and get
|
||||||
delete_user_meta( $user_id, $key );
|
delete_user_meta( self::$author_id, $key );
|
||||||
$this->assertEquals( '', get_user_meta($user_id, $key, true) );
|
$this->assertEquals( '', get_user_meta( self::$author_id, $key, true ) );
|
||||||
|
|
||||||
// delete by key AND value
|
// delete by key AND value
|
||||||
update_user_meta( $user_id, $key, $val );
|
update_user_meta( self::$author_id, $key, $val );
|
||||||
// incorrect key: key still exists
|
// incorrect key: key still exists
|
||||||
delete_user_meta( $user_id, $key, rand_str() );
|
delete_user_meta( self::$author_id, $key, rand_str() );
|
||||||
$this->assertEquals( $val, get_user_meta($user_id, $key, true) );
|
$this->assertEquals( $val, get_user_meta( self::$author_id, $key, true ) );
|
||||||
// correct key: deleted
|
// correct key: deleted
|
||||||
delete_user_meta( $user_id, $key, $val );
|
delete_user_meta( self::$author_id, $key, $val );
|
||||||
$this->assertEquals( '', get_user_meta($user_id, $key, true) );
|
$this->assertEquals( '', get_user_meta( self::$author_id, $key, true ) );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,40 +125,38 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
rand_str() => 'val-'.rand_str(),
|
rand_str() => 'val-'.rand_str(),
|
||||||
);
|
);
|
||||||
|
|
||||||
$user_id = $this->factory->user->create( array( 'role' => 'author' ) );
|
|
||||||
|
|
||||||
// there is already some stuff in the array
|
// there is already some stuff in the array
|
||||||
$this->assertTrue(is_array(get_user_meta($user_id)));
|
$this->assertTrue( is_array( get_user_meta( self::$author_id ) ) );
|
||||||
|
|
||||||
foreach ($vals as $k=>$v)
|
|
||||||
update_user_meta( $user_id, $k, $v );
|
|
||||||
|
|
||||||
|
foreach ( $vals as $k => $v ) {
|
||||||
|
update_user_meta( self::$author_id, $k, $v );
|
||||||
|
}
|
||||||
// get the complete usermeta array
|
// get the complete usermeta array
|
||||||
$out = get_user_meta($user_id);
|
$out = get_user_meta( self::$author_id );
|
||||||
|
|
||||||
// for reasons unclear, the resulting array is indexed numerically; meta keys are not included anywhere.
|
// for reasons unclear, the resulting array is indexed numerically; meta keys are not included anywhere.
|
||||||
// so we'll just check to make sure our values are included somewhere.
|
// so we'll just check to make sure our values are included somewhere.
|
||||||
foreach ($vals as $k=>$v)
|
foreach ( $vals as $k => $v ) {
|
||||||
$this->assertTrue( isset( $out[$k] ) && $out[$k][0] == $v );
|
$this->assertTrue( isset( $out[$k] ) && $out[$k][0] == $v );
|
||||||
|
}
|
||||||
// delete one key and check again
|
// delete one key and check again
|
||||||
$keys = array_keys( $vals );
|
$keys = array_keys( $vals );
|
||||||
$key_to_delete = array_pop( $keys );
|
$key_to_delete = array_pop( $keys );
|
||||||
delete_user_meta($user_id, $key_to_delete);
|
delete_user_meta( self::$author_id, $key_to_delete );
|
||||||
$out = get_user_meta($user_id);
|
$out = get_user_meta( self::$author_id );
|
||||||
// make sure that key is excluded from the results
|
// make sure that key is excluded from the results
|
||||||
foreach ($vals as $k=>$v) {
|
foreach ($vals as $k=>$v) {
|
||||||
if ($k == $key_to_delete)
|
if ($k == $key_to_delete) {
|
||||||
$this->assertFalse( isset( $out[$k] ) );
|
$this->assertFalse( isset( $out[$k] ) );
|
||||||
else
|
} else {
|
||||||
$this->assertTrue( isset( $out[$k] ) && $out[$k][0] == $v );
|
$this->assertTrue( isset( $out[$k] ) && $out[$k][0] == $v );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Test property magic functions for property get/set/isset.
|
// Test property magic functions for property get/set/isset.
|
||||||
function test_user_properties() {
|
function test_user_properties() {
|
||||||
$user_id = $this->factory->user->create( array( 'role' => 'author' ) );
|
$user = new WP_User( self::$author_id );
|
||||||
$user = new WP_User( $user_id );
|
|
||||||
|
|
||||||
foreach ( $user->data as $key => $data ) {
|
foreach ( $user->data as $key => $data ) {
|
||||||
$this->assertEquals( $data, $user->$key );
|
$this->assertEquals( $data, $user->$key );
|
||||||
@ -168,9 +180,7 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
* @ticket 20043
|
* @ticket 20043
|
||||||
*/
|
*/
|
||||||
public function test_user_unset() {
|
public function test_user_unset() {
|
||||||
// New user
|
$user = new WP_User( self::$author_id );
|
||||||
$user_id = $this->factory->user->create( array( 'role' => 'author' ) );
|
|
||||||
$user = new WP_User( $user_id );
|
|
||||||
|
|
||||||
// Test custom fields
|
// Test custom fields
|
||||||
$user->customField = 123;
|
$user->customField = 123;
|
||||||
@ -204,12 +214,9 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
|
|
||||||
// Test meta property magic functions for property get/set/isset.
|
// Test meta property magic functions for property get/set/isset.
|
||||||
function test_user_meta_properties() {
|
function test_user_meta_properties() {
|
||||||
global $wpdb;
|
$user = new WP_User( self::$author_id );
|
||||||
|
|
||||||
$user_id = $this->factory->user->create( array( 'role' => 'author' ) );
|
update_user_option( self::$author_id, 'foo', 'foo', true );
|
||||||
$user = new WP_User( $user_id );
|
|
||||||
|
|
||||||
update_user_option( $user_id, 'foo', 'foo', true );
|
|
||||||
|
|
||||||
$this->assertTrue( isset( $user->foo ) );
|
$this->assertTrue( isset( $user->foo ) );
|
||||||
|
|
||||||
@ -220,8 +227,7 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
* @expectedDeprecated WP_User->id
|
* @expectedDeprecated WP_User->id
|
||||||
*/
|
*/
|
||||||
function test_id_property_back_compat() {
|
function test_id_property_back_compat() {
|
||||||
$user_id = $this->factory->user->create( array( 'role' => 'author' ) );
|
$user = new WP_User( self::$author_id );
|
||||||
$user = new WP_User( $user_id );
|
|
||||||
|
|
||||||
$this->assertTrue( isset( $user->id ) );
|
$this->assertTrue( isset( $user->id ) );
|
||||||
$this->assertEquals( $user->ID, $user->id );
|
$this->assertEquals( $user->ID, $user->id );
|
||||||
@ -242,7 +248,7 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
);
|
);
|
||||||
|
|
||||||
foreach ( $roles as $role => $level ) {
|
foreach ( $roles as $role => $level ) {
|
||||||
$user_id = $this->factory->user->create( array( 'role' => $role ) );
|
$user_id = self::$static_factory->user->create( array( 'role' => $role ) );
|
||||||
$user = new WP_User( $user_id );
|
$user = new WP_User( $user_id );
|
||||||
|
|
||||||
$this->assertTrue( isset( $user->user_level ) );
|
$this->assertTrue( isset( $user->user_level ) );
|
||||||
@ -251,15 +257,13 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function test_construction() {
|
function test_construction() {
|
||||||
$user_id = $this->factory->user->create( array( 'role' => 'author' ) );
|
$user = new WP_User( self::$author_id );
|
||||||
|
|
||||||
$user = new WP_User( $user_id );
|
|
||||||
$this->assertInstanceOf( 'WP_User', $user );
|
$this->assertInstanceOf( 'WP_User', $user );
|
||||||
$this->assertEquals( $user_id, $user->ID );
|
$this->assertEquals( self::$author_id, $user->ID );
|
||||||
|
|
||||||
$user2 = new WP_User( 0, $user->user_login );
|
$user2 = new WP_User( 0, $user->user_login );
|
||||||
$this->assertInstanceOf( 'WP_User', $user2 );
|
$this->assertInstanceOf( 'WP_User', $user2 );
|
||||||
$this->assertEquals( $user_id, $user2->ID );
|
$this->assertEquals( self::$author_id, $user2->ID );
|
||||||
$this->assertEquals( $user->user_login, $user2->user_login );
|
$this->assertEquals( $user->user_login, $user2->user_login );
|
||||||
|
|
||||||
$user3 = new WP_User();
|
$user3 = new WP_User();
|
||||||
@ -268,31 +272,31 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
$this->assertFalse( isset( $user3->user_login ) );
|
$this->assertFalse( isset( $user3->user_login ) );
|
||||||
|
|
||||||
$user3->init( $user->data );
|
$user3->init( $user->data );
|
||||||
$this->assertEquals( $user_id, $user3->ID );
|
$this->assertEquals( self::$author_id, $user3->ID );
|
||||||
|
|
||||||
$user4 = new WP_User( $user->user_login );
|
$user4 = new WP_User( $user->user_login );
|
||||||
$this->assertInstanceOf( 'WP_User', $user4 );
|
$this->assertInstanceOf( 'WP_User', $user4 );
|
||||||
$this->assertEquals( $user_id, $user4->ID );
|
$this->assertEquals( self::$author_id, $user4->ID );
|
||||||
$this->assertEquals( $user->user_login, $user4->user_login );
|
$this->assertEquals( $user->user_login, $user4->user_login );
|
||||||
|
|
||||||
$user5 = new WP_User( null, $user->user_login );
|
$user5 = new WP_User( null, $user->user_login );
|
||||||
$this->assertInstanceOf( 'WP_User', $user5 );
|
$this->assertInstanceOf( 'WP_User', $user5 );
|
||||||
$this->assertEquals( $user_id, $user5->ID );
|
$this->assertEquals( self::$author_id, $user5->ID );
|
||||||
$this->assertEquals( $user->user_login, $user5->user_login );
|
$this->assertEquals( $user->user_login, $user5->user_login );
|
||||||
|
|
||||||
$user6 = new WP_User( $user );
|
$user6 = new WP_User( $user );
|
||||||
$this->assertInstanceOf( 'WP_User', $user6 );
|
$this->assertInstanceOf( 'WP_User', $user6 );
|
||||||
$this->assertEquals( $user_id, $user6->ID );
|
$this->assertEquals( self::$author_id, $user6->ID );
|
||||||
$this->assertEquals( $user->user_login, $user6->user_login );
|
$this->assertEquals( $user->user_login, $user6->user_login );
|
||||||
|
|
||||||
$user7 = new WP_User( $user->data );
|
$user7 = new WP_User( $user->data );
|
||||||
$this->assertInstanceOf( 'WP_User', $user7 );
|
$this->assertInstanceOf( 'WP_User', $user7 );
|
||||||
$this->assertEquals( $user_id, $user7->ID );
|
$this->assertEquals( self::$author_id, $user7->ID );
|
||||||
$this->assertEquals( $user->user_login, $user7->user_login );
|
$this->assertEquals( $user->user_login, $user7->user_login );
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_get() {
|
function test_get() {
|
||||||
$user_id = $this->factory->user->create( array(
|
$user_id = self::$static_factory->user->create( array(
|
||||||
'role' => 'author',
|
'role' => 'author',
|
||||||
'user_login' => 'test_wp_user_get',
|
'user_login' => 'test_wp_user_get',
|
||||||
'user_pass' => 'password',
|
'user_pass' => 'password',
|
||||||
@ -310,7 +314,7 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function test_has_prop() {
|
function test_has_prop() {
|
||||||
$user_id = $this->factory->user->create( array(
|
$user_id = self::$static_factory->user->create( array(
|
||||||
'role' => 'author',
|
'role' => 'author',
|
||||||
'user_login' => 'test_wp_user_has_prop',
|
'user_login' => 'test_wp_user_has_prop',
|
||||||
'user_pass' => 'password',
|
'user_pass' => 'password',
|
||||||
@ -327,7 +331,7 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function test_update_user() {
|
function test_update_user() {
|
||||||
$user_id = $this->factory->user->create( array(
|
$user_id = self::$static_factory->user->create( array(
|
||||||
'role' => 'author',
|
'role' => 'author',
|
||||||
'user_login' => 'test_wp_update_user',
|
'user_login' => 'test_wp_update_user',
|
||||||
'user_pass' => 'password',
|
'user_pass' => 'password',
|
||||||
@ -363,16 +367,19 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
$this->assertEquals( 'some test user', $user->get( 'display_name' ) );
|
$this->assertEquals( 'some test user', $user->get( 'display_name' ) );
|
||||||
|
|
||||||
// Test update of fields in _get_additional_user_keys()
|
// Test update of fields in _get_additional_user_keys()
|
||||||
$user_data = array( 'ID' => $user_id, 'use_ssl' => 1, 'show_admin_bar_front' => 1,
|
$user_data = array(
|
||||||
|
'ID' => $user_id, 'use_ssl' => 1, 'show_admin_bar_front' => 1,
|
||||||
'rich_editing' => 1, 'first_name' => 'first', 'last_name' => 'last',
|
'rich_editing' => 1, 'first_name' => 'first', 'last_name' => 'last',
|
||||||
'nickname' => 'nick', 'comment_shortcuts' => 'true', 'admin_color' => 'classic',
|
'nickname' => 'nick', 'comment_shortcuts' => 'true', 'admin_color' => 'classic',
|
||||||
'description' => 'describe' );
|
'description' => 'describe'
|
||||||
|
);
|
||||||
wp_update_user( $user_data );
|
wp_update_user( $user_data );
|
||||||
|
|
||||||
$user = new WP_User( $user_id );
|
$user = new WP_User( $user_id );
|
||||||
foreach ( $user_data as $key => $value )
|
foreach ( $user_data as $key => $value ) {
|
||||||
$this->assertEquals( $value, $user->get( $key ), $key );
|
$this->assertEquals( $value, $user->get( $key ), $key );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ticket 19595
|
* ticket 19595
|
||||||
@ -380,7 +387,7 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
function test_global_userdata() {
|
function test_global_userdata() {
|
||||||
global $userdata, $wpdb;
|
global $userdata, $wpdb;
|
||||||
|
|
||||||
$user_id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
|
$user_id = self::$static_factory->user->create( array( 'role' => 'subscriber' ) );
|
||||||
wp_set_current_user( $user_id );
|
wp_set_current_user( $user_id );
|
||||||
|
|
||||||
$this->assertNotEmpty( $userdata );
|
$this->assertNotEmpty( $userdata );
|
||||||
@ -401,8 +408,7 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function test_exists() {
|
function test_exists() {
|
||||||
$user_id = $this->factory->user->create( array( 'role' => 'author' ) );
|
$user = new WP_User( self::$author_id );
|
||||||
$user = new WP_User( $user_id );
|
|
||||||
|
|
||||||
$this->assertTrue( $user->exists() );
|
$this->assertTrue( $user->exists() );
|
||||||
|
|
||||||
@ -420,11 +426,10 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
|
|
||||||
$old_post_id = $id;
|
$old_post_id = $id;
|
||||||
|
|
||||||
$user_id = $this->factory->user->create( array( 'role' => 'author' ) );
|
$user = new WP_User( self::$author_id );
|
||||||
$user = new WP_User( $user_id );
|
|
||||||
|
|
||||||
$post = array(
|
$post = array(
|
||||||
'post_author' => $user_id,
|
'post_author' => self::$author_id,
|
||||||
'post_status' => 'publish',
|
'post_status' => 'publish',
|
||||||
'post_content' => rand_str(),
|
'post_content' => rand_str(),
|
||||||
'post_title' => rand_str(),
|
'post_title' => rand_str(),
|
||||||
@ -439,11 +444,12 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
|
|
||||||
$this->assertNotEmpty( $authordata );
|
$this->assertNotEmpty( $authordata );
|
||||||
$this->assertInstanceOf( 'WP_User', $authordata );
|
$this->assertInstanceOf( 'WP_User', $authordata );
|
||||||
$this->assertEquals( $authordata->ID, $user_id );
|
$this->assertEquals( $authordata->ID, self::$author_id );
|
||||||
|
|
||||||
if ( $old_post_id )
|
if ( $old_post_id ) {
|
||||||
setup_postdata( get_post( $old_post_id ) );
|
setup_postdata( get_post( $old_post_id ) );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ticket 13317
|
* @ticket 13317
|
||||||
@ -456,51 +462,47 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function test_user_get_data_by_id() {
|
function test_user_get_data_by_id() {
|
||||||
$user_id = $this->factory->user->create();
|
$user = WP_User::get_data_by( 'id', self::$author_id );
|
||||||
|
|
||||||
$user = WP_User::get_data_by( 'id', $user_id );
|
|
||||||
$this->assertInstanceOf( 'stdClass', $user );
|
$this->assertInstanceOf( 'stdClass', $user );
|
||||||
$this->assertEquals( $user_id, $user->ID );
|
$this->assertEquals( self::$author_id, $user->ID );
|
||||||
|
|
||||||
// @ticket 23480
|
// @ticket 23480
|
||||||
$user = WP_User::get_data_by( 'id', -1 );
|
$user1 = WP_User::get_data_by( 'id', -1 );
|
||||||
$this->assertEquals( false, $user );
|
$this->assertEquals( false, $user1 );
|
||||||
|
|
||||||
$user = WP_User::get_data_by( 'id', 0 );
|
$user2 = WP_User::get_data_by( 'id', 0 );
|
||||||
$this->assertEquals( false, $user );
|
$this->assertEquals( false, $user2 );
|
||||||
|
|
||||||
$user = WP_User::get_data_by( 'id', null );
|
$user3 = WP_User::get_data_by( 'id', null );
|
||||||
$this->assertEquals( false, $user );
|
$this->assertEquals( false, $user3 );
|
||||||
|
|
||||||
$user = WP_User::get_data_by( 'id', '' );
|
$user4 = WP_User::get_data_by( 'id', '' );
|
||||||
$this->assertEquals( false, $user );
|
$this->assertEquals( false, $user4 );
|
||||||
|
|
||||||
$user = WP_User::get_data_by( 'id', false );
|
$user5 = WP_User::get_data_by( 'id', false );
|
||||||
$this->assertEquals( false, $user );
|
$this->assertEquals( false, $user5 );
|
||||||
|
|
||||||
$user = WP_User::get_data_by( 'id', @$user->user_nicename );
|
$user6 = WP_User::get_data_by( 'id', $user->user_nicename );
|
||||||
$this->assertEquals( false, $user );
|
$this->assertEquals( false, $user6 );
|
||||||
|
|
||||||
$user = WP_User::get_data_by( 'id', 99999 );
|
$user7 = WP_User::get_data_by( 'id', 99999 );
|
||||||
$this->assertEquals( false, $user );
|
$this->assertEquals( false, $user7 );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ticket 33869
|
* @ticket 33869
|
||||||
*/
|
*/
|
||||||
public function test_user_get_data_by_ID_should_alias_to_id() {
|
public function test_user_get_data_by_ID_should_alias_to_id() {
|
||||||
$u = $this->factory->user->create();
|
$user = WP_User::get_data_by( 'ID', self::$author_id );
|
||||||
|
$this->assertEquals( self::$author_id, $user->ID );
|
||||||
$user = WP_User::get_data_by( 'ID', $u );
|
|
||||||
$this->assertEquals( $u, $user->ID );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ticket 21431
|
* @ticket 21431
|
||||||
*/
|
*/
|
||||||
function test_count_many_users_posts() {
|
function test_count_many_users_posts() {
|
||||||
$user_id_a = $this->factory->user->create( array( 'role' => 'author' ) );
|
$user_id_a = self::$static_factory->user->create( array( 'role' => 'author' ) );
|
||||||
$user_id_b = $this->factory->user->create( array( 'role' => 'author' ) );
|
$user_id_b = self::$static_factory->user->create( array( 'role' => 'author' ) );
|
||||||
$post_id_a = $this->factory->post->create( array( 'post_author' => $user_id_a ) );
|
$post_id_a = $this->factory->post->create( array( 'post_author' => $user_id_a ) );
|
||||||
$post_id_b = $this->factory->post->create( array( 'post_author' => $user_id_b ) );
|
$post_id_b = $this->factory->post->create( array( 'post_author' => $user_id_b ) );
|
||||||
$post_id_c = $this->factory->post->create( array( 'post_author' => $user_id_b, 'post_status' => 'private' ) );
|
$post_id_c = $this->factory->post->create( array( 'post_author' => $user_id_b, 'post_status' => 'private' ) );
|
||||||
@ -571,28 +573,29 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
$id1 = wp_insert_user( array(
|
$id1 = wp_insert_user( array(
|
||||||
'user_login' => rand_str(),
|
'user_login' => rand_str(),
|
||||||
'user_pass' => 'password',
|
'user_pass' => 'password',
|
||||||
'user_email' => 'blackburn@battlefield3.com',
|
'user_email' => 'blackburn@battlefield4.com',
|
||||||
) );
|
) );
|
||||||
$this->assertEquals( $id1, email_exists( 'blackburn@battlefield3.com' ) );
|
$this->assertEquals( $id1, email_exists( 'blackburn@battlefield4.com' ) );
|
||||||
|
|
||||||
$id2 = wp_insert_user( array(
|
$id2 = wp_insert_user( array(
|
||||||
'user_login' => rand_str(),
|
'user_login' => rand_str(),
|
||||||
'user_pass' => 'password',
|
'user_pass' => 'password',
|
||||||
'user_email' => 'miller@battlefield3.com',
|
'user_email' => 'miller@battlefield4.com',
|
||||||
) );
|
) );
|
||||||
$this->assertEquals( $id2, email_exists( 'miller@battlefield3.com' ) );
|
$this->assertEquals( $id2, email_exists( 'miller@battlefield4.com' ) );
|
||||||
|
|
||||||
if ( ! is_wp_error( $id2 ) ){
|
if ( ! is_wp_error( $id2 ) ){
|
||||||
$return = wp_update_user( array(
|
wp_update_user( array(
|
||||||
'ID' => $id2,
|
'ID' => $id2,
|
||||||
'user_email' => 'david@battlefield3.com',
|
'user_email' => 'david@battlefield4.com',
|
||||||
) );
|
) );
|
||||||
$this->assertEquals( $id2, email_exists( 'david@battlefield3.com' ) );
|
$this->assertEquals( $id2, email_exists( 'david@battlefield4.com' ) );
|
||||||
|
|
||||||
$return = wp_update_user( array(
|
$return = wp_update_user( array(
|
||||||
'ID' => $id2,
|
'ID' => $id2,
|
||||||
'user_email' => 'blackburn@battlefield3.com',
|
'user_email' => 'blackburn@battlefield4.com',
|
||||||
) );
|
) );
|
||||||
|
|
||||||
if ( ! defined( 'WP_IMPORTING' ) ) {
|
if ( ! defined( 'WP_IMPORTING' ) ) {
|
||||||
$this->assertWPError( $return );
|
$this->assertWPError( $return );
|
||||||
}
|
}
|
||||||
@ -636,7 +639,7 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
* @ticket 29696
|
* @ticket 29696
|
||||||
*/
|
*/
|
||||||
public function test_wp_insert_user_should_sanitize_user_nicename_parameter() {
|
public function test_wp_insert_user_should_sanitize_user_nicename_parameter() {
|
||||||
$user = $this->factory->user->create_and_get();
|
$user = $this->author;
|
||||||
|
|
||||||
$userdata = $user->to_array();
|
$userdata = $user->to_array();
|
||||||
$userdata['user_nicename'] = str_replace( '-', '.', $user->user_nicename );
|
$userdata['user_nicename'] = str_replace( '-', '.', $user->user_nicename );
|
||||||
@ -719,7 +722,7 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
* @ticket 33793
|
* @ticket 33793
|
||||||
*/
|
*/
|
||||||
public function test_wp_insert_user_should_not_truncate_to_a_duplicate_user_nicename() {
|
public function test_wp_insert_user_should_not_truncate_to_a_duplicate_user_nicename() {
|
||||||
$u1 = $this->factory->user->create( array(
|
$u1 = self::$static_factory->user->create( array(
|
||||||
'user_nicename' => str_repeat( 'a', 50 ),
|
'user_nicename' => str_repeat( 'a', 50 ),
|
||||||
) );
|
) );
|
||||||
|
|
||||||
@ -740,7 +743,7 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
* @ticket 33793
|
* @ticket 33793
|
||||||
*/
|
*/
|
||||||
public function test_wp_insert_user_should_not_truncate_to_a_duplicate_user_nicename_when_suffix_has_more_than_one_character() {
|
public function test_wp_insert_user_should_not_truncate_to_a_duplicate_user_nicename_when_suffix_has_more_than_one_character() {
|
||||||
$users = $this->factory->user->create_many( 9, array(
|
$users = self::$static_factory->user->create_many( 4, array(
|
||||||
'user_nicename' => str_repeat( 'a', 50 ),
|
'user_nicename' => str_repeat( 'a', 50 ),
|
||||||
) );
|
) );
|
||||||
|
|
||||||
@ -753,14 +756,14 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
|
|
||||||
$this->assertNotEmpty( $u );
|
$this->assertNotEmpty( $u );
|
||||||
$user = new WP_User( $u );
|
$user = new WP_User( $u );
|
||||||
$expected = str_repeat( 'a', 47 ) . '-10';
|
$expected = str_repeat( 'a', 48 ) . '-5';
|
||||||
$this->assertSame( $expected, $user->user_nicename );
|
$this->assertSame( $expected, $user->user_nicename );
|
||||||
}
|
}
|
||||||
|
|
||||||
function test_changing_email_invalidates_password_reset_key() {
|
function test_changing_email_invalidates_password_reset_key() {
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
|
|
||||||
$user = $this->factory->user->create_and_get();
|
$user = $this->author;
|
||||||
$wpdb->update( $wpdb->users, array( 'user_activation_key' => 'key' ), array( 'ID' => $user->ID ) );
|
$wpdb->update( $wpdb->users, array( 'user_activation_key' => 'key' ), array( 'ID' => $user->ID ) );
|
||||||
clean_user_cache( $user );
|
clean_user_cache( $user );
|
||||||
|
|
||||||
@ -790,43 +793,33 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function test_search_users_login() {
|
public function test_search_users_login() {
|
||||||
$id = $this->factory->user->create( $this->user_data );
|
|
||||||
|
|
||||||
$users = get_users( array( 'search' => 'user1', 'fields' => 'ID' ) );
|
$users = get_users( array( 'search' => 'user1', 'fields' => 'ID' ) );
|
||||||
|
|
||||||
$this->assertTrue( in_array( $id, $users ) );
|
$this->assertTrue( in_array( self::$test_id, $users ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_search_users_url() {
|
public function test_search_users_url() {
|
||||||
$id = $this->factory->user->create( $this->user_data );
|
|
||||||
|
|
||||||
$users = get_users( array( 'search' => '*tacos*', 'fields' => 'ID' ) );
|
$users = get_users( array( 'search' => '*tacos*', 'fields' => 'ID' ) );
|
||||||
|
|
||||||
$this->assertTrue( in_array( $id, $users ) );
|
$this->assertTrue( in_array( self::$test_id, $users ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_search_users_email() {
|
public function test_search_users_email() {
|
||||||
$id = $this->factory->user->create( $this->user_data );
|
|
||||||
|
|
||||||
$users = get_users( array( 'search' => '*battle*', 'fields' => 'ID' ) );
|
$users = get_users( array( 'search' => '*battle*', 'fields' => 'ID' ) );
|
||||||
|
|
||||||
$this->assertTrue( in_array( $id, $users ) );
|
$this->assertTrue( in_array( self::$test_id, $users ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_search_users_nicename() {
|
public function test_search_users_nicename() {
|
||||||
$id = $this->factory->user->create( $this->user_data );
|
|
||||||
|
|
||||||
$users = get_users( array( 'search' => '*one*', 'fields' => 'ID' ) );
|
$users = get_users( array( 'search' => '*one*', 'fields' => 'ID' ) );
|
||||||
|
|
||||||
$this->assertTrue( in_array( $id, $users ) );
|
$this->assertTrue( in_array( self::$test_id, $users ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_search_users_display_name() {
|
public function test_search_users_display_name() {
|
||||||
$id = $this->factory->user->create( $this->user_data );
|
|
||||||
|
|
||||||
$users = get_users( array( 'search' => '*Doe*', 'fields' => 'ID' ) );
|
$users = get_users( array( 'search' => '*Doe*', 'fields' => 'ID' ) );
|
||||||
|
|
||||||
$this->assertTrue( in_array( $id, $users ) );
|
$this->assertTrue( in_array( self::$test_id, $users ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -834,7 +827,7 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
*/
|
*/
|
||||||
function test_email_case() {
|
function test_email_case() {
|
||||||
// Create a test user with a lower-case email address.
|
// Create a test user with a lower-case email address.
|
||||||
$user_id = $this->factory->user->create( array(
|
$user_id = self::$static_factory->user->create( array(
|
||||||
'user_email' => 'test@test.com',
|
'user_email' => 'test@test.com',
|
||||||
) );
|
) );
|
||||||
|
|
||||||
@ -853,7 +846,7 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
*/
|
*/
|
||||||
function test_email_change() {
|
function test_email_change() {
|
||||||
// Create a test user.
|
// Create a test user.
|
||||||
$user_id = $this->factory->user->create( array(
|
$user_id = self::$static_factory->user->create( array(
|
||||||
'user_email' => 'test@test.com',
|
'user_email' => 'test@test.com',
|
||||||
) );
|
) );
|
||||||
|
|
||||||
@ -884,9 +877,7 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
$was_admin_email_sent = false;
|
$was_admin_email_sent = false;
|
||||||
$was_user_email_sent = false;
|
$was_user_email_sent = false;
|
||||||
|
|
||||||
$user = $this->factory->user->create( $this->user_data );
|
wp_new_user_notification( self::$test_id, null, $notify );
|
||||||
|
|
||||||
wp_new_user_notification( $user, null, $notify );
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check to see if a notification email was sent to the
|
* Check to see if a notification email was sent to the
|
||||||
@ -943,14 +934,12 @@ class Tests_User extends WP_UnitTestCase {
|
|||||||
* @expectedDeprecated wp_new_user_notification
|
* @expectedDeprecated wp_new_user_notification
|
||||||
*/
|
*/
|
||||||
function test_wp_new_user_notification_old_signature_throws_deprecated_warning() {
|
function test_wp_new_user_notification_old_signature_throws_deprecated_warning() {
|
||||||
$user = $this->factory->user->create(
|
$user = self::$static_factory->user->create( array(
|
||||||
array(
|
|
||||||
'role' => 'author',
|
'role' => 'author',
|
||||||
'user_login' => 'test_wp_new_user_notification',
|
'user_login' => 'test_wp_new_user_notification',
|
||||||
'user_pass' => 'password',
|
'user_pass' => 'password',
|
||||||
'user_email' => 'test@test.com',
|
'user_email' => 'test@test.com',
|
||||||
)
|
) );
|
||||||
);
|
|
||||||
|
|
||||||
wp_new_user_notification( $user, 'this_is_deprecated' );
|
wp_new_user_notification( $user, 'this_is_deprecated' );
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user