Files
unsupervised-scheduler/tests/Unit/Auth/RoleManagerTest.php
T
thatguygriff b4acae34a3 Grant studio-admin capabilities to WordPress administrators
WordPress administrators (manage_options) now implicitly hold every
studio-admin capability via a user_has_cap filter, so the site owner runs
the studio without being assigned the separate us_studio_admin role. The
grant persists nothing and is removed on deactivation. The us_studio_admin
role still exists for non-administrator staff and does NOT confer any core
WordPress admin powers.

Also re-gate the studio-wide "Scheduler" dashboard off manage_options onto
a new view_all_lessons capability (added to the studio-admin cap set), so a
us_studio_admin user can see it too — previously it was administrator-only.

- RoleManager: STUDIO_ADMIN_CAPS constant, CAP_VIEW_ALL_LESSONS,
  grantStudioCapsToAdministrators() user_has_cap filter
- AdminMenu + LessonController: Scheduler gated on view_all_lessons
- Docs: user-roles.md cap matrix + administrator note; lesson-booking.md
- Tests: administrators receive studio caps; non-admins do not

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-05 12:02:54 -03:00

116 lines
4.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Auth;
use Brain\Monkey\Functions;
use Unsupervised\Schedular\Auth\RoleManager;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class RoleManagerTest extends TestCase
{
public function testRegisterAddsInitHookAndCapFilter(): void
{
Functions\expect('add_action')
->once()
->with('init', \Mockery::any());
Functions\expect('add_filter')
->once()
->with('user_has_cap', \Mockery::any(), 10, 1);
(new RoleManager())->register();
}
public function testGrantsStudioCapsToAdministrators(): void
{
$result = (new RoleManager())->grantStudioCapsToAdministrators(['manage_options' => true]);
foreach (RoleManager::STUDIO_ADMIN_CAPS as $cap) {
self::assertTrue($result[$cap], "administrator should be granted {$cap}");
}
}
public function testDoesNotGrantStudioCapsToNonAdministrators(): void
{
$result = (new RoleManager())->grantStudioCapsToAdministrators(['read' => true]);
self::assertArrayNotHasKey(RoleManager::CAP_MANAGE_OFFERINGS, $result);
self::assertSame(['read' => true], $result);
}
public function testCreateRolesSkipsExistingRoles(): void
{
Functions\when('get_role')->alias(static fn() => new \stdClass());
Functions\expect('add_role')->never();
(new RoleManager())->createRoles();
}
public function testCreateRolesAddsInstructorRoleWithCorrectCaps(): void
{
Functions\when('get_role')->alias(static function (string $role): ?object {
return $role === RoleManager::INSTRUCTOR ? null : new \stdClass();
});
Functions\expect('add_role')
->once()
->with(
RoleManager::INSTRUCTOR,
\Mockery::any(),
\Mockery::on(static function (array $caps): bool {
return ($caps['read'] ?? false) === true
&& ($caps[RoleManager::CAP_MANAGE_AVAILABILITY] ?? false) === true
&& ($caps[RoleManager::CAP_VIEW_LESSONS] ?? false) === true;
})
);
(new RoleManager())->createRoles();
}
public function testCreateRolesAddsStudioAdminRoleWithCorrectCaps(): void
{
Functions\when('get_role')->alias(static function (string $role): ?object {
return $role === RoleManager::STUDIO_ADMIN ? null : new \stdClass();
});
Functions\expect('add_role')
->once()
->with(
RoleManager::STUDIO_ADMIN,
\Mockery::any(),
\Mockery::on(static function (array $caps): bool {
return ($caps['read'] ?? false) === true
&& ($caps[RoleManager::CAP_MANAGE_INSTRUCTORS] ?? false) === true
&& ($caps[RoleManager::CAP_MANAGE_OFFERINGS] ?? false) === true
&& ($caps[RoleManager::CAP_MANAGE_POLICIES] ?? false) === true
&& ($caps[RoleManager::CAP_MANAGE_BILLING] ?? false) === true
&& ($caps[RoleManager::CAP_VIEW_ALL_PAYMENTS] ?? false) === true;
})
);
(new RoleManager())->createRoles();
}
public function testCreateRolesAddsStudentRoleWithCorrectCaps(): void
{
Functions\when('get_role')->alias(static function (string $role): ?object {
return $role === RoleManager::STUDENT ? null : new \stdClass();
});
Functions\expect('add_role')
->once()
->with(
RoleManager::STUDENT,
\Mockery::any(),
\Mockery::on(static function (array $caps): bool {
return ($caps['read'] ?? false) === true
&& ($caps[RoleManager::CAP_BOOK_LESSON] ?? false) === true
&& ($caps[RoleManager::CAP_VIEW_LESSONS] ?? false) === true;
})
);
(new RoleManager())->createRoles();
}
}