wordpress-develop/tests/phpunit/tests/user/countUsers.php
John Blackbourn c759785cea Build/Test Tools: Automatically skip tests in the ms-required and ms-excluded groups.
This removes the need to manually call `$this->skipWithMultisite()` and `$this->skipWithoutMultisite()` from within the test when the test only runs without Multisite or only runs on Multisite, respectively.

Props jdgrimes for the suggestion.

Fixes #40531


git-svn-id: https://develop.svn.wordpress.org/trunk@40564 602fd350-edb4-49c9-b593-d223f7449a82
2017-05-01 20:34:10 +00:00

207 lines
4.6 KiB
PHP

<?php
/**
* @group user
*/
class Tests_User_CountUsers extends WP_UnitTestCase {
/**
* @ticket 22993
*
* @dataProvider data_count_users_strategies
* @group ms-excluded
*/
public function test_count_users_is_accurate( $strategy ) {
// Setup users
$admin = self::factory()->user->create( array(
'role' => 'administrator',
) );
$editor = self::factory()->user->create( array(
'role' => 'editor',
) );
$author = self::factory()->user->create( array(
'role' => 'author',
) );
$contributor = self::factory()->user->create( array(
'role' => 'contributor',
) );
$subscriber = self::factory()->user->create( array(
'role' => 'subscriber',
) );
$none = self::factory()->user->create( array(
'role' => '',
) );
$nobody = self::factory()->user->create( array(
'role' => '',
) );
// Test user counts
$count = count_users( $strategy );
$this->assertEquals( 8, $count['total_users'] );
$this->assertEquals( array(
'administrator' => 2,
'editor' => 1,
'author' => 1,
'contributor' => 1,
'subscriber' => 1,
'none' => 2,
), $count['avail_roles'] );
}
/**
* @ticket 22993
* @group multisite
* @group ms-required
*
* @dataProvider data_count_users_strategies
*/
public function test_count_users_multisite_is_accurate( $strategy ) {
// Setup users
$admin = self::factory()->user->create( array(
'role' => 'administrator',
) );
$editor = self::factory()->user->create( array(
'role' => 'editor',
) );
$author = self::factory()->user->create( array(
'role' => 'author',
) );
$contributor = self::factory()->user->create( array(
'role' => 'contributor',
) );
$subscriber = self::factory()->user->create( array(
'role' => 'subscriber',
) );
$none = self::factory()->user->create( array(
'role' => '',
) );
$nobody = self::factory()->user->create( array(
'role' => '',
) );
// Setup blogs
$blog_1 = (int) self::factory()->blog->create( array(
'user_id' => $editor,
) );
$blog_2 = (int) self::factory()->blog->create( array(
'user_id' => $author,
) );
// Add users to blogs
add_user_to_blog( $blog_1, $subscriber, 'editor' );
add_user_to_blog( $blog_2, $none, 'contributor' );
// Test users counts on root site
$count = count_users( $strategy );
$this->assertEquals( 8, $count['total_users'] );
$this->assertEquals( array(
'administrator' => 2,
'editor' => 1,
'author' => 1,
'contributor' => 1,
'subscriber' => 1,
'none' => 0,
), $count['avail_roles'] );
// Test users counts on blog 1
switch_to_blog( $blog_1 );
$count = count_users( $strategy );
restore_current_blog();
$this->assertEquals( 2, $count['total_users'] );
$this->assertEquals( array(
'administrator' => 1,
'editor' => 1,
'none' => 0,
), $count['avail_roles'] );
// Test users counts on blog 2
switch_to_blog( $blog_2 );
$count = count_users( $strategy );
restore_current_blog();
$this->assertEquals( 2, $count['total_users'] );
$this->assertEquals( array(
'administrator' => 1,
'contributor' => 1,
'none' => 0,
), $count['avail_roles'] );
}
/**
* @ticket 34495
*
* @dataProvider data_count_users_strategies
*/
public function test_count_users_is_accurate_with_multiple_roles( $strategy ) {
// Setup users
$admin = self::factory()->user->create( array(
'role' => 'administrator',
) );
$editor = self::factory()->user->create( array(
'role' => 'editor',
) );
get_userdata( $editor )->add_role( 'author' );
$this->assertEquals( array(
'editor',
'author'
), get_userdata( $editor )->roles );
// Test user counts
$count = count_users( $strategy );
$this->assertEquals( 3, $count['total_users'] );
$this->assertEquals( array(
'administrator' => 2,
'editor' => 1,
'author' => 1,
'none' => 0,
), $count['avail_roles'] );
}
/**
* @ticket 29785
*
* @dataProvider data_count_users_strategies
*/
public function test_count_users_should_not_count_users_who_are_not_in_posts_table( $strategy ) {
global $wpdb;
// Get a 'before' count for comparison.
$count = count_users( $strategy );
$u = self::factory()->user->create( array(
'role' => 'editor',
) );
// Manually delete the user, but leave the capabilities usermeta.
$wpdb->delete( $wpdb->users, array(
'ID' => $u,
) );
$count2 = count_users( $strategy );
$this->assertEqualSets( $count, $count2 );
}
function data_count_users_strategies() {
return array(
array(
'time',
),
array(
'memory',
),
);
}
}