fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / ticket / sort_by_cf.t
1
2 use RT::Test nodata => 1, tests => 21;
3 RT::Init();
4
5 use strict;
6 use warnings;
7
8 use RT::Tickets;
9 use RT::Queue;
10 use RT::CustomField;
11
12 my($ret,$msg);
13
14
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
18
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(
23     Name => $queue,
24     Description => 'queue for custom field sort testing'
25 );
26 ok($ret, "$queue test queue creation. $msg");
27
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);
34
35 ($ret, $msg) = $cfO->Create( Name => 'Order',
36                              Queue => 0,
37                              SortOrder => 1,
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");
41
42 ($ret, $msg) = $cfA->Create( Name => 'Alpha',
43                              Queue => $queue_obj->id,
44                              SortOrder => 1,
45                              Description => 'A Testing custom field',
46                              Type=> 'FreeformSingle');
47 ok($ret, "Custom Field Alpha created");
48
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");
54
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");
60
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,
65              Subject => 'One',
66            );
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');
71
72 my $t2 = RT::Ticket->new(RT->SystemUser);
73 $t2->Create( Queue => $queue_obj->Id,
74              Subject => 'Two',
75            );
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');
80
81 # helper
82 sub check_order {
83   my ($tx, @order) = @_;
84   my @results;
85   while (my $t = $tx->Next) {
86     push @results, $t->CustomFieldValues($cfO->Id)->First->Content;
87   }
88   my $results = join (" ",@results);
89   my $order = join(" ",@order);
90   @_ = ($results, $order , "Ordered correctly: $order");
91   goto \&is;
92 }
93
94 # The real tests start here
95 my $tx = RT::Tickets->new( RT->SystemUser );
96
97
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);
103
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);
109
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' );
116 is($tx->Count,2);
117 check_order( $tx, 1, 2);
118
119 $tx = RT::Tickets->new( RT->SystemUser );
120 $tx->FromSQL(qq[queue="$queue"] );
121 $tx->OrderBy( FIELD => "CF.{Charlie}", ORDER => 'ASC' );
122 is($tx->Count,2);
123 check_order( $tx, 2, 1);
124
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,
128              Subject => 'Three',
129            );
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');
134
135 $tx = RT::Tickets->new( RT->SystemUser );
136 $tx->FromSQL(qq[queue="$queue"] );
137 $tx->OrderByCols(
138     { FIELD => "CF.${queue}.{Charlie}", ORDER => 'ASC' },
139     { FIELD => "CF.${queue}.{Alpha}",   ORDER => 'DES' },
140 );
141 is($tx->Count,3);
142 check_order( $tx, 3, 2, 1);
143
144 $tx = RT::Tickets->new( RT->SystemUser );
145 $tx->FromSQL(qq[queue="$queue"] );
146 $tx->OrderByCols(
147     { FIELD => "CF.${queue}.{Charlie}", ORDER => 'DES' },
148     { FIELD => "CF.${queue}.{Alpha}",   ORDER => 'ASC' },
149 );
150 is($tx->Count,3);
151 check_order( $tx, 1, 2, 3);
152
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"] );
157 $tx->OrderByCols(
158     { FIELD => "CF.${queue}.{Charlie}", ORDER => 'ASC' },
159     { FIELD => "CF.${queue}.{Alpha}",   ORDER => 'ASC' },
160 );
161 is($tx->Count,3);
162 check_order( $tx, 2, 3, 1);
163
164 $tx = RT::Tickets->new( RT->SystemUser );
165 $tx->FromSQL(qq[queue="$queue"] );
166 $tx->OrderByCols(
167     { FIELD => "CF.${queue}.{Charlie}", ORDER => 'DES' },
168     { FIELD => "CF.${queue}.{Alpha}",   ORDER => 'DES' },
169 );
170 is($tx->Count,3);
171 check_order( $tx, 1, 3, 2);