fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / ticket / search_by_cf_freeform_single.t
1
2 use strict;
3 use warnings;
4
5 use RT::Test nodata => 1, tests => undef;
6
7 my $q = RT::Test->load_or_create_queue( Name => 'Regression' );
8 ok $q && $q->id, 'loaded or created queue';
9 my $queue = $q->Name;
10
11 diag "create a CF";
12 my ($cf_name, $cf_id, $cf) = ("Test", 0, undef);
13 {
14     $cf = RT::CustomField->new( RT->SystemUser );
15     my ($ret, $msg) = $cf->Create(
16         Name  => $cf_name,
17         Queue => $q->id,
18         Type  => 'FreeformSingle',
19     );
20     ok($ret, "Custom Field Order created");
21     $cf_id = $cf->id;
22 }
23
24 my $other_q = RT::Test->load_or_create_queue( Name => 'Other' );
25 ok $other_q && $other_q->id, 'loaded or created queue';
26
27 my $ylong = 'y' x 300;
28 subtest "Creating tickets" => sub {
29     RT::Test->create_tickets( { Queue => $q->id },
30         { Subject => '-' },
31         { Subject => "other", Queue => $other_q->id },
32         { Subject => 'x', "CustomField-$cf_id" => 'x', },
33         { Subject => 'y', "CustomField-$cf_id" => 'y', },
34         { Subject => 'z', "CustomField-$cf_id" => 'z', },
35         { Subject => 'ylong', "CustomField-$cf_id" => $ylong, },
36     );
37 };
38
39 my @tests = (
40     "CF.{$cf_id} IS NULL"                        => { '-' => 1, other => 1, x => 0, y => 0, z => 0, ylong => 0 },
41     "CF.{$cf_id}.Content IS NULL"                => { '-' => 1, other => 1, x => 0, y => 0, z => 0, ylong => 1 },
42     "CF.{$cf_id}.LargeContent IS NULL"           => { '-' => 1, other => 1, x => 1, y => 1, z => 1, ylong => 0 },
43     "'CF.{$cf_name}' IS NULL"                    => { '-' => 1, other => 1, x => 0, y => 0, z => 0, ylong => 0 },
44     "'CF.{$cf_name}.Content' IS NULL"            => { '-' => 1, other => 1, x => 0, y => 0, z => 0, ylong => 1 },
45     "'CF.{$cf_name}.LargeContent' IS NULL"       => { '-' => 1, other => 1, x => 1, y => 1, z => 1, ylong => 0 },
46     "'CF.$queue.{$cf_id}' IS NULL"               => { '-' => 1, other => 1, x => 0, y => 0, z => 0, ylong => 0 },
47     "'CF.$queue.{$cf_name}' IS NULL"             => { '-' => 1, other => 1, x => 0, y => 0, z => 0, ylong => 0 },
48
49     "CF.{$cf_id} IS NOT NULL"                    => { '-' => 0, other => 0, x => 1, y => 1, z => 1, ylong => 1 },
50     "CF.{$cf_id}.Content IS NOT NULL"            => { '-' => 0, other => 0, x => 1, y => 1, z => 1, ylong => 0 },
51     "CF.{$cf_id}.LargeContent IS NOT NULL"       => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 1 },
52     "'CF.{$cf_name}' IS NOT NULL"                => { '-' => 0, other => 0, x => 1, y => 1, z => 1, ylong => 1 },
53     "'CF.{$cf_name}.Content' IS NOT NULL"        => { '-' => 0, other => 0, x => 1, y => 1, z => 1, ylong => 0 },
54     "'CF.{$cf_name}.LargeContent' IS NOT NULL"   => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 1 },
55     "'CF.$queue.{$cf_id}' IS NOT NULL"           => { '-' => 0, other => 0, x => 1, y => 1, z => 1, ylong => 1 },
56     "'CF.$queue.{$cf_name}' IS NOT NULL"         => { '-' => 0, other => 0, x => 1, y => 1, z => 1, ylong => 1 },
57
58     "CF.{$cf_id} = 'x'"                          => { '-' => 0, other => 0, x => 1, y => 0, z => 0, ylong => 0 },
59     "CF.{$cf_id}.Content = 'x'"                  => { '-' => 0, other => 0, x => 1, y => 0, z => 0, ylong => 0 },
60     "CF.{$cf_id}.LargeContent = 'x'"             => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 0 },
61     "CF.{$cf_id} = '$ylong'"                     => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 1 },
62     "CF.{$cf_id}.Content = '$ylong'"             => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 0 },
63     "CF.{$cf_id}.LargeContent = '$ylong'"        => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 1 },
64     "CF.{$cf_id} LIKE 'yyy'"                     => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 1 },
65     "CF.{$cf_id}.Content LIKE 'yyy'"             => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 0 },
66     "CF.{$cf_id}.LargeContent LIKE 'yyy'"        => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 1 },
67     "'CF.{$cf_name}' = 'x'"                      => { '-' => 0, other => 0, x => 1, y => 0, z => 0, ylong => 0 },
68     "'CF.{$cf_name}.Content' = 'x'"              => { '-' => 0, other => 0, x => 1, y => 0, z => 0, ylong => 0 },
69     "'CF.{$cf_name}.LargeContent' = 'x'"         => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 0 },
70     "'CF.$queue.{$cf_id}' = 'x'"                 => { '-' => 0, other => 0, x => 1, y => 0, z => 0, ylong => 0 },
71     "'CF.$queue.{$cf_id}.Content' = 'x'"         => { '-' => 0, other => 0, x => 1, y => 0, z => 0, ylong => 0 },
72     "'CF.$queue.{$cf_id}.LargeContent' = 'x'"    => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 0 },
73     "'CF.$queue.{$cf_name}' = 'x'"               => { '-' => 0, other => 0, x => 1, y => 0, z => 0, ylong => 0 },
74     "'CF.$queue.{$cf_name}.Content' = 'x'"       => { '-' => 0, other => 0, x => 1, y => 0, z => 0, ylong => 0 },
75     "'CF.$queue.{$cf_name}.LargeContent' = 'x'"  => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 0 },
76
77     "CF.{$cf_id} != 'x'"                         => { '-' => 1, other => 1, x => 0, y => 1, z => 1, ylong => 1 },
78     "CF.{$cf_id}.Content != 'x'"                 => { '-' => 1, other => 1, x => 0, y => 1, z => 1, ylong => 1 },
79     "CF.{$cf_id}.LargeContent != 'x'"            => { '-' => 1, other => 1, x => 1, y => 1, z => 1, ylong => 1 },
80     "CF.{$cf_id} != '$ylong'"                    => { '-' => 1, other => 1, x => 1, y => 1, z => 1, ylong => 0 },
81     "CF.{$cf_id}.Content != '$ylong'"            => { '-' => 1, other => 1, x => 1, y => 1, z => 1, ylong => 1 },
82     "CF.{$cf_id}.LargeContent != '$ylong'"       => { '-' => 1, other => 1, x => 1, y => 1, z => 1, ylong => 0 },
83     "CF.{$cf_id} NOT LIKE 'yyy'"                 => { '-' => 1, other => 1, x => 1, y => 1, z => 1, ylong => 0 },
84     "CF.{$cf_id}.Content NOT LIKE 'yyy'"         => { '-' => 1, other => 1, x => 1, y => 1, z => 1, ylong => 1 },
85     "CF.{$cf_id}.LargeContent NOT LIKE 'yyy'"    => { '-' => 1, other => 1, x => 1, y => 1, z => 1, ylong => 0 },
86     "'CF.{$cf_name}' != 'x'"                     => { '-' => 1, other => 1, x => 0, y => 1, z => 1, ylong => 1 },
87     "'CF.{$cf_name}.Content' != 'x'"             => { '-' => 1, other => 1, x => 0, y => 1, z => 1, ylong => 1 },
88     "'CF.{$cf_name}.LargeContent' != 'x'"        => { '-' => 1, other => 1, x => 1, y => 1, z => 1, ylong => 1 },
89     "'CF.$queue.{$cf_id}' != 'x'"                => { '-' => 1, other => 1, x => 0, y => 1, z => 1, ylong => 1 },
90     "'CF.$queue.{$cf_id}.Content' != 'x'"        => { '-' => 1, other => 1, x => 0, y => 1, z => 1, ylong => 1 },
91     "'CF.$queue.{$cf_id}.LargeContent' != 'x'"   => { '-' => 1, other => 1, x => 1, y => 1, z => 1, ylong => 1 },
92     "'CF.$queue.{$cf_name}' != 'x'"              => { '-' => 1, other => 1, x => 0, y => 1, z => 1, ylong => 1 },
93     "'CF.$queue.{$cf_name}.Content' != 'x'"      => { '-' => 1, other => 1, x => 0, y => 1, z => 1, ylong => 1 },
94     "'CF.$queue.{$cf_name}.LargeContent' != 'x'" => { '-' => 1, other => 1, x => 1, y => 1, z => 1, ylong => 1 },
95
96     "CF.{$cf_id} = 'x' OR CF.{$cf_id} = 'y'"                        => { '-' => 0, other => 0, x => 1, y => 1, z => 0, ylong => 0 },
97     "'CF.{$cf_name}' = 'x' OR 'CF.{$cf_name}' = 'y'"                => { '-' => 0, other => 0, x => 1, y => 1, z => 0, ylong => 0 },
98     "'CF.$queue.{$cf_id}' = 'x' OR 'CF.$queue.{$cf_id}' = 'y'"      => { '-' => 0, other => 0, x => 1, y => 1, z => 0, ylong => 0 },
99     "'CF.$queue.{$cf_name}' = 'x' OR 'CF.$queue.{$cf_name}' = 'y'"  => { '-' => 0, other => 0, x => 1, y => 1, z => 0, ylong => 0 },
100
101     "CF.{$cf_id} = 'x' AND CF.{$cf_id} = 'y'"                        => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 0 },
102     "'CF.{$cf_name}' = 'x' AND 'CF.{$cf_name}' = 'y'"                => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 0 },
103     "'CF.$queue.{$cf_id}' = 'x' AND 'CF.$queue.{$cf_id}' = 'y'"      => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 0 },
104     "'CF.$queue.{$cf_name}' = 'x' AND 'CF.$queue.{$cf_name}' = 'y'"  => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 0 },
105
106     "CF.{$cf_id} != 'x' AND CF.{$cf_id} != 'y'"                        => { '-' => 1, other => 1, x => 0, y => 0, z => 1, ylong => 1 },
107     "'CF.{$cf_name}' != 'x' AND 'CF.{$cf_name}' != 'y'"                => { '-' => 1, other => 1, x => 0, y => 0, z => 1, ylong => 1 },
108     "'CF.$queue.{$cf_id}' != 'x' AND 'CF.$queue.{$cf_id}' != 'y'"      => { '-' => 1, other => 1, x => 0, y => 0, z => 1, ylong => 1 },
109     "'CF.$queue.{$cf_name}' != 'x' AND 'CF.$queue.{$cf_name}' != 'y'"  => { '-' => 1, other => 1, x => 0, y => 0, z => 1, ylong => 1 },
110
111     "CF.{$cf_id} = 'x' AND CF.{$cf_id} IS NULL"                        => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 0 },
112     "'CF.{$cf_name}' = 'x' AND 'CF.{$cf_name}' IS NULL"                => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 0 },
113     "'CF.$queue.{$cf_id}' = 'x' AND 'CF.$queue.{$cf_id}' IS NULL"      => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 0 },
114     "'CF.$queue.{$cf_name}' = 'x' AND 'CF.$queue.{$cf_name}' IS NULL"  => { '-' => 0, other => 0, x => 0, y => 0, z => 0, ylong => 0 },
115
116     "CF.{$cf_id} = 'x' OR CF.{$cf_id} IS NULL"                        => { '-' => 1, other => 1, x => 1, y => 0, z => 0, ylong => 0 },
117     "'CF.{$cf_name}' = 'x' OR 'CF.{$cf_name}' IS NULL"                => { '-' => 1, other => 1, x => 1, y => 0, z => 0, ylong => 0 },
118     "'CF.$queue.{$cf_id}' = 'x' OR 'CF.$queue.{$cf_id}' IS NULL"      => { '-' => 1, other => 1, x => 1, y => 0, z => 0, ylong => 0 },
119     "'CF.$queue.{$cf_name}' = 'x' OR 'CF.$queue.{$cf_name}' IS NULL"  => { '-' => 1, other => 1, x => 1, y => 0, z => 0, ylong => 0 },
120
121     "CF.{$cf_id} = 'x' AND CF.{$cf_id} IS NOT NULL"                        => { '-' => 0, other => 0, x => 1, y => 0, z => 0, ylong => 0 },
122     "'CF.{$cf_name}' = 'x' AND 'CF.{$cf_name}' IS NOT NULL"                => { '-' => 0, other => 0, x => 1, y => 0, z => 0, ylong => 0 },
123     "'CF.$queue.{$cf_id}' = 'x' AND 'CF.$queue.{$cf_id}' IS NOT NULL"      => { '-' => 0, other => 0, x => 1, y => 0, z => 0, ylong => 0 },
124     "'CF.$queue.{$cf_name}' = 'x' AND 'CF.$queue.{$cf_name}' IS NOT NULL"  => { '-' => 0, other => 0, x => 1, y => 0, z => 0, ylong => 0 },
125
126     "CF.{$cf_id} = 'x' OR CF.{$cf_id} IS NOT NULL"                        => { '-' => 0, other => 0, x => 1, y => 1, z => 1, ylong => 1 },
127     "'CF.{$cf_name}' = 'x' OR 'CF.{$cf_name}' IS NOT NULL"                => { '-' => 0, other => 0, x => 1, y => 1, z => 1, ylong => 1 },
128     "'CF.$queue.{$cf_id}' = 'x' OR 'CF.$queue.{$cf_id}' IS NOT NULL"      => { '-' => 0, other => 0, x => 1, y => 1, z => 1, ylong => 1 },
129     "'CF.$queue.{$cf_name}' = 'x' OR 'CF.$queue.{$cf_name}' IS NOT NULL"  => { '-' => 0, other => 0, x => 1, y => 1, z => 1, ylong => 1 },
130 );
131 run_tests(@tests);
132
133
134 sub run_tests {
135     my @tests = @_;
136     while (@tests) {
137         my $query = shift @tests;
138         my %results = %{ shift @tests };
139         subtest $query => sub {
140             my $tix = RT::Tickets->new(RT->SystemUser);
141             $tix->FromSQL( "$query" );
142
143             my $error = 0;
144
145             my $count = 0;
146             $count++ foreach grep $_, values %results;
147             is($tix->Count, $count, "found correct number of ticket(s)") or $error = 1;
148
149             my $good_tickets = ($tix->Count == $count);
150             while ( my $ticket = $tix->Next ) {
151                 next if $results{ $ticket->Subject };
152                 diag $ticket->Subject ." ticket has been found when it's not expected";
153                 $good_tickets = 0;
154             }
155             ok( $good_tickets, "all tickets are good" ) or $error = 1;
156
157             diag "Wrong SQL: ". $tix->BuildSelectQuery if $error;
158         };
159     }
160 }
161
162
163 done_testing;