fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / web / scrips.t
1 use strict;
2 use warnings;
3
4 use RT::Test tests => undef;
5
6 RT->Config->Set( UseTransactionBatch => 1 );
7
8 # TODO:
9 # Test the rest of the conditions.
10 # Test actions.
11 # Test templates?
12 # Test cleanup scripts.
13
14 my $queue_g = RT::Test->load_or_create_queue( Name => 'General' );
15 ok $queue_g && $queue_g->id, 'loaded or created queue';
16
17 my $queue_r = RT::Test->load_or_create_queue( Name => 'Regression' );
18 ok $queue_r && $queue_r->id, 'loaded or created queue';
19
20 my ($baseurl, $m) = RT::Test->started_ok;
21 ok $m->login, "logged in";
22
23 $m->follow_link_ok({id => 'admin-global-scrips-create'});
24
25 sub prepare_code_with_value {
26     my $value = shift;
27
28     # changing the ticket is an easy scrip check for a test
29     return
30         '$self->TicketObj->SetSubject(' .
31         '$self->TicketObj->Subject . ' .
32         '"|" . ' . $value .
33         ')';
34 }
35
36 {
37     # preserve order for checking the subject string later
38     my @values_for_actions;
39
40     my $conds = RT::ScripConditions->new(RT->SystemUser);
41     foreach my $cond_value ('On Forward', 'On Forward Ticket', 'On Forward Transaction') {
42         $conds->Limit(
43             FIELD           => 'name',
44             VALUE           => $cond_value,
45             ENTRYAGGREGATOR => 'OR',
46         );
47     }
48
49     while (my $rec = $conds->Next) {
50         push @values_for_actions, [$rec->Id, '"' . $rec->Name . '"'];
51     }
52
53     @values_for_actions = sort { $a->[0] cmp $b->[0] } @values_for_actions;
54
55     foreach my $data (@values_for_actions) {
56         my ($condition, $prepare_code_value) = @$data;
57         diag "Create Scrip (Cond #$condition)" if $ENV{TEST_VERBOSE};
58         $m->follow_link_ok({id => 'admin-global-scrips-create'});
59         my $prepare_code = prepare_code_with_value($prepare_code_value);
60         $m->form_name('CreateScrip');
61         $m->set_fields(
62             'ScripCondition'    => $condition,
63             'ScripAction'       => 'User Defined',
64             'Template'          => 'Blank',
65             'CustomPrepareCode' => $prepare_code,
66         );
67         $m->click('Create');
68         $m->content_like(qr{Scrip Created});
69     }
70
71     my $ticket_obj = RT::Test->create_ticket(
72         Subject => 'subject',
73         Content => 'stuff',
74         Queue   => 1,
75     );
76     my $ticket = $ticket_obj->id;
77     $m->goto_ticket($ticket);
78
79     $m->follow_link_ok(
80         { id => 'page-actions-forward' },
81         'follow 1st Forward to forward ticket'
82     );
83
84     diag "Forward Ticket" if $ENV{TEST_VERBOSE};
85     $m->submit_form(
86         form_name => 'ForwardMessage',
87         fields    => {
88             To => 'rt-test@example.com, rt-to@example.com',
89         },
90         button => 'ForwardAndReturn'
91     );
92
93     $m->text_contains("#${ticket}: subject|On Forward|On Forward Ticket");
94
95     diag "Forward Transaction" if $ENV{TEST_VERBOSE};
96     # get the first transaction on the ticket
97     my ($transaction) = $ticket_obj->Transactions->First->id;
98     $m->get(
99         "$baseurl/Ticket/Forward.html?id=1&QuoteTransaction=$transaction"
100     );
101     $m->submit_form(
102         form_name => 'ForwardMessage',
103         fields    => {
104             To => 'rt-test@example.com, rt-to@example.com',
105         },
106         button => 'ForwardAndReturn'
107     );
108
109     $m->text_contains("#${ticket}: subject|On Forward|On Forward Ticket|On Forward|On Forward Transaction");
110
111     RT::Test->clean_caught_mails;
112 }
113
114 note "check basics in scrip's admin interface";
115 {
116     $m->follow_link_ok( { id => 'admin-global-scrips-create' } );
117     ok $m->form_name('CreateScrip');
118     is $m->value_name('Description'), '', 'empty value';
119     is $m->value_name('ScripAction'), '-', 'empty value';
120     is $m->value_name('ScripCondition'), '-', 'empty value';
121     is $m->value_name('Template'), '-', 'empty value';
122     $m->field('Description' => 'test');
123     $m->click('Create');
124     $m->content_contains("Action is mandatory argument");
125
126     ok $m->form_name('CreateScrip');
127     is $m->value_name('Description'), 'test', 'value stays on the page';
128     $m->select('ScripAction' => 'Notify Ccs');
129     $m->click('Create');
130     $m->content_contains("Template is mandatory argument");
131
132     ok $m->form_name('CreateScrip');
133     is $m->value_name('Description'), 'test', 'value stays on the page';
134     is $m->value_name('ScripAction'), 'Notify Ccs', 'value stays on the page';
135     $m->select('Template' => 'Blank');
136     $m->click('Create');
137     $m->content_contains("Condition is mandatory argument");
138
139     ok $m->form_name('CreateScrip');
140     is $m->value_name('Description'), 'test', 'value stays on the page';
141     is $m->value_name('ScripAction'), 'Notify Ccs', 'value stays on the page';
142     $m->select('ScripCondition' => 'On Close');
143     $m->click('Create');
144     $m->content_contains("Scrip Created");
145
146     ok $m->form_name('ModifyScrip');
147     is $m->value_name('Description'), 'test', 'correct value';
148     is $m->value_name('ScripCondition'), 'On Close', 'correct value';
149     is $m->value_name('ScripAction'), 'Notify Ccs', 'correct value';
150     is $m->value_name('Template'), 'Blank', 'correct value';
151     $m->field('Description' => 'test test');
152     $m->click('Update');
153     # regression
154     $m->content_lacks("Template is mandatory argument");
155
156     ok $m->form_name('ModifyScrip');
157     is $m->value_name('Description'), 'test test', 'correct value';
158     $m->content_contains("Description changed from", "found action result message");
159 }
160
161 note "check application in admin interface";
162 {
163     $m->follow_link_ok({ id => 'admin-global-scrips-create' });
164     $m->submit_form_ok({
165         with_fields => {
166             Description     => "testing application",
167             ScripCondition  => "On Create",
168             ScripAction     => "Open Tickets",
169             Template        => "Blank",
170         },
171         button => 'Create',
172     }, "created scrip");
173     $m->content_contains("Scrip Created", "found result message");
174
175     my ($sid) = ($m->content =~ /Modify scrip #(\d+)/);
176     ok $sid, "found scrip id on the page";
177     RT::Test->object_scrips_are($sid, [0]);
178
179     $m->follow_link_ok({ id => 'page-applies-to' });
180     ok $m->form_name("AddRemoveScrip"), "found form";
181     $m->tick("RemoveScrip-$sid", 0);
182     $m->click_ok("Update", "update scrip application");
183     RT::Test->object_scrips_are($sid, []);
184
185     ok $m->form_name("AddRemoveScrip"), "found form";
186     $m->tick("AddScrip-$sid", 0);
187     $m->tick("AddScrip-$sid", $queue_g->id);
188     $m->click_ok("Update", "update scrip application");
189     RT::Test->object_scrips_are($sid, [0], [$queue_g->id, $queue_r->id]);
190 }
191
192 note "check templates in scrip's admin interface";
193 {
194     my $template = RT::Template->new( RT->SystemUser );
195     my ($status, $msg) = $template->Create( Queue => $queue_g->id, Name => 'foo' );
196     ok $status, 'created a template';
197
198     my $templates = RT::Templates->new( RT->SystemUser );
199     $templates->LimitToGlobal;
200
201     my @default = (
202           '',
203           map $_->Name, @{$templates->ItemsArrayRef}
204     );
205
206     $m->follow_link_ok( { id => 'admin-global-scrips-create' } );
207     ok $m->form_name('CreateScrip');
208     my @templates = ($m->find_all_inputs( type => 'option', name => 'Template' ))[0]
209         ->possible_values;
210     is_deeply([sort @templates], [sort @default]);
211
212     $m->follow_link_ok( { id => 'admin-queues' } );
213     $m->follow_link_ok( { text => 'General' } );
214     $m->follow_link_ok( { id => 'page-scrips-create' } );
215
216     ok $m->form_name('CreateScrip');
217     @templates = ($m->find_all_inputs( type => 'option', name => 'Template' ))[0]
218         ->possible_values;
219     is_deeply([sort @templates], [sort @default, 'foo']);
220
221 note "make sure we can not apply scrip to queue without required template";
222     $m->field('Description' => 'test template');
223     $m->select('ScripCondition' => 'On Close');
224     $m->select('ScripAction' => 'Notify Ccs');
225     $m->select('Template' => 'foo');
226     $m->click('Create');
227     $m->content_contains("Scrip Created");
228
229     $m->follow_link_ok( { id => 'page-applies-to' } );
230     my ($id) = ($m->content =~ /Modify associated objects for scrip #(\d+)/);
231     $m->form_name('AddRemoveScrip');
232     $m->tick('AddScrip-'.$id, $queue_r->id);
233     $m->click('Update');
234     $m->content_like(qr{No template foo in queue Regression or global});
235
236 note "unapply the scrip from any queue";
237     $m->form_name('AddRemoveScrip');
238     $m->tick('RemoveScrip-'.$id, $queue_g->id);
239     $m->click('Update');
240     $m->content_like(qr{Object deleted});
241
242 note "you can pick any template";
243     $m->follow_link_ok( { id => 'page-basics' } );
244     ok $m->form_name('ModifyScrip');
245     @templates = ($m->find_all_inputs( type => 'option', name => 'Template' ))[0]
246         ->possible_values;
247     is_deeply(
248         [sort @templates],
249         [sort do {
250             my $t = RT::Templates->new( RT->SystemUser );
251             $t->UnLimit;
252             ('', $t->DistinctFieldValues('Name'))
253         }],
254     );
255
256 note "go to apply page and apply with template change";
257     $m->follow_link_ok( { id => 'page-applies-to' } );
258     $m->form_name('AddRemoveScrip');
259     $m->field('Template' => 'blank');
260     $m->tick('AddScrip-'.$id, $queue_g->id);
261     $m->tick('AddScrip-'.$id, $queue_r->id);
262     $m->click('Update');
263     $m->content_contains("Template: Template changed from ");
264     $m->content_contains("Object created");
265 }
266
267 note "apply scrip in different stage to different queues";
268 {
269     $m->follow_link_ok( { id => 'admin-queues' } );
270     $m->follow_link_ok( { text => 'General' } );
271     $m->follow_link_ok( { id => 'page-scrips-create'});
272
273     ok $m->form_name('CreateScrip');
274     $m->field('Description' => 'test stage');
275     $m->select('ScripCondition' => 'On Close');
276     $m->select('ScripAction' => 'Notify Ccs');
277     $m->select('Template' => 'Blank');
278     $m->click('Create');
279     $m->content_contains("Scrip Created");
280
281     my ($sid) = ($m->content =~ /Modify scrip #(\d+)/);
282     ok $sid, "found scrip id on the page";
283
284     $m->follow_link_ok({ text => 'Applies to' });
285     ok $m->form_name('AddRemoveScrip');
286     $m->select('Stage' => 'Batch');
287     $m->tick( "AddScrip-$sid" => $queue_r->id );
288     $m->click('Update');
289     $m->content_contains("Object created");
290
291     $m->follow_link_ok({ text => 'General' });
292     $m->follow_link_ok({ id => 'page-scrips' });
293
294     my (@matches) = $m->content =~ /test stage/g;
295     # regression
296     is scalar @matches, 1, 'scrip mentioned only once';
297 }
298
299 undef $m;
300 done_testing;