6 use Test::More tests => 39;
7 use Test::WWW::Mechanize;
8 use HTTP::Request::Common;
13 my $cookie_jar = HTTP::Cookies->new;
14 my $agent = Test::WWW::Mechanize->new();
16 # give the agent a place to stash the cookies
18 $agent->cookie_jar($cookie_jar);
24 # create a regression queue if it doesn't exist
26 my $queue = RT::Queue->new( $RT::SystemUser );
27 $queue->Load( 'Regression' );
29 ok(1, "queue 'Regression' exists - #". $queue->id );
31 $queue->Create( Name => 'Regression' );
32 ok($queue->id, "created queue 'Regression'");
37 my $url = $RT::WebURL;
40 is ($agent->{'status'}, 200, "Loaded a page");
45 # follow the link marked "Login"
47 ok($agent->{form}->find_input('user'));
49 ok($agent->{form}->find_input('pass'));
50 ok ($agent->{'content'} =~ /username:/i);
51 $agent->field( 'user' => 'root' );
52 $agent->field( 'pass' => 'password' );
53 # the field isn't named, so we have to click link 0
55 is($agent->{'status'}, 200, "Fetched the page ok");
56 ok( $agent->{'content'} =~ /Logout/i, "Found a logout link");
60 # {{{ Query Builder tests
62 my $response = $agent->get($url."Search/Build.html");
63 ok( $response->is_success, "Fetched " . $url."Search/Build.html" );
68 ok($agent->form_name('BuildQuery'), "found the form once");
69 $agent->field("ActorField", "Owner");
70 $agent->field("ActorOp", "=");
71 $agent->field("ValueOfActor", "Nobody");
75 ok($agent->form_name('BuildQuery'), "found the form again");
76 $agent->field("QueueOp", "!=");
77 $agent->field("ValueOfQueue", "Regression");
80 ok($agent->form_name('BuildQuery'), "found the form a third time");
82 sub getQueryFromForm {
83 $agent->form_name('BuildQuery');
84 # This pulls out the "hidden input" query from the page
85 my $q = $agent->current_form->find_input("Query")->value;
92 is (getQueryFromForm, "Owner = 'Nobody' AND Queue != 'Regression'");
94 # We're going to delete the owner
96 $agent->select("clauses", ["0"] );
98 $agent->click("DeleteClause");
100 ok($agent->form_name('BuildQuery'), "found the form a fourth time");
102 is (getQueryFromForm, "Queue != 'Regression'");
104 $agent->field("AndOr", "OR");
106 $agent->select("idOp", ">");
108 $agent->field("ValueOfid" => "1234");
110 $agent->click("AddClause");
112 ok($agent->form_name('BuildQuery'), "found the form again");
114 local $TODO = "query builder incorrectly quotes numbers";
115 is(getQueryFromForm, "Queue != 'Regression' OR id > 1234", "added something as OR, and number not quoted");
118 sub selectedClauses {
119 my @clauses = grep { defined } map { $_->value } $agent->current_form->find_input("clauses");
124 is_deeply(selectedClauses, ["1"], 'the id that we just entered is still selected');
126 # Move the second one up a level
129 ok($agent->form_name('BuildQuery'), "found the form again");
130 is(getQueryFromForm, "id > 1234 OR Queue != 'Regression'", "moved up one");
132 is_deeply(selectedClauses, ["0"], 'the one we moved up is selected');
134 $agent->click("Right");
136 ok($agent->form_name('BuildQuery'), "found the form again");
137 is(getQueryFromForm, "Queue != 'Regression' OR ( id > 1234 )", "moved over to the right (and down)");
138 is_deeply(selectedClauses, ["2"], 'the one we moved right is selected');
140 $agent->select("clauses", ["1"]);
144 ok($agent->form_name('BuildQuery'), "found the form again");
145 is(getQueryFromForm, "( id > 1234 ) OR Queue != 'Regression'", "moved up");
147 $agent->select("clauses", ["0"]); # this is a null clause
149 ok($agent->form_name('BuildQuery'), "found the form again");
150 $agent->content_like(qr/error: can\S+t move up/, "i shouldn't have been able to hit up");
152 $agent->click("Left");
153 ok($agent->form_name('BuildQuery'), "found the form again");
154 $agent->content_like(qr/error: can\S+t move left/, "i shouldn't have been able to hit left");
156 $agent->select("clauses", ["1"]);
157 $agent->select("ValueOfStatus" => "stalled");
159 ok($agent->form_name('BuildQuery'), "found the form again");
160 is_deeply(selectedClauses, ["2"], 'the one we added is selected');
161 is( getQueryFromForm, "( id > 1234 AND Status = 'stalled' ) OR Queue != 'Regression'", "added new one" );
163 # click advanced, enter "C1 OR ( C2 AND C3 )", apply, aggregators should stay the same.
165 my $response = $agent->get($url."Search/Edit.html");
166 ok( $response->is_success, "Fetched /Search/Edit.html" );
167 ok($agent->form_number(3), "found the form");
168 $agent->field("Query", "Status = 'new' OR ( Status = 'open' AND Subject LIKE 'office' )");
170 is( getQueryFromForm,
171 "Status = 'new' OR ( Status = 'open' AND Subject LIKE 'office' )",
172 "no aggregators change"
176 # - new items go one level down
177 # - add items at currently selected level
178 # - if nothing is selected, add at end, one level down
181 # - error if nothing selected
182 # - same item should be selected after move
183 # - can't move left if you're at the top level
186 # - error if nothing selected
187 # - same item should be selected after move
188 # - can always move right (no max depth...should there be?)
191 # - error if nothing selected
192 # - same item should be selected after move
193 # - can't move up if you're first in the list
196 # - error if nothing selected
197 # - same item should be selected after move
198 # - can't move down if you're last in the list
201 # - error if nothing selected
202 # - change all aggregators in the grouping
203 # - don't change any others
206 # - error if nothing selected
207 # - delete currently selected item
208 # - delete all children of a grouping
209 # - if delete leaves a node with no children, delete that, too
210 # - what should be selected?
213 # - clears entire query
214 # - clears it from the session, too
218 # create a custom field with nonascii name and try to add a condition
220 my $cf = RT::CustomField->new( $RT::SystemUser );
221 $cf->LoadByName( Name => "\x{442}", Queue => 0 );
223 is($cf->Type, 'Freeform', 'loaded and type is correct');
225 my ($return, $msg) = $cf->Create(
230 ok($return, 'created CF') or diag "error: $msg";
233 my $response = $agent->get($url."Search/Build.html?NewQuery=1");
234 ok( $response->is_success, "Fetched " . $url."Search/Build.html" );
236 ok($agent->form_name('BuildQuery'), "found the form once");
237 $agent->field("ValueOf'CF.{\321\202}'", "\321\201");
239 is( getQueryFromForm,
240 "'CF.{\321\202}' LIKE '\321\201'",
241 "no changes, no duplicate condition with badly encoded text"