PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/requirements/reqTcAssign.php

https://bitbucket.org/pfernandez/testlink1.9.6
PHP | 329 lines | 248 code | 43 blank | 38 comment | 33 complexity | 9823878fef19f24fab139502dfe1b638 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * TestLink Open Source Project - http://testlink.sourceforge.net/
  4. *
  5. * @filesource $RCSfile: reqTcAssign.php,v $
  6. * @version $Revision: 1.21 $
  7. * @modified $Date: 2010/06/24 17:25:55 $ $Author: asimon83 $
  8. *
  9. * @author Martin Havlat
  10. *
  11. * 20100602 - franciscom - BUGID 3495 - Requirements Bulk Assignment crash. (typo error)
  12. * 20100408 - franciscom - BUGID 3361 - FatalError after trying to assign requirements to an empty test suite
  13. * 20081130 - franciscom - BUGID 1852 - Bulk Assignment Feature
  14. *
  15. **/
  16. require_once("../../config.inc.php");
  17. require_once("common.php");
  18. require_once('requirements.inc.php');
  19. testlinkInitPage($db,false,false,"checkRights");
  20. $templateCfg = templateConfiguration();
  21. $args = init_args();
  22. $gui = new stdClass();
  23. $gui->showCloseButton = $args->showCloseButton;
  24. $gui->user_feedback = '';
  25. $gui->tcTitle = null;
  26. $gui->arrAssignedReq = null;
  27. $gui->arrUnassignedReq = null;
  28. $gui->arrReqSpec = null;
  29. $gui->selectedReqSpec = $args->idReqSpec;
  30. $bulkCounter = 0;
  31. $bulkDone = false;
  32. $bulkMsg = null;
  33. $pfn = null;
  34. switch($args->doAction)
  35. {
  36. case 'assign':
  37. $pfn = "assign_to_tcase";
  38. break;
  39. case 'unassign':
  40. $pfn = "unassign_from_tcase";
  41. break;
  42. case 'bulkassign':
  43. // BUGID 3361 - need to check if we have test cases to work on
  44. // BUGID 3495 - Requirements Bulk Assignment crash. (typo error) (dbHandler -> db)
  45. $tsuite_mgr = new testsuite($db);
  46. $tcase_set = $tsuite_mgr->get_testcases_deep($args->id,'only_id');
  47. $bulkCounter = 0;
  48. $bulkDone = true;
  49. $args->edit = 'testsuite';
  50. if( !is_null($tcase_set) && count($tcase_set) > 0 )
  51. {
  52. $bulkCounter = doBulkAssignment($db,$args,$tcase_set);
  53. }
  54. break;
  55. case 'switchspec':
  56. $args->edit = 'testsuite';
  57. break;
  58. }
  59. if(!is_null($pfn))
  60. {
  61. $gui = doSingleTestCaseOperation($db,$args,$gui,$pfn);
  62. }
  63. switch($args->edit)
  64. {
  65. case 'testproject':
  66. show_instructions('assignReqs');
  67. exit();
  68. break;
  69. case 'testsuite':
  70. $gui = processTestSuite($db,$args,$gui);
  71. $templateCfg->default_template = 'reqTcBulkAssignment.tpl';
  72. if($bulkDone)
  73. {
  74. $gui->user_feedback = sprintf(lang_get('bulk_assigment_done'),$bulkCounter);
  75. }
  76. break;
  77. case 'testcase':
  78. $gui = processTestCase($db,$args,$gui);
  79. break;
  80. default:
  81. tlog("Wrong GET/POST arguments.", 'ERROR');
  82. exit();
  83. break;
  84. }
  85. $smarty = new TLSmarty();
  86. $smarty->assign('gui', $gui);
  87. $smarty->assign('modify_req_rights', has_rights($db,"mgt_modify_req"));
  88. $smarty->display($templateCfg->template_dir . $templateCfg->default_template);
  89. function init_args()
  90. {
  91. $iParams = array("id" => array(tlInputParameter::INT_N),
  92. "req_id" => array(tlInputParameter::ARRAY_INT),
  93. "req" => array(tlInputParameter::INT_N),
  94. "showCloseButton" => array(tlInputParameter::STRING_N,0,1),
  95. "doAction" => array(tlInputParameter::STRING_N,0,100),
  96. "edit" => array(tlInputParameter::STRING_N,0,100),
  97. "unassign" => array(tlInputParameter::STRING_N,0,1),
  98. "assign" => array(tlInputParameter::STRING_N,0,1),
  99. "idSRS" => array(tlInputParameter::INT_N));
  100. $args = new stdClass();
  101. R_PARAMS($iParams,$args);
  102. $args->idReqSpec = null;
  103. $args->idReq = $args->req;
  104. $args->reqIdSet = $args->req_id;
  105. if(is_null($args->doAction))
  106. {
  107. $args->doAction = ($args->unassign != "") ? "unassign" : null;
  108. }
  109. if(is_null($args->doAction))
  110. {
  111. $args->doAction = ($args->assign != "") ? "assign" : null;
  112. }
  113. // 20081103 - sisajr - hold choosen SRS (saved for a session)
  114. if ($args->idSRS)
  115. {
  116. $args->idReqSpec = $args->idSRS;
  117. $_SESSION['currentSrsId'] = $args->idReqSpec;
  118. }
  119. else if(isset($_SESSION['currentSrsId']) && intval($_SESSION['currentSrsId']) > 0)
  120. {
  121. $args->idReqSpec = intval($_SESSION['currentSrsId']);
  122. }
  123. $args->tproject_id = isset($_SESSION['testprojectID']) ? $_SESSION['testprojectID'] : 0;
  124. return $args;
  125. }
  126. /**
  127. *
  128. *
  129. */
  130. function processTestSuite(&$dbHandler,&$argsObj,&$guiObj)
  131. {
  132. $tproject_mgr = new testproject($dbHandler);
  133. $guiObj->bulkassign_warning_msg = '';
  134. $guiObj->tsuite_id = $argsObj->id;
  135. $tsuite_info = $tproject_mgr->tree_manager->get_node_hierarchy_info($guiObj->tsuite_id);
  136. $guiObj->pageTitle = lang_get('test_suite') . config_get('gui_title_separator_1') . $tsuite_info['name'];
  137. $guiObj->req_specs = $tproject_mgr->getOptionReqSpec($argsObj->tproject_id,testproject::GET_NOT_EMPTY_REQSPEC);
  138. $guiObj->selectedReqSpec = $argsObj->idReqSpec;
  139. $guiObj->tcase_number = 0;
  140. $guiObj->has_req_spec = false;
  141. $guiObj->tsuite_id = $argsObj->id;
  142. if(!is_null($guiObj->req_specs) && count($guiObj->req_specs))
  143. {
  144. $guiObj->has_req_spec = true;
  145. if(is_null($argsObj->idReqSpec))
  146. {
  147. $guiObj->selectedReqSpec = key($guiObj->req_specs);
  148. }
  149. $req_spec_mgr = new requirement_spec_mgr($dbHandler);
  150. $guiObj->requirements =$req_spec_mgr->get_requirements($guiObj->selectedReqSpec);
  151. $tsuite_mgr = new testsuite($dbHandler);
  152. $tcase_set = $tsuite_mgr->get_testcases_deep($argsObj->id,'only_id');
  153. $guiObj->tcase_number = count($tcase_set);
  154. if( $guiObj->tcase_number > 0 )
  155. {
  156. $guiObj->bulkassign_warning_msg = sprintf(lang_get('bulk_req_assign_msg'),$guiObj->tcase_number);
  157. }
  158. else
  159. {
  160. $guiObj->bulkassign_warning_msg = lang_get('bulk_req_assign_no_test_cases');
  161. }
  162. }
  163. return $guiObj;
  164. }
  165. /**
  166. *
  167. *
  168. */
  169. function doBulkAssignment(&$dbHandler,&$argsObj,$targetTestCaseSet = null)
  170. {
  171. $req_mgr = new requirement_mgr($dbHandler);
  172. $assignmentCounter = 0;
  173. $requirements = array_keys($argsObj->reqIdSet);
  174. if(!is_null($requirements) && count($requirements) > 0)
  175. {
  176. $tcase_set = $targetTestCaseSet;
  177. if( is_null($tcase_set) )
  178. {
  179. $tsuite_mgr = new testsuite($dbHandler);
  180. echo 'DEBUG BEFORE';
  181. $tcase_set = $tsuite_mgr->get_testcases_deep($argsObj->id,'only_id');
  182. }
  183. if( !is_null($tcase_set) && count($tcase_set) )
  184. {
  185. $assignmentCounter = $req_mgr->bulk_assignment($requirements,$tcase_set);
  186. }
  187. }
  188. return $assignmentCounter;
  189. }
  190. function doSingleTestCaseOperation(&$dbHandler,&$argsObj,&$guiObj,$pfn)
  191. {
  192. $msg = '';
  193. $req_ids = array_keys($argsObj->reqIdSet);
  194. if (count($req_ids))
  195. {
  196. $req_mgr = new requirement_mgr($dbHandler);
  197. foreach ($req_ids as $idOneReq)
  198. {
  199. $result = $req_mgr->$pfn($idOneReq,$argsObj->id);
  200. if (!$result)
  201. {
  202. $msg .= $idOneReq . ', ';
  203. }
  204. }
  205. if (!empty($msg))
  206. {
  207. $guiObj->user_feedback = lang_get('req_msg_notupdated_coverage') . $msg;
  208. }
  209. }
  210. else
  211. {
  212. $guiObj->user_feedback = lang_get('req_msg_noselect');
  213. }
  214. return $guiObj;
  215. }
  216. /** @todo should be refactored; used by function processTestCase only */
  217. // Old comment: MHT: I'm not able find a simple SQL (subquery is not supported
  218. // in MySQL 4.0.x); probably temporary table should be used instead of the next
  219. function array_diff_byId ($arrAll, $arrPart)
  220. {
  221. // solve empty arrays
  222. if (!count($arrAll) || is_null($arrAll))
  223. {
  224. return(null);
  225. }
  226. if (!count($arrPart) || is_null($arrPart))
  227. {
  228. return $arrAll;
  229. }
  230. $arrTemp = array();
  231. $arrTemp2 = array();
  232. // converts to associated arrays
  233. foreach ($arrAll as $penny) {
  234. $arrTemp[$penny['id']] = $penny;
  235. }
  236. foreach ($arrPart as $penny) {
  237. $arrTemp2[$penny['id']] = $penny;
  238. }
  239. // exec diff
  240. $arrTemp3 = array_diff_assoc($arrTemp, $arrTemp2);
  241. $arrTemp4 = null;
  242. // convert to numbered array
  243. foreach ($arrTemp3 as $penny) {
  244. $arrTemp4[] = $penny;
  245. }
  246. return $arrTemp4;
  247. }
  248. /**
  249. * processTestCase
  250. *
  251. */
  252. function processTestCase(&$dbHandler,&$argsObj,&$guiObj)
  253. {
  254. $tproject_mgr = new testproject($dbHandler);
  255. // $guiObj->arrReqSpec = $tproject_mgr->getOptionReqSpec($argsObj->tproject_id,testproject::GET_NOT_EMPTY_REQSPEC);
  256. $guiObj->arrReqSpec = $tproject_mgr->genComboReqSpec($argsObj->tproject_id);
  257. $SRS_qty = count($guiObj->arrReqSpec);
  258. if($SRS_qty > 0)
  259. {
  260. $tc_mgr = new testcase($dbHandler);
  261. $arrTc = $tc_mgr->get_by_id($argsObj->id);
  262. if($arrTc)
  263. {
  264. $guiObj->tcTitle = $arrTc[0]['name'];
  265. // get first ReqSpec if not defined
  266. if(is_null($argsObj->idReqSpec))
  267. {
  268. reset($guiObj->arrReqSpec);
  269. $argsObj->idReqSpec = key($guiObj->arrReqSpec);
  270. }
  271. if($argsObj->idReqSpec)
  272. {
  273. $req_spec_mgr = new requirement_spec_mgr($dbHandler);
  274. $guiObj->arrAssignedReq = $req_spec_mgr->get_requirements($argsObj->idReqSpec, 'assigned', $argsObj->id);
  275. $guiObj->arrAllReq = $req_spec_mgr->get_requirements($argsObj->idReqSpec);
  276. $guiObj->arrUnassignedReq = array_diff_byId($guiObj->arrAllReq, $guiObj->arrAssignedReq);
  277. }
  278. }
  279. }
  280. return $guiObj;
  281. }
  282. function checkRights(&$db,&$user)
  283. {
  284. return ($user->hasRight($db,'mgt_view_req') && $user->hasRight($db,'mgt_modify_req'));
  285. }
  286. ?>