Build/Test Tools: Re-use a bunch of fixtures in test classes for user and XMLRPC tests.

Shaves a couple of seconds off of the tests.

See #30017, #38716


git-svn-id: https://develop.svn.wordpress.org/trunk@39189 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn 2016-11-10 01:53:08 +00:00
parent 9ac95f7650
commit 09e332c301
13 changed files with 312 additions and 334 deletions

View File

@ -7,33 +7,31 @@
* @group user
*/
class Tests_User_Author_Template extends WP_UnitTestCase {
protected $author_id = 0;
protected $post_id = 0;
protected static $author_id = 0;
protected static $post_id = 0;
private $permalink_structure;
function setUp() {
parent::setUp();
$this->author_id = self::factory()->user->create( array(
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$author_id = $factory->user->create( array(
'role' => 'author',
'user_login' => 'test_author',
'description' => 'test_author',
) );
$user = new WP_User( $this->author_id );
$post = array(
'post_author' => $this->author_id,
self::$post_id = $factory->post->create( array(
'post_author' => self::$author_id,
'post_status' => 'publish',
'post_content' => rand_str(),
'post_title' => rand_str(),
'post_type' => 'post'
);
) );
}
// insert a post and make sure the ID is ok
$this->post_id = self::factory()->post->create( $post );
function setUp() {
parent::setUp();
setup_postdata( get_post( $this->post_id ) );
setup_postdata( get_post( self::$post_id ) );
}
function tearDown() {
@ -43,7 +41,7 @@ class Tests_User_Author_Template extends WP_UnitTestCase {
function test_get_the_author() {
$author_name = get_the_author();
$user = new WP_User( $this->author_id );
$user = new WP_User( self::$author_id );
$this->assertEquals( $user->display_name, $author_name );
$this->assertEquals( 'test_author', $author_name );
@ -56,14 +54,14 @@ class Tests_User_Author_Template extends WP_UnitTestCase {
$this->assertEquals( 'test_author', get_the_author_meta( 'description' ) );
$this->assertEquals( 'test_author', get_the_author_meta( 'user_description' ) );
add_user_meta( $this->author_id, 'user_description', 'user description' );
$this->assertEquals( 'user description', get_user_meta( $this->author_id, 'user_description', true ) );
add_user_meta( self::$author_id, 'user_description', 'user description' );
$this->assertEquals( 'user description', get_user_meta( self::$author_id, 'user_description', true ) );
// user_description in meta is ignored. The content of description is returned instead.
// See #20285
$this->assertEquals( 'test_author', get_the_author_meta( 'user_description' ) );
$this->assertEquals( 'test_author', get_the_author_meta( 'description' ) );
update_user_meta( $this->author_id, 'user_description', '' );
$this->assertEquals( '', get_user_meta( $this->author_id, 'user_description', true ) );
update_user_meta( self::$author_id, 'user_description', '' );
$this->assertEquals( '', get_user_meta( self::$author_id, 'user_description', true ) );
$this->assertEquals( 'test_author', get_the_author_meta( 'user_description' ) );
$this->assertEquals( 'test_author', get_the_author_meta( 'description' ) );
@ -80,7 +78,7 @@ class Tests_User_Author_Template extends WP_UnitTestCase {
function test_get_the_author_posts() {
// Test with no global post, result should be 0 because no author is found
$this->assertEquals( 0, get_the_author_posts() );
$GLOBALS['post'] = $this->post_id;
$GLOBALS['post'] = self::$post_id;
$this->assertEquals( 1, get_the_author_posts() );
}
@ -91,7 +89,7 @@ class Tests_User_Author_Template extends WP_UnitTestCase {
register_post_type( 'wptests_pt' );
$cpt_ids = self::factory()->post->create_many( 2, array(
'post_author' => $this->author_id,
'post_author' => self::$author_id,
'post_type' => 'wptests_pt',
) );
$GLOBALS['post'] = $cpt_ids[0];

View File

@ -5,36 +5,35 @@
* @group capabilities
*/
class Tests_User_MapMetaCap extends WP_UnitTestCase {
var $super_admins = null;
function setUp() {
parent::setUp();
protected static $post_type = 'mapmetacap';
protected static $super_admins = null;
protected static $user_id = null;
protected static $author_id = null;
protected static $post_id = null;
$this->user_ids = array();
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
self::$author_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
$this->user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
$this->author_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
if ( isset( $GLOBALS['super_admins'] ) )
$this->super_admins = $GLOBALS['super_admins'];
$user = new WP_User( $this->user_id );
if ( isset( $GLOBALS['super_admins'] ) ) {
self::$super_admins = $GLOBALS['super_admins'];
}
$user = new WP_User( self::$user_id );
$GLOBALS['super_admins'] = array( $user->user_login );
$this->post_type = rand_str( 20 );
register_post_type( $this->post_type );
register_post_type( self::$post_type );
$this->post_id = wp_insert_post( array(
'post_title' => rand_str(),
'post_type' => $this->post_type,
self::$post_id = $factory->post->create( array(
'post_type' => self::$post_type,
'post_status' => 'private',
'post_author' => $this->author_id,
'post_author' => self::$author_id,
) );
}
function tearDown() {
$GLOBALS['super_admins'] = $this->super_admins;
unset( $GLOBALS['wp_post_types'][ $this->post_type ] );
parent::tearDown();
public static function wpTearDownAfterClass() {
$GLOBALS['super_admins'] = self::$super_admins;
unset( $GLOBALS['wp_post_types'][ self::$post_type ] );
}
/**
@ -43,87 +42,87 @@ class Tests_User_MapMetaCap extends WP_UnitTestCase {
function test_capability_type_post_with_invalid_id() {
$this->assertEquals(
array( 'do_not_allow' ),
map_meta_cap( 'edit_post', $this->user_id, $this->post_id + 1 )
map_meta_cap( 'edit_post', self::$user_id, self::$post_id + 1 )
);
}
function test_capability_type_post_with_no_extra_caps() {
register_post_type( $this->post_type, array(
register_post_type( self::$post_type, array(
'capability_type' => 'post',
) );
$post_type_object = get_post_type_object( $this->post_type );
$post_type_object = get_post_type_object( self::$post_type );
$this->assertTrue( $post_type_object->map_meta_cap );
$this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ),
map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
map_meta_cap( 'edit_post', self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ),
map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'read_private_posts' ),
map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
map_meta_cap( 'read_post', self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'read_private_posts' ),
map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
map_meta_cap( 'delete_post', self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id ) );
}
function test_custom_capability_type_with_map_meta_cap() {
register_post_type( $this->post_type, array(
register_post_type( self::$post_type, array(
'capability_type' => 'book',
'map_meta_cap' => true,
) );
$post_type_object = get_post_type_object( $this->post_type );
$post_type_object = get_post_type_object( self::$post_type );
$this->assertEquals( array( 'edit_others_books', 'edit_private_books' ),
map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
map_meta_cap( 'edit_post', self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'edit_others_books', 'edit_private_books' ),
map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'read_private_books' ),
map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
map_meta_cap( 'read_post', self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'read_private_books' ),
map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'delete_others_books', 'delete_private_books' ),
map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
map_meta_cap( 'delete_post', self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'delete_others_books', 'delete_private_books' ),
map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id ) );
}
function test_capability_type_post_with_one_renamed_cap() {
register_post_type( $this->post_type, array(
register_post_type( self::$post_type, array(
'capability_type' => 'post',
'capabilities' => array( 'edit_posts' => 'edit_books' ),
) );
$post_type_object = get_post_type_object( $this->post_type );
$post_type_object = get_post_type_object( self::$post_type );
$this->assertFalse( $post_type_object->map_meta_cap );
$this->assertEquals( array( 'edit_post' ),
map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
map_meta_cap( 'edit_post', self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'edit_post' ),
map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'read_post' ),
map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
map_meta_cap( 'read_post', self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'read_post' ),
map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'delete_post' ),
map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
map_meta_cap( 'delete_post', self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'delete_post' ),
map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id ) );
}
function test_capability_type_post_map_meta_cap_true_with_renamed_cap() {
register_post_type( $this->post_type, array(
register_post_type( self::$post_type, array(
'capability_type' => 'post',
'map_meta_cap' => true,
'capabilities' => array(
@ -132,28 +131,28 @@ class Tests_User_MapMetaCap extends WP_UnitTestCase {
),
) );
$post_type_object = get_post_type_object( $this->post_type );
$post_type_object = get_post_type_object( self::$post_type );
$this->assertTrue( $post_type_object->map_meta_cap );
$this->assertEquals( array( 'edit_others_books', 'edit_private_posts' ),
map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
map_meta_cap( 'edit_post', self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'edit_others_books', 'edit_private_posts' ),
map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'read_private_posts' ),
map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
map_meta_cap( 'read_post', self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'read_private_posts' ),
map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
map_meta_cap( 'delete_post', self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id ) );
}
function test_capability_type_post_with_all_meta_caps_renamed() {
register_post_type( $this->post_type, array(
register_post_type( self::$post_type, array(
'capability_type' => 'post',
'capabilities' => array(
'edit_post' => 'edit_book',
@ -162,28 +161,28 @@ class Tests_User_MapMetaCap extends WP_UnitTestCase {
),
) );
$post_type_object = get_post_type_object( $this->post_type );
$post_type_object = get_post_type_object( self::$post_type );
$this->assertFalse( $post_type_object->map_meta_cap );
$this->assertEquals( array( 'edit_book' ),
map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
map_meta_cap( 'edit_post', self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'edit_book' ),
map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'read_book' ),
map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
map_meta_cap( 'read_post', self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'read_book' ),
map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'delete_book' ),
map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
map_meta_cap( 'delete_post', self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'delete_book' ),
map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id ) );
}
function test_capability_type_post_with_all_meta_caps_renamed_mapped() {
register_post_type( $this->post_type, array(
register_post_type( self::$post_type, array(
'capability_type' => 'post',
'map_meta_cap' => true,
'capabilities' => array(
@ -193,24 +192,24 @@ class Tests_User_MapMetaCap extends WP_UnitTestCase {
),
) );
$post_type_object = get_post_type_object( $this->post_type );
$post_type_object = get_post_type_object( self::$post_type );
$this->assertTrue( $post_type_object->map_meta_cap );
$this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ),
map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
map_meta_cap( 'edit_post', self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ),
map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'read_private_posts' ),
map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
map_meta_cap( 'read_post', self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'read_private_posts' ),
map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
map_meta_cap( 'delete_post', self::$user_id, self::$post_id ) );
$this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id ) );
}
function test_unfiltered_html_cap() {
@ -218,9 +217,9 @@ class Tests_User_MapMetaCap extends WP_UnitTestCase {
$this->markTestSkipped( 'DISALLOW_UNFILTERED_HTML is defined.' );
if ( is_multisite() ) {
$this->assertEquals( array( 'do_not_allow' ), map_meta_cap( 'unfiltered_html', 0 ) );
$this->assertEquals( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', $this->user_id ) );
$this->assertEquals( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', self::$user_id ) );
} else {
$this->assertEquals( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', $this->user_id ) );
$this->assertEquals( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', self::$user_id ) );
}
}
@ -238,8 +237,8 @@ class Tests_User_MapMetaCap extends WP_UnitTestCase {
define( 'DISALLOW_UNFILTERED_HTML', true );
}
$this->assertEquals( array( 'update_core' ), map_meta_cap( 'update_core', $this->user_id ) );
$this->assertEquals( array( 'edit_plugins' ), map_meta_cap( 'edit_plugins', $this->user_id ) );
$this->assertEquals( array( 'update_core' ), map_meta_cap( 'update_core', self::$user_id ) );
$this->assertEquals( array( 'edit_plugins' ), map_meta_cap( 'edit_plugins', self::$user_id ) );
}
/**
@ -268,7 +267,7 @@ class Tests_User_MapMetaCap extends WP_UnitTestCase {
) );
update_option( 'page_on_front', $post_id );
$caps = map_meta_cap( 'delete_page', $this->user_id, $post_id );
$caps = map_meta_cap( 'delete_page', self::$user_id, $post_id );
delete_option( 'page_on_front' );
$this->assertEquals( array( 'manage_options' ), $caps );
@ -286,7 +285,7 @@ class Tests_User_MapMetaCap extends WP_UnitTestCase {
) );
update_option( 'page_for_posts', $post_id );
$caps = map_meta_cap( 'delete_page', $this->user_id, $post_id );
$caps = map_meta_cap( 'delete_page', self::$user_id, $post_id );
delete_option( 'page_for_posts' );
$this->assertEquals( array( 'manage_options' ), $caps );

View File

@ -4,27 +4,21 @@
* @group xmlrpc
*/
class Tests_XMLRPC_mw_getPost extends WP_XMLRPC_UnitTestCase {
var $post_data;
var $post_id;
var $post_date_ts;
protected static $post_id;
function setUp() {
parent::setUp();
$author_id = $this->make_user_by_role( 'author' );
$this->post_date_ts = strtotime( '+1 day' );
$this->post_data = array(
'post_title' => rand_str(),
'post_content' => rand_str( 2000 ),
'post_excerpt' => rand_str( 100 ),
'post_author' => $author_id,
'post_date' => strftime( "%Y-%m-%d %H:%M:%S", $this->post_date_ts ),
);
$this->post_id = wp_insert_post( $this->post_data );
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_id = $factory->post->create( array(
'post_author' => $factory->user->create( array(
'user_login' => 'author',
'user_pass' => 'author',
'role' => 'author'
) ),
'post_date' => strftime( "%Y-%m-%d %H:%M:%S", strtotime( '+1 day' ) ),
) );
}
function test_invalid_username_password() {
$result = $this->myxmlrpcserver->mw_getPost( array( $this->post_id, 'username', 'password' ) );
$result = $this->myxmlrpcserver->mw_getPost( array( self::$post_id, 'username', 'password' ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 403, $result->code );
}
@ -32,7 +26,7 @@ class Tests_XMLRPC_mw_getPost extends WP_XMLRPC_UnitTestCase {
function test_incapable_user() {
$this->make_user_by_role( 'subscriber' );
$result = $this->myxmlrpcserver->mw_getPost( array( $this->post_id, 'subscriber', 'subscriber' ) );
$result = $this->myxmlrpcserver->mw_getPost( array( self::$post_id, 'subscriber', 'subscriber' ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 401, $result->code );
}
@ -50,7 +44,7 @@ class Tests_XMLRPC_mw_getPost extends WP_XMLRPC_UnitTestCase {
add_theme_support( 'post-thumbnails' );
$fields = array( 'post' );
$result = $this->myxmlrpcserver->mw_getPost( array( $this->post_id, 'author', 'author' ) );
$result = $this->myxmlrpcserver->mw_getPost( array( self::$post_id, 'author', 'author' ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
// Check data types
@ -76,14 +70,15 @@ class Tests_XMLRPC_mw_getPost extends WP_XMLRPC_UnitTestCase {
$this->assertInternalType( 'string', $result['wp_post_format'] );
$this->assertInternalType( 'bool', $result['sticky'] );
$post_data = get_post( self::$post_id );
// Check expected values
$this->assertStringMatchesFormat( '%d', $result['userid'] );
$this->assertEquals( $this->post_data['post_title'], $result['title'] );
$this->assertEquals( 'draft', $result['post_status'] );
$this->assertEquals( $post_data->post_title, $result['title'] );
$this->assertEquals( 'publish', $result['post_status'] );
$this->assertStringMatchesFormat( '%d', $result['wp_author_id'] );
$this->assertEquals( $this->post_data['post_excerpt'], $result['mt_excerpt'] );
$this->assertEquals( url_to_postid( $result['link'] ), $this->post_id );
$this->assertEquals( $post_data->post_excerpt, $result['mt_excerpt'] );
$this->assertEquals( url_to_postid( $result['link'] ), self::$post_id );
$this->assertEquals( '', $result['wp_post_thumbnail'] );
@ -97,10 +92,10 @@ class Tests_XMLRPC_mw_getPost extends WP_XMLRPC_UnitTestCase {
$filename = ( DIR_TESTDATA.'/images/a2-small.jpg' );
$attachment_id = self::factory()->attachment->create_upload_object( $filename );
set_post_thumbnail( $this->post_id, $attachment_id );
set_post_thumbnail( self::$post_id, $attachment_id );
$fields = array( 'post' );
$result = $this->myxmlrpcserver->mw_getPost( array( $this->post_id, 'author', 'author' ) );
$result = $this->myxmlrpcserver->mw_getPost( array( self::$post_id, 'author', 'author' ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
$this->assertInternalType( 'string', $result['wp_post_thumbnail'] );
@ -112,7 +107,7 @@ class Tests_XMLRPC_mw_getPost extends WP_XMLRPC_UnitTestCase {
function test_date() {
$fields = array( 'post' );
$result = $this->myxmlrpcserver->mw_getPost( array( $this->post_id, 'author', 'author' ) );
$result = $this->myxmlrpcserver->mw_getPost( array( self::$post_id, 'author', 'author' ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
$this->assertInstanceOf( 'IXR_Date', $result['dateCreated'] );
@ -120,11 +115,13 @@ class Tests_XMLRPC_mw_getPost extends WP_XMLRPC_UnitTestCase {
$this->assertInstanceOf( 'IXR_Date', $result['date_modified'] );
$this->assertInstanceOf( 'IXR_Date', $result['date_modified_gmt'] );
$this->assertEquals( $this->post_date_ts, $result['dateCreated']->getTimestamp() );
$this->assertEquals( $this->post_date_ts, $result['date_modified']->getTimestamp() );
$post_data = get_post( self::$post_id );
$post_date_gmt = strtotime( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $this->post_data['post_date'], false ), 'Ymd\TH:i:s' ) );
$post_modified_gmt = strtotime( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $this->post_data['post_date'], false ), 'Ymd\TH:i:s' ) );
$this->assertEquals( strtotime( $post_data->post_date ), $result['dateCreated']->getTimestamp() );
$this->assertEquals( strtotime( $post_data->post_date ), $result['date_modified']->getTimestamp() );
$post_date_gmt = strtotime( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $post_data->post_date, false ), 'Ymd\TH:i:s' ) );
$post_modified_gmt = strtotime( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $post_data->post_date, false ), 'Ymd\TH:i:s' ) );
$this->assertEquals( $post_date_gmt, $result['date_created_gmt']->getTimestamp() );
$this->assertEquals( $post_modified_gmt, $result['date_modified_gmt']->getTimestamp() );

View File

@ -4,23 +4,18 @@
* @group xmlrpc
*/
class Tests_XMLRPC_mw_getRecentPosts extends WP_XMLRPC_UnitTestCase {
var $post_data;
var $post_id;
var $post_date_ts;
protected static $post_id;
function setUp() {
parent::setUp();
$author_id = $this->make_user_by_role( 'author' );
$this->post_date_ts = strtotime( '+1 day' );
$this->post_data = array(
'post_title' => rand_str(),
'post_content' => rand_str( 2000 ),
'post_excerpt' => rand_str( 100 ),
'post_author' => $author_id,
'post_date' => strftime( "%Y-%m-%d %H:%M:%S", $this->post_date_ts ),
);
$this->post_id = wp_insert_post( $this->post_data );
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_id = $factory->post->create( array(
'post_type' => 'page',
'post_author' => $factory->user->create( array(
'user_login' => 'author',
'user_pass' => 'author',
'role' => 'author'
) ),
'post_date' => strftime( "%Y-%m-%d %H:%M:%S", strtotime( '+1 day' ) ),
) );
}
function test_invalid_username_password() {
@ -41,7 +36,7 @@ class Tests_XMLRPC_mw_getRecentPosts extends WP_XMLRPC_UnitTestCase {
}
function test_no_editable_posts() {
wp_delete_post( $this->post_id, true );
wp_delete_post( self::$post_id, true );
$result = $this->myxmlrpcserver->mw_getRecentPosts( array( 1, 'author', 'author' ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
@ -100,17 +95,17 @@ class Tests_XMLRPC_mw_getRecentPosts extends WP_XMLRPC_UnitTestCase {
// create attachment
$filename = ( DIR_TESTDATA.'/images/a2-small.jpg' );
$attachment_id = self::factory()->attachment->create_upload_object( $filename, $this->post_id );
set_post_thumbnail( $this->post_id, $attachment_id );
$attachment_id = self::factory()->attachment->create_upload_object( $filename, self::$post_id );
set_post_thumbnail( self::$post_id, $attachment_id );
$results = $this->myxmlrpcserver->mw_getRecentPosts( array( $this->post_id, 'author', 'author' ) );
$results = $this->myxmlrpcserver->mw_getRecentPosts( array( self::$post_id, 'author', 'author' ) );
$this->assertNotInstanceOf( 'IXR_Error', $results );
foreach( $results as $result ) {
$this->assertInternalType( 'string', $result['wp_post_thumbnail'] );
$this->assertStringMatchesFormat( '%d', $result['wp_post_thumbnail'] );
if( ! empty( $result['wp_post_thumbnail'] ) || $result['postid'] == $this->post_id ) {
if( ! empty( $result['wp_post_thumbnail'] ) || $result['postid'] == self::$post_id ) {
$attachment_id = get_post_meta( $result['postid'], '_thumbnail_id', true );
$this->assertEquals( $attachment_id, $result['wp_post_thumbnail'] );

View File

@ -4,13 +4,12 @@
* @group xmlrpc
*/
class Tests_XMLRPC_wp_deleteTerm extends WP_XMLRPC_UnitTestCase {
var $term;
protected static $term_id;
function setUp() {
parent::setUp();
$this->term = wp_insert_term( 'term' . rand_str() , 'category' );
$this->assertInternalType( 'array', $this->term );
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$term_id = $factory->term->create( array(
'taxonomy' => 'category',
) );
}
function test_invalid_username_password() {
@ -40,7 +39,7 @@ class Tests_XMLRPC_wp_deleteTerm extends WP_XMLRPC_UnitTestCase {
function test_incapable_user() {
$this->make_user_by_role( 'subscriber' );
$result = $this->myxmlrpcserver->wp_deleteTerm( array( 1, 'subscriber', 'subscriber', 'category', $this->term['term_id'] ) );
$result = $this->myxmlrpcserver->wp_deleteTerm( array( 1, 'subscriber', 'subscriber', 'category', self::$term_id ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 401, $result->code );
$this->assertEquals( __( 'Sorry, you are not allowed to delete this term.' ), $result->message );
@ -67,7 +66,7 @@ class Tests_XMLRPC_wp_deleteTerm extends WP_XMLRPC_UnitTestCase {
function test_term_deleted() {
$this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_deleteTerm( array( 1, 'editor', 'editor', 'category', $this->term['term_id'] ) );
$result = $this->myxmlrpcserver->wp_deleteTerm( array( 1, 'editor', 'editor', 'category', self::$term_id ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
$this->assertInternalType( 'boolean', $result );
}

View File

@ -4,19 +4,20 @@
* @group xmlrpc
*/
class Tests_XMLRPC_wp_editTerm extends WP_XMLRPC_UnitTestCase {
var $parent_term;
var $child_term;
var $post_tag;
protected static $parent_term;
protected static $child_term;
protected static $post_tag;
function setUp() {
parent::setUp();
$this->parent_term = wp_insert_term( 'parent' . rand_str() , 'category' );
$this->assertInternalType( 'array', $this->parent_term );
$this->child_term = wp_insert_term( 'child' . rand_str() , 'category' );
$this->assertInternalType( 'array', $this->child_term );
$this->post_tag = wp_insert_term( 'test' . rand_str() , 'post_tag' );
$this->assertInternalType( 'array', $this->post_tag );
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$parent_term = $factory->term->create( array(
'taxonomy' => 'category',
) );
self::$child_term = $factory->term->create( array(
'taxonomy' => 'category',
) );
self::$post_tag = $factory->term->create( array(
'taxonomy' => 'post_tag',
) );
}
function test_invalid_username_password() {
@ -37,7 +38,7 @@ class Tests_XMLRPC_wp_editTerm extends WP_XMLRPC_UnitTestCase {
function test_invalid_taxonomy() {
$this->make_user_by_role( 'subscriber' );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'subscriber', 'subscriber', $this->parent_term['term_id'], array( 'taxonomy' => 'not_existing' ) ) );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'subscriber', 'subscriber', self::$parent_term, array( 'taxonomy' => 'not_existing' ) ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 403, $result->code );
$this->assertEquals( __( 'Invalid taxonomy.' ), $result->message );
@ -46,7 +47,7 @@ class Tests_XMLRPC_wp_editTerm extends WP_XMLRPC_UnitTestCase {
function test_incapable_user() {
$this->make_user_by_role( 'subscriber' );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'subscriber', 'subscriber', $this->parent_term['term_id'], array( 'taxonomy' => 'category' ) ) );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'subscriber', 'subscriber', self::$parent_term, array( 'taxonomy' => 'category' ) ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 401, $result->code );
$this->assertEquals( __( 'Sorry, you are not allowed to edit this term.' ), $result->message );
@ -73,7 +74,7 @@ class Tests_XMLRPC_wp_editTerm extends WP_XMLRPC_UnitTestCase {
function test_empty_term_name() {
$this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', $this->parent_term['term_id'], array( 'taxonomy' => 'category', 'name' => '' ) ) );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', self::$parent_term, array( 'taxonomy' => 'category', 'name' => '' ) ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 403, $result->code );
$this->assertEquals( __( 'The term name cannot be empty.' ), $result->message );
@ -82,7 +83,7 @@ class Tests_XMLRPC_wp_editTerm extends WP_XMLRPC_UnitTestCase {
function test_parent_for_nonhierarchical() {
$this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', $this->post_tag['term_id'], array( 'taxonomy' => 'post_tag', 'parent' => $this->parent_term['term_id'] ) ) );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', self::$post_tag, array( 'taxonomy' => 'post_tag', 'parent' => self::$parent_term ) ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 403, $result->code );
$this->assertEquals( __( "This taxonomy is not hierarchical so you can't set a parent." ), $result->message );
@ -91,7 +92,7 @@ class Tests_XMLRPC_wp_editTerm extends WP_XMLRPC_UnitTestCase {
function test_parent_empty() {
$this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', $this->child_term['term_id'], array( 'taxonomy' => 'category', 'parent' => '', 'name' => 'test' ) ) );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', self::$child_term, array( 'taxonomy' => 'category', 'parent' => '', 'name' => 'test' ) ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
$this->assertTrue( $result );
}
@ -99,19 +100,19 @@ class Tests_XMLRPC_wp_editTerm extends WP_XMLRPC_UnitTestCase {
function test_parent_null() {
$this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', $this->child_term['term_id'], array( 'taxonomy' => 'category', 'parent' => NULL, 'name' => 'test' ) ) );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', self::$child_term, array( 'taxonomy' => 'category', 'parent' => NULL, 'name' => 'test' ) ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
$this->assertInternalType( 'boolean', $result );
$term = get_term( $this->child_term['term_id'], 'category' );
$term = get_term( self::$child_term, 'category' );
$this->assertEquals( '0', $term->parent );
}
function test_parent_invalid() {
$this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', $this->child_term['term_id'], array( 'taxonomy' => 'category', 'parent' => 'dasda', 'name' => 'test' ) ) );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', self::$child_term, array( 'taxonomy' => 'category', 'parent' => 'dasda', 'name' => 'test' ) ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 500, $result->code );
}
@ -119,7 +120,7 @@ class Tests_XMLRPC_wp_editTerm extends WP_XMLRPC_UnitTestCase {
function test_parent_not_existing() {
$this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', $this->child_term['term_id'], array( 'taxonomy' => 'category', 'parent' => 9999, 'name' => 'test' ) ) );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', self::$child_term, array( 'taxonomy' => 'category', 'parent' => 9999, 'name' => 'test' ) ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 403, $result->code );
$this->assertEquals( __( 'Parent term does not exist.' ), $result->message );
@ -128,8 +129,8 @@ class Tests_XMLRPC_wp_editTerm extends WP_XMLRPC_UnitTestCase {
function test_parent_duplicate_slug() {
$this->make_user_by_role( 'editor' );
$parent_term = get_term_by( 'id', $this->parent_term['term_id'], 'category' );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', $this->child_term['term_id'], array( 'taxonomy' => 'category', 'slug' => $parent_term->slug ) ) );
$parent_term = get_term_by( 'id', self::$parent_term, 'category' );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', self::$child_term, array( 'taxonomy' => 'category', 'slug' => $parent_term->slug ) ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 500, $result->code );
$this->assertEquals( htmlspecialchars( sprintf( __('The slug “%s” is already in use by another term'), $parent_term->slug ) ), $result->message );
@ -138,8 +139,8 @@ class Tests_XMLRPC_wp_editTerm extends WP_XMLRPC_UnitTestCase {
function test_edit_all_fields() {
$this->make_user_by_role( 'editor' );
$fields = array( 'taxonomy' => 'category', 'name' => 'Child 2', 'parent' => $this->parent_term['term_id'], 'description' => 'Child term', 'slug' => 'child_2' );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', $this->child_term['term_id'], $fields ) );
$fields = array( 'taxonomy' => 'category', 'name' => 'Child 2', 'parent' => self::$parent_term, 'description' => 'Child term', 'slug' => 'child_2' );
$result = $this->myxmlrpcserver->wp_editTerm( array( 1, 'editor', 'editor', self::$child_term, $fields ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
$this->assertInternalType( 'boolean', $result );

View File

@ -4,39 +4,37 @@
* @group xmlrpc
*/
class Tests_XMLRPC_wp_getComment extends WP_XMLRPC_UnitTestCase {
var $post_id;
var $parent_comment_id;
var $parent_comment_data;
var $child_comment_id;
var $child_comment_data;
protected static $post_id;
protected static $parent_comment_id;
protected static $parent_comment_data;
protected static $child_comment_id;
protected static $child_comment_data;
function setUp() {
parent::setUp();
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_id = $factory->post->create();
$this->post_id = self::factory()->post->create();
$this->parent_comment_data = array(
'comment_post_ID' => $this->post_id,
self::$parent_comment_data = array(
'comment_post_ID' => self::$post_id,
'comment_author' => 'Test commenter',
'comment_author_url' => 'http://example.com/',
'comment_author_email' => 'example@example.com',
'comment_content' => rand_str( 100 ),
);
$this->parent_comment_id = wp_insert_comment( $this->parent_comment_data );
self::$parent_comment_id = wp_insert_comment( self::$parent_comment_data );
$this->child_comment_data = array(
'comment_post_ID' => $this->post_id,
self::$child_comment_data = array(
'comment_post_ID' => self::$post_id,
'comment_author' => 'Test commenter 2',
'comment_author_url' => 'http://example.org/',
'comment_author_email' => 'example@example.org',
'comment_parent' => $this->parent_comment_id,
'comment_parent' => self::$parent_comment_id,
'comment_content' => rand_str( 100 )
);
$this->child_comment_id = wp_insert_comment( $this->child_comment_data );
self::$child_comment_id = wp_insert_comment( self::$child_comment_data );
}
function test_invalid_username_password() {
$result = $this->myxmlrpcserver->wp_getComment( array( 1, 'username', 'password', $this->parent_comment_id ) );
$result = $this->myxmlrpcserver->wp_getComment( array( 1, 'username', 'password', self::$parent_comment_id ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 403, $result->code );
}
@ -44,7 +42,7 @@ class Tests_XMLRPC_wp_getComment extends WP_XMLRPC_UnitTestCase {
function test_incapable_user() {
$this->make_user_by_role( 'contributor' );
$result = $this->myxmlrpcserver->wp_getComment( array( 1, 'contributor', 'contributor', $this->parent_comment_id ) );
$result = $this->myxmlrpcserver->wp_getComment( array( 1, 'contributor', 'contributor', self::$parent_comment_id ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 403, $result->code );
}
@ -52,7 +50,7 @@ class Tests_XMLRPC_wp_getComment extends WP_XMLRPC_UnitTestCase {
function test_valid_comment() {
$this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_getComment( array( 1, 'editor', 'editor', $this->parent_comment_id ) );
$result = $this->myxmlrpcserver->wp_getComment( array( 1, 'editor', 'editor', self::$parent_comment_id ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
// Check data types
@ -76,23 +74,23 @@ class Tests_XMLRPC_wp_getComment extends WP_XMLRPC_UnitTestCase {
$this->assertStringMatchesFormat( '%d', $result['comment_id'] );
$this->assertStringMatchesFormat( '%d', $result['parent'] );
$this->assertStringMatchesFormat( '%d', $result['post_id'] );
$this->assertEquals( $this->parent_comment_id, $result['comment_id'] );
$this->assertEquals( self::$parent_comment_id, $result['comment_id'] );
$this->assertEquals( 0, $result['parent'] );
$this->assertEquals( $this->parent_comment_data['comment_content'], $result['content'] );
$this->assertEquals( $this->post_id, $result['post_id'] );
$this->assertEquals( $this->parent_comment_data['comment_author'], $result['author'] );
$this->assertEquals( $this->parent_comment_data['comment_author_url'], $result['author_url'] );
$this->assertEquals( $this->parent_comment_data['comment_author_email'], $result['author_email'] );
$this->assertEquals( self::$parent_comment_data['comment_content'], $result['content'] );
$this->assertEquals( self::$post_id, $result['post_id'] );
$this->assertEquals( self::$parent_comment_data['comment_author'], $result['author'] );
$this->assertEquals( self::$parent_comment_data['comment_author_url'], $result['author_url'] );
$this->assertEquals( self::$parent_comment_data['comment_author_email'], $result['author_email'] );
}
function test_valid_child_comment() {
$this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_getComment( array( 1, 'editor', 'editor', $this->child_comment_id ) );
$result = $this->myxmlrpcserver->wp_getComment( array( 1, 'editor', 'editor', self::$child_comment_id ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
$this->assertEquals( $this->child_comment_id, $result['comment_id'] );
$this->assertEquals( $this->parent_comment_id, $result['parent'] );
$this->assertEquals( self::$child_comment_id, $result['comment_id'] );
$this->assertEquals( self::$parent_comment_id, $result['parent'] );
}
function test_invalid_id() {

View File

@ -4,29 +4,28 @@
* @group xmlrpc
*/
class Tests_XMLRPC_wp_getMediaItem extends WP_XMLRPC_UnitTestCase {
var $post_id;
protected static $post_id;
var $attachment_data;
var $attachment_id;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_id = $factory->post->create();
}
function setUp() {
parent::setUp();
add_theme_support( 'post-thumbnails' );
$this->post_id = wp_insert_post( array(
'post_title' => rand_str(),
'post_content' => rand_str(),
'post_status' => 'publish'
));
$filename = ( DIR_TESTDATA.'/images/waffles.jpg' );
$contents = file_get_contents( $filename );
$upload = wp_upload_bits(basename($filename), null, $contents);
$this->attachment_id = $this->_make_attachment( $upload, $this->post_id );
$this->attachment_id = $this->_make_attachment( $upload, self::$post_id );
$this->attachment_data = get_post( $this->attachment_id, ARRAY_A );
set_post_thumbnail( $this->post_id, $this->attachment_id );
set_post_thumbnail( self::$post_id, $this->attachment_id );
}
function tearDown() {

View File

@ -4,27 +4,22 @@
* @group xmlrpc
*/
class Tests_XMLRPC_wp_getPage extends WP_XMLRPC_UnitTestCase {
var $post_data;
var $post_id;
var $post_date_ts;
protected static $post_id;
function setUp() {
parent::setUp();
$this->post_date_ts = strtotime( '+1 day' );
$this->post_data = array(
'post_type' => 'page',
'post_title' => rand_str(),
'post_content' => rand_str( 2000 ),
'post_excerpt' => rand_str( 100 ),
'post_author' => $this->make_user_by_role( 'author' ),
'post_date' => strftime( "%Y-%m-%d %H:%M:%S", $this->post_date_ts ),
);
$this->post_id = wp_insert_post( $this->post_data );
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_id = $factory->post->create( array(
'post_type' => 'page',
'post_author' => $factory->user->create( array(
'user_login' => 'author',
'user_pass' => 'author',
'role' => 'author'
) ),
'post_date' => strftime( "%Y-%m-%d %H:%M:%S", strtotime( '+1 day' ) ),
) );
}
function test_invalid_username_password() {
$result = $this->myxmlrpcserver->wp_getPage( array( 1, $this->post_id, 'username', 'password' ) );
$result = $this->myxmlrpcserver->wp_getPage( array( 1, self::$post_id, 'username', 'password' ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 403, $result->code );
}
@ -43,7 +38,7 @@ class Tests_XMLRPC_wp_getPage extends WP_XMLRPC_UnitTestCase {
function test_valid_page() {
$this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_getPage( array( 1, $this->post_id, 'editor', 'editor' ) );
$result = $this->myxmlrpcserver->wp_getPage( array( 1, self::$post_id, 'editor', 'editor' ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
// Check data types
@ -70,27 +65,31 @@ class Tests_XMLRPC_wp_getPage extends WP_XMLRPC_UnitTestCase {
$this->assertInternalType( 'array', $result['custom_fields'] );
$this->assertInternalType( 'string', $result['wp_page_template'] );
$post_data = get_post( self::$post_id );
// Check expected values
$this->assertStringMatchesFormat( '%d', $result['userid'] );
$this->assertEquals( 'draft', $result['page_status'] );
$this->assertEquals( $this->post_data['post_title'], $result['title'] );
$this->assertEquals( url_to_postid( $result['link'] ), $this->post_id );
$this->assertEquals( $this->post_data['post_excerpt'], $result['excerpt'] );
$this->assertEquals( 'future', $result['page_status'] );
$this->assertEquals( $post_data->post_title, $result['title'] );
$this->assertEquals( url_to_postid( $result['link'] ), self::$post_id );
$this->assertEquals( $post_data->post_excerpt, $result['excerpt'] );
$this->assertStringMatchesFormat( '%d', $result['wp_author_id'] );
}
function test_date() {
$this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_getPage( array( 1, $this->post_id, 'editor', 'editor' ) );
$result = $this->myxmlrpcserver->wp_getPage( array( 1, self::$post_id, 'editor', 'editor' ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
$this->assertInstanceOf( 'IXR_Date', $result['dateCreated'] );
$this->assertInstanceOf( 'IXR_Date', $result['date_created_gmt'] );
$date_gmt = strtotime( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $this->post_data['post_date'], false ), 'Ymd\TH:i:s' ) );
$post_data = get_post( self::$post_id );
$this->assertEquals( $this->post_date_ts, $result['dateCreated']->getTimestamp() );
$date_gmt = strtotime( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $post_data->post_date, false ), 'Ymd\TH:i:s' ) );
$this->assertEquals( strtotime( $post_data->post_date ), $result['dateCreated']->getTimestamp() );
$this->assertEquals( $date_gmt, $result['date_created_gmt']->getTimestamp() );
}
}

View File

@ -4,23 +4,18 @@
* @group xmlrpc
*/
class Tests_XMLRPC_wp_getPageList extends WP_XMLRPC_UnitTestCase {
var $post_data;
var $post_id;
var $post_date_ts;
protected static $post_id;
function setUp() {
parent::setUp();
$this->post_date_ts = strtotime( '+1 day' );
$this->post_data = array(
'post_type' => 'page',
'post_title' => rand_str(),
'post_content' => rand_str( 2000 ),
'post_excerpt' => rand_str( 100 ),
'post_author' => $this->make_user_by_role( 'author' ),
'post_date' => strftime( "%Y-%m-%d %H:%M:%S", $this->post_date_ts ),
);
$this->post_id = wp_insert_post( $this->post_data );
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_id = $factory->post->create( array(
'post_type' => 'page',
'post_author' => $factory->user->create( array(
'user_login' => 'author',
'user_pass' => 'author',
'role' => 'author'
) ),
'post_date' => strftime( "%Y-%m-%d %H:%M:%S", strtotime( '+1 day' ) ),
) );
}
function test_invalid_username_password() {

View File

@ -4,83 +4,82 @@
* @group xmlrpc
*/
class Tests_XMLRPC_wp_getPages extends WP_XMLRPC_UnitTestCase {
var $post_data;
var $post_id;
var $post_date_ts;
var $editor_id;
protected static $post_id;
protected static $editor_id;
function setUp() {
parent::setUp();
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_id = $factory->post->create( array(
'post_type' => 'page',
'post_author' => $factory->user->create( array(
'user_login' => 'administrator',
'user_pass' => 'administrator',
'role' => 'administrator'
) ),
'post_date' => strftime( "%Y-%m-%d %H:%M:%S", strtotime( '+1 day' ) ),
) );
self::$editor_id = $factory->user->create( array(
'user_login' => 'editor',
'user_pass' => 'editor',
'role' => 'editor'
) );
}
$this->post_date_ts = strtotime( '+1 day' );
$this->post_data = array(
'post_type' => 'page',
'post_title' => rand_str(),
'post_content' => rand_str( 2000 ),
'post_excerpt' => rand_str( 100 ),
'post_author' => $this->make_user_by_role( 'administrator' ),
'post_date' => strftime( "%Y-%m-%d %H:%M:%S", $this->post_date_ts ),
);
$this->post_id = wp_insert_post( $this->post_data );
$this->editor_id = $this->make_user_by_role( 'editor' );
}
function test_invalid_username_password() {
$result = $this->myxmlrpcserver->wp_getPages( array( 1, 'username', 'password' ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 403, $result->code );
}
function test_invalid_username_password() {
$result = $this->myxmlrpcserver->wp_getPages( array( 1, 'username', 'password' ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 403, $result->code );
}
function test_incapable_user() {
function test_incapable_user() {
$this->make_user_by_role( 'contributor' );
$result = $this->myxmlrpcserver->wp_getPages( array( 1, 'contributor', 'contributor' ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 401, $result->code );
}
$result = $this->myxmlrpcserver->wp_getPages( array( 1, 'contributor', 'contributor' ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 401, $result->code );
}
function test_capable_user() {
$results = $this->myxmlrpcserver->wp_getPages( array( 1, 'administrator', 'administrator' ) );
$this->assertNotInstanceOf( 'IXR_Error', $results );
function test_capable_user() {
$results = $this->myxmlrpcserver->wp_getPages( array( 1, 'administrator', 'administrator' ) );
$this->assertNotInstanceOf( 'IXR_Error', $results );
foreach( $results as $result ) {
$page = get_post( $result['page_id'] );
$this->assertEquals( $page->post_type, 'page' );
}
}
foreach( $results as $result ) {
$page = get_post( $result['page_id'] );
$this->assertEquals( $page->post_type, 'page' );
}
}
function remove_editor_edit_page_cap( $caps, $cap, $user_id, $args ) {
if ( in_array( $cap, array( 'edit_page', 'edit_others_pages' ) ) ) {
if ( $user_id == $this->editor_id && $args[0] == $this->post_id ) {
return array( false );
}
}
function remove_editor_edit_page_cap( $caps, $cap, $user_id, $args ) {
if ( in_array( $cap, array( 'edit_page', 'edit_others_pages' ) ) ) {
if ( $user_id == self::$editor_id && $args[0] == self::$post_id ) {
return array( false );
}
}
return $caps;
}
return $caps;
}
/**
* @ticket 20629
*/
function test_semi_capable_user() {
add_filter( 'map_meta_cap', array( $this, 'remove_editor_edit_page_cap') , 10, 4 );
add_filter( 'map_meta_cap', array( $this, 'remove_editor_edit_page_cap') , 10, 4 );
$results = $this->myxmlrpcserver->wp_getPages( array( 1, 'editor', 'editor' ) );
$this->assertNotInstanceOf( 'IXR_Error', $results );
$results = $this->myxmlrpcserver->wp_getPages( array( 1, 'editor', 'editor' ) );
$this->assertNotInstanceOf( 'IXR_Error', $results );
$found_incapable = false;
foreach( $results as $result ) {
// WP#20629
$this->assertNotInstanceOf( 'IXR_Error', $result );
$found_incapable = false;
foreach( $results as $result ) {
// WP#20629
$this->assertNotInstanceOf( 'IXR_Error', $result );
if ( $result['page_id'] == $this->post_id ) {
$found_incapable = true;
break;
}
}
$this->assertFalse( $found_incapable );
if ( $result['page_id'] == self::$post_id ) {
$found_incapable = true;
break;
}
}
$this->assertFalse( $found_incapable );
remove_filter( 'map_meta_cap', array( $this, 'remove_editor_edit_page_cap' ), 10, 4 );
}
remove_filter( 'map_meta_cap', array( $this, 'remove_editor_edit_page_cap' ), 10, 4 );
}
}

View File

@ -4,13 +4,13 @@
* @group xmlrpc
*/
class Tests_XMLRPC_wp_getTerm extends WP_XMLRPC_UnitTestCase {
var $term;
function setUp() {
parent::setUp();
protected static $term_id;
$this->term = wp_insert_term( 'term' . rand_str() , 'category' );
$this->assertInternalType( 'array', $this->term );
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$term_id = $factory->term->create( array(
'taxonomy' => 'category',
) );
}
function test_invalid_username_password() {
@ -40,7 +40,7 @@ class Tests_XMLRPC_wp_getTerm extends WP_XMLRPC_UnitTestCase {
function test_incapable_user() {
$this->make_user_by_role( 'subscriber' );
$result = $this->myxmlrpcserver->wp_getTerm( array( 1, 'subscriber', 'subscriber', 'category', $this->term['term_id'] ) );
$result = $this->myxmlrpcserver->wp_getTerm( array( 1, 'subscriber', 'subscriber', 'category', self::$term_id ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 401, $result->code );
$this->assertEquals( __( 'Sorry, you are not allowed to assign this term.' ), $result->message );
@ -68,9 +68,9 @@ class Tests_XMLRPC_wp_getTerm extends WP_XMLRPC_UnitTestCase {
function test_valid_term() {
$this->make_user_by_role( 'editor' );
$term = get_term( $this->term['term_id'], 'category', ARRAY_A );
$term = get_term( self::$term_id, 'category', ARRAY_A );
$result = $this->myxmlrpcserver->wp_getTerm( array( 1, 'editor', 'editor', 'category', $this->term['term_id'] ) );
$result = $this->myxmlrpcserver->wp_getTerm( array( 1, 'editor', 'editor', 'category', self::$term_id ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
$this->assertEquals( $result, $term );

View File

@ -4,14 +4,13 @@
* @group xmlrpc
*/
class Tests_XMLRPC_wp_newTerm extends WP_XMLRPC_UnitTestCase {
var $parent_term;
function setUp() {
parent::setUp();
protected static $parent_term_id;
$this->parent_term = wp_insert_term( 'parent' . rand_str(), 'category' );
$this->assertInternalType( 'array', $this->parent_term );
$this->parent_term = $this->parent_term['term_id'];
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$parent_term_id = $factory->term->create( array(
'taxonomy' => 'category',
) );
}
function test_invalid_username_password() {
@ -59,7 +58,7 @@ class Tests_XMLRPC_wp_newTerm extends WP_XMLRPC_UnitTestCase {
function test_parent_for_nonhierarchical() {
$this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_newTerm( array( 1, 'editor', 'editor', array( 'taxonomy' => 'post_tag', 'parent' => $this->parent_term, 'name' => 'test' ) ) );
$result = $this->myxmlrpcserver->wp_newTerm( array( 1, 'editor', 'editor', array( 'taxonomy' => 'post_tag', 'parent' => self::$parent_term_id, 'name' => 'test' ) ) );
$this->assertInstanceOf( 'IXR_Error', $result );
$this->assertEquals( 403, $result->code );
$this->assertEquals( __( 'This taxonomy is not hierarchical.' ), $result->message );
@ -94,7 +93,7 @@ class Tests_XMLRPC_wp_newTerm extends WP_XMLRPC_UnitTestCase {
function test_add_term_with_parent() {
$this->make_user_by_role( 'editor' );
$result = $this->myxmlrpcserver->wp_newTerm( array( 1, 'editor', 'editor', array( 'taxonomy' => 'category', 'parent' => $this->parent_term, 'name' => 'test' ) ) );
$result = $this->myxmlrpcserver->wp_newTerm( array( 1, 'editor', 'editor', array( 'taxonomy' => 'category', 'parent' => self::$parent_term_id, 'name' => 'test' ) ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
$this->assertStringMatchesFormat( '%d', $result );
}
@ -102,7 +101,7 @@ class Tests_XMLRPC_wp_newTerm extends WP_XMLRPC_UnitTestCase {
function test_add_term_with_all() {
$this->make_user_by_role( 'editor' );
$taxonomy = array( 'taxonomy' => 'category', 'parent' => $this->parent_term, 'name' => 'test_all', 'description' => 'Test all', 'slug' => 'test_all' );
$taxonomy = array( 'taxonomy' => 'category', 'parent' => self::$parent_term_id, 'name' => 'test_all', 'description' => 'Test all', 'slug' => 'test_all' );
$result = $this->myxmlrpcserver->wp_newTerm( array( 1, 'editor', 'editor', $taxonomy ) );
$this->assertNotInstanceOf( 'IXR_Error', $result );
$this->assertStringMatchesFormat( '%d', $result );