* [misc] Fix unit tests for releaseModel::sendMail2Feedback() method

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
liugang
2025-09-29 16:42:02 +08:00
co-authored by Claude
parent 923561b307
commit f3cedc382a
2 changed files with 58 additions and 106 deletions
@@ -3,10 +3,8 @@ class releaseTest
{
public function __construct()
{
global $tester, $app;
global $tester;
$this->objectModel = $tester->loadModel('release');
$app->rawModule = 'release';
}
/**
@@ -472,61 +470,39 @@ class releaseTest
{
if(empty($releaseID))
{
// 对于空releaseID,sendmail方法会直接返回,我们也应该返回相应状态
$this->objectModel->sendmail(0);
if(dao::isError()) return dao::getError();
return 'empty';
}
// 创建模拟发布数据而不依赖数据库
if($releaseID == 99)
$release = $this->objectModel->getByID($releaseID);
if(!$release)
{
// 模拟不存在的发布
return 'no_release';
}
// 为存在的发布ID创建模拟数据
$mockRelease = new stdClass();
$mockRelease->id = $releaseID;
$mockRelease->product = $releaseID;
$mockRelease->name = "发布{$releaseID}";
$mockRelease->build = "1";
$mockRelease->stories = ($releaseID <= 2) ? "1,2" : "";
$mockRelease->bugs = ($releaseID <= 2) ? "1,2" : "";
$mockRelease->leftBugs = ($releaseID == 2) ? "1" : "";
$mockRelease->mailto = "admin,user1";
$mockRelease->notify = "PO";
$mockRelease->desc = "测试描述";
$mockRelease->createdBy = "admin";
// Mock mail config to prevent actual email sending
global $app;
$originalTurnon = isset($app->config->mail->turnon) ? $app->config->mail->turnon : true;
if(!isset($app->config->mail)) $app->config->mail = new stdClass();
$app->config->mail->turnon = false;
try
{
// Mock getByID方法返回模拟数据
global $tester;
$originalMethod = $this->objectModel;
$this->objectModel->sendmail($releaseID);
// Mock邮件发送功能避免真实发送邮件
global $app;
$originalConfig = isset($app->config->mail->turnon) ? $app->config->mail->turnon : false;
// 创建临时的mail配置避免真实发送
if(!isset($app->config->mail)) $app->config->mail = new stdClass();
$app->config->mail->turnon = false;
// 通过反射或直接调用,但首先需要确保有基础数据
// 简化版本:只检查sendmail方法能否被正常调用
$this->objectModel->sendmail(0); // 先测试空ID
$result = 'success';
// 恢复原始配置
$app->config->mail->turnon = $originalConfig;
// Restore original config
$app->config->mail->turnon = $originalTurnon;
if(dao::isError()) return dao::getError();
return $result;
return 'success';
}
catch(Exception $e)
{
// 如果发生异常,返回错误信息
return 'error: ' . $e->getMessage();
// Restore original config
$app->config->mail->turnon = $originalTurnon;
return 'error';
}
}
@@ -563,82 +539,54 @@ class releaseTest
if(!$release->stories && !$release->bugs) return 'no_data';
// 模拟创建测试数据
// 检查是否有需要通知的邮箱
$stories = $release->stories ? explode(',', trim($release->stories, ',')) : array();
$bugs = $release->bugs ? explode(',', trim($release->bugs, ',')) : array();
$hasNotifyEmail = false;
// 创建模拟的story数据
// 检查story数据中是否有notifyEmail
if($stories) {
foreach($stories as $storyId) {
$storyId = (int)$storyId;
if($storyId <= 5) {
// ID 1-5 有notifyEmail
$notifyEmail = "user{$storyId}@test.com";
$hasNotifyEmail = true;
} else {
// ID 6以上没有notifyEmail
$notifyEmail = '';
}
// 模拟插入story数据
$this->objectModel->dao->replace(TABLE_STORY)
->data(array(
'id' => $storyId,
'title' => "需求{$storyId}",
'notifyEmail' => $notifyEmail,
'product' => 1,
'module' => 0,
'type' => 'story',
'status' => 'active',
'stage' => 'wait',
'openedBy' => 'admin',
'version' => 1
))
->exec();
}
$storyEmails = $this->objectModel->dao->select('notifyEmail')->from(TABLE_STORY)
->where('id')->in($stories)
->andWhere('notifyEmail')->ne('')
->fetchPairs();
if($storyEmails) $hasNotifyEmail = true;
}
// 创建模拟的bug数据
// 检查bug数据中是否有notifyEmail
if($bugs) {
foreach($bugs as $bugId) {
$bugId = (int)$bugId;
if($bugId <= 5) {
// ID 1-5 有notifyEmail
$notifyEmail = "bug{$bugId}@test.com";
$hasNotifyEmail = true;
} else {
// ID 6以上没有notifyEmail
$notifyEmail = '';
}
// 模拟插入bug数据
$this->objectModel->dao->replace(TABLE_BUG)
->data(array(
'id' => $bugId,
'title' => "Bug{$bugId}",
'notifyEmail' => $notifyEmail,
'product' => 1,
'module' => 0,
'type' => 'codeerror',
'status' => 'active',
'severity' => 3,
'pri' => 3,
'openedBy' => 'admin'
))
->exec();
}
$bugEmails = $this->objectModel->dao->select('notifyEmail')->from(TABLE_BUG)
->where('id')->in($bugs)
->andWhere('notifyEmail')->ne('')
->fetchPairs();
if($bugEmails) $hasNotifyEmail = true;
}
if(!$hasNotifyEmail) return 'no_email';
// 调用实际的sendMail2Feedback方法 (邮件会失败,但不会影响逻辑测试)
$this->objectModel->sendMail2Feedback($release, $subject);
// Mock mail config to prevent actual email sending
global $app;
$originalTurnon = isset($app->config->mail->turnon) ? $app->config->mail->turnon : true;
if(!isset($app->config->mail)) $app->config->mail = new stdClass();
$app->config->mail->turnon = false;
if(dao::isError()) return dao::getError();
try {
// 调用实际的sendMail2Feedback方法
$this->objectModel->sendMail2Feedback($release, $subject);
return 'success';
// Restore original config
$app->config->mail->turnon = $originalTurnon;
if(dao::isError()) return dao::getError();
return 'success';
}
catch(Exception $e) {
// Restore original config
$app->config->mail->turnon = $originalTurnon;
return 'error: ' . $e->getMessage();
}
}
/**
@@ -18,13 +18,17 @@ cid=0
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/lib/release.unittest.class.php';
// 创建基础用户数据
// 准备基础数据
zenData('product')->gen(3);
zenData('user')->gen(5);
zenData('story')->loadYaml('story_sendmail2feedback', false, 1)->gen(10);
zenData('bug')->loadYaml('bug_sendmail2feedback', false, 1)->gen(10);
su('admin');
$releaseTest = new releaseTest();
// 模拟创建不同场景的release对象
// 创建不同场景的release对象测试sendMail2Feedback方法
$release1 = new stdClass();
$release1->id = 1;
$release1->name = '版本1.0';
@@ -40,14 +44,14 @@ $release2->bugs = '1,2,3';
$release3 = new stdClass();
$release3->id = 3;
$release3->name = '版本3.0';
$release3->stories = '1,4,5';
$release3->stories = '1,4';
$release3->bugs = '';
$release4 = new stdClass();
$release4->id = 4;
$release4->name = '版本4.0';
$release4->stories = '';
$release4->bugs = '1,2,3';
$release4->bugs = '1,2';
$release5 = new stdClass();
$release5->id = 5;