4 use HTTP::Request::Common;
8 use RT::Test tests => 50;
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";
25 # {{{ Query Builder tests
27 my $response = $agent->get($url."Search/Build.html");
28 ok $response->is_success, "Fetched ". $url ."Search/Build.html";
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;
41 my @clauses = grep { defined } map { $_->value } $agent->current_form->find_input("clauses");
46 diag "add the first condition" if $ENV{'TEST_VERBOSE'};
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, "Owner = 'Nobody'", 'correct query';
56 diag "set the next condition" if $ENV{'TEST_VERBOSE'};
58 ok($agent->form_name('BuildQuery'), "found the form again");
59 $agent->field("QueueOp", "!=");
60 $agent->field("ValueOfQueue", "Regression");
62 is getQueryFromForm, "Owner = 'Nobody' AND Queue != 'Regression'",
66 diag "We're going to delete the owner" if $ENV{'TEST_VERBOSE'};
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';
74 diag "add a cond with OR and se number by the way" if $ENV{'TEST_VERBOSE'};
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';
87 diag "Move the second one up a level" if $ENV{'TEST_VERBOSE'};
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';
95 diag "Move the second one right" if $ENV{'TEST_VERBOSE'};
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';
104 diag "Move the block up" if $ENV{'TEST_VERBOSE'};
106 $agent->select("clauses", ["1"]);
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';
114 diag "Can not move up the top most clause" if $ENV{'TEST_VERBOSE'};
116 $agent->select("clauses", ["0"]);
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';
123 diag "Can not move left the left most clause" if $ENV{'TEST_VERBOSE'};
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';
131 diag "Add a condition into a nested block" if $ENV{'TEST_VERBOSE'};
133 $agent->select("clauses", ["1"]);
134 $agent->select("ValueOfStatus" => "stalled");
136 ok $agent->form_name('BuildQuery'), "found the form again";
137 is_deeply selectedClauses, ["2"], 'the one we added is only selected';
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."
144 if $ENV{'TEST_VERBOSE'};
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' )");
151 is( getQueryFromForm,
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
199 # create a custom field with nonascii name and try to add a condition
201 my $cf = RT::CustomField->new( $RT::SystemUser );
202 $cf->LoadByName( Name => "\x{442}", Queue => 0 );
204 is($cf->Type, 'Freeform', 'loaded and type is correct');
206 my ($return, $msg) = $cf->Create(
211 ok($return, 'created CF') or diag "error: $msg";
214 my $response = $agent->get($url."Search/Build.html?NewQuery=1");
215 ok( $response->is_success, "Fetched " . $url."Search/Build.html" );
217 ok($agent->form_name('BuildQuery'), "found the form once");
218 $agent->field("ValueOf'CF.{\x{442}}'", "\x{441}");
220 is( getQueryFromForm,
221 "'CF.{\x{442}}' LIKE '\x{441}'",
222 "no changes, no duplicate condition with badly encoded text"
227 diag "input a condition, select (several conditions), click delete"
228 if $ENV{'TEST_VERBOSE'};
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' )");
235 is( getQueryFromForm,
236 "( Status = 'new' OR Status = 'open' )",
239 $agent->select("clauses", [qw(0 1 2)]);
240 $agent->field( ValueOfid => 10 );
241 $agent->click("DeleteClause");
243 is( getQueryFromForm,
245 "replaced query successfuly"
249 diag "send query with not quoted negative number";
251 my $response = $agent->get($url."Search/Build.html?Query=Priority%20>%20-2");
252 ok( $response->is_success, "Fetched " . $url."Search/Build.html" );
254 is( getQueryFromForm,
260 diag "click advanced, enter an invalid SQL IS restriction, apply and check that we corrected it";
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'");
267 is( getQueryFromForm($agent),
268 "Requestor.EmailAddress IS NULL",
269 "foobar is replaced by NULL"
273 diag "click advanced, enter an invalid SQL IS NOT restriction, apply and check that we corrected it";
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'");
280 is( getQueryFromForm($agent),
281 "Requestor.EmailAddress IS NOT NULL",
282 "foobar is replaced by NULL"