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