| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @group l10n |
| 5 | * @group i18n |
| 6 | */ |
| 7 | class Tests_Get_UserGender extends WP_UnitTestCase { |
| 8 | protected $user_id; |
| 9 | |
| 10 | public function setUp() { |
| 11 | parent::setUp(); |
| 12 | |
| 13 | $this->user_id = $this->factory()->user->create( |
| 14 | array( |
| 15 | 'role' => 'administrator', |
| 16 | 'gender' => 'female', |
| 17 | ) |
| 18 | ); |
| 19 | |
| 20 | wp_set_current_user( $this->user_id ); |
| 21 | } |
| 22 | |
| 23 | public function tearDown() { |
| 24 | delete_user_meta( $this->user_id, 'gender' ); |
| 25 | set_current_screen( 'front' ); |
| 26 | |
| 27 | parent::tearDown(); |
| 28 | } |
| 29 | |
| 30 | public function test_user_gender_property() { |
| 31 | set_current_screen( 'dashboard' ); |
| 32 | $this->assertSame( 'female', get_user_gender() ); |
| 33 | $this->assertSame( get_user_by( 'id', $this->user_id )->gender, get_user_gender() ); |
| 34 | } |
| 35 | |
| 36 | public function test_update_user_gender() { |
| 37 | set_current_screen( 'dashboard' ); |
| 38 | update_user_meta( $this->user_id, 'gender', 'male' ); |
| 39 | $this->assertSame( 'male', get_user_gender() ); |
| 40 | } |
| 41 | |
| 42 | public function test_returns_site_gender_if_empty() { |
| 43 | set_current_screen( 'dashboard' ); |
| 44 | update_user_meta( $this->user_id, 'gender', '' ); |
| 45 | $this->assertSame( 'unknown', get_user_gender() ); |
| 46 | } |
| 47 | |
| 48 | public function test_returns_site_gender_if_no_user() { |
| 49 | wp_set_current_user( 0 ); |
| 50 | $this->assertSame( 'unknown', get_user_gender() ); |
| 51 | } |
| 52 | |
| 53 | public function test_returns_correct_user_gender() { |
| 54 | set_current_screen( 'dashboard' ); |
| 55 | $this->assertSame( 'female', get_user_gender() ); |
| 56 | } |
| 57 | |
| 58 | public function test_returns_correct_user_gender_on_frontend() { |
| 59 | $this->assertSame( 'female', get_user_gender() ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @group ms-required |
| 64 | */ |
| 65 | public function test_user_gender_is_same_across_network() { |
| 66 | if ( ! is_multisite() ) { |
| 67 | $this->markTestSkipped( __METHOD__ . ' requires multisite' ); |
| 68 | } |
| 69 | |
| 70 | $user_gender = get_user_gender(); |
| 71 | |
| 72 | switch_to_blog( self::factory()->blog->create() ); |
| 73 | $user_gender_2 = get_user_gender(); |
| 74 | restore_current_blog(); |
| 75 | |
| 76 | $this->assertSame( 'female', $user_gender ); |
| 77 | $this->assertSame( $user_gender, $user_gender_2 ); |
| 78 | } |
| 79 | |
| 80 | public function test_user_id_argument_with_id() { |
| 81 | $user_id = $this->factory()->user->create( |
| 82 | array( |
| 83 | 'gender' => 'male', |
| 84 | ) |
| 85 | ); |
| 86 | |
| 87 | $user_gender1 = get_user_gender( $user_id ); |
| 88 | |
| 89 | delete_user_meta( $user_id, 'gender' ); |
| 90 | |
| 91 | $user_gender2 = get_user_gender( $user_id ); |
| 92 | |
| 93 | $this->assertSame( 'male', $user_gender1 ); |
| 94 | $this->assertSame( 'unknown', $user_gender2 ); |
| 95 | } |
| 96 | |
| 97 | public function test_user_id_argument_with_wp_user_object() { |
| 98 | $user_id = $this->factory()->user->create( |
| 99 | array( |
| 100 | 'gender' => 'male', |
| 101 | ) |
| 102 | ); |
| 103 | |
| 104 | $user = get_user_by( 'id', $user_id ); |
| 105 | |
| 106 | $user_gender1 = get_user_gender( $user ); |
| 107 | |
| 108 | delete_user_meta( $user_id, 'gender' ); |
| 109 | |
| 110 | $user_gender2 = get_user_gender( $user ); |
| 111 | |
| 112 | $this->assertSame( 'male', $user_gender1 ); |
| 113 | $this->assertSame( 'unknown', $user_gender2 ); |
| 114 | } |
| 115 | |
| 116 | public function test_user_id_argument_with_nonexistent_user() { |
| 117 | global $wpdb; |
| 118 | |
| 119 | $user_id = $wpdb->get_var( "SELECT MAX(ID) FROM $wpdb->users" ) + 1; |
| 120 | |
| 121 | $user_gender = get_user_gender( $user_id ); |
| 122 | |
| 123 | $this->assertSame( 'unknown', $user_gender ); |
| 124 | } |
| 125 | |
| 126 | public function test_user_id_argument_with_invalid_type() { |
| 127 | $user_gender = get_user_gender( 'string' ); |
| 128 | $this->assertSame( 'unknown', $user_gender ); |
| 129 | } |
| 130 | } |