fix quantity-less ordering
[freeside.git] / rt / t / api / template-simple.t
1 use strict;
2 use warnings;
3 use RT;
4 use RT::Test tests => 231;
5 use Test::Warn;
6
7 my $queue = RT::Queue->new(RT->SystemUser);
8 $queue->Load("General");
9
10 my $ticket_cf = RT::CustomField->new(RT->SystemUser);
11 $ticket_cf->Create(
12     Name        => 'Department',
13     Queue       => '0',
14     Type        => 'FreeformSingle',
15 );
16
17 my $txn_cf = RT::CustomField->new(RT->SystemUser);
18 $txn_cf->Create(
19     Name        => 'Category',
20     LookupType  => RT::Transaction->CustomFieldLookupType,
21     Type        => 'FreeformSingle',
22 );
23 $txn_cf->AddToObject($queue);
24
25 my $ticket = RT::Ticket->new(RT->SystemUser);
26 my ($id, $msg) = $ticket->Create(
27     Subject   => "template testing",
28     Queue     => "General",
29     Owner     => 'root@localhost',
30     Requestor => ["dom\@example.com"],
31     "CustomField-" . $txn_cf->id => "Special",
32 );
33 ok($id, "Created ticket: $msg");
34 my $txn = $ticket->Transactions->First;
35
36 $ticket->AddCustomFieldValue(
37     Field => 'Department',
38     Value => 'Coolio',
39 );
40
41 TemplateTest(
42     Content      => "\ntest",
43     PerlOutput   => "test",
44     SimpleOutput => "test",
45 );
46
47 TemplateTest(
48     Content      => "\ntest { 5 * 5 }",
49     PerlOutput   => "test 25",
50     SimpleOutput => "test { 5 * 5 }",
51 );
52
53 TemplateTest(
54     Content      => "\ntest { \$Requestor }",
55     PerlOutput   => "test dom\@example.com",
56     SimpleOutput => "test dom\@example.com",
57 );
58
59 TemplateTest(
60     Content      => "\ntest { \$TicketSubject }",
61     PerlOutput   => "test ",
62     SimpleOutput => "test template testing",
63 );
64
65 SimpleTemplateTest(
66     Content => "\ntest { \$TicketQueueId }",
67     Output  => "test 1",
68 );
69
70 SimpleTemplateTest(
71     Content => "\ntest { \$TicketQueueName }",
72     Output  => "test General",
73 );
74
75 SimpleTemplateTest(
76     Content => "\ntest { \$TicketOwnerId }",
77     Output  => "test 12",
78 );
79
80 SimpleTemplateTest(
81     Content => "\ntest { \$TicketOwnerName }",
82     Output  => "test root",
83 );
84
85 SimpleTemplateTest(
86     Content => "\ntest { \$TicketOwnerEmailAddress }",
87     Output  => "test root\@localhost",
88 );
89
90 SimpleTemplateTest(
91     Content => "\ntest { \$TicketStatus }",
92     Output  => "test new",
93 );
94
95 SimpleTemplateTest(
96     Content => "\ntest #{ \$TicketId }",
97     Output  => "test #" . $ticket->id,
98 );
99
100 SimpleTemplateTest(
101     Content => "\ntest { \$TicketCFDepartment }",
102     Output  => "test Coolio",
103 );
104
105 SimpleTemplateTest(
106     Content => "\ntest #{ \$TransactionId }",
107     Output  => "test #" . $txn->id,
108 );
109
110 SimpleTemplateTest(
111     Content => "\ntest { \$TransactionType }",
112     Output  => "test Create",
113 );
114
115 SimpleTemplateTest(
116     Content => "\ntest { \$TransactionCFCategory }",
117     Output  => "test Special",
118 );
119
120 SimpleTemplateTest(
121     Content => "\ntest { \$TicketDelete }",
122     Output  => "test { \$TicketDelete }",
123 );
124
125 SimpleTemplateTest(
126     Content => "\ntest { \$Nonexistent }",
127     Output  => "test { \$Nonexistent }",
128 );
129
130 warning_like {
131     TemplateTest(
132         Content      => "\ntest { \$Ticket->Nonexistent }",
133         PerlOutput   => undef,
134         SimpleOutput => "test { \$Ticket->Nonexistent }",
135     );
136 } qr/RT::Ticket::Nonexistent Unimplemented/;
137
138 warning_like {
139     TemplateTest(
140         Content      => "\ntest { \$Nonexistent->Nonexistent }",
141         PerlOutput   => undef,
142         SimpleOutput => "test { \$Nonexistent->Nonexistent }",
143     );
144 } qr/Can't call method "Nonexistent" on an undefined value/;
145
146 TemplateTest(
147     Content      => "\ntest { \$Ticket->OwnerObj->Name }",
148     PerlOutput   => "test root",
149     SimpleOutput => "test { \$Ticket->OwnerObj->Name }",
150 );
151
152 warning_like {
153     TemplateTest(
154         Content      => "\ntest { *!( }",
155         SyntaxError  => 1,
156         PerlOutput   => undef,
157         SimpleOutput => "test { *!( }",
158     );
159 } qr/Template parsing error: syntax error/;
160
161 TemplateTest(
162     Content      => "\ntest { \$rtname ",
163     SyntaxError  => 1,
164     PerlOutput   => undef,
165     SimpleOutput => undef,
166 );
167
168 is($ticket->Status, 'new', "test setup");
169 SimpleTemplateTest(
170     Content => "\ntest { \$Ticket->SetStatus('resolved') }",
171     Output  => "test { \$Ticket->SetStatus('resolved') }",
172 );
173 is($ticket->Status, 'new', "simple templates can't call ->SetStatus");
174
175 # Make sure changing the template's type works
176 my $template = RT::Template->new(RT->SystemUser);
177 $template->Create(
178     Name    => "type chameleon",
179     Type    => "Perl",
180     Content => "\ntest { 10 * 7 }",
181 );
182 ok($id = $template->id, "Created template");
183 $template->Parse;
184 is($template->MIMEObj->stringify_body, "test 70", "Perl output");
185
186 $template = RT::Template->new(RT->SystemUser);
187 $template->Load($id);
188 is($template->Name, "type chameleon");
189
190 $template->SetType('Simple');
191 $template->Parse;
192 is($template->MIMEObj->stringify_body, "test { 10 * 7 }", "Simple output");
193
194 $template = RT::Template->new(RT->SystemUser);
195 $template->Load($id);
196 is($template->Name, "type chameleon");
197
198 $template->SetType('Perl');
199 $template->Parse;
200 is($template->MIMEObj->stringify_body, "test 70", "Perl output");
201
202 undef $ticket;
203
204 my $counter = 0;
205 sub IndividualTemplateTest {
206     local $Test::Builder::Level = $Test::Builder::Level + 1;
207
208     my %args = (
209         Name => "Test-" . ++$counter,
210         Type => "Perl",
211         @_,
212     );
213
214     my $t = RT::Template->new(RT->SystemUser);
215     $t->Create(
216         Name    => $args{Name},
217         Type    => $args{Type},
218         Content => $args{Content},
219     );
220
221     ok($t->id, "Created $args{Type} template");
222     is($t->Name, $args{Name}, "$args{Type} template name");
223     is($t->Content, $args{Content}, "$args{Type} content");
224     is($t->Type, $args{Type}, "template type");
225
226     # this should never blow up!
227     my ($ok, $msg) = $t->CompileCheck;
228
229     # we don't need to syntax check simple templates since if you mess them up
230     # it's safe to just use the input directly as the template's output
231     if ($args{SyntaxError} && $args{Type} eq 'Perl') {
232         ok(!$ok, "got a syntax error");
233     }
234     else {
235         ok($ok, $msg);
236     }
237
238     ($ok, $msg) = $t->Parse(
239         TicketObj      => $ticket,
240         TransactionObj => $txn,
241     );
242     if (defined $args{Output}) {
243         ok($ok, $msg);
244         is($t->MIMEObj->stringify_body, $args{Output}, "$args{Type} template's output");
245     }
246     else {
247         ok(!$ok, "expected a failure");
248     }
249 }
250
251 sub TemplateTest {
252     local $Test::Builder::Level = $Test::Builder::Level + 1;
253     my %args = @_;
254
255     for my $type ('Perl', 'Simple') {
256         next if $args{"Skip$type"};
257
258         IndividualTemplateTest(
259             %args,
260             Type   => $type,
261             Output => $args{$type . 'Output'},
262         );
263     }
264 }
265
266 sub SimpleTemplateTest {
267     local $Test::Builder::Level = $Test::Builder::Level + 1;
268     my %args = @_;
269
270     IndividualTemplateTest(
271         %args,
272         Type => 'Simple',
273     );
274 }
275