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