3 use HTTP::Request::Common;
7 use RT::Test tests => 70;
9 my $cookie_jar = HTTP::Cookies->new;
10 my ($baseurl, $agent) = RT::Test->started_ok;
13 # give the agent a place to stash the cookies
15 $agent->cookie_jar($cookie_jar);
17 # create a regression queue if it doesn't exist
18 my $queue = RT::Test->load_or_create_queue( Name => 'Regression' );
19 ok $queue && $queue->id, 'loaded or created queue';
21 my $url = $agent->rt_base_url;
22 ok $agent->login, "logged in";
25 my $response = $agent->get($url."Search/Build.html");
26 ok $response->is_success, "Fetched ". $url ."Search/Build.html";
28 sub getQueryFromForm {
30 $agent->form_name('BuildQuery');
31 # This pulls out the "hidden input" query from the page
32 my $q = $agent->current_form->find_input("Query")->value;
41 my @clauses = grep { defined } map { $_->value } $agent->current_form->find_input("clauses");
46 diag "add the first condition";
48 ok $agent->form_name('BuildQuery'), "found the form once";
49 $agent->field("ActorField", "Owner");
50 $agent->field("ActorOp", "=");
51 $agent->field("ValueOfActor", "Nobody");
53 is getQueryFromForm($agent), "Owner = 'Nobody'", 'correct query';
56 diag "set the next condition";
58 ok($agent->form_name('BuildQuery'), "found the form again");
59 $agent->field("QueueOp", "!=");
60 $agent->field("ValueOfQueue", "Regression");
62 is getQueryFromForm($agent), "Owner = 'Nobody' AND Queue != 'Regression'",
66 diag "We're going to delete the owner";
68 $agent->select("clauses", ["0"] );
69 $agent->click("DeleteClause");
70 ok $agent->form_name('BuildQuery'), "found the form";
71 is getQueryFromForm($agent), "Queue != 'Regression'", 'correct query';
74 diag "add a cond with OR and se number by the way";
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($agent), "Queue != 'Regression' OR id > 1234",
82 "added something as OR, and number not quoted";
83 is_deeply selectedClauses($agent), ["1"], 'the id that we just entered is still selected';
87 diag "Move the second one up a level";
90 ok $agent->form_name('BuildQuery'), "found the form again";
91 is getQueryFromForm($agent), "id > 1234 OR Queue != 'Regression'", "moved up one";
92 is_deeply selectedClauses($agent), ["0"], 'the one we moved up is selected';
95 diag "Move the second one right";
97 $agent->click("Right");
98 ok $agent->form_name('BuildQuery'), "found the form again";
99 is getQueryFromForm($agent), "Queue != 'Regression' OR ( id > 1234 )",
100 "moved over to the right (and down)";
101 is_deeply selectedClauses($agent), ["2"], 'the one we moved right is selected';
104 diag "Move the block up";
106 $agent->select("clauses", ["1"]);
108 ok $agent->form_name('BuildQuery'), "found the form again";
109 is getQueryFromForm($agent), "( id > 1234 ) OR Queue != 'Regression'", "moved up";
110 is_deeply selectedClauses($agent), ["0"], 'the one we moved up is selected';
114 diag "Can not move up the top most clause";
116 $agent->select("clauses", ["0"]);
118 ok $agent->form_name('BuildQuery'), "found the form again";
119 $agent->content_contains("error: can't move up", "i shouldn't have been able to hit up");
120 is_deeply selectedClauses($agent), ["0"], 'the one we tried to move is selected';
123 diag "Can not move left the left most clause";
125 $agent->click("Left");
126 ok($agent->form_name('BuildQuery'), "found the form again");
127 $agent->content_contains("error: can't move left", "i shouldn't have been able to hit left");
128 is_deeply selectedClauses($agent), ["0"], 'the one we tried to move is selected';
131 diag "Add a condition into a nested block";
133 $agent->select("clauses", ["1"]);
134 $agent->select("ValueOfStatus" => "stalled");
136 ok $agent->form_name('BuildQuery'), "found the form again";
137 is_deeply selectedClauses($agent), ["2"], 'the one we added is only selected';
138 is getQueryFromForm($agent),
139 "( id > 1234 AND Status = 'stalled' ) OR Queue != 'Regression'",
143 diag "click advanced, enter 'C1 OR ( C2 AND C3 )', apply, aggregators should stay the same.";
145 my $response = $agent->get($url."Search/Edit.html");
146 ok( $response->is_success, "Fetched /Search/Edit.html" );
147 ok($agent->form_name('BuildQueryAdvanced'), "found the form");
148 $agent->field("Query", "Status = 'new' OR ( Status = 'open' AND Subject LIKE 'office' )");
150 is( getQueryFromForm($agent),
151 "Status = 'new' OR ( Status = 'open' AND Subject LIKE 'office' )",
152 "no aggregators change"
156 # - new items go one level down
157 # - add items at currently selected level
158 # - if nothing is selected, add at end, one level down
161 # - error if nothing selected
162 # - same item should be selected after move
163 # - can't move left if you're at the top level
166 # - error if nothing selected
167 # - same item should be selected after move
168 # - can always move right (no max depth...should there be?)
171 # - error if nothing selected
172 # - same item should be selected after move
173 # - can't move up if you're first in the list
176 # - error if nothing selected
177 # - same item should be selected after move
178 # - can't move down if you're last in the list
181 # - error if nothing selected
182 # - change all aggregators in the grouping
183 # - don't change any others
186 # - error if nothing selected
187 # - delete currently selected item
188 # - delete all children of a grouping
189 # - if delete leaves a node with no children, delete that, too
190 # - what should be selected?
193 # - clears entire query
194 # - clears it from the session, too
197 # create a custom field with nonascii name and try to add a condition
199 my $cf = RT::CustomField->new( RT->SystemUser );
200 $cf->LoadByName( Name => "\x{442}", Queue => 0 );
202 is($cf->Type, 'Freeform', 'loaded and type is correct');
204 my ($return, $msg) = $cf->Create(
209 ok($return, 'created CF') or diag "error: $msg";
212 my $response = $agent->get($url."Search/Build.html?NewQuery=1");
213 ok( $response->is_success, "Fetched " . $url."Search/Build.html" );
215 ok($agent->form_name('BuildQuery'), "found the form once");
216 $agent->field("ValueOf'CF.{\x{442}}'", "\x{441}");
218 is( getQueryFromForm($agent),
219 "'CF.{\x{442}}' LIKE '\x{441}'",
220 "no changes, no duplicate condition with badly encoded text"
225 diag "input a condition, select (several conditions), click delete";
227 my $response = $agent->get( $url."Search/Edit.html" );
228 ok $response->is_success, "Fetched /Search/Edit.html";
229 ok $agent->form_name('BuildQueryAdvanced'), "found the form";
230 $agent->field("Query", "( Status = 'new' OR Status = 'open' )");
232 is( getQueryFromForm($agent),
233 "( Status = 'new' OR Status = 'open' )",
236 $agent->select("clauses", [qw(0 1 2)]);
237 $agent->field( ValueOfid => 10 );
238 $agent->click("DeleteClause");
240 is( getQueryFromForm($agent),
242 "replaced query successfuly"
246 diag "send query with not quoted negative number";
248 my $response = $agent->get($url."Search/Build.html?Query=Priority%20>%20-2");
249 ok( $response->is_success, "Fetched " . $url."Search/Build.html" );
251 is( getQueryFromForm($agent),
257 diag "click advanced, enter an invalid SQL IS restriction, apply and check that we corrected it";
259 my $response = $agent->get($url."Search/Edit.html");
260 ok( $response->is_success, "Fetched /Search/Edit.html" );
261 ok($agent->form_name('BuildQueryAdvanced'), "found the form");
262 $agent->field("Query", "Requestor.EmailAddress IS 'FOOBAR'");
264 is( getQueryFromForm($agent),
265 "Requestor.EmailAddress IS NULL",
266 "foobar is replaced by NULL"
270 diag "click advanced, enter an invalid SQL IS NOT restriction, apply and check that we corrected it";
272 my $response = $agent->get($url."Search/Edit.html");
273 ok( $response->is_success, "Fetched /Search/Edit.html" );
274 ok($agent->form_name('BuildQueryAdvanced'), "found the form");
275 $agent->field("Query", "Requestor.EmailAddress IS NOT 'FOOBAR'");
277 is( getQueryFromForm($agent),
278 "Requestor.EmailAddress IS NOT NULL",
279 "foobar is replaced by NULL"
283 diag "click advanced, enter a valid SQL, but the field is lower cased";
285 my $response = $agent->get($url."Search/Edit.html");
286 ok( $response->is_success, "Fetched /Search/Edit.html" );
287 ok($agent->form_name('BuildQueryAdvanced'), "found the form");
288 $agent->field("Query", "status = 'new'");
290 $agent->content_lacks( 'Unknown field:', 'no "unknown field" warning' );
291 is( getQueryFromForm($agent),
293 "field's case is corrected"
297 diag "make sure skipped order by field doesn't break search";
299 my $t = RT::Test->create_ticket( Queue => 'General', Subject => 'test' );
300 ok $t && $t->id, 'created a ticket';
302 $agent->get_ok($url."Search/Edit.html");
303 ok($agent->form_name('BuildQueryAdvanced'), "found the form");
304 $agent->field("Query", "id = ". $t->id);
307 $agent->follow_link_ok({id => 'page-results'});
308 ok( $agent->find_link(
310 url_regex => qr{/Ticket/Display\.html},
311 ), "link to the ticket" );
313 $agent->follow_link_ok({id => 'page-edit_search'});
314 $agent->form_name('BuildQuery');
315 $agent->field("OrderBy", 'Requestor.EmailAddress', 3);
317 $agent->form_name('BuildQuery');
318 is $agent->value('OrderBy', 1), 'id';
319 is $agent->value('OrderBy', 2), '';
320 is $agent->value('OrderBy', 3), 'Requestor.EmailAddress';
322 $agent->follow_link_ok({id => 'page-results'});
323 ok( $agent->find_link(
325 url_regex => qr{/Ticket/Display\.html},
326 ), "link to the ticket" );