Ticket #18429: post_unittests.patch
File post_unittests.patch, 9.4 KB (added by , 13 years ago) |
---|
-
wp-testcase/test-xmlrpc-api/test_wp_editPost.php
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 } -
wp-testcase/test-xmlrpc-api/test_wp_newPost.php
15 15 16 16 $this->orig_users = get_users_of_blog(); 17 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(); 18 25 } 19 26 20 27 function tearDown() { … … 33 40 } 34 41 35 42 function test_invalid_username_password() { 36 $myxmlrpcserver = new wp_xmlrpc_server(); 37 $result = $myxmlrpcserver->wp_newPost( array( 1, 'username', 'password' ) ); 43 $result = $this->myxmlrpcserver->wp_newPost( array( 1, 'username', 'password' ) ); 38 44 $this->assertInstanceOf( 'IXR_Error', $result ); 39 45 $this->assertEquals( 403, $result->code ); 40 46 } 41 47 42 48 function test_incapable_user() { 43 $this->_make_user( 'subscriber', 'subscriber', 'subscriber' ); 44 $myxmlrpcserver = new wp_xmlrpc_server(); 45 $result = $myxmlrpcserver->wp_newPost( array( 1, 'subscriber', 'subscriber' ) ); 49 $result = $this->myxmlrpcserver->wp_newPost( array( 1, 'subscriber', 'subscriber' ) ); 46 50 $this->assertInstanceOf( 'IXR_Error', $result ); 47 51 $this->assertEquals( 401, $result->code ); 48 52 } 49 53 50 54 function test_no_content() { 51 $this->_make_user( 'author', 'author', 'author' ); 52 $myxmlrpcserver = new wp_xmlrpc_server(); 53 $result = $myxmlrpcserver->wp_newPost( array( 1, 'author', 'author' ) ); 55 $result = $this->myxmlrpcserver->wp_newPost( array( 1, 'author', 'author' ) ); 54 56 $this->assertInstanceOf( 'IXR_Error', $result ); 55 57 $this->assertEquals( 500, $result->code ); 56 58 $this->assertEquals( 'Content, title, and excerpt are empty.', $result->message ); 57 59 } 58 60 59 } 60 No newline at end of file 61 function test_basic_content() { 62 $post = array( 'post_title' => 'Test' ); 63 $result = $this->myxmlrpcserver->wp_newPost( array( 1, 'author', 'author', $post ) ); 64 $this->assertNotInstanceOf( 'IXR_Error', $result ); 65 $this->assertStringMatchesFormat( '%d', $result ); 66 } 67 68 function test_ignore_id() { 69 $post = array( 'post_title' => 'Test', 'ID' => 103948 ); 70 $result = $this->myxmlrpcserver->wp_newPost( array( 1, 'author', 'author', $post ) ); 71 $this->assertNotInstanceOf( 'IXR_Error', $result ); 72 $this->assertNotEquals( '103948', $result ); 73 } 74 75 function test_capable_publish() { 76 $post = array( 'post_title' => 'Test', 'post_status' => 'publish' ); 77 $result = $this->myxmlrpcserver->wp_newPost( array( 1, 'author', 'author', $post ) ); 78 $this->assertNotInstanceOf( 'IXR_Error', $result ); 79 } 80 81 function test_incapable_publish() { 82 $post = array( 'post_title' => 'Test', 'post_status' => 'publish' ); 83 $result = $this->myxmlrpcserver->wp_newPost( array( 1, 'contributor', 'contributor', $post ) ); 84 $this->assertInstanceOf( 'IXR_Error', $result ); 85 $this->assertEquals( 401, $result->code ); 86 } 87 88 function test_capable_other_author() { 89 $other_author_id = get_user_by( 'login', 'author' )->ID; 90 $post = array( 'post_title' => 'Test', 'post_author' => $other_author_id ); 91 $result = $this->myxmlrpcserver->wp_newPost( array( 1, 'editor', 'editor', $post ) ); 92 $this->assertNotInstanceOf( 'IXR_Error', $result ); 93 } 94 95 function test_incapable_other_author() { 96 $other_author_id = get_user_by( 'login', 'author' )->ID; 97 $post = array( 'post_title' => 'Test', 'post_author' => $other_author_id ); 98 $result = $this->myxmlrpcserver->wp_newPost( array( 1, 'contributor', 'contributor', $post ) ); 99 $this->assertInstanceOf( 'IXR_Error', $result ); 100 $this->assertEquals( 401, $result->code ); 101 } 102 103 function test_invalid_author() { 104 $post = array( 'post_title' => 'Test', 'post_author' => 99999999 ); 105 $result = $this->myxmlrpcserver->wp_newPost( array( 1, 'editor', 'editor', $post ) ); 106 $this->assertInstanceOf( 'IXR_Error', $result ); 107 $this->assertEquals( 404, $result->code ); 108 } 109 110 function test_empty_author() { 111 $my_author_id = get_user_by( 'login', 'author' )->ID; 112 $post = array( 'post_title' => 'Test' ); 113 $result = $this->myxmlrpcserver->wp_newPost( array( 1, 'author', 'author', $post ) ); 114 $this->assertNotInstanceOf( 'IXR_Error', $result ); 115 116 $out = wp_get_single_post( $result ); 117 $this->assertEquals( $my_author_id, $out->post_author ); 118 } 119 120 }