import rt 3.8.7
[freeside.git] / rt / t / ticket / sort_by_cf.t
1 #!/usr/bin/perl
2
3 use RT::Test tests => 21;
4 RT::Init();
5
6 use strict;
7 use warnings;
8
9 use RT::Tickets;
10 use RT::Queue;
11 use RT::CustomField;
12
13 my($ret,$msg);
14
15
16 # Test Sorting by custom fields.
17 # TODO: it's hard to read this file, conver to new style,
18 # for example look at 23cfsort-freeform-single.t
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 looking 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 is($tx->Count,2);
118 check_order( $tx, 1, 2);
119
120 $tx = new RT::Tickets( $RT::SystemUser );
121 $tx->FromSQL(qq[queue="$queue"] );
122 $tx->OrderBy( FIELD => "CF.{Charlie}", ORDER => 'ASC' );
123 is($tx->Count,2);
124 check_order( $tx, 2, 1);
125
126 # Add a new ticket, to test sorting on multiple columns.
127 my $t3 = RT::Ticket->new($RT::SystemUser);
128 $t3->Create( Queue => $queue_obj->Id,
129              Subject => 'Three',
130            );
131 $t3->AddCustomFieldValue(Field => $cfO->Id,  Value => '3');
132 $t3->AddCustomFieldValue(Field => $cfA->Id,  Value => '3');
133 $t3->AddCustomFieldValue(Field => $cfB->Id,  Value => '2');
134 $t3->AddCustomFieldValue(Field => $cfC->Id,  Value => 'AAA');
135
136 $tx = new RT::Tickets( $RT::SystemUser );
137 $tx->FromSQL(qq[queue="$queue"] );
138 $tx->OrderByCols(
139     { FIELD => "CF.${queue}.{Charlie}", ORDER => 'ASC' },
140     { FIELD => "CF.${queue}.{Alpha}",   ORDER => 'DES' },
141 );
142 is($tx->Count,3);
143 check_order( $tx, 3, 2, 1);
144
145 $tx = new RT::Tickets( $RT::SystemUser );
146 $tx->FromSQL(qq[queue="$queue"] );
147 $tx->OrderByCols(
148     { FIELD => "CF.${queue}.{Charlie}", ORDER => 'DES' },
149     { FIELD => "CF.${queue}.{Alpha}",   ORDER => 'ASC' },
150 );
151 is($tx->Count,3);
152 check_order( $tx, 1, 2, 3);
153
154 # Reverse the order of the secondary column, which changes the order
155 # of the first two tickets.
156 $tx = new RT::Tickets( $RT::SystemUser );
157 $tx->FromSQL(qq[queue="$queue"] );
158 $tx->OrderByCols(
159     { FIELD => "CF.${queue}.{Charlie}", ORDER => 'ASC' },
160     { FIELD => "CF.${queue}.{Alpha}",   ORDER => 'ASC' },
161 );
162 is($tx->Count,3);
163 check_order( $tx, 2, 3, 1);
164
165 $tx = new RT::Tickets( $RT::SystemUser );
166 $tx->FromSQL(qq[queue="$queue"] );
167 $tx->OrderByCols(
168     { FIELD => "CF.${queue}.{Charlie}", ORDER => 'DES' },
169     { FIELD => "CF.${queue}.{Alpha}",   ORDER => 'DES' },
170 );
171 is($tx->Count,3);
172 check_order( $tx, 1, 3, 2);