4 use RT::Test tests => 266;
7 my $queue = RT::Queue->new(RT->SystemUser);
8 $queue->Load("General");
10 my $ticket_cf = RT::CustomField->new(RT->SystemUser);
14 Type => 'FreeformSingle',
17 my $txn_cf = RT::CustomField->new(RT->SystemUser);
20 LookupType => RT::Transaction->CustomFieldLookupType,
21 Type => 'FreeformSingle',
23 $txn_cf->AddToObject($queue);
25 my $ticket = RT::Ticket->new(RT->SystemUser);
26 my ($id, $msg) = $ticket->Create(
27 Subject => "template testing",
29 Owner => 'root@localhost',
30 Requestor => ["dom\@example.com"],
31 "CustomField-" . $txn_cf->id => "Special",
33 ok($id, "Created ticket: $msg");
34 my $txn = $ticket->Transactions->First;
36 $ticket->AddCustomFieldValue(
37 Field => 'Department',
44 SimpleOutput => "test",
48 Content => "\ntest { 5 * 5 }",
49 PerlOutput => "test 25",
50 SimpleOutput => "test { 5 * 5 }",
54 Content => "\ntest { \$Requestor }",
55 PerlOutput => "test dom\@example.com",
56 SimpleOutput => "test dom\@example.com",
60 Content => "\ntest { \$TicketSubject }",
61 PerlOutput => "test ",
62 SimpleOutput => "test template testing",
66 Content => "\ntest { \$TicketQueueId }",
71 Content => "\ntest { \$TicketQueueName }",
72 Output => "test General",
76 Content => "\ntest { \$TicketOwnerId }",
81 Content => "\ntest { \$TicketOwnerName }",
82 Output => "test root",
86 Content => "\ntest { \$TicketOwnerEmailAddress }",
87 Output => "test root\@localhost",
91 Content => "\ntest { \$TicketStatus }",
96 Content => "\ntest #{ \$TicketId }",
97 Output => "test #" . $ticket->id,
101 Content => "\ntest { \$TicketCFDepartment }",
102 Output => "test Coolio",
106 Content => "\ntest #{ \$TransactionId }",
107 Output => "test #" . $txn->id,
111 Content => "\ntest { \$TransactionType }",
112 Output => "test Create",
116 Content => "\ntest { \$TransactionCFCategory }",
117 Output => "test Special",
121 Content => "\ntest { \$TicketDelete }",
122 Output => "test { \$TicketDelete }",
126 Content => "\ntest { \$Nonexistent }",
127 Output => "test { \$Nonexistent }",
132 Content => "\ntest { \$Ticket->Nonexistent }",
134 SimpleOutput => "test { \$Ticket->Nonexistent }",
136 } qr/RT::Ticket::Nonexistent Unimplemented/;
140 Content => "\ntest { \$Nonexistent->Nonexistent }",
142 SimpleOutput => "test { \$Nonexistent->Nonexistent }",
144 } qr/Can't call method "Nonexistent" on an undefined value/;
147 Content => "\ntest { \$Ticket->OwnerObj->Name }",
148 PerlOutput => "test root",
149 SimpleOutput => "test { \$Ticket->OwnerObj->Name }",
154 Content => "\ntest { *!( }",
157 SimpleOutput => "test { *!( }",
159 } qr/Template parsing error: syntax error/;
162 Content => "\ntest { \$rtname ",
165 SimpleOutput => undef,
168 is($ticket->Status, 'new', "test setup");
170 Content => "\ntest { \$Ticket->SetStatus('resolved') }",
171 Output => "test { \$Ticket->SetStatus('resolved') }",
173 is($ticket->Status, 'new', "simple templates can't call ->SetStatus");
175 note "test arguments passing";
178 Content => "\ntest { \$Nonexistent }",
182 Content => "\ntest { \$Nonexistent }",
183 Arguments => { Nonexistent => 'foo' },
184 Output => "test foo",
188 Content => "\n".'array: { join ", ", @array }',
189 Arguments => { array => [qw(foo bar)] },
190 Output => "array: foo, bar",
193 Content => "\n".'hash: { join ", ", map "$_ => $hash{$_}", sort keys %hash }',
194 Arguments => { hash => {1 => 2, a => 'b'} },
195 Output => "hash: 1 => 2, a => b",
198 Content => "\n".'code: { code() }',
199 Arguments => { code => sub { "baz" } },
200 Output => "code: baz",
204 # Make sure changing the template's type works
206 my $template = RT::Template->new(RT->SystemUser);
208 Name => "type chameleon",
210 Content => "\ntest { 10 * 7 }",
212 ok($id = $template->id, "Created template");
214 is($template->MIMEObj->stringify_body, "test 70", "Perl output");
216 $template = RT::Template->new(RT->SystemUser);
217 $template->Load($id);
218 is($template->Name, "type chameleon");
220 $template->SetType('Simple');
222 is($template->MIMEObj->stringify_body, "test { 10 * 7 }", "Simple output");
224 $template = RT::Template->new(RT->SystemUser);
225 $template->Load($id);
226 is($template->Name, "type chameleon");
228 $template->SetType('Perl');
230 is($template->MIMEObj->stringify_body, "test 70", "Perl output");
236 sub IndividualTemplateTest {
237 local $Test::Builder::Level = $Test::Builder::Level + 1;
240 Name => "Test-" . ++$counter,
245 my $t = RT::Template->new(RT->SystemUser);
249 Content => $args{Content},
252 ok($t->id, "Created $args{Type} template");
253 is($t->Name, $args{Name}, "$args{Type} template name");
254 is($t->Content, $args{Content}, "$args{Type} content");
255 is($t->Type, $args{Type}, "template type");
257 # this should never blow up!
258 my ($ok, $msg) = $t->CompileCheck;
260 # we don't need to syntax check simple templates since if you mess them up
261 # it's safe to just use the input directly as the template's output
262 if ($args{SyntaxError} && $args{Type} eq 'Perl') {
263 ok(!$ok, "got a syntax error");
269 ($ok, $msg) = $t->Parse(
271 ? ( %{ $args{'Arguments'} } )
272 : (TicketObj => $ticket, TransactionObj => $txn )
275 if (defined $args{Output}) {
277 is($t->MIMEObj->stringify_body, $args{Output}, "$args{Type} template's output");
280 ok(!$ok, "expected a failure");
285 local $Test::Builder::Level = $Test::Builder::Level + 1;
288 for my $type ('Perl', 'Simple') {
289 IndividualTemplateTest(
292 Output => $args{$type . 'Output'},
297 sub SimpleTemplateTest {
298 local $Test::Builder::Level = $Test::Builder::Level + 1;
299 IndividualTemplateTest( @_, Type => 'Simple' );
302 sub PerlTemplateTest {
303 local $Test::Builder::Level = $Test::Builder::Level + 1;
304 IndividualTemplateTest( @_, Type => 'Perl' );