wpSetCurrentUser.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * @group user
  4. */
  5. class Tests_User_WpSetCurrentUser extends WP_UnitTestCase {
  6. protected static $user_id;
  7. protected static $user_id2;
  8. protected static $user_ids = array();
  9. public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
  10. self::$user_id = $factory->user->create();
  11. self::$user_ids[] = self::$user_id;
  12. self::$user_id2 = $factory->user->create( array( 'user_login' => 'foo' ) );
  13. self::$user_ids[] = self::$user_id2;
  14. }
  15. public function test_set_by_id() {
  16. $user = wp_set_current_user( self::$user_id );
  17. $this->assertSame( self::$user_id, $user->ID );
  18. $this->assertSame( $user, wp_get_current_user() );
  19. $this->assertSame( self::$user_id, get_current_user_id() );
  20. }
  21. public function test_name_should_be_ignored_if_id_is_not_null() {
  22. $user = wp_set_current_user( self::$user_id, 'foo' );
  23. $this->assertSame( self::$user_id, $user->ID );
  24. $this->assertSame( $user, wp_get_current_user() );
  25. $this->assertSame( self::$user_id, get_current_user_id() );
  26. }
  27. public function test_should_set_by_name_if_id_is_null_and_current_user_is_nonempty() {
  28. wp_set_current_user( self::$user_id );
  29. $this->assertSame( self::$user_id, get_current_user_id() );
  30. $user = wp_set_current_user( null, 'foo' );
  31. $this->assertSame( self::$user_id2, $user->ID );
  32. $this->assertSame( $user, wp_get_current_user() );
  33. $this->assertSame( self::$user_id2, get_current_user_id() );
  34. }
  35. /**
  36. * Test that you can set the current user by the name parameter when the current user is 0.
  37. *
  38. * @ticket 20845
  39. */
  40. public function test_should_set_by_name_if_id_is_null() {
  41. wp_set_current_user( 0 );
  42. $this->assertSame( 0, get_current_user_id() );
  43. $user = wp_set_current_user( null, 'foo' );
  44. $this->assertSame( self::$user_id2, $user->ID );
  45. $this->assertSame( $user, wp_get_current_user() );
  46. $this->assertSame( self::$user_id2, get_current_user_id() );
  47. }
  48. }