import rt 3.6.4
[freeside.git] / rt / lib / t / regression / 22search_tix_by_watcher.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 79;
7 use_ok('RT');
8 RT::LoadConfig();
9 RT::Init();
10 use RT::Ticket;
11
12 my $q = RT::Queue->new( $RT::SystemUser );
13 my $queue = 'SearchTests-'. rand(200);
14 $q->Create( Name => $queue );
15
16 my ($total, @data, @tickets, %test) = (0, ());
17
18 sub add_tix_from_data {
19     my @res = ();
20     while (@data) {
21         my $t = RT::Ticket->new($RT::SystemUser);
22         my ( $id, undef $msg ) = $t->Create(
23             Queue => $q->id,
24             %{ shift(@data) },
25         );
26         ok( $id, "ticket created" ) or diag("error: $msg");
27         push @res, $t;
28         $total++;
29     }
30     return @res;
31 }
32
33 sub run_tests {
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 )" );
38
39         my $error = 0;
40
41         my $count = 0;
42         $count++ foreach grep $_, values %{ $test{$key} };
43         TODO: { 
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;
46             };
47
48         my $good_tickets = 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";
52             $good_tickets = 0;
53         }
54         ok( $good_tickets, "all tickets are good with '$key'" ) or $error = 1;
55
56         diag "Wrong SQL query for '$key':". $tix->BuildSelectQuery if $error;
57     }
58 }
59
60 @data = (
61     { Subject => 'xy', Requestor => ['x@example.com', 'y@example.com'] },
62     { Subject => 'x', Requestor => 'x@example.com' },
63     { Subject => 'y', Requestor => 'y@example.com' },
64     { Subject => '-', },
65     { Subject => 'z', Requestor => 'z@example.com' },
66 );
67 %test = (
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 },
70
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 },
73
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 },
76
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 },
79
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 },
82
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 },
85
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 },
88
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 },
91 );
92 @tickets = add_tix_from_data();
93 {
94     my $tix = RT::Tickets->new($RT::SystemUser);
95     $tix->FromSQL("Queue = '$queue'");
96     is($tix->Count, $total, "found $total tickets");
97 }
98 run_tests();
99
100 @data = (
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' ] },
104     { Subject => '-', },
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' ] },
108 );
109 %test = (
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 },
114
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 },
119
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 },
124
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 },
129 );
130 @tickets = add_tix_from_data();
131 {
132     my $tix = RT::Tickets->new($RT::SystemUser);
133     $tix->FromSQL("Queue = '$queue'");
134     is($tix->Count, $total, "found $total tickets");
135 }
136 run_tests();
137
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();
142 {
143     my $tix = RT::Tickets->new($RT::SystemUser);
144     $tix->FromSQL("Queue = '$queue' AND Owner = '". $nobody->id ."'");
145     ok($tix->Count, "found ticket(s)");
146 }
147 {
148     my $tix = RT::Tickets->new($RT::SystemUser);
149     $tix->FromSQL("Queue = '$queue' AND Owner = '". $nobody->Name ."'");
150     ok($tix->Count, "found ticket(s)");
151 }
152 {
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)");
156 }
157 {
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)");
161 }
162
163 {
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)");
167 }
168
169 {
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',
177               CheckACL          => 0,
178               RecordTransaction => 0,
179             );
180     $total--;
181
182     my $tix = RT::Tickets->new($RT::SystemUser);
183     $tix->FromSQL("Queue = '$queue' AND Owner = 'Nobody'");
184     is($tix->Count, $total, "found ticket(s)");
185 }
186
187 {
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',
192                                                          Object => $q
193                                                        );
194     ok($id, "granted OwnTicket right to Everyone on '$queue'") or diag("error: $msg");
195
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;
203
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;
211
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' )"
216                  );
217     is($tix->Count, 2, "found ticket(s)");
218 }
219
220
221 exit(0)