WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/05/2016 05:17:02 PM (4 years ago)
Author:
johnbillion
Message:

Role/capability: Add more complete capability and role assertions to existing user capability tests. Also reuses one more user account fixtures.

Fixes #38236
See #38235

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/user/capabilities.php

    r38731 r38732  
    684684
    685685        // shouldn't have any other caps
    686         $this->assertFalse($user->has_cap('upload_files'));
    687         $this->assertFalse($user->has_cap('edit_published_posts'));
    688         $this->assertFalse($user->has_cap('upload_files'));
    689         $this->assertFalse($user->has_cap('level_4'));
     686        $caps = $this->getAllCapsAndRoles();
     687        foreach ( $caps as $cap => $roles ) {
     688            if ( 'level_1' !== $cap ) {
     689                $this->assertFalse( $user->has_cap( $cap ), "User should not have the {$cap} capability" );
     690            }
     691        }
    690692
    691693        // clean up
     
    758760
    759761        // make sure the other caps didn't get messed up
    760         $this->assertTrue($user_1->has_cap('edit_posts'));
    761         $this->assertTrue($user_1->has_cap('read'));
    762         $this->assertTrue($user_1->has_cap('level_1'));
    763         $this->assertTrue($user_1->has_cap('level_0'));
    764         $this->assertFalse($user_1->has_cap('upload_files'));
    765         $this->assertFalse($user_1->has_cap('edit_published_posts'));
    766         $this->assertFalse($user_1->has_cap('level_2'));
     762        $caps = $this->getAllCapsAndRoles();
     763        foreach ( $caps as $cap => $roles ) {
     764            if ( in_array( 'contributor', $roles, true ) || 'publish_posts' === $cap ) {
     765                $this->assertTrue( $user_1->has_cap( $cap ), "User should have the {$cap} capability" );
     766            } else {
     767                $this->assertFalse( $user_1->has_cap( $cap ), "User should not have the {$cap} capability" );
     768            }
     769        }
    767770
    768771    }
     
    848851        $this->assertTrue($user->exists(), "Problem getting user $id");
    849852
    850         // capabilities for the author role should be gone
    851 #       $this->assertFalse($user->has_cap('edit_posts'));
    852 #       $this->assertFalse($user->has_cap('edit_published_posts'));
    853 #       $this->assertFalse($user->has_cap('upload_files'));
    854 #       $this->assertFalse($user->has_cap('level_2'));
     853        // all capabilities for the user should be gone
     854        foreach ( $this->getAllCapsAndRoles() as $cap => $roles ) {
     855            $this->assertFalse( $user->has_cap( $cap ), "User should not have the {$cap} capability" );
     856        }
    855857
    856858        // the extra capabilities should be gone
     
    960962        $contributor = self::$users['contributor'];
    961963
    962         // editor can edit, view, and trash
     964        // editor can publish, edit, view, and trash
     965        $this->assertTrue( $editor->has_cap( 'publish_post', $post ) );
    963966        $this->assertTrue( $editor->has_cap( 'edit_post', $post ) );
    964967        $this->assertTrue( $editor->has_cap( 'delete_post', $post ) );
     
    966969
    967970        // a contributor cannot (except read a published post)
     971        $this->assertFalse( $contributor->has_cap( 'publish_post', $post ) );
    968972        $this->assertFalse( $contributor->has_cap( 'edit_post', $post ) );
    969973        $this->assertFalse( $contributor->has_cap( 'delete_post', $post ) );
     
    979983        $editor      = self::$users['editor'];
    980984        $contributor = self::$users['contributor'];
     985        $subscriber  = self::$users['subscriber'];
    981986
    982987        // create_posts isn't a real cap.
     
    985990        $this->assertFalse($editor->has_cap('create_posts'));
    986991        $this->assertFalse($contributor->has_cap('create_posts'));
     992        $this->assertFalse($subscriber->has_cap('create_posts'));
    987993
    988994        register_post_type( 'foobar' );
     
    9951001        $this->assertTrue($editor->has_cap( $cap->create_posts ));
    9961002        $this->assertTrue($contributor->has_cap( $cap->create_posts ));
     1003        $this->assertFalse($subscriber->has_cap( $cap->create_posts ));
    9971004
    9981005        _unregister_post_type( 'foobar' );
     
    10081015        $this->assertFalse($editor->has_cap( $cap->create_posts ));
    10091016        $this->assertFalse($contributor->has_cap( $cap->create_posts ));
     1017        $this->assertFalse($subscriber->has_cap( $cap->create_posts ));
    10101018
    10111019        // Add edit_foobars primitive cap to a user.
     
    10161024        $this->assertFalse($editor->has_cap( $cap->create_posts ));
    10171025        $this->assertFalse($contributor->has_cap( $cap->create_posts ));
     1026        $this->assertFalse($subscriber->has_cap( $cap->create_posts ));
    10181027
    10191028        $admin->remove_cap( 'edit_foobars' );
     
    10291038        $this->assertTrue( $contributor->has_cap( $cap->edit_posts ) );
    10301039        $this->assertFalse( $contributor->has_cap( $cap->create_posts ) );
     1040        $this->assertFalse( $subscriber->has_cap( $cap->create_posts ) );
    10311041    }
    10321042
     
    11971207    function test_negative_caps() {
    11981208        $author = self::$users['author'];
     1209
    11991210        $author->add_cap( 'foo', false );
    12001211        $this->assertTrue ( isset( $author->caps['foo'] ) );
     1212        $this->assertFalse( user_can( $author->ID, 'foo' ) );
     1213
    12011214        $author->remove_cap( 'foo' );
    12021215        $this->assertFalse ( isset( $author->caps['foo'] ) );
     1216        $this->assertFalse( user_can( $author->ID, 'foo' ) );
    12031217    }
    12041218
     
    13641378        $user = new WP_User( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
    13651379        $user->add_cap( 'manage_network_users' );
    1366         $other_user = new WP_User( self::factory()->user->create( array( 'role' => 'subscriber' ) ) );
     1380        $other_user = self::$users['subscriber'];
    13671381
    13681382        wp_set_current_user( $user->ID );
     
    14661480        }
    14671481
     1482        $this->assertFalse( current_user_can( 'do_not_allow' ), "Non-logged-in user should not have the do_not_allow capability" );
    14681483    }
    14691484
Note: See TracChangeset for help on using the changeset viewer.