feat: add configurable role for Mahak-created users
Build plugin release / release (push) Successful in 5s

This commit is contained in:
2026-08-24 10:39:32 +03:30
parent e7d718cd0e
commit 5f422ac0e4
6 changed files with 131 additions and 19 deletions
+44 -5
View File
@@ -7,6 +7,7 @@ const TEST_PLUGIN = 'mahak-authenticate-bridge/dabestaniha-authenticate-bridge.p
const TEST_SECRET = 'integration-test-secret';
const TEST_EMAIL = 'mahak-auth-user@example.test';
const TEST_NAME = 'Mahak Auth User';
const TEST_ROLE_EMAIL = 'mahak-role-user@example.test';
function fail(string $message): void
{
@@ -86,6 +87,8 @@ function activate_and_configure_plugin(): void
update_option('mahak_user_identifier', 'email');
update_option('mahak_login_route', 'mahak/login');
update_option('mahak_after_login_route', 'wp-admin/profile.php');
update_option('default_role', 'subscriber');
update_option('mahak_new_user_role', 'wordpress_default');
pass('Plugin activated and configured');
}
@@ -213,6 +216,10 @@ function test_valid_authentication(): void
assert_true($user !== false, 'Authenticated user was not created.');
assert_true($user->display_name === TEST_NAME, 'Authenticated user display name was not saved.');
assert_true(
$user->roles === ['subscriber'],
'New user did not receive the plugin-configured Subscriber role.'
);
$cookieHeader = cookie_header_from_response($response['headers']);
$profileResponse = http_request(TEST_SITE_URL.'/wp-admin/profile.php', $cookieHeader);
@@ -222,7 +229,38 @@ function test_valid_authentication(): void
'Issued cookies did not authenticate a follow-up WordPress admin request.'
);
pass('Valid JWT creates/logs in user and issued cookies authenticate follow-up request');
pass('Valid JWT creates a Subscriber and issued cookies authenticate follow-up requests');
}
function test_configured_new_user_role_is_enforced(): void
{
$smsRoleOverride = function (int $userId): void {
(new WP_User($userId))->set_role('administrator');
};
add_action('user_register', $smsRoleOverride);
$controller = new Dabestaniha\AuthenticateBridge\App\Http\Controllers\AuthenticateController();
$userId = $controller->find_or_create_user('email', TEST_ROLE_EMAIL, 'Role Test User');
remove_action('user_register', $smsRoleOverride);
$user = get_userdata($userId);
assert_true(
$user !== false && $user->roles === ['subscriber'],
'Configured role was not enforced after another plugin changed the role during user_register.'
);
$user->set_role('author');
$existingUserId = $controller->find_or_create_user('email', TEST_ROLE_EMAIL, 'Role Test User');
$existingUser = get_userdata($existingUserId);
assert_true($existingUserId === $userId, 'Existing user was not reused.');
assert_true(
$existingUser !== false && $existingUser->roles === ['author'],
'Existing user role was changed during authentication.'
);
pass('Configured role overrides creation hooks without changing existing users');
}
function test_invalid_authentication(): void
@@ -310,7 +348,7 @@ function test_debug_error_details(): void
function test_plugin_update_discovery(): void
{
$packageUrl = 'https://mahgit.ir/dabestaniha/mahak-authenticate-bridge/releases/download/v2.3.0/mahak-authenticate-bridge.zip';
$packageUrl = 'https://mahgit.ir/dabestaniha/mahak-authenticate-bridge/releases/download/v2.4.0/mahak-authenticate-bridge.zip';
$mockRelease = function ($response, array $request, string $url) use ($packageUrl) {
if ($url !== 'https://mahgit.ir/api/v1/repos/dabestaniha/mahak-authenticate-bridge/releases/latest') {
return $response;
@@ -319,8 +357,8 @@ function test_plugin_update_discovery(): void
return [
'headers' => [],
'body' => json_encode([
'tag_name' => 'v2.3.0',
'html_url' => 'https://mahgit.ir/dabestaniha/mahak-authenticate-bridge/releases/tag/v2.3.0',
'tag_name' => 'v2.4.0',
'html_url' => 'https://mahgit.ir/dabestaniha/mahak-authenticate-bridge/releases/tag/v2.4.0',
'body' => 'Test release',
'draft' => false,
'prerelease' => false,
@@ -346,7 +384,7 @@ function test_plugin_update_discovery(): void
remove_filter('pre_http_request', $mockRelease, 10);
assert_true(isset($updates->response[TEST_PLUGIN]), 'A newer Mahgit release was not offered as a WordPress update.');
assert_true($updates->response[TEST_PLUGIN]->new_version === '2.3.0', 'The offered plugin version was incorrect.');
assert_true($updates->response[TEST_PLUGIN]->new_version === '2.4.0', 'The offered plugin version was incorrect.');
assert_true($updates->response[TEST_PLUGIN]->package === $packageUrl, 'The release package URL was incorrect.');
pass('Published Mahgit releases are discovered by the WordPress updater');
@@ -357,6 +395,7 @@ install_wordpress();
activate_and_configure_plugin();
wait_for_http();
test_valid_authentication();
test_configured_new_user_role_is_enforced();
test_invalid_authentication();
test_debug_error_details();
test_plugin_update_discovery();