4 use Test::More tests => 39;
5 use Test::WWW::Mechanize;
6 use HTTP::Request::Common;
11 my $cookie_jar = HTTP::Cookies->new;
12 my $agent = Test::WWW::Mechanize->new();
14 # give the agent a place to stash the cookies
16 $agent->cookie_jar($cookie_jar);
22 # create a regression queue if it doesn't exist
24 my $queue = RT::Queue->new( $RT::SystemUser );
25 $queue->Load( 'Regression' );
27 ok(1, "queue 'Regression' exists");
29 $queue->Create( Name => 'Regression' );
30 ok($queue->id, "created queue 'Regression'");
35 my $url = $RT::WebURL;
38 is ($agent->{'status'}, 200, "Loaded a page");
43 # follow the link marked "Login"
45 ok($agent->{form}->find_input('user'));
47 ok($agent->{form}->find_input('pass'));
48 ok ($agent->{'content'} =~ /username:/i);
49 $agent->field( 'user' => 'root' );
50 $agent->field( 'pass' => 'password' );
51 # the field isn't named, so we have to click link 0
53 is($agent->{'status'}, 200, "Fetched the page ok");
54 ok( $agent->{'content'} =~ /Logout/i, "Found a logout link");
58 # {{{ Query Builder tests
60 my $response = $agent->get($url."Search/Build.html");
61 ok( $response->is_success, "Fetched " . $url."Search/Build.html" );
66 ok($agent->form_name('BuildQuery'), "found the form once");
67 $agent->field("ActorField", "Owner");
68 $agent->field("ActorOp", "=");
69 $agent->field("ValueOfActor", "Nobody");
73 ok($agent->form_name('BuildQuery'), "found the form again");
74 $agent->field("QueueOp", "!=");
75 $agent->field("ValueOfQueue", "Regression");
78 ok($agent->form_name('BuildQuery'), "found the form a third time");
80 sub getQueryFromForm {
81 $agent->form_name('BuildQuery');
82 # This pulls out the "hidden input" query from the page
83 my $q = $agent->current_form->find_input("Query")->value;
90 is (getQueryFromForm, "Owner = 'Nobody' AND Queue != 'Regression'");
92 # We're going to delete the owner
94 $agent->select("clauses", ["0"] );
96 $agent->click("DeleteClause");
98 ok($agent->form_name('BuildQuery'), "found the form a fourth time");
100 is (getQueryFromForm, "Queue != 'Regression'");
102 $agent->field("AndOr", "OR");
104 $agent->select("idOp", ">");
106 $agent->field("ValueOfid" => "1234");
108 $agent->click("AddClause");
110 ok($agent->form_name('BuildQuery'), "found the form again");
112 local $TODO = "query builder incorrectly quotes numbers";
113 is(getQueryFromForm, "Queue != 'Regression' OR id > 1234", "added something as OR, and number not quoted");
116 sub selectedClauses {
117 my @clauses = grep { defined } map { $_->value } $agent->current_form->find_input("clauses");
122 is_deeply(selectedClauses, ["1"], 'the id that we just entered is still selected');
124 # Move the second one up a level
127 ok($agent->form_name('BuildQuery'), "found the form again");
128 is(getQueryFromForm, "id > 1234 OR Queue != 'Regression'", "moved up one");
130 is_deeply(selectedClauses, ["0"], 'the one we moved up is selected');
132 $agent->click("Right");
134 ok($agent->form_name('BuildQuery'), "found the form again");
135 is(getQueryFromForm, "Queue != 'Regression' OR ( id > 1234 )", "moved over to the right (and down)");
136 is_deeply(selectedClauses, ["2"], 'the one we moved right is selected');
138 $agent->select("clauses", ["1"]);
142 ok($agent->form_name('BuildQuery'), "found the form again");
143 is(getQueryFromForm, "( id > 1234 ) OR Queue != 'Regression'", "moved up");
145 $agent->select("clauses", ["0"]); # this is a null clause
147 ok($agent->form_name('BuildQuery'), "found the form again");
148 $agent->content_like(qr/error: can\S+t move up/, "i shouldn't have been able to hit up");
150 $agent->click("Left");
151 ok($agent->form_name('BuildQuery'), "found the form again");
152 $agent->content_like(qr/error: can\S+t move left/, "i shouldn't have been able to hit left");
154 $agent->select("clauses", ["1"]);
155 $agent->select("ValueOfStatus" => "stalled");
157 ok($agent->form_name('BuildQuery'), "found the form again");
158 is_deeply(selectedClauses, ["2"], 'the one we added is selected');
159 is( getQueryFromForm, "( id > 1234 AND Status = 'stalled' ) OR Queue != 'Regression'", "added new one" );
161 # click advanced, enter "C1 OR ( C2 AND C3 )", apply, aggregators should stay the same.
163 my $response = $agent->get($url."Search/Edit.html");
164 ok( $response->is_success, "Fetched /Search/Edit.html" );
165 ok($agent->form_number(3), "found the form");
166 $agent->field("Query", "Status = 'new' OR ( Status = 'open' AND Subject LIKE 'office' )");
168 is( getQueryFromForm,
169 "Status = 'new' OR ( Status = 'open' AND Subject LIKE 'office' )",
170 "no aggregators change"
174 # - new items go one level down
175 # - add items at currently selected level
176 # - if nothing is selected, add at end, one level down
179 # - error if nothing selected
180 # - same item should be selected after move
181 # - can't move left if you're at the top level
184 # - error if nothing selected
185 # - same item should be selected after move
186 # - can always move right (no max depth...should there be?)
189 # - error if nothing selected
190 # - same item should be selected after move
191 # - can't move up if you're first in the list
194 # - error if nothing selected
195 # - same item should be selected after move
196 # - can't move down if you're last in the list
199 # - error if nothing selected
200 # - change all aggregators in the grouping
201 # - don't change any others
204 # - error if nothing selected
205 # - delete currently selected item
206 # - delete all children of a grouping
207 # - if delete leaves a node with no children, delete that, too
208 # - what should be selected?
211 # - clears entire query
212 # - clears it from the session, too
216 # create a custom field with nonascii name and try to add a condition
218 my $cf = RT::CustomField->new( $RT::SystemUser );
219 $cf->LoadByName( Name => "\x{442}", Queue => 0 );
221 is($cf->Type, 'Freeform', 'loaded and type is correct');
223 my ($return, $msg) = $cf->Create(
228 ok($return, 'created CF') or diag "error: $msg";
231 my $response = $agent->get($url."Search/Build.html?NewQuery=1");
232 ok( $response->is_success, "Fetched " . $url."Search/Build.html" );
234 ok($agent->form_name('BuildQuery'), "found the form once");
235 $agent->field("ValueOf'CF.{\321\202}'", "\321\201");
237 is( getQueryFromForm,
238 "'CF.{\321\202}' LIKE '\321\201'",
239 "no changes, no duplicate condition with badly encoded text"