import rt 3.4.6
[freeside.git] / rt / lib / t / regression / 22search_tix_by_watcher.t
1 #!/usr/bin/perl -w
2 use strict;
3 use warnings;
4
5 use Test::More qw/no_plan/;
6 use_ok('RT');
7 RT::LoadConfig();
8 RT::Init();
9 use RT::Ticket;
10
11 my $q = RT::Queue->new($RT::SystemUser);
12 my $queue = 'SearchTests-'.rand(200);
13 $q->Create(Name => $queue);
14
15 my @data = (
16     { Subject => '1', Requestor => 'bravo@example.com' },
17     { Subject => '2', Cc => 'alpha@example.com' },
18 );
19
20 my $total = 0;
21
22 sub add_tix_from_data {
23     my @res = ();
24     while (@data) {
25         my $t = RT::Ticket->new($RT::SystemUser);
26         my ( $id, undef $msg ) = $t->Create(
27             Queue => $q->id,
28             %{ shift(@data) },
29         );
30         ok( $id, "ticket created" ) or diag("error: $msg");
31         push @res, $t;
32         $total++;
33     }
34     return @res;
35 }
36 add_tix_from_data();
37
38 {
39     my $tix = RT::Tickets->new($RT::SystemUser);
40     $tix->FromSQL("Queue = '$queue'");
41     is($tix->Count, $total, "found $total tickets");
42 }
43
44 {
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");
49 }
50
51 {
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");
56 }
57
58 {
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)");
62     my @mails;
63     while (my $t = $tix->Next) {
64         push @mails, $t->RequestorAddresses;
65         push @mails, $t->CcAddresses;
66     }
67     @mails = sort grep $_, @mails;
68     is_deeply(\@mails, ['alpha@example.com', 'bravo@example.com'], "correct addresses");
69 }
70
71 {
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)");
75 }
76
77 {
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");
82 }
83
84 @data = ( { Subject => '3' } );
85 add_tix_from_data();
86
87 {
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)");
91     my @mails;
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");
94 }
95
96 {
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)");
101     my @mails;
102     while (my $t = $tix->Next) { push @mails, ($t->RequestorAddresses||'') }
103     is( scalar(grep $_, @mails), 0, "no tickets with non required data");
104 }
105
106 {
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)");
111     my @mails;
112     while (my $t = $tix->Next) { push @mails, ($t->RequestorAddresses||'') }
113     is( scalar(grep !$_, @mails), 0, "no tickets with non required data");
114 }
115
116 @data = ( { Subject => '3', Requestor => 'charly@example.com' } );
117 add_tix_from_data();
118
119 {
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)");
125     my @mails;
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"
130              );
131 }
132
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();
137 {
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)");
141 }
142 {
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)");
146 }
147 {
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)");
151 }
152 {
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)");
156 }
157
158 {
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)");
162 }
163
164 {
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',
172               CheckACL          => 0,
173               RecordTransaction => 0,
174             );
175     $total--;
176
177     my $tix = RT::Tickets->new($RT::SystemUser);
178     $tix->FromSQL("Queue = '$queue' AND Owner = 'Nobody'");
179     is($tix->Count, 4, "found ticket(s)");
180 }
181
182 {
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',
187                                                          Object => $q
188                                                        );
189     ok($id, "granted OwnTicket right to Everyone on '$queue'") or diag("error: $msg");
190
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;
198
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;
206
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' )"
211                  );
212     is($tix->Count, 2, "found ticket(s)");
213 }
214
215 exit(0)