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/mw/getPost.php

    r35242 r39189  
    55 */
    66class Tests_XMLRPC_mw_getPost 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         $author_id = $this->make_user_by_role( 'author' );
    15         $this->post_date_ts = strtotime( '+1 day' );
    16         $this->post_data = array(
    17             'post_title' => rand_str(),
    18             'post_content' => rand_str( 2000 ),
    19             'post_excerpt' => rand_str( 100 ),
    20             'post_author' => $author_id,
    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_author' => $factory->user->create( array(
     12                'user_login' => 'author',
     13                'user_pass'  => 'author',
     14                'role'       => 'author'
     15            ) ),
     16            'post_date'   => strftime( "%Y-%m-%d %H:%M:%S", strtotime( '+1 day' ) ),
     17        ) );
    2418    }
    2519
    2620    function test_invalid_username_password() {
    27         $result = $this->myxmlrpcserver->mw_getPost( array( $this->post_id, 'username', 'password' ) );
     21        $result = $this->myxmlrpcserver->mw_getPost( array( self::$post_id, 'username', 'password' ) );
    2822        $this->assertInstanceOf( 'IXR_Error', $result );
    2923        $this->assertEquals( 403, $result->code );
     
    3327        $this->make_user_by_role( 'subscriber' );
    3428
    35         $result = $this->myxmlrpcserver->mw_getPost( array( $this->post_id, 'subscriber', 'subscriber' ) );
     29        $result = $this->myxmlrpcserver->mw_getPost( array( self::$post_id, 'subscriber', 'subscriber' ) );
    3630        $this->assertInstanceOf( 'IXR_Error', $result );
    3731        $this->assertEquals( 401, $result->code );
     
    5145
    5246        $fields = array( 'post' );
    53         $result = $this->myxmlrpcserver->mw_getPost( array( $this->post_id, 'author', 'author' ) );
     47        $result = $this->myxmlrpcserver->mw_getPost( array( self::$post_id, 'author', 'author' ) );
    5448        $this->assertNotInstanceOf( 'IXR_Error', $result );
    5549
     
    7771        $this->assertInternalType( 'bool',   $result['sticky'] );
    7872
     73        $post_data = get_post( self::$post_id );
    7974
    8075        // Check expected values
    8176        $this->assertStringMatchesFormat( '%d', $result['userid'] );
    82         $this->assertEquals( $this->post_data['post_title'], $result['title'] );
    83         $this->assertEquals( 'draft', $result['post_status'] );
     77        $this->assertEquals( $post_data->post_title, $result['title'] );
     78        $this->assertEquals( 'publish', $result['post_status'] );
    8479        $this->assertStringMatchesFormat( '%d', $result['wp_author_id'] );
    85         $this->assertEquals( $this->post_data['post_excerpt'], $result['mt_excerpt'] );
    86         $this->assertEquals( url_to_postid( $result['link'] ), $this->post_id );
     80        $this->assertEquals( $post_data->post_excerpt, $result['mt_excerpt'] );
     81        $this->assertEquals( url_to_postid( $result['link'] ), self::$post_id );
    8782
    8883        $this->assertEquals( '', $result['wp_post_thumbnail'] );
     
    9893        $attachment_id = self::factory()->attachment->create_upload_object( $filename );
    9994
    100         set_post_thumbnail( $this->post_id, $attachment_id );
     95        set_post_thumbnail( self::$post_id, $attachment_id );
    10196
    10297        $fields = array( 'post' );
    103         $result = $this->myxmlrpcserver->mw_getPost( array( $this->post_id, 'author', 'author' ) );
     98        $result = $this->myxmlrpcserver->mw_getPost( array( self::$post_id, 'author', 'author' ) );
    10499        $this->assertNotInstanceOf( 'IXR_Error', $result );
    105100
     
    113108    function test_date() {
    114109        $fields = array( 'post' );
    115         $result = $this->myxmlrpcserver->mw_getPost( array( $this->post_id, 'author', 'author' ) );
     110        $result = $this->myxmlrpcserver->mw_getPost( array( self::$post_id, 'author', 'author' ) );
    116111        $this->assertNotInstanceOf( 'IXR_Error', $result );
    117112
     
    121116        $this->assertInstanceOf( 'IXR_Date', $result['date_modified_gmt'] );
    122117
    123         $this->assertEquals( $this->post_date_ts, $result['dateCreated']->getTimestamp() );
    124         $this->assertEquals( $this->post_date_ts, $result['date_modified']->getTimestamp() );
     118        $post_data = get_post( self::$post_id );
    125119
    126         $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' ) );
    127         $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' ) );
     120        $this->assertEquals( strtotime( $post_data->post_date ), $result['dateCreated']->getTimestamp() );
     121        $this->assertEquals( strtotime( $post_data->post_date ), $result['date_modified']->getTimestamp() );
     122
     123        $post_date_gmt = strtotime( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $post_data->post_date, false ), 'Ymd\TH:i:s' ) );
     124        $post_modified_gmt = strtotime( get_gmt_from_date( mysql2date( 'Y-m-d H:i:s', $post_data->post_date, false ), 'Ymd\TH:i:s' ) );
    128125
    129126        $this->assertEquals( $post_date_gmt, $result['date_created_gmt']->getTimestamp() );
Note: See TracChangeset for help on using the changeset viewer.