2 use RT::Test nodata => 1, tests => 21;
15 # Test Sorting by custom fields.
16 # TODO: it's hard to read this file, conver to new style,
17 # for example look at 23cfsort-freeform-single.t
19 # ---- Create a queue to test with.
20 my $queue = "CFSortQueue-$$";
21 my $queue_obj = RT::Queue->new( RT->SystemUser );
22 ($ret, $msg) = $queue_obj->Create(
24 Description => 'queue for custom field sort testing'
26 ok($ret, "$queue test queue creation. $msg");
28 # ---- Create some custom fields. We're not currently using all of
29 # them to test with, but the more the merrier.
30 my $cfO = RT::CustomField->new(RT->SystemUser);
31 my $cfA = RT::CustomField->new(RT->SystemUser);
32 my $cfB = RT::CustomField->new(RT->SystemUser);
33 my $cfC = RT::CustomField->new(RT->SystemUser);
35 ($ret, $msg) = $cfO->Create( Name => 'Order',
38 Description => q{Something to compare results for, since we can't guarantee ticket ID},
39 Type=> 'FreeformSingle');
40 ok($ret, "Custom Field Order created");
42 ($ret, $msg) = $cfA->Create( Name => 'Alpha',
43 Queue => $queue_obj->id,
45 Description => 'A Testing custom field',
46 Type=> 'FreeformSingle');
47 ok($ret, "Custom Field Alpha created");
49 ($ret, $msg) = $cfB->Create( Name => 'Beta',
50 Queue => $queue_obj->id,
51 Description => 'A Testing custom field',
52 Type=> 'FreeformSingle');
53 ok($ret, "Custom Field Beta created");
55 ($ret, $msg) = $cfC->Create( Name => 'Charlie',
56 Queue => $queue_obj->id,
57 Description => 'A Testing custom field',
58 Type=> 'FreeformSingle');
59 ok($ret, "Custom Field Charlie created");
61 # ----- Create some tickets to test with. Assign them some values to
62 # make it easy to sort with.
63 my $t1 = RT::Ticket->new(RT->SystemUser);
64 $t1->Create( Queue => $queue_obj->Id,
67 $t1->AddCustomFieldValue(Field => $cfO->Id, Value => '1');
68 $t1->AddCustomFieldValue(Field => $cfA->Id, Value => '2');
69 $t1->AddCustomFieldValue(Field => $cfB->Id, Value => '1');
70 $t1->AddCustomFieldValue(Field => $cfC->Id, Value => 'BBB');
72 my $t2 = RT::Ticket->new(RT->SystemUser);
73 $t2->Create( Queue => $queue_obj->Id,
76 $t2->AddCustomFieldValue(Field => $cfO->Id, Value => '2');
77 $t2->AddCustomFieldValue(Field => $cfA->Id, Value => '1');
78 $t2->AddCustomFieldValue(Field => $cfB->Id, Value => '2');
79 $t2->AddCustomFieldValue(Field => $cfC->Id, Value => 'AAA');
83 my ($tx, @order) = @_;
85 while (my $t = $tx->Next) {
86 push @results, $t->CustomFieldValues($cfO->Id)->First->Content;
88 my $results = join (" ",@results);
89 my $order = join(" ",@order);
90 @_ = ($results, $order , "Ordered correctly: $order");
94 # The real tests start here
95 my $tx = RT::Tickets->new( RT->SystemUser );
98 # Make sure we can sort in both directions on a queue specific field.
99 $tx->FromSQL(qq[queue="$queue"] );
100 $tx->OrderBy( FIELD => "CF.${queue}.{Charlie}", ORDER => 'DES' );
101 is($tx->Count,2 ,"We found 2 tickets when looking for cf charlie");
102 check_order( $tx, 1, 2);
104 $tx = RT::Tickets->new( RT->SystemUser );
105 $tx->FromSQL(qq[queue="$queue"] );
106 $tx->OrderBy( FIELD => "CF.${queue}.{Charlie}", ORDER => 'ASC' );
107 is($tx->Count,2, "We found two tickets when sorting by cf charlie without limiting to it" );
108 check_order( $tx, 2, 1);
110 # When ordering by _global_ CustomFields, if more than one queue has a
111 # CF named Charlie, things will go bad. So, these results are uniqued
112 # in Tickets_Overlay.
113 $tx = RT::Tickets->new( RT->SystemUser );
114 $tx->FromSQL(qq[queue="$queue"] );
115 $tx->OrderBy( FIELD => "CF.{Charlie}", ORDER => 'DESC' );
117 check_order( $tx, 1, 2);
119 $tx = RT::Tickets->new( RT->SystemUser );
120 $tx->FromSQL(qq[queue="$queue"] );
121 $tx->OrderBy( FIELD => "CF.{Charlie}", ORDER => 'ASC' );
123 check_order( $tx, 2, 1);
125 # Add a new ticket, to test sorting on multiple columns.
126 my $t3 = RT::Ticket->new(RT->SystemUser);
127 $t3->Create( Queue => $queue_obj->Id,
130 $t3->AddCustomFieldValue(Field => $cfO->Id, Value => '3');
131 $t3->AddCustomFieldValue(Field => $cfA->Id, Value => '3');
132 $t3->AddCustomFieldValue(Field => $cfB->Id, Value => '2');
133 $t3->AddCustomFieldValue(Field => $cfC->Id, Value => 'AAA');
135 $tx = RT::Tickets->new( RT->SystemUser );
136 $tx->FromSQL(qq[queue="$queue"] );
138 { FIELD => "CF.${queue}.{Charlie}", ORDER => 'ASC' },
139 { FIELD => "CF.${queue}.{Alpha}", ORDER => 'DES' },
142 check_order( $tx, 3, 2, 1);
144 $tx = RT::Tickets->new( RT->SystemUser );
145 $tx->FromSQL(qq[queue="$queue"] );
147 { FIELD => "CF.${queue}.{Charlie}", ORDER => 'DES' },
148 { FIELD => "CF.${queue}.{Alpha}", ORDER => 'ASC' },
151 check_order( $tx, 1, 2, 3);
153 # Reverse the order of the secondary column, which changes the order
154 # of the first two tickets.
155 $tx = RT::Tickets->new( RT->SystemUser );
156 $tx->FromSQL(qq[queue="$queue"] );
158 { FIELD => "CF.${queue}.{Charlie}", ORDER => 'ASC' },
159 { FIELD => "CF.${queue}.{Alpha}", ORDER => 'ASC' },
162 check_order( $tx, 2, 3, 1);
164 $tx = RT::Tickets->new( RT->SystemUser );
165 $tx->FromSQL(qq[queue="$queue"] );
167 { FIELD => "CF.${queue}.{Charlie}", ORDER => 'DES' },
168 { FIELD => "CF.${queue}.{Alpha}", ORDER => 'DES' },
171 check_order( $tx, 1, 3, 2);