| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522 |
- <?php
- /**
- * The model file of test task module of ZenTaoPMS.
- *
- * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
- * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
- * @author Chunsheng Wang <chunsheng@cnezsoft.com>
- * @package testtask
- * @version $Id: model.php 5114 2013-07-12 06:02:59Z chencongzhi520@gmail.com $
- * @link https://www.zentao.net
- */
- ?>
- <?php
- class testtaskModel extends model
- {
- /**
- * 创建一个测试单。
- * Create a test task.
- *
- * @param object $testtask
- * @access public
- * @return int|false
- */
- public function create($testtask)
- {
- $this->dao->insert(TABLE_TESTTASK)->data($testtask)
- ->autoCheck('begin,end')
- ->batchcheck($this->config->testtask->create->requiredFields, 'notempty')
- ->checkIF(!empty($testtask->begin) && $testtask->begin != '', 'begin', 'date')
- ->checkIF(!empty($testtask->end) && $testtask->end != '', 'end', 'date')
- ->checkIF(!empty($testtask->begin) && $testtask->begin != '', 'end', 'ge', zget($testtask, 'begin', ''))
- ->checkFlow()
- ->exec();
- if(dao::isError()) return false;
- $taskID = $this->dao->lastInsertID();
- $this->loadModel('action')->create('testtask', $taskID, 'opened');
- return $taskID;
- }
- /**
- * 获取一个产品的测试单列表。
- * Get testtasks of a product.
- *
- * @param int $productID
- * @param string $branch
- * @param string $type
- * @param string $begin
- * @param string $end
- * @param string $orderBy
- * @param object $pager
- * @access public
- * @return array
- */
- public function getProductTasks($productID, $branch = 'all', $type = '', $begin = '', $end = '', $orderBy = 'id_desc', $pager = null)
- {
- if(common::isTutorialMode()) return $this->loadModel('tutorial')->getTesttasks();
- $scopeAndStatus = explode(',', $type);
- $scope = !empty($scopeAndStatus[0]) ? $scopeAndStatus[0] : '';
- $status = !empty($scopeAndStatus[1]) ? $scopeAndStatus[1] : '';
- $branch = $scope == 'all' ? 'all' : $branch;
- $tasks = $this->fetchTesttaskList($productID, $branch, 0, '', $scope, $status, $begin, $end, $orderBy, $pager);
- return $this->processExecutionName($tasks);
- }
- /**
- * 根据项目名称和执行名称来更新执行名称。
- * Update the execution name based on the project name and execution name.
- *
- * @param array $tasks
- * @access private
- * @return array
- */
- private function processExecutionName($tasks)
- {
- foreach($tasks as $task)
- {
- if(!$task->multiple) continue;
- if($task->projectName && $task->executionName)
- {
- $task->executionName = $task->projectName . '/' . $task->executionName;
- continue;
- }
- if(!$task->executionName) $task->executionName = $task->projectName;
- }
- return $tasks;
- }
- /**
- * 获取产品对应的单元测试类型的测试单。
- * Get product unit tasks.
- *
- * @param int $productID
- * @param string $browseType
- * @param string $orderBy
- * @param object $pager
- * @access public
- * @return array
- */
- public function getProductUnitTasks($productID, $browseType = 'newest', $orderBy = 'id_desc', $pager = null)
- {
- $begin = '';
- $end = '';
- $beginAndEnd = $this->loadModel('action')->computeBeginAndEnd($browseType, '', 'next');
- if($browseType != 'all' and $browseType != 'newest' and !empty($beginAndEnd))
- {
- $begin = $beginAndEnd['begin'];
- $end = $beginAndEnd['end'];
- }
- if($browseType == 'newest') $orderBy = 'end_desc,' . $orderBy;
- $projectID = $this->lang->navGroup->testtask != 'qa' ? $this->session->project : 0;
- $tasks = $this->fetchTesttaskList($productID, '', $projectID, 'unit', 'local', '', $begin, $end, $orderBy, $pager);
- $resultGroups = $this->dao->select('t1.task, t2.*')->from(TABLE_TESTRUN)->alias('t1')
- ->leftJoin(TABLE_TESTRESULT)->alias('t2')->on('t1.id=t2.run')
- ->where('t1.task')->in(array_keys($tasks))
- ->fetchGroup('task', 'run');
- /* 计算本测试单执行的用例数、成功数、失败数。*/
- /* Calculate the number of test cases executed by this test, the number of successes, and the number of failures. */
- foreach($tasks as $taskID => $task)
- {
- $results = zget($resultGroups, $taskID, array());
- $task->caseCount = count($results);
- $task->passCount = 0;
- $task->failCount = 0;
- foreach($results as $result)
- {
- if($result->caseResult == 'pass') $task->passCount ++;
- if($result->caseResult == 'fail') $task->failCount ++;
- }
- }
- return $this->processExecutionName($tasks);
- }
- /**
- * 获取一个项目的测试单列表。
- * Get testtasks of a project.
- *
- * @param int $projectID
- * @param int $productID
- * @param string $orderBy
- * @param object $pager
- * @access public
- * @return array
- */
- public function getProjectTasks($projectID, $productID = 0, $orderBy = 'id_desc', $pager = null)
- {
- $tasks = $this->dao->select('t1.*, t1.id AS idName, t5.multiple, IF(t4.shadow = 1, t5.name, t4.name) AS productName, t3.name AS executionName, t2.name AS buildName, t2.branch AS branch, t5.name AS projectName, t4.`order` AS productOrder')
- ->from(TABLE_TESTTASK)->alias('t1')
- ->leftJoin(TABLE_BUILD)->alias('t2')->on('t1.build = t2.id')
- ->leftJoin(TABLE_EXECUTION)->alias('t3')->on('t1.execution = t3.id')
- ->leftJoin(TABLE_PRODUCT)->alias('t4')->on('t1.product = t4.id')
- ->leftJoin(TABLE_PROJECT)->alias('t5')->on('t3.project = t5.id')
- ->where('t1.project')->eq((int)$projectID)
- ->andWhere('t1.auto')->ne('unit')
- ->andWhere('t1.deleted')->eq('0')
- ->beginIF($productID)->andWhere('t1.product')->eq($productID)->fi()
- ->orderBy('productOrder_asc, ' . $orderBy)
- ->page($pager)
- ->fetchAll('id', false);
- return $this->processExecutionName($tasks);
- }
- /**
- * 获取一个执行的测试单列表。
- * Get testtasks of a execution.
- *
- * @param int $executionID
- * @param int $productID
- * @param string $objectType
- * @param string $orderBy
- * @param object $pager
- * @access public
- * @return array
- */
- public function getExecutionTasks($executionID, $productID = 0, $objectType = 'execution', $orderBy = 'id_desc', $pager = null)
- {
- if(common::isTutorialMode()) return $this->loadModel('tutorial')->getTesttasks();
- return $this->dao->select('t1.*, t2.name AS buildName, t3.name AS productName')
- ->from(TABLE_TESTTASK)->alias('t1')
- ->leftJoin(TABLE_BUILD)->alias('t2')->on('t1.build = t2.id')
- ->leftJoin(TABLE_PRODUCT)->alias('t3')->on('t1.product = t3.id')
- ->where('t1.deleted')->eq('0')
- ->beginIF($objectType == 'execution')->andWhere('t1.execution')->eq((int)$executionID)->fi()
- ->beginIF($objectType == 'project')->andWhere('t1.project')->eq((int)$executionID)->fi()
- ->beginIF($productID)->andWhere('t1.product')->eq($productID)->fi()
- ->andWhere('t1.auto')->ne('unit')
- ->orderBy($orderBy)
- ->page($pager)
- ->fetchAll('id', false);
- }
- /**
- * 获取测试单键值对。
- * Get key-value pairs of testtask.
- *
- * @param int $productID
- * @param int $executionID
- * @param int $appendTaskID
- * @access public
- * @return array
- * @param bool $unit
- */
- public function getPairs($productID, $executionID = 0, $appendTaskID = 0, $unit = true)
- {
- $pairs = $this->dao->select('id, name')->from(TABLE_TESTTASK)
- ->where('deleted')->eq('0')
- ->andWhere('product')->eq($productID)
- ->beginIF($executionID)->andWhere('execution')->eq($executionID)->fi()
- ->beginIF($unit)->andWhere('auto')->ne('unit')->fi()
- ->orderBy('id_desc')
- ->fetchPairs();
- if($appendTaskID) $pairs += $this->dao->select('id, name')->from(TABLE_TESTTASK)->where('id')->eq($appendTaskID)->andWhere('auto')->ne('unit')->fetchPairs();
- return $pairs;
- }
- /**
- * 根据多个测试单 ID 获取测试单列表。
- * Get testtasks by id list.
- *
- * @param array $idList
- * @access public
- * @return array
- */
- public function getByList($idList)
- {
- if(!$idList) return array();
- return $this->dao->select('*')->from(TABLE_TESTTASK)->where('id')->in($idList)->fetchAll('id', false);
- }
- /**
- * 通过 ID 列表获取测试单键对。
- * Get key-value pairs of testtasks by id list.
- *
- * @param array $taskIdList
- * @access public
- * @return array
- */
- public function getPairsByList($taskIdList)
- {
- if(common::isTutorialMode()) return $this->loadModel('tutorial')->getTesttaskPairs();
- return $this->dao->select('id,name')->from(TABLE_TESTTASK)->where('id')->in($taskIdList)->fetchPairs();
- }
- /**
- * 根据 ID 获取单条测试单的数据。
- * Get a testtask by id.
- *
- * @param int $taskID
- * @param bool $setImgSize
- * @access public
- * @return object|false
- * @param int $testtaskID
- */
- public function getByID($testtaskID, $setImgSize = false)
- {
- if(common::isTutorialMode()) return $this->loadModel('tutorial')->getTesttask();
- $testtask = $this->dao->select('*')->from(TABLE_TESTTASK)->where('id')->eq($testtaskID)->fetch();
- if(!$testtask) return false;
- $product = $this->dao->select('name,type')->from(TABLE_PRODUCT)->where('id')->eq($testtask->product)->fetch();
- $testtask->productName = !empty($product->name) ? $product->name : '';
- $testtask->productType = !empty($product->type) ? $product->type : '';
- $testtask->branch = 0;
- $testtask->executionName = '';
- $testtask->buildName = '';
- if($testtask->execution)
- {
- $testtask->executionName = $this->dao->select('name')->from(TABLE_EXECUTION)->where('id')->eq($testtask->execution)->fetch('name');
- $testtask->branch = $this->dao->select('branch')->from(TABLE_PROJECTPRODUCT)->where('project')->eq($testtask->execution)->andWhere('product')->eq($testtask->product)->fetch('branch');
- }
- if($testtask->build)
- {
- $build = $this->dao->select('branch,name')->from(TABLE_BUILD)->where('id')->eq($testtask->build)->fetch();
- $testtask->buildName = zget($build, 'name', '');
- $testtask->branch = zget($build, 'branch', '');
- }
- $testtask = $this->loadModel('file')->replaceImgURL($testtask, 'desc');
- if($setImgSize) $testtask->desc = $this->loadModel('file')->setImgSize($testtask->desc);
- $testtask->files = $this->loadModel('file')->getByObject('testtask', $testtask->id);
- if(!$testtask->branch) $testtask->branch = '0';
- return $testtask;
- }
- /**
- * 获取某个用户负责的测试单列表。
- * Get testtasks that a user is responsible for.
- *
- * @param string $account
- * @param object $pager
- * @param string $orderBy
- * @param string $type
- * @access public
- * @return array
- */
- public function getByUser($account, $pager = null, $orderBy = 'id_desc', $type = '')
- {
- return $this->dao->select("t1.*, t2.name AS executionName, t2.multiple AS executionMultiple, t5.name AS projectName, t3.name AS buildName, t4.name AS productName, CONCAT(t2.name, '/', t3.name) as executionBuild")
- ->from(TABLE_TESTTASK)->alias('t1')
- ->leftJoin(TABLE_EXECUTION)->alias('t2')->on('t1.execution = t2.id')
- ->leftJoin(TABLE_BUILD)->alias('t3')->on('t1.build = t3.id')
- ->leftJoin(TABLE_PRODUCT)->alias('t4')->on('t1.product = t4.id')
- ->leftJoin(TABLE_PROJECT)->alias('t5')->on('t2.project = t5.id')
- ->where('t1.deleted')->eq('0')
- ->andWhere('t4.deleted')->eq('0')
- ->andWhere('t1.auto')->ne('unit')
- ->andWhere('(t1.owner')->eq($account)
- ->orWhere("FIND_IN_SET('$account', t1.members)")
- ->markRight(1)
- ->beginIF(!$this->app->user->admin)->andWhere('t2.id')->in($this->app->user->view->sprints)->fi()
- ->beginIF($type == 'wait')->andWhere('t1.status')->ne('done')->fi()
- ->beginIF($type == 'done')->andWhere('t1.status')->eq('done')->fi()
- ->orderBy($orderBy)
- ->page($pager)
- ->fetchAll('id', false);
- }
- /**
- * 根据测试单 ID 和用例 ID 获取一个测试执行。
- * Get a testrun by testtask ID and case ID.
- *
- * @param int $taskID
- * @param int $caseID
- * @access public
- * @return object
- */
- public function getRunByCase($taskID, $caseID)
- {
- return $this->dao->select('*')->from(TABLE_TESTRUN)->where('task')->eq($taskID)->andWhere('`case`')->eq($caseID)->fetch();
- }
- /**
- * 根据不同类型获取可关联的测试用例。
- * Get linkable cases according to different typs.
- *
- * @param int $productID
- * @param object $task
- * @param string $type
- * @param int $param
- * @param object $pager
- * @access public
- * @return array
- */
- public function getLinkableCases($productID, $task, $type = 'all', $param = 0, $pager = null)
- {
- if($this->session->testcaseQuery == false) $this->session->set('testcaseQuery', ' 1 = 1');
- $query = $this->session->testcaseQuery;
- $allProduct = "`product` = 'all'";
- if(strpos($query, '`product` =') === false && $type != 'bysuite') $query .= " AND `product` = $productID";
- if(strpos($query, $allProduct) !== false) $query = str_replace($allProduct, '1', $query);
- $linkedCases = $this->dao->select('`case`')->from(TABLE_TESTRUN)->where('task')->eq($task->id)->fetchPairs('case');
- if($type == 'all') return $this->getAllLinkableCases($task, $query, $linkedCases, $pager);
- if($type == 'bystory') return $this->getLinkableCasesByStory($productID, $task, $query, $linkedCases, $pager);
- if($type == 'bybug') return $this->getLinkableCasesByBug($productID, $task, $query, $linkedCases, $pager);
- if($type == 'bysuite') return $this->getLinkableCasesBySuite($productID, $task, $param, $query, $linkedCases, $pager);
- if($type == 'bybuild') return $this->getLinkableCasesByTestTask($param, $query, $linkedCases, $pager);
- return array();
- }
- /**
- * 获取所有可关联的测试用例。
- * Get all linkable cases.
- *
- * @param object $task
- * @param string $query
- * @param array $linkedCases
- * @param object $pager
- * @access public
- * @return array
- */
- public function getAllLinkableCases($task, $query = '', $linkedCases = array(), $pager = null)
- {
- if(common::isTutorialMode()) return $this->loadModel('tutorial')->getCases();
- return $this->dao->select('*')->from(TABLE_CASE)
- ->where('deleted')->eq('0')
- ->andWhere('status')->ne('wait')
- ->andWhere('type')->ne('unit')
- ->beginIF($query)->andWhere($query)->fi()
- ->beginIF(!empty($linkedCases))->andWhere('id')->notIN($linkedCases)->fi()
- ->beginIF($task->branch !== '')->andWhere('branch')->in("0,{$task->branch}")->fi()
- ->orderBy('id desc')
- ->page($pager)
- ->fetchAll('id', false);
- }
- /**
- * 根据测试单所属的版本关联的需求获取可关联的用例。
- * Get linkable cases based on the stories associated with the build to which the testtask belongs.
- *
- * @param int $productID
- * @param object $task
- * @param string $query
- * @param array $linkedCases
- * @param object $pager
- * @access public
- * @return array
- */
- public function getLinkableCasesByStory($productID, $task, $query = '', $linkedCases = array(), $pager = null)
- {
- $stories = $this->dao->select('stories')->from(TABLE_BUILD)->where('id')->eq($task->build)->fetch('stories');
- if(!$stories) return array();
- $query = preg_replace('/`(\w+)`/', 't1.`$1`', $query);
- return $this->dao->select('t1.*, t2.title AS storyTitle')->from(TABLE_CASE)->alias('t1')
- ->leftJoin(TABLE_STORY)->alias('t2')->on('t1.story = t2.id')
- ->where('t1.deleted')->eq('0')
- ->andWhere('t1.product')->eq($productID)
- ->andWhere('t1.status')->ne('wait')
- ->andWhere('t1.story')->in(trim($stories, ','))
- ->beginIF($query)->andWhere($query)->fi()
- ->beginIF(!empty($linkedCases))->andWhere('t1.id')->notin($linkedCases)->fi()
- ->beginIF($task->branch !== '')->andWhere('t1.branch')->in("0,{$task->branch}")->fi()
- ->beginIF($this->lang->navGroup->testtask != 'qa')->andWhere('t1.project')->eq($this->session->project)->fi()
- ->orderBy('t1.id desc')
- ->page($pager)
- ->fetchAll('id', false);
- }
- /**
- * 根据测试单所属的版本关联的 Bug 获取可关联的用例。
- * Get linkable cases based on the bugs associated with the build to which the testtask belongs.
- *
- * @param int $productID
- * @param object $task
- * @param string $query
- * @param array $linkedCases
- * @param object $pager
- * @access public
- * @return array
- */
- public function getLinkableCasesByBug($productID, $task, $query = '', $linkedCases = array(), $pager = null)
- {
- $bugs = $this->dao->select('bugs')->from(TABLE_BUILD)->where('id')->eq($task->build)->fetch('bugs');
- if(!$bugs) return array();
- return $this->dao->select('*')->from(TABLE_CASE)
- ->where('deleted')->eq('0')
- ->andWhere('product')->eq($productID)
- ->andWhere('status')->ne('wait')
- ->andWhere('fromBug')->in(trim($bugs, ','))
- ->beginIF($query)->andWhere($query)->fi()
- ->beginIF($linkedCases)->andWhere('id')->notIN($linkedCases)->fi()
- ->beginIF($task->branch !== '')->andWhere('branch')->in("0,{$task->branch}")->fi()
- ->beginIF($this->lang->navGroup->testtask != 'qa')->andWhere('project')->eq($this->session->project)->fi()
- ->orderBy('id desc')
- ->page($pager)
- ->fetchAll('id', false);
- }
- /**
- * 根据测试套件获取可关联的用例。
- * Get linkable cases by suite.
- *
- * @param int $productID
- * @param object $task
- * @param int $suite
- * @param string $query
- * @param array $linkedCases
- * @param object $pager
- * @access public
- * @return array
- */
- public function getLinkableCasesBySuite($productID, $task, $suite, $query = '', $linkedCases = array(), $pager = null)
- {
- if(strpos($query, '`product`') !== false) $query = str_replace('`product`', 't1.`product`', $query);
- return $this->dao->select('t1.*, t2.version AS version, COALESCE(t3.title, t1.title) AS title')->from(TABLE_CASE)->alias('t1')
- ->leftJoin(TABLE_SUITECASE)->alias('t2')->on('t1.id=t2.case')
- ->leftJoin(TABLE_CASESPEC)->alias('t3')->on('t1.id=t3.case AND t2.version=t3.version')
- ->where('t1.deleted')->eq('0')
- ->andWhere('t1.product')->eq($productID)
- ->andWhere('t1.status')->ne('wait')
- ->andWhere('t2.suite')->eq($suite)
- ->beginIF($query)->andWhere($query)->fi()
- ->beginIF($linkedCases)->andWhere('t1.id')->notIN($linkedCases)->fi()
- ->beginIF($task->branch !== '')->andWhere('t1.branch')->in("0,{$task->branch}")->fi()
- ->beginIF($this->lang->navGroup->testtask != 'qa')->andWhere('t1.project')->eq($this->session->project)->fi()
- ->orderBy('id desc')
- ->page($pager)
- ->fetchAll('id', false);
- }
- /**
- * 根据测试单获取可关联的用例。
- * Get linkeable cases by testtask.
- *
- * @param int $testTask
- * @param string $query
- * @param array $linkedCases
- * @param object $pager
- * @access public
- * @return array
- */
- public function getLinkableCasesByTestTask($testTask, $query = '', $linkedCases = array(), $pager = null)
- {
- /* Format the query condition. */
- $query = preg_replace('/`(\w+)`/', 't1.`$1`', $query);
- $query = str_replace('t1.`lastRunner`', 't2.`lastRunner`', $query);
- $query = str_replace('t1.`lastRunDate`', 't2.`lastRunDate`', $query);
- $query = str_replace('t1.`lastRunResult`', 't2.`lastRunResult`', $query);
- return $this->dao->select('t1.*, t2.lastRunner, t2.lastRunDate, t2.lastRunResult')->from(TABLE_CASE)->alias('t1')
- ->leftJoin(TABLE_TESTRUN)->alias('t2')->on('t1.id = t2.case')
- ->where('t2.task')->eq($testTask)
- ->andWhere('t1.status')->ne('wait')
- ->beginIF($query)->andWhere($query)->fi()
- ->beginIF($linkedCases)->andWhere('t1.id')->notin($linkedCases)->fi()
- ->beginIF($this->lang->navGroup->testtask != 'qa')->andWhere('t1.project')->eq($this->session->project)->fi()
- ->page($pager)
- ->fetchAll('id', false);
- }
- /**
- * 按条件获取获取产品下的测试单。
- * Get testtasks under a product by condition.
- *
- * @param int $productID
- * @param int $testtaskID
- * @access public
- * @return array
- * @param int $testTaskID
- */
- public function getRelatedTestTasks($productID, $testTaskID)
- {
- $beginDate = $this->dao->select('begin')->from(TABLE_TESTTASK)->where('id')->eq($testTaskID)->fetch('begin');
- return $this->dao->select('id, name')->from(TABLE_TESTTASK)
- ->where('product')->eq($productID)
- ->andWhere('auto')->ne('unit')
- ->beginIF($beginDate)->andWhere('begin')->le($beginDate)->fi()
- ->andWhere('deleted')->eq('0')
- ->andWhere('id')->ne($testTaskID)
- ->orderBy('begin desc')
- ->fetchPairs();
- }
- /**
- * 按执行结果统计测试单中的用例。
- * Get report data of a testtask by execution results.
- *
- * @param int $taskID
- * @access public
- * @return array
- */
- public function getDataOfTestTaskPerRunResult($taskID)
- {
- $datas = $this->dao->select("t1.lastRunResult AS name, COUNT('t1.*') AS value")->from(TABLE_TESTRUN)->alias('t1')
- ->leftJoin(TABLE_CASE)->alias('t2')
- ->on('t1.case = t2.id')
- ->where('t1.task')->eq($taskID)
- ->andWhere('t2.deleted')->eq('0')
- ->groupBy('t1.lastRunResult')
- ->orderBy('value DESC')
- ->fetchAll('name');
- if(!$datas) return array();
- $this->app->loadLang('testcase');
- foreach($datas as $result => $data) $data->name = zget($this->lang->testcase->resultList, $result, $this->lang->testtask->unexecuted);
- return $datas;
- }
- /**
- * 按类型统计测试单中的用例。
- * Get report data of a testtask by case type.
- *
- * @param int $taskID
- * @access public
- * @return array
- */
- public function getDataOfTestTaskPerType($taskID)
- {
- $datas = $this->dao->select('t2.type AS name, COUNT(1) AS value')->from(TABLE_TESTRUN)->alias('t1')
- ->leftJoin(TABLE_CASE)->alias('t2')->on('t1.case = t2.id')
- ->where('t1.task')->eq($taskID)
- ->andWhere('t2.deleted')->eq('0')
- ->groupBy('t2.type')
- ->orderBy('value desc')
- ->fetchAll('name');
- if(!$datas) return array();
- foreach($datas as $result => $data) if(isset($this->lang->testcase->typeList[$result])) $data->name = $this->lang->testcase->typeList[$result];
- return $datas;
- }
- /**
- * 按模块统计测试单中的用例。
- * Get report data of a testtask by case module.
- *
- * @param int $taskID
- * @access public
- * @return array
- */
- public function getDataOfTestTaskPerModule($taskID)
- {
- $datas = $this->dao->select('t2.module AS name, COUNT(1) AS value')->from(TABLE_TESTRUN)->alias('t1')
- ->leftJoin(TABLE_CASE)->alias('t2')->on('t1.case = t2.id')
- ->where('t1.task')->eq($taskID)
- ->andWhere('t2.deleted')->eq('0')
- ->groupBy('t2.module')
- ->orderBy('value desc')
- ->fetchAll('name');
- if(!$datas) return array();
- $modules = $this->loadModel('tree')->getModulesName(array_keys($datas));
- foreach($datas as $moduleID => $data) $data->name = zget($modules, $moduleID, '/');
- return $datas;
- }
- /**
- * 按执行人统计测试单中的用例。
- * Get report data of a testtask by executor.
- *
- * @param int $taskID
- * @access public
- * @return array
- */
- public function getDataOfTestTaskPerRunner($taskID)
- {
- $datas = $this->dao->select('t1.lastRunner AS name, COUNT(1) AS value')->from(TABLE_TESTRUN)->alias('t1')
- ->leftJoin(TABLE_CASE)->alias('t2')->on('t1.case = t2.id')
- ->where('t1.task')->eq($taskID)
- ->andWhere('t2.deleted')->eq('0')
- ->groupBy('t1.lastRunner')
- ->orderBy('value DESC')
- ->fetchAll('name');
- if(!$datas) return array();
- $users = $this->loadModel('user')->getPairs('noclosed|noletter');
- foreach($datas as $result => $data) $data->name = $result ? zget($users, $result) : $this->lang->testtask->unexecuted;
- return $datas;
- }
- /**
- * 更新测试单。
- * Update a test task.
- *
- * @param object $task
- * @param object $oldTask
- * @access public
- * @return array|bool
- */
- public function update($task, $oldTask)
- {
- $this->dao->update(TABLE_TESTTASK)->data($task, 'deleteFiles,renameFiles,files')
- ->autoCheck()
- ->batchcheck($this->config->testtask->edit->requiredFields, 'notempty')
- ->checkIF($task->end != '', 'end', 'ge', $task->begin)
- ->checkFlow()
- ->where('id')->eq($task->id)
- ->exec();
- if(dao::isError()) return false;
- $this->loadModel('file')->processFileDiffsForObject('testtask', $oldTask, $task);
- $task = $this->file->replaceImgURL($task, 'desc');
- return common::createChanges($oldTask, $task);
- }
- /**
- * 开始一个测试单。
- * Start testtask.
- *
- * @param object $task
- * @access public
- * @return bool|array
- */
- public function start($task)
- {
- $taskID = (int)$task->id;
- $oldTask = $this->fetchByID($taskID);
- if(!$oldTask || !self::isClickable($oldTask, 'start')) return false;
- $this->dao->update(TABLE_TESTTASK)->data($task, 'comment,uid')
- ->autoCheck()
- ->checkFlow()
- ->where('id')->eq($taskID)
- ->exec();
- if(dao::isError()) return false;
- return common::createChanges($oldTask, $task);
- }
- /**
- * 关闭一个测试单。
- * Close a testtask.
- *
- * @param object $task
- * @access public
- * @return bool|array
- */
- public function close($task)
- {
- $taskID = (int)$task->id;
- $oldTask = $this->fetchByID($taskID);
- if(!$oldTask || !self::isClickable($oldTask, 'close')) return false;
- if($task->realFinishedDate <= $oldTask->begin) dao::$errors['realFinishedDate'][] = sprintf($this->lang->testtask->finishedDateLess, $oldTask->begin);
- if($task->realFinishedDate > date('Y-m-d 00:00:00', strtotime('+1 day'))) dao::$errors['realFinishedDate'][] = $this->lang->testtask->finishedDateMore;
- if(dao::isError()) return false;
- $this->dao->update(TABLE_TESTTASK)->data($task, 'comment,uid')
- ->autoCheck()
- ->checkFlow()
- ->where('id')->eq($taskID)
- ->exec();
- if(dao::isError()) return false;
- return common::createChanges($oldTask, $task);
- }
- /**
- * 阻塞一个测试单。
- * Block a testtask.
- *
- * @param object $task
- * @access public
- * @return bool|array
- */
- public function block($task)
- {
- $taskID = (int)$task->id;
- $oldTask = $this->fetchByID($taskID);
- if(!$oldTask || !self::isClickable($oldTask, 'block')) return false;
- $this->dao->update(TABLE_TESTTASK)->data($task, 'comment,uid')
- ->autoCheck()
- ->checkFlow()
- ->where('id')->eq($taskID)
- ->exec();
- if(dao::isError()) return false;
- return common::createChanges($oldTask, $task);
- }
- /**
- * 激活一个测试单。
- * Activate a testtask.
- *
- * @param object $task
- * @access public
- * @return bool|array
- */
- public function activate($task)
- {
- $taskID = (int)$task->id;
- $oldTask = $this->fetchByID($taskID);
- if(!$oldTask || !self::isClickable($oldTask, 'activate')) return false;
- $this->dao->update(TABLE_TESTTASK)->data($task, 'comment,uid')
- ->autoCheck()
- ->checkFlow()
- ->where('id')->eq($taskID)
- ->exec();
- if(dao::isError()) return false;
- return common::createChanges($oldTask, $task);
- }
- /**
- * 更新测试单状态。
- * Update testtask's status.
- *
- * @param int $task
- * @access public
- * @return bool
- * @param int $taskID
- */
- public function updateStatus($taskID)
- {
- if(empty($taskID)) return false;
- $oldTask = $this->fetchByID($taskID);
- if(empty($oldTask) || $oldTask->status == 'doing') return false;
- $task = new stdClass();
- $task->status = 'doing';
- if(empty($task->realBegan)) $task->realBegan = helper::today();
- $this->dao->update(TABLE_TESTTASK)->data($task)->autoCheck()->checkFlow()->where('id')->eq($taskID)->exec();
- if(dao::isError()) return false;
- $changes = common::createChanges($oldTask, $task);
- if($changes)
- {
- $actionID = $this->loadModel('action')->create('testtask', $taskID, 'syncByCase');
- $this->action->logHistory($actionID, $changes);
- }
- return !dao::isError();
- }
- /**
- * 关联用例到一个测试单。
- * Link cases to a testtask.
- *
- * @param int $taskID
- * @param string $type all|bystory|bysuite|bybuild|bybug
- * @param array $runs
- * @access public
- * @return bool
- */
- public function linkCase($taskID, $type, $runs)
- {
- if(!$runs) return false;
- $users = array();
- $caseIdList = array_unique(array_filter(array_map(function($run){return $run->case;}, $runs)));
- if($type == 'bybuild' && $caseIdList) $users = $this->dao->select('`case`, assignedTo')->from(TABLE_TESTRUN)->where('`case`')->in($caseIdList)->fetchPairs();
- $testtask = $this->dao->select('*')->from(TABLE_TESTTASK)->where('id')->eq($taskID)->fetch();
- if(!empty($testtask->execution))
- {
- $execution = $this->loadModel('execution')->fetchByID((int)$testtask->execution);
- $executionLastOrder = $this->dao->select('MAX(`order`) AS `order`')->from(TABLE_PROJECTCASE)->where('project')->eq((int)$execution->id)->fetch('order');
- $projectLastOrder = $this->dao->select('MAX(`order`) AS `order`')->from(TABLE_PROJECTCASE)->where('project')->eq((int)$execution->project)->fetch('order');
- }
- elseif(!empty($testtask->project))
- {
- $lastOrder = $this->dao->select('MAX(`order`) AS `order`')->from(TABLE_PROJECTCASE)->where('project')->eq((int)$testtask->project)->fetch('order');
- }
- $case = new stdclass();
- $case->product = $this->session->product;
- $case->version = 1;
- $this->loadModel('action');
- foreach($runs as $run)
- {
- $run->task = $taskID;
- $run->status = 'normal';
- $run->assignedTo = zget($users, $run->case, '');
- $this->dao->replace(TABLE_TESTRUN)->data($run)->exec();
- if(!empty($testtask->joint)) continue;
- /* 在项目或执行下关联用例到测试单时把用例关联到项目或执行。*/
- /* Associate the cases to the project or execution when associating the cases to the testtask under the project or execution. */
- if(!empty($testtask->execution))
- {
- $executionLastOrder++;
- $projectLastOrder++;
- $case->project = (int)$execution->id;
- $case->case = $run->case;
- $case->order = $executionLastOrder;
- $this->dao->replace(TABLE_PROJECTCASE)->data($case)->exec();
- $case->project = (int)$execution->project;
- $case->order = $projectLastOrder;
- $this->dao->replace(TABLE_PROJECTCASE)->data($case)->exec();
- }
- elseif(!empty($testtask->project))
- {
- $case->project = (int)$testtask->project;
- $case->case = $run->case;
- $case->order = ++$lastOrder;
- $this->dao->replace(TABLE_PROJECTCASE)->data($case)->exec();
- }
- $this->action->create('case', $run->case, 'linked2testtask', '', $taskID);
- }
- return !dao::isError();
- }
- /**
- * 从测试单移除一个用例。
- * Remove a case from a testtask.
- *
- * @param int $runID
- * @access public
- * @return bool
- */
- public function unlinkCase($runID)
- {
- $run = $this->dao->select('t1.task,t1.`case`,t2.story')->from(TABLE_TESTRUN)->alias('t1')
- ->leftJoin(TABLE_CASE)->alias('t2')->on('t1.case=t2.id')
- ->where('t1.id')->eq($runID)
- ->fetch();
- if(!$run) return false;
- $linkedProjects = $run->story ? $this->dao->select('project')->from(TABLE_PROJECTSTORY)->where('story')->eq($run->story)->fetchPairs() : array();
- if($linkedProjects) $this->dao->delete()->from(TABLE_PROJECTCASE)->where('`case`')->eq($run->case)->andWhere('project')->notin($linkedProjects)->exec();
- $this->dao->delete()->from(TABLE_TESTRUN)->where('id')->eq($runID)->exec();
- $this->loadModel('action')->create('case' ,$run->case, 'unlinkedfromtesttask', '', $run->task);
- return !dao::isError();
- }
- /**
- * 批量从测试单移除用例。
- * Batch remove cases from a testtask.
- *
- * @param int $taskID
- * @param array $caseIdList
- * @access public
- * @return bool
- */
- public function batchUnlinkCases($taskID, $caseIdList)
- {
- if(!$taskID || !$caseIdList) return false;
- $cases = $this->dao->select('`case`')->from(TABLE_TESTRUN)->where('task')->eq($taskID)->andWhere('`case`')->in($caseIdList)->fetchPairs();
- if(!$cases) return false;
- $this->dao->delete()->from(TABLE_TESTRUN)->where('task')->eq($taskID)->andWhere('`case`')->in($cases)->exec();
- if(dao::isError()) return false;
- $this->loadModel('action');
- foreach($cases as $caseID) $this->action->create('case', $caseID, 'unlinkedfromtesttask', '', $taskID);
- return !dao::isError();
- }
- /**
- * 在测试单内指派用例。
- * Assign a case in a testtask.
- *
- * @param object $run
- * @param object $oldRun
- * @access public
- * @return array|bool
- */
- public function assignCase($run, $oldRun)
- {
- if(!empty($oldRun->assignedTo) && $run->assignedTo == $oldRun->assignedTo) return array();
- $this->dao->update(TABLE_TESTRUN)->data($run, 'comment,uid')->where('id')->eq($run->id)->exec();
- if(dao::isError()) return false;
- return common::createChanges($oldRun, $run);
- }
- /**
- * 批量指派一个测试单中的用例。
- * Batch assign cases in a testtask.
- *
- * @param int $taskID
- * @param string $account
- * @param array $caseIdList
- * @access public
- * @return bool
- */
- public function batchAssign($taskID, $account, $caseIdList)
- {
- if(!$taskID || !$account || !$caseIdList) return false;
- $cases = $this->dao->select('`case`')->from(TABLE_TESTRUN)->where('task')->eq($taskID)->andWhere('`case`')->in($caseIdList)->fetchPairs();
- if(!$cases) return false;
- $this->dao->update(TABLE_TESTRUN)->set('assignedTo')->eq($account)->where('task')->eq($taskID)->andWhere('`case`')->in($cases)->exec();
- if(dao::isError()) return false;
- $this->loadModel('action');
- foreach($cases as $caseID) $this->action->create('case', $caseID, 'assigned', '', $account);
- return !dao::isError();
- }
- /**
- * 给排序字段添加前缀。
- * Add a prefix to the sort field.
- *
- * @param string $orderBy
- * @access private
- * @return string
- */
- private function addPrefixToOrderBy($orderBy)
- {
- $specialFields = ',assignedTo,status,lastRunResult,lastRunner,lastRunDate,';
- $fieldToSort = strpos($orderBy, '_') ? substr($orderBy, 0, strpos($orderBy, '_')) : $orderBy;
- $orderBy = strpos($specialFields, ',' . $fieldToSort . ',') !== false ? ('t1.' . $orderBy) : ('t2.' . $orderBy);
- return $orderBy;
- }
- /**
- * 获取一个测试单关联的测试用例及相关需求。
- * Get cases associated with a testtask.
- *
- * @param int $taskID
- * @param array $modules
- * @param string $orderBy
- * @param object $pager
- * @access public
- * @return array
- */
- public function getRuns($taskID, $modules, $orderBy, $pager = null)
- {
- $orderBy = $this->addPrefixToOrderBy($orderBy);
- return $this->dao->select("t2.*, t1.*, t2.version AS caseVersion, COALESCE(t3.title, '') AS storyTitle, t2.status AS caseStatus, IF(t4.title IS NULL, t2.title, t4.title) AS title")->from(TABLE_TESTRUN)->alias('t1')
- ->leftJoin(TABLE_CASE)->alias('t2')->on('t1.case = t2.id')
- ->leftJoin(TABLE_STORY)->alias('t3')->on('t2.story = t3.id')
- ->leftJoin(TABLE_CASESPEC)->alias('t4')->on('t1.`case` = t4.`case` AND t1.version = t4.version')
- ->where('t1.task')->eq($taskID)
- ->andWhere('t2.deleted')->eq('0')
- ->beginIF(!$this->app->user->admin)->andWhere('t2.product')->in($this->app->user->view->products)->fi()
- ->beginIF($modules)->andWhere('t2.module')->in($modules)->fi()
- ->orderBy($orderBy)
- ->page($pager)
- ->fetchAll('id', false);
- }
- /**
- * 根据一个测试套件获取一个测试单关联的测试用例。
- * Get cases associated with a testtask according by a test suite.
- *
- * @param int $taskID
- * @param int $suiteID
- * @param string $orderBy
- * @param object $pager
- * @access public
- * @return array
- */
- public function getRunsBySuite($taskID, $suiteID, $orderBy, $pager = null)
- {
- $orderBy = $this->addPrefixToOrderBy($orderBy);
- $cases = $this->loadModel('testsuite')->getLinkedCasePairs($suiteID);
- return $this->dao->select("t2.*, t1.*, COALESCE(t3.title, '') AS storyTitle, t2.status AS caseStatus, t2.version AS caseVersion, IF(t4.title IS NULL, t2.title, t4.title) AS title")->from(TABLE_TESTRUN)->alias('t1')
- ->leftJoin(TABLE_CASE)->alias('t2')->on('t1.case = t2.id')
- ->leftJoin(TABLE_STORY)->alias('t3')->on('t2.story = t3.id')
- ->leftJoin(TABLE_CASESPEC)->alias('t4')->on('t1.case = t4.case AND t1.version = t4.version')
- ->where('t1.task')->eq($taskID)
- ->andWhere('t2.deleted')->eq('0')
- ->andWhere('t2.id')->in(array_keys($cases))
- ->orderBy($orderBy)
- ->page($pager)
- ->fetchAll('id', false);
- }
- /**
- * 按照测试单分组展示用例。
- * Group case run by suite.
- *
- * @param int $taskID
- * @param string $orderBy
- * @access public
- * @return array
- */
- public function getRunsForUnitCases($taskID, $orderBy = 't1.id_desc')
- {
- return $this->dao->select("t2.*, t1.*, t2.version AS caseVersion, COALESCE(t3.title, '') AS storyTitle, t2.status AS caseStatus, t4.suite, t5.name AS suiteTitle")->from(TABLE_TESTRUN)->alias('t1')
- ->leftJoin(TABLE_CASE)->alias('t2')->on('t1.case = t2.id')
- ->leftJoin(TABLE_STORY)->alias('t3')->on('t2.story = t3.id')
- ->leftJoin(TABLE_SUITECASE)->alias('t4')->on('t1.case = t4.case')
- ->leftJoin(TABLE_TESTSUITE)->alias('t5')->on('t4.suite = t5.id')
- ->where('t1.task')->eq($taskID)
- ->andWhere('t2.deleted')->eq('0')
- ->orderBy($orderBy)
- ->fetchAll('id', false);
- }
- /**
- * 获取一个测试单中指派给一个用户的测试用例。
- * Get the cases assigned to a user among the cases associated with a testtask.
- *
- * @param int $taskID
- * @param string $user
- * @param array $modules
- * @param string $orderBy
- * @param object $pager
- * @access public
- * @return array
- */
- public function getUserRuns($taskID, $user, $modules = array(), $orderBy = 'id_desc', $pager = null)
- {
- $orderBy = $this->addPrefixToOrderBy($orderBy);
- return $this->dao->select("t2.*, t1.*, COALESCE(t3.title, '') AS storyTitle, t2.status AS caseStatus,t2.version AS caseVersion, IF(t4.title IS NULL, t2.title, t4.title) AS title")->from(TABLE_TESTRUN)->alias('t1')
- ->leftJoin(TABLE_CASE)->alias('t2')->on('t1.case = t2.id')
- ->leftJoin(TABLE_STORY)->alias('t3')->on('t2.story = t3.id')
- ->leftJoin(TABLE_CASESPEC)->alias('t4')->on('t1.case = t4.case AND t1.version = t4.version')
- ->where('t1.task')->eq($taskID)
- ->andWhere('t1.assignedTo')->eq($user)
- ->andWhere('t2.deleted')->eq('0')
- ->beginIF($modules)->andWhere('t2.module')->in($modules)->fi()
- ->orderBy($orderBy)
- ->page($pager)
- ->fetchAll('id', false);
- }
- /**
- * 获取用例以及场景数据。
- * Get cases with scenes data.
- *
- * @param int|array $productID
- * @param array $runs
- * @access public
- * @return array
- */
- public function getSceneCases($productID, $runs)
- {
- $scenes = $this->dao->select('*')->from(TABLE_SCENE)
- ->where('deleted')->eq('0')
- ->andWhere('product')->in($productID)
- ->orderBy('grade_desc, sort_asc')
- ->fetchAll('id', false);
- $displayScenes = array();
- foreach($runs as $run)
- {
- if(!empty($run->scene)) $displayScenes[] = $run->scene;
- $run->parent = !empty($run->scene) ? 'scene-' . $scenes[$run->scene]->id : 0;
- $run->isScene = false;
- }
- foreach($scenes as $scene)
- {
- foreach($displayScenes as $displayScene)
- {
- if(strpos($scene->path, $displayScene . ',') !== false)
- {
- $displayScenes += explode(',', trim($scene->path, ','));
- }
- }
- }
- $displayScenes = array_unique($displayScenes);
- foreach($scenes as $id => $scene)
- {
- if(!in_array($id, $displayScenes)) unset($scenes[$id]);
- $scene->parent = !empty($scene->parent) ? 'scene-' . $scene->parent : 0;
- $scene->id = 'scene-' . $scene->id;
- $scene->isScene = true;
- }
- return array($runs, $scenes);
- }
- /**
- * 获取一个测试单关联的用例。
- * Get cases associated with a testtask.
- *
- * @param int|array $productID
- * @param string $browseType
- * @param int $queryID
- * @param int $moduleID
- * @param string $sort
- * @param object $pager
- * @param object $task
- * @access public
- * @return array
- */
- public function getTaskCases($productID, $browseType, $queryID, $moduleID, $sort, $pager = null, $task = null)
- {
- if(common::isTutorialMode()) return $this->loadModel('tutorial')->getCases();
- $modules = $moduleID ? $this->loadModel('tree')->getAllChildId($moduleID) : array();
- $browseType = ($browseType == 'bymodule' && $this->session->taskCaseBrowseType && $this->session->taskCaseBrowseType != 'bysearch') ? $this->session->taskCaseBrowseType : $browseType;
- $browseType = strtolower($browseType);
- if($browseType == 'bymodule' || $browseType == 'all') return $this->getRuns($task->id, $modules, $sort, $pager);
- if($browseType == 'bysuite') return $this->getRunsBySuite($task->id, $queryID, $sort, $pager);
- if($browseType == 'assignedtome') return $this->getUserRuns($task->id, $this->session->user->account, $modules, $sort, $pager);
- if($browseType == 'bysearch')
- {
- if($this->session->testtaskQuery == false) $this->session->set('testtaskQuery', ' 1 = 1');
- if($queryID)
- {
- $query = $this->loadModel('search')->getQuery($queryID);
- if($query)
- {
- $this->session->set('testtaskQuery', $query->sql);
- $this->session->set('testtaskForm', $query->form);
- }
- }
- /* 预处理搜索表单生成的查询 SQL。*/
- /* Preprocess the query SQL generated by the search form. */
- $allProduct = "`product` = 'all'";
- $caseQuery = $this->session->testtaskQuery;
- $isQueryProduct = strpos($caseQuery, "`product` = '");
- if(strpos($caseQuery, $allProduct) !== false) $caseQuery = str_replace($allProduct, '1', $caseQuery) . ' AND `product` ' . helper::dbIN($this->app->user->view->products);
- $caseQuery = preg_replace('/`(\w+)`/', 't2.`$1`', $caseQuery);
- $caseQuery = str_replace(array('t2.`assignedTo`', 't2.`lastRunner`', 't2.`lastRunDate`', 't2.`lastRunResult`'), array('t1.`assignedTo`', 't1.`lastRunner`', 't1.`lastRunDate`', 't1.`lastRunResult`'), $caseQuery);
- $orderBy = $this->addPrefixToOrderBy($sort);
- return $this->dao->select("t2.*, t1.*, COALESCE(t3.title, '') AS storyTitle, t2.status AS caseStatus, t2.version AS caseVersion, IF(t4.title IS NULL, t2.title, t4.title) AS title")->from(TABLE_TESTRUN)->alias('t1')
- ->leftJoin(TABLE_CASE)->alias('t2')->on('t1.case = t2.id')
- ->leftJoin(TABLE_STORY)->alias('t3')->on('t2.story = t3.id')
- ->leftJoin(TABLE_CASESPEC)->alias('t4')->on('t1.case = t4.case AND t1.version = t4.version')
- ->where($caseQuery)
- ->andWhere('t1.task')->eq($task->id)
- ->andWhere('t2.deleted')->eq('0')
- ->beginIF($isQueryProduct === false)->andWhere('t2.product')->in($productID)->fi()
- ->beginIF($task->branch)->andWhere('t2.branch')->in("0,{$task->branch}")->fi()
- ->orderBy($orderBy)
- ->page($pager)
- ->fetchAll('id', false);
- }
- return array();
- }
- /**
- * 获取某人负责的测试单的键值对。
- * Get the key-value pair of the testtask someone is responsible for.
- *
- * @param string $account
- * @param int $limit
- * @param string $status all|wait|doing|done|blocked
- * @param array $skipProductIDList
- * @param array $skipExecutionIDList
- * @access public
- * @return array
- */
- public function getUserTestTaskPairs($account, $limit = 0, $status = 'all', $skipProductIDList = array(), $skipExecutionIDList = array())
- {
- $stmt = $this->dao->select('t1.id, t1.name, t2.name AS execution')
- ->from(TABLE_TESTTASK)->alias('t1')
- ->leftJoin(TABLE_EXECUTION)->alias('t2')->on('t1.execution = t2.id')
- ->leftJoin(TABLE_PRODUCT)->alias('t3')->on('t1.product=t3.id')
- ->where('t1.owner')->eq($account)
- ->andWhere('t1.deleted')->eq('0')
- ->andWhere('t3.deleted')->eq('0')
- ->beginIF($status != 'all')->andWhere('t1.status')->in($status)->fi()
- ->beginIF(!empty($skipProductIDList))->andWhere('t1.product')->notin($skipProductIDList)->fi()
- ->beginIF(!empty($skipExecutionIDList))->andWhere('t1.execution')->notin($skipExecutionIDList)->fi()
- ->beginIF($limit)->limit($limit)->fi()
- ->query();
- $testtaskPairs = array();
- while($testtask = $stmt->fetch())
- {
- if($testtask->execution) $testtask->execution .= " / ";
- $testtaskPairs[$testtask->id] = $testtask->execution . $testtask->name;
- }
- return $testtaskPairs;
- }
- /**
- * 获取执行结果信息。
- * Get information of a testrun.
- *
- * @param int $runID
- * @access public
- * @return object|false
- */
- public function getRunById($runID)
- {
- $run = common::isTutorialMode() ? $this->loadModel('tutorial')->getRun() : $this->dao->findById($runID)->from(TABLE_TESTRUN)->fetch();
- if(!$run) return false;
- $run->case = $this->loadModel('testcase')->getById($run->case, $run->version);
- return $run;
- }
- /**
- * 根据用例 ID 分组获取关联的测试单。
- * Get testtasks associated with cases by case ID.
- *
- * @param int|array $caseIDList
- * @access public
- * @return array
- */
- public function getGroupByCases($caseIDList)
- {
- return $this->dao->select('t1.case, t2.*, t3.branch')->from(TABLE_TESTRUN)->alias('t1')
- ->leftJoin(TABLE_TESTTASK)->alias('t2')->on('t1.task=t2.id')
- ->leftJoin(TABLE_BUILD)->alias('t3')->on('t2.build = t3.id')
- ->where('t1.case')->in($caseIDList)
- ->fetchGroup('case', 'id');
- }
- /**
- * 初始化一条记录便于自动化测试完成后回填测试结果。
- * Init a record for filling back the test result after the automated test is completed.
- *
- * @param int $runID
- * @param int $caseID
- * @param int $version
- * @param int $nodeID
- * @access public
- * @return int|false
- */
- public function initResultForAutomatedTest($runID = 0, $caseID = 0, $version = 0, $nodeID = 0)
- {
- $result = new stdClass();
- $result->run = $runID;
- $result->case = $caseID;
- $result->version = $version;
- $result->node = $nodeID;
- $result->date = helper::now();
- $result->lastRunner = $this->app->user->account;
- $this->dao->insert(TABLE_TESTRESULT)->data($result)->autoCheck()->exec();
- if(dao::isError()) return false;
- return $this->dao->lastInsertID();
- }
- /**
- * 保存一个测试用例的执行结果。
- * Save the execution results of a test case.
- *
- * @param int $runID
- * @param int $caseID
- * @param int $version
- * @param array $stepResults
- * @param int $deployID
- * @access public
- * @return bool|string
- */
- public function createResult($runID, $caseID, $version, $stepResults, $deployID = 0)
- {
- /* 根据测试用例步骤的执行结果获取测试用例的执行结果。*/
- /* Get the execution results of the test case based on the execution results of the test case steps. */
- $caseResult = 'pass';
- foreach($stepResults as $stepResult)
- {
- if($stepResult->result == 'n/a' || $stepResult->result == 'pass') continue;
- if(isset($this->lang->testcase->resultList[$stepResult->result])) $caseResult = $stepResult->result;
- if($stepResult->result == 'fail') break;
- }
- /* 把用例步骤执行结果转换为数组类型以便于和历史数据保持一致。*/
- /* Convert the execution results of the test case steps to array type for consistency with historical data. */
- foreach($stepResults as $key => $stepResult) $stepResults[$key] = (array)$stepResult;
- /* 保存测试用例的执行结果。*/
- /* Save the execution results of the test case. */
- $now = helper::now();
- $result = new stdclass();
- $result->run = $runID;
- $result->case = $caseID;
- $result->version = $version;
- $result->caseResult = $caseResult;
- $result->stepResults = serialize($stepResults);
- $result->lastRunner = $this->app->user->account;
- $result->date = $now;
- $result->deploy = $deployID;
- $this->dao->insert(TABLE_TESTRESULT)->data($result)->autoCheck()->exec();
- if(dao::isError()) return false;
- /* 把上传的文件关联到到执行结果的用例步骤中。*/
- /* Associated the uploaded files to the test case steps of the execution results. */
- $resultID = $this->dao->lastInsertID();
- foreach($stepResults as $stepID => $stepResult) $this->loadModel('file')->saveUpload('stepResult', $resultID, $stepID, "files{$stepID}", "labels{$stepID}");
- /* 更新测试用例的执行结果。*/
- /* Update the execution results of the test case. */
- $case = new stdclass();
- $case->lastRunner = $this->app->user->account;
- $case->lastRunDate = $now;
- $case->lastRunResult = $caseResult;
- $this->dao->update(TABLE_CASE)->data($case)->where('id')->eq($caseID)->exec();
- if(dao::isError()) return false;
- if($runID)
- {
- /* 更新测试单中测试用例的执行结果。*/
- /* Update the execution results of the test case in testtask. */
- $run = new stdclass();
- $run->status = $caseResult == 'blocked' ? 'blocked' : 'normal';
- $run->lastRunResult = $caseResult;
- $run->lastRunner = $this->app->user->account;
- $run->lastRunDate = $now;
- $this->dao->update(TABLE_TESTRUN)->data($run)->where('id')->eq($runID)->exec();
- }
- $this->loadModel('score')->create('testtask', 'runCase', $runID);
- return $caseResult;
- }
- /**
- * 批量执行测试用例并记录测试结果。
- * Batch run test cases and record the test results.
- *
- * @param array $cases
- * @param string $runCaseType
- * @param int $taskID
- * @access public
- * @return bool
- */
- public function batchRun($cases, $runCaseType = 'testcase', $taskID = 0)
- {
- $caseIdList = array_filter(array_keys($cases));
- if(empty($caseIdList)) return false;
- $runs = array();
- if($runCaseType == 'testtask')
- {
- /* 如果是从测试单中批量执行测试用例,查询出测试用例和测试执行的键值对便于更新本次执行结果。*/
- /* If batch run test cases from testtask, query the key-value pair of test cases and test execution for updating the execution results. */
- $runs = $this->dao->select('`case`, id')->from(TABLE_TESTRUN)
- ->where('`case`')->in($caseIdList)
- ->beginIF($taskID)->andWhere('task')->eq($taskID)->fi()
- ->fetchPairs();
- }
- $now = helper::now();
- $result = new stdClass();
- $result->lastRunner = $this->app->user->account;
- $result->date = $now;
- $case = new stdClass();
- $case->lastRunner = $this->app->user->account;
- $case->lastRunDate = $now;
- $run = new stdclass();
- $run->lastRunner = $this->app->user->account;
- $run->lastRunDate = $now;
- $this->loadModel('action');
- foreach($cases as $caseID => $postCase)
- {
- $runID = zget($runs, $caseID, 0);
- $postSteps = zget($postCase, 'steps', array());
- $postReals = zget($postCase, 'reals', array());
- $caseResult = $postCase->results ? $postCase->results : 'pass';
- $stepResults = $this->processStepResults($caseIdList, $caseID, $caseResult, $postSteps, $postReals);
- $result->run = $runID;
- $result->case = $caseID;
- $result->version = $postCase->version;
- $result->caseResult = $caseResult;
- $result->stepResults = serialize($stepResults);
- $this->dao->insert(TABLE_TESTRESULT)->data($result)->autoCheck()->exec();
- $case->lastRunResult = $caseResult;
- $this->dao->update(TABLE_CASE)->data($case)->where('id')->eq($caseID)->exec();
- $this->action->create('case', $caseID, 'run', '', $taskID . ',' . $caseResult);
- if(!$runID) continue;
- $run->lastRunResult = $caseResult;
- $run->status = $caseResult == 'blocked' ? 'blocked' : 'normal';
- $this->dao->update(TABLE_TESTRUN)->data($run)->where('id')->eq($runID)->exec();
- if(dao::isError()) return false;
- }
- return true;
- }
- /**
- * 处理测试用例步骤的执行结果。
- * Process the execution results of the test case steps.
- *
- * @param array $caseIdList
- * @param int $caseID
- * @param string $caseResult
- * @param array $postSteps
- * @param array $postReals
- * @access private
- * @return array
- */
- private function processStepResults($caseIdList, $caseID, $caseResult, $postSteps, $postReals)
- {
- static $stepGroups = array();
- if(empty($stepGroups))
- {
- $stepGroups = $this->dao->select('t1.id, t1.case')->from(TABLE_CASESTEP)->alias('t1')
- ->leftJoin(TABLE_CASE)->alias('t2')->on('t1.case = t2.id AND t1.version = t2.version')
- ->where('t1.case')->in($caseIdList)
- ->andWhere('t1.type')->ne('group')
- ->andWhere('t2.status')->ne('wait')
- ->fetchGroup('case', 'id');
- }
- if(empty($stepGroups[$caseID]))
- {
- $results[0]['result'] = $caseResult;
- $results[0]['real'] = $caseResult == 'pass' || empty($postReals) ? '' : $postReals[0];
- return $results;
- }
- $results = array();
- $dbSteps = array_keys($stepGroups[$caseID]);
- foreach($dbSteps as $stepID)
- {
- $results[$stepID]['result'] = $caseResult == 'pass' ? $caseResult : $postSteps[$stepID];
- $results[$stepID]['real'] = $caseResult == 'pass' ? '' : $postReals[$stepID] ?? '';
- }
- return $results;
- }
- /**
- * 通过执行 ID 或用例 ID 获取用例的执行结果。
- * Get results by runID or caseID
- *
- * @param int $runID
- * @param int $caseID
- * @param string $status all|done
- * @param string $type all|fail
- * @param int $deployID
- * @access public
- * @return void
- */
- public function getResults($runID, $caseID = 0, $status = 'all', $type = 'all', $deployID = 0)
- {
- if(common::isTutorialMode()) return $this->loadModel('tutorial')->getResults();
- $results = $this->dao->select('*,`stepResults`,`ZTFResult`')->from(TABLE_TESTRESULT)
- ->beginIF($runID > 0)->where('run')->eq($runID)->fi()
- ->beginIF($runID <= 0)->where('`case`')->eq($caseID)->fi()
- ->beginIF($status == 'done')->andWhere('caseResult')->ne('')->fi()
- ->beginIF($type != 'all')->andWhere('caseResult')->eq($type)->fi()
- ->beginIF($deployID)->andWhere('deploy')->eq($deployID)->fi()
- ->orderBy('id desc')
- ->fetchAll('id', false);
- if(!$results) return array();
- list($resultFiles, $stepFiles) = $this->getResultsFiles(array_keys($results));
- $runIdList = $nodeIdList = $relatedVersions = array();
- foreach($results as $result)
- {
- $runIdList[$result->run] = $result->run;
- $nodeIdList[$result->node] = $result->node;
- $relatedVersions[$result->version] = $result->version;
- $runCaseID = $result->case;
- }
- $relatedSteps = $this->dao->select('*')->from(TABLE_CASESTEP)
- ->where('`case`')->eq($runCaseID)
- ->andWhere('version')->in($relatedVersions)
- ->orderBy('id')
- ->fetchGroup('version', 'id');
- $runs = $this->dao->select('t1.id, t1.task, t2.build')->from(TABLE_TESTRUN)->alias('t1')
- ->leftJoin(TABLE_TESTTASK)->alias('t2')->on('t1.task=t2.id')
- ->where('t1.id')->in($runIdList)
- ->fetchAll('id');
- $nodes = $this->dao->select('id,name')->from(TABLE_ZAHOST)->where('id')->in($nodeIdList)->fetchPairs();
- foreach($results as $resultID => $result)
- {
- $result->stepResults = $result->stepResults ? unserialize($result->stepResults) : array();
- $result->build = !empty($runs[$result->run]->build) ? $runs[$result->run]->build : 0;
- $result->task = !empty($runs[$result->run]->task) ? $runs[$result->run]->task : 0;
- $result->nodeName = zget($nodes, $result->node, '');
- $result->files = zget($resultFiles, $resultID, array()); //Get files of case result.
- if(isset($relatedSteps[$result->version])) $result->stepResults = $this->processResultSteps($result, $relatedSteps[$result->version]);
- if(!empty($result->ZTFResult)) $result->ZTFResult = $this->formatZtfLog($result->ZTFResult, $result->stepResults);
- /* Get files of step result. */
- if(!empty($result->stepResults))
- {
- foreach(array_keys($result->stepResults) as $stepID) $result->stepResults[$stepID]['files'] = isset($stepFiles[$resultID][$stepID]) ? $stepFiles[$resultID][$stepID] : array();
- }
- }
- return $results;
- }
- /**
- * 计算用例执行结果的步骤。
- * Process steps of result.
- *
- * @param object $result
- * @param array $relatedStep
- * @access public
- * @return array
- */
- public function processResultSteps($result, $relatedStep)
- {
- $this->loadModel('testcase');
- $preGrade = 1;
- $parentSteps = array();
- $key = array(0, 0, 0);
- foreach($relatedStep as $stepID => $step)
- {
- $parentSteps[$step->id] = $step->parent;
- $grade = 1;
- if(isset($parentSteps[$step->parent]))
- {
- $grade = isset($parentSteps[$parentSteps[$step->parent]]) ? 3 : 2;
- }
- if($grade > $preGrade)
- {
- $key[$grade - 1] = 1;
- }
- else
- {
- if($grade < $preGrade)
- {
- if($grade < 2) $key[1] = 0;
- if($grade < 3) $key[2] = 0;
- }
- $key[$grade - 1] ++;
- }
- $name = implode('.', $key);
- $name = str_replace('.0', '', $name);
- $relatedStep[$stepID] = (array)$step;
- $relatedStep[$stepID]['name'] = $name;
- $relatedStep[$stepID]['grade'] = $grade;
- $relatedStep[$stepID]['desc'] = html_entity_decode($relatedStep[$stepID]['desc']);
- $relatedStep[$stepID]['expect'] = html_entity_decode($relatedStep[$stepID]['expect']);
- if(isset($result->stepResults[$stepID]))
- {
- $relatedStep[$stepID]['result'] = $result->stepResults[$stepID]['result'];
- $relatedStep[$stepID]['real'] = $result->stepResults[$stepID]['real'];
- }
- $preGrade = $grade;
- }
- return $relatedStep;
- }
- /**
- * 获取结果的文件。
- * Get files of the results.
- *
- * @param array $resultIdList
- * @access public
- * @return array
- */
- public function getResultsFiles($resultIdList)
- {
- $resultFiles = array();
- $stepFiles = array();
- $files = $this->dao->select('*')->from(TABLE_FILE)
- ->where('objectType')->in('caseResult, stepResult')
- ->andWhere('objectID')->in($resultIdList)
- ->andWhere('extra')->ne('editor')
- ->orderBy('id')
- ->fetchAll('id', false);
- $this->loadModel('file');
- foreach($files as $file)
- {
- $this->file->setFileWebAndRealPaths($file);
- if($file->objectType == 'caseResult')
- {
- $resultFiles[$file->objectID][$file->id] = $file;
- }
- elseif($file->objectType == 'stepResult')
- {
- $stepFiles[$file->objectID][(int)$file->extra][$file->id] = $file;
- }
- }
- return array($resultFiles, $stepFiles);
- }
- /**
- * 格式化 ztf 的执行日志。
- * Format the execution log of ztf.
- *
- * @param string $result
- * @param array $stepResults
- * @access public
- * @return string
- */
- public function formatZtfLog($result, $stepResults)
- {
- $logObj = json_decode($result);
- $logs = empty($logObj->log) ? '' : $logObj->log;
- if(empty($logs)) return '';
- $logs = str_replace(array("\r", "\n", "\r\n"), "\n", $logs);
- $logList = explode("\n", $logs);
- $logHtml = '';
- foreach($logList as $log)
- {
- $log = preg_replace("/^[\d\-:.\x20]+/", '', $log);
- $log = trim($log);
- if(empty($log)) continue;
- $failHtml = ": <span class='result-testcase fail'>{$this->lang->testtask->fail}</span>";
- $passHtml = ": <span class='result-testcase pass'>{$this->lang->testtask->pass}</span>";
- $log = preg_replace(array("/:\x20失败/", "/:\x20fail/", "/:\x20成功/", "/:\x20pass/"), array($failHtml, $failHtml, $passHtml, $passHtml), $log);
- $logHtml .= "<li>{$log}</li>";
- }
- if(!empty($stepResults))
- {
- $total = count($stepResults);
- $failCount = $passCount = 0;
- foreach($stepResults as $step)
- {
- if(!isset($step['result'])) continue;
- if($step['result'] == 'pass')
- {
- $passCount++;
- }
- elseif($step['result'] == 'fail')
- {
- $failCount++;
- }
- }
- $caseResult = $passCount ? 'pass':'fail';
- $logHtml .= "<li class='result-testcase {$caseResult}'>" . sprintf($this->lang->testtask->stepSummary, $total, $passCount, $failCount) . '</li>';
- }
- return $logHtml;
- }
- /**
- * 判断一个动作是否可以执行。
- * Determine whether an action can be performed.
- *
- * @param object $testtask
- * @param string $action
- * @access public
- * @return bool
- */
- public static function isClickable($testtask, $action)
- {
- $action = strtolower($action);
- if(isset($testtask->status) && empty($testtask->rawStatus)) $testtask->rawStatus = $testtask->status;
- if($action == 'start') return $testtask->rawStatus == 'wait';
- if($action == 'block') return ($testtask->rawStatus == 'doing' || $testtask->rawStatus == 'wait');
- if($action == 'activate') return ($testtask->rawStatus == 'blocked' || $testtask->rawStatus == 'done');
- if($action == 'close') return $testtask->rawStatus != 'done';
- if($action == 'ztfrun') return $testtask->auto == 'auto';
- if($action == 'runcase') return (empty($testtask->lib) || !empty($testtask->product)) && isset($testtask->rawStatus) && $testtask->rawStatus != 'wait';
- return true;
- }
- /**
- * 获取一个测试单的收件人和抄送人。
- * Get the recipient and cc of a testtask.
- *
- * @param object $testtask
- * @access public
- * @return bool|array
- */
- public function getToAndCcList($testtask)
- {
- /* Set toList and ccList. */
- $toList = zget($testtask, 'owner', '') . ',' . zget($testtask, 'members', '') . ',';
- $toList = trim($toList, ',');
- $ccList = str_replace(' ', '', trim(zget($testtask, 'mailto', ''), ','));
- if(empty($toList))
- {
- if(empty($ccList)) return false;
- if(strpos($ccList, ',') === false)
- {
- $toList = $ccList;
- $ccList = '';
- }
- else
- {
- $commaPos = strpos($ccList, ',');
- $toList = substr($ccList, 0, $commaPos);
- $ccList = substr($ccList, $commaPos + 1);
- }
- }
- return array($toList, $ccList);
- }
- /**
- * 导入单元测试结果。
- * Import unit test results.
- *
- * @param object $task
- * @access public
- * @return bool|int
- */
- public function importUnitResult($task)
- {
- /* 获取上传的单元测试文件。*/
- /* Get uploaded unit test file. */
- $files = $this->loadModel('file')->getUpload('resultFile');
- if(empty($files[0]))
- {
- dao::$errors['resultFile'][] = $this->lang->testtask->unitXMLFormat;
- return false;
- }
- /* 使用 simplexml 读取文件。*/
- /* Use simplexml to read file. */
- $file = $files[0];
- $filePath = $this->file->savePath . $this->file->getSaveName($file['pathname']);
- move_uploaded_file($file['tmpname'], $filePath);
- $parsedXML = simplexml_load_file($filePath, null, LIBXML_NOERROR);
- if($parsedXML === false)
- {
- dao::$errors['resultFile'][] = $this->lang->testtask->cannotBeParsed;
- return false;
- }
- /* 解析 xml 文件。*/
- /* Parse xml file. */
- $data = $this->parseXMLResult($parsedXML, $task->product, $task->frame);
- if(empty($data['cases']) && $task->frame == 'cppunit') $data = $this->parseCppXMLResult($parsedXML, $task->product, $task->frame);
- if(empty($data['cases']))
- {
- dao::$errors['resultFile'][] = $this->lang->testtask->noImportData;
- return false;
- }
- /* 创建测试单。*/
- /* Create test task. */
- unset($task->frame);
- $taskID = $this->create($task);
- if(dao::isError()) return false;
- unlink($filePath);
- unset($_SESSION['resultFile']);
- /* 导入单元测试结果文件中包含的数据。*/
- /* Import data in the unit test results. */
- $this->importDataOfUnitResult($taskID, $task->product, $data['suites'], $data['cases'], $data['results'], $data['suiteNames'], $data['caseTitles'], 'unit');
- return $taskID;
- }
- /**
- * 导入单元测试结果文件中包含的数据。
- * Import data in the unit test results.
- *
- * @param int $taskID
- * @param int $productID
- * @param array $suites
- * @param array $cases
- * @param array $results
- * @param array $suiteNames
- * @param array $caseTitles
- * @param string $auto unit|func
- * @access public
- * @return bool
- */
- public function importDataOfUnitResult($taskID, $productID, $suites, $cases, $results, $suiteNames, $caseTitles, $auto = 'unit')
- {
- $this->loadModel('action');
- /* 初始化对象便于在循环中使用。*/
- /* Initialize the object for use in loops. */
- $suiteCase = new stdclass();
- $testRun = new stdclass();
- $testRun->task = $taskID;
- $testRun->status = 'normal';
- $existSuites = $suiteNames ? $this->getExistSuitesOfUnitResult($suiteNames, $productID, $auto) : array();
- foreach($suites as $suiteIndex => $suite)
- {
- $suiteID = 0;
- if($suite) $suiteID = isset($existSuites[$suite->name]) ? $existSuites[$suite->name] : $this->importSuiteOfUnitResult($suite);
- $suiteCase->suite = $suiteID;
- $importCases = zget($cases, $suiteIndex, array());
- $existCases = !empty($caseTitles[$suiteIndex]) ? $this->getExistCasesOfUnitResult($caseTitles[$suiteIndex], $suiteID, $productID, $auto) : array();
- foreach($importCases as $key => $case)
- {
- $caseID = $this->importCaseOfUnitResult($case, $existCases);
- $runID = $this->importRunOfUnitResult($case, $caseID, $testRun);
- if($suiteID) $this->linkImportedCaseToSuite($case, $caseID, $suiteCase);
- $testresult = $results[$suiteIndex][$key];
- $testresult->run = $runID;
- $testresult->case = $caseID;
- $testresult->version = isset($case->version) ? $case->version : 1;
- $this->dao->insert(TABLE_TESTRESULT)->data($testresult)->exec();
- }
- }
- return !dao::isError();
- }
- /**
- * 导入单元测试结果文件中的测试套件。
- * Import test suite in the unit test results.
- *
- * @param object $suite
- * @access private
- * @return int
- */
- private function importSuiteOfUnitResult($suite)
- {
- $this->dao->insert(TABLE_TESTSUITE)->data($suite)->exec();
- $suiteID = $this->dao->lastInsertID();
- $this->action->create('testsuite', $suiteID, 'opened');
- return $suiteID;
- }
- /**
- * 导入单元测试结果文件中的测试用例。
- * Import test case in the unit test results.
- *
- * @param object $case
- * @param array $existCases
- * @access private
- * @return int
- */
- private function importCaseOfUnitResult(&$case, $existCases)
- {
- if(!empty($case->id))
- {
- unset($case->steps);
- $caseID = $case->id;
- $oldCase = $this->dao->select('*')->from(TABLE_CASE)->where('id')->eq($caseID)->fetch();
- $case->version = $oldCase->version;
- $case->openedDate = $oldCase->openedDate;
- $changes = common::createChanges($oldCase, $case);
- if($changes)
- {
- $this->dao->update(TABLE_CASE)->data($case)->where('id')->eq($caseID)->exec();
- $actionID = $this->action->create('case', $caseID, 'Edited');
- $this->action->logHistory($actionID, $changes);
- }
- return $caseID;
- }
- if(!isset($existCases[$case->title]))
- {
- $this->dao->insert(TABLE_CASE)->data($case, 'steps')->exec();
- $caseID = $this->dao->lastInsertID();
- $steps = zget($case, 'steps', array());
- foreach($steps as $step)
- {
- $step->case = $caseID;
- $step->version = 1;
- $this->dao->insert(TABLE_CASESTEP)->data($step)->exec();
- }
- $this->action->create('case', $caseID, 'Opened');
- return $caseID;
- }
- return $existCases[$case->title];
- }
- /**
- * 导入单元测试结果文件中的测试结果。
- * Import test result in the unit test results.
- *
- * @param object $case
- * @param int $caseID
- * @param object $testRun
- * @access private
- * @return int
- */
- private function importRunOfUnitResult($case, $caseID, $testRun)
- {
- $testRun->case = $caseID;
- $testRun->version = $case->version;
- $testRun->lastRunner = $case->lastRunner;
- $testRun->lastRunDate = $case->lastRunDate;
- $testRun->lastRunResult = $case->lastRunResult;
- $testRunInfo = $this->dao->select('id')->from(TABLE_TESTRUN)->where('case')->eq($caseID)->andWhere('task')->eq($testRun->task)->fetch();
- if(isset($testRunInfo->id))
- {
- $this->dao->update(TABLE_TESTRUN)->data($testRun)->where('id')->eq($testRunInfo->id)->exec();
- return $testRunInfo->id;
- }
- $this->dao->insert(TABLE_TESTRUN)->data($testRun)->exec();
- return dao::isError() ? 0 : $this->dao->lastInsertID();
- }
- /**
- * 将导入的测试用例关联到测试套件。
- * Link imported case to suite.
- *
- * @param object $case
- * @param int $caseID
- * @param object $suiteCase
- * @access private
- * @return bool
- */
- private function linkImportedCaseToSuite($case, $caseID, $suiteCase)
- {
- $suiteCase->case = $caseID;
- $suiteCase->version = $case->version;
- $suiteCase->product = $case->product;
- $this->dao->replace(TABLE_SUITECASE)->data($suiteCase)->exec();
- return !dao::isError();
- }
- /**
- * 获取单元测试结果文件中已存在的测试套件。
- * Get exist suites in the unit test results.
- *
- * @param array $names
- * @param int $productID
- * @param string $auto
- * @access public
- * @return array
- */
- private function getExistSuitesOfUnitResult($names, $productID, $auto)
- {
- if(!$names) return array();
- return $this->dao->select('name, id')->from(TABLE_TESTSUITE)
- ->where('name')->in($names)
- ->andWhere('product')->eq($productID)
- ->andWhere('type')->eq($auto)
- ->andWhere('deleted')->eq('0')
- ->fetchPairs();
- }
- /**
- * 获取单元测试结果文件中已存在的测试用例。
- * Get exist cases in the unit test results.
- *
- * @param array $titles
- * @param int $suiteID
- * @param int $productID
- * @param string $auto
- * @access public
- * @return array
- */
- private function getExistCasesOfUnitResult($titles, $suiteID, $productID, $auto)
- {
- if(!$titles) return array();
- $this->dao->select('t1.title, t1.id')->from(TABLE_CASE)->alias('t1');
- if($suiteID) $this->dao->leftJoin(TABLE_SUITECASE)->alias('t2')->on('t1.id=t2.case');
- return $this->dao->where('t1.title')->in($titles)
- ->andWhere('t1.product')->eq($productID)
- ->beginIF($auto == 'unit')->andWhere('t1.auto')->eq($auto)->fi()
- ->andWhere('t1.deleted')->eq('0')
- ->orderBy('t1.id')
- ->fetchPairs();
- }
- /**
- * 解析 xml 文件中的 cppunit 的单元测试结果。
- * Parse unit test result from cppunit xml.
- *
- * @param object $parsedXML
- * @param int $productID
- * @param string $frame
- * @access public
- * @return array
- */
- public function parseCppXMLResult($parsedXML, $productID, $frame)
- {
- /* Get testcase node. */
- $failNodes = $parsedXML->xpath('FailedTests/FailedTest');
- $passNodes = $parsedXML->xpath('SuccessfulTests/Test');
- $matchNodes = array_merge($failNodes, $passNodes);
- if(count($matchNodes) == 0) return array('suites' => array(), 'cases' => array(), 'results' => array(), 'suiteNames' => array(), 'caseTitles' => array());
- /* Get cases and results by parsed node. */
- $now = helper::now();
- $cases = array();
- $results = array();
- $caseTitles = array();
- $suiteIndex = 0;
- $suites = array($suiteIndex => '');
- foreach($matchNodes as $caseIndex => $matchNode)
- {
- $result = $this->initResult($now);
- $result->duration = 0;
- $result->xml = $matchNode->asXML();
- if(isset($matchNode->Message))
- {
- $result->caseResult = 'fail';
- $result->stepResults[0]['result'] = 'fail';
- $result->stepResults[0]['real'] = (string)$matchNode->Message;
- }
- $result->stepResults = serialize($result->stepResults);
- $case = $this->initCase($productID, (string)$matchNode->name, $now, 'unit', $frame ?: 'junit');
- $case->lastRunResult = $result->caseResult;
- $caseTitles[$suiteIndex][] = $case->title;
- $cases[$suiteIndex][$caseIndex] = $case;
- $results[$suiteIndex][$caseIndex] = $result;
- }
- return array('suites' => $suites, 'cases' => $cases, 'results' => $results, 'suiteNames' => array(), 'caseTitles' => $caseTitles);
- }
- /**
- * 解析 xml 文件中的单元测试结果。
- * Parse unit test result from xml.
- *
- * @param object $parsedXML
- * @param int $productID
- * @param string $frame
- * @access public
- * @return array
- */
- public function parseXMLResult($parsedXML, $productID, $frame)
- {
- /* Parse result xml. */
- $rules = zget($this->config->testtask->unitResultRules, $frame, $this->config->testtask->unitResultRules->common);
- /* Get testcase node. */
- $matchPaths = $rules['path'];
- $nameFields = $rules['name'];
- $failure = $rules['failure'];
- $skipped = $rules['skipped'];
- $suiteField = $rules['suite'];
- $aliasSuite = zget($rules, 'aliasSuite', array());
- $aliasName = zget($rules, 'aliasName', array());
- $matchNodes = array();
- foreach($matchPaths as $matchPath)
- {
- $matchNodes = $parsedXML->xpath($matchPath);
- if(count($matchNodes) != 0) break;
- }
- if(count($matchNodes) == 0) return array('suites' => array(), 'cases' => array(), 'results' => array(), 'suiteNames' => array(), 'caseTitles' => array());
- $parentPath = '';
- $caseNode = $matchPath;
- $parentNodes = array($parsedXML);
- if(strpos($matchPath, '/') !== false)
- {
- $explodedPath = explode('/', $matchPath);
- $caseNode = array_pop($explodedPath);
- $parentPath = implode('/', $explodedPath);
- $parentNodes = $parsedXML->xpath($parentPath);
- }
- /* Get cases and results by parsed node. */
- $now = helper::now();
- $cases = array();
- $results = array();
- $suites = array();
- $caseTitles = array();
- $suiteNames = array();
- foreach($parentNodes as $suiteIndex => $parentNode)
- {
- $caseNodes = $parentNode->xpath($caseNode);
- $attributes = $parentNode->attributes();
- $suite = '';
- if(isset($attributes[$suiteField]))
- {
- $suite = $this->initSuite($productID, (string)$attributes[$suiteField], $now);
- $suiteNames[] = $suite->name;
- }
- else
- {
- $attributes = $caseNodes[0]->attributes();
- foreach($aliasSuite as $alias)
- {
- if(isset($attributes[$alias]))
- {
- $suite = $this->initSuite($productID, (string)$attributes[$alias], $now);
- $suiteNames[] = $suite->name;
- break;
- }
- }
- }
- $suites[$suiteIndex] = $suite;
- foreach($caseNodes as $caseIndex => $matchNode)
- {
- $case = $this->initCase($productID, '', $now, 'unit', $frame ?: 'junit');
- $attributes = $matchNode->attributes();
- foreach($nameFields as $field)
- {
- if(!isset($attributes[$field])) continue;
- $case->title .= (string)$attributes[$field] . ' ';
- }
- $case->title = trim($case->title);
- if(empty($case->title))
- {
- foreach($aliasName as $field)
- {
- if(!isset($attributes[$field])) continue;
- $case->title .= (string)$attributes[$field] . ' ';
- }
- $case->title = trim($case->title);
- }
- if(empty($case->title)) continue;
- $result = $this->initResult($now);
- $result->duration = isset($attributes['time']) ? (float)$attributes['time'] : 0;
- $result->xml = $matchNode->asXML();
- $result = $this->processResult($result, $matchNode, $failure, $skipped);
- $result->stepResults = serialize($result->stepResults);
- $case->lastRunResult = $result->caseResult;
- $caseTitles[$suiteIndex][] = $case->title;
- $cases[$suiteIndex][$caseIndex] = $case;
- $results[$suiteIndex][$caseIndex] = $result;
- }
- }
- return array('suites' => $suites, 'cases' => $cases, 'results' => $results, 'suiteNames' => $suiteNames, 'caseTitles' => $caseTitles);
- }
- /**
- * 解析 ztf 的单元测试结果。
- * Parse unit test result of ztf.
- *
- * @param array $caseResults
- * @param string $frame
- * @param int $productID
- * @param int $jobID
- * @param int $compileID
- * @access public
- * @return array
- */
- public function parseZTFUnitResult($caseResults, $frame, $productID, $jobID, $compileID)
- {
- $now = helper::now();
- $cases = array();
- $results = array();
- $suites = array();
- $caseTitles = array();
- $suiteNames = array();
- $suiteIndex = 0;
- foreach($caseResults as $caseIndex => $caseResult)
- {
- $suite = '';
- if(isset($caseResult->testSuite) and !isset($suiteNames[$caseResult->testSuite]))
- {
- $suite = $this->initSuite($productID, $caseResult->testSuite, $now);
- $suiteNames[$suite->name] = $suite->name;
- $suiteIndex ++;
- }
- if(!isset($suites[$suiteIndex])) $suites[$suiteIndex] = $suite;
- $result = $this->initResult($now);
- $result->job = $jobID;
- $result->compile = $compileID;
- $result->duration = zget($caseResult, 'duration', 0);
- if(!empty($caseResult->failure))
- {
- $result->caseResult = 'fail';
- $result->stepResults[0]['result'] = 'fail';
- $result->stepResults[0]['real'] = zget($caseResult->failure, 'desc', '');
- }
- $result->stepResults = serialize($result->stepResults);
- $case = $this->initCase($productID, '', $now, '', $frame);
- $case->lastRunResult = $result->caseResult;
- if(!empty($caseResult->id)) $case->id = $caseResult->id;
- if(empty($caseResult->id)) $case->title = $caseResult->title;
- if(empty($caseResult->id)) $case->auto = 'unit';
- $caseTitles[$suiteIndex][] = $case->title;
- $cases[$suiteIndex][$caseIndex] = $case;
- $results[$suiteIndex][$caseIndex] = $result;
- }
- return array('suites' => $suites, 'cases' => $cases, 'results' => $results, 'suiteNames' => $suiteNames, 'caseTitles' => $caseTitles);
- }
- /**
- * 解析 ztf 的功能测试结果。
- * Parse function test result of ztf.
- *
- * @param array $caseResults
- * @param string $frame
- * @param int $productID
- * @param int $jobID
- * @param int $compileID
- * @access public
- * @return array
- */
- public function parseZTFFuncResult($caseResults, $frame, $productID, $jobID, $compileID)
- {
- $now = helper::now();
- $cases = array();
- $results = array();
- $suites = array();
- $caseTitles = array();
- $suiteNames = array();
- $suiteIndex = 0;
- foreach($caseResults as $caseIndex => $caseResult)
- {
- $suite = '';
- if(!isset($suites[$suiteIndex])) $suites[$suiteIndex] = $suite;
- $steps = array();
- $result = $this->initResult($now);
- $result->job = $jobID;
- $result->compile = $compileID;
- if(!empty($caseResult->steps))
- {
- $result->stepResults = array();
- $stepStatus = 'pass';
- foreach($caseResult->steps as $i => $step)
- {
- if(!$step->status) $step->status = 'fail';
- $result->stepResults[$i]['result'] = $step->status;
- $result->stepResults[$i]['real'] = $step->checkPoints[0]->actual;
- if($step->status == 'fail') $stepStatus = 'fail';
- $caseStep = new stdclass();
- $caseStep->type = 'step';
- $caseStep->desc = $step->name;
- $caseStep->expect = $step->checkPoints[0]->expect;
- $steps[] = $caseStep;
- }
- $result->caseResult = $stepStatus;
- }
- $result->stepResults = serialize($result->stepResults);
- $case = $this->initCase(0, $caseResult->title, $now, 'func', $frame, 'feature', 'feature');
- $case->steps = $steps;
- $case->lastRunResult = $result->caseResult;
- if(isset($caseResult->id)) $case->id = $caseResult->id;
- $case->product = $productID;
- $caseTitles[$suiteIndex][] = $case->title;
- $cases[$suiteIndex][$caseIndex] = $case;
- $results[$suiteIndex][$caseIndex] = $result;
- }
- return array('suites' => $suites, 'cases' => $cases, 'results' => $results, 'suiteNames' => $suiteNames, 'caseTitles' => $caseTitles);
- }
- /**
- * 初始化测试套件。
- * Initialize the test suite.
- *
- * @param int $product
- * @param string $name
- * @param string $now
- * @access private
- * @return object
- */
- private function initSuite($product, $name, $now)
- {
- $suite = new stdclass();
- $suite->product = $product;
- $suite->name = $name;
- $suite->type = 'unit';
- $suite->addedBy = $this->app->user->account;
- $suite->addedDate = $now;
- return $suite;
- }
- /**
- * 初始化测试用例。
- * Initialize the test case.
- *
- * @param int $product
- * @param string $title
- * @param string $now
- * @param string $auto
- * @param string $frame
- * @param string $type
- * @param string $stage
- * @access private
- * @return object
- */
- private function initCase($product, $title, $now, $auto, $frame, $type = 'unit', $stage = 'unittest')
- {
- $case = new stdclass();
- $case->product = $product;
- $case->title = $title;
- $case->pri = 3;
- $case->type = $type;
- $case->stage = $stage;
- $case->status = 'normal';
- $case->openedBy = $this->app->user->account;
- $case->openedDate = $now;
- $case->version = 1;
- $case->auto = $auto;
- $case->frame = $frame;
- $case->lastRunner = $this->app->user->account;
- $case->lastRunDate = $now;
- return $case;
- }
- /**
- * 初始化测试用例执行结果。
- * Initialize the execution result of the test case.
- *
- * @param string $now
- * @access private
- * @return object
- */
- private function initResult($now)
- {
- $result = new stdclass();
- $result->case = 0;
- $result->version = 1;
- $result->caseResult = 'pass';
- $result->lastRunner = $this->app->user->account;
- $result->date = $now;
- $result->stepResults[0]['result'] = 'pass';
- $result->stepResults[0]['real'] = '';
- return $result;
- }
- /**
- * 根据导入的 xml 文件内容处理测试用例的执行结果。
- * Process the execution result of the test case according to the imported xml file content.
- *
- * @param object $result
- * @param object $matchNode
- * @param string $failure
- * @param string $skipped
- * @access private
- * @return object
- */
- private function processResult($result, $matchNode, $failure, $skipped)
- {
- if(isset($matchNode->$failure))
- {
- $result->caseResult = 'fail';
- $result->stepResults[0]['result'] = 'fail';
- if(is_string($matchNode->$failure))
- {
- $result->stepResults[0]['real'] = (string)$matchNode->$failure;
- return $result;
- }
- if(isset($matchNode->{$failure}[0]))
- {
- $result->stepResults[0]['real'] = (string)$matchNode->{$failure}[0];
- return $result;
- }
- $failureAttrs = $matchNode->$failure->attributes();
- $result->stepResults[0]['real'] = (string)$failureAttrs['message'];
- return $result;
- }
- if(isset($matchNode->$skipped))
- {
- $result->caseResult = 'n/a';
- $result->stepResults[0]['result'] = 'n/a';
- $result->stepResults[0]['real'] = '';
- return $result;
- }
- return $result;
- }
- /**
- * 根据版本查询测试用例。
- * Query test cases by version.
- *
- * @param int $buildID
- * @access public
- * @return object|false
- */
- public function getByBuild($buildID)
- {
- return $this->dao->select('*')->from(TABLE_TESTTASK)
- ->where('build')->eq($buildID)
- ->andWhere('deleted')->eq('0')
- ->fetch();
- }
- /**
- * 获取测试单的所属分支信息。
- *
- * @param object $task
- * @param int $productID
- * @access public
- * @return array
- */
- public function getBranchesByTask($task, $productID = 0)
- {
- $this->app->loadLang('branch');
- $branchPairs = array();
- if(!empty($task->joint))
- {
- $branches = $this->dao->select('t2.id,t2.branch')->from(TABLE_TESTTASKPRODUCT)->alias('t1')
- ->leftJoin(TABLE_BUILD)->alias('t2')->on('t1.build=t2.id')
- ->where('t1.task')->eq($task->id)
- ->beginIF(!empty($productID))->andWhere('t1.product')->eq($productID)->fi()
- ->fetchPairs();
- }
- else
- {
- $branches = $this->dao->select('id,branch')->from(TABLE_BUILD)->where('id')->eq($task->build)->fetchPairs();
- }
- if(empty($branches)) return array(BRANCH_MAIN => $this->lang->branch->main);
- $branchIdList = '';
- foreach($branches as $branch) $branchIdList .= $branch . ',';
- $branchIdList = trim($branchIdList, ',');
- if(empty($branchIdList)) return array(BRANCH_MAIN => $this->lang->branch->main);
- $branchPairs = $this->dao->select('id,name')->from(TABLE_BRANCH)->where('id')->in($branchIdList)->fetchPairs();
- return arrayUnion(array(BRANCH_MAIN => $this->lang->branch->main), $branchPairs);
- }
- }
|