summaryrefslogtreecommitdiff
path: root/rt/lib/t/regression/22search_tix_by_watcher.t
blob: 204c41c374ed18109c40e704354fa89f8f0cdda3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/perl -w

use strict;
use warnings;

use Test::More tests => 119;
use_ok('RT');
RT::LoadConfig();
RT::Init();
use RT::Ticket;

my $q = RT::Queue->new( $RT::SystemUser );
my $queue = 'SearchTests-'. rand(200);
$q->Create( Name => $queue );

my ($total, @data, @tickets, %test) = (0, ());

sub add_tix_from_data {
    my @res = ();
    while (@data) {
        my $t = RT::Ticket->new($RT::SystemUser);
        my ( $id, undef $msg ) = $t->Create(
            Queue => $q->id,
            %{ shift(@data) },
        );
        ok( $id, "ticket created" ) or diag("error: $msg");
        push @res, $t;
        $total++;
    }
    return @res;
}

sub run_tests {
    my $query_prefix = join ' OR ', map 'id = '. $_->id, @tickets;
    foreach my $key ( sort keys %test ) {
        my $tix = RT::Tickets->new($RT::SystemUser);
        $tix->FromSQL( "( $query_prefix ) AND ( $key )" );

        my $error = 0;

        my $count = 0;
        $count++ foreach grep $_, values %{ $test{$key} };
        is($tix->Count, $count, "found correct number of ticket(s) by '$key'") or $error = 1;

        my $good_tickets = 1;
        while ( my $ticket = $tix->Next ) {
            next if $test{$key}->{ $ticket->Subject };
            diag $ticket->Subject ." ticket has been found when it's not expected";
            $good_tickets = 0;
        }
        ok( $good_tickets, "all tickets are good with '$key'" ) or $error = 1;

        diag "Wrong SQL query for '$key':". $tix->BuildSelectQuery if $error;
    }
}

@data = (
    { Subject => 'xy', Requestor => ['x@example.com', 'y@example.com'] },
    { Subject => 'x', Requestor => 'x@example.com' },
    { Subject => 'y', Requestor => 'y@example.com' },
    { Subject => '-', },
    { Subject => 'z', Requestor => 'z@example.com' },
);
%test = (
    'Requestor = "x@example.com"'  => { xy => 1, x => 1, y => 0, '-' => 0, z => 0 },
    'Requestor != "x@example.com"' => { xy => 0, x => 0, y => 1, '-' => 1, z => 1 },

    'Requestor = "y@example.com"'  => { xy => 1, x => 0, y => 1, '-' => 0, z => 0 },
    'Requestor != "y@example.com"' => { xy => 0, x => 1, y => 0, '-' => 1, z => 1 },

    'Requestor LIKE "@example.com"'     => { xy => 1, x => 1, y => 1, '-' => 0, z => 1 },
    'Requestor NOT LIKE "@example.com"' => { xy => 0, x => 0, y => 0, '-' => 1, z => 0 },

    'Requestor IS NULL'            => { xy => 0, x => 0, y => 0, '-' => 1, z => 0 },
    'Requestor IS NOT NULL'        => { xy => 1, x => 1, y => 1, '-' => 0, z => 1 },

# this test is a todo, we run it later
#    'Requestor = "x@example.com" AND Requestor = "y@example.com"'   => { xy => 1, x => 0, y => 0, '-' => 0, z => 0 },
    'Requestor = "x@example.com" OR Requestor = "y@example.com"'    => { xy => 1, x => 1, y => 1, '-' => 0, z => 0 },

    'Requestor != "x@example.com" AND Requestor != "y@example.com"' => { xy => 0, x => 0, y => 0, '-' => 1, z => 1 },
    'Requestor != "x@example.com" OR Requestor != "y@example.com"'  => { xy => 0, x => 1, y => 1, '-' => 1, z => 1 },

    'Requestor = "x@example.com" AND Requestor != "y@example.com"'  => { xy => 0, x => 1, y => 0, '-' => 0, z => 0 },
    'Requestor = "x@example.com" OR Requestor != "y@example.com"'   => { xy => 1, x => 1, y => 0, '-' => 1, z => 1 },

    'Requestor != "x@example.com" AND Requestor = "y@example.com"'  => { xy => 0, x => 0, y => 1, '-' => 0, z => 0 },
    'Requestor != "x@example.com" OR Requestor = "y@example.com"'   => { xy => 1, x => 0, y => 1, '-' => 1, z => 1 },
);
@tickets = add_tix_from_data();
{
    my $tix = RT::Tickets->new($RT::SystemUser);
    $tix->FromSQL("Queue = '$queue'");
    is($tix->Count, $total, "found $total tickets");
}
run_tests();

