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