From 3e8e0dbcce9eee6ddb6220df1253d7e54d5f9378 Mon Sep 17 00:00:00 2001 From: xieqiyu Date: Sat, 16 Oct 2021 15:32:13 +0800 Subject: [PATCH 1/3] * Fix bug#15457. --- module/my/control.php | 4 +-- module/project/model.php | 1 - module/user/control.php | 2 +- module/user/model.php | 72 +++++++++++++++++++--------------------- 4 files changed, 37 insertions(+), 42 deletions(-) diff --git a/module/my/control.php b/module/my/control.php index 859af3765e..36e5c00177 100644 --- a/module/my/control.php +++ b/module/my/control.php @@ -541,7 +541,7 @@ class my extends control /* Get PM id list. */ $accounts = array(); - $projects = $this->user->getExecutions($this->app->user->account, 'project', $status, 'id_desc', $pager); + $projects = $this->user->getObjects($this->app->user->account, 'project', $status, 'id_desc', $pager); foreach($projects as $project) { if(!empty($project->PM) and !in_array($project->PM, $accounts)) $accounts[] = $project->PM; @@ -581,7 +581,7 @@ class my extends control $this->view->title = $this->lang->my->common . $this->lang->colon . $this->lang->my->execution; $this->view->position[] = $this->lang->my->execution; $this->view->tabID = 'project'; - $this->view->executions = $this->user->getExecutions($this->app->user->account, 'execution', $type, $orderBy, $pager); + $this->view->executions = $this->user->getObjects($this->app->user->account, 'execution', $type, $orderBy, $pager); $this->view->type = $type; $this->view->pager = $pager; $this->view->mode = 'execution'; diff --git a/module/project/model.php b/module/project/model.php index 9e683e7098..1d0444e748 100644 --- a/module/project/model.php +++ b/module/project/model.php @@ -501,7 +501,6 @@ class projectModel extends model { return $this->dao->select('*')->from(TABLE_PROJECT) ->where('type')->eq('project') - ->andWhere('deleted')->eq(0) ->andWhere('id')->in($projectIdList) ->beginIF(!$this->app->user->admin)->andWhere('id')->in($this->app->user->view->projects)->fi() ->fetchAll('id'); diff --git a/module/user/control.php b/module/user/control.php index 5300069021..3ee1712a98 100644 --- a/module/user/control.php +++ b/module/user/control.php @@ -362,7 +362,7 @@ class user extends control $this->view->title = $this->lang->user->common . $this->lang->colon . $this->lang->user->execution; $this->view->position[] = $this->lang->user->execution; $this->view->tabID = 'project'; - $this->view->executions = $this->user->getExecutions($account, 'execution', 'all', $orderBy, $pager); + $this->view->executions = $this->user->getObjects($account, 'execution', 'all', $orderBy, $pager); $this->view->user = $user; $this->view->orderBy = $orderBy; $this->view->pager = $pager; diff --git a/module/user/model.php b/module/user/model.php index 21adc31b46..3533390f11 100644 --- a/module/user/model.php +++ b/module/user/model.php @@ -1043,7 +1043,7 @@ class userModel extends model } /** - * Get execution a user participated. + * Get the project or execution in which the user participates.. * * @param string $account * @param string $type project|execution @@ -1053,13 +1053,13 @@ class userModel extends model * @access public * @return array */ - public function getExecutions($account, $type = 'execution', $status = 'all', $orderBy = 'id_desc', $pager = null) + public function getObjects($account, $type = 'execution', $status = 'all', $orderBy = 'id_desc', $pager = null) { - $projectType = $type == 'execution' ? 'sprint,stage' : $type; - $myProjectsList = $this->dao->select('t1.*,t2.*')->from(TABLE_TEAM)->alias('t1') + $objectType = $type == 'execution' ? 'sprint,stage' : $type; + $myObjectsList = $this->dao->select('t1.*,t2.*')->from(TABLE_TEAM)->alias('t1') ->leftJoin(TABLE_PROJECT)->alias('t2')->on('t1.root = t2.id') ->where('t1.type')->eq($type) - ->andWhere('t2.type')->in($projectType) + ->andWhere('t2.type')->in($objectType) ->beginIF(strpos('doing|wait|suspended|closed', $status) !== false)->andWhere('status')->eq($status)->fi() ->beginIF($status == 'done')->andWhere('status')->in('done,closed')->fi() ->beginIF($status == 'undone')->andWhere('status')->notin('done,closed')->fi() @@ -1070,12 +1070,14 @@ class userModel extends model ->andWhere('t2.deleted')->eq(0) ->orderBy("t2.$orderBy") ->page($pager) - ->fetchGroup('project'); + ->fetchAll('root'); + $objectIdList = array(); $projectIdList = array(); - foreach($myProjectsList as $projects) + foreach($myObjectsList as $object) { - foreach($projects as $project) $projectIdList[] = $project->id; + $objectIdList[] = $object->id; + $projectIdList[] = $object->project; } /* Get all tasks and compute totalConsumed, totalLeft, totalWait, progress according to them. */ @@ -1085,22 +1087,22 @@ class userModel extends model $tasks = $this->dao->select('id, project, execution, consumed, `left`, status, assignedTo') ->from(TABLE_TASK) ->where('parent')->lt(1) - ->andWhere($searchField)->in($projectIdList)->fi() + ->andWhere($searchField)->in($objectIdList)->fi() ->andWhere('deleted')->eq(0) ->fetchGroup($searchField, 'id'); /* Compute totalEstimate, totalConsumed, totalLeft. */ - foreach($tasks as $projectID => $projectTasks) + foreach($tasks as $objectID => $objectTasks) { $hour = (object)$emptyHour; - foreach($projectTasks as $task) + foreach($objectTasks as $task) { if($task->status == 'wait') $hour->waitTasks += 1; if($task->status != 'cancel') $hour->totalConsumed += $task->consumed; if($task->status != 'cancel' and $task->status != 'closed') $hour->totalLeft += $task->left; if($task->assignedTo == $account) $hour->assignedToMeTasks += 1; } - $hours[$projectID] = $hour; + $hours[$objectID] = $hour; } /* Compute totalReal and progress. */ @@ -1112,37 +1114,31 @@ class userModel extends model $hour->progress = $hour->totalReal ? round($hour->totalConsumed / $hour->totalReal, 2) * 100 : 0; } - $projectIdList = array_keys($myProjectsList); - $projectList = $this->loadModel('project')->getByIdList($projectIdList); - - $myProjects = array(); - foreach($myProjectsList as $projects) + $myObjects = array(); + $projectList = $this->loadModel('project')->getByIdList($projectIdList); + foreach($myObjectsList as $object) { - foreach($projects as $project) + /* Judge whether the project or execution is delayed. */ + if($object->status != 'done' and $object->status != 'closed' and $object->status != 'suspended') { - /* Judge whether the project is delayed. */ - if($project->status != 'done' and $project->status != 'closed' and $project->status != 'suspended') - { - $delay = helper::diffDate(helper::today(), $project->end); - if($delay > 0) $project->delay = $delay; - } - - /* Process the hours. */ - $project->progress = isset($hours[$project->id]) ? $hours[$project->id]->progress : 0; - $project->waitTasks = isset($hours[$project->id]) ? $hours[$project->id]->waitTasks : 0; - $project->assignedToMeTasks = isset($hours[$project->id]) ? $hours[$project->id]->assignedToMeTasks : 0; - - if($project->project) - { - $parentProject = zget($projectList, $project->project, ''); - if(empty($parentProject)) $parentProject = $this->dao->select('id,name')->from(TABLE_PROJECT)->where('id')->eq($project->project)->fetch(); - $project->projectName = $parentProject ? $parentProject->name : ''; - } - $myProjects[$project->id] = $project; + $delay = helper::diffDate(helper::today(), $object->end); + if($delay > 0) $object->delay = $delay; } + + /* Process the hours. */ + $object->progress = isset($hours[$object->id]) ? $hours[$object->id]->progress : 0; + $object->waitTasks = isset($hours[$object->id]) ? $hours[$object->id]->waitTasks : 0; + $object->assignedToMeTasks = isset($hours[$object->id]) ? $hours[$object->id]->assignedToMeTasks : 0; + + if($object->project) + { + $parentProject = zget($projectList, $object->project, ''); + $object->projectName = $parentProject ? $parentProject->name : ''; + } + $myObjects[$object->id] = $object; } - return $myProjects; + return $myObjects; } /** From 4fe24e06a52c2a41602b0c3a148433280dfbd537 Mon Sep 17 00:00:00 2001 From: zhujinyong Date: Mon, 18 Oct 2021 09:18:19 +0800 Subject: [PATCH 2/3] + Add test for api. --- lib/requests/requests.class.php | 1 + test/api/tokens/post.php | 19 +++++ test/lib/init.php | 28 +++++++- test/lib/rest.php | 119 ++++++++++++++++++++++++++++++++ test/model/user/getbyid.php | 2 +- test/ztest | 2 +- 6 files changed, 167 insertions(+), 4 deletions(-) create mode 100755 test/api/tokens/post.php create mode 100644 test/lib/rest.php diff --git a/lib/requests/requests.class.php b/lib/requests/requests.class.php index 49c60e8ac1..b4336a09ca 100644 --- a/lib/requests/requests.class.php +++ b/lib/requests/requests.class.php @@ -274,6 +274,7 @@ class requests { public static function post($url, $headers = array(), $data = array(), $options = array()) { return self::request($url, $headers, $data, self::POST, $options); } + /** * Send a PUT request */ diff --git a/test/api/tokens/post.php b/test/api/tokens/post.php new file mode 100755 index 0000000000..ddc12df629 --- /dev/null +++ b/test/api/tokens/post.php @@ -0,0 +1,19 @@ +#!/usr/bin/env php +> `[A-Za-z0-9]+` +使用正确用户名和密码获取token >> 登录失败,请检查您的用户名或密码是否填写正确。 + +*/ +$pass = $rest->post('/tokens', array('account' => 'admin', 'password' => '123456')) +$fail = $rest->post('/tokens', array('account' => 'admin', 'password' => '123')) + +r($pass) && c(201) && p('token') && e('`[A-Za-z0-9]+`'); // 使用正确用户名和密码获取token +r($fail) && c(400) && p('error') && e('登录失败,请检查您的用户名或密码是否填写正确。'); // 使用正确用户名和密码获取token diff --git a/test/lib/init.php b/test/lib/init.php index 340a338117..c51d48ff9b 100644 --- a/test/lib/init.php +++ b/test/lib/init.php @@ -17,6 +17,26 @@ error_reporting(E_ALL & E_STRICT); $frameworkRoot = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'framework' . DIRECTORY_SEPARATOR; +/** + * Assert status code and set body as $_result. + * + * @param int $code + * @access public + * @return bool + */ +function c($code) +{ + global $_result; + if($_result and isset($_result->status_code) and $_result->status_code == $code) + { + $_result = $_result->body; + return true; + } + + echo ">> \n\n"; + return false; +} + /* Load the framework. */ include $frameworkRoot . 'router.class.php'; include $frameworkRoot . 'control.class.php'; @@ -26,8 +46,12 @@ include $frameworkRoot . 'helper.class.php'; $app = router::createApp('pms', dirname(dirname(__FILE__)), 'router'); $tester = $app->loadCommon(); -/* Load libraries. */ +/* Load rest for api. */ +if(!isset($config->webSite)) die("Error: \$config->webSite is not set.\n"); + $app->loadClass('requests', true); +include 'rest.php'; +$rest = new Rest($config->webSite . '/api.php/v1'); /* Set configs. */ $config->zendataRoot = dirname(dirname(__FILE__)) . '/zendata'; @@ -83,7 +107,7 @@ function p($key, $delimiter = ',') $result = trim($result, $delimiter); } - echo $result . "\n\n"; + echo $result . "\n"; return true; } diff --git a/test/lib/rest.php b/test/lib/rest.php new file mode 100644 index 0000000000..9ad46d94f1 --- /dev/null +++ b/test/lib/rest.php @@ -0,0 +1,119 @@ +base = $base; + } + + /** + * Get method. + * + * @param string $url + * @param array $headers + * @access public + * @return object + */ + public function get($url, $headers = array()) + { + $headers['Accept'] = 'application/json'; + $resp = requests::get($this->base . $url, $headers, array()); + try + { + $resp->body = json_decode($resp->body); + } + catch(Exception $e) + { + } + + return $resp; + } + + /** + * Post method. + * + * @param string $url + * @param array $data + * @param array $headers + * @access public + * @return object + */ + public function post($url, $data = array(), $headers = array()) + { + $headers['Accept'] = 'application/json'; + $headers['Content-Type'] = 'application/json'; + $data = json_encode($data); + + $resp = requests::post($this->base . $url, $headers, $data, array()); + try + { + $resp->body = json_decode($resp->body); + } + catch(Exception $e) + { + } + + return $resp; + } + + /** + * Put method. + * + * @param string $url + * @param array $data + * @param array $headers + * @access public + * @return object + */ + public function put($url, $data = array(), $headers = array()) + { + $headers['Accept'] = 'application/json'; + $headers['Content-Type'] = 'application/json'; + + $resp = requests::put($this->base . $url, $headers, $data, $options); + try + { + $resp->body = json_decode($resp->body); + } + catch(Exception $e) + { + } + + return $resp; + } + + /** + * Delete method. + * + * @param string $url + * @param array $headers + * @param array $options + * @access public + * @return object + */ + public function delete($url, $headers = array()) + { + $headers['Accept'] = 'application/json'; + return requests::delete($this->base . $url, $headers, array()); + } +} diff --git a/test/model/user/getbyid.php b/test/model/user/getbyid.php index 55eb2cf08d..5cfa37d902 100755 --- a/test/model/user/getbyid.php +++ b/test/model/user/getbyid.php @@ -23,4 +23,4 @@ r($user->getByID('account1')) && p('account') && e('account1'); // r($user->getByID(1)) && p('account') && e(''); // 通过默认字段获取不存在的用户 r($user->getByID(100000, 'id')) && p('account') && e(''); // 通过id字段获取不存在的用户 r($user->getByID('error', 'account')) && p('account') && e(''); // 通过默认字段获取不存在的用户 - */ + */ \ No newline at end of file diff --git a/test/ztest b/test/ztest index a2457c3dbe..b5c47a4a38 100755 --- a/test/ztest +++ b/test/ztest @@ -10,10 +10,10 @@ switch($argv[1]) zdRun(); break; case 'extract': + ztfExtract('api'); ztfExtract('model'); break; case 'api': - ztfExtract('api'); ztfRun('api'); break; case 'control': From abc5c4a302bc1a5feb815ccc337a637beade1bf9 Mon Sep 17 00:00:00 2001 From: zhujinyong Date: Mon, 18 Oct 2021 09:20:56 +0800 Subject: [PATCH 3/3] * Fix code error. --- test/api/tokens/post.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/api/tokens/post.php b/test/api/tokens/post.php index ddc12df629..c30031be65 100755 --- a/test/api/tokens/post.php +++ b/test/api/tokens/post.php @@ -12,8 +12,8 @@ pid=1 使用正确用户名和密码获取token >> 登录失败,请检查您的用户名或密码是否填写正确。 */ -$pass = $rest->post('/tokens', array('account' => 'admin', 'password' => '123456')) -$fail = $rest->post('/tokens', array('account' => 'admin', 'password' => '123')) +$pass = $rest->post('/tokens', array('account' => 'admin', 'password' => '123qwe!@#')); +$fail = $rest->post('/tokens', array('account' => 'admin', 'password' => '123')); r($pass) && c(201) && p('token') && e('`[A-Za-z0-9]+`'); // 使用正确用户名和密码获取token r($fail) && c(400) && p('error') && e('登录失败,请检查您的用户名或密码是否填写正确。'); // 使用正确用户名和密码获取token