Tests: Shave a second off the user capability tests by reusing its user fixtures.
See #30017, #32394 git-svn-id: https://develop.svn.wordpress.org/trunk@36047 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
da84910c8a
commit
8567d80ade
@ -7,14 +7,24 @@
|
|||||||
* @group capabilities
|
* @group capabilities
|
||||||
*/
|
*/
|
||||||
class Tests_User_Capabilities extends WP_UnitTestCase {
|
class Tests_User_Capabilities extends WP_UnitTestCase {
|
||||||
protected $user_ids = array();
|
|
||||||
|
protected static $users = array();
|
||||||
|
|
||||||
|
public static function wpSetUpBeforeClass( $factory ) {
|
||||||
|
self::$users = array(
|
||||||
|
'administrator' => $factory->user->create_and_get( array( 'role' => 'administrator' ) ),
|
||||||
|
'editor' => $factory->user->create_and_get( array( 'role' => 'editor' ) ),
|
||||||
|
'author' => $factory->user->create_and_get( array( 'role' => 'author' ) ),
|
||||||
|
'contributor' => $factory->user->create_and_get( array( 'role' => 'contributor' ) ),
|
||||||
|
'subscriber' => $factory->user->create_and_get( array( 'role' => 'subscriber' ) ),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function setUp() {
|
function setUp() {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
// keep track of users we create
|
// keep track of users we create
|
||||||
$this->_flush_roles();
|
$this->_flush_roles();
|
||||||
|
|
||||||
$this->orig_users = get_users();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function _flush_roles() {
|
function _flush_roles() {
|
||||||
@ -233,13 +243,6 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
|
|
||||||
// test the tests
|
// test the tests
|
||||||
function test_all_caps_of_users_are_being_tested() {
|
function test_all_caps_of_users_are_being_tested() {
|
||||||
$users = array(
|
|
||||||
'administrator' => self::factory()->user->create_and_get( array( 'role' => 'administrator' ) ),
|
|
||||||
'editor' => self::factory()->user->create_and_get( array( 'role' => 'editor' ) ),
|
|
||||||
'author' => self::factory()->user->create_and_get( array( 'role' => 'author' ) ),
|
|
||||||
'contributor' => self::factory()->user->create_and_get( array( 'role' => 'contributor' ) ),
|
|
||||||
'subscriber' => self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) ),
|
|
||||||
);
|
|
||||||
$caps = $this->getCapsAndRoles();
|
$caps = $this->getCapsAndRoles();
|
||||||
|
|
||||||
// `manage_links` is a special case
|
// `manage_links` is a special case
|
||||||
@ -247,7 +250,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
// `unfiltered_upload` is a special case
|
// `unfiltered_upload` is a special case
|
||||||
$this->assertFalse( defined( 'ALLOW_UNFILTERED_UPLOADS' ) );
|
$this->assertFalse( defined( 'ALLOW_UNFILTERED_UPLOADS' ) );
|
||||||
|
|
||||||
foreach ( $users as $role => $user ) {
|
foreach ( self::$users as $role => $user ) {
|
||||||
|
|
||||||
// make sure the user is valid
|
// make sure the user is valid
|
||||||
$this->assertTrue( $user->exists(), "User with {$role} role does not exist" );
|
$this->assertTrue( $user->exists(), "User with {$role} role does not exist" );
|
||||||
@ -271,16 +274,9 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
|
|
||||||
// test the default roles and caps
|
// test the default roles and caps
|
||||||
function test_all_roles_and_caps() {
|
function test_all_roles_and_caps() {
|
||||||
$users = array(
|
|
||||||
'administrator' => self::factory()->user->create_and_get( array( 'role' => 'administrator' ) ),
|
|
||||||
'editor' => self::factory()->user->create_and_get( array( 'role' => 'editor' ) ),
|
|
||||||
'author' => self::factory()->user->create_and_get( array( 'role' => 'author' ) ),
|
|
||||||
'contributor' => self::factory()->user->create_and_get( array( 'role' => 'contributor' ) ),
|
|
||||||
'subscriber' => self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) ),
|
|
||||||
);
|
|
||||||
$caps = $this->getCapsAndRoles();
|
$caps = $this->getCapsAndRoles();
|
||||||
|
|
||||||
foreach ( $users as $role => $user ) {
|
foreach ( self::$users as $role => $user ) {
|
||||||
|
|
||||||
// make sure the user is valid
|
// make sure the user is valid
|
||||||
$this->assertTrue( $user->exists(), "User with {$role} role does not exist" );
|
$this->assertTrue( $user->exists(), "User with {$role} role does not exist" );
|
||||||
@ -307,13 +303,6 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
|
|
||||||
// special case for the link manager
|
// special case for the link manager
|
||||||
function test_link_manager_caps() {
|
function test_link_manager_caps() {
|
||||||
$users = array(
|
|
||||||
'administrator' => self::factory()->user->create_and_get( array( 'role' => 'administrator' ) ),
|
|
||||||
'editor' => self::factory()->user->create_and_get( array( 'role' => 'editor' ) ),
|
|
||||||
'author' => self::factory()->user->create_and_get( array( 'role' => 'author' ) ),
|
|
||||||
'contributor' => self::factory()->user->create_and_get( array( 'role' => 'contributor' ) ),
|
|
||||||
'subscriber' => self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) ),
|
|
||||||
);
|
|
||||||
$caps = array(
|
$caps = array(
|
||||||
'manage_links' => array( 'administrator', 'editor' ),
|
'manage_links' => array( 'administrator', 'editor' ),
|
||||||
);
|
);
|
||||||
@ -321,7 +310,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
$this->assertSame( '0', get_option( 'link_manager_enabled' ) );
|
$this->assertSame( '0', get_option( 'link_manager_enabled' ) );
|
||||||
|
|
||||||
// no-one should have access to the link manager by default
|
// no-one should have access to the link manager by default
|
||||||
foreach ( $users as $role => $user ) {
|
foreach ( self::$users as $role => $user ) {
|
||||||
foreach ( $caps as $cap => $roles ) {
|
foreach ( $caps as $cap => $roles ) {
|
||||||
$this->assertFalse( $user->has_cap( $cap ), "User with the {$role} role should not have the {$cap} capability" );
|
$this->assertFalse( $user->has_cap( $cap ), "User with the {$role} role should not have the {$cap} capability" );
|
||||||
$this->assertFalse( user_can( $user, $cap ), "User with the {$role} role should not have the {$cap} capability" );
|
$this->assertFalse( user_can( $user, $cap ), "User with the {$role} role should not have the {$cap} capability" );
|
||||||
@ -331,7 +320,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
update_option( 'link_manager_enabled', '1' );
|
update_option( 'link_manager_enabled', '1' );
|
||||||
$this->assertSame( '1', get_option( 'link_manager_enabled' ) );
|
$this->assertSame( '1', get_option( 'link_manager_enabled' ) );
|
||||||
|
|
||||||
foreach ( $users as $role => $user ) {
|
foreach ( self::$users as $role => $user ) {
|
||||||
foreach ( $caps as $cap => $roles ) {
|
foreach ( $caps as $cap => $roles ) {
|
||||||
if ( in_array( $role, $roles, true ) ) {
|
if ( in_array( $role, $roles, true ) ) {
|
||||||
$this->assertTrue( $user->has_cap( $cap ), "User with the {$role} role should have the {$cap} capability" );
|
$this->assertTrue( $user->has_cap( $cap ), "User with the {$role} role should have the {$cap} capability" );
|
||||||
@ -350,18 +339,10 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
|
|
||||||
// special case for unfiltered uploads
|
// special case for unfiltered uploads
|
||||||
function test_unfiltered_upload_caps() {
|
function test_unfiltered_upload_caps() {
|
||||||
$users = array(
|
|
||||||
'administrator' => self::factory()->user->create_and_get( array( 'role' => 'administrator' ) ),
|
|
||||||
'editor' => self::factory()->user->create_and_get( array( 'role' => 'editor' ) ),
|
|
||||||
'author' => self::factory()->user->create_and_get( array( 'role' => 'author' ) ),
|
|
||||||
'contributor' => self::factory()->user->create_and_get( array( 'role' => 'contributor' ) ),
|
|
||||||
'subscriber' => self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) ),
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->assertFalse( defined( 'ALLOW_UNFILTERED_UPLOADS' ) );
|
$this->assertFalse( defined( 'ALLOW_UNFILTERED_UPLOADS' ) );
|
||||||
|
|
||||||
// no-one should have this cap
|
// no-one should have this cap
|
||||||
foreach ( $users as $role => $user ) {
|
foreach ( self::$users as $role => $user ) {
|
||||||
$this->assertFalse( $user->has_cap( 'unfiltered_upload' ), "User with the {$role} role should not have the unfiltered_upload capability" );
|
$this->assertFalse( $user->has_cap( 'unfiltered_upload' ), "User with the {$role} role should not have the unfiltered_upload capability" );
|
||||||
$this->assertFalse( user_can( $user, 'unfiltered_upload' ), "User with the {$role} role should not have the unfiltered_upload capability" );
|
$this->assertFalse( user_can( $user, 'unfiltered_upload' ), "User with the {$role} role should not have the unfiltered_upload capability" );
|
||||||
}
|
}
|
||||||
@ -375,7 +356,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
}
|
}
|
||||||
$caps = $this->getCapsAndRoles();
|
$caps = $this->getCapsAndRoles();
|
||||||
|
|
||||||
$user = self::factory()->user->create_and_get( array( 'role' => 'administrator' ) );
|
$user = self::$users['administrator'];
|
||||||
grant_super_admin( $user->ID );
|
grant_super_admin( $user->ID );
|
||||||
|
|
||||||
$this->assertTrue( is_super_admin( $user->ID ) );
|
$this->assertTrue( is_super_admin( $user->ID ) );
|
||||||
@ -413,7 +394,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
|
|
||||||
// a user with multiple roles
|
// a user with multiple roles
|
||||||
function test_user_subscriber_contributor() {
|
function test_user_subscriber_contributor() {
|
||||||
$user = self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) );
|
$user = self::$users['subscriber'];
|
||||||
|
|
||||||
// make sure the user is valid
|
// make sure the user is valid
|
||||||
$this->assertTrue( $user->exists(), "User does not exist" );
|
$this->assertTrue( $user->exists(), "User does not exist" );
|
||||||
@ -434,6 +415,11 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
$this->assertFalse( user_can( $user, $cap ), "User should not have the {$cap} capability" );
|
$this->assertFalse( user_can( $user, $cap ), "User should not have the {$cap} capability" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$user->remove_role( 'contributor' );
|
||||||
|
// user should have one role now
|
||||||
|
$this->assertEquals( array( 'subscriber' ), $user->roles );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// newly added empty role
|
// newly added empty role
|
||||||
@ -719,8 +705,8 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
function test_post_meta_caps() {
|
function test_post_meta_caps() {
|
||||||
// simple tests for some common meta capabilities
|
// simple tests for some common meta capabilities
|
||||||
|
|
||||||
// Make our author
|
// Get our author
|
||||||
$author = new WP_User( self::factory()->user->create( array( 'role' => 'author' ) ) );
|
$author = self::$users['author'];
|
||||||
|
|
||||||
// make a post
|
// make a post
|
||||||
$post = self::factory()->post->create( array( 'post_author' => $author->ID, 'post_type' => 'post' ) );
|
$post = self::factory()->post->create( array( 'post_author' => $author->ID, 'post_type' => 'post' ) );
|
||||||
@ -809,8 +795,8 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
$post = self::factory()->post->create( array( 'post_author' => 0, 'post_type' => 'post', 'post_status' => $status ) );
|
$post = self::factory()->post->create( array( 'post_author' => 0, 'post_type' => 'post', 'post_status' => $status ) );
|
||||||
|
|
||||||
// Add an editor and contributor
|
// Add an editor and contributor
|
||||||
$editor = self::factory()->user->create_and_get( array( 'role' => 'editor' ) );
|
$editor = self::$users['editor'];
|
||||||
$contributor = self::factory()->user->create_and_get( array( 'role' => 'contributor' ) );
|
$contributor = self::$users['contributor'];
|
||||||
|
|
||||||
// editor can edit, view, and trash
|
// editor can edit, view, and trash
|
||||||
$this->assertTrue( $editor->has_cap( 'edit_post', $post ) );
|
$this->assertTrue( $editor->has_cap( 'edit_post', $post ) );
|
||||||
@ -891,8 +877,8 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
function test_page_meta_caps() {
|
function test_page_meta_caps() {
|
||||||
// simple tests for some common meta capabilities
|
// simple tests for some common meta capabilities
|
||||||
|
|
||||||
// Make our author
|
// Get our author
|
||||||
$author = new WP_User( self::factory()->user->create( array( 'role' => 'author' ) ) );
|
$author = self::$users['author'];
|
||||||
|
|
||||||
// make a page
|
// make a page
|
||||||
$page = self::factory()->post->create( array( 'post_author' => $author->ID, 'post_type' => 'page' ) );
|
$page = self::factory()->post->create( array( 'post_author' => $author->ID, 'post_type' => 'page' ) );
|
||||||
@ -901,10 +887,10 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
$this->assertTrue($author->exists(), "Problem getting user " . $author->ID);
|
$this->assertTrue($author->exists(), "Problem getting user " . $author->ID);
|
||||||
|
|
||||||
// add some other users
|
// add some other users
|
||||||
$admin = new WP_User( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
|
$admin = self::$users['administrator'];
|
||||||
$author_2 = new WP_User( self::factory()->user->create( array( 'role' => 'author' ) ) );
|
$author_2 = new WP_User( self::factory()->user->create( array( 'role' => 'author' ) ) );
|
||||||
$editor = new WP_User( self::factory()->user->create( array( 'role' => 'editor' ) ) );
|
$editor = self::$users['editor'];
|
||||||
$contributor = new WP_User( self::factory()->user->create( array( 'role' => 'contributor' ) ) );
|
$contributor = self::$users['contributor'];
|
||||||
|
|
||||||
// administrators, editors and the post owner can edit it
|
// administrators, editors and the post owner can edit it
|
||||||
$this->assertTrue($admin->has_cap('edit_page', $page));
|
$this->assertTrue($admin->has_cap('edit_page', $page));
|
||||||
@ -1019,7 +1005,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function test_subscriber_cant_edit_posts() {
|
function test_subscriber_cant_edit_posts() {
|
||||||
$user = new WP_User( self::factory()->user->create( array( 'role' => 'subscriber' ) ) );
|
$user = self::$users['subscriber'];
|
||||||
wp_set_current_user( $user->ID );
|
wp_set_current_user( $user->ID );
|
||||||
|
|
||||||
$post = self::factory()->post->create( array( 'post_author' => 1 ) );
|
$post = self::factory()->post->create( array( 'post_author' => 1 ) );
|
||||||
@ -1034,8 +1020,8 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$user = new WP_User( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
|
$user = self::$users['administrator'];
|
||||||
$other_user = new WP_User( self::factory()->user->create( array( 'role' => 'subscriber' ) ) );
|
$other_user = self::$users['subscriber'];
|
||||||
|
|
||||||
wp_set_current_user( $user->ID );
|
wp_set_current_user( $user->ID );
|
||||||
|
|
||||||
@ -1048,7 +1034,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$user = new WP_User( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
|
$user = self::$users['administrator'];
|
||||||
|
|
||||||
wp_set_current_user( $user->ID );
|
wp_set_current_user( $user->ID );
|
||||||
|
|
||||||
@ -1061,9 +1047,7 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
function test_contributor_cannot_edit_scheduled_post() {
|
function test_contributor_cannot_edit_scheduled_post() {
|
||||||
|
|
||||||
// Add a contributor
|
// Add a contributor
|
||||||
$contributor = $this->factory->user->create_and_get( array(
|
$contributor = self::$users['contributor'];
|
||||||
'role' => 'contributor',
|
|
||||||
) );
|
|
||||||
|
|
||||||
// Give them a scheduled post
|
// Give them a scheduled post
|
||||||
$post = $this->factory->post->create_and_get( array(
|
$post = $this->factory->post->create_and_get( array(
|
||||||
@ -1128,8 +1112,8 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
$post_id = self::factory()->post->create( array( 'post_type' => 'existed' ) );
|
$post_id = self::factory()->post->create( array( 'post_type' => 'existed' ) );
|
||||||
_unregister_post_type( 'existed' );
|
_unregister_post_type( 'existed' );
|
||||||
|
|
||||||
$subscriber_id = self::factory()->user->create( array( 'role' => 'subscriber' ) );
|
$subscriber_id = self::$users['subscriber']->ID;
|
||||||
$editor_id = self::factory()->user->create( array( 'role' => 'editor' ) );
|
$editor_id = self::$users['editor']->ID;
|
||||||
|
|
||||||
$this->setExpectedIncorrectUsage( 'map_meta_cap' );
|
$this->setExpectedIncorrectUsage( 'map_meta_cap' );
|
||||||
foreach ( array( 'delete_post', 'edit_post', 'read_post', 'publish_post' ) as $cap ) {
|
foreach ( array( 'delete_post', 'edit_post', 'read_post', 'publish_post' ) as $cap ) {
|
||||||
@ -1154,10 +1138,10 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
|
|||||||
|
|
||||||
$cpt = get_post_type_object( 'page_capability' );
|
$cpt = get_post_type_object( 'page_capability' );
|
||||||
|
|
||||||
$admin = self::factory()->user->create_and_get( array( 'role' => 'administrator' ) );
|
$admin = self::$users['administrator'];
|
||||||
$editor = self::factory()->user->create_and_get( array( 'role' => 'editor' ) );
|
$editor = self::$users['editor'];
|
||||||
$author = self::factory()->user->create_and_get( array( 'role' => 'author' ) );
|
$author = self::$users['author'];
|
||||||
$contributor = self::factory()->user->create_and_get( array( 'role' => 'contributor' ) );
|
$contributor = self::$users['contributor'];
|
||||||
|
|
||||||
$this->assertEquals( 'edit_pages', $cpt->cap->edit_posts );
|
$this->assertEquals( 'edit_pages', $cpt->cap->edit_posts );
|
||||||
$this->assertTrue( user_can( $admin->ID, $cpt->cap->edit_posts ) );
|
$this->assertTrue( user_can( $admin->ID, $cpt->cap->edit_posts ) );
|
||||||
|
Loading…
Reference in New Issue
Block a user