import rt 3.8.7
[freeside.git] / rt / t / ticket / cfsort-freeform-multiple.t
1 #!/usr/bin/perl
2
3 use RT::Test tests => 24;
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 custom fields.
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_name - test queue creation. $msg");
24 }
25
26 diag "create a CF\n" if $ENV{TEST_VERBOSE};
27 my $cf_name = "Order$$";
28 my $cf;
29 {
30     $cf = RT::CustomField->new( $RT::SystemUser );
31     my ($ret, $msg) = $cf->Create(
32         Name  => $cf_name,
33         Queue => $queue->id,
34         Type  => 'FreeformMultiple',
35     );
36     ok($ret, "Custom Field Order created");
37 }
38
39 my ($total, @data, @tickets, @test) = (0, ());
40
41 sub add_tix_from_data {
42     my @res = ();
43     @data = sort { rand(100) <=> rand(100) } @data;
44     while (@data) {
45         my $t = RT::Ticket->new($RT::SystemUser);
46         my %args = %{ shift(@data) };
47         my @values = ();
48         if ( exists $args{'CF'} && ref $args{'CF'} ) {
49             @values = @{ delete $args{'CF'} };
50         } elsif ( exists $args{'CF'} ) {
51             @values = (delete $args{'CF'});
52         }
53         $args{ 'CustomField-'. $cf->id } = \@values
54             if @values;
55         my $subject = join(",", sort @values) || '-';
56         my ( $id, undef $msg ) = $t->Create(
57             %args,
58             Queue => $queue->id,
59             Subject => $subject,
60         );
61         ok( $id, "ticket created" ) or diag("error: $msg");
62         push @res, $t;
63         $total++;
64     }
65     return @res;
66 }
67
68 sub run_tests {
69     my $query_prefix = join ' OR ', map 'id = '. $_->id, @tickets;
70     foreach my $test ( @test ) {
71         my $query = join " AND ", map "( $_ )", grep defined && length,
72             $query_prefix, $test->{'Query'};
73
74         foreach my $order (qw(ASC DESC)) {
75             my $error = 0;
76             my $tix = RT::Tickets->new( $RT::SystemUser );
77             $tix->FromSQL( $query );
78             $tix->OrderBy( FIELD => $test->{'Order'}, ORDER => $order );
79
80             ok($tix->Count, "found ticket(s)")
81                 or $error = 1;
82
83             my ($order_ok, $last) = (1, $order eq 'ASC'? '-': 'zzzzzz');
84             my $last_id = $tix->Last->id;
85             while ( my $t = $tix->Next ) {
86                 my $tmp;
87                 next if $t->id == $last_id and $t->Subject eq "-"; # Nulls are allowed to come last, in Pg
88
89                 if ( $order eq 'ASC' ) {
90                     $tmp = ((split( /,/, $last))[0] cmp (split( /,/, $t->Subject))[0]);
91                 } else {
92                     $tmp = -((split( /,/, $last))[-1] cmp (split( /,/, $t->Subject))[-1]);
93                 }
94                 if ( $tmp > 0 ) {
95                     $order_ok = 0; last;
96                 }
97                 $last = $t->Subject;
98             }
99
100             ok( $order_ok, "$order order of tickets is good" )
101                 or $error = 1;
102
103             if ( $error ) {
104                 diag "Wrong SQL query:". $tix->BuildSelectQuery;
105                 $tix->GotoFirstItem;
106                 while ( my $t = $tix->Next ) {
107                     diag sprintf "%02d - %s", $t->id, $t->Subject;
108                 }
109             }
110         }
111     }
112 }
113
114 @data = (
115     { },
116     { CF => ['b', 'd'] },
117     { CF => ['a', 'c'] },
118 );
119 @tickets = add_tix_from_data();
120 @test = (
121     { Order => "CF.{$cf_name}" },
122     { Order => "CF.$queue_name.{$cf_name}" },
123 );
124 run_tests();
125
126 @data = (
127     { CF => ['m', 'a'] },
128     { CF => ['m'] },
129     { CF => ['m', 'o'] },
130 );
131 @tickets = add_tix_from_data();
132 @test = (
133     { Order => "CF.{$cf_name}", Query => "CF.{$cf_name} = 'm'" },
134     { Order => "CF.$queue_name.{$cf_name}", Query => "CF.{$cf_name} = 'm'" },
135 );
136 run_tests();
137