import rt 3.6.6
[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         is($tix->Count, $count, "found correct number of ticket(s) by '$key'") or $error = 1;
44
45         my $good_tickets = 1;
46         while ( my $ticket = $tix->Next ) {
47             next if $test{$key}->{ $ticket->Subject };
48             diag $ticket->Subject ." ticket has been found when it's not expected";
49             $good_tickets = 0;
50         }
51         ok( $good_tickets, "all tickets are good with '$key'" ) or $error = 1;
52
53         diag "Wrong SQL query for '$key':". $tix->BuildSelectQuery if $error;
54     }
55 }
56
57 @data = (
58     { Subject => 'xy', Requestor => ['x@example.com', 'y@example.com'] },
59     { Subject => 'x', Requestor => 'x@example.com' },
60     { Subject => 'y', Requestor => 'y@example.com' },
61     { Subject => '-', },
62     { Subject => 'z', Requestor => 'z@example.com' },
63 );
64 %test = (
65     'Requestor = "x@example.com"'  => { xy => 1, x => 1, y => 0, '-' => 0, z => 0 },
66     'Requestor != "x@example.com"' => { xy => 0, x => 0, y => 1, '-' => 1, z => 1 },
67
68     'Requestor = "y@example.com"'  => { xy => 1, x => 0, y => 1, '-' => 0, z => 0 },
69     'Requestor != "y@example.com"' => { xy => 0, x => 1, y => 0, '-' => 1, z => 1 },
70
71     'Requestor LIKE "@example.com"'     => { xy => 1, x => 1, y => 1, '-' => 0, z => 1 },
72     'Requestor NOT LIKE "@example.com"' => { xy => 0, x => 0, y => 0, '-' => 1, z => 0 },
73
74     'Requestor IS NULL'            => { xy => 0, x => 0, y => 0, '-' => 1, z => 0 },
75     'Requestor IS NOT NULL'        => { xy => 1, x => 1, y => 1, '-' => 0, z => 1 },
76
77 # this test is a todo, we run it later
78 #    'Requestor = "x@example.com" AND Requestor = "y@example.com"'   => { xy => 1, x => 0, y => 0, '-' => 0, z => 0 },
79     'Requestor = "x@example.com" OR Requestor = "y@example.com"'    => { xy => 1, x => 1, y => 1, '-' => 0, z => 0 },
80
81     'Requestor != "x@example.com" AND Requestor != "y@example.com"' => { xy => 0, x => 0, y => 0, '-' => 1, z => 1 },
82     'Requestor != "x@example.com" OR Requestor != "y@example.com"'  => { xy => 0, x => 1, y => 1, '-' => 1, z => 1 },
83
84     'Requestor = "x@example.com" AND Requestor != "y@example.com"'  => { xy => 0, x => 1, y => 0, '-' => 0, z => 0 },
85     'Requestor = "x@example.com" OR Requestor != "y@example.com"'   => { xy => 1, x => 1, y => 0, '-' => 1, z => 1 },
86
87     'Requestor != "x@example.com" AND Requestor = "y@example.com"'  => { xy => 0, x => 0, y => 1, '-' => 0, z => 0 },
88     'Requestor != "x@example.com" OR Requestor = "y@example.com"'   => { xy => 1, x => 0, y => 1, '-' => 1, z => 1 },
89 );
90 @tickets = add_tix_from_data();
91 {
92     my $tix = RT::Tickets->new($RT::SystemUser);
93     $tix->FromSQL("Queue = '$queue'");
94     is($tix->Count, $total, "found $total tickets");
95 }
96 run_tests();
97
98 TODO: {
99     local $TODO = "we can't generate this query yet";
100     %test = (
101         'Requestor = "x@example.com" AND Requestor = "y@example.com"'
102             => { xy => 1, x => 0, y => 0, '-' => 0, z => 0 },
103     );
104     run_tests();
105 }
106
107 @data = (
108     { Subject => 'xy', Cc => ['x@example.com'], Requestor => [ 'y@example.com' ] },
109     { Subject => 'x-', Cc => ['x@example.com'], Requestor => [] },
110     { Subject => '-y', Cc => [],                Requestor => [ 'y@example.com' ] },
111     { Subject => '-', },
112     { Subject => 'zz', Cc => ['z@example.com'], Requestor => [ 'z@example.com' ] },
113     { Subject => 'z-', Cc => ['z@example.com'], Requestor => [] },
114     { Subject => '-z', Cc => [],                Requestor => [ 'z@example.com' ] },
115 );
116 %test = (
117     'Cc = "x@example.com" AND Requestor = "y@example.com"' =>
118         { xy => 1, 'x-' => 0, '-y' => 0, '-' => 0, zz => 0, 'z-' => 0, '-z' => 0 },
119     'Cc = "x@example.com" OR Requestor = "y@example.com"' =>
120         { xy => 1, 'x-' => 1, '-y' => 1, '-' => 0, zz => 0, 'z-' => 0, '-z' => 0 },
121
122     'Cc != "x@example.com" AND Requestor = "y@example.com"' =>
123         { xy => 0, 'x-' => 0, '-y' => 1, '-' => 0, zz => 0, 'z-' => 0, '-z' => 0 },
124     'Cc != "x@example.com" OR Requestor = "y@example.com"' =>
125         { xy => 1, 'x-' => 0, '-y' => 1, '-' => 1, zz => 1, 'z-' => 1, '-z' => 1 },
126
127     'Cc IS NULL AND Requestor = "y@example.com"' =>
128         { xy => 0, 'x-' => 0, '-y' => 1, '-' => 0, zz => 0, 'z-' => 0, '-z' => 0 },
129     'Cc IS NULL OR Requestor = "y@example.com"' =>
130         { xy => 1, 'x-' => 0, '-y' => 1, '-' => 1, zz => 0, 'z-' => 0, '-z' => 1 },
131
132     'Cc IS NOT NULL AND Requestor = "y@example.com"' =>
133         { xy => 1, 'x-' => 0, '-y' => 0, '-' => 0, zz => 0, 'z-' => 0, '-z' => 0 },
134     'Cc IS NOT NULL OR Requestor = "y@example.com"' =>
135         { xy => 1, 'x-' => 1, '-y' => 1, '-' => 0, zz => 1, 'z-' => 1, '-z' => 0 },
136 );
137 @tickets = add_tix_from_data();
138 {
139     my $tix = RT::Tickets->new($RT::SystemUser);
140     $tix->FromSQL("Queue = '$queue'");
141     is($tix->Count, $total, "found $total tickets");
142 }
143 run_tests();
144
145 # owner is special watcher because reference is duplicated in two places,
146 # owner was an ENUM field now it's WATCHERFIELD, but should support old
147 # style ENUM searches for backward compatibility
148 my $nobody = RT::Nobody();
149 {
150     my $tix = RT::Tickets->new($RT::SystemUser);
151     $tix->FromSQL("Queue = '$queue' AND Owner = '". $nobody->id ."'");
152     ok($tix->Count, "found ticket(s)");
153 }
154 {
155     my $tix = RT::Tickets->new($RT::SystemUser);
156     $tix->FromSQL("Queue = '$queue' AND Owner = '". $nobody->Name ."'");
157     ok($tix->Count, "found ticket(s)");
158 }
159 {
160     my $tix = RT::Tickets->new($RT::SystemUser);
161     $tix->FromSQL("Queue = '$queue' AND Owner != '". $nobody->id ."'");
162     is($tix->Count, 0, "found ticket(s)");
163 }
164 {
165     my $tix = RT::Tickets->new($RT::SystemUser);
166     $tix->FromSQL("Queue = '$queue' AND Owner != '". $nobody->Name ."'");
167     is($tix->Count, 0, "found ticket(s)");
168 }
169
170 {
171     my $tix = RT::Tickets->new($RT::SystemUser);
172     $tix->FromSQL("Queue = '$queue' AND Owner.Name LIKE 'nob'");
173     ok($tix->Count, "found ticket(s)");
174 }
175
176 {
177     # create ticket and force type to not a 'ticket' value
178     # bug #6898@rt3.fsck.com
179     # and http://marc.theaimsgroup.com/?l=rt-devel&m=112662934627236&w=2
180     @data = ( { Subject => 'not a ticket' } );
181     my($t) = add_tix_from_data();
182     $t->_Set( Field             => 'Type',
183               Value             => 'not a ticket',
184               CheckACL          => 0,
185               RecordTransaction => 0,
186             );
187     $total--;
188
189     my $tix = RT::Tickets->new($RT::SystemUser);
190     $tix->FromSQL("Queue = '$queue' AND Owner = 'Nobody'");
191     is($tix->Count, $total, "found ticket(s)");
192 }
193
194 {
195     my $everyone = RT::Group->new( $RT::SystemUser );
196     $everyone->LoadSystemInternalGroup('Everyone');
197     ok($everyone->id, "loaded 'everyone' group");
198     my($id, $msg) = $everyone->PrincipalObj->GrantRight( Right => 'OwnTicket',
199                                                          Object => $q
200                                                        );
201     ok($id, "granted OwnTicket right to Everyone on '$queue'") or diag("error: $msg");
202
203     my $u = RT::User->new( $RT::SystemUser );
204     $u->LoadOrCreateByEmail('alpha@example.com');
205     ok($u->id, "loaded user");
206     @data = ( { Subject => '4', Owner => $u->id } );
207     my($t) = add_tix_from_data();
208     is( $t->Owner, $u->id, "created ticket with custom owner" );
209     my $u_alpha_id = $u->id;
210
211     $u = RT::User->new( $RT::SystemUser );
212     $u->LoadOrCreateByEmail('bravo@example.com');
213     ok($u->id, "loaded user");
214     @data = ( { Subject => '5', Owner => $u->id } );
215     ($t) = add_tix_from_data();
216     is( $t->Owner, $u->id, "created ticket with custom owner" );
217     my $u_bravo_id = $u->id;
218
219     my $tix = RT::Tickets->new($RT::SystemUser);
220     $tix->FromSQL("Queue = '$queue' AND
221                    ( Owner = '$u_alpha_id' OR
222                      Owner = '$u_bravo_id' )"
223                  );
224     is($tix->Count, 2, "found ticket(s)");
225 }
226
227
228 exit(0)