diff --git a/module/user/test/model/batchedit.php b/module/user/test/model/batchedit.php deleted file mode 100755 index 2fc4d0e3a2..0000000000 --- a/module/user/test/model/batchedit.php +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env php -gen(15); - -/** - -title=测试 userModel->batchEdit(); -cid=1 -pid=1 - -获取编辑后的第一个用户的account >> newtestuser1 -获取编辑的最后一个用户的真实姓名 >> 新测试用户3 - -*/ - -$user = new userTest(); -$normalUser = array(); -$normalUser['account'] = array(8 => 'newtestuser1', 9 => 'newtestuser2', 10 => 'newtestuser3'); -$normalUser['realname'] = array(8 => '新测试用户1', 9 => '新测试用户2', 10 => '新测试用户3'); -$normalUser['commiter'] = array(8 => 'user1', 9 => 'user2', 10 => 'user3'); -$normalUser['email'] = array(8 => 'testasd@163.com', 9 => '', 10 => '11773@qq.com'); -$normalUser['type'] = array(8 => 'inside', 9 => 'inside', 10 => 'outside'); -$normalUser['join'] = array(8 => '2023-10-01', 9 => '2023-10-01', 10 => '2023-10-01'); -$normalUser['skype'] = array(8 => '', 9 => '', 10 => ''); -$normalUser['qq'] = array(8 => '', 9 => '', 10 => ''); -$normalUser['dingding'] = array(8 => '', 9 => '', 10 => ''); -$normalUser['weixin'] = array(8 => '', 9 => '', 10 => ''); -$normalUser['mobile'] = array(8 => '', 9 => '', 10 => ''); -$normalUser['slack'] = array(8 => '', 9 => '', 10 => ''); -$normalUser['whatsapp'] = array(8 => '', 9 => '', 10 => ''); -$normalUser['phone'] = array(8 => '', 9 => '', 10 => ''); -$normalUser['address'] = array(8 => '', 9 => '', 10 => ''); -$normalUser['zipcode'] = array(8 => '', 9 => '', 10 => ''); -$normalUser['visions'] = array(8 => array('rnd'), 9 => array('rnd', 'lite'), 10 => array('lite')); -$normalUser['dept'] = array(8 => '1', 9 => '1', 10 => '1'); -$normalUser['role'] = array(8 => 'qa', 9 => 'dev', 10 => 'pm'); -$normalUser['password'] = array(8 => 'e10adc3949ba59abbe56e057f20f883e', 9 => 'e10adc3949ba59abbe56e057f20f883e', 10 => 'e10adc3949ba59abbe56e057f20f883e'); - -r($user->batchEditUserTest($normalUser)) && p('8:account') && e('newtestuser1'); //获取编辑后的第一个用户的account -r($user->batchEditUserTest($normalUser)) && p('10:realname') && e('新测试用户3'); //获取编辑的最后一个用户的真实姓名 diff --git a/module/user/test/model/batchupdate.php b/module/user/test/model/batchupdate.php new file mode 100755 index 0000000000..b947809b6a --- /dev/null +++ b/module/user/test/model/batchupdate.php @@ -0,0 +1,79 @@ +#!/usr/bin/env php +batchUpdate(); +cid=1 +pid=1 +*/ +include dirname(__FILE__, 5) . '/test/lib/init.php'; +include dirname(__FILE__, 2) . '/user.class.php'; + +zdTable('user')->gen(2); +zdTable('action')->gen(0); +zdTable('history')->gen(0); + +su('admin'); + +global $app; + +$userTest = new userTest(); + +$random = updateSessionRandom(); +$verify = md5($app->user->password . $random); + +$users1 = array +( + 2 => (object)array('account' => 'user1', 'realname' => 'user11', 'visions' => 'rnd', 'gender' => 'gender') +); + +$users2 = array +( + 2 => (object)array('account' => 'user1', 'realname' => 'user11', 'visions' => 'rnd') +); + +$users3 = array +( + 1 => (object)array('account' => 'admin', 'realname' => 'newAdmin', 'visions' => 'rnd', 'role' => 'newRole') +); + +/** + * 测试传入空数组的情况。 + */ +$result = $userTest->batchUpdateTest(array(), $verify); +r($result) && p('result') && e(0); // 传入空数组,返回 false。 + +/** + * 测试更新用户失败情况。 + */ +$result = $userTest->batchUpdateTest($users1, $verify); +r($result) && p('result') && e(0); // 更新用户失败,返回 false。 +r($result) && p('errors:gender') && e('『性别』不符合格式,应当为:『/f|m/』。'); // 更新用户失败,提示错误信息。 + +/** + * 测试更新用户成功的情况。 + */ +$result = $userTest->batchUpdateTest($users2, $verify); +r($result) && p('result') && e(1); // 更新用户成功,返回 true。 + +$action = $tester->dao->select('*')->from(TABLE_ACTION)->orderBy('id_desc')->limit(1)->fetch(); +r($action) && p('objectType,objectID,action') && e('user,2,edited'); // 创建日志成功,最后一条记录的对象类型是 user,对象 id 是 2,动作是 edited。 + +$histories = $tester->dao->select('*')->from(TABLE_HISTORY)->where('action')->eq($action->id)->fetchAll(); +r(count($histories)) && p() && e(2); // 创建历史记录成功,最后一条日志的历史记录是 2 条。 +r($histories) && p('0:field,old,new') && e('realname,用户1,user11'); // 创建历史记录成功,第 1 条历史记录的字段是 account,旧值是 user1,新值是 user11。 +r($histories) && p('1:field|old|new', '|') && e('visions|rnd,lite,or|rnd'); // 创建历史记录成功,第 2 条历史记录的字段是 visions,旧值是 rnd,lite,or,新值是 rnd。 + +/** + * 测试更新当前登录用户。 + */ +r($app->user) && p('account,realname,role') && e('admin,admin,qa'); // 当前登录用户的用户名是 admin,真实姓名是 admin,角色是 qa。 + +$result = $userTest->batchUpdateTest($users3, $verify); +r($result) && p('result') && e(1); // 更新当前登录用户成功,返回 true。 +r($app->user) && p('account,realname,role') && e('admin,newAdmin,newRole'); // 当前登录用户的用户名是 admin,真实姓名是 newAdmin,角色是 newRole。 + +/** + * 检测事务提交功能。 + */ +$user = $userTest->getByIdTest('user1'); +r($user) && p('id,realname,visions') && e('2,user11,rnd'); // 事务提交成功,能查询到修改的用户。 diff --git a/module/user/test/user.class.php b/module/user/test/user.class.php index 4ec6a5fdf2..4cd8db33d2 100755 --- a/module/user/test/user.class.php +++ b/module/user/test/user.class.php @@ -291,36 +291,25 @@ class userTest } /** - * Test batch edit users. + * 测试批量更新用户。 + * Test batch update users. * - * @param array $params + * @param array $users + * @param string $verifyPassword * @access public - * @return void + * @return array */ - public function batchEditUserTest($params = array()) + public function batchUpdateTest(array $users, string $verifyPassword): array { - $_POST = $params; - $_POST['verifyPassword'] = 'bac0bbaaf7192f219bebd5387e88c5d7'; + $result = $this->objectModel->batchUpdate($users, $verifyPassword); + $errors = dao::getError(); - $this->objectModel->batchEdit(); - unset($_POST); - - if(dao::isError()) + foreach($errors as $key => $error) { - return dao::getError(); + if(is_array($error)) $errors[$key] = implode('', $error); } - else - { - $users = array(); - $userIDList = array_keys($params['account']); - foreach($userIDList as $userID) - { - $user = $this->objectModel->getByID($userID, 'id'); - $users[$user->id] = $user; - } - return $users; - } + return array('result' => (int)$result, 'errors' => $errors); } /**