control.php 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106
  1. <?php
  2. /**
  3. * The control file of case currentModule of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Chunsheng Wang <chunsheng@cnezsoft.com>
  8. * @package case
  9. * @version $Id: control.php 5112 2013-07-12 02:51:33Z chencongzhi520@gmail.com $
  10. * @link https://www.zentao.net
  11. */
  12. class testcase extends control
  13. {
  14. /**
  15. * All products.
  16. *
  17. * @var array
  18. * @access public
  19. */
  20. public $products = array();
  21. /**
  22. * Project id.
  23. *
  24. * @var int
  25. * @access public
  26. */
  27. public $projectID = 0;
  28. /**
  29. * Construct function, load product, tree, user auto.
  30. *
  31. * @access public
  32. * @return void
  33. */
  34. public function __construct($moduleName = '', $methodName = '')
  35. {
  36. parent::__construct($moduleName, $methodName);
  37. $this->loadModel('product');
  38. $this->loadModel('tree');
  39. $this->loadModel('user');
  40. $this->loadModel('qa');
  41. /* Get product data. */
  42. $products = array();
  43. $objectID = 0;
  44. $tab = ($this->app->tab == 'project' or $this->app->tab == 'execution') ? $this->app->tab : 'qa';
  45. if(!isInModal() && $this->app->methodName != 'delete')
  46. {
  47. if($this->app->tab == 'project')
  48. {
  49. $objectID = $this->session->project;
  50. $products = $this->product->getProducts((int)$objectID, 'all', '', false);
  51. }
  52. elseif($this->app->tab == 'execution')
  53. {
  54. $objectID = $this->session->execution;
  55. $products = $this->product->getProducts((int)$objectID, 'all', '', false);
  56. }
  57. else
  58. {
  59. $mode = ($this->app->methodName == 'create' and empty($this->config->CRProduct)) ? 'noclosed' : '';
  60. $products = $this->product->getPairs($mode, 0, '', 'all');
  61. }
  62. }
  63. else
  64. {
  65. $products = $this->product->getPairs('', 0, '', 'all');
  66. }
  67. $this->view->products = $this->products = $products;
  68. }
  69. /**
  70. * Browse cases.
  71. *
  72. * @param int $productID
  73. * @param string $branch
  74. * @param string $browseType
  75. * @param int $param
  76. * @param string $caseType
  77. * @param string $orderBy
  78. * @param int $recTotal
  79. * @param int $recPerPage
  80. * @param int $pageID
  81. * @param int $projectID
  82. * @param string $from
  83. * @param int $blockID
  84. * @access public
  85. * @return void
  86. */
  87. public function browse($productID = 0, $branch = '', $browseType = 'all', $param = 0, $caseType = '', $orderBy = 'sort_asc,id_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1, $projectID = 0, $from = 'testcase', $blockID = 0)
  88. {
  89. $this->testcaseZen->checkProducts(); // 如果不存在产品,则跳转到产品创建页面。
  90. if($from == 'doc' || $from == 'ai')
  91. {
  92. $this->app->loadLang('doc');
  93. $realProducts = $this->product->getPairs('nodeleted', 0, '', 'all');
  94. if(empty($realProducts)) return $this->send(array('result' => 'fail', 'message' => $this->lang->doc->tips->noProduct));
  95. }
  96. /* 把访问的产品ID等状态信息保存到session和cookie中。*/
  97. /* Save the product id user last visited to session and cookie. */
  98. $productID = ($this->app->tab != 'project' || $from == 'doc' || $from == 'ai') ? $this->product->checkAccess($productID, $this->products) : $productID;
  99. $branch = $this->testcaseZen->getBrowseBranch($branch);
  100. $browseType = strtolower($browseType);
  101. $moduleID = $browseType == 'bymodule' ? $param : 0;
  102. $suiteID = $browseType == 'bysuite' ? $param : ($browseType == 'bymodule' ? ($this->cookie->caseSuite ? (int)$this->cookie->caseSuite : 0) : 0);
  103. $queryID = $browseType == 'bysearch' ? $param : 0;
  104. $this->testcaseZen->setBrowseCookie($productID, $branch, $browseType, (string)$param);
  105. $moduleID = $browseType == 'bymodule' ? $param : (int)$this->cookie->caseModule;
  106. $suiteID = $browseType == 'bysuite' ? $param : ($browseType == 'bymodule' ? ($this->cookie->caseSuite ? (int)$this->cookie->caseSuite : 0) : 0);
  107. $queryID = $browseType == 'bysearch' ? $param : 0;
  108. if($browseType == 'bymodule' && $this->session->caseBrowseType) $browseType = $this->session->caseBrowseType == 'bysearch' ? 'all' : $this->session->caseBrowseType;
  109. $this->testcaseZen->setBrowseSession($productID, $branch, $moduleID, $browseType, $orderBy);
  110. if($from != 'doc') list($productID, $branch) = $this->testcaseZen->setBrowseMenu($productID, $branch, $projectID);
  111. $currentModule = ($this->app->tab == 'project' && $from != 'doc' && $from != 'ai') ? 'project' : 'testcase';
  112. $currentMethod = ($this->app->tab == 'project' && $from != 'doc' && $from != 'ai') ? 'testcase' : 'browse';
  113. $projectParam = ($this->app->tab == 'project' && $from != 'doc' && $from != 'ai') ? "projectID={$this->session->project}&" : '';
  114. $suffixParam = "&caseType=$caseType&orderBy=$orderBy&recTotal=$recTotal&recPerPage=$recPerPage&pageID=$pageID";
  115. if($from == 'doc' || $from == 'ai') $suffixParam .= "&projectID=$projectID&from=$from&blockID=$blockID";
  116. $actionURL = $this->createLink($currentModule, $currentMethod, $projectParam . "productID=$productID&branch=$branch&browseType=bySearch&queryID=myQueryID" . $suffixParam);
  117. $this->testcaseZen->buildBrowseSearchForm($productID, $queryID, $projectID, $actionURL);
  118. $this->testcaseZen->assignCasesForBrowse($productID, $branch, $browseType, ($browseType == 'bysearch' ? $queryID : $suiteID), $moduleID, $caseType, $orderBy, $recTotal, $recPerPage, $pageID, $from);
  119. $this->testcaseZen->assignModuleTreeForBrowse($productID, $branch, $projectID);
  120. $this->testcaseZen->assignProductAndBranchForBrowse($productID, $branch, $projectID);
  121. $this->testcaseZen->assignForBrowse($productID, $branch, $browseType, $projectID, $param, $moduleID, $suiteID, $caseType);
  122. $this->view->from = $from;
  123. $this->view->blockID = $blockID;
  124. $this->view->docBlock = false;
  125. $this->view->idList = '';
  126. $this->view->suffixParam = $suffixParam;
  127. if($from === 'doc')
  128. {
  129. $docBlock = $this->loadModel('doc')->getDocBlock($blockID);
  130. $this->view->docBlock = $docBlock;
  131. if($docBlock)
  132. {
  133. $content = json_decode($docBlock->content, true);
  134. if(isset($content['idList'])) $this->view->idList = $content['idList'];
  135. }
  136. }
  137. $this->display();
  138. }
  139. /**
  140. * 浏览场景列表。
  141. * Browse scenes.
  142. *
  143. * @param int $productID
  144. * @param string $branch
  145. * @param int $moduleID
  146. * @param string $orderBy
  147. * @param int $recTotal
  148. * @param int $recPerPage
  149. * @param int $pageID
  150. * @access public
  151. * @return void
  152. */
  153. public function browseScene($productID = 0, $branch = '', $moduleID = 0, $orderBy = 'sort_asc,id_asc', $recTotal = 0, $recPerPage = 20, $pageID = 1)
  154. {
  155. $this->testcaseZen->checkProducts(); // 如果不存在产品,则跳转到产品创建页面。
  156. $this->app->loadClass('pager', true);
  157. $pager = new pager($recTotal, $recPerPage, $pageID);
  158. $productID = $this->product->checkAccess($productID, $this->products);
  159. $branch = $this->testcaseZen->getBrowseBranch($branch);
  160. $tree = $moduleID ? $this->tree->getByID($moduleID) : '';
  161. $showModule = !empty($this->config->testcase->browse->showModule) ? $this->config->testcase->browse->showModule : '';
  162. $this->testcaseZen->setBrowseCookie($productID, $branch);
  163. $this->testcaseZen->setBrowseSession($productID, $branch, $moduleID);
  164. $this->testcaseZen->assignProductAndBranchForBrowse($productID, $branch);
  165. /* 这些变量用于设置公共的头部菜单。These variables are used to set the common header menu. */
  166. $this->view->browseType = 'onlyscene';
  167. $this->view->caseType = '';
  168. $this->view->projectID = 0;
  169. $this->view->suiteList = $this->loadModel('testsuite')->getSuites($productID);
  170. $this->view->title = zget($this->products, $productID, '') . $this->lang->hyphen . $this->lang->testcase->common;
  171. $this->view->scenes = $this->testcase->getSceneGroups($productID, $branch, $moduleID, $orderBy, $pager);
  172. $this->view->users = $this->user->getPairs('noletter');
  173. $this->view->modules = $this->tree->getOptionMenu($productID, 'case', 0, $branch == 'all' ? '0' : $branch);
  174. $this->view->moduleTree = $this->tree->getTreeMenu($productID, 'case', 0, ['treeModel', 'createSceneLink'], ['orderBy' => $orderBy], $branch);
  175. $this->view->moduleName = $moduleID ? $tree->name : $this->lang->tree->all;
  176. $this->view->modulePairs = $showModule ? $this->tree->getModulePairs($productID, 'case', $showModule) : [];
  177. $this->view->moduleID = $moduleID;
  178. $this->view->orderBy = $orderBy;
  179. $this->view->pager = $pager;
  180. $this->display();
  181. }
  182. /**
  183. * 分组查看用例。
  184. * Group case.
  185. *
  186. * @param int $productID
  187. * @param string $branch
  188. * @param string $groupBy
  189. * @param int $objectID
  190. * @param string $caseType
  191. * @param string $browseType
  192. * @access public
  193. * @return void
  194. */
  195. public function groupCase($productID = 0, $branch = '', $groupBy = 'story', $objectID = 0, $caseType = '', $browseType = 'all')
  196. {
  197. if($browseType) $this->session->set('caseBrowseType', $browseType);
  198. $this->testcaseZen->checkProducts(); // 如果不存在产品,则跳转到产品创建页面。
  199. /* 设置SESSION和COOKIE,获取产品信息。 */
  200. /* Set session and cookie, and get product. */
  201. $productID = $this->product->checkAccess($productID, $this->products);
  202. $product = $this->product->getByID($productID);
  203. $this->session->set('caseList', $this->app->getURI(true), $this->app->tab);
  204. if($branch === '') $branch = $this->cookie->preBranch;
  205. if(empty($groupBy)) $groupBy = 'story';
  206. $projectID = $executionID = 0;
  207. if($this->app->tab == 'project')
  208. {
  209. $projectID = $this->session->project;
  210. $this->view->projectID = $projectID;
  211. }
  212. else
  213. {
  214. $executionID = $this->session->execution;
  215. $this->view->executionID = $executionID;
  216. }
  217. /* 设置菜单。 */
  218. /* Set menu. */
  219. $this->testcaseZen->setMenu((int)$projectID, (int)$executionID, $productID, $branch);
  220. /* 展示变量. */
  221. /* Show the variables. */
  222. $this->view->title = $this->products[$productID] . $this->lang->hyphen . $this->lang->testcase->common;
  223. $this->view->productID = $productID;
  224. $this->view->users = $this->user->getPairs('noletter');
  225. $this->view->browseType = 'group';
  226. $this->view->groupBy = $groupBy;
  227. $this->view->orderBy = $groupBy;
  228. $this->view->cases = $this->testcaseZen->getGroupCases($productID, $branch, $groupBy, $caseType, $browseType);
  229. $this->view->suiteList = $this->loadModel('testsuite')->getSuites($productID);
  230. $this->view->branch = $branch;
  231. $this->view->caseType = $caseType;
  232. $this->view->product = $product;
  233. $this->display();
  234. }
  235. /**
  236. * Show zero case story.
  237. *
  238. * @param int $productID
  239. * @param int $branchID
  240. * @param string $orderBy
  241. * @param int $objectID
  242. * @param int $recTotal
  243. * @param int $recPerPage
  244. * @param int $pageID
  245. * @access public
  246. * @return void
  247. */
  248. public function zeroCase($productID = 0, $branchID = 0, $orderBy = 'id_desc', $objectID = 0, $recTotal = 0, $recPerPage = 20, $pageID = 1)
  249. {
  250. $this->testcaseZen->checkProducts(); // 如果不存在产品,则跳转到产品创建页面。
  251. /* 设置 session, cookie 和菜单。*/
  252. /* Set session, cookie and set menu. */
  253. $this->session->set('storyList', $this->app->getURI(true) . '#app=' . $this->app->tab, 'product');
  254. $this->session->set('caseList', $this->app->getURI(true), $this->app->tab);
  255. $this->testcaseZen->setMenu((int)$this->session->project, 0, $productID, $branchID);
  256. $projectID = $executionID = 0;
  257. if($this->app->tab == 'project')
  258. {
  259. $projectID = $objectID;
  260. $products = $this->product->getProducts($this->session->project, 'all', '', false);
  261. $productID = $this->product->checkAccess($productID, $products);
  262. $this->config->hasSwitcherMethods[] = 'testcase-zerocase';
  263. $productPairs = $this->loadModel('project')->getMultiLinkedProducts($this->session->project);
  264. $this->view->projectID = $projectID;
  265. $this->view->switcherParams = "projectID={$this->session->project}&productID={$productID}&currentMethod=zerocase";
  266. $this->view->switcherText = zget($productPairs, $productID, $this->lang->product->all);
  267. }
  268. if($this->app->tab == 'execution')
  269. {
  270. $executionID = $objectID;
  271. $products = $this->product->getProducts($executionID, 'all', '', false);
  272. $productID = $this->product->checkAccess($productID, $products);
  273. $this->config->hasSwitcherMethods[] = 'testcase-zerocase';
  274. $productPairs = $this->loadModel('project')->getMultiLinkedProducts($executionID);
  275. $this->view->executionID = $executionID;
  276. $this->view->switcherParams = "executioID={$executionID}&productID={$productID}&currentMethod=zerocase";
  277. $this->view->switcherText = zget($productPairs, $productID, $this->lang->product->all);
  278. }
  279. else
  280. {
  281. $products = $this->product->getPairs();
  282. $productID = $this->product->checkAccess($productID, $products);
  283. $this->loadModel('qa');
  284. $this->app->rawModule = 'testcase';
  285. foreach($this->config->qa->menuList as $module) $this->lang->navGroup->$module = 'qa';
  286. }
  287. /* 设置 分页。*/
  288. /* Set pager. */
  289. $this->app->loadClass('pager', $static = true);
  290. $pager = new pager($recTotal, $recPerPage, $pageID);
  291. /* 追加二次排序条件。*/
  292. /* Append id for second sort. */
  293. $sort = common::appendOrder(empty($orderBy) ? 'id_desc' : $orderBy);
  294. if(strpos($sort, 'planTitle') !== false) $sort = str_replace('planTitle', 'plan', $sort);
  295. $this->lang->testcase->featureBar['zerocase'] = $this->lang->testcase->featureBar['browse'];
  296. /* 展示变量. */
  297. /* Show the variables. */
  298. $this->loadModel('story');
  299. $this->view->title = $this->lang->story->zeroCase;
  300. $this->view->stories = $this->story->getZeroCase($productID, $projectID, $executionID, $branchID, $sort, $pager);
  301. $this->view->users = $this->user->getPairs('noletter');
  302. $this->view->objectID = $objectID;
  303. $this->view->productID = $productID;
  304. $this->view->branch = $branchID;
  305. $this->view->orderBy = $orderBy;
  306. $this->view->suiteList = $this->loadModel('testsuite')->getSuites($productID);
  307. $this->view->browseType = '';
  308. $this->view->product = $this->product->getByID($productID);
  309. $this->view->pager = $pager;
  310. $this->display();
  311. }
  312. /**
  313. * 创建一个测试用例。
  314. * Create a test case.
  315. * @param int $productID
  316. * @param string $branch
  317. * @param int $moduleID
  318. * @param string $from
  319. * @param int $param
  320. * @param int $storyID
  321. * @param string $extras
  322. * @access public
  323. * @return void
  324. */
  325. public function create($productID, $branch = '', $moduleID = 0, $from = '', $param = 0, $storyID = 0, $extras = '')
  326. {
  327. $this->testcaseZen->checkProducts(); // 如果不存在产品,则跳转到产品创建页面。
  328. if(!empty($_POST))
  329. {
  330. /* 构建用例。 */
  331. /* Build Case. */
  332. $case = $this->testcaseZen->buildCaseForCreate($from, $param);
  333. /* 创建测试用例前检验表单数据是否正确。 */
  334. /* Check from data for create case. */
  335. $this->testcaseZen->checkCreateFormData($case);
  336. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  337. $caseID = $this->testcase->create($case);
  338. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  339. $this->testcaseZen->afterCreate($case, $caseID);
  340. return $this->testcaseZen->responseAfterCreate($caseID, $case->module);
  341. }
  342. if(empty($this->products)) $this->locate($this->createLink('product', 'create'));
  343. /* 设置产品id和分支。 */
  344. /* Set productID and branch. */
  345. $productID = $this->product->checkAccess($productID, $this->products);
  346. if($branch === '') $branch = $this->cookie->preBranch ? : '';
  347. $this->testcaseZen->assignCreateVars($productID, $branch, $moduleID, $from, $param, $storyID);
  348. /* 设置自定义字段。 */
  349. /* Set custom fields. */
  350. foreach(explode(',', $this->config->testcase->list->customCreateFields) as $field) $customFields[$field] = $this->lang->testcase->$field;
  351. $this->view->customFields = $customFields;
  352. $this->view->showFields = $this->config->testcase->custom->createFields;
  353. $extras = str_replace(array(',', ' '), array('&', ''), $extras);
  354. parse_str($extras, $output);
  355. $this->view->title = $this->products[$productID] . $this->lang->hyphen . $this->lang->testcase->create;
  356. $this->view->productID = $productID;
  357. $this->view->users = $this->user->getPairs('noletter|noclosed|nodeleted');
  358. $this->view->gobackLink = isset($output['from']) && $output['from'] == 'global' ? $this->createLink('testcase', 'browse', "productID=$productID") : '';
  359. $this->view->needReview = $this->testcase->forceNotReview() == true ? 0 : 1;
  360. $this->view->onlyAutoCase = $this->cookie->onlyAutoCase;
  361. $this->display();
  362. }
  363. /**
  364. * 批量创建用例。
  365. * Create batch testcase.
  366. *
  367. * @param int $productID
  368. * @param string $branch
  369. * @param int $moduleID
  370. * @param int $storyID
  371. * @access public
  372. * @return void
  373. */
  374. public function batchCreate($productID, $branch = '', $moduleID = 0, $storyID = 0)
  375. {
  376. $this->testcaseZen->checkProducts(); // 如果不存在产品,则跳转到产品创建页面。
  377. if(!empty($_POST))
  378. {
  379. $testcases = $this->testcaseZen->buildCasesForBathcCreate($productID);
  380. $testcases = $this->testcaseZen->checkTestcasesForBatchCreate($testcases, $productID);
  381. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  382. foreach($testcases as $testcase)
  383. {
  384. $testcaseID = $this->testcase->create($testcase);
  385. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  386. $this->executeHooks($testcaseID);
  387. $this->testcase->syncCase2Project($testcase, $testcaseID);
  388. }
  389. if(!dao::isError()) $this->loadModel('score')->create('ajax', 'batchCreate');
  390. return $this->testcaseZen->responseAfterBatchCreate($productID, $branch);
  391. }
  392. if(empty($this->products)) $this->locate($this->createLink('product', 'create'));
  393. /* 设置 session 和 cookie, 并且设置产品 id、分支。 */
  394. /* Set session and cookie, and set product id, branch. */
  395. $productID = $this->product->checkAccess($productID, $this->products);
  396. if($branch === '') $branch = $this->cookie->preBranch;
  397. /* 设置菜单。 */
  398. /* Set menu. */
  399. $this->testcaseZen->setMenu((int)$this->session->project, 0, $productID, $branch);
  400. /* 指派产品、分支、需求、自定义字段等变量. */
  401. /* Assign the variables about product, branches, story and custom fields. */
  402. $this->testcaseZen->assignForBatchCreate($productID, $branch, $moduleID, $storyID);
  403. /* 展示变量. */
  404. /* Show the variables. */
  405. $this->view->title = $this->products[$productID] . $this->lang->hyphen . $this->lang->testcase->batchCreate;
  406. $this->view->productID = $productID;
  407. $this->view->productName = $this->products[$productID];
  408. $this->view->moduleOptionMenu = $this->tree->getOptionMenu($productID, 'case', 0, $branch === 'all' ? '0' : $branch);
  409. $this->view->currentSceneID = 0;
  410. $this->view->branch = $branch;
  411. $this->view->needReview = $this->testcase->forceNotReview() == true ? 0 : 1;
  412. $this->display();
  413. }
  414. /**
  415. * 创建 bug。
  416. * Create bug.
  417. *
  418. * @param int $productID
  419. * @param int $caseID
  420. * @param int $version
  421. * @param int $runID
  422. * @param string $extras
  423. * @access public
  424. * @return void
  425. */
  426. public function createBug($productID, $caseID, $version, $runID = 0)
  427. {
  428. $this->testcaseZen->checkProducts(); // 如果不存在产品,则跳转到产品创建页面。
  429. /* 获取用例和用例执行结果。 */
  430. /* Get case and results. */
  431. $case = '';
  432. if($runID)
  433. {
  434. $case = $this->loadModel('testtask')->getRunById($runID)->case;
  435. }
  436. elseif($caseID)
  437. {
  438. $case = $this->testcase->fetchById($caseID);
  439. }
  440. /* 如果用例不存在,关闭模态框。 */
  441. /* If case isn't exist, close modal box. */
  442. if(!$case) return $this->send(array('result' => 'fail', 'message' => $this->lang->notFound, 'closeModal' => true));
  443. /* 如果产品未获取,获取产品。 */
  444. /* If product isn't available, get the product. */
  445. if(!isset($this->products[$productID]))
  446. {
  447. $product = $this->product->getByID($productID);
  448. $this->products[$productID] = $product->name;
  449. }
  450. /* 展示变量. */
  451. /* Show the variables. */
  452. $this->view->title = $this->products[$productID] . $this->lang->hyphen . $this->lang->testcase->createBug;
  453. $this->view->caseID = $caseID;
  454. $this->view->version = $version;
  455. $this->view->runID = $runID;
  456. $this->display();
  457. }
  458. /**
  459. * View a test case.
  460. *
  461. * @param int $caseID
  462. * @param int $version
  463. * @param string $from
  464. * @param int $taskID
  465. * @param string $stepsType
  466. * @param int $suiteID
  467. * @access public
  468. * @return void
  469. */
  470. public function view($caseID, $version = 0, $from = 'testcase', $taskID = 0, $stepsType = '', $suiteID = 0)
  471. {
  472. $this->session->set('bugList', $this->app->getURI(true), $this->app->tab);
  473. $this->session->set('testtaskID', $taskID, $this->app->tab);
  474. if(empty($stepsType)) $stepsType = $this->cookie->stepsType;
  475. if(empty($stepsType)) $stepsType = 'table';
  476. setCookie('stepsType', $stepsType, $this->config->cookieLife, $this->config->webRoot);
  477. $case = $this->testcase->getById($caseID, $version);
  478. /* 如果用例不存在,返回到测试仪表盘页面。 */
  479. /* If testcase isn't exist, locate to qa-ndex.*/
  480. if(!$case)
  481. {
  482. if(defined('RUN_MODE') && RUN_MODE == 'api') return $this->send(array('status' => 'fail', 'message' => '404 Not found'));
  483. if(!$this->server->http_referer) return print(js::alert($this->lang->notFound) . js::locate($this->createLink('testcase', 'browse', '')));
  484. return $this->send(array('result' => 'fail', 'load' => array('alert' => $this->lang->notFound, 'locate' => $this->createLink('testcase', 'browse'))));
  485. }
  486. $case->caseID = $case->id;
  487. $this->executeHooks($caseID);
  488. if(defined('RUN_MODE') && RUN_MODE == 'api' && !empty($this->app->version)) return $this->send(array('status' => 'success', 'case' => $case));
  489. $this->testcaseZen->assignCaseForView($case, $from, $taskID);
  490. $isLibCase = $case->lib && empty($case->product);
  491. /* 如果用例是在用例库内,指定相关变量。 */
  492. /* If testcase is in case lib, assign associated variables. */
  493. if($isLibCase)
  494. {
  495. $libraries = $this->loadModel('caselib')->getLibraries();
  496. $this->app->tab == 'project' ? $this->loadModel('project')->setMenu($this->session->project) : $this->caselib->setLibMenu($libraries, $case->lib);
  497. if($this->app->tab == 'project') $this->view->projectID = (int)$this->session->project;
  498. $this->view->libID = $case->lib;
  499. $this->view->title = "CASE #$case->id $case->title - " . $libraries[$case->lib];
  500. $this->view->libName = $libraries[$case->lib];
  501. }
  502. /* 如果用例不在用例库内,指定相关变量。 */
  503. /* If testcase isn't in case lib, assign associated variables. */
  504. else
  505. {
  506. $this->testcaseZen->checkProducts(); // 如果不存在产品,则跳转到产品创建页面。
  507. $productID = $case->product;
  508. $product = $this->product->getByID($productID);
  509. $branches = $product->type == 'normal' ? array() : $this->loadModel('branch')->getPairs($productID);
  510. $this->testcaseZen->setMenu((int)$this->session->project, (int)$this->session->execution, $productID, $case->branch);
  511. $this->view->title = "CASE #$case->id $case->title - " . $product->name;
  512. $this->view->product = $product;
  513. $this->view->branches = $branches;
  514. $this->view->branchName = $product->type == 'normal' ? '' : zget($branches, $case->branch, '');
  515. }
  516. $this->view->suiteID = $suiteID;
  517. $this->view->testcase = $case;
  518. $this->view->stepsType = $stepsType;
  519. $this->view->version = $version ? $version : $case->version;
  520. $this->view->isLibCase = $isLibCase;
  521. $this->display();
  522. }
  523. /**
  524. * 编辑用例。
  525. * Edit a case.
  526. *
  527. * @param int $caseID
  528. * @param string $comment
  529. * @param int $executionID
  530. * @param string $from
  531. * @access public
  532. * @return void
  533. */
  534. public function edit($caseID, $comment = 'false', $executionID = 0, $from = 'testcase')
  535. {
  536. $oldCase = $this->testcase->getByID($caseID);
  537. if(!$oldCase) return $this->send(array('result' => 'fail', 'callback' => "zui.Modal.alert('{$this->lang->notFound}')", 'load' => array('back' => true), 'message' => $this->lang->testcase->noCase));
  538. $testtasks = $this->loadModel('testtask')->getGroupByCases($caseID);
  539. $testtasks = empty($testtasks[$caseID]) ? array() : $testtasks[$caseID];
  540. if(!empty($_POST))
  541. {
  542. $formData = form::data($this->config->testcase->form->edit, $caseID);
  543. $case = $this->testcaseZen->prepareEditExtras($formData, $oldCase);
  544. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  545. $changes = array();
  546. if($comment != 'true')
  547. {
  548. $changes = $this->testcase->update($case, $oldCase, $testtasks);
  549. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  550. }
  551. if($case->comment || !empty($changes)) $this->testcaseZen->addEditAction($caseID, $oldCase->status, $case->status, $changes, $case->comment);
  552. $message = $this->executeHooks($caseID);
  553. if(!$message) $message = $this->lang->saveSuccess;
  554. if(defined('RUN_MODE') && RUN_MODE == 'api') return $this->send(array('status' => 'success', 'data' => $caseID));
  555. if(isInModal() && $this->app->tab == 'my') return $this->send(array('result' => 'success', 'message' => $message, 'closeModal' => true));
  556. $testtaskID = $from == 'testtask' && $this->session->testtaskID ? $this->session->testtaskID : 0;
  557. $locate = $oldCase->lib ? $this->createLink('caselib', 'viewCase', "caseID={$caseID}") : $this->createLink('testcase', 'view', "caseID={$caseID}&version=0&from={$from}&testtaskID={$testtaskID}");
  558. return $this->send(array('result' => 'success', 'message' => $message, 'closeModal' => true, 'load' => $locate));
  559. }
  560. $case = $this->testcaseZen->preProcessForEdit($oldCase);
  561. if($case->lib && empty($case->product))
  562. {
  563. $productID = isset($this->session->product) ? $this->session->product : 0;
  564. $libraries = $this->loadModel('caselib')->getLibraries();
  565. $this->testcaseZen->setMenuForLibCaseEdit($case, $libraries);
  566. $this->testcaseZen->assignForEditLibCase($case, $libraries);
  567. }
  568. else
  569. {
  570. $this->testcaseZen->checkProducts(); // 如果不存在产品,则跳转到产品创建页面。
  571. $productID = $case->product;
  572. $this->testcaseZen->setMenuForCaseEdit($case, $executionID);
  573. $this->testcaseZen->assignForEditCase($case, $executionID);
  574. }
  575. $this->testcaseZen->assignForEdit($productID, $case, $testtasks);
  576. $this->display();
  577. }
  578. /**
  579. * Batch edit case.
  580. *
  581. * @param int $productID
  582. * @param string $branch
  583. * @param string $type
  584. * @param string $from
  585. * @access public
  586. * @return void
  587. */
  588. public function batchEdit($productID = 0, $branch = '0', $type = 'case', $from = '')
  589. {
  590. if($type == 'case') $this->testcaseZen->checkProducts(); // 如果不存在产品,则跳转到产品创建页面。
  591. if(!$this->post->caseIdList && !$this->post->id) $this->locate($this->session->caseList ? $this->session->caseList : inlink('browse', "productID={$productID}"));
  592. $caseIdList = $this->post->caseIdList ? array_unique($this->post->caseIdList) : array_unique($this->post->id);
  593. $cases = $this->testcase->getByList($caseIdList);
  594. $caseIdList = array_keys($cases);
  595. $testtasks = $this->loadModel('testtask')->getGroupByCases($caseIdList);
  596. if($this->post->id)
  597. {
  598. $oldSteps = $this->testcase->fetchStepsByList($caseIdList);
  599. $editedCases = $this->testcaseZen->buildCasesForBathcEdit($cases, $oldSteps);
  600. $editedCases = $this->testcaseZen->checkCasesForBatchEdit($editedCases);
  601. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  602. /* 更新用例。 */
  603. /* Update cases. */
  604. $caseFiles = $this->testcase->getRelatedFiles($caseIdList);
  605. foreach($editedCases as $caseID => $case)
  606. {
  607. $oldCase = $cases[$caseID];
  608. if(!isset($case->files)) $case->files = zget($caseFiles, $caseID, array());
  609. if(!isset($oldCase->files)) $oldCase->files = zget($caseFiles, $caseID, array());
  610. if(!isset($oldCase->steps)) $oldCase->steps = zget($oldSteps, $caseID, array());
  611. $changes = $this->testcase->update($case, $oldCase, zget($testtasks, $caseID, array()));
  612. $this->executeHooks($caseID);
  613. if(empty($changes)) continue;
  614. $actionID = $this->loadModel('action')->create('case', $caseID, 'Edited');
  615. $this->action->logHistory($actionID, $changes);
  616. }
  617. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  618. $this->loadModel('score')->create('ajax', 'batchEdit');
  619. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $this->session->caseList));
  620. }
  621. if($this->app->tab == 'project')
  622. {
  623. $this->loadModel('project')->setMenu($this->session->project);
  624. $this->view->projectID = $this->session->project;
  625. }
  626. elseif($this->app->tab == 'my')
  627. {
  628. $this->loadModel('my');
  629. if($from == 'work' || $from == 'contribute')
  630. {
  631. $this->lang->my->menu->{$from}['subModule'] = 'testcase';
  632. $this->lang->my->menu->{$from}['subMenu']->testcase['subModule'] = 'testcase';
  633. }
  634. }
  635. $this->testcaseZen->assignForBatchEdit($productID, $branch, $type, $cases);
  636. if($type == 'lib') $this->testcaseZen->assignLibForBatchEdit($productID);
  637. /* 判断要编辑的用例是否太大,设置 session。 */
  638. /* Judge whether the editedCases is too large and set session. */
  639. $countInputVars = count($cases) * (count(explode(',', $this->config->testcase->custom->batchEditFields)) + 3);
  640. $showSuhosinInfo = common::judgeSuhosinSetting($countInputVars);
  641. if($showSuhosinInfo) $this->view->suhosinInfo = extension_loaded('suhosin') ? sprintf($this->lang->suhosinInfo, $countInputVars) : sprintf($this->lang->maxVarsInfo, $countInputVars);
  642. $stories = $this->loadModel('story')->getProductStoryPairs($productID, $branch, array(), 'all', 'id_desc', 0, '', 'story', false);
  643. $cases = $this->testcaseZen->processStepsAndExpectsForBatchEdit($cases);
  644. /* 展示变量. */
  645. /* Show the variables. */
  646. $this->view->stories = $this->story->addGradeLabel($stories);
  647. $this->view->caseIdList = $caseIdList;
  648. $this->view->productID = $productID;
  649. $this->view->cases = $cases;
  650. $this->view->forceNotReview = $this->testcase->forceNotReview();
  651. $this->view->testtasks = $testtasks;
  652. $this->view->isLibCase = $type == 'lib' ? true : false;
  653. $this->display();
  654. }
  655. /**
  656. * 评审用例。
  657. * Review case.
  658. *
  659. * @param int $caseID
  660. * @access public
  661. * @return void
  662. */
  663. public function review($caseID)
  664. {
  665. $oldCase = $this->testcase->getByID($caseID);
  666. if($_POST)
  667. {
  668. $case = $this->testcaseZen->prepareReviewData($caseID, $oldCase);
  669. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  670. $this->testcase->review($case, $oldCase);
  671. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  672. $message = $this->executeHooks($caseID);
  673. if(!$message) $message = $this->lang->saveSuccess;
  674. return $this->send(array('result' => 'success', 'message' => $message, 'closeModal' => true, 'load' => true));
  675. }
  676. if((isset($oldCase->caseStatus) && $oldCase->caseStatus != 'wait') || $oldCase->status != 'wait')
  677. {
  678. return $this->send(array('result' => 'fail', 'callback' => "zui.Modal.alert({icon: 'icon-exclamation-sign', iconClass: 'warning-pale rounded-full icon-2x', message: '{$this->lang->hasReviewed}'}).then((res) => {loadCurrentPage()});"));
  679. }
  680. $this->view->title = $this->lang->testcase->review;
  681. $this->view->users = $this->user->getPairs('noletter|noclosed|nodeleted');
  682. $this->view->testcaseID = $caseID;
  683. $this->view->actions = $this->loadModel('action')->getList('case', $caseID);
  684. $this->display();
  685. }
  686. /**
  687. * Batch review case.
  688. *
  689. * @param string $result
  690. * @access public
  691. * @return void
  692. */
  693. public function batchReview($result)
  694. {
  695. if($this->post->caseIdList)
  696. {
  697. $this->testcase->batchReview($this->post->caseIdList, $result);
  698. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  699. }
  700. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'closeModal' => true, 'load' => $this->session->caseList));
  701. }
  702. /**
  703. * 删除用例。
  704. * Delete a test case.
  705. *
  706. * @param int $caseID
  707. * @access public
  708. * @return void
  709. */
  710. public function delete($caseID)
  711. {
  712. $case = $this->testcase->getByID($caseID);
  713. if(!$case) return $this->send(array('result' => 'fail', 'message' => $this->lang->testcase->noCase));
  714. $this->testcase->delete(TABLE_CASE, $caseID);
  715. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  716. $message = $this->executeHooks($caseID);
  717. if(!$message) $message = $this->lang->saveSuccess;
  718. if(defined('RUN_MODE') && RUN_MODE == 'api') return $this->send(array('status' => 'success'));
  719. $locateLink = $this->session->caseList ? $this->session->caseList : inlink('browse', "productID={$case->product}");
  720. return $this->send(array('result' => 'success', 'message' => $message, 'load' => $locateLink));
  721. }
  722. /**
  723. * Batch delete cases and scenes.
  724. *
  725. * @access public
  726. * @return void
  727. */
  728. public function batchDelete()
  729. {
  730. $caseIdList = zget($_POST, 'caseIdList', array());
  731. $sceneIdList = zget($_POST, 'sceneIdList', array());
  732. if($caseIdList || $sceneIdList)
  733. {
  734. $this->testcase->batchDelete($caseIdList, $sceneIdList);
  735. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  736. }
  737. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $this->session->caseList));
  738. }
  739. /**
  740. * 批量修改用例的分支。
  741. * Batch change branch.
  742. *
  743. * @param int $branchID
  744. * @access public
  745. * @return void
  746. */
  747. public function batchChangeBranch($branchID)
  748. {
  749. $caseIdList = zget($_POST, 'caseIdList', array());
  750. $sceneIdList = zget($_POST, 'sceneIdList', array());
  751. if($caseIdList || $sceneIdList)
  752. {
  753. $this->testcase->batchChangeBranch($caseIdList, $sceneIdList, $branchID);
  754. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  755. }
  756. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $this->session->caseList));
  757. }
  758. /**
  759. * 批量修改用例的模块。
  760. * Batch change the module of case.
  761. *
  762. * @param int $moduleID
  763. * @access public
  764. * @return void
  765. */
  766. public function batchChangeModule($moduleID)
  767. {
  768. $caseIdList = zget($_POST, 'caseIdList', array());
  769. $sceneIdList = zget($_POST, 'sceneIdList', array());
  770. if($caseIdList || $sceneIdList)
  771. {
  772. $this->testcase->batchChangeModule($caseIdList, $sceneIdList, $moduleID);
  773. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  774. }
  775. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => true));
  776. }
  777. /**
  778. * Batch review case.
  779. *
  780. * @param string $type
  781. * @access public
  782. * @return void
  783. */
  784. public function batchChangeType($type)
  785. {
  786. $caseIdList = zget($_POST, 'caseIdList', array());
  787. if($caseIdList)
  788. {
  789. $this->testcase->batchChangeType($caseIdList, $type);
  790. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  791. }
  792. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $this->session->caseList));
  793. }
  794. /**
  795. * 关联相关用例。
  796. * Link related cases.
  797. *
  798. * @param int $caseID
  799. * @param string $browseType
  800. * @param int $param
  801. * @param int $recTotal
  802. * @param int $recPerPage
  803. * @param int $pageID
  804. * @access public
  805. * @return void
  806. */
  807. public function linkCases($caseID, $browseType = '', $param = 0, $recTotal = 0, $recPerPage = 20, $pageID = 1)
  808. {
  809. $case = $this->testcase->getByID($caseID);
  810. /* 设置导航。*/
  811. /* Set menu. */
  812. $this->testcase->setMenu($case->product, $case->branch);
  813. /* 构建搜索表单。*/
  814. /* Build the search form. */
  815. $queryID = ($browseType == 'bySearch') ? (int)$param : 0;
  816. $this->testcaseZen->buildLinkCasesSearchForm($case, $queryID);
  817. /* 获取可关联的用例。*/
  818. /* Get cases to link. */
  819. $cases2Link = $this->testcase->getCases2Link($caseID, $browseType, $queryID);
  820. /* 用例分页。*/
  821. /* Pager. */
  822. $this->app->loadClass('pager', true);
  823. $recTotal = count($cases2Link);
  824. $pager = new pager($recTotal, $recPerPage, $pageID);
  825. $cases2Link = array_chunk($cases2Link, $pager->recPerPage);
  826. /* Assign. */
  827. $this->view->title = $case->title . $this->lang->hyphen . $this->lang->testcase->linkCases;
  828. $this->view->case = $case;
  829. $this->view->cases2Link = empty($cases2Link) ? $cases2Link : $cases2Link[$pageID - 1];
  830. $this->view->users = $this->loadModel('user')->getPairs('noletter');
  831. $this->view->pager = $pager;
  832. $this->display();
  833. }
  834. /**
  835. * 关联相关 bug。
  836. * Link related bugs.
  837. *
  838. * @param int $caseID
  839. * @param string $browseType
  840. * @param int $param
  841. * @param string $orderBy
  842. * @param int $recTotal
  843. * @param int $recPerPage
  844. * @param int $pageID
  845. * @access public
  846. * @return void
  847. */
  848. public function linkBugs($caseID, $browseType = '', $param = 0, $orderBy = 'id_asc', $recTotal = 0, $recPerPage = 20, $pageID = 1)
  849. {
  850. $this->loadModel('bug');
  851. $case = $this->testcase->getByID($caseID);
  852. /* 构建搜索表单。*/
  853. /* Build the search form. */
  854. $queryID = ($browseType == 'bySearch') ? (int)$param : 0;
  855. $this->testcaseZen->buildLinkBugsSearchForm($case, $queryID);
  856. /* 获取关联的bug。*/
  857. /* Get bugs to link. */
  858. $bugs2Link = $this->testcase->getBugs2Link($caseID, $browseType, $queryID, $orderBy);
  859. /* bug 分页。*/
  860. /* Pager. */
  861. $this->app->loadClass('pager', true);
  862. if(empty($recTotal)) $recTotal = count($bugs2Link);
  863. $pager = new pager($recTotal, $recPerPage, $pageID);
  864. $bugs2Link = array_chunk($bugs2Link, $pager->recPerPage);
  865. /* Assign. */
  866. $this->view->title = $this->lang->testcase->linkBugs;
  867. $this->view->case = $case;
  868. $this->view->bugs2Link = empty($bugs2Link) ? $bugs2Link : $bugs2Link[$pageID - 1];
  869. $this->view->users = $this->loadModel('user')->getPairs('noletter');
  870. $this->view->pager = $pager;
  871. $this->view->browseType = $browseType;
  872. $this->view->param = $param;
  873. $this->view->orderBy = $orderBy;
  874. $this->display();
  875. }
  876. /**
  877. * 确认用例变更。
  878. * Confirm testcase changed.
  879. *
  880. * @param int $caseID
  881. * @param int $taskID
  882. * @param string $from view|list
  883. * @access public
  884. * @return void
  885. */
  886. public function confirmChange($caseID, $taskID = 0, $from = 'view')
  887. {
  888. $case = $this->testcase->getById($caseID);
  889. $this->dao->update(TABLE_TESTRUN)->set('version')->eq($case->version)->where('`case`')->eq($caseID)->exec();
  890. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $from == 'view' ? inlink('view', "caseID={$caseID}&version={$case->version}&from=testtask&taskID={$taskID}") : true));
  891. }
  892. /**
  893. * 确认用例库用例的更新。
  894. * Confirm case changed in lib.
  895. *
  896. * @param int $caseID
  897. * @param int $libCaseID
  898. * @access public
  899. * @return void
  900. */
  901. public function confirmLibcaseChange($caseID, $libCaseID)
  902. {
  903. /** 获取用例和用例库的用例,设置用例版本。 /
  904. /* Get case and lib case, set case version.*/
  905. $case = $this->testcase->getById($caseID);
  906. $libCase = $this->testcase->getById($libCaseID);
  907. $version = $case->version + 1;
  908. /* 更新用例基础信息。 */
  909. /* Update case base information. */
  910. $this->dao->update(TABLE_CASE)
  911. ->set('version')->eq($version)
  912. ->set('fromCaseVersion')->eq($version)
  913. ->set('precondition')->eq($libCase->precondition)
  914. ->set('title')->eq($libCase->title)
  915. ->set('keywords')->eq($libCase->keywords)
  916. ->where('id')->eq($caseID)
  917. ->exec();
  918. /* 更新用例步骤。 */
  919. /* Update case steps. */
  920. $parentSteps = array();
  921. foreach($libCase->steps as $step)
  922. {
  923. $data = new stdclass();
  924. $data->parent = zget($parentSteps, $step->parent, 0);
  925. $data->case = $caseID;
  926. $data->version = $version;
  927. $data->type = $step->type;
  928. $data->desc = $step->desc;
  929. $data->expect = $step->expect;
  930. $this->dao->insert(TABLE_CASESTEP)->data($data)->exec();
  931. $parentSteps[$step->id] = $this->dao->lastInsertID();
  932. }
  933. /* 更新用例文件。 */
  934. /* Update case files. */
  935. $this->dao->delete()->from(TABLE_FILE)->where('objectType')->eq('testcase')->andWhere('objectID')->eq($caseID)->exec();
  936. foreach($libCase->files as $fileID => $file)
  937. {
  938. $fileName = pathinfo($file->pathname, PATHINFO_FILENAME);
  939. $datePath = substr($file->pathname, 0, 6);
  940. $realPath = $this->app->getAppRoot() . "www/data/upload/{$this->app->company->id}/" . "{$datePath}/" . $fileName;
  941. $rand = rand();
  942. $newFileName = $fileName . 'copy' . $rand;
  943. $newFilePath = $this->app->getAppRoot() . "www/data/upload/{$this->app->company->id}/" . "{$datePath}/" . $newFileName;
  944. copy($realPath, $newFilePath);
  945. $newFileName = $file->pathname;
  946. $newFileName = str_replace('.', "copy$rand.", $newFileName);
  947. $file->title = $file->name;
  948. unset($file->id, $file->name, $file->realPath, $file->webPath, $file->url);
  949. $file->objectID = $caseID;
  950. $file->pathname = $newFileName;
  951. $this->dao->insert(TABLE_FILE)->data($file)->exec();
  952. }
  953. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => true, 'closeModal' => true));
  954. }
  955. /**
  956. * 忽略用例库用例的更新。
  957. * Ignore case changed in lib.
  958. *
  959. * @param int $caseID
  960. * @access public
  961. * @return void
  962. */
  963. public function ignoreLibcaseChange($caseID)
  964. {
  965. $case = $this->testcase->fetchBaseInfo($caseID);
  966. $this->dao->update(TABLE_CASE)->set('fromCaseVersion')->eq($case->version)->where('id')->eq($caseID)->exec();
  967. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => true, 'closeModal' => true));
  968. }
  969. /**
  970. * Confirm story changes.
  971. *
  972. * @param int $caseID
  973. * @param bool $reload
  974. * @access public
  975. * @return void
  976. */
  977. public function confirmStoryChange($caseID, $reload = true)
  978. {
  979. $case = $this->testcase->fetchBaseInfo($caseID);
  980. if($case->story)
  981. {
  982. $story = $this->loadModel('story')->fetchBaseInfo($case->story);
  983. if($story->version != $case->storyVersion)
  984. {
  985. $this->dao->update(TABLE_CASE)->set('storyVersion')->eq($story->version)->where('id')->eq($caseID)->exec();
  986. $this->loadModel('action')->create('case', $caseID, 'confirmed', '', $story->version);
  987. }
  988. }
  989. if($reload) return $this->send(array('load' => true));
  990. }
  991. /**
  992. * Batch confirm story change of cases.
  993. *
  994. * @access public
  995. * @return void
  996. */
  997. public function batchConfirmStoryChange()
  998. {
  999. $caseIdList = zget($_POST, 'caseIdList', array());
  1000. if($caseIdList)
  1001. {
  1002. $this->testcase->batchConfirmStoryChange($caseIdList);
  1003. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  1004. }
  1005. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $this->session->caseList));
  1006. }
  1007. /**
  1008. * 导出用例。
  1009. * export cases.
  1010. *
  1011. * @param int $productID
  1012. * @param string $orderBy
  1013. * @param int $taskID
  1014. * @param string $browseType
  1015. * @access public
  1016. * @return void
  1017. */
  1018. public function export($productID, $orderBy, $taskID = 0, $browseType = '')
  1019. {
  1020. $this->testcaseZen->checkProducts(); // 如果不存在产品,则跳转到产品创建页面。
  1021. $product = $this->loadModel('product')->getByID($productID);
  1022. if($product->shadow) $this->config->testcase->exportFields = str_replace('product,', '', $this->config->testcase->exportFields);
  1023. if($product->type == 'normal') $this->config->testcase->exportFields = str_replace('branch,', '', $this->config->testcase->exportFields);
  1024. if($product->type != 'normal') $this->lang->testcase->branch = $this->lang->product->branchName[$product->type];
  1025. if($_POST)
  1026. {
  1027. $fields = $this->testcaseZen->getExportFields($product->type);
  1028. $cases = $this->testcase->getCasesToExport($this->post->exportType, $taskID, $orderBy, (int)$this->post->limit);
  1029. $cases = $this->testcaseZen->processCasesForExport($cases, $productID, $taskID);
  1030. if($this->config->edition != 'open') list($fields, $cases) = $this->loadModel('workflowfield')->appendDataFromFlow($fields, $cases);
  1031. $this->post->set('fields', $fields);
  1032. $this->post->set('rows', $cases);
  1033. $this->post->set('kind', 'testcase');
  1034. $this->fetch('file', 'export2' . $this->post->fileType, $_POST);
  1035. }
  1036. $fileName = $this->lang->testcase->common;
  1037. $browseType = isset($this->lang->testcase->featureBar['browse'][$browseType]) ? $this->lang->testcase->featureBar['browse'][$browseType] : '';
  1038. if($taskID)
  1039. {
  1040. $task = $this->loadModel('testtask')->fetchByID($taskID);
  1041. $taskName = $task->name;
  1042. $this->config->testcase->exportFields = str_replace('pri,', 'pri,assignedTo,', $this->config->testcase->exportFields);
  1043. }
  1044. $this->view->fileName = $product->name . $this->lang->dash . ($taskID ? $taskName . $this->lang->dash : '') . $browseType . $fileName;
  1045. $this->view->allExportFields = $this->config->testcase->exportFields;
  1046. $this->view->customExport = true;
  1047. $this->display();
  1048. }
  1049. /**
  1050. * 导出模板。
  1051. * Export template.
  1052. *
  1053. * @param int $productID
  1054. * @access public
  1055. * @return void
  1056. */
  1057. public function exportTemplate($productID)
  1058. {
  1059. $this->testcaseZen->checkProducts(); // 如果不存在产品,则跳转到产品创建页面。
  1060. if($_POST)
  1061. {
  1062. $num = (int)$this->post->num;
  1063. $product = $this->loadModel('product')->getByID($productID);
  1064. $fields = $this->testcaseZen->getFieldsForExportTemplate($product->type);
  1065. $rows = $this->testcaseZen->getRowsForExportTemplate($product, $num);
  1066. $this->post->set('fields', $fields);
  1067. $this->post->set('kind', 'testcase');
  1068. $this->post->set('rows', $rows);
  1069. $this->post->set('extraNum', $num);
  1070. $this->post->set('fileName', 'Template');
  1071. $this->fetch('file', 'export2csv', $_POST);
  1072. }
  1073. $this->display();
  1074. }
  1075. /**
  1076. * 从 csv 导入用例。
  1077. * Import testcases from csv.
  1078. *
  1079. * @param int $productID
  1080. * @param string $branch
  1081. * @access public
  1082. * @return void
  1083. */
  1084. public function import($productID, $branch = '0')
  1085. {
  1086. $this->testcaseZen->checkProducts(); // 如果不存在产品,则跳转到产品创建页面。
  1087. if($_POST)
  1088. {
  1089. /* 获取上传的文件。 */
  1090. /* Get file. */
  1091. $file = $this->loadModel('file')->getUpload('file');
  1092. if(empty($_FILES)) return $this->send(array('result' => 'fail', 'message' => $this->lang->file->errorFileFormat));
  1093. if(empty($file[0])) return $this->send(array('result' => 'fail', 'message' => $this->lang->testcase->errorFileNotEmpty));
  1094. $file = $file[0];
  1095. if(!$file || (isset($file['extension']) && $file['extension'] != 'csv')) return $this->send(array('result' => 'fail', 'message' => $this->lang->file->errorFileFormat));
  1096. /* 移动上传的文件。 */
  1097. /* Move file. */
  1098. $fileName = $this->file->savePath . $this->file->getSaveName($file['pathname']);
  1099. move_uploaded_file($file['tmpname'], $fileName);
  1100. /* 获取上传文件中的用例字段。 */
  1101. /* Get imported fields of the testcase. */
  1102. $fields = $this->testcase->getImportFields($productID);
  1103. $fields = array_flip($fields);
  1104. /* 获取字段列值。 */
  1105. /* Get column key. */
  1106. $columnKey = $this->testcaseZen->processImportColumnKey($fileName, $fields);
  1107. /* 如果字段列少于3个,或者编码非utf-8,再次计算字段列值。 */
  1108. /* If there are less than 3 column keys, or if the encoding is not utf-8, process the column key again. */
  1109. if(count($columnKey) <= 3 || $this->post->encode != 'utf-8')
  1110. {
  1111. /* 转换编码. */
  1112. /* Convert Encoding. */
  1113. $encode = $this->post->encode != "utf-8" ? $this->post->encode : 'gbk';
  1114. $fc = file_get_contents($fileName);
  1115. $fc = helper::convertEncoding($fc, $encode, 'utf-8');
  1116. file_put_contents($fileName, $fc);
  1117. /* 获取字段列值。 */
  1118. /* Get column key. */
  1119. $columnKey = $this->testcaseZen->processImportColumnKey($fileName, $fields);
  1120. if(count($columnKey) == 0) return $this->send(array('result' => 'fail', 'message' => $this->lang->testcase->errorEncode));
  1121. }
  1122. $this->session->set('fileImport', $fileName);
  1123. return $this->send(array('result' => 'success', 'load' => inlink('showImport', "productID={$productID}&branch={$branch}"), 'closeModal' => true));
  1124. }
  1125. $this->display();
  1126. }
  1127. /**
  1128. * Import case from lib.
  1129. *
  1130. * @param int $productID
  1131. * @param string $branch
  1132. * @param int $libID
  1133. * @param string $orderBy
  1134. * @param int $recTotal
  1135. * @param int $recPerPage
  1136. * @param int $pageID
  1137. * @access public
  1138. * @return void
  1139. * @param string $browseType
  1140. * @param int $queryID
  1141. * @param int $projectID
  1142. */
  1143. public function importFromLib($productID, $branch = '0', $libID = 0, $orderBy = 'id_desc', $browseType = '', $queryID = 0, $recTotal = 0, $recPerPage = 20, $pageID = 1, $projectID = 0)
  1144. {
  1145. $this->testcaseZen->checkProducts(); // 如果不存在产品,则跳转到产品创建页面。
  1146. /* 获取用例库,设置用例库 id 和分支。 */
  1147. /* Get libraries, set lib id and branch. */
  1148. $libraries = $this->loadModel('caselib')->getLibraries();
  1149. if(empty($libraries)) return $this->send(array('result' => 'fail', 'load' => array('alert' => $this->lang->testcase->noLibrary)));
  1150. if(empty($libID) || !isset($libraries[$libID])) $libID = key($libraries);
  1151. if($branch == '') $branch = 0;
  1152. if($_POST)
  1153. {
  1154. list($cases, $steps, $files, $hasImported) = $this->testcaseZen->buildDataForImportFromLib($productID, $branch, $libID);
  1155. $this->loadModel('action');
  1156. $errors = '';
  1157. $importedCases = array();
  1158. foreach($cases as $oldCaseID => $case)
  1159. {
  1160. $this->config->testcase->create->requiredFields = strpos(",{$this->config->testcase->create->requiredFields},", ',module,') !== false ? ',module,' : '';
  1161. $this->testcase->doCreate($case);
  1162. if(dao::isError())
  1163. {
  1164. $errors .= "{$oldCaseID}:";
  1165. foreach(dao::getError() as $fieldErrors) $errors .= implode(',', $fieldErrors);
  1166. continue;
  1167. }
  1168. $caseID = $this->dao->lastInsertID();
  1169. $this->executeHooks($caseID);
  1170. $this->testcase->syncCase2Project($case, $caseID);
  1171. $this->testcase->importSteps($caseID, zget($steps, $oldCaseID, array()));
  1172. $this->testcase->importFiles($caseID, zget($files, $oldCaseID, array()));
  1173. $importedCases[] = $oldCaseID;
  1174. $this->action->create('case', $caseID, 'fromlib', '', $libID);
  1175. }
  1176. if(!empty($errors)) return $this->send(array('result' => 'fail', 'callback' => "zui.Modal.alert('{$errors}');"));
  1177. if(!empty($importedCases)) $this->send(array('result' => 'fail', 'message' => sprintf($this->lang->testcase->importedFromLib, count($importedCases), implode(',', $importedCases)), 'load' => true));
  1178. if(!empty($hasImported) && is_string($hasImported)) return $this->send(array('result' => 'fail', 'message' => sprintf($this->lang->testcase->importedCases, trim($hasImported, ',')), 'load' => true));
  1179. return $this->send(array('result' => 'success', 'message' => $this->lang->importSuccess, 'load' => true));
  1180. }
  1181. /* 设置菜单。 */
  1182. /* Set menu. */
  1183. $this->testcaseZen->setMenu((int)$this->session->project, 0, $productID, $branch);
  1184. $browseType = strtolower($browseType);
  1185. /* Load pager. */
  1186. $this->app->loadClass('pager', $static = true);
  1187. $pager = pager::init(0, $recPerPage, $pageID);
  1188. $cases = $this->testcase->getCanImportCases($productID, $libID, $orderBy, $pager, $browseType, (int)$queryID);
  1189. /* 展示变量. */
  1190. /* Show the variables. */
  1191. $this->testcaseZen->assignForImportFromLib($productID, $branch, $libID, $orderBy, $queryID, $libraries, $projectID, $cases);
  1192. $this->view->title = $this->lang->testcase->common . $this->lang->hyphen . $this->lang->testcase->importFromLib;
  1193. $this->view->libraries = $libraries;
  1194. $this->view->cases = $cases;
  1195. $this->view->libModules = $this->tree->getOptionMenu($libID, 'caselib');
  1196. $this->view->pager = $pager;
  1197. $this->view->browseType = $browseType;
  1198. $this->display();
  1199. }
  1200. /**
  1201. * 展示导入的数据。
  1202. * Show import data.
  1203. *
  1204. * @param int $productID
  1205. * @param string $branch
  1206. * @param int $pagerID
  1207. * @param int $maxImport
  1208. * @param string $insert 0 is covered old, 1 is insert new.
  1209. * @access public
  1210. * @return void
  1211. */
  1212. public function showImport($productID, $branch = '0', $pagerID = 1, $maxImport = 0, $insert = '')
  1213. {
  1214. $this->testcaseZen->checkProducts(); // 如果不存在产品,则跳转到产品创建页面。
  1215. /* Get file information. */
  1216. $file = $this->session->fileImport;
  1217. $tmpPath = $this->loadModel('file')->getPathOfImportedFile();
  1218. $tmpFile = $tmpPath . DS . md5(basename($file));
  1219. if($_POST)
  1220. {
  1221. $cases = $this->testcaseZen->buildCasesForShowImport($productID);
  1222. $cases = $this->testcaseZen->checkCasesForShowImport($cases);
  1223. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  1224. $cases = $this->testcaseZen->importCases($cases);
  1225. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  1226. return $this->testcaseZen->responseAfterShowImport($productID, $branch, $maxImport, $tmpFile);
  1227. }
  1228. $this->testcaseZen->setMenu((int)$this->session->project, 0, $productID, $branch);
  1229. /* Get imported data. */
  1230. if(!empty($maxImport) && file_exists($tmpFile))
  1231. {
  1232. $data = unserialize(file_get_contents($tmpFile));
  1233. }
  1234. else
  1235. {
  1236. $pagerID = 1;
  1237. list($data, $stepVars) = $this->testcaseZen->getImportedData($productID, $file);
  1238. file_put_contents($tmpFile, serialize($data));
  1239. }
  1240. $this->testcaseZen->assignShowImportVars($productID, $branch, $data['caseData'], isset($stepVars) ? $stepVars : 0, $pagerID, $maxImport);
  1241. $this->view->title = $this->lang->testcase->common . $this->lang->hyphen . $this->lang->testcase->showImport;
  1242. $this->view->cases = $this->testcase->getByProduct($productID);
  1243. $this->view->productID = $productID;
  1244. $this->view->branch = $branch;
  1245. $this->view->product = $this->product->getByID($productID);
  1246. $this->view->maxImport = $maxImport;
  1247. $this->view->dataInsert = $insert;
  1248. $this->display();
  1249. }
  1250. /**
  1251. * 导入用例到用例库。
  1252. * Import cases to library.
  1253. *
  1254. * @param int $caseID
  1255. * @access public
  1256. * @return void
  1257. */
  1258. public function importToLib($caseID = 0)
  1259. {
  1260. if(!empty($_POST))
  1261. {
  1262. $libID = (int)$this->post->lib;
  1263. if(empty($libID))
  1264. {
  1265. dao::$errors['lib'][] = sprintf($this->lang->error->notempty, $this->lang->testcase->caselib);
  1266. return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  1267. }
  1268. list($cases, $steps, $files) = $this->testcaseZen->buildDataForImportToLib($caseID, $libID);
  1269. $this->testcase->importToLib($cases, $steps, $files);
  1270. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  1271. if(!empty($caseID)) return $this->send(array('result' => 'success', 'message' => $this->lang->importSuccess, 'closeModal' => true));
  1272. return $this->send(array('result' => 'success', 'message' => $this->lang->importSuccess, 'load' => true, 'closeModal' => true));
  1273. }
  1274. $this->view->title = $this->lang->testcase->importToLib;
  1275. $this->view->libraries = $this->loadModel('caselib')->getLibraries();
  1276. $this->display();
  1277. }
  1278. /**
  1279. * 用例产生的 bugs。
  1280. * Case bugs.
  1281. *
  1282. * @param int $runID
  1283. * @param int $caseID
  1284. * @param int $version
  1285. * @param string $orderBy
  1286. * @access public
  1287. * @return void
  1288. */
  1289. public function bugs($runID, $caseID = 0, $version = 0, $orderBy = 'id_asc')
  1290. {
  1291. $this->view->title = $this->lang->testcase->bugs;
  1292. $this->view->bugs = $this->loadModel('bug')->getCaseBugs($runID, $caseID, $version, $orderBy);
  1293. $this->view->users = $this->loadModel('user')->getPairs('noletter');
  1294. $this->view->orderBy = $orderBy;
  1295. $this->display();
  1296. }
  1297. /**
  1298. * 查看自动化脚本。
  1299. * Show script.
  1300. *
  1301. * @param int $caseID
  1302. * @access public
  1303. * @return void
  1304. */
  1305. public function showScript($caseID)
  1306. {
  1307. $case = $this->testcase->getByID($caseID);
  1308. if($case) $case->script = html_entity_decode($case->script);
  1309. $this->view->case = $case;
  1310. $this->display();
  1311. }
  1312. /**
  1313. * 自动化设置。
  1314. * Automation test setting.
  1315. *
  1316. * @param int $productID
  1317. * @access public
  1318. * @return void
  1319. */
  1320. public function automation($productID = 0)
  1321. {
  1322. $this->testcaseZen->checkProducts(); // 如果不存在产品,则跳转到产品创建页面。
  1323. if($_POST)
  1324. {
  1325. /* 设置语言项以便 form 类检查必填项时输出正确的字段名。*/
  1326. /* Set language item for form class to check required fields. */
  1327. $this->loadModel('zanode');
  1328. $this->lang->testcase->scriptPath = $this->lang->zanode->scriptPath;
  1329. $this->lang->testcase->node = $this->lang->zanode->common;
  1330. $automation = form::data($this->config->testcase->form->automation)
  1331. ->add('createdBy', $this->app->user->account)
  1332. ->add('createddate', helper::now())
  1333. ->get();
  1334. $this->zanode->setAutomationSetting($automation);
  1335. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  1336. return $this->sendSuccess(array('load' => inlink('browse', "productID={$automation->product}")));
  1337. }
  1338. $this->view->title = $this->lang->testcase->automation;
  1339. $this->view->automation = $this->loadModel('zanode')->getAutomationByProduct($productID);
  1340. $this->view->nodeList = $this->zanode->getPairs();
  1341. $this->view->products = $productID ? array() : $this->product->getPairs('', 0, '', 'all');
  1342. $this->view->productID = $productID;
  1343. $this->display();
  1344. }
  1345. /**
  1346. * 获取待审核的用例数。
  1347. * Ajax: Get amount of casees pending review.
  1348. *
  1349. * @access public
  1350. * @return int
  1351. */
  1352. public function ajaxGetReviewAmount()
  1353. {
  1354. echo $this->testcaseTao->getReviewAmount();
  1355. }
  1356. /**
  1357. * Create scene.
  1358. *
  1359. * @param int $productID
  1360. * @param string $branch
  1361. * @param int $moduleID
  1362. * @access public
  1363. * @return void
  1364. */
  1365. public function createScene($productID, $branch = '', $moduleID = 0)
  1366. {
  1367. $this->testcaseZen->checkProducts(); // 如果不存在产品,则跳转到产品创建页面。
  1368. if($_POST)
  1369. {
  1370. $this->lang->testcase->title = $this->lang->testcase->sceneTitle;
  1371. $scene = form::data($this->config->testcase->form->createScene)->get();
  1372. $this->testcase->createScene($scene);
  1373. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  1374. /* 记录父场景 ID,便于下次创建场景时默认选中父场景。*/
  1375. /* Record the ID of the parent scene, so that the parent scene will be selected by default when creating a scene next time. */
  1376. helper::setcookie('lastCaseScene', $scene->parent);
  1377. $locate = $this->session->caseList ?: inlink('browse', "productID={$scene->product}&branch={$scene->branch}&browseType=all&param={$scene->module}");
  1378. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $locate));
  1379. }
  1380. $this->assignCreateSceneVars($productID, $branch, $moduleID);
  1381. $this->display();
  1382. }
  1383. /**
  1384. * Ajax get module scenes.
  1385. *
  1386. * @param int $productID
  1387. * @param string $branch
  1388. * @param int $moduleID
  1389. * @param int $sceneID
  1390. * @access public
  1391. * @return void
  1392. */
  1393. public function ajaxGetScenes($productID, $branch = 'all', $moduleID = 0, $sceneID = 0)
  1394. {
  1395. if(empty($branch)) $branch = 'all';
  1396. $optionMenu = $this->testcase->getSceneMenu($productID, $moduleID, $branch, 0, $sceneID);
  1397. $items = array();
  1398. foreach($optionMenu as $optionID => $optionName) $items[] = array('text' => $optionName, 'value' => $optionID);
  1399. return $this->send($items);
  1400. }
  1401. /**
  1402. * 通过 ajax 方式获取模块选项。
  1403. * Ajax get option menu.
  1404. *
  1405. * @param int $rootID
  1406. * @param int $branch
  1407. * @param int $rootModuleID
  1408. * @param string $returnType
  1409. * @param string $fieldID
  1410. * @access public
  1411. * @return void
  1412. */
  1413. public function ajaxGetOptionMenu($rootID, $branch = 0, $rootModuleID = 0, $returnType = 'html', $fieldID = '')
  1414. {
  1415. $optionMenu = $this->loadModel('tree')->getOptionMenu($rootID, 'case', $rootModuleID, $branch);
  1416. if($returnType == 'mhtml')
  1417. {
  1418. $field = $fieldID ? "modules[$fieldID]" : 'module';
  1419. $output = html::select("$field", $optionMenu, '', "class='input'");
  1420. die($output);
  1421. }
  1422. if($returnType == 'json') die(json_encode($optionMenu));
  1423. $items = array();
  1424. foreach($optionMenu as $optionID => $optionName) $items[] = array('text' => $optionName, 'value' => $optionID);
  1425. return print(json_encode($items));
  1426. }
  1427. /**
  1428. * Batch change scene.
  1429. *
  1430. * @param int $sceneID
  1431. * @access public
  1432. * @return void
  1433. */
  1434. public function batchChangeScene($sceneID)
  1435. {
  1436. $caseIdList = zget($_POST, 'caseIdList', array());
  1437. if($caseIdList)
  1438. {
  1439. $this->testcase->batchChangeScene($caseIdList, $sceneID);
  1440. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  1441. }
  1442. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $this->session->caseList));
  1443. }
  1444. /**
  1445. * Change scene.
  1446. *
  1447. * @access public
  1448. * @return void
  1449. */
  1450. public function changeScene()
  1451. {
  1452. $sourceID = $this->post->sourceID;
  1453. $sceneID = $this->post->targetID;
  1454. if($sourceID == $sceneID) return $this->send(array('result' => 'success', 'load' => true));
  1455. if(strpos($sourceID, 'case_') !== false)
  1456. {
  1457. $caseID = str_replace('case_', '', $sourceID);
  1458. $oldCase = $this->dao->select('scene')->from(TABLE_CASE)->where('id')->eq($caseID)->fetch();
  1459. if($oldCase->scene == $sceneID) return $this->send(array('result' => 'success', 'load' => true));
  1460. $this->dao->update(TABLE_CASE)->set('scene')->eq($sceneID)->where('id')->eq($caseID)->exec();
  1461. if(dao::isError()) $this->send(array('result' => 'fail', 'message' => dao::getError()));
  1462. $newCase = new stdclass();
  1463. $newCase->scene = $sceneID;
  1464. $changes = common::createChanges($oldCase, $newCase);
  1465. $actionID = $this->loadModel('action')->create('case', (int)$caseID, 'edited');
  1466. $this->action->logHistory($actionID, $changes);
  1467. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => true));
  1468. }
  1469. $oldScene = $this->testcase->getSceneByID((int)$sourceID);
  1470. $newScene = $this->testcase->getSceneByID((int)$sceneID);
  1471. $oldParentPath = substr($oldScene->path, 0, strpos($oldScene->path, ",{$oldScene->id},") + strlen(",{$oldScene->id},"));
  1472. $this->dao->update(TABLE_SCENE)->set('parent')->eq($sceneID)->where('id')->eq($oldScene->id)->exec();
  1473. $this->dao->update(TABLE_SCENE)
  1474. ->set('product')->eq($newScene->product)
  1475. ->set('branch')->eq($newScene->branch)
  1476. ->set('module')->eq($newScene->module)
  1477. ->set("grade=grade + {$newScene->grade} + 1 - {$oldScene->grade}")
  1478. ->set("path=REPLACE(path, '{$oldParentPath}', '{$newScene->path}{$oldScene->id},')")
  1479. ->where('path')->like("{$oldScene->path}%")
  1480. ->exec();
  1481. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => true));
  1482. }
  1483. /**
  1484. * Delete a scene and its children.
  1485. *
  1486. * @param int $sceneID
  1487. * @param string $confirm
  1488. * @access public
  1489. * @return void
  1490. */
  1491. public function deleteScene($sceneID, $confirm = 'no')
  1492. {
  1493. $scene = $this->testcase->getSceneByID($sceneID);
  1494. $count = $this->dao->select('COUNT(1) AS count')->from(TABLE_SCENE)->where('deleted')->eq('0')->andWhere('parent')->eq($sceneID)->fetch('count');
  1495. if($count)
  1496. {
  1497. if($confirm != 'yes')
  1498. {
  1499. $confirmURL = inlink('deleteScene', "sceneID={$sceneID}&confirm=yes");
  1500. return $this->send(array('result' => 'fail', 'callback' => "zui.Modal.confirm({message: '{$this->lang->testcase->hasChildren}', icon: 'icon-exclamation-sign', iconClass: 'warning-pale rounded-full icon-2x'}).then((res) => {if(res) $.ajaxSubmit({url: '{$confirmURL}'});});"));
  1501. }
  1502. $this->loadModel('action');
  1503. $scenes = $this->dao->select('id')->from(TABLE_SCENE)->where('deleted')->eq('0')->andWhere('path')->like($scene->path . '%')->fetchPairs();
  1504. $this->dao->update(TABLE_SCENE)->set('deleted')->eq('1')->where('deleted')->eq(0)->andWhere('path')->like($scene->path . '%')->exec();
  1505. foreach($scenes as $sceneID) $this->action->create('scene', $sceneID, 'deleted', '', actionModel::CAN_UNDELETED);
  1506. }
  1507. else
  1508. {
  1509. if($confirm != 'yes')
  1510. {
  1511. $confirmURL = inlink('deleteScene', "sceneID={$sceneID}&confirm=yes");
  1512. $sceneDeleteConfirmMessage = sprintf($this->lang->testcase->confirmDeleteScene, $scene->title);
  1513. return $this->send(array('result' => 'fail', 'callback' => "zui.Modal.confirm({message: '{$sceneDeleteConfirmMessage}', icon: 'icon-exclamation-sign', iconClass: 'warning-pale rounded-full icon-2x'}).then((res) => {if(res) $.ajaxSubmit({url: '{$confirmURL}'});});"));
  1514. }
  1515. $this->testcase->delete(TABLE_SCENE, $sceneID);
  1516. }
  1517. $locateLink = $this->session->caseList ? $this->session->caseList : inlink('browse', "productID={$scene->product}");
  1518. return $this->send(array('result' => 'success', 'load' => $locateLink));
  1519. }
  1520. /**
  1521. * 编辑一个场景。
  1522. * Edit a scene.
  1523. *
  1524. * @param int $sceneID
  1525. * @access public
  1526. * @return void
  1527. */
  1528. public function editScene($sceneID)
  1529. {
  1530. $oldScene = $this->testcase->getSceneByID($sceneID);
  1531. if(!$oldScene)
  1532. {
  1533. $browseLink = $this->session->caseList ? $this->session->caseList : inlink('browse');
  1534. return $this->send(array('result' => 'fail', 'message' => $this->lang->notFound, 'load' => $browseLink));
  1535. }
  1536. if($_POST)
  1537. {
  1538. $this->lang->testcase->title = $this->lang->testcase->sceneTitle;
  1539. $scene = form::data($this->config->testcase->form->editScene)->get();
  1540. $this->testcase->updateScene($scene, $oldScene);
  1541. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  1542. if(defined('RUN_MODE') && RUN_MODE == 'api') return $this->send(array('status' => 'success', 'data' => $sceneID));
  1543. $locate = $this->session->caseList ? $this->session->caseList : inlink('browse', "productID={$scene->product}&branch={$scene->branch}&browseType=all&param={$scene->module}");
  1544. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $locate));
  1545. }
  1546. $this->assignEditSceneVars($oldScene);
  1547. $this->display();
  1548. }
  1549. /**
  1550. * Update order.
  1551. *
  1552. * @access public
  1553. * @return void
  1554. */
  1555. public function updateOrder()
  1556. {
  1557. $sourceID = (int)$this->post->sourceID;
  1558. $sourceOrder = (int)$this->post->sourceOrder;
  1559. $targetID = (int)$this->post->targetID;
  1560. $targetOrder = (int)$this->post->targetOrder;
  1561. $type = $this->post->type;
  1562. $module = $this->post->module;
  1563. $table = $module == 'case' ? TABLE_CASE : TABLE_SCENE;
  1564. $target = $this->testcase->fetchByID($targetID, $module);
  1565. $sortList = $this->dao->select('id,sort')->from($table)
  1566. ->where($module == 'case' ? 'scene' : 'parent')->eq($module == 'case' ? $target->scene : $target->parent)
  1567. ->andWhere('product')->eq($target->product)
  1568. ->orderBy('sort_asc,id_desc')
  1569. ->fetchPairs();
  1570. $sortList = array_keys($sortList);
  1571. $sourceIndex = array_search($sourceID, $sortList); // 获取拖动排序对象的索引。
  1572. $sourceData = array_splice($sortList, $sourceIndex, 1); // 将拖动排序的对象从数组移除。
  1573. $targetIndex = array_search($targetID, $sortList); // 获取拖动后排序对象的索引。
  1574. array_splice($sortList, $type == 'before' ? $targetIndex : ($targetIndex + 1), 0, $sourceData); // 将排序的对象放置在拖动后排序对象的前面或者后面。
  1575. foreach($sortList as $sort => $objectID) $this->dao->update($table)->set('sort')->eq($sort)->where('id')->eq($objectID)->exec();
  1576. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => true));
  1577. }
  1578. /**
  1579. * 导出 xmind 格式的用例。
  1580. * Export xmind.
  1581. *
  1582. * @param int $productID
  1583. * @param int $moduleID
  1584. * @param string $branch
  1585. * @access public
  1586. * @return void
  1587. */
  1588. public function exportXmind($productID, $moduleID, $branch)
  1589. {
  1590. if($_POST)
  1591. {
  1592. $configList = $this->testcaseZen->buildMindConfig('xmind');
  1593. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  1594. $this->testcase->saveMindConfig('xmind', $configList);
  1595. $imoduleID = $this->post->imodule ? $this->post->imodule : 0;
  1596. $context = $this->testcaseZen->getMindExport('xmind', $productID, (int)$imoduleID, $branch);
  1597. $productName = '';
  1598. if(count($context['caseList']))
  1599. {
  1600. $productName = $context['caseList'][0]->productName;
  1601. }
  1602. else
  1603. {
  1604. $product = $this->product->getByID($productID);
  1605. $productName = $product->name;
  1606. }
  1607. $fileData = $this->testcaseZen->getXmindExportData($productID, $productName, $context);
  1608. $this->fetch('file', 'sendDownHeader', array('fileName' => $productName, 'xmind', $fileData));
  1609. }
  1610. $product = $this->product->fetchByID($productID);
  1611. $this->view->settings = $this->testcase->getMindConfig('freemind');
  1612. $this->view->productName = $product->name;
  1613. $this->view->moduleID = $moduleID;
  1614. $this->view->moduleOptionMenu = $this->tree->getOptionMenu($productID, 'case', 0, ($branch === 'all' || !isset($branches[$branch])) ? '0' : $branch);
  1615. $this->display();
  1616. }
  1617. /**
  1618. * 导出 xmind 格式的用例。
  1619. * Export xmind.
  1620. *
  1621. * @param int $productID
  1622. * @param int $moduleID
  1623. * @param string $branch
  1624. * @access public
  1625. * @return void
  1626. */
  1627. public function exportFreeMind($productID, $moduleID, $branch)
  1628. {
  1629. if($_POST)
  1630. {
  1631. $configList = $this->testcaseZen->buildMindConfig('freemind');
  1632. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  1633. $this->testcase->saveMindConfig('freemind', $configList);
  1634. $imoduleID = $this->post->imodule ? $this->post->imodule : 0;
  1635. $context = $this->testcaseZen->getMindExport('freemind', $productID, (int)$imoduleID, $branch);
  1636. $productName = '';
  1637. if(count($context['caseList']))
  1638. {
  1639. $productName = $context['caseList'][0]->productName;
  1640. }
  1641. else
  1642. {
  1643. $product = $this->product->getByID($productID);
  1644. $productName = $product->name;
  1645. }
  1646. $xmlDoc = $this->testcaseZen->createFreeMindXmlDoc($productID, $productName, $context);
  1647. $xmlStr = $xmlDoc->saveXML();
  1648. $this->fetch('file', 'sendDownHeader', array('fileName' => $productName, 'mm', $xmlStr));
  1649. }
  1650. $product = $this->product->fetchByID($productID);
  1651. $this->view->settings = $this->testcase->getMindConfig('freemind');
  1652. $this->view->productName = $product->name;
  1653. $this->view->moduleID = $moduleID;
  1654. $this->view->moduleOptionMenu = $this->tree->getOptionMenu($productID, 'case', 0, ($branch === 'all' || !isset($branches[$branch])) ? '0' : $branch);
  1655. $this->display();
  1656. }
  1657. /**
  1658. * 获取 xmind 的配置项。
  1659. * Get xmind config.
  1660. *
  1661. * @access public
  1662. * @return void
  1663. */
  1664. public function getXmindConfig()
  1665. {
  1666. $result = $this->testcase->getMindConfig('xmind');
  1667. $this->send($result);
  1668. }
  1669. /**
  1670. * 获取 xmind 文件内容。
  1671. * Get xmind content.
  1672. *
  1673. * @access public
  1674. * @return void
  1675. */
  1676. public function getXmindImport()
  1677. {
  1678. if(!commonModel::hasPriv('testcase', 'importXmind')) $this->loadModel('common')->deny('testcase', 'importXmind');
  1679. if($this->session->xmindImportType == 'xml')
  1680. {
  1681. $xmlPath = $this->session->xmindImport . '/content.xml';
  1682. $results = str_replace(array('<', '>'), array('&lt;', '&gt;'), $this->testcase->getXmindImport($xmlPath));
  1683. }
  1684. else
  1685. {
  1686. $jsonPath = $this->session->xmindImport . '/content.json';
  1687. $results = file_get_contents($jsonPath);
  1688. }
  1689. echo $results;
  1690. }
  1691. /**
  1692. * 导入 xmind.
  1693. * Import xmind.
  1694. *
  1695. * @param int $productID
  1696. * @param string $branch
  1697. * @access public
  1698. * @return void
  1699. */
  1700. public function importXmind($productID, $branch)
  1701. {
  1702. if($_POST)
  1703. {
  1704. $file = $this->loadModel('file')->getUpload('file');
  1705. if(empty($_FILES)) return $this->send(array('result' => 'fail', 'message' => $this->lang->file->errorFileFormat));
  1706. if(empty($file[0])) return $this->send(array('result' => 'fail', 'message' => $this->lang->testcase->errorFileNotEmpty));
  1707. $file = $file[0];
  1708. if(!$file || (isset($file['extension']) && $file['extension'] != 'xmind')) return $this->send(array('result' => 'fail', 'message' => $this->lang->file->errorFileFormat));
  1709. /* 保存xmind配置。*/
  1710. /* Sav xmind config. */
  1711. $configList = $this->testcaseZen->buildMindConfig('xmind');
  1712. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  1713. $this->testcase->saveMindConfig('xmind', $configList);
  1714. /* 检查扩展名。*/
  1715. /* Check extension name of file. */
  1716. $extName = trim(strtolower(pathinfo($_FILES['file']['name'][0], PATHINFO_EXTENSION)));
  1717. if($extName != 'xmind') return $this->send(array('result' => 'fail', 'message' => $this->lang->testcase->errorFileFormat));
  1718. $result = $this->testcaseZen->parseUploadFile($productID, $branch);
  1719. if(is_array($result)) return $this->send($result);
  1720. return $this->send(array('result' => 'success', 'load' => $this->createLink('testcase', 'showXmindImport', "productID=$result&branch=$branch"), 'closeModal' => true));
  1721. }
  1722. $this->view->settings = $this->testcase->getMindConfig('xmind');
  1723. $this->display();
  1724. }
  1725. /**
  1726. * 保存导入的 xmind 文件。
  1727. * Save imported xmind.
  1728. *
  1729. * @access public
  1730. * @return void
  1731. */
  1732. public function saveXmindImport()
  1733. {
  1734. if(!commonModel::hasPriv('testcase', 'importXmind')) $this->loadModel('common')->deny('testcase', 'importXmind');
  1735. if(!empty($_POST))
  1736. {
  1737. $productID = (int)$this->post->productID;
  1738. $branch = $this->post->branch;
  1739. $isInsert = (bool)$this->post->insert;
  1740. $sceneList = json_decode($this->post->sceneList, true);
  1741. $testcaseList = json_decode($this->post->testcaseList, true);
  1742. $cases = $this->testcaseZen->buildCasesByXmind($productID, $branch, $testcaseList, $isInsert);
  1743. $result = $this->testcase->saveXmindImport($sceneList, $cases);
  1744. if($result['result'] == 'success') $result['load'] = $this->app->tab == 'project' ? $this->createLink('project', 'testcase', "projectID={$this->session->project}&productID={$productID}") : inlink('browse', "productID={$productID}");
  1745. return $this->send($result);
  1746. }
  1747. return $this->send(array('result' => 'fail', 'message' => $this->lang->errorSaveXmind));
  1748. }
  1749. /**
  1750. * 展示导入的 xmind 内容。
  1751. * Show imported xmind.
  1752. *
  1753. * @param int $productID
  1754. * @param string $branch
  1755. * @access public
  1756. * @return void
  1757. */
  1758. public function showXmindImport($productID, $branch)
  1759. {
  1760. if(!commonModel::hasPriv('testcase', 'importXmind')) $this->loadModel('common')->deny('testcase', 'importXmind');
  1761. /* Set menu. */
  1762. $this->testcaseZen->setMenu($this->session->project, 0, $productID, $branch);
  1763. $product = $this->product->getByID($productID);
  1764. $branches = (isset($product->type) && $product->type != 'normal') ? $this->loadModel('branch')->getPairs($productID, 'active') : array();
  1765. if($this->session->xmindImportType == 'xml')
  1766. {
  1767. $xmlPath = $this->session->xmindImport . '/content.xml';
  1768. $results = str_replace(array('<', '>'), array('&lt;', '&gt;'), $this->testcase->getXmindImport($xmlPath));
  1769. }
  1770. else
  1771. {
  1772. $jsonPath = $this->session->xmindImport . '/content.json';
  1773. $results = file_get_contents($jsonPath);
  1774. }
  1775. $results = json_decode($results, true);
  1776. $scenes = array();
  1777. if(!empty($results[0]['rootTopic'])) $scenes = $this->testcaseZen->processScene($results[0]['rootTopic']);
  1778. if(!empty($results['xmap-content']['sheet']['topic'])) $scenes = $this->testcaseZen->processScene($results['xmap-content']['sheet']['topic']);
  1779. $this->view->title = $this->lang->testcase->xmindImport;
  1780. $this->view->settings = $this->testcase->getMindConfig('xmind');
  1781. $this->view->product = $product;
  1782. $this->view->productID = $productID;
  1783. $this->view->branch = $branch;
  1784. $this->view->scenes = $scenes;
  1785. $this->view->moduleOptionMenu = $this->tree->getOptionMenu($productID, 'case', 0, ($branch === 'all' or !isset($branches[$branch])) ? '0' : $branch);
  1786. $this->display();
  1787. }
  1788. /**
  1789. * AJAX: 获取用例对应分支的模块。
  1790. * AJAX: Get module items for case.
  1791. *
  1792. * @param int $productID
  1793. * @param int $libID
  1794. * @param int $branch
  1795. * @param int $caseID
  1796. * @access public
  1797. * @return json
  1798. */
  1799. public function ajaxGetCanImportModuleItems($productID, $libID, $branch, $caseID)
  1800. {
  1801. $moduleItems = $this->testcase->getCanImportedModules($productID, $libID, $branch, 'items', array($caseID => $caseID));
  1802. $caseModuleItmes = isset($moduleItems[$caseID]) ? $moduleItems[$caseID] : array();
  1803. return print(json_encode($caseModuleItmes));
  1804. }
  1805. /**
  1806. * AJAX: 获取用例对应产品模块的场景。
  1807. * AJAX: Get scenes items for case.
  1808. *
  1809. * @param int $productID
  1810. * @param int $moduleID
  1811. * @param string $branch
  1812. * @access public
  1813. * @return json
  1814. */
  1815. public function ajaxGetProductScenes($productID, $moduleID, $branch)
  1816. {
  1817. $items = array();
  1818. $branches = $this->loadModel('branch')->getPairs($productID);
  1819. $scenes = $this->testcase->getSceneMenu($productID, $moduleID, ($branch === 'all' || !isset($branches[$branch])) ? 'all' : (string)$branch);
  1820. foreach($scenes as $sceneID => $sceneName) $items[] = array('text' => $sceneName, 'value' => $sceneID);
  1821. return print(json_encode($items));
  1822. }
  1823. }