3 use Test::More tests => 21;
18 # Test Sorting by custom fields.
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(
25 Description => 'queue for custom field sort testing'
27 ok($ret, "$queue test queue creation. $msg");
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);
36 ($ret, $msg) = $cfO->Create( Name => 'Order',
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");
43 ($ret, $msg) = $cfA->Create( Name => 'Alpha',
44 Queue => $queue_obj->id,
46 Description => 'A Testing custom field',
47 Type=> 'FreeformSingle');
48 ok($ret, "Custom Field Alpha created");
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");
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");
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,
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');
73 my $t2 = RT::Ticket->new($RT::SystemUser);
74 $t2->Create( Queue => $queue_obj->Id,
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');
84 my ($tx, @order) = @_;
86 while (my $t = $tx->Next) {
87 push @results, $t->CustomFieldValues($cfO->Id)->First->Content;
89 my $results = join (" ",@results);
90 my $order = join(" ",@order);
91 @_ = ($results, $order , "Ordered correctly: $order");
95 # The real tests start here
96 my $tx = new RT::Tickets( $RT::SystemUser );
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);
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);
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;
120 local $TODO = 'order by CF fail';
121 check_order( $tx, 1, 2);
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;
130 local $TODO = 'order by CF fail';
131 check_order( $tx, 2, 1);
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,
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');
144 $tx = new RT::Tickets( $RT::SystemUser );
145 $tx->FromSQL(qq[queue="$queue"] );
147 { FIELD => "CF.${queue}.{Charlie}", ORDER => 'ASC' },
148 { FIELD => "CF.${queue}.{Alpha}", ORDER => 'DES' },
152 local $TODO = 'order by CF fail';
153 check_order( $tx, 3, 2, 1);
156 $tx = new RT::Tickets( $RT::SystemUser );
157 $tx->FromSQL(qq[queue="$queue"] );
159 { FIELD => "CF.${queue}.{Charlie}", ORDER => 'DES' },
160 { FIELD => "CF.${queue}.{Alpha}", ORDER => 'ASC' },
164 local $TODO = 'order by CF fail';
165 check_order( $tx, 1, 2, 3);
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"] );
173 { FIELD => "CF.${queue}.{Charlie}", ORDER => 'ASC' },
174 { FIELD => "CF.${queue}.{Alpha}", ORDER => 'ASC' },
178 local $TODO = 'order by CF fail';
179 check_order( $tx, 2, 3, 1);
182 $tx = new RT::Tickets( $RT::SystemUser );
183 $tx->FromSQL(qq[queue="$queue"] );
185 { FIELD => "CF.${queue}.{Charlie}", ORDER => 'DES' },
186 { FIELD => "CF.${queue}.{Alpha}", ORDER => 'DES' },
190 local $TODO = 'order by CF fail';
191 check_order( $tx, 1, 3, 2);