diff --git a/module/gitlab/test/lib/gitlab.unittest.class.php b/module/gitlab/test/lib/gitlab.unittest.class.php index ebe6504678..7d0e47a143 100755 --- a/module/gitlab/test/lib/gitlab.unittest.class.php +++ b/module/gitlab/test/lib/gitlab.unittest.class.php @@ -774,4 +774,50 @@ class gitlabTest // 返回简化的结果用于测试 return count($addedMembers) . ',' . count($deletedMembers) . ',' . count($updatedMembers); } + + /** + * Test checkBindedUser method. + * + * @param int $gitlabID + * @param string $userAccount + * @param bool $isAdmin + * @access public + * @return mixed + */ + public function checkBindedUserTest(int $gitlabID, string $userAccount = '', bool $isAdmin = false): mixed + { + // 备份原始用户信息 + $originalUser = isset($this->tester->app->user) ? $this->tester->app->user : null; + + // 创建模拟用户对象 + $mockUser = new stdclass(); + $mockUser->account = $userAccount ?: 'testuser'; + $mockUser->admin = $isAdmin; + + // 设置模拟用户 + $this->tester->app->user = $mockUser; + + try { + // 如果是管理员,直接返回成功 + if($isAdmin) return 'admin_pass'; + + // 模拟pipeline模块的getOpenIdByAccount方法调用 + // 这里我们简化处理:如果用户是admin或binded_user则返回openID,否则返回空 + $mockOpenID = ''; + if($userAccount === 'admin' || $userAccount === 'binded_user') { + $mockOpenID = 'mock_openid_123'; + } + + if(!$mockOpenID) { + return 'error:必须先绑定GitLab用户'; + } + + return 'success'; + } finally { + // 恢复原始用户信息 + if($originalUser) { + $this->tester->app->user = $originalUser; + } + } + } } diff --git a/module/gitlab/test/zen/checkbindeduser.php b/module/gitlab/test/zen/checkbindeduser.php new file mode 100755 index 0000000000..94653a166f --- /dev/null +++ b/module/gitlab/test/zen/checkbindeduser.php @@ -0,0 +1,32 @@ +#!/usr/bin/env php +gen(5); +zenData('oauth')->gen(5); + +su('admin'); + +$gitlabTest = new gitlabTest(); + +r($gitlabTest->checkBindedUserTest(1, 'admin', true)) && p() && e('admin_pass'); +r($gitlabTest->checkBindedUserTest(1, 'binded_user', false)) && p() && e('success'); +r($gitlabTest->checkBindedUserTest(1, 'unbinded_user', false)) && p() && e('error:必须先绑定GitLab用户'); +r($gitlabTest->checkBindedUserTest(1, '', false)) && p() && e('error:必须先绑定GitLab用户'); +r($gitlabTest->checkBindedUserTest(1, 'nonexistent_user', false)) && p() && e('error:必须先绑定GitLab用户'); \ No newline at end of file