1 #!/opt/perl/bin/perl -w
3 # tests relating to searching. Especially around custom fields, and
9 use Test::More tests => 35;
16 my $q = RT::Queue->new($RT::SystemUser);
17 my $queue = 'SearchTests-'.$$;
18 $q->Create(Name => $queue);
19 ok ($q->id, "Created the queue");
23 # we believe the Type shouldn't matter.
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;
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;
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;
42 # we'll need a small pile of them, to test various combinations and nulls.
43 # there's probably a way to think harder and do this with fewer
46 my $t1 = RT::Ticket->new($RT::SystemUser);
47 my ( $id, undef $msg ) = $t1->Create(
49 Subject => 'SearchTest1',
50 Requestor => ['search1@example.com'],
58 my $t2 = RT::Ticket->new($RT::SystemUser);
59 ( $id, undef, $msg ) = $t2->Create(
61 Subject => 'SearchTest2',
62 Requestor => ['search2@example.com'],
69 my $t3 = RT::Ticket->new($RT::SystemUser);
70 ( $id, undef, $msg ) = $t3->Create(
72 Subject => 'SearchTest3',
73 Requestor => ['search3@example.com'],
75 # $cflabel2 => 'bar3',
80 my $t4 = RT::Ticket->new($RT::SystemUser);
81 ( $id, undef, $msg ) = $t4->Create(
83 Subject => 'SearchTest4',
84 Requestor => ['search4@example.com'],
87 # $cflabel3 => 'qux4',
91 my $t5 = RT::Ticket->new($RT::SystemUser);
92 ( $id, undef, $msg ) = $t5->Create(
94 # Subject => 'SearchTest5',
95 Requestor => ['search5@example.com'],
102 my $t6 = RT::Ticket->new($RT::SystemUser);
103 ( $id, undef, $msg ) = $t6->Create(
105 Subject => 'SearchTest6',
106 # Requestor => ['search6@example.com'],
113 my $t7 = RT::Ticket->new($RT::SystemUser);
114 ( $id, undef, $msg ) = $t7->Create(
116 Subject => 'SearchTest7',
117 Requestor => ['search7@example.com'],
118 # $cflabel => 'foo7',
119 # $cflabel2 => 'bar7',
124 # we have tickets. start searching
125 my $tix = RT::Tickets->new($RT::SystemUser);
126 $tix->FromSQL("Queue = '$queue'");
127 is($tix->Count, 7, "found all the tickets");
130 # very simple searches. both CF and normal
132 $tix = RT::Tickets->new($RT::SystemUser);
133 $tix->FromSQL("Queue = '$queue' AND CF.SearchTest = 'foo1'");
134 is($tix->Count, 1, "matched identical subject");
136 $tix = RT::Tickets->new($RT::SystemUser);
137 $tix->FromSQL("Queue = '$queue' AND CF.SearchTest LIKE 'foo1'");
138 is($tix->Count, 1, "matched LIKE subject");
140 $tix = RT::Tickets->new($RT::SystemUser);
141 $tix->FromSQL("Queue = '$queue' AND CF.SearchTest = 'foo'");
142 is($tix->Count, 0, "IS a regexp match");
144 $tix = RT::Tickets->new($RT::SystemUser);
145 $tix->FromSQL("Queue = '$queue' AND CF.SearchTest LIKE 'foo'");
146 is($tix->Count, 5, "matched LIKE subject");
149 $tix = RT::Tickets->new($RT::SystemUser);
150 $tix->FromSQL("Queue = '$queue' AND CF.SearchTest IS NULL");
152 is($tix->Count, 2, "IS null CF");
154 $tix = RT::Tickets->new($RT::SystemUser);
155 $tix->FromSQL("Queue = '$queue' AND Requestors LIKE 'search1'");
156 is($tix->Count, 1, "LIKE requestor");
158 $tix = RT::Tickets->new($RT::SystemUser);
159 $tix->FromSQL("Queue = '$queue' AND Requestors = 'search1\@example.com'");
160 is($tix->Count, 1, "IS requestor");
162 $tix = RT::Tickets->new($RT::SystemUser);
163 $tix->FromSQL("Queue = '$queue' AND Requestors LIKE 'search'");
164 is($tix->Count, 6, "LIKE requestor");
168 local $TODO = "Can't search for 'no requestor";
169 $tix = RT::Tickets->new($RT::SystemUser);
170 $tix->FromSQL("Queue = '$queue' AND Requestors IS NULL");
171 is($tix->Count, 1, "Search for no requestor");
175 $tix = RT::Tickets->new($RT::SystemUser);
176 $tix->FromSQL("Queue = '$queue' AND Subject = 'SearchTest1'");
177 is($tix->Count, 1, "IS subject");
179 $tix = RT::Tickets->new($RT::SystemUser);
180 $tix->FromSQL("Queue = '$queue' AND Subject LIKE 'SearchTest1'");
181 is($tix->Count, 1, "LIKE subject");
183 $tix = RT::Tickets->new($RT::SystemUser);
184 $tix->FromSQL("Queue = '$queue' AND Subject = ''");
185 is($tix->Count, 1, "found one ticket");
187 $tix = RT::Tickets->new($RT::SystemUser);
188 $tix->FromSQL("Queue = '$queue' AND Subject LIKE 'SearchTest'");
189 is($tix->Count, 6, "found two ticket");
191 $tix = RT::Tickets->new($RT::SystemUser);
192 $tix->FromSQL("Queue = '$queue' AND Subject LIKE 'qwerty'");
193 is($tix->Count, 0, "found zero ticket");
198 # various combinations
200 $tix = RT::Tickets->new($RT::SystemUser);
201 $tix->FromSQL("CF.SearchTest LIKE 'foo' AND CF.SearchTest2 LIKE 'bar1'");
202 is($tix->Count, 1, "LIKE cf and LIKE cf");
204 $tix = RT::Tickets->new($RT::SystemUser);
205 $tix->FromSQL("CF.SearchTest = 'foo1' AND CF.SearchTest2 = 'bar1'");
206 is($tix->Count, 1, "is cf and is cf");
208 $tix = RT::Tickets->new($RT::SystemUser);
209 $tix->FromSQL("CF.SearchTest = 'foo' AND CF.SearchTest2 LIKE 'bar1'");
210 is($tix->Count, 0, "is cf and like cf");
212 $tix = RT::Tickets->new($RT::SystemUser);
213 $tix->FromSQL("CF.SearchTest LIKE 'foo' AND CF.SearchTest2 LIKE 'bar' AND CF.SearchTest3 LIKE 'qux'");
214 is($tix->Count, 3, "like cf and like cf and like cf");
216 $tix = RT::Tickets->new($RT::SystemUser);
217 $tix->FromSQL("CF.SearchTest LIKE 'foo' AND CF.SearchTest2 LIKE 'bar' AND CF.SearchTest3 LIKE 'qux6'");
218 is($tix->Count, 1, "like cf and like cf and is cf");
220 $tix = RT::Tickets->new($RT::SystemUser);
221 $tix->FromSQL("CF.SearchTest LIKE 'foo' AND Subject LIKE 'SearchTest'");
222 is($tix->Count, 4, "like cf and like subject");
224 $tix = RT::Tickets->new($RT::SystemUser);
225 $tix->FromSQL("CF.SearchTest IS NULL AND CF.SearchTest2 = 'bar2'");
227 is($tix->Count, 1, "null cf and is cf");
230 $tix = RT::Tickets->new($RT::SystemUser);
231 $tix->FromSQL("Queue = '$queue' AND CF.SearchTest IS NULL AND CF.SearchTest2 IS NULL");
233 is($tix->Count, 1, "null cf and null cf");