diff --git a/module/mr/test/lib/zen.class.php b/module/mr/test/lib/zen.class.php index 622a9db781..0c89ed7007 100644 --- a/module/mr/test/lib/zen.class.php +++ b/module/mr/test/lib/zen.class.php @@ -241,4 +241,19 @@ class mrZenTest extends baseTest return $result; } + + /** + * Test saveMrData method. + * + * @param object $repo + * @param array $rawMrList + * @access public + * @return bool|array + */ + public function saveMrDataTest(object $repo, array $rawMrList): bool|array + { + $result = $this->invokeArgs('saveMrData', [$repo, $rawMrList]); + if(dao::isError()) return dao::getError(); + return $result; + } } diff --git a/module/mr/test/zen/savemrdata.php b/module/mr/test/zen/savemrdata.php new file mode 100755 index 0000000000..d244be3142 --- /dev/null +++ b/module/mr/test/zen/savemrdata.php @@ -0,0 +1,110 @@ +#!/usr/bin/env php + 0 @1 + +*/ + +include dirname(__FILE__, 5) . '/test/lib/init.php'; +include dirname(__FILE__, 2) . '/lib/zen.class.php'; + +global $app, $tester; +$app->setMethodName('view'); + +zenData('repo')->loadYaml('savemrdata/repo', false, 2)->gen(10); +zenData('mr')->loadYaml('savemrdata/mr', false, 2)->gen(0); +zenData('action')->loadYaml('savemrdata/action', false, 2)->gen(0); + +su('admin'); + +$mrTest = new mrZenTest(); + +// 测试步骤1: 保存单个普通MR数据(状态为open,需转换为opened) +$repo = new stdclass(); +$repo->id = 1; +$repo->serviceHost = 1; +$repo->SCM = 'Gitlab'; + +$rawMR = new stdclass(); +$rawMR->iid = 100; +$rawMR->source_project_id = '10'; +$rawMR->source_branch = 'feature-test'; +$rawMR->target_project_id = '1'; +$rawMR->target_branch = 'main'; +$rawMR->title = 'Test MR 1'; +$rawMR->created_at = '2025-11-10T10:00:00Z'; +$rawMR->updated_at = '2025-11-10T12:00:00Z'; +$rawMR->merge_status = 'can_be_merged'; +$rawMR->state = 'open'; +r($mrTest->saveMrDataTest($repo, array($rawMR))) && p() && e('1'); + +// 测试步骤2: 保存多个MR数据(包含3个MR) +$rawMRList = array(); +for($i = 1; $i <= 3; $i++) +{ + $rawMR = new stdclass(); + $rawMR->iid = 200 + $i; + $rawMR->source_project_id = (string)(10 + $i); + $rawMR->source_branch = 'feature-' . $i; + $rawMR->target_project_id = '1'; + $rawMR->target_branch = 'main'; + $rawMR->title = 'Test MR ' . $i; + $rawMR->created_at = '2025-11-10T10:00:00Z'; + $rawMR->updated_at = '2025-11-10T12:00:00Z'; + $rawMR->merge_status = 'can_be_merged'; + $rawMR->state = 'opened'; + $rawMRList[] = $rawMR; +} +r($mrTest->saveMrDataTest($repo, $rawMRList)) && p() && e('1'); + +// 测试步骤3: 保存pullreq类型数据(flow字段存在) +$rawMR = new stdclass(); +$rawMR->iid = 300; +$rawMR->source_project_id = '20'; +$rawMR->source_branch = 'feature-flow'; +$rawMR->target_project_id = '1'; +$rawMR->target_branch = 'main'; +$rawMR->title = 'Test Flow MR'; +$rawMR->created_at = '2025-11-10T10:00:00Z'; +$rawMR->updated_at = '2025-11-10T12:00:00Z'; +$rawMR->merge_status = 'can_be_merged'; +$rawMR->state = 'opened'; +$rawMR->flow = 1; +r($mrTest->saveMrDataTest($repo, array($rawMR))) && p() && e('1'); + +// 测试步骤4: 保存使用毫秒时间戳的MR数据 +$rawMR = new stdclass(); +$rawMR->iid = 400; +$rawMR->source_project_id = '30'; +$rawMR->source_branch = 'feature-timestamp'; +$rawMR->target_project_id = '1'; +$rawMR->target_branch = 'main'; +$rawMR->title = 'Test Timestamp MR'; +$rawMR->created = 1699603200000; +$rawMR->updated = 1699610400000; +$rawMR->merge_status = 'can_be_merged'; +$rawMR->state = 'opened'; +r($mrTest->saveMrDataTest($repo, array($rawMR))) && p() && e('1'); + +// 测试步骤5: 保存空的MR列表 +r($mrTest->saveMrDataTest($repo, array())) && p() && e('1'); + +// 测试步骤6: 验证保存的MR状态已转换(open -> opened) +$savedMR = $tester->dao->select('*')->from(TABLE_MR)->where('mriid')->eq(100)->fetch(); +r($savedMR) && p('status') && e('opened'); + +// 测试步骤7: 验证action记录已创建 +$actionCount = $tester->dao->select('count(*) as count')->from(TABLE_ACTION)->where('objectType')->in('mr,pullreq')->fetch('count'); +r($actionCount > 0) && p() && e('1'); \ No newline at end of file diff --git a/module/mr/test/zen/yaml/savemrdata/action.yaml b/module/mr/test/zen/yaml/savemrdata/action.yaml new file mode 100644 index 0000000000..2a979808ea --- /dev/null +++ b/module/mr/test/zen/yaml/savemrdata/action.yaml @@ -0,0 +1,76 @@ +--- +title: 测试saveMrData方法使用的action数据 +author: Claude +version: 1.0 + +fields: +- field: id + note: ID + range: 1-1000 + prefix: "" + postfix: "" +- field: objectType + note: 对象类型 + range: 'mr,pullreq,task,bug' + prefix: "" + postfix: "" +- field: objectID + note: 对象ID + range: 1-100 + prefix: "" + postfix: "" +- field: product + note: 产品 + range: '1,2,3' + prefix: "" + postfix: "" +- field: project + note: 项目 + range: 1-10 + prefix: "" + postfix: "" +- field: execution + note: 执行 + range: 0,1,2 + prefix: "" + postfix: "" +- field: actor + note: 操作者 + range: 'system,admin,user1' + prefix: "" + postfix: "" +- field: action + note: 动作 + range: 'opened,closed,merged,commented' + prefix: "" + postfix: "" +- field: date + note: 日期 + range: '2025-11-01 00:00:00,2025-11-10 10:00:00' + prefix: "" + postfix: "" +- field: comment + note: 评论 + range: '[]{50},Comment text{50}' + prefix: "" + postfix: "" +- field: extra + note: 额外信息 + range: '[]{80},{}' + prefix: "" + postfix: "" +- field: read + note: 是否已读 + range: '0,1' + prefix: "" + postfix: "" +- field: vision + note: 视图 + range: rnd + prefix: "" + postfix: "" +- field: efforted + note: 是否有工时 + range: 0,1 + prefix: "" + postfix: "" diff --git a/module/mr/test/zen/yaml/savemrdata/mr.yaml b/module/mr/test/zen/yaml/savemrdata/mr.yaml new file mode 100644 index 0000000000..ad3be0e60b --- /dev/null +++ b/module/mr/test/zen/yaml/savemrdata/mr.yaml @@ -0,0 +1,86 @@ +--- +title: 测试saveMrData方法使用的mr数据 +author: Claude +version: 1.0 + +fields: +- field: id + note: ID + range: 1-100 + prefix: "" + postfix: "" +- field: hostID + note: 服务器ID + range: 1-3 + prefix: "" + postfix: "" +- field: sourceProject + note: 源项目ID + range: '1,2,3,10,20' + prefix: "" + postfix: "" +- field: sourceBranch + note: 源分支 + range: 'feature,develop,hotfix,release' + prefix: "" + postfix: "" +- field: targetProject + note: 目标项目ID + range: '1,2,3' + prefix: "" + postfix: "" +- field: targetBranch + note: 目标分支 + range: 'main,master,develop' + prefix: "" + postfix: "" +- field: mriid + note: 合并请求IID + range: 1-1000 + prefix: "" + postfix: "" +- field: title + note: 合并请求标题 + range: 'Fix bug,Add feature,Update docs,Refactor code' + prefix: "" + postfix: "" +- field: repoID + note: 代码库ID + range: 1-10 + prefix: "" + postfix: "" +- field: createdBy + note: 创建者 + range: 'system,admin,user1' + prefix: "" + postfix: "" +- field: createdDate + note: 创建时间 + range: '2025-11-01 00:00:00,2025-11-10 10:00:00,2025-11-10 12:00:00' + prefix: "" + postfix: "" +- field: editedDate + note: 编辑时间 + range: '2025-11-01 00:00:00,2025-11-10 10:00:00,2025-11-10 12:00:00' + prefix: "" + postfix: "" +- field: mergeStatus + note: 合并状态 + range: 'can_be_merged,cannot_be_merged,unchecked' + prefix: "" + postfix: "" +- field: status + note: 状态 + range: 'opened,merged,closed' + prefix: "" + postfix: "" +- field: isFlow + note: 是否为Flow + range: 0,1 + prefix: "" + postfix: "" +- field: deleted + note: 是否删除 + range: '0' + prefix: "" + postfix: "" diff --git a/module/mr/test/zen/yaml/savemrdata/repo.yaml b/module/mr/test/zen/yaml/savemrdata/repo.yaml new file mode 100644 index 0000000000..9557ae29c3 --- /dev/null +++ b/module/mr/test/zen/yaml/savemrdata/repo.yaml @@ -0,0 +1,56 @@ +--- +title: 测试saveMrData方法使用的repo数据 +author: Claude +version: 1.0 + +fields: +- field: id + note: ID + range: 1-10 + prefix: "" + postfix: "" +- field: product + note: 所属产品 + range: 1 + prefix: "" + postfix: "" +- field: projects + note: 所属项目 + range: '1' + prefix: "" + postfix: "" +- field: name + note: 代码库名称 + range: 'Test Repo,GitLab Repo,Gitea Repo' + prefix: "" + postfix: "" +- field: path + note: 代码库路径 + range: '/tmp/repo{}' + prefix: "" + postfix: "" +- field: encoding + note: 编码 + range: utf-8 + prefix: "" + postfix: "" +- field: SCM + note: SCM类型 + range: Gitlab,Gitea,Gogs + prefix: "" + postfix: "" +- field: serviceHost + note: 服务器ID + range: 1-3 + prefix: "" + postfix: "" +- field: serviceProject + note: 服务项目ID + range: '1,2,3' + prefix: "" + postfix: "" +- field: deleted + note: 是否删除 + range: '0' + prefix: "" + postfix: ""