3 use RT::Test tests => 57;
12 # Test Sorting by FreeformSingle custom field.
14 diag "Create a queue to test with." if $ENV{TEST_VERBOSE};
15 my $queue_name = "CFSortQueue-$$";
18 $queue = RT::Queue->new( $RT::SystemUser );
19 my ($ret, $msg) = $queue->Create(
21 Description => 'queue for custom field sort testing'
23 ok($ret, "$queue test queue creation. $msg");
26 # CFs for testing, later we create another one
30 diag "create a CF\n" if $ENV{TEST_VERBOSE};
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'},
37 Type => 'FreeformSingle',
39 ok($ret, "Custom Field $CF{'CF'}{'name'} created");
42 my ($total, @data, @tickets, @test) = (0, ());
44 sub add_tix_from_data {
46 @data = sort { rand(100) <=> rand(100) } @data;
48 my $t = RT::Ticket->new($RT::SystemUser);
49 my %args = %{ shift(@data) };
52 foreach my $e ( grep exists $CF{$_} && defined $CF{$_}, keys %args ) {
54 if ( ref $args{ $e } ) {
55 @values = @{ delete $args{ $e } };
57 @values = (delete $args{ $e });
59 $args{ 'CustomField-'. $CF{ $e }{'obj'}->id } = \@values
61 $subject = join(",", sort @values) || '-'
65 my ( $id, undef $msg ) = $t->Create(
70 ok( $id, "ticket created" ) or diag("error: $msg");
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'};
83 foreach my $order (qw(ASC DESC)) {
85 my $tix = RT::Tickets->new( $RT::SystemUser );
86 $tix->FromSQL( $query );
87 $tix->OrderBy( FIELD => $test->{'Order'}, ORDER => $order );
89 ok($tix->Count, "found ticket(s)")
92 my ($order_ok, $last) = (1, $order eq 'ASC'? '-': 'zzzzzz');
93 my $last_id = $tix->Last->id;
94 while ( my $t = $tix->Next ) {
96 next if $t->id == $last_id and $t->Subject eq "-"; # Nulls are allowed to come last, in Pg
98 if ( $order eq 'ASC' ) {
99 $tmp = ((split( /,/, $last))[0] cmp (split( /,/, $t->Subject))[0]);
101 $tmp = -((split( /,/, $last))[-1] cmp (split( /,/, $t->Subject))[-1]);
109 ok( $order_ok, "$order order of tickets is good" )
113 diag "Wrong SQL query:". $tix->BuildSelectQuery;
115 while ( my $t = $tix->Next ) {
116 diag sprintf "%02d - %s", $t->id, $t->Subject;
128 @tickets = add_tix_from_data();
130 { Order => "CF.{$cf_name}" },
131 { Order => "CF.$queue_name.{$cf_name}" },
140 @tickets = add_tix_from_data();
142 { Query => "CF.{$cf_name} LIKE 'a'", Order => "CF.{$cf_name}" },
143 { Query => "CF.{$cf_name} LIKE 'a'", Order => "CF.$queue_name.{$cf_name}" },
149 { Subject => 'a', CF => 'a' },
150 { Subject => 'b', CF => 'b' },
151 { Subject => 'c', CF => 'c' },
153 @tickets = add_tix_from_data();
155 { Query => "CF.{$cf_name} != 'c'", Order => "CF.{$cf_name}" },
156 { Query => "CF.{$cf_name} != 'c'", Order => "CF.$queue_name.{$cf_name}" },
162 diag "create another CF\n" if $ENV{TEST_VERBOSE};
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'},
169 Type => 'FreeformSingle',
171 ok($ret, "Custom Field $CF{'AnotherCF'}{'name'} created");
174 # test that order is not affect by other fields (had such problem)
177 { Subject => 'a', CF => 'a', AnotherCF => 'za' },
178 { Subject => 'b', CF => 'b', AnotherCF => 'ya' },
179 { Subject => 'c', CF => 'c', AnotherCF => 'xa' },
181 @tickets = add_tix_from_data();
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}" },