5 use Test::More qw/no_plan/;
11 my $q = RT::Queue->new($RT::SystemUser);
12 my $queue = 'SearchTests-'.rand(200);
13 $q->Create(Name => $queue);
16 { Subject => '1', Requestor => 'bravo@example.com' },
17 { Subject => '2', Cc => 'alpha@example.com' },
22 sub add_tix_from_data {
25 my $t = RT::Ticket->new($RT::SystemUser);
26 my ( $id, undef $msg ) = $t->Create(
30 ok( $id, "ticket created" ) or diag("error: $msg");
39 my $tix = RT::Tickets->new($RT::SystemUser);
40 $tix->FromSQL("Queue = '$queue'");
41 is($tix->Count, $total, "found $total tickets");
45 my $tix = RT::Tickets->new($RT::SystemUser);
46 $tix->FromSQL("Queue = '$queue' AND Requestor = 'bravo\@example.com'");
47 is($tix->Count, 1, "found ticket(s)");
48 is($tix->First->RequestorAddresses, 'bravo@example.com',"correct requestor");
52 my $tix = RT::Tickets->new($RT::SystemUser);
53 $tix->FromSQL("Queue = '$queue' AND Cc = 'alpha\@example.com'");
54 is($tix->Count, 1, "found ticket(s)");
55 is($tix->First->CcAddresses, 'alpha@example.com', "correct Cc");
59 my $tix = RT::Tickets->new($RT::SystemUser);
60 $tix->FromSQL("Queue = '$queue' AND (Cc = 'alpha\@example.com' OR Requestor = 'bravo\@example.com')");
61 is($tix->Count, 2, "found ticket(s)");
63 while (my $t = $tix->Next) {
64 push @mails, $t->RequestorAddresses;
65 push @mails, $t->CcAddresses;
67 @mails = sort grep $_, @mails;
68 is_deeply(\@mails, ['alpha@example.com', 'bravo@example.com'], "correct addresses");
72 my $tix = RT::Tickets->new($RT::SystemUser);
73 $tix->FromSQL("Queue = '$queue' AND (Cc = 'alpha\@example.com' AND Requestor = 'bravo\@example.com')");
74 is($tix->Count, 0, "found ticket(s)");
78 my $tix = RT::Tickets->new($RT::SystemUser);
79 $tix->FromSQL("Queue = '$queue' AND Cc != 'alpha\@example.com'");
80 is($tix->Count, 1, "found ticket(s)");
81 is($tix->First->RequestorAddresses, 'bravo@example.com',"correct requestor");
84 @data = ( { Subject => '3' } );
88 my $tix = RT::Tickets->new($RT::SystemUser);
89 $tix->FromSQL("Queue = '$queue' AND Cc != 'alpha\@example.com'");
90 is($tix->Count, 2, "found ticket(s)");
92 while (my $t = $tix->Next) { push @mails, ($t->CcAddresses||'') }
93 is( scalar(grep 'alpha@example.com' eq $_, @mails), 0, "no tickets with non required data");
97 # has no requestor search
98 my $tix = RT::Tickets->new($RT::SystemUser);
99 $tix->FromSQL("Queue = '$queue' AND Requestor IS NULL");
100 is($tix->Count, 2, "found ticket(s)");
102 while (my $t = $tix->Next) { push @mails, ($t->RequestorAddresses||'') }
103 is( scalar(grep $_, @mails), 0, "no tickets with non required data");
107 # has at least one requestor search
108 my $tix = RT::Tickets->new($RT::SystemUser);
109 $tix->FromSQL("Queue = '$queue' AND Requestor IS NOT NULL");
110 is($tix->Count, 1, "found ticket(s)");
112 while (my $t = $tix->Next) { push @mails, ($t->RequestorAddresses||'') }
113 is( scalar(grep !$_, @mails), 0, "no tickets with non required data");
116 @data = ( { Subject => '3', Requestor => 'charly@example.com' } );
120 # has no requestor search
121 my $tix = RT::Tickets->new($RT::SystemUser);
122 $tix->FromSQL("Queue = '$queue' AND
123 (Requestor = 'bravo\@example.com' OR Requestor = 'charly\@example.com')");
124 is($tix->Count, 2, "found ticket(s)");
126 while (my $t = $tix->Next) { push @mails, ($t->RequestorAddresses||'') }
127 is_deeply( [sort @mails],
128 ['bravo@example.com', 'charly@example.com'],
129 "requestor addresses are correct"
133 # owner is special watcher because reference is duplicated in two places,
134 # owner was an ENUM field now it's WATCHERFIELD, but should support old
135 # style ENUM searches for backward compatibility
136 my $nobody = RT::Nobody();
138 my $tix = RT::Tickets->new($RT::SystemUser);
139 $tix->FromSQL("Queue = '$queue' AND Owner = '". $nobody->id ."'");
140 is($tix->Count, 4, "found ticket(s)");
143 my $tix = RT::Tickets->new($RT::SystemUser);
144 $tix->FromSQL("Queue = '$queue' AND Owner = '". $nobody->Name ."'");
145 is($tix->Count, 4, "found ticket(s)");
148 my $tix = RT::Tickets->new($RT::SystemUser);
149 $tix->FromSQL("Queue = '$queue' AND Owner != '". $nobody->id ."'");
150 is($tix->Count, 0, "found ticket(s)");
153 my $tix = RT::Tickets->new($RT::SystemUser);
154 $tix->FromSQL("Queue = '$queue' AND Owner != '". $nobody->Name ."'");
155 is($tix->Count, 0, "found ticket(s)");
159 my $tix = RT::Tickets->new($RT::SystemUser);
160 $tix->FromSQL("Queue = '$queue' AND Owner.Name LIKE 'nob'");
161 is($tix->Count, 4, "found ticket(s)");
165 # create ticket and force type to not a 'ticket' value
166 # bug #6898@rt3.fsck.com
167 # and http://marc.theaimsgroup.com/?l=rt-devel&m=112662934627236&w=2
168 @data = ( { Subject => 'not a ticket' } );
169 my($t) = add_tix_from_data();
170 $t->_Set( Field => 'Type',
171 Value => 'not a ticket',
173 RecordTransaction => 0,
177 my $tix = RT::Tickets->new($RT::SystemUser);
178 $tix->FromSQL("Queue = '$queue' AND Owner = 'Nobody'");
179 is($tix->Count, 4, "found ticket(s)");
183 my $everyone = RT::Group->new( $RT::SystemUser );
184 $everyone->LoadSystemInternalGroup('Everyone');
185 ok($everyone->id, "loaded 'everyone' group");
186 my($id, $msg) = $everyone->PrincipalObj->GrantRight( Right => 'OwnTicket',
189 ok($id, "granted OwnTicket right to Everyone on '$queue'") or diag("error: $msg");
191 my $u = RT::User->new( $RT::SystemUser );
192 $u->LoadByCols( EmailAddress => 'alpha@example.com' );
193 ok($u->id, "loaded user");
194 @data = ( { Subject => '4', Owner => $u->id } );
195 my($t) = add_tix_from_data();
196 is( $t->Owner, $u->id, "created ticket with custom owner" );
197 my $u_alpha_id = $u->id;
199 $u = RT::User->new( $RT::SystemUser );
200 $u->LoadByCols( EmailAddress => 'bravo@example.com' );
201 ok($u->id, "loaded user");
202 @data = ( { Subject => '5', Owner => $u->id } );
203 ($t) = add_tix_from_data();
204 is( $t->Owner, $u->id, "created ticket with custom owner" );
205 my $u_bravo_id = $u->id;
207 my $tix = RT::Tickets->new($RT::SystemUser);
208 $tix->FromSQL("Queue = '$queue' AND
209 ( Owner = '$u_alpha_id' OR
210 Owner = '$u_bravo_id' )"
212 is($tix->Count, 2, "found ticket(s)");