* Refactor checkDate4Plan method and case.

This commit is contained in:
tianshujie
2023-09-26 16:41:25 +08:00
parent 417d5ab193
commit 8b8eab602f
3 changed files with 55 additions and 7 deletions
+8 -7
View File
@@ -770,6 +770,7 @@ class productplanModel extends model
}
/**
* 检查计划的日期。
* Check date for plan.
*
* @param object $plan
@@ -778,7 +779,7 @@ class productplanModel extends model
* @access public
* @return void
*/
public function checkDate4Plan($plan, $begin, $end)
public function checkDate4Plan(object $plan, string $begin, string $end): void
{
if($plan->parent == -1)
{
@@ -787,17 +788,17 @@ class productplanModel extends model
$maxEnd = $end;
foreach($childPlans as $childPlan)
{
if($childPlan->begin < $minBegin and $minBegin != $this->config->productplan->future) $minBegin = $childPlan->begin;
if($childPlan->end > $maxEnd and $maxEnd != $this->config->productplan->future) $maxEnd = $childPlan->end;
if($childPlan->begin < $minBegin && $minBegin != $this->config->productplan->future) $minBegin = $childPlan->begin;
if($childPlan->end > $maxEnd && $maxEnd != $this->config->productplan->future) $maxEnd = $childPlan->end;
}
if($minBegin < $begin and $begin != $this->config->productplan->future) dao::$errors['begin'] = sprintf($this->lang->beginGreaterChild, $minBegin);
if($maxEnd > $end and $end != $this->config->productplan->future) dao::$errors['end'] = sprintf($this->lang->endLessThanChild, $maxEnd);
if($minBegin < $begin && $begin != $this->config->productplan->future) dao::$errors['begin'] = sprintf($this->lang->productplan->beginGreaterChild, $minBegin);
if($maxEnd > $end && $end != $this->config->productplan->future) dao::$errors['end'] = sprintf($this->lang->productplan->endLessThanChild, $maxEnd);
}
elseif($plan->parent > 0)
{
$parentPlan = $this->getByID($plan->parent);
if($begin < $parentPlan->begin and $parentPlan->begin != $this->config->productplan->future) dao::$errors['begin'] = sprintf($this->lang->productplan->beginLessThanParent, $parentPlan->begin);
if($end > $parentPlan->end and $parentPlan->end != $this->config->productplan->future) dao::$errors['end'] = sprintf($this->lang->productplan->endGreatThanParent, $parentPlan->end);
if($begin < $parentPlan->begin && $parentPlan->begin != $this->config->productplan->future) dao::$errors['begin'] = sprintf($this->lang->productplan->beginLessThanParent, $parentPlan->begin);
if($end > $parentPlan->end && $parentPlan->end != $this->config->productplan->future) dao::$errors['end'] = sprintf($this->lang->productplan->endGreatThanParent, $parentPlan->end);
}
}
@@ -0,0 +1,28 @@
#!/usr/bin/env php
<?php
/**
title=productpanModel->checkDate4Plan();
timeout=0
cid=1
*/
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/productplan.class.php';
zdTable('productplan')->config('productplan')->gen(5);
zdTable('user')->gen(5);
$planIdList = array(1, 2, 3);
$beginList = array('2030-01-01', '2021-06-02', '2020-01-01');
$endList = array('2030-01-01', '2021-06-14', '2021-07-30');
$planTester = new productplan('admin');
r($planTester->checkDate4PlanTest($planIdList[0], $beginList[0], $endList[0])) && p('0') && e('测试通过'); // 父计划输入正确的开始日期,检查通过
r($planTester->checkDate4PlanTest($planIdList[0], $beginList[0], $endList[0])) && p('0') && e('测试通过'); // 父计划输入正确的结束日期,检查通过
r($planTester->checkDate4PlanTest($planIdList[0], $beginList[1], $endList[0])) && p('begin') && e('子计划的开始日期:2021-06-01,开始日期不能大于子计划的开始日期'); // 父计划输入错误的开始日期,检查报错提示信息
r($planTester->checkDate4PlanTest($planIdList[0], $beginList[0], $endList[1])) && p('end') && e('子计划的完成日期:2021-06-15,完成日期不能小于子计划的完成日期'); // 父计划输入错误的结束日期,检查报错提示信息
r($planTester->checkDate4PlanTest($planIdList[1], $beginList[1], $endList[1])) && p('0') && e('测试通过'); // 子计划输入正确的开始日期,检查通过
r($planTester->checkDate4PlanTest($planIdList[1], $beginList[1], $endList[1])) && p('0') && e('测试通过'); // 子计划输入正确的结束日期,检查通过
r($planTester->checkDate4PlanTest($planIdList[1], $beginList[2], $endList[1])) && p('begin') && e('父计划的开始日期:2021-01-01,开始日期不能小于父计划的开始日期'); // 子计划输入错误的开始日期,检查报错提示信息
r($planTester->checkDate4PlanTest($planIdList[1], $beginList[1], $endList[2])) && p('end') && e('父计划的完成日期:2021-06-30,完成日期不能大于父计划的完成日期'); // 子计划输入错误的结束日期,检查报错提示信息
@@ -497,4 +497,23 @@ class productPlan
}
return $changes;
}
/**
* 检查计划的日期。
* Check date for plan.
*
* @param int $planID
* @param string $begin
* @param string $end
* @access public
* @return array
*/
public function checkDate4PlanTest(int $planID, string $begin, string $end): array
{
$plan = $this->productplan->getByID($planID);
$this->productplan->checkDate4Plan($plan, $begin, $end);
if(dao::isError()) return dao::getError();
return array('测试通过');
}
}