Make WordPress Core

Ticket #18428: 18428.alt_unittests.patch

File 18428.alt_unittests.patch, 14.4 KB (added by maxcutler, 13 years ago)
  • tests/xmlrpc/wp/editUserInfo.php

     
     1<?php
     2
     3/**
     4 * @group xmlrpc
     5 * @group user
     6 */
     7class TestXMLRPCServer_wp_editUserInfo extends WP_XMLRPC_UnitTestCase {
     8
     9        function test_invalid_username_password() {
     10                $result = $this->myxmlrpcserver->wp_editUserInfo( array( 1, 'username', 'password', array() ) );
     11                $this->assertInstanceOf( 'IXR_Error', $result );
     12                $this->assertEquals( 403, $result->code );
     13        }
     14
     15    function test_subscriber_profile() {
     16        $subscriber_id = $this->make_user_by_role( 'subscriber' );
     17
     18        $new_data = array(
     19            'first_name' => rand_str(),
     20            'last_name' => rand_str(),
     21            'website' => 'http://www.example.org/subscriber',
     22            'display_name' => rand_str(),
     23            'nickname' => rand_str(),
     24            'nicename' => rand_str(),
     25            'bio' => rand_str(200)
     26        );
     27        $result = $this->myxmlrpcserver->wp_editUserInfo( array( 1, 'subscriber', 'subscriber', $new_data ) );
     28        $this->assertNotInstanceOf( 'IXR_Error', $result );
     29        $this->assertTrue( $result );
     30
     31        // verify that the new values were stored
     32        $user_data = get_userdata( $subscriber_id );
     33        $this->assertEquals( $new_data['first_name'], $user_data->first_name );
     34        $this->assertEquals( $new_data['last_name'], $user_data->last_name );
     35        $this->assertEquals( $new_data['website'], $user_data->user_url );
     36        $this->assertEquals( $new_data['display_name'], $user_data->display_name );
     37        $this->assertEquals( $new_data['nickname'], $user_data->nickname );
     38        $this->assertEquals( $new_data['nicename'], $user_data->user_nicename );
     39        $this->assertEquals( $new_data['bio'], $user_data->description );
     40    }
     41
     42    function test_ignore_password_change() {
     43        $this->make_user_by_role( 'author' );
     44        $new_pass = rand_str();
     45        $new_data = array( 'password' => $new_pass );
     46
     47        $result = $this->myxmlrpcserver->wp_editUserInfo( array( 1, 'author', 'author', $new_data ) );
     48        $this->assertNotInstanceOf( 'IXR_Error', $result );
     49        $this->assertTrue( $result );
     50
     51        $auth_old = wp_authenticate( 'author', 'author' );
     52        $auth_new = wp_authenticate( 'author', $new_pass );
     53        $this->assertInstanceOf( 'WP_User', $auth_old );
     54        $this->assertTrue( is_wp_error( $auth_new ) );
     55    }
     56
     57    function test_ignore_email_change() {
     58        $editor_id = $this->make_user_by_role( 'editor' );
     59        $new_email = rand_str() . '@example.com';
     60        $new_data = array( 'email' => $new_email );
     61
     62        $result = $this->myxmlrpcserver->wp_editUserInfo( array( 1, 'editor', 'editor', $new_data ) );
     63        $this->assertNotInstanceOf( 'IXR_Error', $result );
     64        $this->assertTrue( $result );
     65
     66        $user_data = get_userdata( $editor_id );
     67        $this->assertNotEquals( $new_email, $user_data->email );
     68    }
     69}
  • tests/xmlrpc/wp/getUser.php

     
     1<?php
     2
     3/**
     4 * @group xmlrpc
     5 * @group user
     6 */
     7class TestXMLRPCServer_wp_getUser extends WP_XMLRPC_UnitTestCase {
     8
     9        function test_invalid_username_password() {
     10                $result = $this->myxmlrpcserver->wp_getUser( array( 1, 'username', 'password', 1 ) );
     11                $this->assertInstanceOf( 'IXR_Error', $result );
     12                $this->assertEquals( 403, $result->code );
     13        }
     14
     15        function test_invalid_user() {
     16                $this->make_user_by_role( 'administrator' );
     17
     18                $result = $this->myxmlrpcserver->wp_getUser( array( 1, 'administrator', 'administrator', 34902348908234 ) );
     19                $this->assertInstanceOf( 'IXR_Error', $result );
     20                $this->assertEquals( 404, $result->code );
     21        }
     22
     23        function test_incapable_user() {
     24                $this->make_user_by_role( 'subscriber' );
     25                $editor_id = $this->make_user_by_role( 'editor' );
     26
     27                $result = $this->myxmlrpcserver->wp_getUser( array( 1, 'subscriber', 'subscriber', $editor_id ) );
     28                $this->assertInstanceOf( 'IXR_Error', $result );
     29                $this->assertEquals( 401, $result->code );
     30        }
     31
     32        function test_subscriber_self() {
     33                $subscriber_id = $this->make_user_by_role( 'subscriber' );
     34
     35                $result = $this->myxmlrpcserver->wp_getUser( array( 1, 'subscriber', 'subscriber', $subscriber_id ) );
     36                $this->assertNotInstanceOf( 'IXR_Error', $result );
     37                $this->assertEquals( $subscriber_id, $result['user_id'] );
     38        }
     39
     40        function test_valid_user() {
     41                $this->make_user_by_role( 'administrator' );
     42
     43                $registered_date = strtotime( '-1 day' );
     44                $user_data = array(
     45                        'user_login' => 'getusertestuser',
     46                        'user_pass' => rand_str(),
     47                        'first_name' => rand_str(),
     48                        'last_name' => rand_str(),
     49                        'description' => rand_str( 100 ),
     50                        'user_email' => 'getUserTestUser@example.com',
     51                        'nickname' => rand_str(),
     52                        'user_nicename' => rand_str(),
     53                        'display_name' => rand_str(),
     54                        'user_url' => 'http://www.example.com/testuser',
     55                        'role' => 'author',
     56                        'aim' => rand_str(),
     57                        'user_registered' => strftime( "%Y-%m-%d %H:%M:%S", $registered_date )
     58                );
     59                $user_id = wp_insert_user( $user_data );
     60
     61                $result = $this->myxmlrpcserver->wp_getUser( array( 1, 'administrator', 'administrator', $user_id ) );
     62                $this->assertNotInstanceOf( 'IXR_Error', $result );
     63
     64                // check data types
     65                $this->assertInternalType( 'string', $result['user_id'] );
     66                $this->assertStringMatchesFormat( '%d', $result['user_id'] );
     67                $this->assertInternalType( 'string', $result['username'] );
     68                $this->assertInternalType( 'string', $result['first_name'] );
     69                $this->assertInternalType( 'string', $result['last_name'] );
     70                $this->assertInstanceOf( 'IXR_Date', $result['registered'] );
     71                $this->assertInternalType( 'string', $result['bio'] );
     72                $this->assertInternalType( 'string', $result['email'] );
     73                $this->assertInternalType( 'string', $result['nickname'] );
     74                $this->assertInternalType( 'string', $result['nicename'] );
     75                $this->assertInternalType( 'string', $result['url'] );
     76                $this->assertInternalType( 'string', $result['display_name'] );
     77                $this->assertInternalType( 'array', $result['capabilities'] );
     78                $this->assertInternalType( 'array', $result['roles'] );
     79
     80                // check expected values
     81                $this->assertEquals( $user_id, $result['user_id'] );
     82                $this->assertEquals( $user_data['user_login'], $result['username'] );
     83                $this->assertEquals( $user_data['first_name'], $result['first_name'] );
     84                $this->assertEquals( $user_data['last_name'], $result['last_name'] );
     85                $this->assertEquals( $registered_date, $result['registered']->getTimestamp() );
     86                $this->assertEquals( $user_data['description'], $result['bio'] );
     87                $this->assertEquals( $user_data['user_email'], $result['email'] );
     88                $this->assertEquals( $user_data['nickname'], $result['nickname'] );
     89                $this->assertEquals( $user_data['user_nicename'], $result['nicename'] );
     90                $this->assertEquals( $user_data['user_url'], $result['url'] );
     91                $this->assertEquals( $user_data['display_name'], $result['display_name'] );
     92                $this->assertArrayHasKey( $user_data['role'], $result['capabilities'] );
     93                $this->assertEquals( 1, $result['capabilities'][$user_data['role']] );
     94                $this->assertEquals( $user_data['user_login'], $result['username'] );
     95                $this->assertContains( $user_data['role'], $result['roles'] );
     96
     97                wp_delete_user( $user_id );
     98        }
     99
     100        function test_no_fields() {
     101                $editor_id = $this->make_user_by_role( 'editor' );
     102                $this->make_user_by_role( 'administrator' );
     103
     104                $result = $this->myxmlrpcserver->wp_getUser( array( 1, 'administrator', 'administrator', $editor_id, array() ) );
     105                $this->assertNotInstanceOf( 'IXR_Error', $result );
     106                $this->assertEquals( $editor_id, $result['user_id'] );
     107
     108                $expected_fields = array( 'user_id' );
     109                $this->assertEquals( $expected_fields, array_keys( $result ) );
     110        }
     111
     112        function test_basic_fields() {
     113                $editor_id = $this->make_user_by_role( 'editor' );
     114                $this->make_user_by_role( 'administrator' );
     115
     116                $result = $this->myxmlrpcserver->wp_getUser( array( 1, 'administrator', 'administrator', $editor_id, array( 'basic' ) ) );
     117                $this->assertNotInstanceOf( 'IXR_Error', $result );
     118                $this->assertEquals( $editor_id, $result['user_id'] );
     119
     120                $expected_fields = array( 'user_id', 'username', 'email', 'registered', 'display_name', 'nicename' );
     121                $this->assertEquals( sort( $expected_fields ), sort( array_keys( $result ) ) );
     122        }
     123
     124        function test_arbitrary_fields() {
     125                $editor_id = $this->make_user_by_role( 'editor' );
     126                $this->make_user_by_role( 'administrator' );
     127
     128                $fields = array( 'email', 'bio', 'user_contacts' );
     129
     130                $result = $this->myxmlrpcserver->wp_getUser( array( 1, 'administrator', 'administrator', $editor_id, $fields ) );
     131                $this->assertNotInstanceOf( 'IXR_Error', $result );
     132                $this->assertEquals( $editor_id, $result['user_id'] );
     133
     134                $expected_fields = array_merge( array( 'user_id' ), $fields );
     135                $this->assertEquals( sort( $expected_fields ), sort( array_keys( $result ) ) );
     136        }
     137}
  • tests/xmlrpc/wp/getUserInfo.php

     
     1<?php
     2
     3/**
     4 * @group xmlrpc
     5 * @group user
     6 */
     7class TestXMLRPCServer_wp_getUserInfo extends WP_XMLRPC_UnitTestCase {
     8
     9        function test_invalid_username_password() {
     10                $result = $this->myxmlrpcserver->wp_getUserInfo( array( 1, 'username', 'password' ) );
     11                $this->assertInstanceOf( 'IXR_Error', $result );
     12                $this->assertEquals( 403, $result->code );
     13        }
     14
     15        function test_subscriber() {
     16                $subscriber_id = $this->make_user_by_role( 'subscriber' );
     17
     18                $result = $this->myxmlrpcserver->wp_getUserInfo( array( 1, 'subscriber', 'subscriber' ) );
     19                $this->assertNotInstanceOf( 'IXR_Error', $result );
     20                $this->assertEquals( $subscriber_id, $result['user_id'] );
     21                $this->assertContains( 'subscriber', $result['roles'] );
     22        }
     23
     24        function test_administrator() {
     25                $administrator_id = $this->make_user_by_role( 'administrator' );
     26
     27                $result = $this->myxmlrpcserver->wp_getUserInfo( array( 1, 'administrator', 'administrator' ) );
     28                $this->assertNotInstanceOf( 'IXR_Error', $result );
     29                $this->assertEquals( $administrator_id, $result['user_id'] );
     30                $this->assertContains( 'administrator', $result['roles'] );
     31        }
     32
     33        function test_arbitrary_fields() {
     34                $editor_id = $this->make_user_by_role( 'editor' );
     35
     36                $fields = array( 'email', 'bio', 'user_contacts' );
     37
     38                $result = $this->myxmlrpcserver->wp_getUserInfo( array( 1, 'editor', 'editor', $fields ) );
     39                $this->assertNotInstanceOf( 'IXR_Error', $result );
     40                $this->assertEquals( $editor_id, $result['user_id'] );
     41
     42                $expected_fields = array_merge( array( 'user_id' ), $fields );
     43                $this->assertEquals( sort( $expected_fields ), sort( array_keys( $result ) ) );
     44        }
     45}
  • tests/xmlrpc/wp/getUsers.php

     
     1<?php
     2
     3/**
     4 * @group xmlrpc
     5 * @group user
     6 */
     7class TestXMLRPCServer_wp_getUsers extends WP_XMLRPC_UnitTestCase {
     8
     9        function test_invalid_username_password() {
     10                $results = $this->myxmlrpcserver->wp_getUsers( array( 1, 'username', 'password' ) );
     11                $this->assertInstanceOf( 'IXR_Error', $results );
     12                $this->assertEquals( 403, $results->code );
     13        }
     14
     15        function test_incapable_user() {
     16                $this->make_user_by_role( 'subscriber' );
     17
     18                $results = $this->myxmlrpcserver->wp_getUsers( array( 1, 'subscriber', 'subscriber' ) );
     19                $this->assertInstanceOf( 'IXR_Error', $results );
     20                $this->assertEquals( 401, $results->code );
     21        }
     22
     23        function test_capable_user() {
     24                $this->make_user_by_role( 'administrator' );
     25
     26                $results = $this->myxmlrpcserver->wp_getUsers( array( 1, 'administrator', 'administrator' ) );
     27                $this->assertNotInstanceOf( 'IXR_Error', $results );
     28        }
     29
     30        function test_invalid_role() {
     31                $this->make_user_by_role( 'administrator' );
     32
     33                $filter = array( 'role' => rand_str() );
     34                $results = $this->myxmlrpcserver->wp_getUsers( array( 1, 'administrator', 'administrator', $filter ) );
     35                $this->assertInstanceOf( 'IXR_Error', $results );
     36                $this->assertEquals( 403, $results->code );
     37        }
     38
     39        function test_role_filter() {
     40                $author_id = $this->make_user_by_role( 'author' );
     41                $editor_id = $this->make_user_by_role( 'editor' );
     42                $administrator_id = $this->make_user_by_role( 'administrator' );
     43
     44                // test a single role ('editor')
     45                $filter = array( 'role' => 'editor' );
     46                $results = $this->myxmlrpcserver->wp_getUsers( array( 1, 'administrator', 'administrator', $filter ) );
     47                $this->assertNotInstanceOf( 'IXR_Error', $results );
     48                $this->assertCount( 1, $results );
     49                $this->assertEquals( $editor_id, $results[0]['user_id'] );
     50
     51                // test 'authors', which should return all non-subscribers
     52                // (see 'who' => 'authors' param to WP_User_Query)
     53                $filter2 = array( 'role' => 'authors' );
     54                $results2 = $this->myxmlrpcserver->wp_getUsers( array( 1, 'administrator', 'administrator', $filter2 ) );
     55                $this->assertNotInstanceOf( 'IXR_Error', $results2 );
     56                $this->assertCount( 3, array_intersect( array( $author_id, $editor_id, $administrator_id ), wp_list_pluck( $results2, 'user_id' ) ) );
     57        }
     58
     59        function test_paging_filters() {
     60                $this->make_user_by_role( 'administrator' );
     61                $this->factory->user->create_many( 13 );
     62
     63                $user_ids = get_users( array( 'fields' => 'ID' ) );
     64
     65                $users_found = array();
     66                $page_size = floor( count( $user_ids ) / 3 );
     67
     68                $filter = array( 'number' => $page_size, 'offset' => 0 );
     69                do {
     70                        $presults = $this->myxmlrpcserver->wp_getUsers( array( 1, 'administrator', 'administrator', $filter ) );
     71                        foreach ( $presults as $user ) {
     72                                $users_found[] = $user['user_id'];
     73                        }
     74                        $filter['offset'] += $page_size;
     75                } while ( count( $presults ) > 0 );
     76
     77                // verify that $user_ids matches $users_found
     78                $this->assertEquals( 0, count( array_diff( $user_ids, $users_found ) ) );
     79        }
     80
     81        function test_order_filters() {
     82                $this->make_user_by_role( 'administrator' );
     83
     84                $filter = array( 'orderby' => 'email', 'order' => 'ASC' );
     85                $results = $this->myxmlrpcserver->wp_getUsers( array( 1, 'administrator', 'administrator', $filter ) );
     86                $this->assertNotInstanceOf( 'IXR_Error', $results );
     87
     88                $last_email = '';
     89                foreach ( $results as $user ) {
     90                        $this->assertLessThanOrEqual( 0, strcmp( $last_email, $user['email'] ) );
     91                        $last_email = $user['email'];
     92                }
     93        }
     94}