* Refactor the getBySearch function.

This commit is contained in:
daitingting
2023-09-13 06:44:58 +00:00
parent 2c2a86c842
commit c7efd4467f
3 changed files with 101 additions and 34 deletions
+37 -34
View File
@@ -261,7 +261,7 @@ class testcaseModel extends model
if($browseType == 'needconfirm') return $this->testcaseTao->getNeedConfirmList($productID, $branch, $modules, $auto, $caseType, $orderBy, $pager);
if($browseType == 'bysuite') return $this->testcaseTao->getBySuite($productID, $branch, $queryID, $modules, $auto, $orderBy, $pager);
if($browseType == 'bysearch') return $this->getBySearch($productID, $queryID, $orderBy, $pager, $branch, $auto);
if($browseType == 'bysearch') return $this->getBySearch($productID, $branch, $queryID, $auto, $orderBy, $pager);
return array();
}
@@ -271,15 +271,15 @@ class testcaseModel extends model
* Get cases by search.
*
* @param int $productID
* @param int|string $branch
* @param int $queryID
* @param string $auto no|unit
* @param string $orderBy
* @param object $pager
* @param int|string $branch
* @param string $auto no|unit
* @access public
* @return array
*/
public function getBySearch($productID, $queryID, $orderBy, $pager = null, $branch = 0, $auto = 'no')
public function getBySearch(int $productID, int|string $branch = 0, int $queryID, string $auto = 'no', string $orderBy = 'id_desc', object $pager = null): array
{
if($queryID)
{
@@ -289,49 +289,52 @@ class testcaseModel extends model
$this->session->set('testcaseQuery', $query->sql);
$this->session->set('testcaseForm', $query->form);
}
else
{
$this->session->set('testcaseQuery', ' 1 = 1');
}
}
else
{
if($this->session->testcaseQuery == false) $this->session->set('testcaseQuery', ' 1 = 1');
}
if($this->session->testcaseQuery == false) $this->session->set('testcaseQuery', ' 1 = 1');
$caseQuery = '(' . $this->session->testcaseQuery;
/* 处理用例查询中的产品条件。*/
/* Process product condition in case query. */
$queryProductID = $productID;
$allProduct = "`product` = 'all'";
$caseQuery = '(' . $this->session->testcaseQuery;
if(strpos($this->session->testcaseQuery, $allProduct) !== false)
if(strpos($this->session->testcaseQuery, "`product` = 'all'") !== false)
{
$products = $this->app->user->view->products;
$caseQuery = str_replace($allProduct, '1', $caseQuery);
$caseQuery = $caseQuery . ' AND `product` ' . helper::dbIN($products);
$caseQuery = str_replace("`product` = 'all'", '1', $caseQuery);
$caseQuery .= ' AND `product` ' . helper::dbIN($this->app->user->view->products);
$queryProductID = 'all';
}
$allBranch = "`branch` = 'all'";
if($branch !== 'all' and strpos($caseQuery, '`branch` =') === false) $caseQuery .= " AND `branch` in('$branch')";
if(strpos($caseQuery, $allBranch) !== false) $caseQuery = str_replace($allBranch, '1', $caseQuery);
$caseQuery .= ')';
$caseQuery = str_replace('`version`', 't1.`version`', $caseQuery);
/* 处理用例查询中的产品分支条件。*/
/* Process branch condition in case query. */
if($branch !== 'all' && strpos($caseQuery, '`branch` =') === false) $caseQuery .= " AND `branch` in ('$branch')";
if(strpos($caseQuery, "`branch` = 'all'") !== false) $caseQuery = str_replace("`branch` = 'all'", '1', $caseQuery);
/* 处理用例查询中的版本条件。*/
/* Process version condition in case query. */
$caseQuery = str_replace('`version`', 't1.`version`', $caseQuery);
/* 处理用例查询中的产品条件。*/
/* Process product condition in case query. */
if($this->app->tab == 'project') $caseQuery = str_replace('`product`', 't2.`product`', $caseQuery);
$caseQuery .= ')';
/* Search criteria under compatible project. */
$sql = $this->dao->select('*')->from(TABLE_CASE)->alias('t1');
if($this->app->tab == 'project') $sql->leftJoin(TABLE_PROJECTCASE)->alias('t2')->on('t1.id=t2.case');
$cases = $sql
->where($caseQuery)
->beginIF($this->app->tab == 'project' and $this->config->systemMode == 'new')->andWhere('t2.project')->eq($this->session->project)->fi()
->beginIF($this->app->tab == 'project' and !empty($productID) and $queryProductID != 'all')->andWhere('t2.product')->eq($productID)->fi()
->beginIF($this->app->tab != 'project' and !empty($productID) and $queryProductID != 'all')->andWhere('t1.product')->eq($productID)->fi()
if($this->app->tab == 'project') $sql->leftJoin(TABLE_PROJECTCASE)->alias('t2')->on('t1.id = t2.case');
return $sql->where($caseQuery)
->beginIF($this->app->tab == 'project' && $this->config->systemMode == 'ALM')->andWhere('t2.project')->eq($this->session->project)->fi()
->beginIF($this->app->tab == 'project' && !empty($productID) && $queryProductID != 'all')->andWhere('t2.product')->eq($productID)->fi()
->beginIF($this->app->tab != 'project' && !empty($productID) && $queryProductID != 'all')->andWhere('t1.product')->eq($productID)->fi()
->beginIF($auto == 'auto' || $auto == 'unit')->andWhere('t1.auto')->eq($auto)->fi()
->beginIF($auto != 'auto' && $auto != 'unit')->andWhere('t1.auto')->ne('unit')->fi()
->andWhere('t1.deleted')->eq(0)
->orderBy($orderBy)->page($pager)->fetchAll('id');
return $cases;
->andWhere('t1.deleted')->eq('0')
->orderBy($orderBy)
->page($pager)
->fetchAll('id');
}
/**
@@ -703,7 +706,7 @@ class testcaseModel extends model
if($browseType != 'bySearch') return array();
$case = $this->getByID($caseID);
$cases2Link = $this->getBySearch($case->product, $queryID, 'id', null, $case->branch);
$cases2Link = $this->getBySearch($case->product, $case->branch, $queryID, $auto = 'no', $orderBy = 'id');
foreach($cases2Link as $key => $case2Link)
{
if($case2Link->id == $caseID) unset($cases2Link[$key]);
+36
View File
@@ -0,0 +1,36 @@
#!/usr/bin/env php
<?php
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/testcase.class.php';
su('admin');
$caseData = zdTable('case');
$caseData->gen(10);
/**
title=测试 testcaseModel->getByOpenedBy();
timeout=0
cid=1
- 测试搜索产品1下的用例
- 第1条的title属性 @这个是测试用例1
- 第2条的title属性 @这个是测试用例2
- 第3条的title属性 @这个是测试用例3
- 第4条的title属性 @这个是测试用例4
- 测试搜索产品1 项目1 下的用例
- 第1条的title属性 @这个是测试用例1
- 第3条的title属性 @这个是测试用例3
- 第5条的title属性 @这个是测试用例5
- 第7条的title属性 @这个是测试用例7
- 第9条的title属性 @这个是测试用例9
*/
$tabList = array('qa', 'project');
$projectIdList = array('0', '1', '2');
$productIdList = array('0', '1', '2');
$testcase = new testcaseTest();
r($testcase->getBySearchTest($tabList[0], $projectIdList[0], $productIdList[1])) && p('1:title;2:title;3:title;4:title') && e('这个是测试用例1;这个是测试用例2;这个是测试用例3;这个是测试用例4'); // 测试搜索产品1下的用例
r($testcase->getBySearchTest($tabList[1], $projectIdList[1], $productIdList[1])) && p('1:title;3:title;5:title;7:title;9:title') && e('这个是测试用例1;这个是测试用例3;这个是测试用例5;这个是测试用例7;这个是测试用例9'); // 测试搜索产品1 项目1 下的用例
+28
View File
@@ -289,6 +289,34 @@ class testcaseTest
return count($objects);
}
/**
* 测试搜索用例。
* Test get cases by search.
*
* @param int $productID
* @param int|string $branch
* @param int $queryID
* @param string $auto
* @param string $orderBy
* @param object $pager
* @access public
* @return array
*/
public function getBySearchTest(string $tab = 'qa', int $projectID, int $productID, int|string $branch = 0, int $queryID = 0, string $auto = 'no', string $orderBy = 'id_desc', object $pager = null): array
{
global $tester;
$tester->app->tab = $tab;
$tester->config->systemMode = 'ALM';
$_SESSION['project'] = $projectID;
$objects = $this->objectModel->getBySearch($productID, $branch, $queryID, $auto, $orderBy, $pager);
if(dao::isError()) return false;
return $objects;
}
/**
* 测试获取需求关联的用例。
* Test get stories' cases.