wordpress-develop/tests/phpunit/tests/user/wpDropdownUsers.php
Sergey Biryukov c70fe62ed1 Tests: Replace assertContains() with assertStringContainsString() when used with strings.
Using the `assertContains()` and `assertNotContains()` methods with string haystacks was deprecated in PHPUnit 8 and removed in PHPUnit 9.

While WordPress test suite currently only supports PHPUnit up to 7.5.x, this allows us to switch to newer assertions ahead of adding full support for PHPUnit 8+.

These methods introduced in PHPUnit 7.5 should be used as an alternative:

* `assertStringContainsString()`
* `assertStringContainsStringIgnoringCase`
* `assertStringNotContainsString()`
* `assertStringNotContainsStringIgnoringCase`

As WordPress currently uses PHPUnit 5.7.x to run tests on PHP 5.6, polyfills for these methods were added to the `WP_UnitTestCase` class for PHPUnit < 7.5.

Follow-up to [51331], [51451], [51461].

Props jrf, dd32, SergeyBiryukov.
See #53363, #46149.

git-svn-id: https://develop.svn.wordpress.org/trunk@51462 602fd350-edb4-49c9-b593-d223f7449a82
2021-07-19 14:00:11 +00:00

205 lines
4.7 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->assertStringContainsString( $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->assertStringContainsString( $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->assertStringContainsString( $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->assertStringContainsString( $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->assertStringContainsString( $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->assertStringNotContainsString( (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->assertStringNotContainsString( $u1->user_login, $found );
$this->assertStringContainsString( $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->assertStringNotContainsString( $u1->user_login, $found );
$this->assertStringContainsString( $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->assertStringNotContainsString( $u1->user_login, $found );
$this->assertStringContainsString( $u2->user_login, $found );
}
}