fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / api / scrip.t
1
2 use strict;
3 use warnings;
4 use RT::Test;
5
6 my $queue = RT::Test->load_or_create_queue( Name => 'General' );
7 ok $queue && $queue->id, 'loaded or created queue';
8
9 note 'basic scrips functionality test: create+execute';
10 {
11     my $s1 = RT::Scrip->new(RT->SystemUser);
12     my ($val, $msg) = $s1->Create(
13         Queue => $queue->Id,
14         ScripAction => 'User Defined',
15         ScripCondition => 'User Defined',
16         CustomIsApplicableCode => '$self->TicketObj->Subject =~ /fire/? 1 : 0',
17         CustomPrepareCode => 'return 1',
18         CustomCommitCode => '$self->TicketObj->SetPriority("87");',
19         Template => 'Blank'
20     );
21     ok($val,$msg);
22
23     my $ticket = RT::Ticket->new(RT->SystemUser);
24     my ($tv,$ttv,$tm) = $ticket->Create(
25         Queue => $queue->Id,
26         Subject => "hair on fire",
27     );
28     ok($tv, $tm);
29
30     is ($ticket->Priority , '87', "Ticket priority is set right");
31
32     my $ticket2 = RT::Ticket->new(RT->SystemUser);
33     my ($t2v,$t2tv,$t2m) = $ticket2->Create(
34         Queue => $queue->Id,
35         Subject => "hair in water",
36     );
37     ok($t2v, $t2m);
38     isnt ($ticket2->Priority , '87', "Ticket priority is set right");
39 }
40
41 note 'modify properties of a scrip';
42 {
43     my $scrip = RT::Scrip->new($RT::SystemUser);
44     my ( $val, $msg ) = $scrip->Create(
45         ScripCondition => 'On Comment',
46         ScripAction    => 'Notify Owner',
47     );
48     ok( !$val, "missing template: $msg" );
49     ( $val, $msg ) = $scrip->Create(
50         ScripCondition => 'On Comment',
51         ScripAction    => 'Notify Owner',
52         Template       => 'not exists',
53     );
54     ok( !$val, "invalid template: $msg" );
55
56     ( $val, $msg ) = $scrip->Create(
57         ScripAction => 'Notify Owner',
58         Template    => 'Blank',
59     );
60     ok( !$val, "missing condition: $msg" );
61     ( $val, $msg ) = $scrip->Create(
62         ScripCondition => 'not exists',
63         ScripAction    => 'Notify Owner',
64         Template       => 'Blank',
65     );
66     ok( !$val, "invalid condition: $msg" );
67
68     ( $val, $msg ) = $scrip->Create(
69         ScripCondition => 'On Comment',
70         Template       => 'Blank',
71     );
72     ok( !$val, "missing action: $msg" );
73     ( $val, $msg ) = $scrip->Create(
74         ScripCondition => 'On Comment',
75         ScripAction    => 'not exists',
76         Template       => 'Blank',
77     );
78     ok( !$val, "invalid action: $msg" );
79
80     ( $val, $msg ) = $scrip->Create(
81         ScripAction    => 'Notify Owner',
82         ScripCondition => 'On Comment',
83         Template       => 'Blank',
84     );
85     ok( $val, "created scrip: $msg" );
86     $scrip->Load($val);
87     ok( $scrip->id, 'loaded scrip ' . $scrip->id );
88
89     ( $val, $msg ) = $scrip->SetScripCondition();
90     ok( !$val, "missing condition: $msg" );
91     ( $val, $msg ) = $scrip->SetScripCondition('not exists');
92     ok( !$val, "invalid condition: $msg" );
93     ( $val, $msg ) = $scrip->SetScripCondition('On Correspond');
94     ok( $val, "updated condition to 'On Correspond': $msg" );
95
96     ( $val, $msg ) = $scrip->SetScripAction();
97     ok( !$val, "missing action: $msg" );
98     ( $val, $msg ) = $scrip->SetScripAction('not exists');
99     ok( !$val, "invalid action: $msg" );
100     ( $val, $msg ) = $scrip->SetScripAction('Notify AdminCcs');
101     ok( $val, "updated action to 'Notify AdminCcs': $msg" );
102
103     ( $val, $msg ) = $scrip->SetTemplate();
104     ok( !$val, "missing template $msg" );
105     ( $val, $msg ) = $scrip->SetTemplate('not exists');
106     ok( !$val, "invalid template $msg" );
107     ( $val, $msg ) = $scrip->SetTemplate('Forward');
108     ok( $val, "updated template to 'Forward': $msg" );
109
110     ok( $scrip->Delete, 'delete the scrip' );
111 }
112
113 my $queue_B = RT::Test->load_or_create_queue( Name => 'B' );
114 ok $queue_B && $queue_B->id, 'loaded or created queue';
115
116 note 'check creation errors vs. templates';
117 {
118     my $scrip = RT::Scrip->new(RT->SystemUser);
119     my ($status, $msg) = $scrip->Create(
120         Queue          => $queue->id,
121         ScripAction    => 'User Defined',
122         ScripCondition => 'User Defined',
123         Template       => 'not exist',
124     );
125     ok(!$status, "couldn't create scrip, not existing template");
126
127     ($status, $msg) = $scrip->Create(
128         ScripAction    => 'User Defined',
129         ScripCondition => 'User Defined',
130         Template       => 'not exist',
131     );
132     ok(!$status, "couldn't create scrip, not existing template");
133
134     ($status, $msg) = $scrip->Create(
135         Queue          => $queue->id,
136         ScripAction    => 'User Defined',
137         ScripCondition => 'User Defined',
138         Template       => 54321,
139     );
140     ok(!$status, "couldn't create scrip, not existing template");
141
142     ($status, $msg) = $scrip->Create(
143         ScripAction    => 'User Defined',
144         ScripCondition => 'User Defined',
145         Template       => 54321,
146     );
147     ok(!$status, "couldn't create scrip, not existing template");
148
149     my $template = RT::Template->new( RT->SystemUser );
150     ($status, $msg) = $template->Create( Queue => $queue->id, Name => 'bar' );
151     ok $status, 'created a template';
152
153     ($status, $msg) = $scrip->Create(
154         ScripAction    => 'User Defined',
155         ScripCondition => 'User Defined',
156         Template       => $template->id,
157     );
158     ok(!$status, "couldn't create scrip, wrong template");
159
160     ($status, $msg) = $scrip->Create(
161         Queue          => $queue_B->id,
162         ScripAction    => 'User Defined',
163         ScripCondition => 'User Defined',
164         Template       => $template->id,
165     );
166     ok(!$status, "couldn't create scrip, wrong template");
167 }
168
169 note 'check applications vs. templates';
170 {
171     my $template = RT::Template->new( RT->SystemUser );
172     my ($status, $msg) = $template->Create( Queue => $queue->id, Name => 'foo' );
173     ok $status, 'created a template';
174
175     my $scrip = RT::Scrip->new(RT->SystemUser);
176     ($status, $msg) = $scrip->Create(
177         Queue          => $queue->Id,
178         ScripAction    => 'User Defined',
179         ScripCondition => 'User Defined',
180         Template       => 'foo',
181         CustomIsApplicableCode  => "1;",
182         CustomPrepareCode       => "1;",
183         CustomCommitCode        => "1;",
184     );
185     ok($status, 'created a scrip') or diag "error: $msg";
186     RT::Test->object_scrips_are($scrip, [$queue], [0, $queue_B]);
187
188     ($status, $msg) = $scrip->AddToObject( $queue_B->id );
189     ok(!$status, $msg);
190     RT::Test->object_scrips_are($scrip, [$queue], [0, $queue_B]);
191     my $obj_scrip = RT::ObjectScrip->new( RT->SystemUser );
192     ok($obj_scrip->LoadByCols( Scrip => $scrip->id, ObjectId => $queue->id ));
193     is($obj_scrip->Stage, 'TransactionCreate');
194     is($obj_scrip->FriendlyStage, 'Normal');
195
196     $template = RT::Template->new( RT->SystemUser );
197     ($status, $msg) = $template->Create( Queue => $queue_B->id, Name => 'foo' );
198     ok $status, 'created a template';
199
200     ($status, $msg) = $scrip->AddToObject( $queue_B->id );
201     ok($status, 'added scrip to another queue');
202     RT::Test->object_scrips_are($scrip, [$queue, $queue_B], [0]);
203
204     ($status, $msg) = $scrip->RemoveFromObject( $queue_B->id );
205     ok($status, 'removed scrip from queue');
206
207     ($status, $msg) = $template->Delete;
208     ok $status, 'deleted template foo in queue B';
209
210     ($status, $msg) = $scrip->AddToObject( $queue_B->id );
211     ok(!$status, $msg);
212     RT::Test->object_scrips_are($scrip, [$queue], [0, $queue_B]);
213
214     ($status, $msg) = $template->Create( Queue => 0, Name => 'foo' );
215     ok $status, 'created a global template';
216
217     ($status, $msg) = $scrip->AddToObject( $queue_B->id );
218     ok($status, 'added scrip');
219     RT::Test->object_scrips_are($scrip, [$queue, $queue_B], [0]);
220 }
221
222 note 'basic check for disabling scrips';
223 {
224     my $scrip = RT::Scrip->new(RT->SystemUser);
225     my ($status, $msg) = $scrip->Create(
226         Queue => $queue->id,
227         ScripCondition => 'On Create',
228         ScripAction => 'User Defined',
229         CustomPrepareCode => 'return 1',
230         CustomCommitCode => '$self->TicketObj->SetPriority("87"); return 1',
231         Template => 'Blank'
232     );
233     ok($status, "created scrip");
234     is($scrip->Disabled, 0, "not disabled");
235
236     {
237         my $ticket = RT::Ticket->new(RT->SystemUser);
238         my ($tid, undef, $msg) = $ticket->Create(
239             Queue => $queue->id,
240             Subject => "test",
241         );
242         ok($tid, "created ticket") or diag "error: $msg";
243         is ($ticket->Priority , '87', "Ticket priority is set right");
244     }
245
246     ($status,$msg) = $scrip->SetDisabled(1);
247     is($scrip->Disabled, 1, "disabled");
248
249     {
250         my $ticket = RT::Ticket->new(RT->SystemUser);
251         my ($tid, undef, $msg) = $ticket->Create(
252             Queue => $queue->id,
253             Subject => "test",
254         );
255         ok($tid, "created ticket") or diag "error: $msg";
256         isnt ($ticket->Priority , '87', "Ticket priority is set right");
257     }
258
259     is($scrip->FriendlyStage('TransactionCreate'), 'Normal',
260         'Correct stage wording for TransactionCreate');
261     is($scrip->FriendlyStage('TransactionBatch'), 'Batch',
262         'Correct stage wording for TransactionBatch');
263     RT->Config->Set('UseTransactionBatch', 0);
264     is($scrip->FriendlyStage('TransactionBatch'), 'Batch (disabled by config)',
265         'Correct stage wording for TransactionBatch with UseTransactionBatch disabled');
266 }