wordpress-develop/tests/phpunit/tests/user/wpDropdownUsers.php
Sergey Biryukov 7d15225ad0 Users: Check if the user ID passed as selected to wp_dropdown_users() corresponds to an existing user.
This avoids a few PHP notices if the `include_selected` parameter was specified and a non-existing user ID was passed.

Props campusboy1987.
Fixes #51370.

git-svn-id: https://develop.svn.wordpress.org/trunk@49036 602fd350-edb4-49c9-b593-d223f7449a82
2020-09-23 00:25:47 +00:00

205 lines
4.5 KiB
PHP

<?php
/**
* Test functions in wp-includes/user.php
*
* @group user
*/
class Tests_User_WpDropdownUsers extends WP_UnitTestCase {
/**
* @ticket 31251
*/
public function test_default_value_of_show_should_be_display_name() {
// Create a user with a different display_name.
$u = $this->factory->user->create(
array(
'user_login' => 'foo',
'display_name' => 'Foo Person',
)
);
$found = wp_dropdown_users(
array(
'echo' => false,
)
);
$expected = "<option value='$u'>Foo Person</option>";
$this->assertContains( $expected, $found );
}
/**
* @ticket 31251
*/
public function test_show_should_display_display_name_show_is_specified_as_empty() {
// Create a user with a different display_name.
$u = $this->factory->user->create(
array(
'user_login' => 'foo',
'display_name' => 'Foo Person',
)
);
// Get the result of a non-default, but acceptable input for 'show' parameter to wp_dropdown_users().
$found = wp_dropdown_users(
array(
'echo' => false,
'show' => '',
)
);
$expected = "<option value='$u'>Foo Person</option>";
$this->assertContains( $expected, $found );
}
/**
* @ticket 31251
*/
public function test_show_should_display_user_property_when_the_value_of_show_is_a_valid_user_property() {
// Create a user with a different display_name.
$u = $this->factory->user->create(
array(
'user_login' => 'foo',
'display_name' => 'Foo Person',
)
);
// Get the result of a non-default, but acceptable input for 'show' parameter to wp_dropdown_users().
$found = wp_dropdown_users(
array(
'echo' => false,
'show' => 'user_login',
)
);
$expected = "<option value='$u'>foo</option>";
$this->assertContains( $expected, $found );
}
/**
* @ticket 31251
*/
public function test_show_display_name_with_login() {
// Create a user with a different display_name.
$u = $this->factory->user->create(
array(
'user_login' => 'foo',
'display_name' => 'Foo Person',
)
);
// Get the result of a non-default, but acceptable input for 'show' parameter to wp_dropdown_users().
$found = wp_dropdown_users(
array(
'echo' => false,
'show' => 'display_name_with_login',
)
);
$expected = "<option value='$u'>Foo Person (foo)</option>";
$this->assertContains( $expected, $found );
}
/**
* @ticket 31251
*/
public function test_include_selected() {
$users = self::factory()->user->create_many( 2 );
$found = wp_dropdown_users(
array(
'echo' => false,
'include' => $users[0],
'selected' => $users[1],
'include_selected' => true,
'show' => 'user_login',
)
);
$user1 = get_userdata( $users[1] );
$this->assertContains( $user1->user_login, $found );
}
/**
* @ticket 51370
*/
public function test_include_selected_with_non_existing_user_id() {
$found = wp_dropdown_users(
array(
'echo' => false,
'selected' => PHP_INT_MAX,
'include_selected' => true,
'show' => 'user_login',
)
);
$this->assertNotContains( (string) PHP_INT_MAX, $found );
}
/**
* @ticket 38135
*/
public function test_role() {
$u1 = self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) );
$u2 = self::factory()->user->create_and_get( array( 'role' => 'author' ) );
$found = wp_dropdown_users(
array(
'echo' => false,
'role' => 'author',
'show' => 'user_login',
)
);
$this->assertNotContains( $u1->user_login, $found );
$this->assertContains( $u2->user_login, $found );
}
/**
* @ticket 38135
*/
public function test_role__in() {
$u1 = self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) );
$u2 = self::factory()->user->create_and_get( array( 'role' => 'author' ) );
$found = wp_dropdown_users(
array(
'echo' => false,
'role__in' => array( 'author', 'editor' ),
'show' => 'user_login',
)
);
$this->assertNotContains( $u1->user_login, $found );
$this->assertContains( $u2->user_login, $found );
}
/**
* @ticket 38135
*/
public function test_role__not_in() {
$u1 = self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) );
$u2 = self::factory()->user->create_and_get( array( 'role' => 'author' ) );
$found = wp_dropdown_users(
array(
'echo' => false,
'role__not_in' => array( 'subscriber', 'editor' ),
'show' => 'user_login',
)
);
$this->assertNotContains( $u1->user_login, $found );
$this->assertContains( $u2->user_login, $found );
}
}