fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / api / scrip_order.t
1
2 use strict;
3 use warnings;
4
5 use RT::Test tests => 204;
6
7 my $queue = RT::Test->load_or_create_queue( Name => 'General' );
8 ok $queue && $queue->id, 'loaded or created queue';
9
10 note "check that execution order reflects sort order";
11 {
12     my $ten = main->create_scrip_ok(
13         Description => "Set priority to 10",
14         Queue => $queue->id, 
15         CustomCommitCode => '$self->TicketObj->SetPriority(10);',
16     );
17
18     my $five = main->create_scrip_ok(
19         Description => "Set priority to 5",
20         Queue => $queue->id,
21         CustomCommitCode => '$self->TicketObj->SetPriority(5);', 
22     );
23
24     my $ticket = RT::Ticket->new(RT->SystemUser);
25     my ($id, $msg) = $ticket->Create( 
26         Queue => $queue->id, 
27         Subject => "Scrip order test $$",
28     );
29     ok($ticket->id, "Created ticket? id=$id");
30     is($ticket->Priority , 5, "By default newer scrip is last");
31
32     main->move_scrip_ok( $five, $queue->id, 'up' );
33
34     $ticket = RT::Ticket->new(RT->SystemUser);
35     ($id, $msg) = $ticket->Create(
36         Queue => $queue->id,
37         Subject => "Scrip order test $$",
38     );
39     ok($ticket->id, "Created ticket? id=$id");
40     is($ticket->Priority , 10, "Moved scrip and result is different");
41 }
42
43 my $queue_B = RT::Test->load_or_create_queue( Name => 'Other' );
44 ok $queue_B && $queue_B->id, 'loaded or created queue';
45
46 note "move around two local scrips";
47 {
48     main->delete_all_scrips();
49
50     my @scrips;
51     push @scrips, main->create_scrip_ok( Queue => $queue->id );
52     push @scrips, main->create_scrip_ok( Queue => $queue->id );
53     main->check_scrips_order(\@scrips, [$queue]);
54
55     main->move_scrip_ok( $scrips[0], $queue->id, 'down' );
56     @scrips = @scrips[1, 0];
57     main->check_scrips_order(\@scrips, [$queue]);
58
59     main->move_scrip_ok( $scrips[0], $queue->id, 'down' );
60     @scrips = @scrips[1, 0];
61     main->check_scrips_order(\@scrips, [$queue]);
62
63     main->move_scrip_ok( $scrips[1], $queue->id, 'up' );
64     @scrips = @scrips[1, 0];
65     main->check_scrips_order(\@scrips, [$queue]);
66
67     main->move_scrip_ok( $scrips[1], $queue->id, 'up' );
68     @scrips = @scrips[1, 0];
69     main->check_scrips_order(\@scrips, [$queue]);
70 }
71
72 note "move around two global scrips";
73 {
74     main->delete_all_scrips();
75
76     my @scrips;
77     push @scrips, main->create_scrip_ok( Queue => 0 );
78     push @scrips, main->create_scrip_ok( Queue => 0 );
79     main->check_scrips_order(\@scrips, [$queue]);
80
81     main->move_scrip_ok( $scrips[0], 0, 'down' );
82     @scrips = @scrips[1, 0];
83     main->check_scrips_order(\@scrips, [$queue]);
84
85     main->move_scrip_ok( $scrips[0], 0, 'down' );
86     @scrips = @scrips[1, 0];
87     main->check_scrips_order(\@scrips, [$queue]);
88
89     main->move_scrip_ok( $scrips[1], 0, 'up' );
90     @scrips = @scrips[1, 0];
91     main->check_scrips_order(\@scrips, [$queue]);
92
93     main->move_scrip_ok( $scrips[1], 0, 'up' );
94     @scrips = @scrips[1, 0];
95     main->check_scrips_order(\@scrips, [$queue]);
96 }
97
98 note "move local scrip below global";
99 {
100     main->delete_all_scrips();
101     my @scrips;
102     push @scrips, main->create_scrip_ok( Queue => $queue->id );
103     push @scrips, main->create_scrip_ok( Queue => $queue_B->id );
104     push @scrips, main->create_scrip_ok( Queue => 0 );
105     push @scrips, main->create_scrip_ok( Queue => $queue->id );
106     main->check_scrips_order(\@scrips, [$queue, $queue_B]);
107
108     main->move_scrip_ok( $scrips[0], $queue->id, 'down' );
109     @scrips = @scrips[1, 2, 0, 3];
110     main->check_scrips_order(\@scrips, [$queue, $queue_B]);
111 }
112
113 note "move local scrip above global";
114 {
115     main->delete_all_scrips();
116     my @scrips;
117     push @scrips, main->create_scrip_ok( Queue => $queue_B->id );
118     push @scrips, main->create_scrip_ok( Queue => 0 );
119     push @scrips, main->create_scrip_ok( Queue => $queue->id );
120     push @scrips, main->create_scrip_ok( Queue => $queue_B->id );
121     main->check_scrips_order(\@scrips, [$queue, $queue_B]);
122
123     main->move_scrip_ok( $scrips[-1], $queue_B->id, 'up' );
124     @scrips = @scrips[0, 3, 1, 2];
125     main->check_scrips_order(\@scrips, [$queue, $queue_B]);
126 }
127
128 note "move global scrip down with local in between";
129 {
130     main->delete_all_scrips();
131     my @scrips;
132     push @scrips, main->create_scrip_ok( Queue => 0 );
133     push @scrips, main->create_scrip_ok( Queue => $queue_B->id );
134     push @scrips, main->create_scrip_ok( Queue => $queue->id );
135     push @scrips, main->create_scrip_ok( Queue => 0 );
136     push @scrips, main->create_scrip_ok( Queue => $queue->id );
137     main->check_scrips_order(\@scrips, [$queue, $queue_B]);
138
139     main->move_scrip_ok( $scrips[0], 0, 'down' );
140     @scrips = @scrips[1, 2, 3, 0, 4];
141     main->check_scrips_order(\@scrips, [$queue, $queue_B]);
142 }
143
144 note "move global scrip up with local in between";
145 {
146     main->delete_all_scrips();
147     my @scrips;
148     push @scrips, main->create_scrip_ok( Queue => $queue->id );
149     push @scrips, main->create_scrip_ok( Queue => 0 );
150     push @scrips, main->create_scrip_ok( Queue => $queue_B->id );
151     push @scrips, main->create_scrip_ok( Queue => $queue->id );
152     push @scrips, main->create_scrip_ok( Queue => 0 );
153     main->check_scrips_order(\@scrips, [$queue, $queue_B]);
154
155     main->move_scrip_ok( $scrips[-1], 0, 'up' );
156     @scrips = @scrips[0, 4, 1, 2, 3];
157     main->check_scrips_order(\@scrips, [$queue, $queue_B]);
158 }
159
160 note "delete scrips one by one";
161 {
162     main->delete_all_scrips();
163     my @scrips;
164     push @scrips, main->create_scrip_ok( Queue => $queue->id );
165     push @scrips, main->create_scrip_ok( Queue => $queue_B->id );
166     push @scrips, main->create_scrip_ok( Queue => 0 );
167     push @scrips, main->create_scrip_ok( Queue => $queue_B->id );
168     push @scrips, main->create_scrip_ok( Queue => $queue->id );
169     push @scrips, main->create_scrip_ok( Queue => 0 );
170     main->check_scrips_order(\@scrips, [$queue, $queue_B]);
171
172     foreach my $idx (3, 2, 0 ) {
173         $_->Delete foreach splice @scrips, $idx, 1;
174         main->check_scrips_order(\@scrips, [$queue, $queue_B]);
175     }
176 }
177
178 sub create_scrip_ok {
179     local $Test::Builder::Level = $Test::Builder::Level + 1;
180     my $self = shift;
181     my %args = (
182         ScripCondition => 'On Create',
183         ScripAction => 'User Defined', 
184         CustomPrepareCode => 'return 1',
185         CustomCommitCode => 'return 1', 
186         Template => 'Blank',
187         Stage => 'TransactionCreate',
188         @_
189     );
190
191     my $scrip = RT::Scrip->new( RT->SystemUser );
192     my ($id, $msg) = $scrip->Create( %args );
193     ok($id, "Created scrip") or diag "error: $msg";
194
195     return $scrip;
196 }
197
198 sub delete_all_scrips {
199     local $Test::Builder::Level = $Test::Builder::Level + 1;
200     my $self = shift;
201     my $scrips = RT::Scrips->new( RT->SystemUser );
202     $scrips->UnLimit;
203     $_->Delete foreach @{ $scrips->ItemsArrayRef };
204 }
205
206 sub move_scrip_ok {
207     local $Test::Builder::Level = $Test::Builder::Level + 1;
208     my $self = shift;
209     my ($scrip, $queue, $dir) = @_;
210
211     my $rec = RT::ObjectScrip->new( RT->SystemUser );
212     $rec->LoadByCols( Scrip => $scrip->id, ObjectId => $queue );
213     ok $rec->id, 'found application of the scrip';
214
215     my $method = 'Move'. ucfirst lc $dir;
216     my ($status, $msg) = $rec->$method();
217     ok $status, "moved scrip $dir" or diag "error: $msg";
218 }
219
220 sub check_scrips_order {
221     local $Test::Builder::Level = $Test::Builder::Level + 1;
222     my $self = shift;
223     my $scrips = shift;
224     my $queues = shift;
225
226     foreach my $qid ( 0, map $_->id, @$queues ) {
227         my $list = RT::Scrips->new( RT->SystemUser );
228         $list->LimitToGlobal;
229         $list->LimitToQueue( $qid ) if $qid;
230         $list->ApplySortOrder;
231         is_deeply(
232             [map $_->id, @{ $list->ItemsArrayRef } ],
233             [map $_->id, grep $_->IsAdded( $qid ) || $_->IsGlobal, @$scrips],
234             'list of scrips match expected'
235         )
236     }
237
238     foreach my $qid ( map $_->id, @$queues ) {
239         my $list = RT::ObjectScrips->new( RT->SystemUser );
240         $list->LimitToObjectId( 0 );
241         $list->LimitToObjectId( $qid );
242
243         my %so;
244         $so{ $_->SortOrder }++ foreach @{ $list->ItemsArrayRef };
245         ok( !grep( {$_ != 1} values %so), 'no dublicate order' );
246     }
247     {
248         my $list = RT::ObjectScrips->new( RT->SystemUser );
249         $list->UnLimit;
250         $list->OrderBy( FIELD => 'SortOrder', ORDER => 'ASC' );
251
252         my $prev;
253         foreach my $rec ( @{ $list->ItemsArrayRef } ) {
254             my $so = $rec->SortOrder;
255             do { $prev = $so; next } unless defined $prev;
256
257             ok $so == $prev || $so == $prev+1, "sequential order";
258             $prev = $so;
259         }
260     }
261 }
262
263 sub dump_sort_order {
264
265     diag " id oid so";
266     diag join "\n", map { join "\t", @$_ } map @$_, $RT::Handle->dbh->selectall_arrayref(
267         "select Scrip, ObjectId, SortOrder from ObjectScrips ORDER BY SortOrder"
268     );
269
270 }
271
272