import rt 3.8.7
[freeside.git] / rt / t / ticket / cfsort-freeform-single.t
1 #!/usr/bin/perl
2
3 use RT::Test tests => 57;
4
5 use strict;
6 use warnings;
7
8 use RT::Tickets;
9 use RT::Queue;
10 use RT::CustomField;
11
12 # Test Sorting by FreeformSingle custom field.
13
14 diag "Create a queue to test with." if $ENV{TEST_VERBOSE};
15 my $queue_name = "CFSortQueue-$$";
16 my $queue;
17 {
18     $queue = RT::Queue->new( $RT::SystemUser );
19     my ($ret, $msg) = $queue->Create(
20         Name => $queue_name,
21         Description => 'queue for custom field sort testing'
22     );
23     ok($ret, "$queue test queue creation. $msg");
24 }
25
26 # CFs for testing, later we create another one
27 my %CF;
28 my $cf_name;
29
30 diag "create a CF\n" if $ENV{TEST_VERBOSE};
31 {
32     $cf_name = $CF{'CF'}{'name'} = "Order$$";
33     $CF{'CF'}{'obj'} = RT::CustomField->new( $RT::SystemUser );
34     my ($ret, $msg) = $CF{'CF'}{'obj'}->Create(
35         Name  => $CF{'CF'}{'name'},
36         Queue => $queue->id,
37         Type  => 'FreeformSingle',
38     );
39     ok($ret, "Custom Field $CF{'CF'}{'name'} created");
40 }
41
42 my ($total, @data, @tickets, @test) = (0, ());
43
44 sub add_tix_from_data {
45     my @res = ();
46     @data = sort { rand(100) <=> rand(100) } @data;
47     while (@data) {
48         my $t = RT::Ticket->new($RT::SystemUser);
49         my %args = %{ shift(@data) };
50
51         my $subject = '-';
52         foreach my $e ( grep exists $CF{$_} && defined $CF{$_}, keys %args ) {
53             my @values = ();
54             if ( ref $args{ $e } ) {
55                 @values = @{ delete $args{ $e } };
56             } else {
57                 @values = (delete $args{ $e });
58             }
59             $args{ 'CustomField-'. $CF{ $e }{'obj'}->id } = \@values
60                 if @values;
61             $subject = join(",", sort @values) || '-'
62                 if $e eq 'CF';
63         }
64
65         my ( $id, undef $msg ) = $t->Create(
66             %args,
67             Queue => $queue->id,
68             Subject => $subject,
69         );
70         ok( $id, "ticket created" ) or diag("error: $msg");
71         push @res, $t;
72         $total++;
73     }
74     return @res;
75 }
76
77 sub run_tests {
78     my $query_prefix = join ' OR ', map 'id = '. $_->id, @tickets;
79     foreach my $test ( @test ) {
80         my $query = join " AND ", map "( $_ )", grep defined && length,
81             $query_prefix, $test->{'Query'};
82
83         foreach my $order (qw(ASC DESC)) {
84             my $error = 0;
85             my $tix = RT::Tickets->new( $RT::SystemUser );
86             $tix->FromSQL( $query );
87             $tix->OrderBy( FIELD => $test->{'Order'}, ORDER => $order );
88
89             ok($tix->Count, "found ticket(s)")
90                 or $error = 1;
91
92             my ($order_ok, $last) = (1, $order eq 'ASC'? '-': 'zzzzzz');
93             my $last_id = $tix->Last->id;
94             while ( my $t = $tix->Next ) {
95                 my $tmp;
96                 next if $t->id == $last_id and $t->Subject eq "-"; # Nulls are allowed to come last, in Pg
97
98                 if ( $order eq 'ASC' ) {
99                     $tmp = ((split( /,/, $last))[0] cmp (split( /,/, $t->Subject))[0]);
100                 } else {
101                     $tmp = -((split( /,/, $last))[-1] cmp (split( /,/, $t->Subject))[-1]);
102                 }
103                 if ( $tmp > 0 ) {
104                     $order_ok = 0; last;
105                 }
106                 $last = $t->Subject;
107             }
108
109             ok( $order_ok, "$order order of tickets is good" )
110                 or $error = 1;
111
112             if ( $error ) {
113                 diag "Wrong SQL query:". $tix->BuildSelectQuery;
114                 $tix->GotoFirstItem;
115                 while ( my $t = $tix->Next ) {
116                     diag sprintf "%02d - %s", $t->id, $t->Subject;
117                 }
118             }
119         }
120     }
121 }
122
123 @data = (
124     { },
125     { CF => 'a' },
126     { CF => 'b' },
127 );
128 @tickets = add_tix_from_data();
129 @test = (
130     { Order => "CF.{$cf_name}" },
131     { Order => "CF.$queue_name.{$cf_name}" },
132 );
133 run_tests();
134
135 @data = (
136     { },
137     { CF => 'aa' },
138     { CF => 'ab' },
139 );
140 @tickets = add_tix_from_data();
141 @test = (
142     { Query => "CF.{$cf_name} LIKE 'a'", Order => "CF.{$cf_name}" },
143     { Query => "CF.{$cf_name} LIKE 'a'", Order => "CF.$queue_name.{$cf_name}" },
144 );
145 run_tests();
146
147 @data = (
148     { Subject => '-', },
149     { Subject => 'a', CF => 'a' },
150     { Subject => 'b', CF => 'b' },
151     { Subject => 'c', CF => 'c' },
152 );
153 @tickets = add_tix_from_data();
154 @test = (
155     { Query => "CF.{$cf_name} != 'c'", Order => "CF.{$cf_name}" },
156     { Query => "CF.{$cf_name} != 'c'", Order => "CF.$queue_name.{$cf_name}" },
157 );
158 run_tests();
159
160
161
162 diag "create another CF\n" if $ENV{TEST_VERBOSE};
163 {
164     $CF{'AnotherCF'}{'name'} = "OrderAnother$$";
165     $CF{'AnotherCF'}{'obj'} = RT::CustomField->new( $RT::SystemUser );
166     my ($ret, $msg) = $CF{'AnotherCF'}{'obj'}->Create(
167         Name  => $CF{'AnotherCF'}{'name'},
168         Queue => $queue->id,
169         Type  => 'FreeformSingle',
170     );
171     ok($ret, "Custom Field $CF{'AnotherCF'}{'name'} created");
172 }
173
174 # test that order is not affect by other fields (had such problem)
175 @data = (
176     { Subject => '-', },
177     { Subject => 'a', CF => 'a', AnotherCF => 'za' },
178     { Subject => 'b', CF => 'b', AnotherCF => 'ya' },
179     { Subject => 'c', CF => 'c', AnotherCF => 'xa' },
180 );
181 @tickets = add_tix_from_data();
182 @test = (
183     { Order => "CF.{$cf_name}" },
184     { Order => "CF.$queue_name.{$cf_name}" },
185     { Query => "CF.{$cf_name} != 'c'", Order => "CF.{$cf_name}" },
186     { Query => "CF.{$cf_name} != 'c'", Order => "CF.$queue_name.{$cf_name}" },
187 );
188 run_tests();
189
190
191