| | 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_editTerm extends WPTestCase { |
| | 7 | var $user_ids = array(); |
| | 8 | var $term_ids = array(); |
| | 9 | |
| | 10 | var $parent_term; |
| | 11 | var $child_term; |
| | 12 | var $post_tag; |
| | 13 | |
| | 14 | function setUp() { |
| | 15 | parent::setUp(); |
| | 16 | // keep track of users we create |
| | 17 | $this->user_ids = array(); |
| | 18 | $this->_flush_roles(); |
| | 19 | |
| | 20 | $this->orig_users = get_users_of_blog(); |
| | 21 | add_filter( 'pre_option_enable_xmlrpc', '__return_true' ); |
| | 22 | |
| | 23 | $this->_make_user( 'subscriber', 'subscriber', 'subscriber' ); |
| | 24 | $this->_make_user( 'contributor', 'contributor', 'contributor' ); |
| | 25 | $this->_make_user( 'author', 'author', 'author' ); |
| | 26 | $this->_make_user( 'editor', 'editor', 'editor' ); |
| | 27 | |
| | 28 | $this->parent_term = wp_insert_term( 'parent' , 'category' ); |
| | 29 | $this->child_term = wp_insert_term( 'child' , 'category' ); |
| | 30 | $this->post_tag = wp_insert_term( 'test' , 'post_tag' ); |
| | 31 | |
| | 32 | $this->myxmlrpcserver = new wp_xmlrpc_server(); |
| | 33 | } |
| | 34 | |
| | 35 | function tearDown() { |
| | 36 | parent::tearDown(); |
| | 37 | // delete any users that were created during tests |
| | 38 | foreach ($this->user_ids as $id) |
| | 39 | wp_delete_user($id); |
| | 40 | |
| | 41 | wp_delete_term( $this->parent_term['term_id'], 'category' ); |
| | 42 | wp_delete_term( $this->child_term['term_id'], 'category' ); |
| | 43 | wp_delete_term( $this->post_tag['term_id'], 'post_tag' ); |
| | 44 | |
| | 45 | remove_filter( 'pre_option_enable_xmlrpc', '__return_true' ); |
| | 46 | } |
| | 47 | |
| | 48 | function _flush_roles() { |
| | 49 | // we want to make sure we're testing against the db, not just in-memory data |
| | 50 | // this will flush everything and reload it from the db |
| | 51 | unset( $GLOBALS['wp_user_roles'] ); |
| | 52 | } |
| | 53 | |
| | 54 | |
| | 55 | function test_invalid_username_password() { |
| | 56 | $result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'username', 'password' ) ); |
| | 57 | $this->assertInstanceOf( 'IXR_Error', $result ); |
| | 58 | $this->assertEquals( 403, $result->code ); |
| | 59 | } |
| | 60 | |
| | 61 | function test_empty_taxonomy() { |
| | 62 | $result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'subscriber', 'subscriber' ) ); |
| | 63 | $this->assertInstanceOf( 'IXR_Error', $result ); |
| | 64 | $this->assertEquals( 403, $result->code ); |
| | 65 | $this->assertEquals( __( 'Invalid taxonomy.' ), $result->message ); |
| | 66 | } |
| | 67 | |
| | 68 | function test_invalid_taxonomy() { |
| | 69 | $result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'subscriber', 'subscriber', $this->parent_term['term_id'], array( 'taxonomy' => 'not_existing' ) ) ); |
| | 70 | $this->assertInstanceOf( 'IXR_Error', $result ); |
| | 71 | $this->assertEquals( 403, $result->code ); |
| | 72 | $this->assertEquals( __( 'Invalid taxonomy.' ), $result->message ); |
| | 73 | } |
| | 74 | |
| | 75 | function test_incapable_user() { |
| | 76 | $result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'subscriber', 'subscriber', $this->parent_term['term_id'], array( 'taxonomy' => 'category' ) ) ); |
| | 77 | $this->assertInstanceOf( 'IXR_Error', $result ); |
| | 78 | $this->assertEquals( 401, $result->code ); |
| | 79 | $this->assertEquals( __( 'You are not allowed to edit terms in this taxonomy.' ), $result->message ); |
| | 80 | } |
| | 81 | |
| | 82 | function test_term_not_exists() { |
| | 83 | $result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', 9999, array( 'taxonomy' => 'category' ) ) ); |
| | 84 | $this->assertInstanceOf( 'IXR_Error', $result ); |
| | 85 | $this->assertEquals( 404, $result->code ); |
| | 86 | $this->assertEquals( __( 'Invalid term ID.' ), $result->message ); |
| | 87 | } |
| | 88 | |
| | 89 | function test_empty_term() { |
| | 90 | $result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', '', array( 'taxonomy' => 'category' ) ) ); |
| | 91 | $this->assertInstanceOf( 'IXR_Error', $result ); |
| | 92 | $this->assertEquals( 500, $result->code ); |
| | 93 | $this->assertEquals( __('Empty Term'), $result->message ); |
| | 94 | } |
| | 95 | |
| | 96 | function test_empty_term_name() { |
| | 97 | $result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', $this->parent_term['term_id'], array( 'taxonomy' => 'category', 'name' => '' ) ) ); |
| | 98 | $this->assertInstanceOf( 'IXR_Error', $result ); |
| | 99 | $this->assertEquals( 403, $result->code ); |
| | 100 | $this->assertEquals( __( 'The term name cannot be empty.' ), $result->message ); |
| | 101 | } |
| | 102 | |
| | 103 | function test_parent_for_nonhierarchical() { |
| | 104 | $result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', $this->post_tag['term_id'], array( 'taxonomy' => 'post_tag', 'parent' => $this->parent_term['term_id'] ) ) ); |
| | 105 | $this->assertInstanceOf( 'IXR_Error', $result ); |
| | 106 | $this->assertEquals( 403, $result->code ); |
| | 107 | $this->assertEquals( __( 'This taxonomy is not hierarchical.' ), $result->message ); |
| | 108 | } |
| | 109 | |
| | 110 | function test_parent_empty() { |
| | 111 | $result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', $this->child_term['term_id'], array( 'taxonomy' => 'category', 'parent' => '', 'name' => 'test' ) ) ); |
| | 112 | $this->assertInstanceOf( 'IXR_Error', $result ); |
| | 113 | $this->assertEquals( 500, $result->code ); |
| | 114 | $this->assertEquals( __('Empty Term'), $result->message ); |
| | 115 | } |
| | 116 | |
| | 117 | function test_parent_invalid() { |
| | 118 | $result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', $this->child_term['term_id'], array( 'taxonomy' => 'category', 'parent' => 'dasda', 'name' => 'test' ) ) ); |
| | 119 | $this->assertInstanceOf( 'IXR_Error', $result ); |
| | 120 | $this->assertEquals( 500, $result->code ); |
| | 121 | } |
| | 122 | |
| | 123 | function test_parent_not_existing() { |
| | 124 | $result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', $this->child_term['term_id'], array( 'taxonomy' => 'category', 'parent' => 9999, 'name' => 'test' ) ) ); |
| | 125 | $this->assertInstanceOf( 'IXR_Error', $result ); |
| | 126 | $this->assertEquals( 403, $result->code ); |
| | 127 | $this->assertEquals( __( 'Invalid parent term ID.' ), $result->message ); |
| | 128 | } |
| | 129 | |
| | 130 | function test_parent_duplicate_slug() { |
| | 131 | $result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', $this->child_term['term_id'], array( 'taxonomy' => 'category', 'slug' => 'parent' ) ) ); |
| | 132 | $this->assertInstanceOf( 'IXR_Error', $result ); |
| | 133 | $this->assertEquals( 500, $result->code ); |
| | 134 | $this->assertEquals( htmlspecialchars( sprintf( __('The slug “%s” is already in use by another term'), 'parent' ) ), $result->message ); |
| | 135 | } |
| | 136 | |
| | 137 | function test_edit_all_fields() { |
| | 138 | $fields = array( 'taxonomy' => 'category', 'name' => 'Child 2', 'parent' => $this->parent_term['term_id'], 'description' => 'Child term', 'slug' => 'child_2' ); |
| | 139 | $result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', $this->child_term['term_id'], $fields ) ); |
| | 140 | $this->assertNotInstanceOf( 'IXR_Error', $result ); |
| | 141 | } |
| | 142 | } |
| | 143 | No newline at end of file |