6 my $queue = RT::Test->load_or_create_queue( Name => 'General' );
7 ok $queue && $queue->id, 'loaded or created queue';
9 note 'basic scrips functionality test: create+execute';
11 my $s1 = RT::Scrip->new(RT->SystemUser);
12 my ($val, $msg) = $s1->Create(
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");',
23 my $ticket = RT::Ticket->new(RT->SystemUser);
24 my ($tv,$ttv,$tm) = $ticket->Create(
26 Subject => "hair on fire",
30 is ($ticket->Priority , '87', "Ticket priority is set right");
32 my $ticket2 = RT::Ticket->new(RT->SystemUser);
33 my ($t2v,$t2tv,$t2m) = $ticket2->Create(
35 Subject => "hair in water",
38 isnt ($ticket2->Priority , '87', "Ticket priority is set right");
41 note 'modify properties of a scrip';
43 my $scrip = RT::Scrip->new($RT::SystemUser);
44 my ( $val, $msg ) = $scrip->Create(
45 ScripCondition => 'On Comment',
46 ScripAction => 'Notify Owner',
48 ok( !$val, "missing template: $msg" );
49 ( $val, $msg ) = $scrip->Create(
50 ScripCondition => 'On Comment',
51 ScripAction => 'Notify Owner',
52 Template => 'not exists',
54 ok( !$val, "invalid template: $msg" );
56 ( $val, $msg ) = $scrip->Create(
57 ScripAction => 'Notify Owner',
60 ok( !$val, "missing condition: $msg" );
61 ( $val, $msg ) = $scrip->Create(
62 ScripCondition => 'not exists',
63 ScripAction => 'Notify Owner',
66 ok( !$val, "invalid condition: $msg" );
68 ( $val, $msg ) = $scrip->Create(
69 ScripCondition => 'On Comment',
72 ok( !$val, "missing action: $msg" );
73 ( $val, $msg ) = $scrip->Create(
74 ScripCondition => 'On Comment',
75 ScripAction => 'not exists',
78 ok( !$val, "invalid action: $msg" );
80 ( $val, $msg ) = $scrip->Create(
81 ScripAction => 'Notify Owner',
82 ScripCondition => 'On Comment',
85 ok( $val, "created scrip: $msg" );
87 ok( $scrip->id, 'loaded scrip ' . $scrip->id );
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" );
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" );
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" );
110 ok( $scrip->Delete, 'delete the scrip' );
113 my $queue_B = RT::Test->load_or_create_queue( Name => 'B' );
114 ok $queue_B && $queue_B->id, 'loaded or created queue';
116 note 'check creation errors vs. templates';
118 my $scrip = RT::Scrip->new(RT->SystemUser);
119 my ($status, $msg) = $scrip->Create(
121 ScripAction => 'User Defined',
122 ScripCondition => 'User Defined',
123 Template => 'not exist',
125 ok(!$status, "couldn't create scrip, not existing template");
127 ($status, $msg) = $scrip->Create(
128 ScripAction => 'User Defined',
129 ScripCondition => 'User Defined',
130 Template => 'not exist',
132 ok(!$status, "couldn't create scrip, not existing template");
134 ($status, $msg) = $scrip->Create(
136 ScripAction => 'User Defined',
137 ScripCondition => 'User Defined',
140 ok(!$status, "couldn't create scrip, not existing template");
142 ($status, $msg) = $scrip->Create(
143 ScripAction => 'User Defined',
144 ScripCondition => 'User Defined',
147 ok(!$status, "couldn't create scrip, not existing template");
149 my $template = RT::Template->new( RT->SystemUser );
150 ($status, $msg) = $template->Create( Queue => $queue->id, Name => 'bar' );
151 ok $status, 'created a template';
153 ($status, $msg) = $scrip->Create(
154 ScripAction => 'User Defined',
155 ScripCondition => 'User Defined',
156 Template => $template->id,
158 ok(!$status, "couldn't create scrip, wrong template");
160 ($status, $msg) = $scrip->Create(
161 Queue => $queue_B->id,
162 ScripAction => 'User Defined',
163 ScripCondition => 'User Defined',
164 Template => $template->id,
166 ok(!$status, "couldn't create scrip, wrong template");
169 note 'check applications vs. templates';
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';
175 my $scrip = RT::Scrip->new(RT->SystemUser);
176 ($status, $msg) = $scrip->Create(
178 ScripAction => 'User Defined',
179 ScripCondition => 'User Defined',
181 CustomIsApplicableCode => "1;",
182 CustomPrepareCode => "1;",
183 CustomCommitCode => "1;",
185 ok($status, 'created a scrip') or diag "error: $msg";
186 RT::Test->object_scrips_are($scrip, [$queue], [0, $queue_B]);
188 ($status, $msg) = $scrip->AddToObject( $queue_B->id );
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');
196 $template = RT::Template->new( RT->SystemUser );
197 ($status, $msg) = $template->Create( Queue => $queue_B->id, Name => 'foo' );
198 ok $status, 'created a template';
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]);
204 ($status, $msg) = $scrip->RemoveFromObject( $queue_B->id );
205 ok($status, 'removed scrip from queue');
207 ($status, $msg) = $template->Delete;
208 ok $status, 'deleted template foo in queue B';
210 ($status, $msg) = $scrip->AddToObject( $queue_B->id );
212 RT::Test->object_scrips_are($scrip, [$queue], [0, $queue_B]);
214 ($status, $msg) = $template->Create( Queue => 0, Name => 'foo' );
215 ok $status, 'created a global template';
217 ($status, $msg) = $scrip->AddToObject( $queue_B->id );
218 ok($status, 'added scrip');
219 RT::Test->object_scrips_are($scrip, [$queue, $queue_B], [0]);
222 note 'basic check for disabling scrips';
224 my $scrip = RT::Scrip->new(RT->SystemUser);
225 my ($status, $msg) = $scrip->Create(
227 ScripCondition => 'On Create',
228 ScripAction => 'User Defined',
229 CustomPrepareCode => 'return 1',
230 CustomCommitCode => '$self->TicketObj->SetPriority("87"); return 1',
233 ok($status, "created scrip");
234 is($scrip->Disabled, 0, "not disabled");
237 my $ticket = RT::Ticket->new(RT->SystemUser);
238 my ($tid, undef, $msg) = $ticket->Create(
242 ok($tid, "created ticket") or diag "error: $msg";
243 is ($ticket->Priority , '87', "Ticket priority is set right");
246 ($status,$msg) = $scrip->SetDisabled(1);
247 is($scrip->Disabled, 1, "disabled");
250 my $ticket = RT::Ticket->new(RT->SystemUser);
251 my ($tid, undef, $msg) = $ticket->Create(
255 ok($tid, "created ticket") or diag "error: $msg";
256 isnt ($ticket->Priority , '87', "Ticket priority is set right");
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');