WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/10/2016 01:53:08 AM (4 years ago)
Author:
johnbillion
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/xmlrpc/wp/getPage.php

    r35244 r39189  
    55 */
    66class Tests_XMLRPC_wp_getPage extends WP_XMLRPC_UnitTestCase {
    7     var $post_data;
    8     var $post_id;
    9     var $post_date_ts;
     7    protected static $post_id;
    108
    11     function setUp() {
    12         parent::setUp();
    13 
    14         $this->post_date_ts = strtotime( '+1 day' );
    15         $this->post_data = array(
    16             'post_type' => 'page',
    17             'post_title' => rand_str(),
    18             'post_content' => rand_str( 2000 ),
    19             'post_excerpt' => rand_str( 100 ),
    20             'post_author' => $this->make_user_by_role( 'author' ),
    21             'post_date'  => strftime( "%Y-%m-%d %H:%M:%S", $this->post_date_ts ),
    22         );
    23         $this->post_id = wp_insert_post( $this->post_data );
     9    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     10        self::$post_id = $factory->post->create( array(
     11            'post_type'   => 'page',
     12            'post_author' => $factory->user->create( array(
     13                'user_login' => 'author',
     14                'user_pass'  => 'author',
     15                'role'       => 'author'
     16            ) ),
     17            'post_date'   => strftime( "%Y-%m-%d %H:%M:%S", strtotime( '+1 day' ) ),
     18        ) );
    2419    }
    2520
    2621    function test_invalid_username_password() {
    27         $result = $this->myxmlrpcserver->wp_getPage( array( 1, $this->post_id, 'username', 'password' ) );
     22        $result = $this->myxmlrpcserver->wp_getPage( array( 1, self::$post_id, 'username', 'password' ) );
    2823        $this->assertInstanceOf( 'IXR_Error', $result );
    2924        $this->assertEquals( 403, $result->code );
     
    4439        $this->make_user_by_role( 'editor' );
    4540
    46         $result = $this->myxmlrpcserver->wp_getPage( array( 1, $this->post_id, 'editor', 'editor' ) );
     41        $result = $this->myxmlrpcserver->wp_getPage( array( 1, self::$post_id, 'editor', 'editor' ) );
    4742        $this->assertNotInstanceOf( 'IXR_Error', $result );
    4843
     
    7166        $this->assertInternalType( 'string', $result['wp_page_template'] );
    7267
     68        $post_data = get_post( self::$post_id );
     69
    7370        // Check expected values
    7471        $this->assertStringMatchesFormat( '%d', $result['userid'] );
    75         $this->assertEquals( 'draft', $result['page_status'] );
    76         $this->assertEquals( $this->post_data['post_title'], $result['title'] );
    77         $this->assertEquals( url_to_postid( $result['link'] ), $this->post_id );
    78         $this->assertEquals( $this->post_data['post_excerpt'], $result['excerpt'] );
     72        $this->assertEquals( 'future', $result['page_status'] );
     73        $this->assertEquals( $post_data->post_title, $result['title'] );
     74        $this->assertEquals( url_to_postid( $result['link'] ), self::$post_id );
     75        $this->assertEquals( $post_data->post_excerpt, $result['excerpt'] );
    7976        $this->assertStringMatchesFormat( '%d', $result['wp_author_id'] );
    8077    }
     
    8380        $this->make_user_by_role( 'editor' );
    8481
    85         $result = $this->myxmlrpcserver->wp_getPage( array( 1, $this->post_id, 'editor', 'editor' ) );
     82        $result = $this->myxmlrpcserver->wp_getPage( array( 1, self::$post_id, 'editor', 'editor' ) );
    8683        $this->assertNotInstanceOf( 'IXR_Error', $result );
    8784
     
    8986        $this->assertInstanceOf( 'IXR_Date', $result['date_created_gmt'] );
    9087
    91         $date_gmt = strtotime( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $this->post_data['post_date'], false ), 'Ymd\TH:i:s' ) );
     88        $post_data = get_post( self::$post_id );
    9289
    93         $this->assertEquals( $this->post_date_ts, $result['dateCreated']->getTimestamp() );
     90        $date_gmt = strtotime( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $post_data->post_date, false ), 'Ymd\TH:i:s' ) );
     91
     92        $this->assertEquals( strtotime( $post_data->post_date ), $result['dateCreated']->getTimestamp() );
    9493        $this->assertEquals( $date_gmt, $result['date_created_gmt']->getTimestamp() );
    9594    }
Note: See TracChangeset for help on using the changeset viewer.