import rt 3.6.6
[freeside.git] / rt / lib / t / regression / 23cfsort.t
1 #!/usr/bin/perl
2
3 use Test::More tests => 21;
4 use RT;
5 RT::LoadConfig();
6 RT::Init();
7
8 use strict;
9 use warnings;
10
11 use RT::Tickets;
12 use RT::Queue;
13 use RT::CustomField;
14
15 my($ret,$msg);
16
17
18 # Test Sorting by custom fields.
19
20 # ---- Create a queue to test with.
21 my $queue = "CFSortQueue-$$";
22 my $queue_obj = RT::Queue->new( $RT::SystemUser );
23 ($ret, $msg) = $queue_obj->Create(
24     Name => $queue,
25     Description => 'queue for custom field sort testing'
26 );
27 ok($ret, "$queue test queue creation. $msg");
28
29 # ---- Create some custom fields.  We're not currently using all of
30 # them to test with, but the more the merrier.
31 my $cfO = RT::CustomField->new($RT::SystemUser);
32 my $cfA = RT::CustomField->new($RT::SystemUser);
33 my $cfB = RT::CustomField->new($RT::SystemUser);
34 my $cfC = RT::CustomField->new($RT::SystemUser);
35
36 ($ret, $msg) = $cfO->Create( Name => 'Order',
37                              Queue => 0,
38                              SortOrder => 1,
39                              Description => q{Something to compare results for, since we can't guarantee ticket ID},
40                              Type=> 'FreeformSingle');
41 ok($ret, "Custom Field Order created");
42
43 ($ret, $msg) = $cfA->Create( Name => 'Alpha',
44                              Queue => $queue_obj->id,
45                              SortOrder => 1,
46                              Description => 'A Testing custom field',
47                              Type=> 'FreeformSingle');
48 ok($ret, "Custom Field Alpha created");
49
50 ($ret, $msg) = $cfB->Create( Name => 'Beta',
51                              Queue => $queue_obj->id,
52                              Description => 'A Testing custom field',
53                              Type=> 'FreeformSingle');
54 ok($ret, "Custom Field Beta created");
55
56 ($ret, $msg) = $cfC->Create( Name => 'Charlie',
57                              Queue => $queue_obj->id,
58                              Description => 'A Testing custom field',
59                              Type=> 'FreeformSingle');
60 ok($ret, "Custom Field Charlie created");
61
62 # ----- Create some tickets to test with.  Assign them some values to
63 # make it easy to sort with.
64 my $t1 = RT::Ticket->new($RT::SystemUser);
65 $t1->Create( Queue => $queue_obj->Id,
66              Subject => 'One',
67            );
68 $t1->AddCustomFieldValue(Field => $cfO->Id,  Value => '1');
69 $t1->AddCustomFieldValue(Field => $cfA->Id,  Value => '2');
70 $t1->AddCustomFieldValue(Field => $cfB->Id,  Value => '1');
71 $t1->AddCustomFieldValue(Field => $cfC->Id,  Value => 'BBB');
72
73 my $t2 = RT::Ticket->new($RT::SystemUser);
74 $t2->Create( Queue => $queue_obj->Id,
75              Subject => 'Two',
76            );
77 $t2->AddCustomFieldValue(Field => $cfO->Id,  Value => '2');
78 $t2->AddCustomFieldValue(Field => $cfA->Id,  Value => '1');
79 $t2->AddCustomFieldValue(Field => $cfB->Id,  Value => '2');
80 $t2->AddCustomFieldValue(Field => $cfC->Id,  Value => 'AAA');
81
82 # helper
83 sub check_order {
84   my ($tx, @order) = @_;
85   my @results;
86   while (my $t = $tx->Next) {
87     push @results, $t->CustomFieldValues($cfO->Id)->First->Content;
88   }
89   my $results = join (" ",@results);
90   my $order = join(" ",@order);
91   @_ = ($results, $order , "Ordered correctly: $order");
92   goto \&is;
93 }
94
95 # The real tests start here
96 my $tx = new RT::Tickets( $RT::SystemUser );
97
98
99 # Make sure we can sort in both directions on a queue specific field.
100 $tx->FromSQL(qq[queue="$queue"] );
101 $tx->OrderBy( FIELD => "CF.${queue}.{Charlie}", ORDER => 'DES' );
102 is($tx->Count,2 ,"We found 2 tickets when lookign for cf charlie");
103 check_order( $tx, 1, 2);
104
105 $tx = new RT::Tickets( $RT::SystemUser );
106 $tx->FromSQL(qq[queue="$queue"] );
107 $tx->OrderBy( FIELD => "CF.${queue}.{Charlie}", ORDER => 'ASC' );
108 is($tx->Count,2, "We found two tickets when sorting by cf charlie without limiting to it" );
109 check_order( $tx, 2, 1);
110
111 # When ordering by _global_ CustomFields, if more than one queue has a
112 # CF named Charlie, things will go bad.  So, these results are uniqued
113 # in Tickets_Overlay.
114 $tx = new RT::Tickets( $RT::SystemUser );
115 $tx->FromSQL(qq[queue="$queue"] );
116 $tx->OrderBy( FIELD => "CF.{Charlie}", ORDER => 'DESC' );
117 diag $tx->BuildSelectQuery;
118 is($tx->Count,2);
119 TODO: {
120     local $TODO = 'order by CF fail';
121 check_order( $tx, 1, 2);
122 }
123
124 $tx = new RT::Tickets( $RT::SystemUser );
125 $tx->FromSQL(qq[queue="$queue"] );
126 $tx->OrderBy( FIELD => "CF.{Charlie}", ORDER => 'ASC' );
127 diag $tx->BuildSelectQuery;
128 is($tx->Count,2);
129 TODO: {
130     local $TODO = 'order by CF fail';
131 check_order( $tx, 2, 1);
132 }
133
134 # Add a new ticket, to test sorting on multiple columns.
135 my $t3 = RT::Ticket->new($RT::SystemUser);
136 $t3->Create( Queue => $queue_obj->Id,
137              Subject => 'Three',
138            );
139 $t3->AddCustomFieldValue(Field => $cfO->Id,  Value => '3');
140 $t3->AddCustomFieldValue(Field => $cfA->Id,  Value => '3');
141 $t3->AddCustomFieldValue(Field => $cfB->Id,  Value => '2');
142 $t3->AddCustomFieldValue(Field => $cfC->Id,  Value => 'AAA');
143
144 $tx = new RT::Tickets( $RT::SystemUser );
145 $tx->FromSQL(qq[queue="$queue"] );
146 $tx->OrderByCols(
147     { FIELD => "CF.${queue}.{Charlie}", ORDER => 'ASC' },
148     { FIELD => "CF.${queue}.{Alpha}",   ORDER => 'DES' },
149 );
150 is($tx->Count,3);
151 TODO: {
152     local $TODO = 'order by CF fail';
153 check_order( $tx, 3, 2, 1);
154 }
155
156 $tx = new RT::Tickets( $RT::SystemUser );
157 $tx->FromSQL(qq[queue="$queue"] );
158 $tx->OrderByCols(
159     { FIELD => "CF.${queue}.{Charlie}", ORDER => 'DES' },
160     { FIELD => "CF.${queue}.{Alpha}",   ORDER => 'ASC' },
161 );
162 is($tx->Count,3);
163 TODO: {
164     local $TODO = 'order by CF fail';
165 check_order( $tx, 1, 2, 3);
166 }
167
168 # Reverse the order of the secondary column, which changes the order
169 # of the first two tickets.
170 $tx = new RT::Tickets( $RT::SystemUser );
171 $tx->FromSQL(qq[queue="$queue"] );
172 $tx->OrderByCols(
173     { FIELD => "CF.${queue}.{Charlie}", ORDER => 'ASC' },
174     { FIELD => "CF.${queue}.{Alpha}",   ORDER => 'ASC' },
175 );
176 is($tx->Count,3);
177 TODO: {
178     local $TODO = 'order by CF fail';
179 check_order( $tx, 2, 3, 1);
180 }
181
182 $tx = new RT::Tickets( $RT::SystemUser );
183 $tx->FromSQL(qq[queue="$queue"] );
184 $tx->OrderByCols(
185     { FIELD => "CF.${queue}.{Charlie}", ORDER => 'DES' },
186     { FIELD => "CF.${queue}.{Alpha}",   ORDER => 'DES' },
187 );
188 is($tx->Count,3);
189 TODO: {
190     local $TODO = 'order by CF fail';
191 check_order( $tx, 1, 3, 2);
192 }