import rt 3.8.10
[freeside.git] / rt / t / web / query_builder.t
1 #!/usr/bin/perl
2
3 use strict;
4 use HTTP::Request::Common;
5 use HTTP::Cookies;
6 use LWP;
7 use Encode;
8 use RT::Test tests => 50;
9
10 my $cookie_jar = HTTP::Cookies->new;
11 my ($baseurl, $agent) = RT::Test->started_ok;
12
13
14 # give the agent a place to stash the cookies
15
16 $agent->cookie_jar($cookie_jar);
17
18 # create a regression queue if it doesn't exist
19 my $queue = RT::Test->load_or_create_queue( Name => 'Regression' );
20 ok $queue && $queue->id, 'loaded or created queue';
21
22 my $url = $agent->rt_base_url;
23 ok $agent->login, "logged in";
24
25 # {{{ Query Builder tests
26
27 my $response = $agent->get($url."Search/Build.html");
28 ok $response->is_success, "Fetched ". $url ."Search/Build.html";
29
30 sub getQueryFromForm {
31     $agent->form_name('BuildQuery');
32     # This pulls out the "hidden input" query from the page
33     my $q = $agent->current_form->find_input("Query")->value;
34     $q =~ s/^\s+//g;
35     $q =~ s/\s+$//g;
36     $q =~ s/\s+/ /g;
37     return $q;
38 }
39
40 sub selectedClauses {
41     my @clauses = grep { defined } map { $_->value } $agent->current_form->find_input("clauses");
42     return [ @clauses ];
43 }
44
45
46 diag "add the first condition" if $ENV{'TEST_VERBOSE'};
47 {
48     ok $agent->form_name('BuildQuery'), "found the form once";
49     $agent->field("ActorField", "Owner");
50     $agent->field("ActorOp", "=");
51     $agent->field("ValueOfActor", "Nobody");
52     $agent->submit;
53     is getQueryFromForm, "Owner = 'Nobody'", 'correct query';
54 }
55
56 diag "set the next condition" if $ENV{'TEST_VERBOSE'};
57 {
58     ok($agent->form_name('BuildQuery'), "found the form again");
59     $agent->field("QueueOp", "!=");
60     $agent->field("ValueOfQueue", "Regression");
61     $agent->submit;
62     is getQueryFromForm, "Owner = 'Nobody' AND Queue != 'Regression'",
63         'correct query';
64 }
65
66 diag "We're going to delete the owner" if $ENV{'TEST_VERBOSE'};
67 {
68     $agent->select("clauses", ["0"] );
69     $agent->click("DeleteClause");
70     ok $agent->form_name('BuildQuery'), "found the form";
71     is getQueryFromForm, "Queue != 'Regression'", 'correct query';
72 }
73
74 diag "add a cond with OR and se number by the way" if $ENV{'TEST_VERBOSE'};
75 {
76     $agent->field("AndOr", "OR");
77     $agent->select("idOp", ">");
78     $agent->field("ValueOfid" => "1234");
79     $agent->click("AddClause");
80     ok $agent->form_name('BuildQuery'), "found the form again";
81     is getQueryFromForm, "Queue != 'Regression' OR id > 1234",
82         "added something as OR, and number not quoted";
83     is_deeply selectedClauses, ["1"], 'the id that we just entered is still selected';
84
85 }
86
87 diag "Move the second one up a level" if $ENV{'TEST_VERBOSE'};
88 {
89     $agent->click("Up");
90     ok $agent->form_name('BuildQuery'), "found the form again";
91     is getQueryFromForm, "id > 1234 OR Queue != 'Regression'", "moved up one";
92     is_deeply selectedClauses, ["0"], 'the one we moved up is selected';
93 }
94
95 diag "Move the second one right" if $ENV{'TEST_VERBOSE'};
96 {
97     $agent->click("Right");
98     ok $agent->form_name('BuildQuery'), "found the form again";
99     is getQueryFromForm, "Queue != 'Regression' OR ( id > 1234 )",
100         "moved over to the right (and down)";
101     is_deeply selectedClauses, ["2"], 'the one we moved right is selected';
102 }
103
104 diag "Move the block up" if $ENV{'TEST_VERBOSE'};
105 {
106     $agent->select("clauses", ["1"]);
107     $agent->click("Up");
108     ok $agent->form_name('BuildQuery'), "found the form again";
109     is getQueryFromForm, "( id > 1234 ) OR Queue != 'Regression'", "moved up";
110     is_deeply selectedClauses, ["0"], 'the one we moved up is selected';
111 }
112
113
114 diag "Can not move up the top most clause" if $ENV{'TEST_VERBOSE'};
115 {
116     $agent->select("clauses", ["0"]);
117     $agent->click("Up");
118     ok $agent->form_name('BuildQuery'), "found the form again";
119     $agent->content_like(qr/error: can\S+t move up/, "i shouldn't have been able to hit up");
120     is_deeply selectedClauses, ["0"], 'the one we tried to move is selected';
121 }
122
123 diag "Can not move left the left most clause" if $ENV{'TEST_VERBOSE'};
124 {
125     $agent->click("Left");
126     ok($agent->form_name('BuildQuery'), "found the form again");
127     $agent->content_like(qr/error: can\S+t move left/, "i shouldn't have been able to hit left");
128     is_deeply selectedClauses, ["0"], 'the one we tried to move is selected';
129 }
130
131 diag "Add a condition into a nested block" if $ENV{'TEST_VERBOSE'};
132 {
133     $agent->select("clauses", ["1"]);
134     $agent->select("ValueOfStatus" => "stalled");
135     $agent->submit;
136     ok $agent->form_name('BuildQuery'), "found the form again";
137     is_deeply selectedClauses, ["2"], 'the one we added is only selected';
138     is getQueryFromForm,
139         "( id > 1234 AND Status = 'stalled' ) OR Queue != 'Regression'",
140         "added new one";
141 }
142
143 diag "click advanced, enter 'C1 OR ( C2 AND C3 )', apply, aggregators should stay the same."
144     if $ENV{'TEST_VERBOSE'};
145 {
146     my $response = $agent->get($url."Search/Edit.html");
147     ok( $response->is_success, "Fetched /Search/Edit.html" );
148     ok($agent->form_number(3), "found the form");
149     $agent->field("Query", "Status = 'new' OR ( Status = 'open' AND Subject LIKE 'office' )");
150     $agent->submit;
151     is( getQueryFromForm,
152         "Status = 'new' OR ( Status = 'open' AND Subject LIKE 'office' )",
153         "no aggregators change"
154     );
155 }
156
157 # - new items go one level down
158 # - add items at currently selected level
159 # - if nothing is selected, add at end, one level down
160 #
161 # move left
162 # - error if nothing selected
163 # - same item should be selected after move
164 # - can't move left if you're at the top level
165 #
166 # move right
167 # - error if nothing selected
168 # - same item should be selected after move
169 # - can always move right (no max depth...should there be?)
170 #
171 # move up
172 # - error if nothing selected
173 # - same item should be selected after move
174 # - can't move up if you're first in the list
175 #
176 # move down
177 # - error if nothing selected
178 # - same item should be selected after move
179 # - can't move down if you're last in the list
180 #
181 # toggle
182 # - error if nothing selected
183 # - change all aggregators in the grouping
184 # - don't change any others
185 #
186 # delete
187 # - error if nothing selected
188 # - delete currently selected item
189 # - delete all children of a grouping
190 # - if delete leaves a node with no children, delete that, too
191 # - what should be selected?
192 #
193 # Clear
194 # - clears entire query
195 # - clears it from the session, too
196
197 # }}}
198
199 # create a custom field with nonascii name and try to add a condition
200 {
201     my $cf = RT::CustomField->new( $RT::SystemUser );
202     $cf->LoadByName( Name => "\x{442}", Queue => 0 );
203     if ( $cf->id ) {
204         is($cf->Type, 'Freeform', 'loaded and type is correct');
205     } else {
206         my ($return, $msg) = $cf->Create(
207             Name => "\x{442}",
208             Queue => 0,
209             Type => 'Freeform',
210         );
211         ok($return, 'created CF') or diag "error: $msg";
212     }
213
214     my $response = $agent->get($url."Search/Build.html?NewQuery=1");
215     ok( $response->is_success, "Fetched " . $url."Search/Build.html" );
216
217     ok($agent->form_name('BuildQuery'), "found the form once");
218     $agent->field("ValueOf'CF.{\x{442}}'", "\x{441}");
219     $agent->submit();
220     is( getQueryFromForm,
221         "'CF.{\x{442}}' LIKE '\x{441}'",
222         "no changes, no duplicate condition with badly encoded text"
223     );
224
225 }
226
227 diag "input a condition, select (several conditions), click delete"
228     if $ENV{'TEST_VERBOSE'};
229 {
230     my $response = $agent->get( $url."Search/Edit.html" );
231     ok $response->is_success, "Fetched /Search/Edit.html";
232     ok $agent->form_number(3), "found the form";
233     $agent->field("Query", "( Status = 'new' OR Status = 'open' )");
234     $agent->submit;
235     is( getQueryFromForm,
236         "( Status = 'new' OR Status = 'open' )",
237         "query is the same"
238     );
239     $agent->select("clauses", [qw(0 1 2)]);
240     $agent->field( ValueOfid => 10 );
241     $agent->click("DeleteClause");
242
243     is( getQueryFromForm,
244         "id < 10",
245         "replaced query successfuly"
246     );
247 }
248
249 diag "send query with not quoted negative number";
250 {
251     my $response = $agent->get($url."Search/Build.html?Query=Priority%20>%20-2");
252     ok( $response->is_success, "Fetched " . $url."Search/Build.html" );
253
254     is( getQueryFromForm,
255         "Priority > -2",
256         "query is the same"
257     );
258 }
259
260 diag "click advanced, enter an invalid SQL IS restriction, apply and check that we corrected it";
261 {
262     my $response = $agent->get($url."Search/Edit.html");
263     ok( $response->is_success, "Fetched /Search/Edit.html" );
264     ok($agent->form_number(3), "found the form");
265     $agent->field("Query", "Requestor.EmailAddress IS 'FOOBAR'");
266     $agent->submit;
267     is( getQueryFromForm($agent),
268         "Requestor.EmailAddress IS NULL",
269         "foobar is replaced by NULL"
270     );
271 }
272
273 diag "click advanced, enter an invalid SQL IS NOT restriction, apply and check that we corrected it";
274 {
275     my $response = $agent->get($url."Search/Edit.html");
276     ok( $response->is_success, "Fetched /Search/Edit.html" );
277     ok($agent->form_number(3), "found the form");
278     $agent->field("Query", "Requestor.EmailAddress IS NOT 'FOOBAR'");
279     $agent->submit;
280     is( getQueryFromForm($agent),
281         "Requestor.EmailAddress IS NOT NULL",
282         "foobar is replaced by NULL"
283     );
284 }
285