| | 1 | <?php |
| | 2 | include_once(ABSPATH . 'wp-admin/includes/admin.php'); |
| | 3 | include_once(ABSPATH . WPINC . '/class-IXR.php'); |
| | 4 | include_once(ABSPATH . WPINC . '/class-wp-xmlrpc-server.php'); |
| | 5 | |
| | 6 | class TestXMLRPCServer_wp_editPost extends WPTestCase { |
| | 7 | var $user_ids = array(); |
| | 8 | |
| | 9 | function setUp() { |
| | 10 | |
| | 11 | parent::setUp(); |
| | 12 | // keep track of users we create |
| | 13 | $this->user_ids = array(); |
| | 14 | $this->_flush_roles(); |
| | 15 | |
| | 16 | $this->orig_users = get_users_of_blog(); |
| | 17 | add_filter( 'pre_option_enable_xmlrpc', '__return_true' ); |
| | 18 | |
| | 19 | $this->_make_user( 'subscriber', 'subscriber', 'subscriber' ); |
| | 20 | $this->_make_user( 'contributor', 'contributor', 'contributor' ); |
| | 21 | $this->_make_user( 'author', 'author', 'author' ); |
| | 22 | $this->_make_user( 'editor', 'editor', 'editor' ); |
| | 23 | |
| | 24 | $this->myxmlrpcserver = new wp_xmlrpc_server(); |
| | 25 | } |
| | 26 | |
| | 27 | function tearDown() { |
| | 28 | parent::tearDown(); |
| | 29 | // delete any users that were created during tests |
| | 30 | foreach ($this->user_ids as $id) |
| | 31 | wp_delete_user($id); |
| | 32 | |
| | 33 | remove_filter( 'pre_option_enable_xmlrpc', '__return_true' ); |
| | 34 | } |
| | 35 | |
| | 36 | function _flush_roles() { |
| | 37 | // we want to make sure we're testing against the db, not just in-memory data |
| | 38 | // this will flush everything and reload it from the db |
| | 39 | unset( $GLOBALS['wp_user_roles'] ); |
| | 40 | } |
| | 41 | |
| | 42 | function test_invalid_username_password() { |
| | 43 | $result = $this->myxmlrpcserver->wp_editPost( array( 1, 'username', 'password' ) ); |
| | 44 | $this->assertInstanceOf( 'IXR_Error', $result ); |
| | 45 | $this->assertEquals( 403, $result->code ); |
| | 46 | } |
| | 47 | |
| | 48 | function test_edit_own_post() { |
| | 49 | $contributor_id = get_user_by( 'login', 'contributor' )->ID; |
| | 50 | $post = array( 'post_title' => 'Post test', 'post_author' => $contributor_id ); |
| | 51 | $post_id = wp_insert_post( $post ); |
| | 52 | |
| | 53 | $new_title = 'Post test (updated)'; |
| | 54 | $post2 = array( 'post_title' => $new_title ); |
| | 55 | $result = $this->myxmlrpcserver->wp_editPost( array( 1, 'contributor', 'contributor', $post_id, $post2 ) ); |
| | 56 | $this->assertNotInstanceOf( 'IXR_Error', $result ); |
| | 57 | $this->assertTrue($result); |
| | 58 | |
| | 59 | $out = wp_get_single_post( $post_id ); |
| | 60 | $this->assertEquals( $new_title, $out->post_title ); |
| | 61 | } |
| | 62 | |
| | 63 | function test_capable_edit_others_post() { |
| | 64 | $contributor_id = get_user_by( 'login', 'contributor' )->ID; |
| | 65 | $post = array( 'post_title' => 'Post test', 'post_author' => $contributor_id ); |
| | 66 | $post_id = wp_insert_post( $post ); |
| | 67 | |
| | 68 | $new_title = 'Post test (updated)'; |
| | 69 | $post2 = array( 'post_title' => $new_title ); |
| | 70 | $result = $this->myxmlrpcserver->wp_editPost( array( 1, 'editor', 'editor', $post_id, $post2 ) ); |
| | 71 | $this->assertNotInstanceOf( 'IXR_Error', $result ); |
| | 72 | $this->assertTrue($result); |
| | 73 | |
| | 74 | $out = wp_get_single_post( $post_id ); |
| | 75 | $this->assertEquals( $new_title, $out->post_title ); |
| | 76 | } |
| | 77 | |
| | 78 | function test_incapable_edit_others_post() { |
| | 79 | $author_id = get_user_by( 'login', 'author' )->ID; |
| | 80 | $original_title = 'Post test'; |
| | 81 | $post = array( 'post_title' => $original_title, 'post_author' => $author_id ); |
| | 82 | $post_id = wp_insert_post( $post ); |
| | 83 | |
| | 84 | $new_title = 'Post test (updated)'; |
| | 85 | $post2 = array( 'post_title' => $new_title ); |
| | 86 | $result = $this->myxmlrpcserver->wp_editPost( array( 1, 'contributor', 'contributor', $post_id, $post2 ) ); |
| | 87 | $this->assertInstanceOf( 'IXR_Error', $result ); |
| | 88 | $this->assertEquals( 401, $result->code ); |
| | 89 | |
| | 90 | $out = wp_get_single_post( $post_id ); |
| | 91 | $this->assertEquals( $original_title, $out->post_title ); |
| | 92 | } |
| | 93 | |
| | 94 | function test_capable_reassign_author() { |
| | 95 | $contributor_id = get_user_by( 'login', 'contributor' )->ID; |
| | 96 | $post = array( 'post_title' => 'Post test', 'post_author' => $contributor_id ); |
| | 97 | $post_id = wp_insert_post( $post ); |
| | 98 | |
| | 99 | $author_id = get_user_by( 'login', 'author' )->ID; |
| | 100 | $post2 = array( 'post_author' => $author_id ); |
| | 101 | $result = $this->myxmlrpcserver->wp_editPost( array( 1, 'editor', 'editor', $post_id, $post2 ) ); |
| | 102 | $this->assertNotInstanceOf( 'IXR_Error', $result ); |
| | 103 | $this->assertTrue($result); |
| | 104 | |
| | 105 | $out = wp_get_single_post( $post_id ); |
| | 106 | $this->assertEquals( $author_id, $out->post_author ); |
| | 107 | } |
| | 108 | |
| | 109 | function test_incapable_reassign_author() { |
| | 110 | $contributor_id = get_user_by( 'login', 'contributor' )->ID; |
| | 111 | $post = array( 'post_title' => 'Post test', 'post_author' => $contributor_id ); |
| | 112 | $post_id = wp_insert_post( $post ); |
| | 113 | |
| | 114 | $author_id = get_user_by( 'login', 'author' )->ID; |
| | 115 | $post2 = array( 'post_author' => $author_id ); |
| | 116 | $result = $this->myxmlrpcserver->wp_editPost( array( 1, 'contributor', 'contributor', $post_id, $post2 ) ); |
| | 117 | $this->assertInstanceOf( 'IXR_Error', $result ); |
| | 118 | $this->assertEquals( 401, $result->code ); |
| | 119 | |
| | 120 | $out = wp_get_single_post( $post_id ); |
| | 121 | $this->assertEquals( $contributor_id, $out->post_author ); |
| | 122 | } |
| | 123 | |
| | 124 | } |