6 use Test::More tests => 79;
12 my $q = RT::Queue->new( $RT::SystemUser );
13 my $queue = 'SearchTests-'. rand(200);
14 $q->Create( Name => $queue );
16 my ($total, @data, @tickets, %test) = (0, ());
18 sub add_tix_from_data {
21 my $t = RT::Ticket->new($RT::SystemUser);
22 my ( $id, undef $msg ) = $t->Create(
26 ok( $id, "ticket created" ) or diag("error: $msg");
34 my $query_prefix = join ' OR ', map 'id = '. $_->id, @tickets;
35 foreach my $key ( sort keys %test ) {
36 my $tix = RT::Tickets->new($RT::SystemUser);
37 $tix->FromSQL( "( $query_prefix ) AND ( $key )" );
42 $count++ foreach grep $_, values %{ $test{$key} };
44 local $TODO = "we can't generate this query yet";
45 is($tix->Count, $count, "found correct number of ticket(s) by '$key'") or $error = 1;
49 while ( my $ticket = $tix->Next ) {
50 next if $test{$key}->{ $ticket->Subject };
51 diag $ticket->Subject ." ticket has been found when it's not expected";
54 ok( $good_tickets, "all tickets are good with '$key'" ) or $error = 1;
56 diag "Wrong SQL query for '$key':". $tix->BuildSelectQuery if $error;
61 { Subject => 'xy', Requestor => ['x@example.com', 'y@example.com'] },
62 { Subject => 'x', Requestor => 'x@example.com' },
63 { Subject => 'y', Requestor => 'y@example.com' },
65 { Subject => 'z', Requestor => 'z@example.com' },
68 'Requestor = "x@example.com"' => { xy => 1, x => 1, y => 0, '-' => 0, z => 0 },
69 'Requestor != "x@example.com"' => { xy => 0, x => 0, y => 1, '-' => 1, z => 1 },
71 'Requestor = "y@example.com"' => { xy => 1, x => 0, y => 1, '-' => 0, z => 0 },
72 'Requestor != "y@example.com"' => { xy => 0, x => 1, y => 0, '-' => 1, z => 1 },
74 'Requestor LIKE "@example.com"' => { xy => 1, x => 1, y => 1, '-' => 0, z => 1 },
75 'Requestor NOT LIKE "@example.com"' => { xy => 0, x => 0, y => 0, '-' => 1, z => 0 },
77 'Requestor IS NULL' => { xy => 0, x => 0, y => 0, '-' => 1, z => 0 },
78 'Requestor IS NOT NULL' => { xy => 1, x => 1, y => 1, '-' => 0, z => 1 },
80 'Requestor = "x@example.com" AND Requestor = "y@example.com"' => { xy => 1, x => 0, y => 0, '-' => 0, z => 0 },
81 'Requestor = "x@example.com" OR Requestor = "y@example.com"' => { xy => 1, x => 1, y => 1, '-' => 0, z => 0 },
83 'Requestor != "x@example.com" AND Requestor != "y@example.com"' => { xy => 0, x => 0, y => 0, '-' => 1, z => 1 },
84 'Requestor != "x@example.com" OR Requestor != "y@example.com"' => { xy => 0, x => 1, y => 1, '-' => 1, z => 1 },
86 'Requestor = "x@example.com" AND Requestor != "y@example.com"' => { xy => 0, x => 1, y => 0, '-' => 0, z => 0 },
87 'Requestor = "x@example.com" OR Requestor != "y@example.com"' => { xy => 1, x => 1, y => 0, '-' => 1, z => 1 },
89 'Requestor != "x@example.com" AND Requestor = "y@example.com"' => { xy => 0, x => 0, y => 1, '-' => 0, z => 0 },
90 'Requestor != "x@example.com" OR Requestor = "y@example.com"' => { xy => 1, x => 0, y => 1, '-' => 1, z => 1 },
92 @tickets = add_tix_from_data();
94 my $tix = RT::Tickets->new($RT::SystemUser);
95 $tix->FromSQL("Queue = '$queue'");
96 is($tix->Count, $total, "found $total tickets");
101 { Subject => 'xy', Cc => ['x@example.com'], Requestor => [ 'y@example.com' ] },
102 { Subject => 'x-', Cc => ['x@example.com'], Requestor => [] },
103 { Subject => '-y', Cc => [], Requestor => [ 'y@example.com' ] },
105 { Subject => 'zz', Cc => ['z@example.com'], Requestor => [ 'z@example.com' ] },
106 { Subject => 'z-', Cc => ['z@example.com'], Requestor => [] },
107 { Subject => '-z', Cc => [], Requestor => [ 'z@example.com' ] },
110 'Cc = "x@example.com" AND Requestor = "y@example.com"' =>
111 { xy => 1, 'x-' => 0, '-y' => 0, '-' => 0, zz => 0, 'z-' => 0, '-z' => 0 },
112 'Cc = "x@example.com" OR Requestor = "y@example.com"' =>
113 { xy => 1, 'x-' => 1, '-y' => 1, '-' => 0, zz => 0, 'z-' => 0, '-z' => 0 },
115 'Cc != "x@example.com" AND Requestor = "y@example.com"' =>
116 { xy => 0, 'x-' => 0, '-y' => 1, '-' => 0, zz => 0, 'z-' => 0, '-z' => 0 },
117 'Cc != "x@example.com" OR Requestor = "y@example.com"' =>
118 { xy => 1, 'x-' => 0, '-y' => 1, '-' => 1, zz => 1, 'z-' => 1, '-z' => 1 },
120 'Cc IS NULL AND Requestor = "y@example.com"' =>
121 { xy => 0, 'x-' => 0, '-y' => 1, '-' => 0, zz => 0, 'z-' => 0, '-z' => 0 },
122 'Cc IS NULL OR Requestor = "y@example.com"' =>
123 { xy => 1, 'x-' => 0, '-y' => 1, '-' => 1, zz => 0, 'z-' => 0, '-z' => 1 },
125 'Cc IS NOT NULL AND Requestor = "y@example.com"' =>
126 { xy => 1, 'x-' => 0, '-y' => 0, '-' => 0, zz => 0, 'z-' => 0, '-z' => 0 },
127 'Cc IS NOT NULL OR Requestor = "y@example.com"' =>
128 { xy => 1, 'x-' => 1, '-y' => 1, '-' => 0, zz => 1, 'z-' => 1, '-z' => 0 },
130 @tickets = add_tix_from_data();
132 my $tix = RT::Tickets->new($RT::SystemUser);
133 $tix->FromSQL("Queue = '$queue'");
134 is($tix->Count, $total, "found $total tickets");
138 # owner is special watcher because reference is duplicated in two places,
139 # owner was an ENUM field now it's WATCHERFIELD, but should support old
140 # style ENUM searches for backward compatibility
141 my $nobody = RT::Nobody();
143 my $tix = RT::Tickets->new($RT::SystemUser);
144 $tix->FromSQL("Queue = '$queue' AND Owner = '". $nobody->id ."'");
145 ok($tix->Count, "found ticket(s)");
148 my $tix = RT::Tickets->new($RT::SystemUser);
149 $tix->FromSQL("Queue = '$queue' AND Owner = '". $nobody->Name ."'");
150 ok($tix->Count, "found ticket(s)");
153 my $tix = RT::Tickets->new($RT::SystemUser);
154 $tix->FromSQL("Queue = '$queue' AND Owner != '". $nobody->id ."'");
155 is($tix->Count, 0, "found ticket(s)");
158 my $tix = RT::Tickets->new($RT::SystemUser);
159 $tix->FromSQL("Queue = '$queue' AND Owner != '". $nobody->Name ."'");
160 is($tix->Count, 0, "found ticket(s)");
164 my $tix = RT::Tickets->new($RT::SystemUser);
165 $tix->FromSQL("Queue = '$queue' AND Owner.Name LIKE 'nob'");
166 ok($tix->Count, "found ticket(s)");
170 # create ticket and force type to not a 'ticket' value
171 # bug #6898@rt3.fsck.com
172 # and http://marc.theaimsgroup.com/?l=rt-devel&m=112662934627236&w=2
173 @data = ( { Subject => 'not a ticket' } );
174 my($t) = add_tix_from_data();
175 $t->_Set( Field => 'Type',
176 Value => 'not a ticket',
178 RecordTransaction => 0,
182 my $tix = RT::Tickets->new($RT::SystemUser);
183 $tix->FromSQL("Queue = '$queue' AND Owner = 'Nobody'");
184 is($tix->Count, $total, "found ticket(s)");
188 my $everyone = RT::Group->new( $RT::SystemUser );
189 $everyone->LoadSystemInternalGroup('Everyone');
190 ok($everyone->id, "loaded 'everyone' group");
191 my($id, $msg) = $everyone->PrincipalObj->GrantRight( Right => 'OwnTicket',
194 ok($id, "granted OwnTicket right to Everyone on '$queue'") or diag("error: $msg");
196 my $u = RT::User->new( $RT::SystemUser );
197 $u->LoadOrCreateByEmail('alpha@example.com');
198 ok($u->id, "loaded user");
199 @data = ( { Subject => '4', Owner => $u->id } );
200 my($t) = add_tix_from_data();
201 is( $t->Owner, $u->id, "created ticket with custom owner" );
202 my $u_alpha_id = $u->id;
204 $u = RT::User->new( $RT::SystemUser );
205 $u->LoadOrCreateByEmail('bravo@example.com');
206 ok($u->id, "loaded user");
207 @data = ( { Subject => '5', Owner => $u->id } );
208 ($t) = add_tix_from_data();
209 is( $t->Owner, $u->id, "created ticket with custom owner" );
210 my $u_bravo_id = $u->id;
212 my $tix = RT::Tickets->new($RT::SystemUser);
213 $tix->FromSQL("Queue = '$queue' AND
214 ( Owner = '$u_alpha_id' OR
215 Owner = '$u_bravo_id' )"
217 is($tix->Count, 2, "found ticket(s)");