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
|
use strict;
use warnings;
use RT;
use RT::Test tests => undef;
use Test::Warn;
my $queue = RT::Queue->new(RT->SystemUser);
$queue->Load("General");
my $ticket_cf = RT::CustomField->new(RT->SystemUser);
$ticket_cf->Create(
Name => 'Department',
Queue => '0',
Type => 'FreeformSingle',
);
my $txn_cf = RT::CustomField->new(RT->SystemUser);
$txn_cf->Create(
Name => 'Category',
LookupType => RT::Transaction->CustomFieldLookupType,
Type => 'FreeformSingle',
);
$txn_cf->AddToObject($queue);
my $ticket = RT::Ticket->new(RT->SystemUser);
my ($id, $msg) = $ticket->Create(
Subject => "template testing",
Queue => "General",
Owner => 'root@localhost',
Requestor => ["dom\@example.com"],
"CustomField-" . $txn_cf->id => "Special",
);
ok($id, "Created ticket: $msg");
my $txn = $ticket->Transactions->First;
$ticket->AddCustomFieldValue(
Field => 'Department',
Value => 'Coolio',
);
TemplateTest(
Content => "\ntest",
PerlOutput => "test",
SimpleOutput => "test",
);
TemplateTest(
Content => "\ntest { 5 * 5 }",
PerlOutput => "test 25",
SimpleOutput => "test { 5 * 5 }",
);
TemplateTest(
Content => "\ntest { \$Requestor }",
PerlOutput => "test dom\@example.com",
SimpleOutput => "test dom\@example.com",
);
TemplateTest(
Content => "\ntest { \$TicketSubject }",
PerlOutput => "test ",
SimpleOutput => "test template testing",
);
SimpleTemplateTest(
Content => "\ntest { \$TicketQueueId }",
Output => "test 1",
);
SimpleTemplateTest(
Content => "\ntest { \$TicketQueueName }",
Output => "test General",
);
SimpleTemplateTest(
Content => "\ntest { \$TicketOwnerId }",
Output => "test 14",
);
SimpleTemplateTest(
Content => "\ntest { \$TicketOwnerName }",
Output => "test root",
);
SimpleTemplateTest(
Content => "\ntest { \$TicketOwnerEmailAddress }",
Output => "test root\@localhost",
);
SimpleTemplateTest(
Content => "\ntest { \$TicketStatus }",
Output => "test new",
);
SimpleTemplateTest(
Content => "\ntest #{ \$TicketId }",
Output => "test #" . $ticket->id,
);
SimpleTemplateTest(
Content => "\ntest { \$TicketCFDepartment }",
Output => "test Coolio",
);
SimpleTemplateTest(
Content => "\ntest #{ \$TransactionId }",
Output => "test #" . $txn->id,
);
SimpleTemplateTest(
Content => "\ntest { \$TransactionType }",
Output => "test Create",
);
SimpleTemplateTest(
Content => "\ntest { \$TransactionCFCategory }",
Output => "test Special",
);
SimpleTemplateTest(
Content => "\ntest { \$TicketDelete }",
Output => "test { \$TicketDelete }",
);
SimpleTemplateTest(
Content => "\ntest { \$Nonexistent }",
Output => "test { \$Nonexistent }",
);
warning_like {
TemplateTest(
Content => "\ntest { \$Ticket->Nonexistent }",
PerlOutput => undef,
SimpleOutput => "test { \$Ticket->Nonexistent }",
);
} qr/RT::Ticket::Nonexistent Unimplemented/;
warning_like {
TemplateTest(
Content => "\ntest { \$Nonexistent->Nonexistent }",
PerlOutput => undef,
SimpleOutput => "test { \$Nonexistent->Nonexistent }",
);
} qr/Can't call method "Nonexistent" on an undefined value/;
TemplateTest(
Content => "\ntest { \$Ticket->OwnerObj->Name }",
PerlOutput => "test root",
SimpleOutput => "test { \$Ticket->OwnerObj->Name }",
);
warning_like {
TemplateTest(
Content => "\ntest { *!( }",
SyntaxError => 1,
PerlOutput => undef,
SimpleOutput => "test { *!( }",
);
} qr/Template parsing error: syntax error/;
warning_like {
TemplateTest(
Content => "\ntest { \$rtname ",
SyntaxError => 1,
PerlOutput => undef,
SimpleOutput => undef,
);
} qr/Template parsing error in Test-\d+ \(#\d+\): End of data inside program text/;
is($ticket->Status, 'new', "test setup");
SimpleTemplateTest(
Content => "\ntest { \$Ticket->SetStatus('resolved') }",
Output => "test { \$Ticket->SetStatus('resolved') }",
);
is($ticket->Status, 'new', "simple templates can't call ->SetStatus");
note "test arguments passing";
{
PerlTemplateTest(
Content => "\ntest { \$Nonexistent }",
Output => "test ",
);
PerlTemplateTest(
Content => "\ntest { \$Nonexistent }",
Arguments => { Nonexistent => 'foo' },
Output => "test foo",
);
PerlTemplateTest(
Content => "\n".'array: { join ", ", @array }',
Arguments => { array => [qw(foo bar)] },
Output => "array: foo, bar",
);
PerlTemplateTest(
Content => "\n".'hash: { join ", ", map "$_ => $hash{$_}", sort keys %hash }',
Arguments => { hash => {1 => 2, a => 'b'} },
Output => "hash: 1 => 2, a => b",
);
PerlTemplateTest(
Content => "\n".'code: { code() }',
Arguments => { code => sub { "baz" } },
Output => "code: baz",
);
}
# Make sure changing the template's type works
{
my $template = RT::Template->new(RT->SystemUser);
$template->Create(
Name => "type chameleon",
Type => "Perl",
Content => "\ntest { 10 * 7 }",
);
ok($id = $template->id, "Created template");
$template->Parse;
is($template->MIMEObj->stringify_body, "test 70", "Perl output");
$template = RT::Template->new(RT->SystemUser);
$template->Load($id);
is($template->Name, "type chameleon");
$template->SetType('Simple');
$template->Parse;
is($template->MIMEObj->stringify_body, "test { 10 * 7 }", "Simple output");
$template = RT::Template->new(RT->SystemUser);
$template->Load($id);
is($template->Name, "type chameleon");
$template->SetType('Perl');
$template->Parse;
is($template->MIMEObj->stringify_body, "test 70", "Perl output");
}
undef $ticket;
done_testing;
my $counter = 0;
sub IndividualTemplateTest {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my %args = (
Name => "Test-" . ++$counter,
Type => "Perl",
@_,
);
my $t = RT::Template->new(RT->SystemUser);
$t->Create(
Name => $args{Name},
Type => $args{Type},
Content => $args{Content},
);
ok($t->id, "Created $args{Type} template");
is($t->Name, $args{Name}, "$args{Type} template name");
is($t->Content, $args{Content}, "$args{Type} content");
is($t->Type, $args{Type}, "template type");
# this should never blow up!
my ($ok, $msg) = $t->CompileCheck;
# we don't need to syntax check simple templates since if you mess them up
# it's safe to just use the input directly as the template's output
if ($args{SyntaxError} && $args{Type} eq 'Perl') {
ok(!$ok, "got a syntax error");
}
else {
ok($ok, $msg);
}
($ok, $msg) = $t->Parse(
$args{'Arguments'}
? ( %{ $args{'Arguments'} } )
: (TicketObj => $ticket, TransactionObj => $txn )
,
);
if (defined $args{Output}) {
ok($ok, $msg);
is($t->MIMEObj->stringify_body, $args{Output}, "$args{Type} template's output");
}
else {
ok(!$ok, "expected a failure");
}
}
sub TemplateTest {
local $Test::Builder::Level = $Test::Builder::Level + 1;
my %args = @_;
for my $type ('Perl', 'Simple') {
IndividualTemplateTest(
%args,
Type => $type,
Output => $args{$type . 'Output'},
);
}
}
sub SimpleTemplateTest {
local $Test::Builder::Level = $Test::Builder::Level + 1;
IndividualTemplateTest( @_, Type => 'Simple' );
}
sub PerlTemplateTest {
local $Test::Builder::Level = $Test::Builder::Level + 1;
IndividualTemplateTest( @_, Type => 'Perl' );
}
|