This commit was generated by cvs2svn to compensate for changes in r4407,
[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 => 35;
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 # setup some tickets
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
44
45
46 my $t1 = RT::Ticket->new($RT::SystemUser);
47 my ( $id, undef $msg ) = $t1->Create(
48     Queue      => $q->id,
49     Subject    => 'SearchTest1',
50     Requestor  => ['search1@example.com'],
51     $cflabel   => 'foo1',
52     $cflabel2  => 'bar1',
53     $cflabel3  => 'qux1',
54 );
55 ok( $id, $msg );
56
57
58 my $t2 = RT::Ticket->new($RT::SystemUser);
59 ( $id, undef, $msg ) = $t2->Create(
60     Queue      => $q->id,
61     Subject    => 'SearchTest2',
62     Requestor  => ['search2@example.com'],
63 #    $cflabel   => 'foo2',
64     $cflabel2  => 'bar2',
65     $cflabel3  => 'qux2',
66 );
67 ok( $id, $msg );
68
69 my $t3 = RT::Ticket->new($RT::SystemUser);
70 ( $id, undef, $msg ) = $t3->Create(
71     Queue      => $q->id,
72     Subject    => 'SearchTest3',
73     Requestor  => ['search3@example.com'],
74     $cflabel   => 'foo3',
75 #    $cflabel2  => 'bar3',
76     $cflabel3  => 'qux3',
77 );
78 ok( $id, $msg );
79
80 my $t4 = RT::Ticket->new($RT::SystemUser);
81 ( $id, undef, $msg ) = $t4->Create(
82     Queue      => $q->id,
83     Subject    => 'SearchTest4',
84     Requestor  => ['search4@example.com'],
85     $cflabel   => 'foo4',
86     $cflabel2  => 'bar4',
87 #    $cflabel3  => 'qux4',
88 );
89 ok( $id, $msg );
90
91 my $t5 = RT::Ticket->new($RT::SystemUser);
92 ( $id, undef, $msg ) = $t5->Create(
93     Queue      => $q->id,
94 #    Subject    => 'SearchTest5',
95     Requestor  => ['search5@example.com'],
96     $cflabel   => 'foo5',
97     $cflabel2  => 'bar5',
98     $cflabel3  => 'qux5',
99 );
100 ok( $id, $msg );
101
102 my $t6 = RT::Ticket->new($RT::SystemUser);
103 ( $id, undef, $msg ) = $t6->Create(
104     Queue      => $q->id,
105     Subject    => 'SearchTest6',
106 #    Requestor  => ['search6@example.com'],
107     $cflabel   => 'foo6',
108     $cflabel2  => 'bar6',
109     $cflabel3  => 'qux6',
110 );
111 ok( $id, $msg );
112
113 my $t7 = RT::Ticket->new($RT::SystemUser);
114 ( $id, undef, $msg ) = $t7->Create(
115     Queue      => $q->id,
116     Subject    => 'SearchTest7',
117     Requestor  => ['search7@example.com'],
118 #    $cflabel   => 'foo7',
119 #    $cflabel2  => 'bar7',
120     $cflabel3  => 'qux7',
121 );
122 ok( $id, $msg );
123
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");
128
129
130 # very simple searches. both CF and normal
131
132 $tix = RT::Tickets->new($RT::SystemUser);
133 $tix->FromSQL("Queue = '$queue' AND CF.SearchTest = 'foo1'");
134 is($tix->Count, 1, "matched identical subject");
135
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");
139
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");
143
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");
147
148
149 $tix = RT::Tickets->new($RT::SystemUser);
150 $tix->FromSQL("Queue = '$queue' AND CF.SearchTest IS NULL");
151     
152     is($tix->Count, 2, "IS null CF");
153
154 $tix = RT::Tickets->new($RT::SystemUser);
155 $tix->FromSQL("Queue = '$queue' AND Requestors LIKE 'search1'");
156 is($tix->Count, 1, "LIKE requestor");
157
158 $tix = RT::Tickets->new($RT::SystemUser);
159 $tix->FromSQL("Queue = '$queue' AND Requestors = 'search1\@example.com'");
160 is($tix->Count, 1, "IS requestor");
161
162 $tix = RT::Tickets->new($RT::SystemUser);
163 $tix->FromSQL("Queue = '$queue' AND Requestors LIKE 'search'");
164 is($tix->Count, 6, "LIKE requestor");
165
166 TODO: {
167     
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");
172
173 };
174
175 $tix = RT::Tickets->new($RT::SystemUser);
176 $tix->FromSQL("Queue = '$queue' AND Subject = 'SearchTest1'");
177 is($tix->Count, 1, "IS subject");
178
179 $tix = RT::Tickets->new($RT::SystemUser);
180 $tix->FromSQL("Queue = '$queue' AND Subject LIKE 'SearchTest1'");
181 is($tix->Count, 1, "LIKE subject");
182
183 $tix = RT::Tickets->new($RT::SystemUser);
184 $tix->FromSQL("Queue = '$queue' AND Subject = ''");
185 is($tix->Count, 1, "found one ticket");
186
187 $tix = RT::Tickets->new($RT::SystemUser);
188 $tix->FromSQL("Queue = '$queue' AND Subject LIKE 'SearchTest'");
189 is($tix->Count, 6, "found two ticket");
190
191 $tix = RT::Tickets->new($RT::SystemUser);
192 $tix->FromSQL("Queue = '$queue' AND Subject LIKE 'qwerty'");
193 is($tix->Count, 0, "found zero ticket");
194
195
196
197
198 # various combinations
199
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");
203
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");
207
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");
211
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");
215
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");
219
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");
223
224 $tix = RT::Tickets->new($RT::SystemUser);
225 $tix->FromSQL("CF.SearchTest IS NULL AND CF.SearchTest2 = 'bar2'");
226     
227     is($tix->Count, 1, "null cf and is cf");
228
229
230 $tix = RT::Tickets->new($RT::SystemUser);
231 $tix->FromSQL("Queue = '$queue' AND CF.SearchTest IS NULL AND CF.SearchTest2 IS NULL");
232
233         is($tix->Count, 1, "null cf and null cf"); 
234
235