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