-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtr_new_case.cgi
More file actions
executable file
·196 lines (168 loc) · 6.98 KB
/
tr_new_case.cgi
File metadata and controls
executable file
·196 lines (168 loc) · 6.98 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
#!/usr/bin/perl -wT
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Test Runner System.
#
# The Initial Developer of the Original Code is Maciej Maczynski.
# Portions created by Maciej Maczynski are Copyright (C) 2001
# Maciej Maczynski. All Rights Reserved.
#
# Contributor(s): Greg Hendricks <[email protected]>
use strict;
use lib qw(. lib);
use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::Error;
use Bugzilla::Util;
use Bugzilla::User;
BEGIN { Bugzilla->extensions }
use Bugzilla::Extension::Testopia::Util;
use Bugzilla::Extension::Testopia::TestCase;
use Bugzilla::Extension::Testopia::Search;
use Bugzilla::Extension::Testopia::Table;
use Bugzilla::Extension::Testopia::Constants;
use JSON;
###############################################################################
# tr_new_case.cgi
# Presents a webform to the user for the creation of a new test case.
#
# INTERFACE:
# plan_id: list - list of plans that the newly created test case will
# be attached to. If no plan_id is found, the user will first
# be presented with a form to select a plan.
#
# action: undef - Present form for new case creation
# "Add" - Form has been submitted with case data. Create the test
# case.
#
################################################################################
my $vars = {};
Bugzilla->login(LOGIN_REQUIRED);
my $cgi = Bugzilla->cgi;
my $template = Bugzilla->template;
Bugzilla->error_mode(ERROR_MODE_AJAX) if $cgi->referer() && $cgi->referer() !~ /tr_new_case/;
print $cgi->header;
my $action = $cgi->param('action') || '';
my @plan_id = split(',', $cgi->param('plan_id'));
unless ($plan_id[0]){
$vars->{'product'} = Bugzilla::Extension::Testopia::Product->new({'name' => $cgi->param('product')}) if ($cgi->param('product'));
$vars->{'bug_id'} = $cgi->param('bug');
$vars->{'form_action'} = 'tr_new_case.cgi';
$vars->{'type'} = "Case";
$template->process("testopia/plan/choose.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
my %seen;
my @plans;
my @plan_ids;
my @categories;
# Weed out duplicates.
foreach my $entry (@plan_id){
foreach my $id (split(/[\s,]+/, $entry)){
detaint_natural($id);
validate_test_id($id, 'plan');
$seen{$id} = 1;
}
}
# Users need write permission on the plan in order to create a test case against
# that plan. See tr_plan_access.cgi
foreach my $id (keys %seen){
my $plan = Bugzilla::Extension::Testopia::TestPlan->new($id);
ThrowUserError("testopia-create-denied", {'object' => 'Test Case', 'plan' => $plan}) unless $plan->canedit;
push @plan_ids, $id;
push @plans, $plan;
push @categories, @{$plan->product->categories};
}
# We need at least one category in the list.
ThrowUserError('testopia-create-category', {'plan' => $plans[0] }) if scalar @categories < 1;
if ($action eq 'add'){
Bugzilla->error_mode(ERROR_MODE_AJAX);
my @comps = split(',', $cgi->param("components"));
my $case = Bugzilla::Extension::Testopia::TestCase->create({
'alias' => $cgi->param('alias') || '',
'case_status_id' => $cgi->param('status') || '',
'category_id' => $cgi->param('category') || '',
'priority_id' => $cgi->param('priority') || '',
'isautomated' => $cgi->param("isautomated") eq 'on' ? 1 : 0,
'estimated_time' => $cgi->param("estimated_time") || '',
'script' => $cgi->param("script") || '',
'arguments' => $cgi->param("arguments") || '',
'summary' => $cgi->param("summary") || '',
'requirement' => $cgi->param("requirement") || '',
'default_tester_id' => $cgi->param("tester") || '',
'author_id' => Bugzilla->user->id || '',
'action' => $cgi->param("tcaction") || '',
'effect' => $cgi->param("tceffect") || '',
'setup' => $cgi->param("tcsetup") || '',
'breakdown' => $cgi->param("tcbreakdown") || '',
'dependson' => $cgi->param("tcdependson") || '',
'blocks' => $cgi->param("tcblocks") || '',
'tags' => $cgi->param('addtags') || '',
'runs' => $cgi->param('addruns') || '',
'bugs' => $cgi->param('bugs') || '',
'plans' => \@plans,
'components' => \@comps,
});
my $err = JSON::false;
for (my $i=1; $i<5; $i++){
next unless defined $cgi->upload("file$i");
my $fh = $cgi->upload("file$i");
my $data;
# enable 'slurp' mode
local $/;
$data = <$fh>;
Bugzilla->error_mode(ERROR_MODE_DIE);
eval {
$data || ThrowUserError("zero_length_file");
my $attachment = Bugzilla::Extension::Testopia::Attachment->create({
case_id => $case->id,
submitter_id => Bugzilla->user->id,
description => $cgi->param("file_desc$i") || 'Attachment',
filename => $cgi->upload("file$i"),
mime_type => $cgi->uploadInfo($cgi->param("file$i"))->{'Content-Type'},
contents => $data
});
};
if ($@){
$err = JSON::true;
}
}
print "{success: true, tc: '". $case->id ."', err: $err}";
}
####################
### Display Form ###
####################
else {
my $summary;
my $text;
if( $cgi->param('bug')){
my $bug;
$bug = Bugzilla::Bug->new($cgi->param('bug'),Bugzilla->user->id);
my $bug_id = $bug->bug_id;
my $description = '<br><pre>' . wrap_comment(@{$bug->comments({order => 'oldest_to_newest'})}[0]->{'thetext'}) . '</pre>';
my $short_desc = $bug->short_desc;
$summary = Bugzilla->params->{"bug-to-test-case-summary"};
$summary =~ s/%id%/$bug_id/g;
$summary =~ s/%summary%/$short_desc/g;
$vars->{'bugs'} = $bug->bug_id;
}
my $case = {'plans' => join(',', @plan_ids),
'category' => {name => '--default--'},
'summary' => $summary,
};
$vars->{'tc'} = $case;
$vars->{'product_id'} = $plans[0]->product_id;
$template->process("testopia/case/add.html.tmpl", $vars) ||
ThrowTemplateError($template->error());
}