Changeset 35247 for trunk/tests/phpunit/tests/user.php
- Timestamp:
- 10/17/2015 09:14:28 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/user.php
r35245 r35247 6 6 */ 7 7 class Tests_User extends WP_UnitTestCase { 8 protected static $test_id; 8 protected static $admin_id; 9 protected static $editor_id; 9 10 protected static $author_id; 11 protected static $contrib_id; 12 protected static $sub_id; 13 14 protected static $user_ids = array(); 15 10 16 protected static $_author; 11 17 protected $author; … … 13 19 14 20 public static function wpSetUpBeforeClass( $factory ) { 15 self::$ test_id = $factory->user->create( array(21 self::$user_ids[] = self::$contrib_id = $factory->user->create( array( 16 22 'user_login' => 'user1', 17 23 'user_nicename' => 'userone', … … 21 27 'display_name' => 'John Doe', 22 28 'user_email' => 'blackburn@battlefield3.com', 23 'user_url' => 'http://tacos.com' 24 ) ); 25 self::$author_id = $factory->user->create( array( 'role' => 'author' ) ); 29 'user_url' => 'http://tacos.com', 30 'role' => 'contributor' 31 ) ); 32 33 self::$user_ids[] = self::$author_id = $factory->user->create( array( 34 'user_login' => 'author_login', 35 'user_email' => 'author@email.com', 36 'role' => 'author' 37 ) ); 38 39 self::$user_ids[] = self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) ); 40 self::$user_ids[] = self::$editor_id = $factory->user->create( array( 41 'role' => 'editor', 42 'user_email' => 'test@test.com', 43 ) ); 44 self::$user_ids[] = self::$sub_id = $factory->user->create( array( 'role' => 'subscriber' ) ); 45 26 46 self::$_author = get_user_by( 'ID', self::$author_id ); 27 47 } 28 48 29 49 public static function wpTearDownAfterClass() { 30 $ids = array( self::$test_id, self::$author_id ); 31 foreach ( $ids as $id ) { 50 foreach ( self::$user_ids as $id ) { 32 51 self::delete_user( $id ); 33 52 } … … 42 61 function test_get_users_of_blog() { 43 62 // add one of each user role 44 $nusers = array(); 45 foreach ( array('administrator', 'editor', 'author', 'contributor', 'subscriber' ) as $role ) { 46 $id = self::factory()->user->create( array( 'role' => $role ) ); 47 $nusers[ $id ] = $id; 48 } 63 $nusers = array( 64 self::$contrib_id, 65 self::$author_id, 66 self::$admin_id, 67 self::$editor_id, 68 self::$sub_id, 69 ); 49 70 50 71 $user_list = get_users(); … … 54 75 foreach ( $user_list as $user ) { 55 76 // only include the users we just created - there might be some others that existed previously 56 if ( i sset( $nusers[$user->ID]) ) {57 $found[ $user->ID] = $user->ID;77 if ( in_array( $user->ID, $nusers ) ) { 78 $found[] = $user->ID; 58 79 } 59 80 } 60 81 61 82 // make sure every user we created was returned 62 $this->assertEqual s($nusers, $found);83 $this->assertEqualSets( $nusers, $found ); 63 84 } 64 85 … … 237 258 function test_user_level_property_back_compat() { 238 259 $roles = array( 239 'administrator' => 10, 240 'editor' => 7, 241 'author' => 2, 242 'contributor' => 1, 243 'subscriber' => 0, 244 ); 245 246 foreach ( $roles as $role => $level ) { 247 $user_id = self::factory()->user->create( array( 'role' => $role ) ); 260 self::$admin_id => 10, 261 self::$editor_id => 7, 262 self::$author_id => 2, 263 self::$contrib_id => 1, 264 self::$sub_id => 0, 265 ); 266 267 foreach ( $roles as $user_id => $level ) { 248 268 $user = new WP_User( $user_id ); 249 269 … … 293 313 294 314 function test_get() { 295 $user_id = self::factory()->user->create( array( 296 'role' => 'author', 297 'user_login' => 'test_wp_user_get', 298 'user_pass' => 'password', 299 'user_email' => 'test@test.com', 300 ) ); 301 302 $user = new WP_User( $user_id ); 303 $this->assertEquals( 'test_wp_user_get', $user->get( 'user_login' ) ); 304 $this->assertEquals( 'test@test.com', $user->get( 'user_email' ) ); 315 $user = new WP_User( self::$author_id ); 316 $this->assertEquals( 'author_login', $user->get( 'user_login' ) ); 317 $this->assertEquals( 'author@email.com', $user->get( 'user_email' ) ); 305 318 $this->assertEquals( 0, $user->get( 'use_ssl' ) ); 306 319 $this->assertEquals( '', $user->get( 'field_that_does_not_exist' ) ); 307 320 308 update_user_meta( $user_id, 'dashed-key', 'abcdefg' );321 update_user_meta( self::$author_id, 'dashed-key', 'abcdefg' ); 309 322 $this->assertEquals( 'abcdefg', $user->get( 'dashed-key' ) ); 310 323 } 311 324 312 325 function test_has_prop() { 313 $user_id = self::factory()->user->create( array( 314 'role' => 'author', 315 'user_login' => 'test_wp_user_has_prop', 316 'user_pass' => 'password', 317 'user_email' => 'test2@test.com', 318 ) ); 319 320 $user = new WP_User( $user_id ); 326 $user = new WP_User( self::$author_id ); 321 327 $this->assertTrue( $user->has_prop( 'user_email') ); 322 328 $this->assertTrue( $user->has_prop( 'use_ssl' ) ); 323 329 $this->assertFalse( $user->has_prop( 'field_that_does_not_exist' ) ); 324 330 325 update_user_meta( $user_id, 'dashed-key', 'abcdefg' );331 update_user_meta( self::$author_id, 'dashed-key', 'abcdefg' ); 326 332 $this->assertTrue( $user->has_prop( 'dashed-key' ) ); 327 333 } 328 334 329 335 function test_update_user() { 330 $user_id = self::factory()->user->create( array( 331 'role' => 'author', 332 'user_login' => 'test_wp_update_user', 333 'user_pass' => 'password', 334 'user_email' => 'test3@test.com', 335 ) ); 336 $user = new WP_User( $user_id ); 337 338 update_user_meta( $user_id, 'description', 'about me' ); 336 $user = new WP_User( self::$author_id ); 337 338 update_user_meta( self::$author_id, 'description', 'about me' ); 339 339 $this->assertEquals( 'about me', $user->get( 'description' ) ); 340 340 341 $user_data = array( 'ID' => $user_id, 'display_name' => 'test user' );341 $user_data = array( 'ID' => self::$author_id, 'display_name' => 'test user' ); 342 342 wp_update_user( $user_data ); 343 343 344 $user = new WP_User( $user_id );344 $user = new WP_User( self::$author_id ); 345 345 $this->assertEquals( 'test user', $user->get( 'display_name' ) ); 346 346 … … 349 349 350 350 // Pass as stdClass 351 $user_data = array( 'ID' => $user_id, 'display_name' => 'a test user' );351 $user_data = array( 'ID' => self::$author_id, 'display_name' => 'a test user' ); 352 352 wp_update_user( (object) $user_data ); 353 353 354 $user = new WP_User( $user_id );354 $user = new WP_User( self::$author_id ); 355 355 $this->assertEquals( 'a test user', $user->get( 'display_name' ) ); 356 356 357 // Pass as WP_User358 $user = new WP_User( $user_id );359 357 $user->display_name = 'some test user'; 360 358 wp_update_user( $user ); 361 359 362 $user = new WP_User( $user_id );363 360 $this->assertEquals( 'some test user', $user->get( 'display_name' ) ); 364 361 365 362 // Test update of fields in _get_additional_user_keys() 366 363 $user_data = array( 367 'ID' => $user_id, 'use_ssl' => 1, 'show_admin_bar_front' => 1,364 'ID' => self::$author_id, 'use_ssl' => 1, 'show_admin_bar_front' => 1, 368 365 'rich_editing' => 1, 'first_name' => 'first', 'last_name' => 'last', 369 366 'nickname' => 'nick', 'comment_shortcuts' => 'true', 'admin_color' => 'classic', … … 372 369 wp_update_user( $user_data ); 373 370 374 $user = new WP_User( $user_id );371 $user = new WP_User( self::$author_id ); 375 372 foreach ( $user_data as $key => $value ) { 376 373 $this->assertEquals( $value, $user->get( $key ), $key ); … … 384 381 global $userdata, $wpdb; 385 382 386 $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) ); 387 wp_set_current_user( $user_id ); 383 wp_set_current_user( self::$sub_id ); 388 384 389 385 $this->assertNotEmpty( $userdata ); 390 386 $this->assertInstanceOf( 'WP_User', $userdata ); 391 $this->assertEquals( $userdata->ID, $user_id );387 $this->assertEquals( $userdata->ID, self::$sub_id ); 392 388 $prefix = $wpdb->get_blog_prefix(); 393 389 $cap_key = $prefix . 'capabilities'; … … 498 494 */ 499 495 function test_count_many_users_posts() { 500 $user_id_a = self::factory()->user->create( array( 'role' => 'author' ) );501 496 $user_id_b = self::factory()->user->create( array( 'role' => 'author' ) ); 502 $post_id_a = self::factory()->post->create( array( 'post_author' => $user_id_a) );497 $post_id_a = self::factory()->post->create( array( 'post_author' => self::$author_id ) ); 503 498 $post_id_b = self::factory()->post->create( array( 'post_author' => $user_id_b ) ); 504 499 $post_id_c = self::factory()->post->create( array( 'post_author' => $user_id_b, 'post_status' => 'private' ) ); 505 500 506 wp_set_current_user( $user_id_a);507 $counts = count_many_users_posts( array( $user_id_a, $user_id_b), 'post', false );508 $this->assertEquals( 1, $counts[ $user_id_a] );501 wp_set_current_user( self::$author_id ); 502 $counts = count_many_users_posts( array( self::$author_id, $user_id_b ), 'post', false ); 503 $this->assertEquals( 1, $counts[self::$author_id] ); 509 504 $this->assertEquals( 1, $counts[$user_id_b] ); 510 505 511 $counts = count_many_users_posts( array( $user_id_a, $user_id_b), 'post', true );512 $this->assertEquals( 1, $counts[ $user_id_a] );506 $counts = count_many_users_posts( array( self::$author_id, $user_id_b ), 'post', true ); 507 $this->assertEquals( 1, $counts[self::$author_id] ); 513 508 $this->assertEquals( 1, $counts[$user_id_b] ); 514 509 515 510 wp_set_current_user( $user_id_b ); 516 $counts = count_many_users_posts( array( $user_id_a, $user_id_b), 'post', false );517 $this->assertEquals( 1, $counts[ $user_id_a] );511 $counts = count_many_users_posts( array( self::$author_id, $user_id_b ), 'post', false ); 512 $this->assertEquals( 1, $counts[self::$author_id] ); 518 513 $this->assertEquals( 2, $counts[$user_id_b] ); 519 514 520 $counts = count_many_users_posts( array( $user_id_a, $user_id_b), 'post', true );521 $this->assertEquals( 1, $counts[ $user_id_a] );515 $counts = count_many_users_posts( array( self::$author_id, $user_id_b ), 'post', true ); 516 $this->assertEquals( 1, $counts[self::$author_id] ); 522 517 $this->assertEquals( 1, $counts[$user_id_b] ); 523 518 } … … 860 855 $users = get_users( array( 'search' => 'user1', 'fields' => 'ID' ) ); 861 856 862 $this->assertTrue( in_array( self::$ test_id, $users ) );857 $this->assertTrue( in_array( self::$contrib_id, $users ) ); 863 858 } 864 859 … … 866 861 $users = get_users( array( 'search' => '*tacos*', 'fields' => 'ID' ) ); 867 862 868 $this->assertTrue( in_array( self::$ test_id, $users ) );863 $this->assertTrue( in_array( self::$contrib_id, $users ) ); 869 864 } 870 865 … … 872 867 $users = get_users( array( 'search' => '*battle*', 'fields' => 'ID' ) ); 873 868 874 $this->assertTrue( in_array( self::$ test_id, $users ) );869 $this->assertTrue( in_array( self::$contrib_id, $users ) ); 875 870 } 876 871 … … 878 873 $users = get_users( array( 'search' => '*one*', 'fields' => 'ID' ) ); 879 874 880 $this->assertTrue( in_array( self::$ test_id, $users ) );875 $this->assertTrue( in_array( self::$contrib_id, $users ) ); 881 876 } 882 877 … … 884 879 $users = get_users( array( 'search' => '*Doe*', 'fields' => 'ID' ) ); 885 880 886 $this->assertTrue( in_array( self::$ test_id, $users ) );881 $this->assertTrue( in_array( self::$contrib_id, $users ) ); 887 882 } 888 883 … … 891 886 */ 892 887 function test_email_case() { 893 // Create a test user with a lower-case email address.894 $user_id = self::factory()->user->create( array(895 'user_email' => 'test@test.com',896 ) );897 898 888 // Alter the case of the email address (which stays the same). 899 889 $userdata = array( 900 'ID' => $user_id,890 'ID' => self::$editor_id, 901 891 'user_email' => 'test@TEST.com', 902 892 ); 903 893 $update = wp_update_user( $userdata ); 904 894 905 $this->assertEquals( $user_id, $update );895 $this->assertEquals( self::$editor_id, $update ); 906 896 } 907 897 … … 910 900 */ 911 901 function test_email_change() { 912 // Create a test user.913 $user_id = self::factory()->user->create( array(914 'user_email' => 'test@test.com',915 ) );916 917 902 // Change the email address. 918 903 $userdata = array( 919 'ID' => $user_id,904 'ID' => self::$editor_id, 920 905 'user_email' => 'test2@test.com', 921 906 ); … … 923 908 924 909 // Was this successful? 925 $this->assertEquals( $user_id, $update );910 $this->assertEquals( self::$editor_id, $update ); 926 911 927 912 // Verify that the email address has been updated. 928 $user = get_userdata( $user_id );913 $user = get_userdata( self::$editor_id ); 929 914 $this->assertEquals( $user->user_email, 'test2@test.com' ); 930 915 } … … 942 927 $was_user_email_sent = false; 943 928 944 wp_new_user_notification( self::$ test_id, null, $notify );929 wp_new_user_notification( self::$contrib_id, null, $notify ); 945 930 946 931 /* … … 999 984 */ 1000 985 function test_wp_new_user_notification_old_signature_throws_deprecated_warning() { 1001 $user = self::factory()->user->create( array( 1002 'role' => 'author', 1003 'user_login' => 'test_wp_new_user_notification', 1004 'user_pass' => 'password', 1005 'user_email' => 'test@test.com', 1006 ) ); 1007 1008 wp_new_user_notification( $user, 'this_is_deprecated' ); 986 wp_new_user_notification( self::$author_id, 'this_is_deprecated' ); 1009 987 } 1010 988
Note: See TracChangeset
for help on using the changeset viewer.