PageRenderTime 61ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/testcases/tcSearch.php

https://bitbucket.org/pfernandez/testlink1.9.6
PHP | 319 lines | 233 code | 43 blank | 43 comment | 42 complexity | 62dbc648ea06fad6d51f0f908dff3fe2 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. * This script is distributed under the GNU General Public License 2 or later.
  5. *
  6. * Display test cases search results.
  7. *
  8. * @package TestLink
  9. * @author TestLink community
  10. * @copyright 2007-2009, TestLink community
  11. * @version CVS: $Id: tcSearch.php,v 1.15 2010/08/14 15:48:50 franciscom Exp $
  12. * @link http://www.teamst.org/index.php
  13. *
  14. *
  15. * @internal revisions
  16. * 20100814 - franciscom - improvements on logic and feedback when user fill in test case id filter
  17. * 20100609 - franciscom - BUGID 1627: Search Test Case by Date of Creation
  18. * 20100526 - Julian - BUGID 3490 - Search Test Cases based on requirements
  19. * 20100409 - franciscom - BUGID 3371 - Search Test Cases based on Test Importance
  20. * 20100326 - franciscom - BUGID 3334 - search fails if test case has 0 steps
  21. * 20100124 - franciscom - BUGID 3077 - search on preconditions
  22. * 20100106 - franciscom - Multiple Test Case Steps Feature
  23. * 20090228 - franciscom - if targetTestCase == test case prefix =>
  24. * consider as empty => means search all.
  25. *
  26. * 20090125 - franciscom - BUGID - search by requirement doc id
  27. **/
  28. require_once("../../config.inc.php");
  29. require_once("common.php");
  30. testlinkInitPage($db);
  31. $templateCfg = templateConfiguration();
  32. $tproject_mgr = new testproject($db);
  33. $tcase_cfg = config_get('testcase_cfg');
  34. $args = init_args();
  35. $gui = initializeGui($args);
  36. $map = null;
  37. if ($args->tprojectID)
  38. {
  39. $tables = tlObjectWithDB::getDBTables(array('cfield_design_values','nodes_hierarchy',
  40. 'requirements','req_coverage','tcsteps',
  41. 'testcase_keywords','tcversions'));
  42. $gui->tcasePrefix = $tproject_mgr->getTestCasePrefix($args->tprojectID);
  43. $gui->tcasePrefix .= $tcase_cfg->glue_character;
  44. $from = array('by_keyword_id' => ' ', 'by_custom_field' => ' ', 'by_requirement_doc_id' => '');
  45. $filter = null;
  46. $tcaseID = null;
  47. // if Both dates exists check From >= To
  48. if( !is_null($args->creation_date_from) && !is_null($args->creation_date_to) )
  49. {
  50. $date_from = date_create_from_format('Y-n-j', $args->creation_date_from);
  51. $date_to = date_create_from_format('Y-n-j', $args->creation_date_to);
  52. }
  53. if( !is_null($args->creation_date_from) )
  54. {
  55. $db_date = $db->db->DBdate($args->creation_date_from);
  56. $filter['by_creation_date_from'] = " AND TCV.creation_ts >= {$db_date} ";
  57. }
  58. if( !is_null($args->creation_date_to) )
  59. {
  60. $db_date = $db->db->DBdate($args->creation_date_to);
  61. $filter['by_creation_date_to'] = " AND TCV.creation_ts <= {$db_date} ";
  62. }
  63. if($args->targetTestCase != "" && strcmp($args->targetTestCase,$gui->tcasePrefix) != 0)
  64. {
  65. if (strpos($args->targetTestCase,$tcase_cfg->glue_character) === false)
  66. {
  67. $args->targetTestCase = $gui->tcasePrefix . $args->targetTestCase;
  68. }
  69. $tcase_mgr = new testcase ($db);
  70. $tcaseID = $tcase_mgr->getInternalID($args->targetTestCase,$tcase_cfg->glue_character);
  71. $filter['by_tc_id'] = " AND NH_TCV.parent_id = {$tcaseID} ";
  72. }
  73. else
  74. {
  75. $tproject_mgr->get_all_testcases_id($args->tprojectID,$a_tcid);
  76. $filter['by_tc_id'] = " AND NH_TCV.parent_id IN (" . implode(",",$a_tcid) . ") ";
  77. }
  78. if($args->version)
  79. {
  80. $filter['by_version'] = " AND TCV.version = {$args->version} ";
  81. }
  82. if($args->keyword_id)
  83. {
  84. $from['by_keyword_id'] = " JOIN {$tables['testcase_keywords']} KW ON KW.testcase_id = NH_TC.id ";
  85. $filter['by_keyword_id'] = " AND KW.keyword_id = {$args->keyword_id} ";
  86. }
  87. if($args->name != "")
  88. {
  89. $args->name = $db->prepare_string($args->name);
  90. $filter['by_name'] = " AND NH_TC.name like '%{$args->name}%' ";
  91. }
  92. if($args->summary != "")
  93. {
  94. $args->summary = $db->prepare_string($args->summary);
  95. $filter['by_summary'] = " AND TCV.summary like '%{$args->summary}%' ";
  96. }
  97. if($args->preconditions != "")
  98. {
  99. $args->preconditions = $db->prepare_string($args->preconditions);
  100. $filter['by_preconditions'] = " AND TCV.preconditions like '%{$args->preconditions}%' ";
  101. }
  102. if($args->steps != "")
  103. {
  104. $args->steps = $db->prepare_string($args->steps);
  105. $filter['by_steps'] = " AND TCSTEPS.actions like '%{$args->steps}%' ";
  106. }
  107. if($args->expected_results != "")
  108. {
  109. $args->expected_results = $db->prepare_string($args->expected_results);
  110. $filter['by_expected_results'] = " AND TCSTEPS.expected_results like '%{$args->expected_results}%' ";
  111. }
  112. if($args->custom_field_id > 0)
  113. {
  114. $args->custom_field_id = $db->prepare_string($args->custom_field_id);
  115. $args->custom_field_value = $db->prepare_string($args->custom_field_value);
  116. $from['by_custom_field']= " JOIN {$tables['cfield_design_values']} CFD " .
  117. " ON CFD.node_id=NH_TC.id ";
  118. $filter['by_custom_field'] = " AND CFD.field_id={$args->custom_field_id} " .
  119. " AND CFD.value like '%{$args->custom_field_value}%' ";
  120. }
  121. if($args->requirement_doc_id != "")
  122. {
  123. $args->requirement_doc_id = $db->prepare_string($args->requirement_doc_id);
  124. $from['by_requirement_doc_id'] = " JOIN {$tables['req_coverage']} RC" .
  125. " ON RC.testcase_id = NH_TC.id " .
  126. " JOIN {$tables['requirements']} REQ " .
  127. " ON REQ.id=RC.req_id " ;
  128. $filter['by_requirement_doc_id'] = " AND REQ.req_doc_id like '%{$args->requirement_doc_id}%' ";
  129. }
  130. // BUGID 3371
  131. if( $args->importance > 0)
  132. {
  133. $filter['importance'] = " AND TCV.importance = {$args->importance} ";
  134. }
  135. $sqlFields = " SELECT NH_TC.id AS testcase_id,NH_TC.name,TCV.id AS tcversion_id," .
  136. " TCV.summary, TCV.version, TCV.tc_external_id ";
  137. $sqlCount = "SELECT COUNT(NH_TC.id) ";
  138. // BUGID 3334 - search fails if test case has 0 steps
  139. // Added LEFT OUTER
  140. $sqlPart2 = " FROM {$tables['nodes_hierarchy']} NH_TC " .
  141. " JOIN {$tables['nodes_hierarchy']} NH_TCV ON NH_TCV.parent_id = NH_TC.id " .
  142. " JOIN {$tables['tcversions']} TCV ON NH_TCV.id = TCV.id " .
  143. " LEFT OUTER JOIN {$tables['nodes_hierarchy']} NH_TCSTEPS ON NH_TCSTEPS.parent_id = NH_TCV.id " .
  144. " LEFT OUTER JOIN {$tables['tcsteps']} TCSTEPS ON NH_TCSTEPS.id = TCSTEPS.id " .
  145. " {$from['by_keyword_id']} {$from['by_custom_field']} {$from['by_requirement_doc_id']} " .
  146. " WHERE 1=1 ";
  147. // 20100814 - franciscom
  148. // if user fill in test case [external] id filter, and we were not able to get tcaseID, do any query is useless
  149. $applyFilters = true;
  150. if( !is_null($filter) && isset($filter['by_tc_id']) && !is_null($tcaseID) && ($tcaseID <= 0) )
  151. {
  152. // get the right feedback message
  153. $applyFilters = false;
  154. $gui->warning_msg = $tcaseID == 0 ? lang_get('testcase_does_not_exists') : lang_get('prefix_does_not_exists');
  155. }
  156. if( $applyFilters )
  157. {
  158. if ($filter)
  159. {
  160. $sqlPart2 .= implode("",$filter);
  161. }
  162. // Count results
  163. $sql = $sqlCount . $sqlPart2;
  164. $gui->row_qty = $db->fetchOneValue($sql);
  165. if ($gui->row_qty)
  166. {
  167. if ($gui->row_qty <= $tcase_cfg->search->max_qty_for_display)
  168. {
  169. $sql = $sqlFields . $sqlPart2;
  170. $map = $db->fetchRowsIntoMap($sql,'testcase_id');
  171. }
  172. else
  173. {
  174. $gui->warning_msg = lang_get('too_wide_search_criteria');
  175. }
  176. }
  177. }
  178. }
  179. $smarty = new TLSmarty();
  180. if($gui->row_qty > 0)
  181. {
  182. $tpl = 'tcSearchResults.tpl';
  183. $gui->pageTitle .= " - " . lang_get('match_count') . " : " . $gui->row_qty;
  184. if ($map)
  185. {
  186. $tcase_mgr = new testcase($db);
  187. $tcase_set = array_keys($map);
  188. $gui->path_info = $tproject_mgr->tree_manager->get_full_path_verbose($tcase_set);
  189. $gui->resultSet = $map;
  190. }
  191. }
  192. else
  193. {
  194. $the_tpl = config_get('tpl');
  195. $tpl = isset($the_tpl['tcSearchView']) ? $the_tpl['tcSearchView'] : 'tcView.tpl';
  196. }
  197. $smarty->assign('gui',$gui);
  198. $smarty->display($templateCfg->template_dir . $tpl);
  199. /**
  200. *
  201. *
  202. */
  203. function init_args()
  204. {
  205. $args = new stdClass();
  206. $iParams = array("keyword_id" => array(tlInputParameter::INT_N),
  207. "version" => array(tlInputParameter::INT_N,999),
  208. "custom_field_id" => array(tlInputParameter::INT_N),
  209. "name" => array(tlInputParameter::STRING_N,0,50),
  210. "summary" => array(tlInputParameter::STRING_N,0,50),
  211. "steps" => array(tlInputParameter::STRING_N,0,50),
  212. "expected_results" => array(tlInputParameter::STRING_N,0,50),
  213. "custom_field_value" => array(tlInputParameter::STRING_N,0,20),
  214. "targetTestCase" => array(tlInputParameter::STRING_N,0,30),
  215. "preconditions" => array(tlInputParameter::STRING_N,0,50),
  216. "requirement_doc_id" => array(tlInputParameter::STRING_N,0,32),
  217. "importance" => array(tlInputParameter::INT_N),
  218. "creation_date_from_Day" => array(tlInputParameter::INT_N),
  219. "creation_date_from_Month" => array(tlInputParameter::INT_N),
  220. "creation_date_from_Year" => array(tlInputParameter::INT_N),
  221. "creation_date_to_Day" => array(tlInputParameter::INT_N),
  222. "creation_date_to_Month" => array(tlInputParameter::INT_N),
  223. "creation_date_to_Year" => array(tlInputParameter::INT_N) );
  224. $args = new stdClass();
  225. R_PARAMS($iParams,$args);
  226. $args->userID = isset($_SESSION['userID']) ? $_SESSION['userID'] : 0;
  227. $args->tprojectID = isset($_SESSION['testprojectID']) ? $_SESSION['testprojectID'] : 0;
  228. // special situation: dates
  229. $date_vars = array('target_date_Year','target_date_Month','target_date_Day');
  230. $start_date_vars = array('start_date_Year','start_date_Month','start_date_Day');
  231. $date_vars_prefix = array('creation_date_from','creation_date_to');
  232. $date_pieces = array('_Year','_Month','_Day');
  233. $create_date = array('creation_date_from' => true,'creation_date_to' => true);
  234. foreach($date_vars_prefix as $target)
  235. {
  236. $xx = array();
  237. $args->$target = null;
  238. foreach($date_pieces as $pk)
  239. {
  240. $accessKey = $target . $pk;
  241. $create_date[$target] = $create_date[$target] && !is_null($args->$accessKey) && trim($args->$accessKey) != '' &&
  242. intval($args->$accessKey) > 0;
  243. $xx[] = $args->$accessKey;
  244. }
  245. if($create_date[$target])
  246. {
  247. $args->$target = implode('-',$xx);
  248. }
  249. }
  250. new dBug($args);
  251. return $args;
  252. }
  253. /**
  254. *
  255. *
  256. */
  257. function initializeGui(&$argsObj)
  258. {
  259. $gui = new stdClass();
  260. $gui->pageTitle = lang_get('caption_search_form');
  261. $gui->warning_msg = '';
  262. $gui->tcasePrefix = '';
  263. $gui->path_info = null;
  264. $gui->resultSet = null;
  265. $gui->bodyOnLoad = null;
  266. $gui->bodyOnUnload = null;
  267. $gui->refresh_tree = false;
  268. $gui->hilite_testcase_name = false;
  269. $gui->show_match_count = false;
  270. $gui->tc_current_version = null;
  271. $gui->row_qty = 0;
  272. $gui->creation_date_from = $argsObj->creation_date_from;
  273. $gui->creation_date_to = $argsObj->creation_date_to;
  274. return $gui;
  275. }
  276. ?>