import rt 3.4.6
[freeside.git] / rt / lib / t / regression / 21query-builder.t
1 #!/usr/bin/perl
2
3 use strict;
4 use Test::More tests => 31;
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
21 # get the top page
22 my $url = $RT::WebURL;
23 $agent->get($url);
24
25 is ($agent->{'status'}, 200, "Loaded a page");
26
27
28 # {{{ test a login
29
30 # follow the link marked "Login"
31
32 ok($agent->{form}->find_input('user'));
33
34 ok($agent->{form}->find_input('pass'));
35 ok ($agent->{'content'} =~ /username:/i);
36 $agent->field( 'user' => 'root' );
37 $agent->field( 'pass' => 'password' );
38 # the field isn't named, so we have to click link 0
39 $agent->click(0);
40 is($agent->{'status'}, 200, "Fetched the page ok");
41 ok( $agent->{'content'} =~ /Logout/i, "Found a logout link");
42
43 # }}}
44
45 # {{{ Query Builder tests
46
47 my $response = $agent->get($url."Search/Build.html");
48 ok( $response->is_success, "Fetched " . $url."Search/Build.html" );
49
50 # Adding items
51
52 # set the first value
53 ok($agent->form_name('BuildQuery'), "found the form once");
54 $agent->field("ActorField", "Owner");
55 $agent->field("ActorOp", "=");
56 $agent->field("ValueOfActor", "Nobody");
57 $agent->submit();
58
59 # set the next value
60 ok($agent->form_name('BuildQuery'), "found the form again");
61 $agent->field("QueueOp", "!=");
62 $agent->field("ValueOfQueue", "Regression");
63 $agent->submit();
64
65 ok($agent->form_name('BuildQuery'), "found the form a third time");
66
67 sub getQueryFromForm {
68     # This pulls out the "hidden input" query from the page
69     my $q = $agent->current_form->find_input("Query")->value;
70     $q =~ s/^\s+//g;
71     $q =~ s/\s+$//g;
72     $q =~ s/\s+/ /g;
73     return $q;
74 }
75
76 is (getQueryFromForm, "Owner = 'Nobody' AND Queue != 'Regression'");
77
78 # We're going to delete the owner
79
80 $agent->select("clauses", ["0"] );
81
82 $agent->click("DeleteClause");
83
84 ok($agent->form_name('BuildQuery'), "found the form a fourth time");
85
86 is (getQueryFromForm, "Queue != 'Regression'");
87
88 $agent->field("AndOr", "OR");
89
90 $agent->select("idOp", ">");
91
92 $agent->field("ValueOfid" => "1234");
93
94 $agent->click("AddClause");
95
96 ok($agent->form_name('BuildQuery'), "found the form again");
97 TODO: {
98   local $TODO = "query builder incorrectly quotes numbers";
99   is(getQueryFromForm, "Queue != 'Regression' OR id > 1234", "added something as OR, and number not quoted");
100 }
101
102 sub selectedClauses {
103     my @clauses = grep { defined } map { $_->value } $agent->current_form->find_input("clauses");
104     return [ @clauses ];
105 }
106
107
108 is_deeply(selectedClauses, ["1"], 'the id that we just entered is still selected');
109
110 # Move the second one up a level
111 $agent->click("Up"); 
112
113 ok($agent->form_name('BuildQuery'), "found the form again");
114 is(getQueryFromForm, "id > 1234 OR Queue != 'Regression'", "moved up one");
115
116 is_deeply(selectedClauses, ["0"], 'the one we moved up is selected');
117
118 $agent->click("Right");
119
120 ok($agent->form_name('BuildQuery'), "found the form again");
121 is(getQueryFromForm, "Queue != 'Regression' OR ( id > 1234 )", "moved over to the right (and down)");
122 is_deeply(selectedClauses, ["2"], 'the one we moved right is selected');
123
124 $agent->select("clauses", ["1"]);
125
126 $agent->click("Up");
127
128 ok($agent->form_name('BuildQuery'), "found the form again");
129 TODO: {
130   local $TODO = "query builder incorrectly changes OR to AND";
131   is(getQueryFromForm, "( id > 1234 ) OR Queue != 'Regression'", "moved up");
132 }
133
134 $agent->select("clauses", ["0"]); # this is a null clause
135
136 $agent->click("Up");
137
138 ok($agent->form_name('BuildQuery'), "found the form again");
139
140 $agent->content_like(qr/error: can\S+t move up/, "i shouldn't have been able to hit up");
141
142 $agent->click("Left");
143
144 ok($agent->form_name('BuildQuery'), "found the form again");
145
146 $agent->content_like(qr/error: can\S+t move left/, "i shouldn't have been able to hit left");
147
148 $agent->select("clauses", ["1"]);
149 $agent->select("ValueOfStatus" => "stalled");
150
151 $agent->submit;
152 ok($agent->form_name('BuildQuery'), "found the form again");
153 is_deeply(selectedClauses, ["2"], 'the one we added is selected');
154 TODO: {
155   local $TODO = "query builder incorrectly changes OR to AND";
156   is(getQueryFromForm, "( id > 1234 AND Status = 'stalled' ) OR Queue != 'Regression'", "added new one");
157 }
158
159
160
161 # - new items go one level down
162 # - add items at currently selected level
163 # - if nothing is selected, add at end, one level down
164 #
165 # move left
166 # - error if nothing selected
167 # - same item should be selected after move
168 # - can't move left if you're at the top level
169 #
170 # move right
171 # - error if nothing selected
172 # - same item should be selected after move
173 # - can always move right (no max depth...should there be?)
174 #
175 # move up
176 # - error if nothing selected
177 # - same item should be selected after move
178 # - can't move up if you're first in the list
179 #
180 # move down
181 # - error if nothing selected
182 # - same item should be selected after move
183 # - can't move down if you're last in the list
184 #
185 # toggle
186 # - error if nothing selected
187 # - change all aggregators in the grouping
188 # - don't change any others
189 #
190 # delete
191 # - error if nothing selected
192 # - delete currently selected item
193 # - delete all children of a grouping
194 # - if delete leaves a node with no children, delete that, too
195 # - what should be selected?
196 #
197 # Clear
198 # - clears entire query
199 # - clears it from the session, too
200
201 # }}}
202
203
204 1;