Make WordPress Core


Ignore:
Timestamp:
11/10/2016 01:53:08 AM (8 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/user/mapMetaCap.php

    r38378 r39189  
    66 */
    77class Tests_User_MapMetaCap extends WP_UnitTestCase {
    8     var $super_admins = null;
    9 
    10     function setUp() {
    11         parent::setUp();
    12 
    13         $this->user_ids = array();
    14 
    15         $this->user_id   = self::factory()->user->create( array( 'role' => 'administrator' ) );
    16         $this->author_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
    17 
    18         if ( isset( $GLOBALS['super_admins'] ) )
    19             $this->super_admins = $GLOBALS['super_admins'];
    20         $user = new WP_User( $this->user_id );
     8
     9    protected static $post_type    = 'mapmetacap';
     10    protected static $super_admins = null;
     11    protected static $user_id      = null;
     12    protected static $author_id    = null;
     13    protected static $post_id      = null;
     14
     15    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
     16        self::$user_id   = self::factory()->user->create( array( 'role' => 'administrator' ) );
     17        self::$author_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
     18
     19        if ( isset( $GLOBALS['super_admins'] ) ) {
     20            self::$super_admins = $GLOBALS['super_admins'];
     21        }
     22        $user = new WP_User( self::$user_id );
    2123        $GLOBALS['super_admins'] = array( $user->user_login );
    2224
    23         $this->post_type = rand_str( 20 );
    24         register_post_type( $this->post_type );
    25 
    26         $this->post_id = wp_insert_post( array(
    27             'post_title' => rand_str(),
    28             'post_type' => $this->post_type,
     25        register_post_type( self::$post_type );
     26
     27        self::$post_id = $factory->post->create( array(
     28            'post_type'   => self::$post_type,
    2929            'post_status' => 'private',
    30             'post_author' => $this->author_id,
    31         ) );
    32     }
    33 
    34     function tearDown() {
    35         $GLOBALS['super_admins'] = $this->super_admins;
    36         unset( $GLOBALS['wp_post_types'][ $this->post_type ] );
    37         parent::tearDown();
     30            'post_author' => self::$author_id,
     31        ) );
     32    }
     33
     34    public static function wpTearDownAfterClass() {
     35        $GLOBALS['super_admins'] = self::$super_admins;
     36        unset( $GLOBALS['wp_post_types'][ self::$post_type ] );
    3837    }
    3938
     
    4443        $this->assertEquals(
    4544            array( 'do_not_allow' ),
    46             map_meta_cap( 'edit_post', $this->user_id, $this->post_id + 1 )
     45            map_meta_cap( 'edit_post', self::$user_id, self::$post_id + 1 )
    4746        );
    4847    }
     
    5049    function test_capability_type_post_with_no_extra_caps() {
    5150
    52         register_post_type( $this->post_type, array(
    53             'capability_type' => 'post',
    54         ) );
    55         $post_type_object = get_post_type_object( $this->post_type );
     51        register_post_type( self::$post_type, array(
     52            'capability_type' => 'post',
     53        ) );
     54        $post_type_object = get_post_type_object( self::$post_type );
    5655
    5756        $this->assertTrue( $post_type_object->map_meta_cap );
    5857
    5958        $this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ),
    60             map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
    61         $this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ),
    62             map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
    63 
    64         $this->assertEquals( array( 'read_private_posts' ),
    65             map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
    66         $this->assertEquals( array( 'read_private_posts' ),
    67             map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
    68 
    69         $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
    70             map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
    71         $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
    72             map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
     59            map_meta_cap( 'edit_post', self::$user_id, self::$post_id ) );
     60        $this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ),
     61            map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id ) );
     62
     63        $this->assertEquals( array( 'read_private_posts' ),
     64            map_meta_cap( 'read_post', self::$user_id, self::$post_id ) );
     65        $this->assertEquals( array( 'read_private_posts' ),
     66            map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id ) );
     67
     68        $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
     69            map_meta_cap( 'delete_post', self::$user_id, self::$post_id ) );
     70        $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
     71            map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id ) );
    7372    }
    7473
    7574    function test_custom_capability_type_with_map_meta_cap() {
    76         register_post_type( $this->post_type, array(
     75        register_post_type( self::$post_type, array(
    7776            'capability_type' => 'book',
    7877            'map_meta_cap' => true,
    7978        ) );
    8079
    81         $post_type_object = get_post_type_object( $this->post_type );
     80        $post_type_object = get_post_type_object( self::$post_type );
    8281
    8382        $this->assertEquals( array( 'edit_others_books', 'edit_private_books' ),
    84             map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
     83            map_meta_cap( 'edit_post', self::$user_id, self::$post_id ) );
    8584        $this->assertEquals( array( 'edit_others_books', 'edit_private_books' ),
    86             map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
     85            map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id ) );
    8786
    8887        $this->assertEquals( array( 'read_private_books' ),
    89             map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
     88            map_meta_cap( 'read_post', self::$user_id, self::$post_id ) );
    9089        $this->assertEquals( array( 'read_private_books' ),
    91             map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
     90            map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id ) );
    9291
    9392        $this->assertEquals( array( 'delete_others_books', 'delete_private_books' ),
    94             map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
     93            map_meta_cap( 'delete_post', self::$user_id, self::$post_id ) );
    9594        $this->assertEquals( array( 'delete_others_books', 'delete_private_books' ),
    96             map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
     95            map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id ) );
    9796    }
    9897
    9998    function test_capability_type_post_with_one_renamed_cap() {
    100         register_post_type( $this->post_type, array(
     99        register_post_type( self::$post_type, array(
    101100            'capability_type' => 'post',
    102101            'capabilities' => array( 'edit_posts' => 'edit_books' ),
    103102        ) );
    104103
    105         $post_type_object = get_post_type_object( $this->post_type );
     104        $post_type_object = get_post_type_object( self::$post_type );
    106105
    107106        $this->assertFalse( $post_type_object->map_meta_cap );
    108107
    109108        $this->assertEquals( array( 'edit_post' ),
    110             map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
     109            map_meta_cap( 'edit_post', self::$user_id, self::$post_id ) );
    111110        $this->assertEquals( array( 'edit_post' ),
    112             map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
     111            map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id ) );
    113112
    114113        $this->assertEquals( array( 'read_post' ),
    115             map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
     114            map_meta_cap( 'read_post', self::$user_id, self::$post_id ) );
    116115        $this->assertEquals( array( 'read_post' ),
    117             map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
     116            map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id ) );
    118117
    119118        $this->assertEquals( array( 'delete_post' ),
    120             map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
     119            map_meta_cap( 'delete_post', self::$user_id, self::$post_id ) );
    121120        $this->assertEquals( array( 'delete_post' ),
    122             map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
     121            map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id ) );
    123122    }
    124123
    125124    function test_capability_type_post_map_meta_cap_true_with_renamed_cap() {
    126         register_post_type( $this->post_type, array(
     125        register_post_type( self::$post_type, array(
    127126            'capability_type' => 'post',
    128127            'map_meta_cap' => true,
     
    133132        ) );
    134133
    135         $post_type_object = get_post_type_object( $this->post_type );
     134        $post_type_object = get_post_type_object( self::$post_type );
    136135
    137136        $this->assertTrue( $post_type_object->map_meta_cap );
    138137
    139138        $this->assertEquals( array( 'edit_others_books', 'edit_private_posts' ),
    140             map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
     139            map_meta_cap( 'edit_post', self::$user_id, self::$post_id ) );
    141140        $this->assertEquals( array( 'edit_others_books', 'edit_private_posts' ),
    142             map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
    143 
    144         $this->assertEquals( array( 'read_private_posts' ),
    145             map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
    146         $this->assertEquals( array( 'read_private_posts' ),
    147             map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
    148 
    149         $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
    150             map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
    151         $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
    152             map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
     141            map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id ) );
     142
     143        $this->assertEquals( array( 'read_private_posts' ),
     144            map_meta_cap( 'read_post', self::$user_id, self::$post_id ) );
     145        $this->assertEquals( array( 'read_private_posts' ),
     146            map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id ) );
     147
     148        $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
     149            map_meta_cap( 'delete_post', self::$user_id, self::$post_id ) );
     150        $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
     151            map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id ) );
    153152    }
    154153
    155154    function test_capability_type_post_with_all_meta_caps_renamed() {
    156         register_post_type( $this->post_type, array(
     155        register_post_type( self::$post_type, array(
    157156            'capability_type' => 'post',
    158157            'capabilities' => array(
     
    163162        ) );
    164163
    165         $post_type_object = get_post_type_object( $this->post_type );
     164        $post_type_object = get_post_type_object( self::$post_type );
    166165
    167166        $this->assertFalse( $post_type_object->map_meta_cap );
    168167
    169168        $this->assertEquals( array( 'edit_book' ),
    170             map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
     169            map_meta_cap( 'edit_post', self::$user_id, self::$post_id ) );
    171170        $this->assertEquals( array( 'edit_book' ),
    172             map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
     171            map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id ) );
    173172
    174173        $this->assertEquals( array( 'read_book' ),
    175             map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
     174            map_meta_cap( 'read_post', self::$user_id, self::$post_id ) );
    176175        $this->assertEquals( array( 'read_book' ),
    177             map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
     176            map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id ) );
    178177
    179178        $this->assertEquals( array( 'delete_book' ),
    180             map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
     179            map_meta_cap( 'delete_post', self::$user_id, self::$post_id ) );
    181180        $this->assertEquals( array( 'delete_book' ),
    182             map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
     181            map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id ) );
    183182    }
    184183
    185184    function test_capability_type_post_with_all_meta_caps_renamed_mapped() {
    186         register_post_type( $this->post_type, array(
     185        register_post_type( self::$post_type, array(
    187186            'capability_type' => 'post',
    188187            'map_meta_cap' => true,
     
    194193        ) );
    195194
    196         $post_type_object = get_post_type_object( $this->post_type );
     195        $post_type_object = get_post_type_object( self::$post_type );
    197196
    198197        $this->assertTrue( $post_type_object->map_meta_cap );
    199198
    200199        $this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ),
    201             map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
    202         $this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ),
    203             map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
    204 
    205         $this->assertEquals( array( 'read_private_posts' ),
    206             map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
    207         $this->assertEquals( array( 'read_private_posts' ),
    208             map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
    209 
    210         $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
    211             map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
    212         $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
    213             map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
     200            map_meta_cap( 'edit_post', self::$user_id, self::$post_id ) );
     201        $this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ),
     202            map_meta_cap( $post_type_object->cap->edit_post, self::$user_id, self::$post_id ) );
     203
     204        $this->assertEquals( array( 'read_private_posts' ),
     205            map_meta_cap( 'read_post', self::$user_id, self::$post_id ) );
     206        $this->assertEquals( array( 'read_private_posts' ),
     207            map_meta_cap( $post_type_object->cap->read_post, self::$user_id, self::$post_id ) );
     208
     209        $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
     210            map_meta_cap( 'delete_post', self::$user_id, self::$post_id ) );
     211        $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
     212            map_meta_cap( $post_type_object->cap->delete_post, self::$user_id, self::$post_id ) );
    214213    }
    215214
     
    219218        if ( is_multisite() ) {
    220219            $this->assertEquals( array( 'do_not_allow' ), map_meta_cap( 'unfiltered_html', 0 ) );
    221             $this->assertEquals( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', $this->user_id ) );
     220            $this->assertEquals( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', self::$user_id ) );
    222221        } else {
    223             $this->assertEquals( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', $this->user_id ) );
     222            $this->assertEquals( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', self::$user_id ) );
    224223        }
    225224    }
     
    239238        }
    240239
    241         $this->assertEquals( array( 'update_core' ), map_meta_cap( 'update_core', $this->user_id ) );
    242         $this->assertEquals( array( 'edit_plugins' ), map_meta_cap( 'edit_plugins', $this->user_id ) );
     240        $this->assertEquals( array( 'update_core' ), map_meta_cap( 'update_core', self::$user_id ) );
     241        $this->assertEquals( array( 'edit_plugins' ), map_meta_cap( 'edit_plugins', self::$user_id ) );
    243242    }
    244243
     
    269268
    270269        update_option( 'page_on_front', $post_id );
    271         $caps = map_meta_cap( 'delete_page', $this->user_id, $post_id );
     270        $caps = map_meta_cap( 'delete_page', self::$user_id, $post_id );
    272271        delete_option( 'page_on_front' );
    273272
     
    287286
    288287        update_option( 'page_for_posts', $post_id );
    289         $caps = map_meta_cap( 'delete_page', $this->user_id, $post_id );
     288        $caps = map_meta_cap( 'delete_page', self::$user_id, $post_id );
    290289        delete_option( 'page_for_posts' );
    291290
Note: See TracChangeset for help on using the changeset viewer.