Make WordPress Core


Ignore:
Timestamp:
10/17/2015 08:48:33 PM (9 years ago)
Author:
wonderboymusic
Message:

Unit Tests: better fixtures for Tests_AdminBar. Don't force-delete some posts: some filter callbacks are no-ops on Multisite if the post is vanquished.

See #30017, #33968.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/adminbar.php

    r35242 r35246  
    77 */
    88class Tests_AdminBar extends WP_UnitTestCase {
     9    protected static $editor_id;
     10    protected static $admin_id;
     11    protected static $no_role_id;
     12    protected static $post_id;
     13    protected static $blog_id;
     14
     15    protected static $user_ids = array();
    916
    1017    public static function setUpBeforeClass() {
    1118        require_once( ABSPATH . WPINC . '/class-wp-admin-bar.php' );
     19
     20        parent::setUpBeforeClass();
     21    }
     22
     23    public static function wpSetUpBeforeClass( $factory ) {
     24        self::$user_ids[] = self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
     25        self::$user_ids[] = self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
     26        self::$user_ids[] = self::$no_role_id = $factory->user->create( array( 'role' => '' ) );
     27    }
     28
     29    public static function wpTearDownAfterClass() {
     30        foreach ( self::$user_ids as $id ) {
     31            self::delete_user( $id );
     32        }
    1233    }
    1334
     
    1637     */
    1738    function test_content_post_type() {
    18         wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
     39        wp_set_current_user( self::$editor_id );
    1940
    2041        register_post_type( 'content', array( 'show_in_admin_bar' => true ) );
     
    3556     */
    3657    function test_merging_existing_meta_values() {
    37         wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
     58        wp_set_current_user( self::$editor_id );
    3859
    3960        $admin_bar = new WP_Admin_Bar;
     
    4364            'meta' => array( 'class' => 'test-class' ),
    4465        ) );
    45         $node = $admin_bar->get_node( 'test-node' );
    46         $this->assertEquals( array( 'class' => 'test-class' ), $node->meta );
     66
     67        $node1 = $admin_bar->get_node( 'test-node' );
     68        $this->assertEquals( array( 'class' => 'test-class' ), $node1->meta );
    4769
    4870        $admin_bar->add_node( array(
     
    5173        ) );
    5274
    53         $node = $admin_bar->get_node( 'test-node' );
    54         $this->assertEquals( array( 'class' => 'test-class', 'some-meta' => 'value' ), $node->meta );
     75        $node2 = $admin_bar->get_node( 'test-node' );
     76        $this->assertEquals( array( 'class' => 'test-class', 'some-meta' => 'value' ), $node2->meta );
    5577    }
    5678
     
    6385        }
    6486
    65         $nobody = self::factory()->user->create( array( 'role' => '' ) );
    66         $this->assertFalse( user_can( $nobody, 'read' ) );
    67 
    68         wp_set_current_user( $nobody );
     87        $this->assertFalse( user_can( self::$no_role_id, 'read' ) );
     88
     89        wp_set_current_user( self::$no_role_id );
    6990
    7091        $wp_admin_bar = $this->get_standard_admin_bar();
     
    82103        $this->assertFalse( $node_user_info->href );
    83104        $this->assertNull( $node_edit_profile );
    84 
    85105    }
    86106
     
    93113        }
    94114
    95         $editor = self::factory()->user->create( array( 'role' => 'editor' ) );
    96         $this->assertTrue( user_can( $editor, 'read' ) );
    97 
    98         wp_set_current_user( $editor );
     115        $this->assertTrue( user_can( self::$editor_id, 'read' ) );
     116
     117        wp_set_current_user( self::$editor_id );
    99118
    100119        $wp_admin_bar = $this->get_standard_admin_bar();
     
    126145        }
    127146
    128         $admin  = self::factory()->user->create( array( 'role' => 'administrator' ) );
    129         $editor = self::factory()->user->create( array( 'role' => 'editor' ) );
    130 
    131         $this->assertTrue( user_can( $admin, 'read' ) );
    132         $this->assertTrue( user_can( $editor, 'read' ) );
    133 
    134         $new_blog_id = self::factory()->blog->create( array(
    135             'user_id' => $admin,
    136         ) );
    137 
    138         $this->assertTrue( is_user_member_of_blog( $admin, $new_blog_id ) );
    139         $this->assertFalse( is_user_member_of_blog( $editor, $new_blog_id ) );
    140 
    141         wp_set_current_user( $editor );
    142 
    143         switch_to_blog( $new_blog_id );
     147        $blog_id = self::factory()->blog->create( array(
     148            'user_id' => self::$admin_id,
     149        ) );
     150
     151        $this->assertTrue( user_can( self::$admin_id, 'read' ) );
     152        $this->assertTrue( user_can( self::$editor_id, 'read' ) );
     153
     154        $this->assertTrue( is_user_member_of_blog( self::$admin_id, $blog_id ) );
     155        $this->assertFalse( is_user_member_of_blog( self::$editor_id, $blog_id ) );
     156
     157        wp_set_current_user( self::$editor_id );
     158
     159        switch_to_blog( $blog_id );
    144160
    145161        $wp_admin_bar = $this->get_standard_admin_bar();
     
    151167
    152168        // get primary blog
    153         $primary = get_active_blog_for_user( $editor );
     169        $primary = get_active_blog_for_user( self::$editor_id );
    154170        $this->assertInternalType( 'object', $primary );
    155171
     
    168184
    169185        restore_current_blog();
    170 
    171186    }
    172187
     
    180195        }
    181196
    182         $admin  = self::factory()->user->create( array( 'role' => 'administrator' ) );
    183         $nobody = self::factory()->user->create( array( 'role' => '' ) );
    184 
    185         $this->assertTrue( user_can( $admin, 'read' ) );
    186         $this->assertFalse( user_can( $nobody, 'read' ) );
    187 
    188         $new_blog_id = self::factory()->blog->create( array(
    189             'user_id' => $admin,
    190         ) );
    191 
    192         $this->assertTrue( is_user_member_of_blog( $admin, $new_blog_id ) );
    193         $this->assertFalse( is_user_member_of_blog( $nobody, $new_blog_id ) );
    194         $this->assertTrue( is_user_member_of_blog( $nobody, get_current_blog_id() ) );
     197        $this->assertTrue( user_can( self::$admin_id, 'read' ) );
     198        $this->assertFalse( user_can( self::$no_role_id, 'read' ) );
     199
     200        $blog_id = self::factory()->blog->create( array(
     201            'user_id' => self::$admin_id,
     202        ) );
     203
     204        $this->assertTrue( is_user_member_of_blog( self::$admin_id, $blog_id ) );
     205        $this->assertFalse( is_user_member_of_blog( self::$no_role_id, $blog_id ) );
     206        $this->assertTrue( is_user_member_of_blog( self::$no_role_id, get_current_blog_id() ) );
    195207
    196208        // Remove `$nobody` from the current blog, so they're not a member of any blog
    197         $removed = remove_user_from_blog( $nobody, get_current_blog_id() );
     209        $removed = remove_user_from_blog( self::$no_role_id, get_current_blog_id() );
    198210
    199211        $this->assertTrue( $removed );
    200         $this->assertFalse( is_user_member_of_blog( $nobody, get_current_blog_id() ) );
    201 
    202         wp_set_current_user( $nobody );
    203 
    204         switch_to_blog( $new_blog_id );
     212        $this->assertFalse( is_user_member_of_blog( self::$no_role_id, get_current_blog_id() ) );
     213
     214        wp_set_current_user( self::$no_role_id );
     215
     216        switch_to_blog( $blog_id );
    205217
    206218        $wp_admin_bar = $this->get_standard_admin_bar();
     
    212224
    213225        // get primary blog
    214         $primary = get_active_blog_for_user( $nobody );
     226        $primary = get_active_blog_for_user( self::$no_role_id );
    215227        $this->assertNull( $primary );
    216228
     
    228240
    229241        restore_current_blog();
    230 
    231242    }
    232243
Note: See TracChangeset for help on using the changeset viewer.