* Refactor doCreateStory method and add unit test.

This commit is contained in:
wangyidong
2023-06-02 08:31:29 +08:00
parent a7062f2bd7
commit aca053e650
4 changed files with 96 additions and 16 deletions
+9 -1
View File
@@ -639,9 +639,17 @@ class storyTao extends storyModel
->fetchAll();
}
/**
* 将需求数据插入到需求表。
* Do create story data.
*
* @param object $story
* @access protected
* @return int|false
*/
protected function doCreateStory(object $story): int|false
{
$this->dao->insert(TABLE_STORY)->data($story, 'spec,verify,reviewer,URS,region,lane')
$this->dao->insert(TABLE_STORY)->data($story, 'spec,verify,reviewer,URS,region,lane,branches,plans,modules')
->autoCheck()
->checkIF($story->notifyEmail, 'notifyEmail', 'email')
->checkFlow()
+17
View File
@@ -738,4 +738,21 @@ class storyTest
if(dao::isError()) return dao::getError();
return $this->objectModel->dao->select('*')->from(TABLE_STORYSPEC)->fetchAll();
}
/**
* 测试 doCreateStory 方法。
* Test doCreateStory method.
*
* @param object $story
* @access public
* @return object|array
*/
public function doCreateStoryTest(object $story): object|array
{
$this->objectModel->dao->delete()->from(TABLE_STORY)->exec();
$storyID = $this->objectModel->doCreateStory($story);
if(dao::isError()) return dao::getError();
return $this->objectModel->dao->select('*')->from(TABLE_STORY)->where('id')->eq($storyID)->fetch();
}
}
+49
View File
@@ -0,0 +1,49 @@
#!/usr/bin/env php
<?php
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/story.class.php';
su('admin');
zdTable('story')->gen(0);
/**
title=测试 storyModel->doCreateStory();
cid=1
pid=1
*/
$data = new stdclass();
$data->product = 1;
$data->module = 0;
$data->modules = array(0);
$data->plans = array(0);
$data->plan = 0;
$data->assignedTo = '';
$data->source = '';
$data->sourceNote = '';
$data->feedbackBy = '';
$data->notifyEmail = '';
$data->parent = 0;
$data->title = 'test story';
$data->color = '';
$data->category = 'feature';
$data->pri = 3;
$data->estimate = 1;
$data->spec = 'test spec';
$data->verify = 'test verify';
$data->keywords = '';
$data->type = 'story';
$data->status = 'active';
$data->version = 1;
$data->openedBy = 'admin';
$data->openedDate = date('Y-m-d H:i:s');
$storyTest = new storyTest();
$story = $storyTest->doCreateStoryTest($data);
r($story) && p('product,title,status') && e('1,test story,active'); //检查保存后的数据。
$data->notifyEmail = 'test';
$error = $storyTest->doCreateStoryTest($data);
r($error['notifyEmail'][0]) && p() && e('『通知邮箱』应当为合法的EMAIL。'); //检查报错信息。
+21 -15
View File
@@ -258,12 +258,13 @@ class storyZen extends story
}
$branch = current(explode(',', $branch));
$product = $this->product->getByID($productID);
$users = $this->user->getPairs('pdfirst|noclosed|nodeleted');
$stories = $this->story->getParentStoryPairs($productID);
$plans = $this->loadModel('productplan')->getPairsForStory($productID, $branch == 0 ? '' : $branch, 'skipParent|unexpired|noclosed');
$plans = array_map(function($planName){return str_replace(FUTURE_TIME, $this->lang->story->undetermined, $planName);}, $plans);
$needReview = ($account == $product->PO || $objectID > 0 || $this->config->story->needReview == 0 || !$this->story->checkForceReview());
$product = $this->product->getByID($productID);
$users = $this->user->getPairs('pdfirst|noclosed|nodeleted');
$stories = $this->story->getParentStoryPairs($productID);
$plans = $this->loadModel('productplan')->getPairsForStory($productID, $branch == 0 ? '' : $branch, 'skipParent|unexpired|noclosed');
$plans = array_map(function($planName){return str_replace(FUTURE_TIME, $this->lang->story->undetermined, $planName);}, $plans);
$forceReview = $this->story->checkForceReview();
$needReview = ($account == $product->PO || $objectID > 0 || $this->config->story->needReview == 0 || !$forceReview);
$reviewers = $product->reviewer;
if(!$reviewers and $product->acl != 'open') $reviewers = $this->loadModel('user')->getProductViewListUsers($product, '', '', '', '');
@@ -295,17 +296,20 @@ class storyZen extends story
if(empty($fields['branch']['default'])) $fields['branch']['default'] = $branch;
if(empty($fields['branches']['default'])) $fields['branches']['default'] = $branch;
if(empty($fields['plans']['default'])) $fields['plans']['default'] = zget($initStory, 'plan', 0);
if(empty($needReview)) $fields['reviewer']['default'] = $product->PO;
if(empty($needReview)) $fields['reviewer']['default'] = $product->PO;
if($forceReview) $fields['reviewer']['required'] = true;
/* 删除不需要的字段。 */
if(empty($branches)) unset($fields['branch'], $fields['branches'], $fields['modules'], $fields['plans']);
$this->view->productID = $productID;
$this->view->product = $product;
$this->view->branch = $branch;
$this->view->branches = $branches;
$this->view->objectID = $objectID;
$this->view->needReview = $needReview;
$this->view->productID = $productID;
$this->view->product = $product;
$this->view->branch = $branch;
$this->view->branches = $branches;
$this->view->objectID = $objectID;
$this->view->forceReview = $forceReview;
$this->view->needReview = $needReview;
return $fields;
}
@@ -393,11 +397,13 @@ class storyZen extends story
/* Set Custom. */
$customFields = explode(',', $this->config->story->list->customCreateFields);
$showFields = explode(',', trim($this->config->story->custom->createFields, ','));
$showFields = trim($this->config->story->custom->createFields, ',');
foreach($customFields as $field)
{
if(!str_contains($showFields, $field)) $fields[$field]['control'] = '';
if(!str_contains($showFields, $field)) unset($fields[$field]['control']);
}
return $fields;
}
/**