From a670e5b83118166d93053cb4635bfbbb2391eb3a Mon Sep 17 00:00:00 2001 From: wangyidong Date: Fri, 17 Nov 2023 10:30:59 +0800 Subject: [PATCH] * Add test unit for account browse page. --- module/account/control.php | 9 +++--- module/account/model.php | 11 +++---- module/account/test/model/getlist.php | 27 ++++++++++++++++ module/search/model.php | 6 ++-- test/data/account.yaml | 44 +++++++++++++++++++++++++++ 5 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 module/account/test/model/getlist.php create mode 100644 test/data/account.yaml diff --git a/module/account/control.php b/module/account/control.php index 58380d3261..17566c67b7 100644 --- a/module/account/control.php +++ b/module/account/control.php @@ -14,16 +14,16 @@ class account extends control /** * Browse accouts page. * - * @param string $browseType - * @param string $param - * @param string $orderBy + * @param string $browseType + * @param string $param + * @param string $orderBy * @param int $recTotal * @param int $recPerPage * @param int $pageID * @access public * @return void */ - public function browse($browseType = 'all', $param = 0, $orderBy = 'id_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1) + public function browse(string $browseType = 'all', string $param = '', string $orderBy = 'id_desc', int $recTotal = 0, int $recPerPage = 20, int $pageID = 1) { $this->app->loadLang('serverroom'); $browseType = strtolower($browseType); @@ -47,7 +47,6 @@ class account extends control $this->view->param = $param; $this->view->orderBy = $orderBy; $this->view->browseType = $browseType; - $this->view->position[] = $this->lang->account->common; $this->display(); } diff --git a/module/account/model.php b/module/account/model.php index 78ccf07232..0c95d7738d 100644 --- a/module/account/model.php +++ b/module/account/model.php @@ -45,25 +45,22 @@ class accountModel extends model * @access public * @return array */ - public function getList($browseType = 'all', $param = 0, $orderBy = 't1.id_desc', $pager = null) + public function getList(string $browseType = 'all', string $param = '', string $orderBy = 'id_desc', object|null $pager = null) { - $query = ''; + $query = ''; if($browseType == 'bysearch') { /* Concatenate the conditions for the query. */ + if(!$this->session->accountQuery) $this->session->set('accountQuery', ' 1 = 1'); if($param) { - $query = $this->loadModel('search')->getZinQuery($param); + $query = $this->loadModel('search')->getQuery((int)$param); if($query) { $this->session->set('accountQuery', $query->sql); $this->session->set('accountForm', $query->form); } } - else - { - if(!$this->session->accountQuery) $this->session->set('accountQuery', ' 1 = 1'); - } $query = $this->session->accountQuery; } diff --git a/module/account/test/model/getlist.php b/module/account/test/model/getlist.php new file mode 100644 index 0000000000..50e4a1e2ef --- /dev/null +++ b/module/account/test/model/getlist.php @@ -0,0 +1,27 @@ +#!/usr/bin/env php +gen(5); +zdTable('account')->gen(10); +zdTable('userquery')->gen(1); + +/** + +title=accountModel->getList(); +timeout=0 +cid=1 + +*/ + +global $tester; +$accountModel = $tester->loadModel('account'); +r(count($accountModel->getList())) && p() && e('10'); // 获取所有数据。 +r(count($accountModel->getList('bysearch', '0'))) && p() && e('10'); // 根据搜索条件搜索。 +r(count($accountModel->getList('bysearch', '1'))) && p() && e('0'); // 根据第一个保存条件搜索。 + +$accountModel->app->loadClass('pager', $static = true); +$pager = new pager(0, 5, 1); +r(count($accountModel->getList('all', '', 'id_desc', $pager))) && p() && e('5'); // 分页,每页5条,获取第一页。 diff --git a/module/search/model.php b/module/search/model.php index 928d576f46..895ce0affd 100644 --- a/module/search/model.php +++ b/module/search/model.php @@ -378,16 +378,16 @@ class searchModel extends model * @access public * @return object */ - public function getQuery($queryID) + public function getQuery(int $queryID): object { - $query = $this->dao->findByID((int)$queryID)->from(TABLE_USERQUERY)->fetch(); + $query = $this->dao->findByID($queryID)->from(TABLE_USERQUERY)->fetch(); if(!$query) return false; /* Decode html encode. */ $query->form = htmlspecialchars_decode($query->form, ENT_QUOTES); $query->sql = htmlspecialchars_decode($query->sql, ENT_QUOTES); - $hasDynamic = strpos($query->form, '$') !== false; + $hasDynamic = str_contains($query->form, '$'); $query->form = unserialize($query->form); if($hasDynamic) { diff --git a/test/data/account.yaml b/test/data/account.yaml new file mode 100644 index 0000000000..70631690f7 --- /dev/null +++ b/test/data/account.yaml @@ -0,0 +1,44 @@ +title: table zt_account +desc: "运维账号" +author: automated export +version: "1.0" +fields: + - field: id + range: 1-10000 + - field: name + prefix: 运维账号 + range: 1-10000 + - field: type + range: "``" + - field: provider + range: qingyun,tencent,aliyun,azure,local,westcn + - field: adminURI + range: "``" + - field: account + note: "管理员" + prefix: user + range: 1-10000 + postfix: "@cnezsoft.com" + - field: email + prefix: user + range: 1-10000 + postfix: "@cnezsoft.com" + - field: createdBy + note: "用户名" + fields: + - field: account1 + range: admin,user{99},test{100},dev{100},pm{100},po{100},td{100},pd{100},qd{100},top{100},outside{100},others{100},a,bb,ccc,qwuiadsd?!2as@#%$aasd~aj1!@#1 + - field: account2 + range: "[],1-99,1-100,1-100,1-100,1-100,1-100,1-100,1-100,1-100,1-100,1-100,[]{4}" + - field: createdDate + range: "(-1M)-(+1w):-1D" + type: timestamp + format: "YYYY-MM-DD hh:mm:ss" + - field: password + range: "``" + - field: mobile + range: "``" + - field: extra + range: "``" + - field: status + range: "``"