import rt 3.8.11
[freeside.git] / rt / lib / t / regression / 21query-builder.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 39;
7 use Test::WWW::Mechanize;
8 use HTTP::Request::Common;
9 use HTTP::Cookies;
10 use LWP;
11 use Encode;
12
13 my $cookie_jar = HTTP::Cookies->new;
14 my $agent = Test::WWW::Mechanize->new();
15
16 # give the agent a place to stash the cookies
17
18 $agent->cookie_jar($cookie_jar);
19
20 use RT;
21 RT::LoadConfig();
22 RT::Init();
23
24 # create a regression queue if it doesn't exist
25 {
26     my $queue = RT::Queue->new( $RT::SystemUser );
27     $queue->Load( 'Regression' );
28     if ( $queue->id ) {
29         ok(1, "queue 'Regression' exists - #". $queue->id );
30     } else {
31         $queue->Create( Name => 'Regression' );
32         ok($queue->id, "created queue 'Regression'");
33     }
34 }
35
36 # get the top page
37 my $url = $RT::WebURL;
38 $agent->get($url);
39
40 is ($agent->{'status'}, 200, "Loaded a page");
41
42
43 # {{{ test a login
44
45 # follow the link marked "Login"
46
47 ok($agent->{form}->find_input('user'));
48
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
54 $agent->click(0);
55 is($agent->{'status'}, 200, "Fetched the page ok");
56 ok( $agent->{'content'} =~ /Logout/i, "Found a logout link");
57
58 # }}}
59
60 # {{{ Query Builder tests
61
62 my $response = $agent->get($url."Search/Build.html");
63 ok( $response->is_success, "Fetched " . $url."Search/Build.html" );
64
65 # Adding items
66
67 # set the first value
68 ok($agent->form_name('BuildQuery'), "found the form once");
69 $agent->field("ActorField", "Owner");
70 $agent->field("ActorOp", "=");
71 $agent->field("ValueOfActor", "Nobody");
72 $agent->submit();
73
74 # set the next value
75 ok($agent->form_name('BuildQuery'), "found the form again");
76 $agent->field("QueueOp", "!=");
77 $agent->field("ValueOfQueue", "Regression");
78 $agent->submit();
79
80 ok($agent->form_name('BuildQuery'), "found the form a third time");
81
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;
86     $q =~ s/^\s+//g;
87     $q =~ s/\s+$//g;
88     $q =~ s/\s+/ /g;
89     return $q;
90 }
91
92 is (getQueryFromForm, "Owner = 'Nobody' AND Queue != 'Regression'");
93
94 # We're going to delete the owner
95
96 $agent->select("clauses", ["0"] );
97
98 $agent->click("DeleteClause");
99
100 ok($agent->form_name('BuildQuery'), "found the form a fourth time");
101
102 is (getQueryFromForm, "Queue != 'Regression'");
103
104 $agent->field("AndOr", "OR");
105
106 $agent->select("idOp", ">");
107
108 $agent->field("ValueOfid" => "1234");
109
110 $agent->click("AddClause");
111
112 ok($agent->form_name('BuildQuery'), "found the form again");
113 TODO: {
114   local $TODO = "query builder incorrectly quotes numbers";
115   is(getQueryFromForm, "Queue != 'Regression' OR id > 1234", "added something as OR, and number not quoted");
116 }
117
118 sub selectedClauses {
119     my @clauses = grep { defined } map { $_->value } $agent->current_form->find_input("clauses");
120     return [ @clauses ];
121 }
122
123
124 is_deeply(selectedClauses, ["1"], 'the id that we just entered is still selected');
125
126 # Move the second one up a level
127 $agent->click("Up"); 
128
129 ok($agent->form_name('BuildQuery'), "found the form again");
130 is(getQueryFromForm, "id > 1234 OR Queue != 'Regression'", "moved up one");
131
132 is_deeply(selectedClauses, ["0"], 'the one we moved up is selected');
133
134 $agent->click("Right");
135
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');
139
140 $agent->select("clauses", ["1"]);
141
142 $agent->click("Up");
143
144 ok($agent->form_name('BuildQuery'), "found the form again");
145 is(getQueryFromForm, "( id > 1234 ) OR Queue != 'Regression'", "moved up");
146
147 $agent->select("clauses", ["0"]); # this is a null clause
148 $agent->click("Up");
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");
151
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");
155
156 $agent->select("clauses", ["1"]);
157 $agent->select("ValueOfStatus" => "stalled");
158 $agent->submit;
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" );
162
163 # click advanced, enter "C1 OR ( C2 AND C3 )", apply, aggregators should stay the same.
164 {
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' )");
169     $agent->submit;
170     is( getQueryFromForm,
171         "Status = 'new' OR ( Status = 'open' AND Subject LIKE 'office' )",
172         "no aggregators change"
173     );
174 }
175
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
179 #
180 # move left
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
184 #
185 # move right
186 # - error if nothing selected
187 # - same item should be selected after move
188 # - can always move right (no max depth...should there be?)
189 #
190 # move up
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
194 #
195 # move down
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
199 #
200 # toggle
201 # - error if nothing selected
202 # - change all aggregators in the grouping
203 # - don't change any others
204 #
205 # delete
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?
211 #
212 # Clear
213 # - clears entire query
214 # - clears it from the session, too
215
216 # }}}
217
218 # create a custom field with nonascii name and try to add a condition
219 {
220     my $cf = RT::CustomField->new( $RT::SystemUser );
221     $cf->LoadByName( Name => "\x{442}", Queue => 0 );
222     if ( $cf->id ) {
223         is($cf->Type, 'Freeform', 'loaded and type is correct');
224     } else {
225         my ($return, $msg) = $cf->Create(
226             Name => "\x{442}",
227             Queue => 0,
228             Type => 'Freeform',
229         );
230         ok($return, 'created CF') or diag "error: $msg";
231     }
232
233     my $response = $agent->get($url."Search/Build.html?NewQuery=1");
234     ok( $response->is_success, "Fetched " . $url."Search/Build.html" );
235
236     ok($agent->form_name('BuildQuery'), "found the form once");
237     $agent->field("ValueOf'CF.{\321\202}'", "\321\201");
238     $agent->submit();
239     is( getQueryFromForm,
240         "'CF.{\321\202}' LIKE '\321\201'",
241         "no changes, no duplicate condition with badly encoded text"
242     );
243
244         $cf->delete();
245 }
246
247 1;