4 use HTTP::Request::Common;
8 use RT::Test tests => 70;
10 my $cookie_jar = HTTP::Cookies->new;
11 my ($baseurl, $agent) = RT::Test->started_ok;
14 # give the agent a place to stash the cookies
16 $agent->cookie_jar($cookie_jar);
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';
22 my $url = $agent->rt_base_url;
23 ok $agent->login, "logged in";
26 my $response = $agent->get($url."Search/Build.html");
27 ok $response->is_success, "Fetched ". $url ."Search/Build.html";
29 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;
42 my @clauses = grep { defined } map { $_->value } $agent->current_form->find_input("clauses");
47 diag "add the first condition";
49 ok $agent->form_name('BuildQuery'), "found the form once";
50 $agent->field("ActorField", "Owner");
51 $agent->field("ActorOp", "=");
52 $agent->field("ValueOfActor", "Nobody");
54 is getQueryFromForm($agent), "Owner = 'Nobody'", 'correct query';
57 diag "set the next condition";
59 ok($agent->form_name('BuildQuery'), "found the form again");
60 $agent->field("QueueOp", "!=");
61 $agent->field("ValueOfQueue", "Regression");
63 is getQueryFromForm($agent), "Owner = 'Nobody' AND Queue != 'Regression'",
67 diag "We're going to delete the owner";
69 $agent->select("clauses", ["0"] );
70 $agent->click("DeleteClause");
71 ok $agent->form_name('BuildQuery'), "found the form";
72 is getQueryFromForm($agent), "Queue != 'Regression'", 'correct query';
75 diag "add a cond with OR and se number by the way";
77 $agent->field("AndOr", "OR");
78 $agent->select("idOp", ">");
79 $agent->field("ValueOfid" => "1234");
80 $agent->click("AddClause");
81 ok $agent->form_name('BuildQuery'), "found the form again";
82 is getQueryFromForm($agent), "Queue != 'Regression' OR id > 1234",
83 "added something as OR, and number not quoted";
84 is_deeply selectedClauses($agent), ["1"], 'the id that we just entered is still selected';
88 diag "Move the second one up a level";
91 ok $agent->form_name('BuildQuery'), "found the form again";
92 is getQueryFromForm($agent), "id > 1234 OR Queue != 'Regression'", "moved up one";
93 is_deeply selectedClauses($agent), ["0"], 'the one we moved up is selected';
96 diag "Move the second one right";
98 $agent->click("Right");
99 ok $agent->form_name('BuildQuery'), "found the form again";
100 is getQueryFromForm($agent), "Queue != 'Regression' OR ( id > 1234 )",
101 "moved over to the right (and down)";
102 is_deeply selectedClauses($agent), ["2"], 'the one we moved right is selected';
105 diag "Move the block up";
107 $agent->select("clauses", ["1"]);
109 ok $agent->form_name('BuildQuery'), "found the form again";
110 is getQueryFromForm($agent), "( id > 1234 ) OR Queue != 'Regression'", "moved up";
111 is_deeply selectedClauses($agent), ["0"], 'the one we moved up is selected';
115 diag "Can not move up the top most clause";
117 $agent->select("clauses", ["0"]);
119 ok $agent->form_name('BuildQuery'), "found the form again";
120 $agent->content_contains("error: can't move up", "i shouldn't have been able to hit up");
121 is_deeply selectedClauses($agent), ["0"], 'the one we tried to move is selected';
124 diag "Can not move left the left most clause";
126 $agent->click("Left");
127 ok($agent->form_name('BuildQuery'), "found the form again");
128 $agent->content_contains("error: can't move left", "i shouldn't have been able to hit left");
129 is_deeply selectedClauses($agent), ["0"], 'the one we tried to move is selected';
132 diag "Add a condition into a nested block";
134 $agent->select("clauses", ["1"]);
135 $agent->select("ValueOfStatus" => "stalled");
137 ok $agent->form_name('BuildQuery'), "found the form again";
138 is_deeply selectedClauses($agent), ["2"], 'the one we added is only selected';
139 is getQueryFromForm($agent),
140 "( id > 1234 AND Status = 'stalled' ) OR Queue != 'Regression'",
144 diag "click advanced, enter 'C1 OR ( C2 AND C3 )', apply, aggregators should stay the same.";
146 my $response = $agent->get($url."Search/Edit.html");
147 ok( $response->is_success, "Fetched /Search/Edit.html" );
148 ok($agent->form_name('BuildQueryAdvanced'), "found the form");
149 $agent->field("Query", "Status = 'new' OR ( Status = 'open' AND Subject LIKE 'office' )");
151 is( getQueryFromForm($agent),
152 "Status = 'new' OR ( Status = 'open' AND Subject LIKE 'office' )",
153 "no aggregators change"
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
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
167 # - error if nothing selected
168 # - same item should be selected after move
169 # - can always move right (no max depth...should there be?)
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
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
182 # - error if nothing selected
183 # - change all aggregators in the grouping
184 # - don't change any others
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?
194 # - clears entire query
195 # - clears it from the session, too
198 # create a custom field with nonascii name and try to add a condition
200 my $cf = RT::CustomField->new( RT->SystemUser );
201 $cf->LoadByName( Name => "\x{442}", Queue => 0 );
203 is($cf->Type, 'Freeform', 'loaded and type is correct');
205 my ($return, $msg) = $cf->Create(
210 ok($return, 'created CF') or diag "error: $msg";
213 my $response = $agent->get($url."Search/Build.html?NewQuery=1");
214 ok( $response->is_success, "Fetched " . $url."Search/Build.html" );
216 ok($agent->form_name('BuildQuery'), "found the form once");
217 $agent->field("ValueOf'CF.{\x{442}}'", "\x{441}");
219 is( getQueryFromForm($agent),
220 "'CF.{\x{442}}' LIKE '\x{441}'",
221 "no changes, no duplicate condition with badly encoded text"
226 diag "input a condition, select (several conditions), click delete";
228 my $response = $agent->get( $url."Search/Edit.html" );
229 ok $response->is_success, "Fetched /Search/Edit.html";
230 ok $agent->form_name('BuildQueryAdvanced'), "found the form";
231 $agent->field("Query", "( Status = 'new' OR Status = 'open' )");
233 is( getQueryFromForm($agent),
234 "( Status = 'new' OR Status = 'open' )",
237 $agent->select("clauses", [qw(0 1 2)]);
238 $agent->field( ValueOfid => 10 );
239 $agent->click("DeleteClause");
241 is( getQueryFromForm($agent),
243 "replaced query successfuly"
247 diag "send query with not quoted negative number";
249 my $response = $agent->get($url."Search/Build.html?Query=Priority%20>%20-2");
250 ok( $response->is_success, "Fetched " . $url."Search/Build.html" );
252 is( getQueryFromForm($agent),
258 diag "click advanced, enter an invalid SQL IS restriction, apply and check that we corrected it";
260 my $response = $agent->get($url."Search/Edit.html");
261 ok( $response->is_success, "Fetched /Search/Edit.html" );
262 ok($agent->form_name('BuildQueryAdvanced'), "found the form");
263 $agent->field("Query", "Requestor.EmailAddress IS 'FOOBAR'");
265 is( getQueryFromForm($agent),
266 "Requestor.EmailAddress IS NULL",
267 "foobar is replaced by NULL"
271 diag "click advanced, enter an invalid SQL IS NOT restriction, apply and check that we corrected it";
273 my $response = $agent->get($url."Search/Edit.html");
274 ok( $response->is_success, "Fetched /Search/Edit.html" );
275 ok($agent->form_name('BuildQueryAdvanced'), "found the form");
276 $agent->field("Query", "Requestor.EmailAddress IS NOT 'FOOBAR'");
278 is( getQueryFromForm($agent),
279 "Requestor.EmailAddress IS NOT NULL",
280 "foobar is replaced by NULL"
284 diag "click advanced, enter a valid SQL, but the field is lower cased";
286 my $response = $agent->get($url."Search/Edit.html");
287 ok( $response->is_success, "Fetched /Search/Edit.html" );
288 ok($agent->form_name('BuildQueryAdvanced'), "found the form");
289 $agent->field("Query", "status = 'new'");
291 $agent->content_lacks( 'Unknown field:', 'no "unknown field" warning' );
292 is( getQueryFromForm($agent),
294 "field's case is corrected"
298 diag "make sure skipped order by field doesn't break search";
300 my $t = RT::Test->create_ticket( Queue => 'General', Subject => 'test' );
301 ok $t && $t->id, 'created a ticket';
303 $agent->get_ok($url."Search/Edit.html");
304 ok($agent->form_name('BuildQueryAdvanced'), "found the form");
305 $agent->field("Query", "id = ". $t->id);
308 $agent->follow_link_ok({id => 'page-results'});
309 ok( $agent->find_link(
311 url_regex => qr{/Ticket/Display\.html},
312 ), "link to the ticket" );
314 $agent->follow_link_ok({id => 'page-edit_search'});
315 $agent->form_name('BuildQuery');
316 $agent->field("OrderBy", 'Requestor.EmailAddress', 3);
318 $agent->form_name('BuildQuery');
319 is $agent->value('OrderBy', 1), 'id';
320 is $agent->value('OrderBy', 2), '';
321 is $agent->value('OrderBy', 3), 'Requestor.EmailAddress';
323 $agent->follow_link_ok({id => 'page-results'});
324 ok( $agent->find_link(
326 url_regex => qr{/Ticket/Display\.html},
327 ), "link to the ticket" );