# mixing searches by watchers with other conditions
# http://rt3.fsck.com/Ticket/Display.html?id=9322
%test = (
    'Subject LIKE "x" AND Requestor = "y@example.com"' =>
        { xy => 1, x => 0, y => 0, '-' => 0, z => 0 },
    'Subject NOT LIKE "x" AND Requestor = "y@example.com"' =>
        { xy => 0, x => 0, y => 1, '-' => 0, z => 0 },
    'Subject LIKE "x" AND Requestor != "y@example.com"' =>
        { xy => 0, x => 1, y => 0, '-' => 0, z => 0 },
    'Subject NOT LIKE "x" AND Requestor != "y@example.com"' =>
        { xy => 0, x => 0, y => 0, '-' => 1, z => 1 },

    'Subject LIKE "x" OR Requestor = "y@example.com"' =>
        { xy => 1, x => 1, y => 1, '-' => 0, z => 0 },
    'Subject NOT LIKE "x" OR Requestor = "y@example.com"' =>
        { xy => 1, x => 0, y => 1, '-' => 1, z => 1 },
    'Subject LIKE "x" OR Requestor != "y@example.com"' =>
        { xy => 1, x => 1, y => 0, '-' => 1, z => 1 },
    'Subject NOT LIKE "x" OR Requestor != "y@example.com"' =>
        { xy => 0, x => 1, y => 1, '-' => 1, z => 1 },

# group of cases when user doesn't exist in DB at all
    'Subject LIKE "x" AND Requestor = "not-exist@example.com"' =>
        { xy => 0, x => 0, y => 0, '-' => 0, z => 0 },
    'Subject NOT LIKE "x" AND Requestor = "not-exist@example.com"' =>
        { xy => 0, x => 0, y => 0, '-' => 0, z => 0 },
    'Subject LIKE "x" AND Requestor != "not-exist@example.com"' =>
        { xy => 1, x => 1, y => 0, '-' => 0, z => 0 },
    'Subject NOT LIKE "x" AND Requestor != "not-exist@example.com"' =>
        { xy => 0, x => 0, y => 1, '-' => 1, z => 1 },
    'Subject LIKE "x" OR Requestor = "not-exist@example.com"' =>
        { xy => 1, x => 1, y => 0, '-' => 0, z => 0 },
    'Subject NOT LIKE "x" OR Requestor = "not-exist@example.com"' =>
        { xy => 0, x => 0, y => 1, '-' => 1, z => 1 },
    'Subject LIKE "x" OR Requestor != "not-exist@example.com"' =>
        { xy => 1, x => 1, y => 1, '-' => 1, z => 1 },
    'Subject NOT LIKE "x" OR Requestor != "not-exist@example.com"' =>
        { xy => 1, x => 1, y => 1, '-' => 1, z => 1 },

    'Subject LIKE "z" AND (Requestor = "x@example.com" OR Requestor = "y@example.com")' =>
        { xy => 0, x => 0, y => 0, '-' => 0, z => 0 },
    'Subject NOT LIKE "z" AND (Requestor = "x@example.com" OR Requestor = "y@example.com")' =>
        { xy => 1, x => 1, y => 1, '-' => 0, z => 0 },
    'Subject LIKE "z" OR (Requestor = "x@example.com" OR Requestor = "y@example.com")' =>
        { xy => 1, x => 1, y => 1, '-' => 0, z => 1 },
    'Subject NOT LIKE "z" OR (Requestor = "x@example.com" OR Requestor = "y@example.com")' =>
        { xy => 1, x => 1, y => 1, '-' => 1, z => 0 },
);
run_tests();

TODO: {
    local $TODO = "we can't generate this query yet";
    %test = (
        'Requestor = "x@example.com" AND Requestor = "y@example.com"'
            => { xy => 1, x => 0, y => 0, '-' => 0, z => 0 },
    );
    run_tests();
}

