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/getPages.php

    r25002 r39189  
    55 */
    66class Tests_XMLRPC_wp_getPages extends WP_XMLRPC_UnitTestCase {
    7     var $post_data;
    8     var $post_id;
    9     var $post_date_ts;
    10     var $editor_id;
     7    protected static $post_id;
     8    protected static $editor_id;
    119
    12     function setUp() {
    13         parent::setUp();
     10    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     11        self::$post_id = $factory->post->create( array(
     12            'post_type'   => 'page',
     13            'post_author' => $factory->user->create( array(
     14                'user_login' => 'administrator',
     15                'user_pass'  => 'administrator',
     16                'role'       => 'administrator'
     17            ) ),
     18            'post_date'   => strftime( "%Y-%m-%d %H:%M:%S", strtotime( '+1 day' ) ),
     19        ) );
     20        self::$editor_id = $factory->user->create( array(
     21            'user_login' => 'editor',
     22            'user_pass'  => 'editor',
     23            'role'       => 'editor'
     24        ) );
     25    }
    1426
    15         $this->post_date_ts = strtotime( '+1 day' );
    16         $this->post_data = array(
    17             'post_type' => 'page',
    18             'post_title' => rand_str(),
    19             'post_content' => rand_str( 2000 ),
    20             'post_excerpt' => rand_str( 100 ),
    21             'post_author' => $this->make_user_by_role( 'administrator' ),
    22             'post_date'  => strftime( "%Y-%m-%d %H:%M:%S", $this->post_date_ts ),
    23         );
    24         $this->post_id = wp_insert_post( $this->post_data );
    25         $this->editor_id = $this->make_user_by_role( 'editor' );
    26     }
     27    function test_invalid_username_password() {
     28        $result = $this->myxmlrpcserver->wp_getPages( array( 1, 'username', 'password' ) );
     29        $this->assertInstanceOf( 'IXR_Error', $result );
     30        $this->assertEquals( 403, $result->code );
     31    }
    2732
    28     function test_invalid_username_password() {
    29         $result = $this->myxmlrpcserver->wp_getPages( array( 1, 'username', 'password' ) );
    30         $this->assertInstanceOf( 'IXR_Error', $result );
    31         $this->assertEquals( 403, $result->code );
    32     }
    33 
    34     function test_incapable_user() {
     33    function test_incapable_user() {
    3534        $this->make_user_by_role( 'contributor' );
    3635
    37         $result = $this->myxmlrpcserver->wp_getPages( array( 1, 'contributor', 'contributor' ) );
    38         $this->assertInstanceOf( 'IXR_Error', $result );
    39         $this->assertEquals( 401, $result->code );
    40     }
     36        $result = $this->myxmlrpcserver->wp_getPages( array( 1, 'contributor', 'contributor' ) );
     37        $this->assertInstanceOf( 'IXR_Error', $result );
     38        $this->assertEquals( 401, $result->code );
     39    }
    4140
    42     function test_capable_user() {
    43         $results = $this->myxmlrpcserver->wp_getPages( array( 1, 'administrator', 'administrator' ) );
    44         $this->assertNotInstanceOf( 'IXR_Error', $results );
     41    function test_capable_user() {
     42        $results = $this->myxmlrpcserver->wp_getPages( array( 1, 'administrator', 'administrator' ) );
     43        $this->assertNotInstanceOf( 'IXR_Error', $results );
    4544
    46         foreach( $results as $result ) {
    47             $page = get_post( $result['page_id'] );
    48             $this->assertEquals( $page->post_type, 'page' );
    49         }
    50     }
     45        foreach( $results as $result ) {
     46            $page = get_post( $result['page_id'] );
     47            $this->assertEquals( $page->post_type, 'page' );
     48        }
     49    }
    5150
    52     function remove_editor_edit_page_cap( $caps, $cap, $user_id, $args ) {
    53         if ( in_array( $cap, array( 'edit_page', 'edit_others_pages' ) ) ) {
    54             if ( $user_id == $this->editor_id && $args[0] == $this->post_id ) {
    55                 return array( false );
    56             }
    57         }
     51    function remove_editor_edit_page_cap( $caps, $cap, $user_id, $args ) {
     52        if ( in_array( $cap, array( 'edit_page', 'edit_others_pages' ) ) ) {
     53            if ( $user_id == self::$editor_id && $args[0] == self::$post_id ) {
     54                return array( false );
     55            }
     56        }
    5857
    59         return $caps;
    60     }
     58        return $caps;
     59    }
    6160
    6261    /**
     
    6463     */
    6564    function test_semi_capable_user() {
    66         add_filter( 'map_meta_cap', array( $this, 'remove_editor_edit_page_cap') , 10, 4 );
     65        add_filter( 'map_meta_cap', array( $this, 'remove_editor_edit_page_cap') , 10, 4 );
    6766
    68         $results = $this->myxmlrpcserver->wp_getPages( array( 1, 'editor', 'editor' ) );
    69         $this->assertNotInstanceOf( 'IXR_Error', $results );
     67        $results = $this->myxmlrpcserver->wp_getPages( array( 1, 'editor', 'editor' ) );
     68        $this->assertNotInstanceOf( 'IXR_Error', $results );
    7069
    71         $found_incapable = false;
    72         foreach( $results as $result ) {
    73             // WP#20629
    74             $this->assertNotInstanceOf( 'IXR_Error', $result );
     70        $found_incapable = false;
     71        foreach( $results as $result ) {
     72            // WP#20629
     73            $this->assertNotInstanceOf( 'IXR_Error', $result );
    7574
    76             if ( $result['page_id'] == $this->post_id ) {
    77                 $found_incapable = true;
    78                 break;
    79             }
    80         }
    81         $this->assertFalse( $found_incapable );
     75            if ( $result['page_id'] == self::$post_id ) {
     76                $found_incapable = true;
     77                break;
     78            }
     79        }
     80        $this->assertFalse( $found_incapable );
    8281
    83         remove_filter( 'map_meta_cap', array( $this, 'remove_editor_edit_page_cap' ), 10, 4 );
    84     }
     82        remove_filter( 'map_meta_cap', array( $this, 'remove_editor_edit_page_cap' ), 10, 4 );
     83    }
    8584
    8685}
Note: See TracChangeset for help on using the changeset viewer.