Make WordPress Core

Ticket #18428: 18428.combo_unittests.2.patch

File 18428.combo_unittests.2.patch, 17.1 KB (added by maxcutler, 13 years ago)
  • tests/xmlrpc/wp/deleteUser.php

     
     1<?php
     2
     3/**
     4 * @group xmlrpc
     5 * @group user
     6 */
     7class TestXMLRPCServer_wp_deleteUser extends WP_XMLRPC_UnitTestCase {
     8
     9        protected $administrator_id;
     10
     11        function setUp() {
     12                parent::setUp();
     13
     14                // to delete users in multisite, you must be a superadmin.
     15                // so promote the administrator user to super_admin for these tests
     16                $this->administrator_id = $this->make_user_by_role( 'administrator' );
     17                if ( is_multisite() )
     18                        grant_super_admin( $this->administrator_id );
     19        }
     20
     21        function tearDown() {
     22                if ( is_multisite() )
     23                        revoke_super_admin( $this->administrator_id );
     24
     25                parent::tearDown();
     26        }
     27
     28        function test_invalid_username_password() {
     29                $result = $this->myxmlrpcserver->wp_deleteUser( array( 1, 'username', 'password', 1 ) );
     30                $this->assertInstanceOf( 'IXR_Error', $result );
     31                $this->assertEquals( 403, $result->code );
     32        }
     33
     34        function test_incapable_user() {
     35                $this->make_user_by_role( 'author' );
     36
     37                $result = $this->myxmlrpcserver->wp_deleteUser( array( 1, 'author', 'author', 1 ) );
     38                $this->assertInstanceOf( 'IXR_Error', $result );
     39                $this->assertEquals( 401, $result->code );
     40        }
     41
     42        function test_invalid_user() {
     43                $result = $this->myxmlrpcserver->wp_deleteUser( array( 1, 'administrator', 'administrator', 3492083940823 ) );
     44                $this->assertInstanceOf( 'IXR_Error', $result );
     45                $this->assertEquals( 404, $result->code );
     46        }
     47
     48        function test_delete_self() {
     49                $result = $this->myxmlrpcserver->wp_deleteUser( array( 1, 'administrator', 'administrator', $this->administrator_id ) );
     50                $this->assertInstanceOf( 'IXR_Error', $result );
     51                $this->assertEquals( 401, $result->code );
     52        }
     53
     54        function test_invalid_reassign_user() {
     55                $author_id = $this->make_user_by_role( 'author' );
     56
     57                $result = $this->myxmlrpcserver->wp_deleteUser( array( 1, 'administrator', 'administrator', $author_id, 3908423098423 ) );
     58                $this->assertInstanceOf( 'IXR_Error', $result );
     59                $this->assertEquals( 404, $result->code );
     60        }
     61
     62        function test_reassign_same() {
     63                $author_id = $this->make_user_by_role( 'author' );
     64
     65                $result = $this->myxmlrpcserver->wp_deleteUser( array( 1, 'administrator', 'administrator', $author_id, $author_id ) );
     66                $this->assertInstanceOf( 'IXR_Error', $result );
     67                $this->assertEquals( 401, $result->code );
     68        }
     69
     70        function test_valid_delete() {
     71                $user_id = $this->make_user_by_role( 'author' );
     72
     73                // make some posts for the user
     74                $num_posts = rand( 3, 10 );
     75                $post_ids = $this->factory->post->create_many( $num_posts, array( 'post_author' => $user_id ) );
     76
     77                $result = $this->myxmlrpcserver->wp_deleteUser( array( 1, 'administrator', 'administrator', $user_id ) );
     78                $this->assertNotInstanceOf( 'IXR_Error', $result );
     79
     80                // verify user was deleted
     81                if ( is_multisite() ) {
     82                        $this->assertEquals( 0, $result['user_status'] );
     83                } else {
     84                        $this->assertFalse( get_user_by( 'id', $user_id ) );
     85                }
     86
     87                // since reassign wasn't used, the user's posts should have been trashed
     88                foreach ( $post_ids as $post_id ) {
     89                        $post = get_post( $post_id );
     90                        $this->assertEquals( 'trash', $post->post_status );
     91                }
     92        }
     93
     94        function test_valid_delete_reassign() {
     95                $user_id = $this->make_user_by_role( 'author' );
     96                $reassign_id = $this->factory->user->create( array(
     97                        'user_login' => 'author2',
     98                        'user_pass'  => 'author2',
     99                        'role'       => 'author'
     100                ));
     101
     102                // make some posts for the user
     103                $num_posts = rand( 3, 10 );
     104                $post_ids = $this->factory->post->create_many( $num_posts, array( 'post_author' => $user_id ) );
     105
     106                $result = $this->myxmlrpcserver->wp_deleteUser( array( 1, 'administrator', 'administrator', $user_id, $reassign_id ) );
     107                $this->assertNotInstanceOf( 'IXR_Error', $result );
     108
     109                // verify user was deleted
     110                if ( is_multisite() ) {
     111                        $this->assertEquals( 0, $result['user_status'] );
     112                } else {
     113                        $this->assertFalse( get_user_by( 'id', $user_id ) );
     114                }
     115
     116                // verify posts were reassigned to other author
     117                foreach ( $post_ids as $post_id ) {
     118                        $post = get_post( $post_id );
     119                        $this->assertEquals( $reassign_id, $post->post_author );
     120                        $this->assertEquals( 'publish', $post->post_status );
     121                }
     122        }
     123}
  • tests/xmlrpc/wp/editUser.php

     
     1<?php
     2
     3/**
     4 * @group xmlrpc
     5 * @group user
     6 */
     7class TestXMLRPCServer_wp_editUser extends WP_XMLRPC_UnitTestCase {
     8
     9        function test_invalid_username_password() {
     10                $result = $this->myxmlrpcserver->wp_editUser( array( 1, 'username', 'password', 1, array() ) );
     11                $this->assertInstanceOf( 'IXR_Error', $result );
     12                $this->assertEquals( 403, $result->code );
     13        }
     14}
  • 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                $this->assertInternalType( 'array', $result['user_contacts'] );
     80
     81                // check expected values
     82                $this->assertEquals( $user_id, $result['user_id'] );
     83                $this->assertEquals( $user_data['user_login'], $result['username'] );
     84                $this->assertEquals( $user_data['first_name'], $result['first_name'] );
     85                $this->assertEquals( $user_data['last_name'], $result['last_name'] );
     86                $this->assertEquals( $registered_date, $result['registered']->getTimestamp() );
     87                $this->assertEquals( $user_data['description'], $result['bio'] );
     88                $this->assertEquals( $user_data['user_email'], $result['email'] );
     89                $this->assertEquals( $user_data['nickname'], $result['nickname'] );
     90                $this->assertEquals( $user_data['user_nicename'], $result['nicename'] );
     91                $this->assertEquals( $user_data['user_url'], $result['url'] );
     92                $this->assertEquals( $user_data['display_name'], $result['display_name'] );
     93                $this->assertArrayHasKey( $user_data['role'], $result['capabilities'] );
     94                $this->assertEquals( 1, $result['capabilities'][$user_data['role']] );
     95                $this->assertEquals( $user_data['user_login'], $result['username'] );
     96                $this->assertContains( $user_data['role'], $result['roles'] );
     97                $this->assertArrayHasKey( 'aim', $result['user_contacts'] );
     98                $this->assertEquals( $user_data['aim'], $result['user_contacts']['aim'] );
     99
     100                wp_delete_user( $user_id );
     101        }
     102
     103        function test_no_fields() {
     104                $editor_id = $this->make_user_by_role( 'editor' );
     105                $this->make_user_by_role( 'administrator' );
     106
     107                $result = $this->myxmlrpcserver->wp_getUser( array( 1, 'administrator', 'administrator', $editor_id, array() ) );
     108                $this->assertNotInstanceOf( 'IXR_Error', $result );
     109                $this->assertEquals( $editor_id, $result['user_id'] );
     110
     111                $expected_fields = array( 'user_id' );
     112                $this->assertEquals( $expected_fields, array_keys( $result ) );
     113        }
     114
     115        function test_basic_fields() {
     116                $editor_id = $this->make_user_by_role( 'editor' );
     117                $this->make_user_by_role( 'administrator' );
     118
     119                $result = $this->myxmlrpcserver->wp_getUser( array( 1, 'administrator', 'administrator', $editor_id, array( 'basic' ) ) );
     120                $this->assertNotInstanceOf( 'IXR_Error', $result );
     121                $this->assertEquals( $editor_id, $result['user_id'] );
     122
     123                $expected_fields = array( 'user_id', 'username', 'email', 'registered', 'display_name', 'nicename' );
     124                $this->assertEquals( sort( $expected_fields ), sort( array_keys( $result ) ) );
     125        }
     126
     127        function test_arbitrary_fields() {
     128                $editor_id = $this->make_user_by_role( 'editor' );
     129                $this->make_user_by_role( 'administrator' );
     130
     131                $fields = array( 'email', 'bio', 'user_contacts' );
     132
     133                $result = $this->myxmlrpcserver->wp_getUser( array( 1, 'administrator', 'administrator', $editor_id, $fields ) );
     134                $this->assertNotInstanceOf( 'IXR_Error', $result );
     135                $this->assertEquals( $editor_id, $result['user_id'] );
     136
     137                $expected_fields = array_merge( array( 'user_id' ), $fields );
     138                $this->assertEquals( sort( $expected_fields ), sort( array_keys( $result ) ) );
     139        }
     140}
  • 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}
     95 No newline at end of file
  • tests/xmlrpc/wp/newUser.php

     
     1<?php
     2
     3/**
     4 * @group xmlrpc
     5 * @group user
     6 */
     7class TestXMLRPCServer_wp_newUser extends WP_XMLRPC_UnitTestCase {
     8
     9        function test_invalid_username_password() {
     10                $result = $this->myxmlrpcserver->wp_newUser( array( 1, 'username', 'password', array() ) );
     11                $this->assertInstanceOf( 'IXR_Error', $result );
     12                $this->assertEquals( 403, $result->code );
     13        }
     14}