+ Add unit test case for gitea->bindUser.

This commit is contained in:
caoyanyi
2023-12-21 16:56:25 +08:00
parent 2fdf2cd18b
commit 0cc2dd536a
4 changed files with 90 additions and 43 deletions
+22 -30
View File
@@ -55,52 +55,44 @@ class giteaModel extends model
*/
public function bindUser(int $giteaID, array $users, array $giteaNames): bool
{
$accountList = array();
$repeatUsers = array();
foreach($users as $openID => $user)
$userPairs = array();
foreach($users as $openID => $account)
{
if(empty($user)) continue;
if(empty($account)) continue;
if(isset($accountList[$user])) $repeatUsers[] = $user;
$accountList[$user] = $openID;
if(in_array($account, $userPairs)) $repeatUsers[] = $account;
$userPairs[$openID] = $account;
}
$userList = $this->loadModel('user')->getRealNameAndEmails($repeatUsers);
/* Check user repeat bind. */
if(count($repeatUsers))
if($repeatUsers)
{
$userList = $this->loadModel('user')->getRealNameAndEmails($repeatUsers);
dao::$errors = sprintf($this->lang->gitea->bindUserError, join(',', helper::arrayColumn($userList, 'realname')));
return false;
}
$user = new stdclass();
$user->providerID = $giteaID;
$user->providerType = 'gitea';
$bindedUsers = $this->dao->select('openID,account')->from(TABLE_OAUTH)
->where('providerType')->eq('gitea')
->andWhere('providerID')->eq($giteaID)
->fetchPairs();
$this->dao->delete()->from(TABLE_OAUTH)->where('providerType')->eq('gitea')->andWhere('providerID')->eq($giteaID)->exec();
$oldUsers = $this->dao->select('*')->from(TABLE_OAUTH)->where('providerType')->eq($user->providerType)->andWhere('providerID')->eq($user->providerID)->fetchAll('openID');
foreach($users as $openID => $account)
$this->loadModel('action');
foreach($userPairs as $openID => $account)
{
$existAccount = isset($oldUsers[$openID]) ? $oldUsers[$openID] : '';
/* If user binded user is change, delete it. */
if($existAccount && $existAccount->account != $account)
{
$this->dao->delete()->from(TABLE_OAUTH)
->where('openID')->eq($openID)
->andWhere('providerType')->eq($user->providerType)
->andWhere('providerID')->eq($user->providerID)
->exec();
$this->loadModel('action')->create('giteauser', $giteaID, 'unbind', '', $giteaNames[$openID]);
}
if(isset($bindedUsers[$openID]) && $bindedUsers[$openID] != $account) $this->action->create('giteauser', $giteaID, 'unbind', '', $giteaNames[$openID]);
/* Add zentao user and gitea user binded. */
if(!$existAccount || $existAccount->account != $account)
{
if(!$account) continue;
$user->account = $account;
$user->openID = $openID;
$this->dao->insert(TABLE_OAUTH)->data($user)->exec();
$this->loadModel('action')->create('giteauser', $giteaID, 'bind', '', $giteaNames[$openID]);
}
$user = new stdclass();
$user->providerID = $giteaID;
$user->providerType = 'gitea';
$user->account = $account;
$user->openID = $openID;
$this->dao->insert(TABLE_OAUTH)->data($user)->exec();
$this->action->create('giteauser', $giteaID, 'bind', '', $giteaNames[$openID]);
}
return !dao::isError();
}
+16 -10
View File
@@ -1,26 +1,32 @@
<?php
class giteaTest
{
public $tester;
private $gitea;
public function __construct()
{
global $tester;
$this->tester = $tester;
$this->gitea = $this->tester->loadModel('gitea');
$this->gitea = $tester->loadModel('gitea');
}
/**
* Get gitea tasks.
* Test bindUser method.
*
* @param int $id
* @param array $userList
* @access public
* @return array
* @return array|string
*/
public function getTasks($id)
public function bindUserTester(array $userList): array|string
{
$tasks = $this->gitea->getTasks($id);
if(empty($tasks)) return 0;
return $tasks;
$nameList = array();
foreach($userList as $openID => $user) $nameList[$openID] = 'Gitea-' . $user;
$result = $this->gitea->bindUser(1, $userList, $nameList);
if(!$result) return dao::getError();
return $this->gitea->dao->select('*')->from(TABLE_OAUTH)
->where('providerID')->eq(1)
->andWhere('providerType')->eq('gitea')
->fetchAll();
}
}
+49
View File
@@ -0,0 +1,49 @@
#!/usr/bin/env php
<?php
/**
title=测试 giteaModel::bindUser();
timeout=0
cid=1
- 用户列表为空 @0
- 用户列表不为空
- 第0条的account属性 @user1
- 第2条的account属性 @user3
- 用户列表不为空,但有重复 @不能重复绑定用户 用户1
- 用户列表不为空,且绑定关系发生变化
- 第0条的account属性 @user1
- 第2条的account属性 @user4
- 用户列表不为空,且删除了一个用户绑定
- 第0条的account属性 @user1
- 第1条的account属性 @user4
*/
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/gitea.class.php';
zdTable('user')->gen(10);
zdTable('oauth')->config('oauth')->gen(10);
su('admin');
$gitea = new giteaTest();
$userList = array();
r($gitea->bindUserTester($userList)) && p() && e('0'); // 用户列表为空
$userList = array(
1 => 'user1',
2 => 'user2',
3 => 'user3',
);
r($gitea->bindUserTester($userList)) && p('0:account;2:account') && e('user1,user3'); // 用户列表不为空
$userList[3] = 'user1';
r($gitea->bindUserTester($userList)) && p() && e('不能重复绑定用户 用户1'); // 用户列表不为空,但有重复
$userList[3] = 'user4';
r($gitea->bindUserTester($userList)) && p('0:account;2:account') && e('user1,user4'); // 用户列表不为空,且绑定关系发生变化
$userList[2] = '';
r($gitea->bindUserTester($userList)) && p('0:account;1:account') && e('user1,user4'); // 用户列表不为空,且删除了一个用户绑定
+3 -3
View File
@@ -3,11 +3,11 @@ author: Yanyi Cao
version: "1.0"
fields:
- field: account
range: user1
prefix: user
range: 1-10
- field: openID
range: 5
range: 1-10
- field: providerType
range: gitea
- field: providerID
range: 4