-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathexec.inc.php
More file actions
1038 lines (871 loc) · 34.9 KB
/
Copy pathexec.inc.php
File metadata and controls
1038 lines (871 loc) · 34.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* TestLink Open Source Project - http://testlink.sourceforge.net/
* This script is distributed under the GNU General Public License 2 or later.
*
* Functions for execution feature (add test results)
* Legacy code (party covered by classes now)
*
* @package TestLink
* @copyright 2005-2023, TestLink community
* @filesource exec.inc.php
* @link http://www.testlink.org/
*
*
**/
require_once('common.php');
require_once('attachments.inc.php');
/**
* Building the dropdown box of results filter
*
* @return array map of 'status_code' => localized string
**/
function createResultsMenu($statusToExclude = null) {
$resultsCfg = config_get('results');
// Fixed values, that has to be added always
$my_all = isset($resultsCfg['status_label']['all']) ?
$resultsCfg['status_label']['all'] : '';
$menu_data[$resultsCfg['status_code']['all']] = $my_all;
$menu_data[$resultsCfg['status_code']['not_run']] =
lang_get($resultsCfg['status_label']['not_run']);
// loop over status for user interface, because these are the statuses
// user can assign while executing test cases
foreach($resultsCfg['status_label_for_exec_ui'] as $verbose_status => $status_label) {
$code = $resultsCfg['status_code'][$verbose_status];
$menu_data[$code] = lang_get($status_label);
}
if( null != $statusToExclude ) {
foreach($statusToExclude as $code) {
unset($menu_data[$code]);
}
}
return $menu_data;
}
/**
* write execution result to DB
*
* @param resource &$db reference to database handler
* @param obj &$execSign object with tproject_id,tplan_id,build_id,platform_id,user_id
*
*
*/
function write_execution(&$db,&$execSign,&$exec_data,&$issueTracker) {
static $docRepo;
static $resultsCfg;
static $execCfg;
static $tcaseCfg;
static $executions_table;
static $tcaseMgr;
static $cfield_mgr;
static $tprojectMgr;
$uploadOp = null;
if(is_null($docRepo)) {
$docRepo = tlAttachmentRepository::create($db);
$resultsCfg = config_get('results');
$execCfg = config_get('exec_cfg');
$tcaseCfg = config_get('testcase_cfg');
$executions_table = DB_TABLE_PREFIX . 'executions';
$tcaseMgr = new testcase($db);
$cfield_mgr = New cfield_mgr($db);
$tprojectMgr = new testproject($db);
}
$db_now = $db->db_now();
$cf_prefix = $cfield_mgr->get_name_prefix();
$len_cfp = tlStringLen($cf_prefix);
$cf_nodeid_pos = 4;
$bulk_notes = '';
$ENABLED = 1;
$cf_map = $cfield_mgr->get_linked_cfields_at_execution($execSign->tproject_id,$ENABLED,'testcase');
$has_custom_fields = is_null($cf_map) ? 0 : 1;
// extract custom fields id.
$map_nodeid_array_cfnames=null;
foreach($exec_data as $input_name => $value) {
if( strncmp($input_name,$cf_prefix,$len_cfp) == 0 ) {
$dummy=explode('_',$input_name);
$map_nodeid_array_cfnames[$dummy[$cf_nodeid_pos]][]=$input_name;
}
}
$executedInPlatform = ($execSign->platform_id == -1) ? 0 : $execSign->platform_id;
// Steps Partial Execution Feature
// When writting the execution, we will delete any partial execution
// if the test case version has steps defined.
// we check for the existence of inputs related to test case steps,
// because our choice is also get the test steps ids fromm these inputs.
//
if( isset($_REQUEST['step_notes']) ) {
$stepsIDSet = array_keys($_REQUEST['step_notes']);
$ctx = new stdClass();
$ctx->testplan_id = $execSign->tplan_id;
$ctx->platform_id = $executedInPlatform;
$ctx->build_id = $execSign->build_id;
$tcaseMgr->deleteStepsPartialExec($stepsIDSet,$ctx);
}
if( isset($exec_data['do_bulk_save']) ) {
// create structure to use common algoritm
$item2loop= $exec_data['status'];
$is_bulk_save=1;
$bulk_notes = $db->prepare_string(trim($exec_data['bulk_exec_notes']));
$execStatusKey = 'status';
}
else {
$execStatusKey = 'statusSingle';
$item2loop= $exec_data[$execStatusKey];
$is_bulk_save=0;
}
$addIssueOp = array('createIssue' => null, 'issueForStep' => null, 'type' => null);
foreach ( $item2loop as $tcversion_id => $val) {
$tcase_id=$exec_data['tc_version'][$tcversion_id];
$current_status = $exec_data[$execStatusKey][$tcversion_id];
$version_number=$exec_data['version_number'][$tcversion_id];;
$has_been_executed = ($current_status != $resultsCfg['status_code']['not_run'] ? TRUE : FALSE);
if($has_been_executed) {
$my_notes = $is_bulk_save ? $bulk_notes : $db->prepare_string(trim($exec_data['notes'][$tcversion_id]));
$sql = "INSERT INTO {$executions_table} ".
"(build_id,tester_id,status,testplan_id,tcversion_id," .
" execution_ts,notes,tcversion_number,platform_id,execution_duration)".
" VALUES ( {$execSign->build_id}, {$execSign->user_id}, '{$exec_data[$execStatusKey][$tcversion_id]}',".
"{$execSign->tplan_id}, {$tcversion_id},{$db_now},'{$my_notes}'," .
"{$version_number},{$executedInPlatform}";
$dura = 'NULL ';
if(isset($exec_data['execution_duration'])) {
if(trim($exec_data['execution_duration']) == '') {
$dura = 'NULL ';
} else {
$dura = floatval($exec_data['execution_duration']);
}
}
$sql .= ',' . $dura . ")";
$db->exec_query($sql);
// at least for Postgres DBMS table name is needed.
$execution_id = $db->insert_id($executions_table);
$execSet[$tcversion_id] = $execution_id;
//
$tcvRelations = (array)$tcaseMgr->getTCVRelationsRaw($tcversion_id);
if( count($tcvRelations) > 0 ) {
$itemSet = array_keys($tcvRelations);
$tcaseMgr->closeOpenTCVRelation($itemSet,LINK_TC_RELATION_CLOSED_BY_EXEC);
}
// DO FREEZE all OPEN Coverage Links
// Conditional DO: FREEZE all REQ Versions Linked To Test Case Version
// Check if Test Project has the requirement management feature enabled
$topt = $tprojectMgr->getOptions($execSign->tproject_id);
if( $topt->requirementsEnabled ) {
$cOpt = array('freeze_req_version' =>
$tcaseCfg->freezeReqVersionAfterExec);
$tcaseMgr->closeOpenReqLinks($tcversion_id,
LINK_TC_REQ_CLOSED_BY_EXEC,$cOpt);
}
if( $has_custom_fields ) {
// test useful when doing bulk update, because some type of custom fields
// like checkbox can not exist on exec_data. => why ??
//
$hash_cf = null;
$access_key = $is_bulk_save ? 0 : $tcase_id;
if( isset($map_nodeid_array_cfnames[$access_key]) )
{
foreach($map_nodeid_array_cfnames[$access_key] as $cf_v)
{
$hash_cf[$cf_v]=$exec_data[$cf_v];
}
}
$cfield_mgr->execution_values_to_db($hash_cf,$tcversion_id, $execution_id, $execSign->tplan_id,$cf_map);
}
// Attachment @exec level
// Available only in single test execution
//
$uploadOp = new stdClass();
$uploadOp->tcLevel = null;
$uploadOp->stepLevel = null;
if( isset($_FILES['uploadedFile']['name'][0]) &&
!is_null($_FILES['uploadedFile']['name'][0]) &&
$_FILES['uploadedFile']['name'][0][0] != '') {
$uploadOp->tcLevel = addAttachmentsToExec($execution_id,$docRepo);
}
$hasMoreData = new stdClass();
$hasMoreData->step_notes = isset($exec_data['step_notes']);
$hasMoreData->step_status = isset($exec_data['step_status']);
$hasMoreData->nike = $execCfg->steps_exec &&
($hasMoreData->step_notes || $hasMoreData->step_status);
if( $hasMoreData->nike ) {
$target = DB_TABLE_PREFIX . 'execution_tcsteps';
$key2loop = array_keys($exec_data['step_notes']);
$stepsSql = " SELECT id, step_number FROM " . DB_TABLE_PREFIX . 'tcsteps' .
" WHERE id IN (" . implode(",", $key2loop) . ")";
$stepsDecod = $db->fetchRowsIntoMap($stepsSql,'id');
foreach( $key2loop as $step_id ) {
$doIt = (!is_null($exec_data['step_notes'][$step_id]) &&
trim($exec_data['step_notes'][$step_id]) != '') ||
$exec_data['step_status'][$step_id] != $resultsCfg['status_code']['not_run'];
if( $doIt ) {
$sql = " INSERT INTO {$target} (execution_id,tcstep_id,notes";
$values = " VALUES ( {$execution_id}, {$step_id}," .
"'" . $db->prepare_string($exec_data['step_notes'][$step_id]) . "'";
$status = strtolower(trim($exec_data['step_status'][$step_id]));
$status = $status[0];
if( $status != $resultsCfg['status_code']['not_run'] ) {
$sql .= ",status";
$values .= ",'" . $db->prepare_string($status) . "'";
}
$sql .= ") " . $values . ")";
$db->exec_query($sql);
$execution_tcsteps_id = $db->insert_id($target);
// NOW MANAGE attachments
$repOpt = array('allow_empty_title' => TRUE);
$opeOKMsg = lang_get('file_upload_step_exec_ok');
$opeKOMsg = lang_get('file_upload_step_exec_ko');
if( isset($_FILES['uploadedFile']['name'][$step_id]) &&
$_FILES['uploadedFile']['name'][$step_id] != '' &&
!is_null($_FILES['uploadedFile']['name'][$step_id])) {
// May be we have enabled MULTIPLE on file upload
if( is_array($_FILES['uploadedFile']['name'][$step_id])) {
$curly = count($_FILES['uploadedFile']['name'][$step_id]);
for($moe=0; $moe < $curly; $moe++) {
$fSize = isset($_FILES['uploadedFile']['size'][$step_id][$moe]) ?
$_FILES['uploadedFile']['size'][$step_id][$moe] : 0;
$fTmpName = isset($_FILES['uploadedFile']['tmp_name'][$step_id][$moe]) ?
$_FILES['uploadedFile']['tmp_name'][$step_id][$moe] : '';
if ($fSize && $fTmpName != "") {
$fk2loop = array_keys($_FILES['uploadedFile']);
foreach($fk2loop as $tk) {
$fInfo[$tk] = $_FILES['uploadedFile'][$tk][$step_id][$moe];
}
$upx = $docRepo->insertAttachment($execution_tcsteps_id,$target,'',$fInfo,$repOpt);
if (is_null($uploadOp->stepLevel)) {
$uploadOp->stepLevel = new stdClass();
$uploadOp->stepLevel->msg = '';
}
if ($uploadOp->stepLevel->msg != '') {
$uploadOp->stepLevel->msg .= '<br>';
}
$uploadMsg = $opeOKMsg;
if ($upx->statusOK == false) {
$uploadMsg = $opeKOMsg;
}
$userMsg = str_replace( '%step%',
$stepsDecod[$step_id]['step_number'],
str_replace('%filename%',$fInfo['name'],$uploadMsg)
);
$uploadOp->stepLevel->msg .= $userMsg . '<br>';
if ($upx->statusOK == false) {
$uploadOp->stepLevel->msg .= $upx->msg . '<br>';
}
}
}
} else {
$fSize = isset($_FILES['uploadedFile']['size'][$step_id]) ? $_FILES['uploadedFile']['size'][$step_id] : 0;
$fTmpName = isset($_FILES['uploadedFile']['tmp_name'][$step_id]) ?
$_FILES['uploadedFile']['tmp_name'][$step_id] : '';
if ($fSize && $fTmpName != "") {
$fk2loop = array_keys($_FILES['uploadedFile']);
foreach($fk2loop as $tk) {
$fInfo[$tk] = $_FILES['uploadedFile'][$tk][$step_id];
}
$upx = $docRepo->insertAttachment($execution_tcsteps_id,
$target,'',$fInfo);
if ($upx != null && $upx->statusOK == false && $uploadOp->stepLevel == null) {
$uploadOp->stepLevel = $upx;
}
}
}
}
}
}
}
// Copy attachments from latest execution ?
$itCheckOK = !is_null($issueTracker) &&
method_exists($issueTracker,'addIssue');
// re-init
$addIssueOp = array('createIssue' => null, 'issueForStep' => null, 'type' => null);
if($itCheckOK) {
$execContext = new stdClass();
$execContext->exec_id = $execution_id;
$execContext->tcversion_id = $tcversion_id;
$execContext->user = $execSign->user;
$execContext->basehref = $execSign->basehref;
$execContext->tplan_apikey = $execSign->tplan_apikey;
$execContext->addLinkToTL = $execSign->addLinkToTL;
$execContext->addLinkToTLPrintView =
$execSign->addLinkToTLPrintView;
$execContext->direct_link = $execSign->direct_link;
$execContext->tcstep_id = 0;
// Issue on Test Case
if( isset($exec_data['createIssue']) ) {
completeCreateIssue($execContext,$execSign);
$aop = array('addLinkToTL' => $execContext->addLinkToTL,
'addLinkToTLPrintView' => $execContext->addLinkToTLPrintView);
$addIssueOp['createIssue'] = addIssue($db,$execContext,$issueTracker,
$aop);
$addIssueOp['type'] = 'createIssue';
}
// Issues at step level
if( isset($exec_data['issueForStep']) ) {
$addIssueOp['type'] = 'issueForStep';
foreach($exec_data['issueForStep'] as $stepID => $val) {
$addl = completeIssueForStep($execContext,$execSign,$exec_data,
$stepID);
$addIssueOp['issueForStep'][$stepID] =
addIssue($db,$execContext,$issueTracker,$addl);
}
}
} // $itCheckOK
}
}
return array($execSet,$addIssueOp,$uploadOp);
}
/**
* DELETE + INSERT => this way we will not add duplicates
*
*/
function write_execution_bug(&$db,$exec_id, $bug_id,$tcstep_id,$just_delete=false)
{
$execution_bugs = DB_TABLE_PREFIX . 'execution_bugs';
// Instead of Check if record exists before inserting, do delete + insert
$prep_bug_id = $db->prepare_string($bug_id);
$safe['exec_id'] = intval($exec_id);
$safe['tcstep_id'] = intval($tcstep_id);
$sql = " DELETE FROM {$execution_bugs} " .
" WHERE execution_id=" . $safe['exec_id'] .
" AND tcstep_id=" . $safe['tcstep_id'] .
" AND bug_id='" . $prep_bug_id . "'";
$result = $db->exec_query($sql);
if(!$just_delete)
{
$sql = " INSERT INTO {$execution_bugs} (execution_id,tcstep_id,bug_id) " .
" VALUES(" . $safe['exec_id'] . ',' . $safe['tcstep_id'] .
" ,'" . $prep_bug_id . "')";
$result = $db->exec_query($sql);
}
return $result ? 1 : 0;
}
/**
* get data about bug from external tool
*
* @param resource &$db reference to database handler
* @param object &$bug_interface reference to instance of bugTracker class
* @param integer $execution_id Identifier of execution record
*
* @return array list of 'bug_id' with values: build_name,link_to_bts,isResolved
*/
function get_bugs_for_exec(&$db,&$bug_interface,$execution_id,$raw = null)
{
$tables = tlObjectWithDB::getDBTables(
array('executions','execution_bugs','builds','tcsteps'));
$bug_list = array();
$cfg = config_get('exec_cfg');
$debugMsg = 'FILE:: ' . __FILE__ . ' :: FUNCTION:: ' . __FUNCTION__;
if( is_object($bug_interface) )
{
$sql = "/* $debugMsg */ " .
" SELECT execution_id,bug_id,tcstep_id,step_number," .
" builds.name AS build_name " .
" FROM {$tables['execution_bugs']} " .
" JOIN {$tables['executions']} executions " .
" ON executions.id = execution_id" .
" JOIN {$tables['builds']} builds " .
" ON builds.id = executions.build_id " .
" LEFT OUTER JOIN {$tables['tcsteps']} tcsteps " .
" ON tcsteps.id = tcstep_id " .
" WHERE execution_id = " . intval($execution_id) .
" {$cfg->bugs_order_clause}";
$map = $db->get_recordset($sql);
if( !is_null($map) )
{
$opt['raw'] = $raw;
$addAttr = !is_null($raw);
foreach($map as $elem)
{
if(!isset($bug_list[$elem['bug_id']]))
{
$dummy = $bug_interface->buildViewBugLink($elem['bug_id'],$opt);
$bug_list[$elem['bug_id']]['link_to_bts'] = $dummy->link;
$bug_list[$elem['bug_id']]['build_name'] = $elem['build_name'];
$bug_list[$elem['bug_id']]['isResolved'] = $dummy->isResolved;
$bug_list[$elem['bug_id']]['tcstep_id'] = $elem['tcstep_id'];
$bug_list[$elem['bug_id']]['step_number'] = $elem['step_number'];
}
if($addAttr)
{
foreach($raw as $kj)
{
if( property_exists($dummy,$kj) )
{
$bug_list[$elem['bug_id']][$kj] = $dummy->$kj;
}
}
}
unset($dummy);
}
}
}
return $bug_list;
}
/**
* get data about one test execution
*
* @param resource &$db reference to database handler
* @param datatype $execution_id
*
* @return array all values of executions DB table in format field=>value
*/
function get_execution(&$dbHandler,$execution_id,$opt=null)
{
$my = array('options' => array('output' => 'raw'));
$my['options'] = array_merge($my['options'], (array)$opt);
$tables = tlObjectWithDB::getDBTables(array('executions','nodes_hierarchy','builds','platforms'));
$safe_id = intval($execution_id);
switch($my['options']['output'])
{
case 'audit':
$sql = " SELECT B.name AS build_name, COALESCE(PLAT.name,'') AS platform_name, " .
" NH_TPLAN.name AS testplan_name, NH_TC.name AS testcase_name, " .
" E.id AS exec_id, NH_TPROJ.name AS testproject_name " .
" FROM {$tables['executions']} E " .
" JOIN {$tables['builds']} B ON B.id = E.build_id " .
" LEFT OUTER JOIN {$tables['platforms']} PLAT ON PLAT.id = E.platform_id " .
" JOIN {$tables['nodes_hierarchy']} NH_TPLAN ON NH_TPLAN.id = E.testplan_id " .
" JOIN {$tables['nodes_hierarchy']} NH_TCV ON NH_TCV.id = E.tcversion_id " .
" JOIN {$tables['nodes_hierarchy']} NH_TC ON NH_TC.id = NH_TCV.parent_id " .
" JOIN {$tables['nodes_hierarchy']} NH_TPROJ ON NH_TPROJ.id = NH_TPLAN.parent_id " .
" WHERE E.id = " . $safe_id;
break;
case 'raw':
default:
$sql = " SELECT * FROM {$tables['executions']} E ".
" WHERE E.id = " . $safe_id;
break;
}
tLog(__FUNCTION__ . ':' . $sql,"DEBUG");
$rs = $dbHandler->get_recordset($sql);
return $rs;
}
/**
* @param $db resource the database connecton
* @param $execID integer the execution id whose notes should be set
* @param $notes string the execution notes to set
* @return unknown_type
*/
function updateExecutionNotes(&$db,$execID,$notes)
{
$table = tlObjectWithDB::getDBTables('executions');
$sql = "UPDATE {$table['executions']} " .
"SET notes = '" . $db->prepare_string($notes) . "' " .
"WHERE id = " . intval($execID);
return $db->exec_query($sql) ? tl::OK : tl::ERROR;
}
/**
* get data about bug from external tool
*
* @param resource &$db reference to database handler
* @param object &$bug_interface reference to instance of bugTracker class
* @param integer $execution_id Identifier of execution record
*
* @return array list of 'bug_id' with values: build_name,link_to_bts,isResolved
*/
function getBugsForExecutions(&$db,&$bug_interface,$execSet,$raw = null)
{
$tables = tlObjectWithDB::getDBTables(array('executions','execution_bugs','builds'));
$bugSet = array();
$bugCache = array();
$cc = 0;
$debugMsg = 'FILE:: ' . __FILE__ . ' :: FUNCTION:: ' . __FUNCTION__;
if( is_object($bug_interface) )
{
$sql = "/* $debugMsg */ SELECT EB.execution_id,EB.bug_id,B.name AS build_name " .
" FROM {$tables['execution_bugs']} EB " .
" JOIN {$tables['executions']} E ON E.id = EB.execution_id " .
" JOIN {$tables['builds']} B ON B.id = E.build_id " .
" WHERE EB.execution_id IN (" . implode(',',$execSet) . ")" .
" ORDER BY B.name,EB.bug_id";
$rs = $db->fetchMapRowsIntoMap($sql,'execution_id','bug_id');
if( !is_null($rs) )
{
$opt['raw'] = $raw;
$addAttr = !is_null($raw);
$cc = 0;
foreach($rs as $key => $bugElem)
{
foreach($bugElem as $bugID => $elem)
{
if(!isset($bugCache[$elem['bug_id']]))
{
$dummy = $bug_interface->buildViewBugLink($elem['bug_id'],$opt);
$bugCache[$elem['bug_id']]['link_to_bts'] = $dummy->link;
$bugCache[$elem['bug_id']]['build_name'] = $elem['build_name'];
$bugCache[$elem['bug_id']]['isResolved'] = $dummy->isResolved;
if($addAttr)
{
foreach($raw as $kj)
{
if( property_exists($dummy,$kj) )
{
$bugCache[$elem['bug_id']][$kj] = $dummy->$kj;
}
}
}
}
$bugSet[$key][$elem['bug_id']] = $bugCache[$elem['bug_id']];
unset($dummy);
}
}
}
}
return $bugSet;
}
/**
*
*/
function addIssue($dbHandler,$argsObj,$itsObj,$opt=null) {
static $my;
if(!$my) {
$my = new stdClass();
$my->resultsCfg = config_get('results');
$my->tcaseMgr = new testcase($dbHandler);
}
$ret = array();
$ret['status_ok'] = true;
$ret['msg'] = '';
$issueText = generateIssueText($dbHandler,$argsObj,$itsObj,$opt);
$issueTrackerCfg = $itsObj->getCfg();
if(property_exists($issueTrackerCfg, 'issuetype')) {
$issueType = intval($issueTrackerCfg->issuetype);
}
if(property_exists($argsObj, 'issueType')) {
$issueType = intval($argsObj->issueType);
}
$setReporter = true;
if(method_exists($itsObj,'getCreateIssueFields')) {
$issueFields = $itsObj->getCreateIssueFields();
if(!is_null($issueFields)) {
$issueFields = current($issueFields);
}
$setReporter = isset($issueFields[$issueType]['fields']['reporter']);
}
$opt = new stdClass();
if ($setReporter) {
$opt->reporter = $argsObj->user->login;
$opt->reporter_email = trim($argsObj->user->emailAddress);
if ('' == $opt->reporter_email) {
$opt->reporter_email = $opt->reporter;
}
// Specific for JIRA after Atlassian GDRP Changes
if (method_exists($itsObj,'getUserAccountID')) {
if ($opt->reporter_email != '') {
$opt->reporter = $itsObj->getUserAccountID($opt->reporter_email);
}
}
}
if ($opt->reporter == null) {
$opt = new stdClass();
}
$p2check = array('issueType','issuePriority',
'artifactComponent','artifactVersion');
foreach($p2check as $prop) {
if(property_exists($argsObj, $prop) && !is_null($argsObj->$prop)) {
$opt->$prop = $argsObj->$prop;
}
}
$opt->execContext = $issueText->execContext;
$opt->execSignature = $issueText->execSignature;
// Management of Dynamic Values From XML Configuration
// (@20180120 only works for redmine)
$opt->tagValue = $issueText->tagValue;
$rs = $itsObj->addIssue($issueText->summary, $issueText->description,$opt);
$ret['msg'] = $rs['msg'];
if( ($ret['status_ok'] = $rs['status_ok']) ) {
if (write_execution_bug($dbHandler,$argsObj->exec_id, $rs['id'],$argsObj->tcstep_id)){
logAuditEvent(TLS("audit_executionbug_added",$rs['id']),"CREATE",$argsObj->exec_id,"executions");
}
}
return $ret;
}
/**
* copy issues from execution to another execution
*
*/
function copyIssues(&$dbHandler,$source,$dest) {
$debugMsg = 'FILE:: ' . __FILE__ . ' :: FUNCTION:: ' . __FUNCTION__;
$tables = tlObjectWithDB::getDBTables(array('execution_bugs'));
$blist=array();
$sql = "/* $debugMsg */ SELECT bug_id FROM {$tables['execution_bugs']} " .
" WHERE execution_id = " . intval($source);
$linkedIssues = $dbHandler->fetchRowsIntoMap($sql,'bug_id');
if( !is_null($linkedIssues) )
{
$idSet = array_keys($linkedIssues);
$safeDest = intval($dest);
$blist = implode("','", $idSet);
$sql = "/* $debugMsg */ DELETE FROM {$tables['execution_bugs']} " .
" WHERE execution_id=" . $safeDest .
" AND bug_id IN ('" . $blist ."')";
$dbHandler->exec_query($sql);
$dummy = array();
foreach($idSet as $bi)
{
$dummy[] = "({$safeDest},'{$bi}')";
}
$sql = "INSERT INTO {$tables['execution_bugs']} (execution_id,bug_id) VALUES " .
implode(",", $dummy);
$dbHandler->exec_query($sql);
}
}
/**
*
*/
function generateIssueText($dbHandler,$argsObj,$itsObj,$opt=null) {
$ret = new stdClass();
$options = array('addLinkToTL' => false, 'addLinkToTLPrintView' => false);
$options = array_merge($options,(array)$opt);
$opOK = false;
$msg = '';
$resultsCfg = config_get('results');
$tcaseMgr = new testcase($dbHandler);
$exec = current($tcaseMgr->getExecution($argsObj->exec_id,$argsObj->tcversion_id));
$tcase = $tcaseMgr->get_by_id(null, $argsObj->tcversion_id, null, array('output' => 'essential',
'getPrefix' => true));
$ret->auditSign = $tcaseMgr->getAuditSignature((object)array('id' => $tcase[0]['testcase_id']));
$exec['statusVerbose'] = $exec['status'];
if( isset($resultsCfg['code_status'][$exec['status']]) ) {
$exec['statusVerbose'] = $resultsCfg['code_status'][$exec['status']];
}
unset($tcaseMgr);
$platform_identity = '';
if($exec['platform_id'] > 0) {
$platform_identity = $exec['platform_name'];
}
// will be used to manage Dynamic Values From XML Configuration
$ret->tagValue = new stdClass();
$ret->tagValue->tag = array('%%EXECID%%','%%TESTER%%','%%TESTPLAN%%',
'%%PLATFORM_VALUE%%','%%BUILD%%', '%%EXECTS%%',
'%%EXECSTATUS%%', '%%TCNAME%%', '%%TCEXTID%%');
$ret->tagValue->value = array($argsObj->exec_id,$exec['tester_login'],
$exec['testplan_name'],$platform_identity,
$exec['build_name'],$exec['execution_ts'],
$exec['statusVerbose'], $tcase[0]['name'],
$tcase[0]['fullExternalID']);
$ret->execContext = array('testplan_name' => $exec['testplan_name'],
'platform_name' => $platform_identity,
'build_name' => $exec['build_name']);
$ret->execSignature = array('id' => $argsObj->exec_id,
'timestamp' => $exec['execution_ts'],
'status' => $exec['statusVerbose']);
if(property_exists($argsObj, 'bug_notes')) {
$lblKeys = array('issue_exec_id','issue_tester','issue_tplan','issue_build',
'execution_ts_iso','issue_exec_result','issue_platform',
'tc_name', 'tc_external_id');
$lbl = array();
$l2d = count($lblKeys);
for($ldx=0; $ldx < $l2d; $ldx++) {
$lbl[$lblKeys[$ldx]] = lang_get($lblKeys[$ldx]);
}
$tags = $ret->tagValue->tag;
$tags[] = '%%EXECNOTES%%';
$values = array(sprintf($lbl['issue_exec_id'],$argsObj->exec_id),
sprintf($lbl['issue_tester'],$exec['tester_login']),
sprintf($lbl['issue_tplan'],$exec['testplan_name']),
sprintf($lbl['issue_platform'],$platform_identity),
sprintf($lbl['issue_build'],$exec['build_name']),
sprintf($lbl['execution_ts_iso'],$exec['execution_ts']),
sprintf($lbl['issue_exec_result'],$exec['statusVerbose']),
sprintf($lbl['tc_name'], $tcase[0]['name']),
sprintf($lbl['tc_external_id'], $tcase[0]['fullExternalID']),
$exec['execution_notes']);
$ret->description = str_replace($tags,$values,$argsObj->bug_notes);
// 20190426
$target['value'] = '%%EXECPLINK%%';
$doIt = true;
$url2use = $argsObj->basehref . 'lnl.php?type=exec&id=' .
$argsObj->exec_id . '&apikey=' .
$exec['testplan_api_key'];
$ret->description = str_replace($target['value'],$url2use,$ret->description);
// @since 1.9.14
// %%EXECATT:1%% => lnl.php?type=file&id=1&apikey=gfhdgjfgdsjgfjsg
$target['value'] = '%%EXECATT:';
$target['len'] = strlen($target['value']);
$doIt = true;
$url2use = $argsObj->basehref . 'lnl.php?type=file&id=';
while($doIt) {
$mx = strpos($ret->description,$target['value']);
if( ($doIt = !($mx === FALSE)) ) {
$offset = $mx+$target['len'];
$cx = strpos($ret->description,'%%',$offset);
if($cx === FALSE) {
// chaos! => abort
$doIt = false;
break;
}
$old = substr($ret->description,$mx,$cx-$mx+2); // 2 is MAGIC!!!
$new = str_replace($target['value'],$url2use,$old);
$new = str_replace('%%','&apikey=' . $argsObj->tplan_apikey,$new);
$ret->description = str_replace($old,$new,$ret->description);
}
}
}
else {
$ret->description = sprintf(lang_get('issue_generated_description'),
$argsObj->exec_id,$exec['tester_login'],$exec['testplan_name']);
$ret->description .= ($platform_identity != '') ? $platform_identity . "\n" : '';
$ret->description .= sprintf(lang_get('issue_build') . "\n" . lang_get('execution_ts_iso') . "\n",
$exec['build_name'],$exec['execution_ts']);
$ret->description .= "\n" . $exec['statusVerbose'] . "\n\n" . $exec['execution_notes'];
}
$ret->timestamp = sprintf(lang_get('execution_ts_iso'),$exec['execution_ts']);
$ret->summary = $ret->auditSign . ' - ' . $ret->timestamp;
if(property_exists($argsObj,'bug_summary') && strlen(trim($argsObj->bug_summary)) != 0 ) {
$ret->summary = $argsObj->bug_summary;
}
if( $options['addLinkToTL'] ) {
$ret->description .= "\n\n" . lang_get('dl2tl') . $argsObj->direct_link;
}
if( $options['addLinkToTLPrintView'] ) {
$ret->description .= "\n\n" . lang_get('dl2tlpv') . $argsObj->basehref .
'lnl.php?type=exec&id=' . $argsObj->exec_id . '&apikey=' .
$exec['testplan_api_key'];
}
return $ret;
}
/**
*
*/
function getIssueTrackerMetaData($itsObj)
{
if(!isset($_SESSION['issueTrackerCfg']) || !$_SESSION['issueTrackerCfg'][$itsObj->name])
{
$ret = array();
$ret['issueTypes'] = null;
$ret['components'] = null;
$ret['priorities'] = null;
$ret['versions'] = null;
$target = array('issueTypes' => 'getIssueTypesForHTMLSelect',
'priorities' => 'getPrioritiesForHTMLSelect',
'versions' => 'getVersionsForHTMLSelect',
'components' => 'getComponentsForHTMLSelect');
foreach($target as $key => $worker)
{
if(method_exists($itsObj, $worker) )
{
$ret[$key] = $itsObj->$worker();
}
}
$_SESSION['issueTrackerCfg'][$itsObj->name]=$ret;
}
return $_SESSION['issueTrackerCfg'][$itsObj->name];
}
/**
*
*
*/
function completeCreateIssue(&$execContext,$exsig)
{
$p2i = array('bug_summary','bug_notes','issueType',
'issuePriority','artifactVersion',
'artifactComponent');
foreach($p2i as $key)
{
if(property_exists($exsig,$key))
{
$execContext->$key = $exsig->$key;
}
}
}
/**
*
*
*/
function completeIssueForStep(&$execContext,$execSigfrid,$exData,$stepID) {
$p2i = array('issueSummaryForStep','issueBodyForStep',
'issueTypeForStep','issuePriorityForStep',
'artifactVersionForStep',
'artifactComponentForStep');
foreach($p2i as $key) {
if( isset($exData,$key) && isset($exData[$key],$stepID) ) {
if( !property_exists($execContext,$key) ) {
$execContext->$key = array();
}
$ref = &$execContext->$key;
$ref[$stepID] = $exData[$key][$stepID];
}
}
$p2i = array('issueSummaryForStep' => 'bug_summary',
'issueBodyForStep' => 'bug_notes',
'issueTypeForStep' => 'issueType',
'issuePriorityForStep' => 'issuePriority',
'artifactVersionForStep' => 'artifactVersion',
'artifactComponentForStep' => 'artifactComponent');
foreach($p2i as $from => $to) {
if(property_exists($execContext,$from) ) {
$ref = &$execContext->$from;
$execContext->$to = $ref[$stepID];
}
}
$addLink = false;
if( property_exists($execSigfrid, 'addLinkToTLForStep') ) {
$addLink = isset($execSigfrid->addLinkToTLForStep[$stepID]);
}
return $addLink;
}
/**
*
*/
function addAttachmentsToExec($execID,&$docRepo) {
$tableRef = DB_TABLE_PREFIX . 'executions';
$repOpt = array('allow_empty_title' => TRUE);
echo '<pre>';
var_dump($_FILES['uploadedFile']['name'][0]);
echo '</pre>';
$honeyPot = [
'name' => null,
'size' => null,
'tmp_name' => null,
'type' => null,
'error' => null,
'full_path' => null
];
foreach($honeyPot as $bee => $nuu) {
// 0 is magic!!, 0 is used in the smarty template
// May be we have enabled MULTIPLE on file upload
$honeyPot[$bee] = (array)$_FILES['uploadedFile'][$bee][0];
}
$curly = count($honeyPot);
$op = new stdClass();
$op->msg = '';
$opeOKMsg = lang_get('file_upload_ok');
$fInfo = [];
for($moe=0; $moe < $curly; $moe++) {
$fSize = isset($honeyPot['size'][$moe]) ? $honeyPot['size'][$moe] : 0;
$fTmpName = isset($honeyPot['tmp_name'][$moe]) ? $honeyPot['tmp_name'][$moe] : '';
if ($fSize >0 && $fTmpName != "") {
$fk2loop = array_keys($_FILES['uploadedFile']);