wordpress-develop/tests/phpunit/tests/xmlrpc/wp/getUsers.php
Sergey Biryukov 164b22cf6a Tests: First pass at using assertSame() instead of assertEquals() in most of the unit tests.
This ensures that not only the return values match the expected results, but also that their type is the same.

Going forward, stricter type checking by using `assertSame()` should generally be preferred to `assertEquals()` where appropriate, to make the tests more reliable.

Props johnbillion, jrf, SergeyBiryukov.
See #38266.

git-svn-id: https://develop.svn.wordpress.org/trunk@48937 602fd350-edb4-49c9-b593-d223f7449a82
2020-09-02 00:35:36 +00:00

125 lines
4.2 KiB
PHP

<?php
/**
* @group xmlrpc
* @group user
*/
class Tests_XMLRPC_wp_getUsers extends WP_XMLRPC_UnitTestCase {
function test_invalid_username_password() {
$results = $this->myxmlrpcserver->wp_getUsers( array( 1, 'username', 'password' ) );
$this->assertIXRError( $results );
$this->assertSame( 403, $results->code );
}
function test_incapable_user() {
$this->make_user_by_role( 'subscriber' );
$results = $this->myxmlrpcserver->wp_getUsers( array( 1, 'subscriber', 'subscriber' ) );
$this->assertIXRError( $results );
$this->assertSame( 401, $results->code );
}
function test_capable_user() {
$this->make_user_by_role( 'administrator' );
$result = $this->myxmlrpcserver->wp_getUsers( array( 1, 'administrator', 'administrator' ) );
$this->assertNotIXRError( $result );
// Check data types.
$this->assertInternalType( 'string', $result[0]['user_id'] );
$this->assertStringMatchesFormat( '%d', $result[0]['user_id'] );
$this->assertInternalType( 'string', $result[0]['username'] );
$this->assertInternalType( 'string', $result[0]['first_name'] );
$this->assertInternalType( 'string', $result[0]['last_name'] );
$this->assertInstanceOf( 'IXR_Date', $result[0]['registered'] );
$this->assertInternalType( 'string', $result[0]['bio'] );
$this->assertInternalType( 'string', $result[0]['email'] );
$this->assertInternalType( 'string', $result[0]['nickname'] );
$this->assertInternalType( 'string', $result[0]['nicename'] );
$this->assertInternalType( 'string', $result[0]['url'] );
$this->assertInternalType( 'string', $result[0]['display_name'] );
$this->assertInternalType( 'array', $result[0]['roles'] );
}
function test_invalid_role() {
$administrator_id = $this->make_user_by_role( 'administrator' );
if ( is_multisite() ) {
grant_super_admin( $administrator_id );
}
$filter = array( 'role' => 'invalidrole' );
$results = $this->myxmlrpcserver->wp_getUsers( array( 1, 'administrator', 'administrator', $filter ) );
$this->assertIXRError( $results );
$this->assertSame( 403, $results->code );
}
function test_role_filter() {
$author_id = $this->make_user_by_role( 'author' );
$editor_id = $this->make_user_by_role( 'editor' );
$administrator_id = $this->make_user_by_role( 'administrator' );
if ( is_multisite() ) {
grant_super_admin( $administrator_id );
}
// Test a single role ('editor').
$filter = array( 'role' => 'editor' );
$results = $this->myxmlrpcserver->wp_getUsers( array( 1, 'administrator', 'administrator', $filter ) );
$this->assertNotIXRError( $results );
$this->assertCount( 1, $results );
$this->assertEquals( $editor_id, $results[0]['user_id'] );
// Test 'authors', which should return all non-subscribers.
$filter2 = array( 'who' => 'authors' );
$results2 = $this->myxmlrpcserver->wp_getUsers( array( 1, 'administrator', 'administrator', $filter2 ) );
$this->assertNotIXRError( $results2 );
$this->assertCount( 3, array_intersect( array( $author_id, $editor_id, $administrator_id ), wp_list_pluck( $results2, 'user_id' ) ) );
}
function test_paging_filters() {
$administrator_id = $this->make_user_by_role( 'administrator' );
if ( is_multisite() ) {
grant_super_admin( $administrator_id );
}
self::factory()->user->create_many( 5 );
$user_ids = get_users( array( 'fields' => 'ID' ) );
$users_found = array();
$page_size = 2;
$filter = array(
'number' => $page_size,
'offset' => 0,
);
do {
$presults = $this->myxmlrpcserver->wp_getUsers( array( 1, 'administrator', 'administrator', $filter ) );
foreach ( $presults as $user ) {
$users_found[] = $user['user_id'];
}
$filter['offset'] += $page_size;
} while ( count( $presults ) > 0 );
// Verify that $user_ids matches $users_found.
$this->assertSame( 0, count( array_diff( $user_ids, $users_found ) ) );
}
function test_order_filters() {
$this->make_user_by_role( 'administrator' );
$filter = array(
'orderby' => 'email',
'order' => 'ASC',
);
$results = $this->myxmlrpcserver->wp_getUsers( array( 1, 'administrator', 'administrator', $filter ) );
$this->assertNotIXRError( $results );
$last_email = '';
foreach ( $results as $user ) {
$this->assertLessThanOrEqual( 0, strcmp( $last_email, $user['email'] ) );
$last_email = $user['email'];
}
}
}