guard = new StudentAdminGuard(); Functions\when('wp_doing_ajax')->justReturn(false); } /** * @param list $held Capabilities the user is treated as holding. */ private function stubUser(bool $loggedIn, array $held = []): void { Functions\when('is_user_logged_in')->justReturn($loggedIn); Functions\when('current_user_can')->alias(static fn (string $cap): bool => in_array($cap, $held, true)); } public function testBlocksStudentWithNoBackOfficeCapabilities(): void { // A student holds only front-end capabilities. $this->stubUser(true, [RoleManager::CAP_BOOK_LESSON, RoleManager::CAP_VIEW_LESSONS]); self::assertTrue($this->guard->shouldBlockAdminAccess()); } public function testAllowsInstructor(): void { $this->stubUser(true, [RoleManager::CAP_MANAGE_AVAILABILITY]); self::assertFalse($this->guard->shouldBlockAdminAccess()); } public function testAllowsAdministrator(): void { $this->stubUser(true, ['manage_options']); self::assertFalse($this->guard->shouldBlockAdminAccess()); } public function testDoesNotBlockLoggedOutRequests(): void { $this->stubUser(false); self::assertFalse($this->guard->shouldBlockAdminAccess()); } public function testDoesNotBlockAjaxRequests(): void { Functions\when('wp_doing_ajax')->justReturn(true); $this->stubUser(true, [RoleManager::CAP_BOOK_LESSON]); self::assertFalse($this->guard->shouldBlockAdminAccess()); } public function testHidesAdminBarForStudent(): void { $this->stubUser(true, [RoleManager::CAP_BOOK_LESSON]); self::assertFalse($this->guard->hideAdminBar(true)); } public function testKeepsAdminBarForInstructor(): void { $this->stubUser(true, [RoleManager::CAP_MANAGE_AVAILABILITY]); self::assertTrue($this->guard->hideAdminBar(true)); } public function testLeavesAdminBarUntouchedForLoggedOutVisitor(): void { $this->stubUser(false); self::assertFalse($this->guard->hideAdminBar(false)); } }