Skip to content

Commit

Permalink
Merge pull request #193 from humanmade/support-user-objects
Browse files Browse the repository at this point in the history
Support WP_User objects
  • Loading branch information
faisal-alvi authored Feb 3, 2023
2 parents 95d3f15 + 60063d5 commit b868527
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions includes/class-simple-local-avatars.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ public function get_user_id( $id_or_email ) {
$user_id = (int) $id_or_email;
} elseif ( is_object( $id_or_email ) && ! empty( $id_or_email->user_id ) ) {
$user_id = (int) $id_or_email->user_id;
} elseif ( $id_or_email instanceof WP_User ) {
$user_id = $id_or_email->ID;
} elseif ( $id_or_email instanceof WP_Post && ! empty( $id_or_email->post_author ) ) {
$user_id = (int) $id_or_email->post_author;
} elseif ( is_string( $id_or_email ) ) {
Expand Down
24 changes: 20 additions & 4 deletions tests/phpunit/SimpleLocalAvatarsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ public function setUp(): void {
'X' => __( 'X — Even more mature than above', 'simple-local-avatars' ),
) );

$user = (object) [
'ID' => 1,
'display_name' => 'TEST_USER',
];
$user = Mockery::mock( WP_User::class );
$user->ID = 1;
$user->display_name = 'TEST_USER';

// Init $POST.
$_POST = array();
Expand Down Expand Up @@ -489,4 +488,21 @@ public function test_avatar_delete() {

$this->instance->avatar_delete( 1 );
}

public function test_get_user_id() {
$this->assertEquals( 1, $this->instance->get_user_id( '1' ) );
$this->assertEquals( 1, $this->instance->get_user_id( 'test@example.com' ) );

$user = Mockery::mock( WP_User::class );
$user->ID = 1;
$this->assertEquals( 1, $this->instance->get_user_id( $user ) );

$post = Mockery::mock( WP_Post::class );
$post->post_author = 1;
$this->assertEquals( 1, $this->instance->get_user_id( $post ) );

$comment = Mockery::mock( WP_Comment::class );
$comment->user_id = '1';
$this->assertEquals( 1, $this->instance->get_user_id( $comment ) );
}
}

0 comments on commit b868527

Please sign in to comment.