3 use RT::Test tests => 21;
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
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 looking 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' );
118 check_order( $tx, 1, 2);
120 $tx = new RT::Tickets( $RT::SystemUser );
121 $tx->FromSQL(qq[queue="$queue"] );
122 $tx->OrderBy( FIELD => "CF.{Charlie}", ORDER => 'ASC' );
124 check_order( $tx, 2, 1);
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,
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');
136 $tx = new RT::Tickets( $RT::SystemUser );
137 $tx->FromSQL(qq[queue="$queue"] );
139 { FIELD => "CF.${queue}.{Charlie}", ORDER => 'ASC' },
140 { FIELD => "CF.${queue}.{Alpha}", ORDER => 'DES' },
143 check_order( $tx, 3, 2, 1);
145 $tx = new RT::Tickets( $RT::SystemUser );
146 $tx->FromSQL(qq[queue="$queue"] );
148 { FIELD => "CF.${queue}.{Charlie}", ORDER => 'DES' },
149 { FIELD => "CF.${queue}.{Alpha}", ORDER => 'ASC' },
152 check_order( $tx, 1, 2, 3);
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"] );
159 { FIELD => "CF.${queue}.{Charlie}", ORDER => 'ASC' },
160 { FIELD => "CF.${queue}.{Alpha}", ORDER => 'ASC' },
163 check_order( $tx, 2, 3, 1);
165 $tx = new RT::Tickets( $RT::SystemUser );
166 $tx->FromSQL(qq[queue="$queue"] );
168 { FIELD => "CF.${queue}.{Charlie}", ORDER => 'DES' },
169 { FIELD => "CF.${queue}.{Alpha}", ORDER => 'DES' },
172 check_order( $tx, 1, 3, 2);