PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/functions/exec.inc.php

https://bitbucket.org/pfernandez/testlink1.9.6
PHP | 298 lines | 161 code | 35 blank | 102 comment | 11 complexity | 0976079171b54384b2548f4f7e4664fe 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. * Functions for execution feature (add test results)
  7. * Legacy code (party covered by classes now)
  8. *
  9. * @package TestLink
  10. * @author Martin Havlat
  11. * @copyright 2005-2009, TestLink community
  12. * @version CVS: $Id: exec.inc.php,v 1.60 2010/06/24 17:25:53 asimon83 Exp $
  13. * @link http://www.teamst.org/index.php
  14. *
  15. * @internal Revisions:
  16. *
  17. * 20100522 - franciscom - BUGID 3479 - Bulk Execution - Custom Fields Bulk Assignment (write_execution())
  18. * 20100522 - franciscom - BUGID 3440 - get_bugs_for_exec() - added is_object() check
  19. * 20090815 - franciscom - write_execution() - interface changes
  20. * 20081231 - franciscom - write_execution() changes to manage bulks exec notes
  21. * 20080528 - franciscom - BUGID 1504 - changes in write_execution
  22. * using version_number
  23. * 20080504 - franciscom - removed deprecated functions
  24. * 20051119 - scs - added fix for 227
  25. * 20060311 - kl - some modifications to SQL queries dealing with 1.7
  26. * builds table in order to comply with new 1.7 schema
  27. * 20060528 - franciscom - adding management of bulk update
  28. * 20060916 - franciscom - added write_execution_bug()
  29. * get_bugs_for_exec()
  30. * 20070105 - franciscom - interface changes write_execution()
  31. * 20070222 - franciscom - BUGID 645 createResultsMenu()
  32. * 20070617 - franciscom - BUGID insert_id() problems for Postgres and Oracle?
  33. **/
  34. /**
  35. * @uses common.php required basic environment (configuration and core libraries)
  36. **/
  37. require_once('common.php');
  38. /**
  39. * Building the dropdown box of results filter
  40. *
  41. * @return array map of 'status_code' => localized string
  42. **/
  43. // BUGID 645
  44. function createResultsMenu()
  45. {
  46. $resultsCfg = config_get('results');
  47. // Fixed values, that has to be added always
  48. $my_all = isset($resultsCfg['status_label']['all'])?$resultsCfg['status_label']['all']:'';
  49. $menu_data[$resultsCfg['status_code']['all']] = $my_all;
  50. $menu_data[$resultsCfg['status_code']['not_run']] = lang_get($resultsCfg['status_label']['not_run']);
  51. // loop over status for user interface, because these are the statuses
  52. // user can assign while executing test cases
  53. foreach($resultsCfg['status_label_for_exec_ui'] as $verbose_status => $status_label)
  54. {
  55. $code = $resultsCfg['status_code'][$verbose_status];
  56. $menu_data[$code] = lang_get($status_label);
  57. }
  58. return $menu_data;
  59. }
  60. /**
  61. * write execution result to DB
  62. *
  63. * @param resource &$db reference to database handler
  64. * @param obj &$exec_signature object with tproject_id,tplan_id,build_id,platform_id,user_id
  65. *
  66. * @internal Revisions:
  67. *
  68. *
  69. * 20100522 - BUGID 3479 - Bulk Execution - Custom Fields Bulk Assignment
  70. */
  71. function write_execution(&$db,&$exec_signature,&$exec_data,$map_last_exec)
  72. {
  73. $executions_table = DB_TABLE_PREFIX . 'executions';
  74. $resultsCfg = config_get('results');
  75. // $bugInterfaceOn = config_get('bugInterfaceOn');
  76. $db_now = $db->db_now();
  77. $cfield_mgr = New cfield_mgr($db);
  78. $cf_prefix = $cfield_mgr->get_name_prefix();
  79. $len_cfp = tlStringLen($cf_prefix);
  80. $cf_nodeid_pos = 4;
  81. $bulk_notes = '';
  82. $ENABLED = 1;
  83. $cf_map = $cfield_mgr->get_linked_cfields_at_execution($exec_signature->tproject_id,$ENABLED,'testcase');
  84. $has_custom_fields = is_null($cf_map) ? 0 : 1;
  85. // extract custom fields id.
  86. $map_nodeid_array_cfnames=null;
  87. foreach($exec_data as $input_name => $value)
  88. {
  89. if( strncmp($input_name,$cf_prefix,$len_cfp) == 0 )
  90. {
  91. $dummy=explode('_',$input_name);
  92. $map_nodeid_array_cfnames[$dummy[$cf_nodeid_pos]][]=$input_name;
  93. }
  94. }
  95. if( isset($exec_data['do_bulk_save']) )
  96. {
  97. // create structure to use common algoritm
  98. $item2loop= $exec_data['status'];
  99. $is_bulk_save=1;
  100. $bulk_notes = $db->prepare_string(trim($exec_data['bulk_exec_notes']));
  101. }
  102. else
  103. {
  104. $item2loop= $exec_data['save_results'];
  105. $is_bulk_save=0;
  106. }
  107. foreach ( $item2loop as $tcversion_id => $val)
  108. {
  109. $tcase_id=$exec_data['tc_version'][$tcversion_id];
  110. $current_status = $exec_data['status'][$tcversion_id];
  111. $version_number=$exec_data['version_number'][$tcversion_id];;
  112. $has_been_executed = ($current_status != $resultsCfg['status_code']['not_run'] ? TRUE : FALSE);
  113. if($has_been_executed)
  114. {
  115. $my_notes = $is_bulk_save ? $bulk_notes : $db->prepare_string(trim($exec_data['notes'][$tcversion_id]));
  116. $sql = "INSERT INTO {$executions_table} ".
  117. "(build_id,tester_id,status,testplan_id,tcversion_id," .
  118. " execution_ts,notes,tcversion_number,platform_id)".
  119. " VALUES ( {$exec_signature->build_id}, {$exec_signature->user_id}, '{$exec_data['status'][$tcversion_id]}',".
  120. "{$exec_signature->tplan_id}, {$tcversion_id},{$db_now},'{$my_notes}'," .
  121. "{$version_number},{$exec_signature->platform_id}" .
  122. ")";
  123. $db->exec_query($sql);
  124. // at least for Postgres DBMS table name is needed.
  125. $execution_id = $db->insert_id($executions_table);
  126. if( $has_custom_fields )
  127. {
  128. // test useful when doing bulk update, because some type of custom fields
  129. // like checkbox can not exist on exec_data. => why ??
  130. //
  131. $hash_cf = null;
  132. $access_key = $is_bulk_save ? 0 : $tcase_id;
  133. if( isset($map_nodeid_array_cfnames[$access_key]) )
  134. {
  135. foreach($map_nodeid_array_cfnames[$access_key] as $cf_v)
  136. {
  137. $hash_cf[$cf_v]=$exec_data[$cf_v];
  138. }
  139. }
  140. $cfield_mgr->execution_values_to_db($hash_cf,$tcversion_id, $execution_id, $exec_signature->tplan_id,$cf_map);
  141. }
  142. }
  143. }
  144. }
  145. /**
  146. *
  147. *
  148. */
  149. function write_execution_bug(&$db,$exec_id, $bug_id,$just_delete=false)
  150. {
  151. $execution_bugs = DB_TABLE_PREFIX . 'execution_bugs';
  152. // Instead of Check if record exists before inserting, do delete + insert
  153. $prep_bug_id = $db->prepare_string($bug_id);
  154. $sql = "DELETE FROM {$execution_bugs} " .
  155. "WHERE execution_id={$exec_id} " .
  156. "AND bug_id='" . $prep_bug_id ."'";
  157. $result = $db->exec_query($sql);
  158. if(!$just_delete)
  159. {
  160. $sql = "INSERT INTO {$execution_bugs} " .
  161. "(execution_id,bug_id) " .
  162. "VALUES({$exec_id},'" . $prep_bug_id . "')";
  163. $result = $db->exec_query($sql);
  164. }
  165. return $result ? 1 : 0;
  166. }
  167. /**
  168. * get data about bug from external tool
  169. *
  170. * @param resource &$db reference to database handler
  171. * @param object &$bug_interface reference to instance of bugTracker class
  172. * @param integer $execution_id Identifier of execution record
  173. *
  174. * @return array list of 'bug_id' with values: 'build_name' and 'link_to_bts'
  175. */
  176. function get_bugs_for_exec(&$db,&$bug_interface,$execution_id)
  177. {
  178. $tables['execution_bugs'] = DB_TABLE_PREFIX . 'execution_bugs';
  179. $tables['executions'] = DB_TABLE_PREFIX . 'executions';
  180. $tables['builds'] = DB_TABLE_PREFIX . 'builds';
  181. $bug_list=array();
  182. $sql = "SELECT execution_id,bug_id,builds.name AS build_name " .
  183. "FROM {$tables['execution_bugs']}, {$tables['executions']} executions, " .
  184. " {$tables['builds']} builds ".
  185. "WHERE execution_id={$execution_id} " .
  186. "AND execution_id=executions.id " .
  187. "AND executions.build_id=builds.id " .
  188. "ORDER BY builds.name,bug_id";
  189. $map = $db->get_recordset($sql);
  190. // BUGID 3440 - added is_object() check
  191. if( !is_null($map) && is_object($bug_interface))
  192. {
  193. foreach($map as $elem)
  194. {
  195. $bug_list[$elem['bug_id']]['link_to_bts'] = $bug_interface->buildViewBugLink($elem['bug_id'],GET_BUG_SUMMARY);
  196. $bug_list[$elem['bug_id']]['build_name'] = $elem['build_name'];
  197. }
  198. }
  199. return($bug_list);
  200. }
  201. /**
  202. * get data about one test execution
  203. *
  204. * @param resource &$db reference to database handler
  205. * @param datatype $execution_id
  206. *
  207. * @return array all values of executions DB table in format field=>value
  208. */
  209. function get_execution(&$db,$execution_id)
  210. {
  211. $tables['executions'] = DB_TABLE_PREFIX . 'executions';
  212. $sql = "SELECT * " .
  213. "FROM {$tables['executions']} ".
  214. "WHERE id={$execution_id} ";
  215. $map = $db->get_recordset($sql);
  216. return($map);
  217. }
  218. /**
  219. * delete one test execution from database (include child data and relations)
  220. *
  221. * @param resource &$db reference to database handler
  222. * @param datatype $execution_id
  223. *
  224. * @return boolean result of delete
  225. *
  226. * @TODO delete attachment, userassignment
  227. * @TODO run SQL as transaction if database engine allows
  228. **/
  229. function delete_execution(&$db,$exec_id)
  230. {
  231. $tables['execution_bugs'] = DB_TABLE_PREFIX . 'execution_bugs';
  232. $tables['executions'] = DB_TABLE_PREFIX . 'executions';
  233. $tables['cfield_execution_values'] = DB_TABLE_PREFIX . 'cfield_execution_values';
  234. $sql = array(
  235. "DELETE FROM {$tables['execution_bugs']} WHERE execution_id = {$exec_id}", // delete bugs
  236. "DELETE FROM {$tables['cfield_execution_values']} WHERE execution_id = {$exec_id}", // delete CF
  237. "DELETE FROM {$tables['executions']} WHERE id = {$exec_id}" // delete execution
  238. );
  239. foreach ($sql as $the_stm)
  240. {
  241. $result = $db->exec_query($the_stm);
  242. if (!$result)
  243. {
  244. break;
  245. }
  246. }
  247. return $result;
  248. }
  249. /**
  250. * @param $db resource the database connecton
  251. * @param $execID integer the execution id whose notes should be set
  252. * @param $notes string the execution notes to set
  253. * @return unknown_type
  254. */
  255. function updateExecutionNotes(&$db,$execID,$notes)
  256. {
  257. $table = tlObjectWithDB::getDBTables('executions');
  258. $sql = "UPDATE {$table['executions']} " .
  259. "SET notes = '" . $db->prepare_string($notes) . "' " .
  260. "WHERE id = {$execID}";
  261. return $db->exec_query($sql) ? tl::OK : tl::ERROR;
  262. }
  263. ?>