Merge branch 'master' of https://github.com/jgoodman/Freeside
[freeside.git] / rt / t / api / action-createtickets.t
1
2 use strict;
3 use warnings;
4 use RT;
5 use RT::Test tests => 54;
6
7
8 {
9
10 ok (require RT::Action::CreateTickets);
11 use_ok('RT::Scrip');
12 use_ok('RT::Template');
13 use_ok('RT::ScripAction');
14 use_ok('RT::ScripCondition');
15 use_ok('RT::Ticket');
16
17
18 use_ok('RT::CustomField');
19
20 my $global_cf = RT::CustomField->new($RT::SystemUser);
21 my ($id, $msg)=  $global_cf->Create( Name => 'GlobalCF',
22                                  Queue => '0',
23                                  SortOrder => '1',
24                                  Description => 'A Testing custom field',
25                                  Type=> 'SelectSingle');
26 ok($id, 'Global custom field correctly created');
27
28
29 my $approvalsq = RT::Queue->new(RT->SystemUser);
30 $approvalsq->Create(Name => 'Approvals');
31 ok ($approvalsq->Id, "Created Approvals test queue");
32
33 my $queue_cf = RT::CustomField->new($RT::SystemUser);
34 ($id) = $queue_cf->Create(
35     Name => 'QueueCF',
36     Queue => $approvalsq->Id,
37     SortOrder => 2,
38     Description => 'A testing queue-specific custom field',
39     Type => 'SelectSingle',
40 );
41 ok($id, 'Queue-specific custom field correctly created');
42
43
44
45 my $approvals = 
46 '===Create-Ticket: approval
47 Queue: Approvals
48 Type: approval
49 AdminCc: {join ("\nAdminCc: ",@admins) }
50 Depended-On-By: {$Tickets{"TOP"}->Id}
51 Refers-To: TOP 
52 CustomField-GlobalCF: A Value
53 CustomField-QueueCF: Another Value
54 Subject: Approval for ticket: {$Tickets{"TOP"}->Id} - {$Tickets{"TOP"}->Subject}
55 Due: {time + 86400}
56 Content-Type: text/plain
57 Content: Your approval is requested for the ticket {$Tickets{"TOP"}->Id}: {$Tickets{"TOP"}->Subject}
58 Blah
59 Blah
60 ENDOFCONTENT
61 ===Create-Ticket: two
62 Subject: Manager approval.
63 Depended-On-By: approval
64 Queue: Approvals
65 Content-Type: text/plain
66 Content: 
67 Your minion approved ticket {$Tickets{"TOP"}->Id}. you ok with that?
68 ENDOFCONTENT
69 ';
70
71 like ($approvals , qr/Content/, "Read in the approvals template");
72
73 my $apptemp = RT::Template->new(RT->SystemUser);
74 $apptemp->Create( Content => $approvals, Name => "Approvals", Queue => "0");
75
76 ok ($apptemp->Id);
77
78 my $q = RT::Queue->new(RT->SystemUser);
79 $q->Create(Name => 'WorkflowTest');
80 ok ($q->Id, "Created workflow test queue");
81
82 my $scrip = RT::Scrip->new(RT->SystemUser);
83 my ($sval, $smsg) =$scrip->Create( ScripCondition => 'On Transaction',
84                 ScripAction => 'Create Tickets',
85                 Template => 'Approvals',
86                 Queue => $q->Id);
87 ok ($sval, $smsg);
88 ok ($scrip->Id, "Created the scrip");
89 ok ($scrip->TemplateObj->Id, "Created the scrip template");
90 ok ($scrip->ConditionObj->Id, "Created the scrip condition");
91 ok ($scrip->ActionObj->Id, "Created the scrip action");
92
93 my $t = RT::Ticket->new(RT->SystemUser);
94 my($tid, $ttrans, $tmsg) = $t->Create(Subject => "Sample workflow test",
95            Owner => "root",
96            Queue => $q->Id);
97
98 ok ($tid,$tmsg);
99
100 my $deps = $t->DependsOn;
101 is ($deps->Count, 1, "The ticket we created depends on one other ticket");
102 my $dependson= $deps->First->TargetObj;
103 ok ($dependson->Id, "It depends on a real ticket");
104 is ($dependson->FirstCustomFieldValue('GlobalCF'), 'A Value',
105   'global custom field was set');
106 is ($dependson->FirstCustomFieldValue('QueueCF'), 'Another Value',
107   'queue custom field was set');
108 unlike ($dependson->Subject, qr/\{/, "The subject doesn't have braces in it. that means we're interpreting expressions");
109 is ($t->ReferredToBy->Count,1, "It's only referred to by one other ticket");
110 is ($t->ReferredToBy->First->BaseObj->Id,$t->DependsOn->First->TargetObj->Id, "The same ticket that depends on it refers to it.");
111 use RT::Action::CreateTickets;
112 my $action =  RT::Action::CreateTickets->new( CurrentUser => RT->SystemUser);
113
114 # comma-delimited templates
115 my $commas = <<"EOF";
116 id,Queue,Subject,Owner,Content
117 ticket1,General,"foo, bar",root,blah
118 ticket2,General,foo bar,root,blah
119 ticket3,General,foo' bar,root,blah'boo
120 ticket4,General,foo' bar,,blah'boo
121 EOF
122
123
124 # Comma delimited templates with missing data
125 my $sparse_commas = <<"EOF";
126 id,Queue,Subject,Owner,Requestor
127 ticket14,General,,,bobby
128 ticket15,General,,,tommy
129 ticket16,General,,suzie,tommy
130 ticket17,General,Foo "bar" baz,suzie,tommy
131 ticket18,General,'Foo "bar" baz',suzie,tommy
132 ticket19,General,'Foo bar' baz,suzie,tommy
133 EOF
134
135
136 # tab-delimited templates
137 my $tabs = <<"EOF";
138 id\tQueue\tSubject\tOwner\tContent
139 ticket10\tGeneral\t"foo' bar"\troot\tblah'
140 ticket11\tGeneral\tfoo, bar\troot\tblah
141 ticket12\tGeneral\tfoo' bar\troot\tblah'boo
142 ticket13\tGeneral\tfoo' bar\t\tblah'boo
143 EOF
144
145 my %expected;
146
147 $expected{ticket1} = <<EOF;
148 Queue: General
149 Subject: foo, bar
150 Owner: root
151 Content: blah
152 ENDOFCONTENT
153 EOF
154
155 $expected{ticket2} = <<EOF;
156 Queue: General
157 Subject: foo bar
158 Owner: root
159 Content: blah
160 ENDOFCONTENT
161 EOF
162
163 $expected{ticket3} = <<EOF;
164 Queue: General
165 Subject: foo' bar
166 Owner: root
167 Content: blah'boo
168 ENDOFCONTENT
169 EOF
170
171 $expected{ticket4} = <<EOF;
172 Queue: General
173 Subject: foo' bar
174 Owner: 
175 Content: blah'boo
176 ENDOFCONTENT
177 EOF
178
179 $expected{ticket10} = <<EOF;
180 Queue: General
181 Subject: foo' bar
182 Owner: root
183 Content: blah'
184 ENDOFCONTENT
185 EOF
186
187 $expected{ticket11} = <<EOF;
188 Queue: General
189 Subject: foo, bar
190 Owner: root
191 Content: blah
192 ENDOFCONTENT
193 EOF
194
195 $expected{ticket12} = <<EOF;
196 Queue: General
197 Subject: foo' bar
198 Owner: root
199 Content: blah'boo
200 ENDOFCONTENT
201 EOF
202
203 $expected{ticket13} = <<EOF;
204 Queue: General
205 Subject: foo' bar
206 Owner: 
207 Content: blah'boo
208 ENDOFCONTENT
209 EOF
210
211
212 $expected{'ticket14'} = <<EOF;
213 Queue: General
214 Subject: 
215 Owner: 
216 Requestor: bobby
217 EOF
218 $expected{'ticket15'} = <<EOF;
219 Queue: General
220 Subject: 
221 Owner: 
222 Requestor: tommy
223 EOF
224 $expected{'ticket16'} = <<EOF;
225 Queue: General
226 Subject: 
227 Owner: suzie
228 Requestor: tommy
229 EOF
230 $expected{'ticket17'} = <<EOF;
231 Queue: General
232 Subject: Foo "bar" baz
233 Owner: suzie
234 Requestor: tommy
235 EOF
236 $expected{'ticket18'} = <<EOF;
237 Queue: General
238 Subject: Foo "bar" baz
239 Owner: suzie
240 Requestor: tommy
241 EOF
242 $expected{'ticket19'} = <<EOF;
243 Queue: General
244 Subject: 'Foo bar' baz
245 Owner: suzie
246 Requestor: tommy
247 EOF
248
249
250
251
252 $action->Parse(Content =>$commas);
253 $action->Parse(Content =>$sparse_commas);
254 $action->Parse(Content => $tabs);
255
256 my %got;
257 foreach (@{ $action->{'create_tickets'} }) {
258   $got{$_} = $action->{'templates'}->{$_};
259 }
260
261 foreach my $id ( sort keys %expected ) {
262     ok(exists($got{"create-$id"}), "template exists for $id");
263     is($got{"create-$id"}, $expected{$id}, "template is correct for $id");
264 }
265
266
267 }
268