3 use HTTP::Request::Common;
6 use RT::Test tests => 70;
8 my $cookie_jar = HTTP::Cookies->new;
9 my ($baseurl, $agent) = RT::Test->started_ok;
12 # give the agent a place to stash the cookies
14 $agent->cookie_jar($cookie_jar);
16 # create a regression queue if it doesn't exist
17 my $queue = RT::Test->load_or_create_queue( Name => 'Regression' );
18 ok $queue && $queue->id, 'loaded or created queue';
20 my $url = $agent->rt_base_url;
21 ok $agent->login, "logged in";
24 my $response = $agent->get($url."Search/Build.html");
25 ok $response->is_success, "Fetched ". $url ."Search/Build.html";
27 sub getQueryFromForm {
29 $agent->form_name('BuildQuery');
30 # This pulls out the "hidden input" query from the page
31 my $q = $agent->current_form->find_input("Query")->value;
40 my @clauses = grep { defined } map { $_->value } $agent->current_form->find_input("clauses");
45 diag "add the first condition";
47 ok $agent->form_name('BuildQuery'), "found the form once";
48 $agent->field("ActorField", "Owner");
49 $agent->field("ActorOp", "=");
50 $agent->field("ValueOfActor", "Nobody");
52 is getQueryFromForm($agent), "Owner = 'Nobody'", 'correct query';
55 diag "set the next condition";
57 ok($agent->form_name('BuildQuery'), "found the form again");
58 $agent->field("QueueOp", "!=");
59 $agent->field("ValueOfQueue", "Regression");
61 is getQueryFromForm($agent), "Owner = 'Nobody' AND Queue != 'Regression'",
65 diag "We're going to delete the owner";
67 $agent->select("clauses", ["0"] );
68 $agent->click("DeleteClause");
69 ok $agent->form_name('BuildQuery'), "found the form";
70 is getQueryFromForm($agent), "Queue != 'Regression'", 'correct query';
73 diag "add a cond with OR and se number by the way";
75 $agent->field("AndOr", "OR");
76 $agent->select("idOp", ">");
77 $agent->field("ValueOfid" => "1234");
78 $agent->click("AddClause");
79 ok $agent->form_name('BuildQuery'), "found the form again";
80 is getQueryFromForm($agent), "Queue != 'Regression' OR id > 1234",
81 "added something as OR, and number not quoted";
82 is_deeply selectedClauses($agent), ["1"], 'the id that we just entered is still selected';
86 diag "Move the second one up a level";
89 ok $agent->form_name('BuildQuery'), "found the form again";
90 is getQueryFromForm($agent), "id > 1234 OR Queue != 'Regression'", "moved up one";
91 is_deeply selectedClauses($agent), ["0"], 'the one we moved up is selected';
94 diag "Move the second one right";
96 $agent->click("Right");
97 ok $agent->form_name('BuildQuery'), "found the form again";
98 is getQueryFromForm($agent), "Queue != 'Regression' OR ( id > 1234 )",
99 "moved over to the right (and down)";
100 is_deeply selectedClauses($agent), ["2"], 'the one we moved right is selected';
103 diag "Move the block up";
105 $agent->select("clauses", ["1"]);
107 ok $agent->form_name('BuildQuery'), "found the form again";
108 is getQueryFromForm($agent), "( id > 1234 ) OR Queue != 'Regression'", "moved up";
109 is_deeply selectedClauses($agent), ["0"], 'the one we moved up is selected';
113 diag "Can not move up the top most clause";
115 $agent->select("clauses", ["0"]);
117 ok $agent->form_name('BuildQuery'), "found the form again";
118 $agent->content_contains("error: can't move up", "i shouldn't have been able to hit up");
119 is_deeply selectedClauses($agent), ["0"], 'the one we tried to move is selected';
122 diag "Can not move left the left most clause";
124 $agent->click("Left");
125 ok($agent->form_name('BuildQuery'), "found the form again");
126 $agent->content_contains("error: can't move left", "i shouldn't have been able to hit left");
127 is_deeply selectedClauses($agent), ["0"], 'the one we tried to move is selected';
130 diag "Add a condition into a nested block";
132 $agent->select("clauses", ["1"]);
133 $agent->select("ValueOfStatus" => "stalled");
135 ok $agent->form_name('BuildQuery'), "found the form again";
136 is_deeply selectedClauses($agent), ["2"], 'the one we added is only selected';
137 is getQueryFromForm($agent),
138 "( id > 1234 AND Status = 'stalled' ) OR Queue != 'Regression'",
142 diag "click advanced, enter 'C1 OR ( C2 AND C3 )', apply, aggregators should stay the same.";
144 my $response = $agent->get($url."Search/Edit.html");
145 ok( $response->is_success, "Fetched /Search/Edit.html" );
146 ok($agent->form_name('BuildQueryAdvanced'), "found the form");
147 $agent->field("Query", "Status = 'new' OR ( Status = 'open' AND Subject LIKE 'office' )");
149 is( getQueryFromForm($agent),
150 "Status = 'new' OR ( Status = 'open' AND Subject LIKE 'office' )",
151 "no aggregators change"
155 # - new items go one level down
156 # - add items at currently selected level
157 # - if nothing is selected, add at end, one level down
160 # - error if nothing selected
161 # - same item should be selected after move
162 # - can't move left if you're at the top level
165 # - error if nothing selected
166 # - same item should be selected after move
167 # - can always move right (no max depth...should there be?)
170 # - error if nothing selected
171 # - same item should be selected after move
172 # - can't move up if you're first in the list
175 # - error if nothing selected
176 # - same item should be selected after move
177 # - can't move down if you're last in the list
180 # - error if nothing selected
181 # - change all aggregators in the grouping
182 # - don't change any others
185 # - error if nothing selected
186 # - delete currently selected item
187 # - delete all children of a grouping
188 # - if delete leaves a node with no children, delete that, too
189 # - what should be selected?
192 # - clears entire query
193 # - clears it from the session, too
196 # create a custom field with nonascii name and try to add a condition
198 my $cf = RT::CustomField->new( RT->SystemUser );
199 $cf->LoadByName( Name => "\x{442}", Queue => 0 );
201 is($cf->Type, 'Freeform', 'loaded and type is correct');
203 my ($return, $msg) = $cf->Create(
208 ok($return, 'created CF') or diag "error: $msg";
211 my $response = $agent->get($url."Search/Build.html?NewQuery=1");
212 ok( $response->is_success, "Fetched " . $url."Search/Build.html" );
214 ok($agent->form_name('BuildQuery'), "found the form once");
215 $agent->field("ValueOf'CF.{\x{442}}'", "\x{441}");
217 is( getQueryFromForm($agent),
218 "'CF.{\x{442}}' LIKE '\x{441}'",
219 "no changes, no duplicate condition with badly encoded text"
224 diag "input a condition, select (several conditions), click delete";
226 my $response = $agent->get( $url."Search/Edit.html" );
227 ok $response->is_success, "Fetched /Search/Edit.html";
228 ok $agent->form_name('BuildQueryAdvanced'), "found the form";
229 $agent->field("Query", "( Status = 'new' OR Status = 'open' )");
231 is( getQueryFromForm($agent),
232 "( Status = 'new' OR Status = 'open' )",
235 $agent->select("clauses", [qw(0 1 2)]);
236 $agent->field( ValueOfid => 10 );
237 $agent->click("DeleteClause");
239 is( getQueryFromForm($agent),
241 "replaced query successfuly"
245 diag "send query with not quoted negative number";
247 my $response = $agent->get($url."Search/Build.html?Query=Priority%20>%20-2");
248 ok( $response->is_success, "Fetched " . $url."Search/Build.html" );
250 is( getQueryFromForm($agent),
256 diag "click advanced, enter an invalid SQL IS restriction, apply and check that we corrected it";
258 my $response = $agent->get($url."Search/Edit.html");
259 ok( $response->is_success, "Fetched /Search/Edit.html" );
260 ok($agent->form_name('BuildQueryAdvanced'), "found the form");
261 $agent->field("Query", "Requestor.EmailAddress IS 'FOOBAR'");
263 is( getQueryFromForm($agent),
264 "Requestor.EmailAddress IS NULL",
265 "foobar is replaced by NULL"
269 diag "click advanced, enter an invalid SQL IS NOT restriction, apply and check that we corrected it";
271 my $response = $agent->get($url."Search/Edit.html");
272 ok( $response->is_success, "Fetched /Search/Edit.html" );
273 ok($agent->form_name('BuildQueryAdvanced'), "found the form");
274 $agent->field("Query", "Requestor.EmailAddress IS NOT 'FOOBAR'");
276 is( getQueryFromForm($agent),
277 "Requestor.EmailAddress IS NOT NULL",
278 "foobar is replaced by NULL"
282 diag "click advanced, enter a valid SQL, but the field is lower cased";
284 my $response = $agent->get($url."Search/Edit.html");
285 ok( $response->is_success, "Fetched /Search/Edit.html" );
286 ok($agent->form_name('BuildQueryAdvanced'), "found the form");
287 $agent->field("Query", "status = 'new'");
289 $agent->content_lacks( 'Unknown field:', 'no "unknown field" warning' );
290 is( getQueryFromForm($agent),
292 "field's case is corrected"
296 diag "make sure skipped order by field doesn't break search";
298 my $t = RT::Test->create_ticket( Queue => 'General', Subject => 'test' );
299 ok $t && $t->id, 'created a ticket';
301 $agent->get_ok($url."Search/Edit.html");
302 ok($agent->form_name('BuildQueryAdvanced'), "found the form");
303 $agent->field("Query", "id = ". $t->id);
306 $agent->follow_link_ok({id => 'page-results'});
307 ok( $agent->find_link(
309 url_regex => qr{/Ticket/Display\.html},
310 ), "link to the ticket" );
312 $agent->follow_link_ok({id => 'page-edit_search'});
313 $agent->form_name('BuildQuery');
314 $agent->field("OrderBy", 'Requestor.EmailAddress', 3);
316 $agent->form_name('BuildQuery');
317 is $agent->value('OrderBy', 1), 'id';
318 is $agent->value('OrderBy', 2), '';
319 is $agent->value('OrderBy', 3), 'Requestor.EmailAddress';
321 $agent->follow_link_ok({id => 'page-results'});
322 ok( $agent->find_link(
324 url_regex => qr{/Ticket/Display\.html},
325 ), "link to the ticket" );