@data = (
    { Subject => 'xy', Cc => ['x@example.com'], Requestor => [ 'y@example.com' ] },
    { Subject => 'x-', Cc => ['x@example.com'], Requestor => [] },
    { Subject => '-y', Cc => [],                Requestor => [ 'y@example.com' ] },
    { Subject => '-', },
    { Subject => 'zz', Cc => ['z@example.com'], Requestor => [ 'z@example.com' ] },
    { Subject => 'z-', Cc => ['z@example.com'], Requestor => [] },
    { Subject => '-z', Cc => [],                Requestor => [ 'z@example.com' ] },
);
%test = (
    'Cc = "x@example.com" AND Requestor = "y@example.com"' =>
        { xy => 1, 'x-' => 0, '-y' => 0, '-' => 0, zz => 0, 'z-' => 0, '-z' => 0 },
    'Cc = "x@example.com" OR Requestor = "y@example.com"' =>
        { xy => 1, 'x-' => 1, '-y' => 1, '-' => 0, zz => 0, 'z-' => 0, '-z' => 0 },

    'Cc != "x@example.com" AND Requestor = "y@example.com"' =>
        { xy => 0, 'x-' => 0, '-y' => 1, '-' => 0, zz => 0, 'z-' => 0, '-z' => 0 },
    'Cc != "x@example.com" OR Requestor = "y@example.com"' =>
        { xy => 1, 'x-' => 0, '-y' => 1, '-' => 1, zz => 1, 'z-' => 1, '-z' => 1 },

    'Cc IS NULL AND Requestor = "y@example.com"' =>
        { xy => 0, 'x-' => 0, '-y' => 1, '-' => 0, zz => 0, 'z-' => 0, '-z' => 0 },
    'Cc IS NULL OR Requestor = "y@example.com"' =>
        { xy => 1, 'x-' => 0, '-y' => 1, '-' => 1, zz => 0, 'z-' => 0, '-z' => 1 },

    'Cc IS NOT NULL AND Requestor = "y@example.com"' =>
        { xy => 1, 'x-' => 0, '-y' => 0, '-' => 0, zz => 0, 'z-' => 0, '-z' => 0 },
    'Cc IS NOT NULL OR Requestor = "y@example.com"' =>
        { xy => 1, 'x-' => 1, '-y' => 1, '-' => 0, zz => 1, 'z-' => 1, '-z' => 0 },
);
@tickets = add_tix_from_data();
{
    my $tix = RT::Tickets->new($RT::SystemUser);
    $tix->FromSQL("Queue = '$queue'");
    is($tix->Count, $total, "found $total tickets");
}
run_tests();


# owner is special watcher because reference is duplicated in two places,
# owner was an ENUM field now it's WATCHERFIELD, but should support old
# style ENUM searches for backward compatibility
my $nobody = RT::Nobody();
{
    my $tix = RT::Tickets->new($RT::SystemUser);
    $tix->FromSQL("Queue = '$queue' AND Owner = '". $nobody->id ."'");
    ok($tix->Count, "found ticket(s)");
}
{
    my $tix = RT::Tickets->new($RT::SystemUser);
    $tix->FromSQL("Queue = '$queue' AND Owner = '". $nobody->Name ."'");
    ok($tix->Count, "found ticket(s)");
}
{
    my $tix = RT::Tickets->new($RT::SystemUser);
    $tix->FromSQL("Queue = '$queue' AND Owner != '". $nobody->id ."'");
    is($tix->Count, 0, "found ticket(s)");
}
{
    my $tix = RT::Tickets->new($RT::SystemUser);
    $tix->FromSQL("Queue = '$queue' AND Owner != '". $nobody->Name ."'");
    is($tix->Count, 0, "found ticket(s)");
}

{
    my $tix = RT::Tickets->new($RT::SystemUser);
    $tix->FromSQL("Queue = '$queue' AND Owner.Name LIKE 'nob'");
    ok($tix->Count, "found ticket(s)");
}

{
    # create ticket and force type to not a 'ticket' value
    # bug #6898@rt3.fsck.com
    # and http://marc.theaimsgroup.com/?l=rt-devel&m=112662934627236&w=2
    @data = ( { Subject => 'not a ticket' } );
    my($t) = add_tix_from_data();
    $t->_Set( Field             => 'Type',
              Value             => 'not a ticket',
              CheckACL          => 0,
              RecordTransaction => 0,
            );
    $total--;

    my $tix = RT::Tickets->new($RT::SystemUser);
    $tix->FromSQL("Queue = '$queue' AND Owner = 'Nobody'");
    is($tix->Count, $total, "found ticket(s)");
}

{
    my $everyone = RT::Group->new( $RT::SystemUser );
    $everyone->LoadSystemInternalGroup('Everyone');
    ok($everyone->id, "loaded 'everyone' group");
    my($id, $msg) = $everyone->PrincipalObj->GrantRight( Right => 'OwnTicket',
                                                         Object => $q
                                                       );
    ok($id, "granted OwnTicket right to Everyone on '$queue'") or diag("error: $msg");

    my $u = RT::User->new( $RT::SystemUser );
    $u->LoadOrCreateByEmail('alpha@example.com');
    ok($u->id, "loaded user");
    @data = ( { Subject => '4', Owner => $u->id } );
    my($t) = add_tix_from_data();
    is( $t->Owner, $u->id, "created ticket with custom owner" );
    my $u_alpha_id = $u->id;

    $u = RT::User->new( $RT::SystemUser );
    $u->LoadOrCreateByEmail('bravo@example.com');
    ok($u->id, "loaded user");
    @data = ( { Subject => '5', Owner => $u->id } );
    ($t) = add_tix_from_data();
    is( $t->Owner, $u->id, "created ticket with custom owner" );
    my $u_bravo_id = $u->id;

    my $tix = RT::Tickets->new($RT::SystemUser);
    $tix->FromSQL("Queue = '$queue' AND
                   ( Owner = '$u_alpha_id' OR
                     Owner = '$u_bravo_id' )"
                 );
    is($tix->Count, 2, "found ticket(s)");
}


exit(0)