* Refactor testcase::batchCreate.

This commit is contained in:
liumengyi
2023-08-28 09:57:48 +08:00
parent 35f74bb686
commit c0c01df840
3 changed files with 188 additions and 80 deletions
-1
View File
@@ -36,7 +36,6 @@ $config->testcase->form->batchCreate['title'] = array('required' => true,
$config->testcase->form->batchCreate['type'] = array('required' => false, 'type' => 'string', 'default' => '');
$config->testcase->form->batchCreate['color'] = array('required' => false, 'type' => 'string', 'default' => '');
$config->testcase->form->batchCreate['pri'] = array('required' => false, 'type' => 'int', 'default' => 0);
$config->testcase->form->batchCreate['needReview'] = array('required' => false, 'type' => 'int', 'default' => 0);
$config->testcase->form->batchCreate['precondition'] = array('required' => false, 'type' => 'string', 'default' => '');
$config->testcase->form->batchCreate['keywords'] = array('required' => false, 'type' => 'string', 'default' => '');
$config->testcase->form->batchCreate['stage'] = array('required' => false, 'type' => 'array', 'default' => array(''), 'filter' => 'join');
+26 -78
View File
@@ -281,110 +281,58 @@ class testcase extends control
}
/**
* Create a batch test case.
* 批量创建用例。
* Create batch testcase.
*
* @param int $productID
* @param int $moduleID
* @param int $storyID
* @param int $productID
* @param string $branch
* @param int $moduleID
* @param int $storyID
* @access public
* @return void
*/
public function batchCreate($productID, $branch = '', $moduleID = 0, $storyID = 0)
public function batchCreate(int $productID, string $branch = '', int $moduleID = 0, int $storyID = 0)
{
$this->loadModel('story');
if(!empty($_POST))
{
$caseIdList = $this->testcase->batchCreate($productID, $branch, $storyID);
$testcases = $this->testcaseZen->buildCasesForBathcCreate($productID);
$testcases = $this->testcaseZen->checkTestcasesForBatchCreate($testcases, $productID);
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
if(helper::isAjaxRequest('modal')) return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'closeModal' => true, 'load' => true));
foreach($testcases as $testcase)
{
$testcaseID = $this->testcase->create($testcase);
$this->executeHooks($testcaseID);
$this->testcase->syncCase2Project($testcase, $testcaseID);
}
if($this->viewType == 'json') return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'idList' => $caseIdList));
helper::setcookie('caseModule', 0);
$currentModule = $this->app->tab == 'project' ? 'project' : 'testcase';
$currentMethod = $this->app->tab == 'project' ? 'testcase' : 'browse';
$projectParam = $this->app->tab == 'project' ? "projectID={$this->session->project}&" : '';
return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $this->createLink($currentModule, $currentMethod, "{$projectParam}productID={$productID}&branch={$branch}&browseType=all&param=0&caseType=&orderBy=id_desc")));
if(!dao::isError()) $this->loadModel('score')->create('ajax', 'batchCreate');
return $this->testcaseZen->responseAfterBatchCreate($productID, $branch);
}
if(empty($this->products)) $this->locate($this->createLink('product', 'create'));
/* Set productID and currentModuleID. */
/* 设置 session 和 cookie, 并且设置产品 id、分支。 */
/* Set session and cookie, and set product id, branch. */
$productID = $this->product->saveState($productID, $this->products);
if($branch === '') $branch = $this->cookie->preBranch;
if($storyID and empty($moduleID))
{
$story = $this->loadModel('story')->getByID($storyID);
$moduleID = $story->module;
}
$currentModuleID = (int)$moduleID;
/* 设置菜单。 */
/* Set menu. */
$this->app->tab == 'project' ? $this->loadModel('project')->setMenu($this->session->project) : $this->testcase->setMenu($this->products, $productID, $branch);
/* Set story list. */
$story = $storyID ? $this->story->getByID($storyID) : '';
$storyPairs = $this->loadModel('story')->getProductStoryPairs($productID, $branch === 'all' ? 0 : $branch);
$storyPairs += $storyID ? array($storyID => $story->id . ':' . $story->title) : array();
/* Set custom. */
$product = $this->product->getById($productID);
foreach(explode(',', $this->config->testcase->customBatchCreateFields) as $field)
{
if($product->type != 'normal') $customFields[$product->type] = $this->lang->product->branchName[$product->type];
$customFields[$field] = $this->lang->testcase->$field;
}
if($product->type != 'normal')
{
$this->config->testcase->custom->batchCreateFields = sprintf($this->config->testcase->custom->batchCreateFields, $product->type);
}
else
{
$this->config->testcase->custom->batchCreateFields = trim(sprintf($this->config->testcase->custom->batchCreateFields, ''), ',');
}
$showFields = $this->config->testcase->custom->batchCreateFields;
if($product->type == 'normal')
{
$showFields = str_replace(array(0 => ",branch,", 1 => ",platform,"), '', ",$showFields,");
$showFields = trim($showFields, ',');
}
if($this->app->tab == 'project' and $product->type != 'normal')
{
$this->lang->product->branch = sprintf($this->lang->product->branch, $this->lang->product->branchName[$product->type]);
$productBranches = $this->loadModel('execution')->getBranchByProduct($productID, $this->session->project, 'noclosed|withMain');
$branches = isset($productBranches[$productID]) ? $productBranches[$productID] : array();
$branch = key($branches);
}
else
{
$branches = $this->loadModel('branch')->getPairs($productID, 'active');
}
/* Set module option menu. */
$moduleOptionMenu = $this->tree->getOptionMenu($productID, 'case', 0, $branch === 'all' ? 0 : $branch);
$sceneOptionMenu = $this->testcase->getSceneMenu($productID, $moduleID, $viewType = 'case', $startSceneID = 0, ($branch === 'all' or !isset($branches[$branch])) ? 0 : $branch);
$this->view->customFields = $customFields;
$this->view->showFields = $showFields;
/* 指派产品、分支、需求、自定义字段等变量. */
/* Assign the variables about product, branches, story and custom fields. */
$this->testcaseZen->assignForBatchCreate($productID, $branch, $moduleID, $storyID);
/* 展示变量. */
/* Show the variables. */
$this->view->title = $this->products[$productID] . $this->lang->colon . $this->lang->testcase->batchCreate;
$this->view->product = $product;
$this->view->productID = $productID;
$this->view->story = $story;
$this->view->storyPairs = $storyPairs;
$this->view->productName = $this->products[$productID];
$this->view->moduleOptionMenu = $moduleOptionMenu;
$this->view->currentModuleID = $currentModuleID;
$this->view->sceneOptionMenu = $sceneOptionMenu;
$this->view->moduleOptionMenu = $this->tree->getOptionMenu($productID, 'case', 0, $branch === 'all' ? 0 : $branch);
$this->view->currentSceneID = 0;
$this->view->branch = $branch;
$this->view->branches = $branches;
$this->view->needReview = $this->testcase->forceNotReview() == true ? 0 : 1;
$this->display();
}
+162 -1
View File
@@ -754,6 +754,67 @@ class testcaseZen extends testcase
$this->view->actions = $this->loadModel('action')->getList('case', $case->id);
}
/**
* 指定批量创建用例的相关变量。
* Assign for batch creating case.
*
* @param int $productID
* @param string $branch
* @param int $moduleID
* @param int $storyID
* @access protected
* @return void
*/
protected function assignForBatchCreate(int $productID, string $branch = '', int $moduleID = 0, int $storyID = 0)
{
$product = $this->product->getById($productID);
/* 设置分支。 */
/* Set branches. */
if($this->app->tab == 'project' and $product->type != 'normal')
{
$this->lang->product->branch = sprintf($this->lang->product->branch, $this->lang->product->branchName[$product->type]);
$productBranches = $this->loadModel('execution')->getBranchByProduct($productID, $this->session->project, 'noclosed|withMain');
$branches = isset($productBranches[$productID]) ? $productBranches[$productID] : array();
$branch = key($branches);
}
else
{
$branches = $this->loadModel('branch')->getPairs($productID, 'active');
}
/* 设置自定义字段和展示字段。 */
/* Set custom fields and show fields. */
foreach(explode(',', $this->config->testcase->customBatchCreateFields) as $field)
{
if($product->type != 'normal') $customFields[$product->type] = $this->lang->product->branchName[$product->type];
$customFields[$field] = $this->lang->testcase->$field;
}
$showFields = sprintf($this->config->testcase->custom->batchCreateFields, $product->type);
if($product->type == 'normal')
{
$showFields = str_replace(array(",branch,", ",platform,"), '', ",$showFields,");
$showFields = trim($showFields, ',');
}
/* 设置需求键对。 */
/* Set story pairs. */
$story = $storyID ? $this->story->getByID($storyID) : '';
$storyPairs = $this->loadModel('story')->getProductStoryPairs($productID, $branch === 'all' ? 0 : $branch);
$storyPairs += $storyID ? array($storyID => $story->id . ':' . $story->title) : array();
if($storyID && empty($moduleID)) $moduleID = $story->module;
$this->view->product = $product;
$this->view->branches = $branches;
$this->view->customFields = $customFields;
$this->view->showFields = $showFields;
$this->view->story = $story;
$this->view->storyPairs = $storyPairs;
$this->view->sceneOptionMenu = $this->testcase->getSceneMenu($productID, $moduleID, 'case', 0, $branch === 'all' || !isset($branches[$branch]) ? 0 : $branch);
$this->view->currentModuleID = $moduleID;
}
/**
* 构建创建 testcase 页面数据。
* Build form fields for creating testcase.
@@ -776,6 +837,44 @@ class testcaseZen extends testcase
->get();
}
/**
* 构建批量创建用例的数据。
* Build cases for bathc creating.
*
* @param int $productID
* @access protected
* @return array
*/
protected function buildCasesForBathcCreate(int $productID): array
{
$now = helper::now();
$account = $this->app->user->account;
$forceNotReview = $this->testcase->forceNotReview();
$storyVersions = array();
$testcases = form::batchData($this->config->testcase->form->batchCreate)->get();
foreach($testcases as $testcase)
{
$testcase->product = $productID;
if($this->app->tab == 'project') $testcase->project = $this->session->project;
$testcase->openedBy = $account;
$testcase->openedDate = $now;
$testcase->status = $forceNotReview || $testcase->review == 0 ? 'normal' : 'wait';
$testcase->version = 1;
$testcase->storyVersion = isset($storyVersions[$testcase->story]) ? $storyVersions[$testcase->story] : 0;
$testcase->steps = array();
$testcase->expects = array();
$testcase->stepType = array();
if($testcase->story && !isset($storyVersions[$testcase->story]))
{
if(count($storyVersions) == 0) $this->loadModel('story');
$testcase->storyVersion = $this->story->getVersion($testcase->story);
$storyVersions[$testcase->story] = $testcase->storyVersion;
}
unset($testcase->review);
}
return $testcases;
}
/**
* 创建测试用例前检验表单数据是否正确。
* check from data for create case.
@@ -806,6 +905,44 @@ class testcaseZen extends testcase
return true;
}
/**
* 批量创建测试用例前检验数据是否正确。
* Check testcases for batch creating.
*
* @param array $testcases
* @param int $productID
* @access protected
* @return array
*/
protected function checkTestcasesForBatchCreate(array $testcases, int $productID): array
{
$this->loadModel('common');
foreach($testcases as $i => $testcase)
{
/* 检查重复项。 */
/* Check duplicate. */
$result = $this->common->removeDuplicate('testcase', $testcase, "product={$productID}");
if(zget($result, 'stop', false) !== false)
{
unset($testcases[$i]);
continue;
}
/* 检验必填项。 */
/* Check reuqired. */
foreach(explode(',', $this->config->testcase->create->requiredFields) as $field)
{
$field = trim($field);
if($field && empty($testcases[$i]->{$field}))
{
$fieldName = $this->config->testcase->form->batchCreate[$field]['type'] != 'array' ? "{$field}[{$i}]" : "{$field}[{$i}][]";
dao::$errors[$fieldName][] = sprintf($this->lang->error->notempty, $this->lang->testcase->{$field});
}
}
}
return $testcases;
}
/**
* 初始化用例数据。
* Initialize the testcase.
@@ -1035,7 +1172,7 @@ class testcaseZen extends testcase
*
* @param int $caseID
* @access protected
* @return void
* @return array
*/
protected function responseAfterCreate(int $caseID): array
{
@@ -1052,5 +1189,29 @@ class testcaseZen extends testcase
$locateLink = $this->app->tab == 'project' ? $this->createLink('project', 'testcase', "projectID={$this->session->project}") : $this->createLink('testcase', 'browse', "productID={$this->post->product}&branch={$this->post->branch}");
return $this->send(array('result' => 'success', 'message' => $message, 'load' => $useSession ? $this->session->caseList : $locateLink));
}
/**
* 返回批量创建 testcase 的结果。
* Respond after batch creating testcase.
*
* @param int $productID
* @param string|int $branch
* @access protected
* @return void
*/
protected function responseAfterBatchCreate(int $productID, string|int $branch): array
{
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
if(helper::isAjaxRequest('modal')) return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'closeModal' => true, 'load' => true));
if($this->viewType == 'json') return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'idList' => $caseIdList));
helper::setcookie('caseModule', '0');
$currentModule = $this->app->tab == 'project' ? 'project' : 'testcase';
$currentMethod = $this->app->tab == 'project' ? 'testcase' : 'browse';
$projectParam = $this->app->tab == 'project' ? "projectID={$this->session->project}&" : '';
return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $this->createLink($currentModule, $currentMethod, "{$projectParam}productID={$productID}&branch={$branch}&browseType=all&param=0&caseType=&orderBy=id_desc")));
}
}