fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / ticket / search.t
1
2 # tests relating to searching. Especially around custom fields, and
3 # corner cases.
4
5 use strict;
6 use warnings;
7
8 use RT::Test nodata => 1, tests => undef;
9
10 # setup the queue
11
12 my $q = RT::Queue->new(RT->SystemUser);
13 my $queue = 'SearchTests-'.$$;
14 $q->Create(Name => $queue);
15 ok ($q->id, "Created the queue");
16
17
18 # and setup the CFs
19 # we believe the Type shouldn't matter.
20
21 my $cf = RT::CustomField->new(RT->SystemUser);
22 $cf->Create(Name => 'SearchTest', Type => 'Freeform', MaxValues => 0, Queue => $q->id);
23 ok($cf->id, "Created the SearchTest CF");
24 my $cflabel = "CustomField-".$cf->id;
25
26 my $cf2 = RT::CustomField->new(RT->SystemUser);
27 $cf2->Create(Name => 'SearchTest2', Type => 'Freeform', MaxValues => 0, Queue => $q->id);
28 ok($cf2->id, "Created the SearchTest2 CF");
29 my $cflabel2 = "CustomField-".$cf2->id;
30
31 my $cf3 = RT::CustomField->new(RT->SystemUser);
32 $cf3->Create(Name => 'SearchTest3', Type => 'Freeform', MaxValues => 0, Queue => $q->id);
33 ok($cf3->id, "Created the SearchTest3 CF");
34 my $cflabel3 = "CustomField-".$cf3->id;
35
36
37 # There was a bug involving a missing join to ObjectCustomFields that
38 # caused spurious results on negative searches if another custom field
39 # with the same name existed on a different queue.  Hence, we make
40 # duplicate CFs on a different queue here
41 my $dup = RT::Queue->new(RT->SystemUser);
42 $dup->Create(Name => $queue . "-Copy");
43 ok ($dup->id, "Created the duplicate queue");
44 my $dupcf = RT::CustomField->new(RT->SystemUser);
45 $dupcf->Create(Name => 'SearchTest', Type => 'Freeform', MaxValues => 0, Queue => $dup->id);
46 ok($dupcf->id, "Created the duplicate SearchTest CF");
47 $dupcf = RT::CustomField->new(RT->SystemUser);
48 $dupcf->Create(Name => 'SearchTest2', Type => 'Freeform', MaxValues => 0, Queue => $dup->id);
49 ok($dupcf->id, "Created the SearchTest2 CF");
50 $dupcf = RT::CustomField->new(RT->SystemUser);
51 $dupcf->Create(Name => 'SearchTest3', Type => 'Freeform', MaxValues => 0, Queue => $dup->id);
52 ok($dupcf->id, "Created the SearchTest3 CF");
53
54
55 # setup some tickets
56 # we'll need a small pile of them, to test various combinations and nulls.
57 # there's probably a way to think harder and do this with fewer
58
59
60 my $t1 = RT::Ticket->new(RT->SystemUser);
61 my ( $id, undef $msg ) = $t1->Create(
62     Queue      => $q->id,
63     Subject    => 'SearchTest1',
64     Requestor  => ['search1@example.com'],
65     $cflabel   => 'foo1',
66     $cflabel2  => 'bar1',
67     $cflabel3  => 'qux1',
68 );
69 ok( $id, $msg );
70
71
72 my $t2 = RT::Ticket->new(RT->SystemUser);
73 ( $id, undef, $msg ) = $t2->Create(
74     Queue      => $q->id,
75     Subject    => 'SearchTest2',
76     Requestor  => ['search2@example.com'],
77 #    $cflabel   => 'foo2',
78     $cflabel2  => 'bar2',
79     $cflabel3  => 'qux2',
80 );
81 ok( $id, $msg );
82
83 my $t3 = RT::Ticket->new(RT->SystemUser);
84 ( $id, undef, $msg ) = $t3->Create(
85     Queue      => $q->id,
86     Subject    => 'SearchTest3',
87     Requestor  => ['search3@example.com'],
88     $cflabel   => 'foo3',
89 #    $cflabel2  => 'bar3',
90     $cflabel3  => 'qux3',
91 );
92 ok( $id, $msg );
93
94 my $t4 = RT::Ticket->new(RT->SystemUser);
95 ( $id, undef, $msg ) = $t4->Create(
96     Queue      => $q->id,
97     Subject    => 'SearchTest4',
98     Requestor  => ['search4@example.com'],
99     $cflabel   => 'foo4',
100     $cflabel2  => 'bar4',
101 #    $cflabel3  => 'qux4',
102 );
103 ok( $id, $msg );
104
105 my $t5 = RT::Ticket->new(RT->SystemUser);
106 ( $id, undef, $msg ) = $t5->Create(
107     Queue      => $q->id,
108 #    Subject    => 'SearchTest5',
109     Requestor  => ['search5@example.com'],
110     $cflabel   => 'foo5',
111     $cflabel2  => 'bar5',
112     $cflabel3  => 'qux5',
113 );
114 ok( $id, $msg );
115
116 my $t6 = RT::Ticket->new(RT->SystemUser);
117 ( $id, undef, $msg ) = $t6->Create(
118     Queue      => $q->id,
119     Subject    => 'SearchTest6',
120 #    Requestor  => ['search6@example.com'],
121     $cflabel   => 'foo6',
122     $cflabel2  => 'bar6',
123     $cflabel3  => 'qux6',
124 );
125 ok( $id, $msg );
126
127 my $t7 = RT::Ticket->new(RT->SystemUser);
128 ( $id, undef, $msg ) = $t7->Create(
129     Queue      => $q->id,
130     Subject    => 'SearchTest7',
131     Requestor  => ['search7@example.com'],
132 #    $cflabel   => 'foo7',
133 #    $cflabel2  => 'bar7',
134     $cflabel3  => 'qux7',
135 );
136 ok( $id, $msg );
137
138 # we have tickets. start searching
139 my $tix = RT::Tickets->new(RT->SystemUser);
140 $tix->FromSQL("Queue = '$queue'");
141 is($tix->Count, 7, "found all the tickets")
142     or diag "wrong results from SQL:\n". $tix->BuildSelectCountQuery;
143
144
145 # very simple searches. both CF and normal
146
147 $tix = RT::Tickets->new(RT->SystemUser);
148 $tix->FromSQL("Queue = '$queue' AND CF.SearchTest = 'foo1'");
149 is($tix->Count, 1, "matched identical subject")
150     or diag "wrong results from SQL:\n". $tix->BuildSelectCountQuery;
151
152 $tix = RT::Tickets->new(RT->SystemUser);
153 $tix->FromSQL("Queue = '$queue' AND CF.SearchTest LIKE 'foo1'");
154 is($tix->Count, 1, "matched LIKE subject")
155     or diag "wrong results from SQL:\n". $tix->BuildSelectCountQuery;
156
157 $tix = RT::Tickets->new(RT->SystemUser);
158 $tix->FromSQL("Queue = '$queue' AND CF.SearchTest = 'foo'");
159 is($tix->Count, 0, "IS a regexp match")
160     or diag "wrong results from SQL:\n". $tix->BuildSelectCountQuery;
161
162 $tix = RT::Tickets->new(RT->SystemUser);
163 $tix->FromSQL("Queue = '$queue' AND CF.SearchTest LIKE 'foo'");
164 is($tix->Count, 5, "matched LIKE subject")
165     or diag "wrong results from SQL:\n". $tix->BuildSelectCountQuery;
166
167
168 $tix = RT::Tickets->new(RT->SystemUser);
169 $tix->FromSQL("Queue = '$queue' AND CF.SearchTest IS NULL");
170 is($tix->Count, 2, "IS null CF")
171     or diag "wrong results from SQL:\n". $tix->BuildSelectCountQuery;
172
173 $tix = RT::Tickets->new(RT->SystemUser);
174 $tix->FromSQL("Queue = '$queue' AND Requestors LIKE 'search1'");
175 is($tix->Count, 1, "LIKE requestor")
176     or diag "wrong results from SQL:\n". $tix->BuildSelectCountQuery;
177
178 $tix = RT::Tickets->new(RT->SystemUser);
179 $tix->FromSQL("Queue = '$queue' AND Requestors = 'search1\@example.com'");
180 is($tix->Count, 1, "IS requestor")
181     or diag "wrong results from SQL:\n". $tix->BuildSelectCountQuery;
182
183 $tix = RT::Tickets->new(RT->SystemUser);
184 $tix->FromSQL("Queue = '$queue' AND Requestors LIKE 'search'");
185 is($tix->Count, 6, "LIKE requestor")
186     or diag "wrong results from SQL:\n". $tix->BuildSelectCountQuery;
187
188 $tix = RT::Tickets->new(RT->SystemUser);
189 $tix->FromSQL("Queue = '$queue' AND Requestors IS NULL");
190 is($tix->Count, 1, "Search for no requestor")
191     or diag "wrong results from SQL:\n". $tix->BuildSelectCountQuery;
192
193 $tix = RT::Tickets->new(RT->SystemUser);
194 $tix->FromSQL("Queue = '$queue' AND Subject = 'SearchTest1'");
195 is($tix->Count, 1, "IS subject")
196     or diag "wrong results from SQL:\n". $tix->BuildSelectCountQuery;
197
198 $tix = RT::Tickets->new(RT->SystemUser);
199 $tix->FromSQL("Queue = '$queue' AND Subject LIKE 'SearchTest1'");
200 is($tix->Count, 1, "LIKE subject")
201     or diag "wrong results from SQL:\n". $tix->BuildSelectCountQuery;
202
203 $tix = RT::Tickets->new(RT->SystemUser);
204 $tix->FromSQL("Queue = '$queue' AND Subject = ''");
205 is($tix->Count, 1, "found one ticket")
206     or diag "wrong results from SQL:\n". $tix->BuildSelectCountQuery;
207
208 $tix = RT::Tickets->new(RT->SystemUser);
209 $tix->FromSQL("Queue = '$queue' AND Subject LIKE 'SearchTest'");
210 is($tix->Count, 6, "found two ticket")
211     or diag "wrong results from SQL:\n". $tix->BuildSelectCountQuery;
212
213 $tix = RT::Tickets->new(RT->SystemUser);
214 $tix->FromSQL("Queue = '$queue' AND Subject LIKE 'qwerty'");
215 is($tix->Count, 0, "found zero ticket")
216     or diag "wrong results from SQL:\n". $tix->BuildSelectCountQuery;
217
218
219
220
221 # various combinations
222
223 $tix = RT::Tickets->new(RT->SystemUser);
224 $tix->FromSQL("CF.SearchTest LIKE 'foo' AND CF.SearchTest2 LIKE 'bar1'");
225 is($tix->Count, 1, "LIKE cf and LIKE cf");
226
227 $tix = RT::Tickets->new(RT->SystemUser);
228 $tix->FromSQL("CF.SearchTest = 'foo1' AND CF.SearchTest2 = 'bar1'");
229 is($tix->Count, 1, "is cf and is cf");
230
231 $tix = RT::Tickets->new(RT->SystemUser);
232 $tix->FromSQL("CF.SearchTest = 'foo' AND CF.SearchTest2 LIKE 'bar1'");
233 is($tix->Count, 0, "is cf and like cf");
234
235 $tix = RT::Tickets->new(RT->SystemUser);
236 $tix->FromSQL("CF.SearchTest LIKE 'foo' AND CF.SearchTest2 LIKE 'bar' AND CF.SearchTest3 LIKE 'qux'");
237 is($tix->Count, 3, "like cf and like cf and like cf");
238
239 $tix = RT::Tickets->new(RT->SystemUser);
240 $tix->FromSQL("CF.SearchTest LIKE 'foo' AND CF.SearchTest2 LIKE 'bar' AND CF.SearchTest3 LIKE 'qux6'");
241 is($tix->Count, 1, "like cf and like cf and is cf");
242
243 $tix = RT::Tickets->new(RT->SystemUser);
244 $tix->FromSQL("CF.SearchTest LIKE 'foo' AND Subject LIKE 'SearchTest'");
245 is($tix->Count, 4, "like cf and like subject");
246
247 $tix = RT::Tickets->new(RT->SystemUser);
248 $tix->FromSQL("CF.SearchTest IS NULL AND CF.SearchTest2 = 'bar2'");
249 is($tix->Count, 1, "null cf and is cf");
250
251
252 $tix = RT::Tickets->new(RT->SystemUser);
253 $tix->FromSQL("Queue = '$queue' AND CF.SearchTest IS NULL AND CF.SearchTest2 IS NULL");
254 is($tix->Count, 1, "null cf and null cf"); 
255
256 # tests with the same CF listed twice
257
258 $tix = RT::Tickets->new(RT->SystemUser);
259 $tix->FromSQL("CF.{SearchTest} = 'foo1'");
260 is($tix->Count, 1, "is cf.{name} format");
261
262 $tix = RT::Tickets->new(RT->SystemUser);
263 $tix->FromSQL("CF.SearchTest = 'foo1' OR CF.SearchTest = 'foo3'");
264 is($tix->Count, 2, "is cf1 or is cf1");
265
266 $tix = RT::Tickets->new(RT->SystemUser);
267 $tix->FromSQL("CF.SearchTest = 'foo1' OR CF.SearchTest IS NULL");
268 is($tix->Count, 3, "is cf1 or null cf1");
269
270 $tix = RT::Tickets->new(RT->SystemUser);
271 $tix->FromSQL("(CF.SearchTest = 'foo1' OR CF.SearchTest = 'foo3') AND (CF.SearchTest2 = 'bar1' OR CF.SearchTest2 = 'bar2')");
272 is($tix->Count, 1, "(is cf1 or is cf1) and (is cf2 or is cf2)");
273
274 $tix = RT::Tickets->new(RT->SystemUser);
275 $tix->FromSQL("CF.SearchTest = 'foo1' OR CF.SearchTest = 'foo3' OR CF.SearchTest2 = 'bar1' OR CF.SearchTest2 = 'bar2'");
276 is($tix->Count, 3, "is cf1 or is cf1 or is cf2 or is cf2");
277
278 # tests with lower cased NULL
279 $tix = RT::Tickets->new(RT->SystemUser);
280 $tix->FromSQL('Requestor.Name IS null');
281 is($tix->Count, 1, "t6 doesn't have a Requestor");
282 like($tix->BuildSelectCountQuery, qr/\bNULL\b/, "Contains upper-case NULL");
283 unlike($tix->BuildSelectCountQuery, qr/\bnull\b/, "Lacks lower-case NULL");
284
285
286 # tests for searching by queue lifecycle
287 $tix = RT::Tickets->new(RT->SystemUser);
288 $tix->FromSQL('Lifecycle="default"');
289 is($tix->Count,7,"We found all 7 tickets in a queue with the default lifecycle");
290
291 $tix = RT::Tickets->new(RT->SystemUser);
292 $tix->FromSQL('Lifecycle ="approvals" OR Lifecycle="default"');
293 is($tix->Count,7,"We found 7 tickets in a queue with a lifecycle of default or approvals");
294
295 $tix = RT::Tickets->new(RT->SystemUser);
296 $tix->FromSQL('Lifecycle ="approvals" AND Lifecycle="default"');
297 is($tix->Count,0,"We found 0 tickets in a queue with a lifecycle of default AND approvals...(because that's impossible");
298
299 $tix = RT::Tickets->new(RT->SystemUser);
300 $tix->FromSQL('Queue="'.$queue.'" AND Lifecycle="default"');
301 is($tix->Count,7,"We found 7 tickets in $queue with a lifecycle of default");
302
303
304 $tix = RT::Tickets->new(RT->SystemUser);
305 $tix->FromSQL('Lifecycle !="approvals"');
306 is($tix->Count,7,"We found 7 tickets in a queue with a lifecycle other than approvals");
307
308 $tix = RT::Tickets->new(RT->SystemUser);
309 $tix->FromSQL('Lifecycle!="default"');
310 is($tix->Count,0,"We found 0 tickets in a queue with a lifecycle other than default");
311
312 $tix = RT::Tickets->new(RT->SystemUser);
313 $tix->FromSQL('Lifecycle="approvals"');
314 is($tix->Count,0,"We found 0 tickets in a queue with the approvals lifecycle");
315
316
317 done_testing;