Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions features/context.feature
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,44 @@ Feature: Context handling via --context global flag
User ID in admin_init: {EDITOR_ID}
"""

Scenario: Admin context resolves a super admin on multisite when no user is specified
Given a WP multisite install
And I run `wp user create anotheradmin anotheradmin@example.com --role=administrator`
And I run `wp eval 'update_site_option( "site_admins", array( "anotheradmin" ) );'`
And I run `wp --context=admin eval 'echo "Current user: " . wp_get_current_user()->user_login;'`
Then STDOUT should contain:
"""
Current user: anotheradmin
"""

Scenario: Admin context resolves an administrator on single site when no user is specified
Given a WP install
When I run `wp --context=admin eval 'echo "User ID: " . get_current_user_id();'`
Then STDOUT should be:
"""
User ID: 1
"""

Scenario: Admin context emits error when no suitable admin user is found on multisite
Given a WP multisite install
And I run `wp eval 'update_site_option( "site_admins", array() );'`
And I try `wp --context=admin eval ''`
Then the return code should be 1
And STDERR should contain:
"""
Error: No super admin user found. Specify one with --user=<login>.
"""

Scenario: Admin context emits error when no administrator is found on single site
Given a WP install
When I run `wp user update 1 --role=subscriber`
And I try `wp --context=admin eval ''`
Then the return code should be 1
And STDERR should contain:
"""
Error: No administrator user found. Specify one with --user=<login>.
"""

Scenario: Admin context throws an error for a non-existent user
Given a WP install
And a wp-cli.yml file:
Expand Down
45 changes: 43 additions & 2 deletions php/WP_CLI/Context/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use WP_CLI;
use WP_CLI\Context;
use WP_CLI\Fetchers\User;
use WP_User;

/**
* Context which simulates the administrator backend.
Expand Down Expand Up @@ -48,14 +49,15 @@ function () use ( $config ) {
$user = $fetcher->get_check( $config['user'] );
$admin_user_id = $user->ID;
} else {
// TODO: Add logic to find an administrator user.
$admin_user_id = 1;
$admin_user_id = $this->find_admin_user_id();
}

/**
* @var int<1, max> $admin_user_id
*/

WP_CLI::debug( sprintf( 'Continuing as admin user %d', $admin_user_id ), Context::DEBUG_GROUP );

$this->log_in_as_admin_user( $admin_user_id );
},
defined( 'PHP_INT_MIN' ) ? PHP_INT_MIN : -2147483648, // phpcs:ignore PHPCompatibility.Constants.NewConstants.php_int_minFound
Expand All @@ -72,6 +74,45 @@ function () {
);
}

/**
* Find a suitable admin user ID for the current environment.
*
* On multisite, resolves a super admin via get_super_admins().
* On single site, finds a user with the administrator role.
*
* @return int<1, max> Admin user ID.
*/
private function find_admin_user_id() {
if ( is_multisite() ) {
$super_admins = get_super_admins();

foreach ( $super_admins as $super_admin_login ) {
$user = get_user_by( 'login', $super_admin_login );
if ( $user instanceof WP_User ) {
/** @var int<1, max> */
return $user->ID;
}
}

WP_CLI::error( 'No super admin user found. Specify one with --user=<login>.' );
}

$admins = get_users(
[
'role' => 'administrator',
'number' => 1,
'orderby' => 'ID',
'order' => 'ASC',
]
);

if ( ! empty( $admins ) ) {
return $admins[0]->ID;
}

WP_CLI::error( 'No administrator user found. Specify one with --user=<login>.' );
}

/**
* Get a fake admin page filename that reflects the current command.
*
Expand Down
Loading