@@ -1,479 +1,478 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class IssueQuery < Query |
|
19 | 19 | |
|
20 | 20 | self.queried_class = Issue |
|
21 | 21 | |
|
22 | 22 | self.available_columns = [ |
|
23 | 23 | QueryColumn.new(:id, :sortable => "#{Issue.table_name}.id", :default_order => 'desc', :caption => '#', :frozen => true), |
|
24 | 24 | QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true), |
|
25 | 25 | QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true), |
|
26 | 26 | QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue), |
|
27 | 27 | QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true), |
|
28 | 28 | QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true), |
|
29 | 29 | QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"), |
|
30 | 30 | QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true), |
|
31 | 31 | QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true), |
|
32 | 32 | QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'), |
|
33 | 33 | QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true), |
|
34 | 34 | QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true), |
|
35 | 35 | QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"), |
|
36 | 36 | QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"), |
|
37 | 37 | QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"), |
|
38 | 38 | QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true), |
|
39 | 39 | QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'), |
|
40 | 40 | QueryColumn.new(:closed_on, :sortable => "#{Issue.table_name}.closed_on", :default_order => 'desc'), |
|
41 | 41 | QueryColumn.new(:relations, :caption => :label_related_issues), |
|
42 | 42 | QueryColumn.new(:description, :inline => false) |
|
43 | 43 | ] |
|
44 | 44 | |
|
45 | 45 | scope :visible, lambda {|*args| |
|
46 | 46 | user = args.shift || User.current |
|
47 | 47 | base = Project.allowed_to_condition(user, :view_issues, *args) |
|
48 | 48 | scope = includes(:project).where("#{table_name}.project_id IS NULL OR (#{base})") |
|
49 | 49 | |
|
50 | 50 | if user.admin? |
|
51 | 51 | scope.where("#{table_name}.visibility <> ? OR #{table_name}.user_id = ?", VISIBILITY_PRIVATE, user.id) |
|
52 | 52 | elsif user.memberships.any? |
|
53 | 53 | scope.where("#{table_name}.visibility = ?" + |
|
54 | 54 | " OR (#{table_name}.visibility = ? AND #{table_name}.id IN (" + |
|
55 | 55 | "SELECT DISTINCT q.id FROM #{table_name} q" + |
|
56 | 56 | " INNER JOIN #{table_name_prefix}queries_roles#{table_name_suffix} qr on qr.query_id = q.id" + |
|
57 | 57 | " INNER JOIN #{MemberRole.table_name} mr ON mr.role_id = qr.role_id" + |
|
58 | 58 | " INNER JOIN #{Member.table_name} m ON m.id = mr.member_id AND m.user_id = ?" + |
|
59 | 59 | " WHERE q.project_id IS NULL OR q.project_id = m.project_id))" + |
|
60 | 60 | " OR #{table_name}.user_id = ?", |
|
61 | 61 | VISIBILITY_PUBLIC, VISIBILITY_ROLES, user.id, user.id) |
|
62 | 62 | elsif user.logged? |
|
63 | 63 | scope.where("#{table_name}.visibility = ? OR #{table_name}.user_id = ?", VISIBILITY_PUBLIC, user.id) |
|
64 | 64 | else |
|
65 | 65 | scope.where("#{table_name}.visibility = ?", VISIBILITY_PUBLIC) |
|
66 | 66 | end |
|
67 | 67 | } |
|
68 | 68 | |
|
69 | 69 | def initialize(attributes=nil, *args) |
|
70 | 70 | super attributes |
|
71 | 71 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } |
|
72 | 72 | end |
|
73 | 73 | |
|
74 | 74 | # Returns true if the query is visible to +user+ or the current user. |
|
75 | 75 | def visible?(user=User.current) |
|
76 | 76 | return true if user.admin? |
|
77 | 77 | return false unless project.nil? || user.allowed_to?(:view_issues, project) |
|
78 | 78 | case visibility |
|
79 | 79 | when VISIBILITY_PUBLIC |
|
80 | 80 | true |
|
81 | 81 | when VISIBILITY_ROLES |
|
82 | 82 | if project |
|
83 | 83 | (user.roles_for_project(project) & roles).any? |
|
84 | 84 | else |
|
85 | 85 | Member.where(:user_id => user.id).joins(:roles).where(:member_roles => {:role_id => roles.map(&:id)}).any? |
|
86 | 86 | end |
|
87 | 87 | else |
|
88 | 88 | user == self.user |
|
89 | 89 | end |
|
90 | 90 | end |
|
91 | 91 | |
|
92 | 92 | def is_private? |
|
93 | 93 | visibility == VISIBILITY_PRIVATE |
|
94 | 94 | end |
|
95 | 95 | |
|
96 | 96 | def is_public? |
|
97 | 97 | !is_private? |
|
98 | 98 | end |
|
99 | 99 | |
|
100 | 100 | def draw_relations |
|
101 | 101 | r = options[:draw_relations] |
|
102 | 102 | r.nil? || r == '1' |
|
103 | 103 | end |
|
104 | 104 | |
|
105 | 105 | def draw_relations=(arg) |
|
106 | 106 | options[:draw_relations] = (arg == '0' ? '0' : nil) |
|
107 | 107 | end |
|
108 | 108 | |
|
109 | 109 | def draw_progress_line |
|
110 | 110 | r = options[:draw_progress_line] |
|
111 | 111 | r == '1' |
|
112 | 112 | end |
|
113 | 113 | |
|
114 | 114 | def draw_progress_line=(arg) |
|
115 | 115 | options[:draw_progress_line] = (arg == '1' ? '1' : nil) |
|
116 | 116 | end |
|
117 | 117 | |
|
118 | 118 | def build_from_params(params) |
|
119 | 119 | super |
|
120 | 120 | self.draw_relations = params[:draw_relations] || (params[:query] && params[:query][:draw_relations]) |
|
121 | 121 | self.draw_progress_line = params[:draw_progress_line] || (params[:query] && params[:query][:draw_progress_line]) |
|
122 | 122 | self |
|
123 | 123 | end |
|
124 | 124 | |
|
125 | 125 | def initialize_available_filters |
|
126 | 126 | principals = [] |
|
127 | 127 | subprojects = [] |
|
128 | 128 | versions = [] |
|
129 | 129 | categories = [] |
|
130 | 130 | issue_custom_fields = [] |
|
131 | 131 | |
|
132 | 132 | if project |
|
133 | 133 | principals += project.principals.sort |
|
134 | 134 | unless project.leaf? |
|
135 | 135 | subprojects = project.descendants.visible.all |
|
136 | 136 | principals += Principal.member_of(subprojects) |
|
137 | 137 | end |
|
138 | 138 | versions = project.shared_versions.all |
|
139 | 139 | categories = project.issue_categories.all |
|
140 | 140 | issue_custom_fields = project.all_issue_custom_fields |
|
141 | 141 | else |
|
142 | 142 | if all_projects.any? |
|
143 | 143 | principals += Principal.member_of(all_projects) |
|
144 | 144 | end |
|
145 | 145 | versions = Version.visible.find_all_by_sharing('system') |
|
146 | 146 | issue_custom_fields = IssueCustomField.where(:is_for_all => true) |
|
147 | 147 | end |
|
148 | 148 | principals.uniq! |
|
149 | 149 | principals.sort! |
|
150 | 150 | users = principals.select {|p| p.is_a?(User)} |
|
151 | 151 | |
|
152 | 152 | add_available_filter "status_id", |
|
153 | 153 | :type => :list_status, :values => IssueStatus.sorted.all.collect{|s| [s.name, s.id.to_s] } |
|
154 | 154 | |
|
155 | 155 | if project.nil? |
|
156 | 156 | project_values = [] |
|
157 | 157 | if User.current.logged? && User.current.memberships.any? |
|
158 | 158 | project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"] |
|
159 | 159 | end |
|
160 | 160 | project_values += all_projects_values |
|
161 | 161 | add_available_filter("project_id", |
|
162 | 162 | :type => :list, :values => project_values |
|
163 | 163 | ) unless project_values.empty? |
|
164 | 164 | end |
|
165 | 165 | |
|
166 | 166 | add_available_filter "tracker_id", |
|
167 | 167 | :type => :list, :values => trackers.collect{|s| [s.name, s.id.to_s] } |
|
168 | 168 | add_available_filter "priority_id", |
|
169 | 169 | :type => :list, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] } |
|
170 | 170 | |
|
171 | 171 | author_values = [] |
|
172 | 172 | author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
173 | 173 | author_values += users.collect{|s| [s.name, s.id.to_s] } |
|
174 | 174 | add_available_filter("author_id", |
|
175 | 175 | :type => :list, :values => author_values |
|
176 | 176 | ) unless author_values.empty? |
|
177 | 177 | |
|
178 | 178 | assigned_to_values = [] |
|
179 | 179 | assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
180 | 180 | assigned_to_values += (Setting.issue_group_assignment? ? |
|
181 | 181 | principals : users).collect{|s| [s.name, s.id.to_s] } |
|
182 | 182 | add_available_filter("assigned_to_id", |
|
183 | 183 | :type => :list_optional, :values => assigned_to_values |
|
184 | 184 | ) unless assigned_to_values.empty? |
|
185 | 185 | |
|
186 | 186 | group_values = Group.all.collect {|g| [g.name, g.id.to_s] } |
|
187 | 187 | add_available_filter("member_of_group", |
|
188 | 188 | :type => :list_optional, :values => group_values |
|
189 | 189 | ) unless group_values.empty? |
|
190 | 190 | |
|
191 | 191 | role_values = Role.givable.collect {|r| [r.name, r.id.to_s] } |
|
192 | 192 | add_available_filter("assigned_to_role", |
|
193 | 193 | :type => :list_optional, :values => role_values |
|
194 | 194 | ) unless role_values.empty? |
|
195 | 195 | |
|
196 | 196 | if versions.any? |
|
197 | 197 | add_available_filter "fixed_version_id", |
|
198 | 198 | :type => :list_optional, |
|
199 | 199 | :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } |
|
200 | 200 | end |
|
201 | 201 | |
|
202 | 202 | if categories.any? |
|
203 | 203 | add_available_filter "category_id", |
|
204 | 204 | :type => :list_optional, |
|
205 | 205 | :values => categories.collect{|s| [s.name, s.id.to_s] } |
|
206 | 206 | end |
|
207 | 207 | |
|
208 | 208 | add_available_filter "subject", :type => :text |
|
209 | 209 | add_available_filter "created_on", :type => :date_past |
|
210 | 210 | add_available_filter "updated_on", :type => :date_past |
|
211 | 211 | add_available_filter "closed_on", :type => :date_past |
|
212 | 212 | add_available_filter "start_date", :type => :date |
|
213 | 213 | add_available_filter "due_date", :type => :date |
|
214 | 214 | add_available_filter "estimated_hours", :type => :float |
|
215 | 215 | add_available_filter "done_ratio", :type => :integer |
|
216 | 216 | |
|
217 | 217 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || |
|
218 | 218 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) |
|
219 | 219 | add_available_filter "is_private", |
|
220 | 220 | :type => :list, |
|
221 | 221 | :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]] |
|
222 | 222 | end |
|
223 | 223 | |
|
224 | 224 | if User.current.logged? |
|
225 | 225 | add_available_filter "watcher_id", |
|
226 | 226 | :type => :list, :values => [["<< #{l(:label_me)} >>", "me"]] |
|
227 | 227 | end |
|
228 | 228 | |
|
229 | 229 | if subprojects.any? |
|
230 | 230 | add_available_filter "subproject_id", |
|
231 | 231 | :type => :list_subprojects, |
|
232 | 232 | :values => subprojects.collect{|s| [s.name, s.id.to_s] } |
|
233 | 233 | end |
|
234 | 234 | |
|
235 | 235 | add_custom_fields_filters(issue_custom_fields) |
|
236 | 236 | |
|
237 | 237 | add_associations_custom_fields_filters :project, :author, :assigned_to, :fixed_version |
|
238 | 238 | |
|
239 | 239 | IssueRelation::TYPES.each do |relation_type, options| |
|
240 | 240 | add_available_filter relation_type, :type => :relation, :label => options[:name] |
|
241 | 241 | end |
|
242 | 242 | |
|
243 | 243 | Tracker.disabled_core_fields(trackers).each {|field| |
|
244 | 244 | delete_available_filter field |
|
245 | 245 | } |
|
246 | 246 | end |
|
247 | 247 | |
|
248 | 248 | def available_columns |
|
249 | 249 | return @available_columns if @available_columns |
|
250 | 250 | @available_columns = self.class.available_columns.dup |
|
251 | 251 | @available_columns += (project ? |
|
252 | 252 | project.all_issue_custom_fields : |
|
253 | 253 | IssueCustomField |
|
254 | 254 | ).visible.collect {|cf| QueryCustomFieldColumn.new(cf) } |
|
255 | 255 | |
|
256 | 256 | if User.current.allowed_to?(:view_time_entries, project, :global => true) |
|
257 | 257 | index = nil |
|
258 | 258 | @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours} |
|
259 | 259 | index = (index ? index + 1 : -1) |
|
260 | 260 | # insert the column after estimated_hours or at the end |
|
261 | 261 | @available_columns.insert index, QueryColumn.new(:spent_hours, |
|
262 | 262 | :sortable => "COALESCE((SELECT SUM(hours) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id), 0)", |
|
263 | 263 | :default_order => 'desc', |
|
264 | 264 | :caption => :label_spent_time |
|
265 | 265 | ) |
|
266 | 266 | end |
|
267 | 267 | |
|
268 | 268 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || |
|
269 | 269 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) |
|
270 | 270 | @available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private") |
|
271 | 271 | end |
|
272 | 272 | |
|
273 | 273 | disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')} |
|
274 | 274 | @available_columns.reject! {|column| |
|
275 | 275 | disabled_fields.include?(column.name.to_s) |
|
276 | 276 | } |
|
277 | 277 | |
|
278 | 278 | @available_columns |
|
279 | 279 | end |
|
280 | 280 | |
|
281 | 281 | def default_columns_names |
|
282 | 282 | @default_columns_names ||= begin |
|
283 | 283 | default_columns = Setting.issue_list_default_columns.map(&:to_sym) |
|
284 | 284 | |
|
285 | 285 | project.present? ? default_columns : [:project] | default_columns |
|
286 | 286 | end |
|
287 | 287 | end |
|
288 | 288 | |
|
289 | 289 | # Returns the issue count |
|
290 | 290 | def issue_count |
|
291 | 291 | Issue.visible.joins(:status, :project).where(statement).count |
|
292 | 292 | rescue ::ActiveRecord::StatementInvalid => e |
|
293 | 293 | raise StatementInvalid.new(e.message) |
|
294 | 294 | end |
|
295 | 295 | |
|
296 | 296 | # Returns the issue count by group or nil if query is not grouped |
|
297 | 297 | def issue_count_by_group |
|
298 | 298 | r = nil |
|
299 | 299 | if grouped? |
|
300 | 300 | begin |
|
301 | 301 | # Rails3 will raise an (unexpected) RecordNotFound if there's only a nil group value |
|
302 | 302 | r = Issue.visible. |
|
303 | 303 | joins(:status, :project). |
|
304 | 304 | where(statement). |
|
305 | 305 | joins(joins_for_order_statement(group_by_statement)). |
|
306 | 306 | group(group_by_statement). |
|
307 | 307 | count |
|
308 | 308 | rescue ActiveRecord::RecordNotFound |
|
309 | 309 | r = {nil => issue_count} |
|
310 | 310 | end |
|
311 | 311 | c = group_by_column |
|
312 | 312 | if c.is_a?(QueryCustomFieldColumn) |
|
313 | 313 | r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h} |
|
314 | 314 | end |
|
315 | 315 | end |
|
316 | 316 | r |
|
317 | 317 | rescue ::ActiveRecord::StatementInvalid => e |
|
318 | 318 | raise StatementInvalid.new(e.message) |
|
319 | 319 | end |
|
320 | 320 | |
|
321 | 321 | # Returns the issues |
|
322 | 322 | # Valid options are :order, :offset, :limit, :include, :conditions |
|
323 | 323 | def issues(options={}) |
|
324 | 324 | order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) |
|
325 | 325 | |
|
326 | 326 | issues = Issue.visible. |
|
327 | 327 | joins(:status, :project). |
|
328 | 328 | where(statement). |
|
329 | 329 | includes(([:status, :project] + (options[:include] || [])).uniq). |
|
330 | 330 | where(options[:conditions]). |
|
331 | 331 | order(order_option). |
|
332 | 332 | joins(joins_for_order_statement(order_option.join(','))). |
|
333 | 333 | limit(options[:limit]). |
|
334 | 334 | offset(options[:offset]). |
|
335 | 335 | all |
|
336 | 336 | |
|
337 | 337 | if has_column?(:spent_hours) |
|
338 | 338 | Issue.load_visible_spent_hours(issues) |
|
339 | 339 | end |
|
340 | 340 | if has_column?(:relations) |
|
341 | 341 | Issue.load_visible_relations(issues) |
|
342 | 342 | end |
|
343 | 343 | issues |
|
344 | 344 | rescue ::ActiveRecord::StatementInvalid => e |
|
345 | 345 | raise StatementInvalid.new(e.message) |
|
346 | 346 | end |
|
347 | 347 | |
|
348 | 348 | # Returns the issues ids |
|
349 | 349 | def issue_ids(options={}) |
|
350 | 350 | order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) |
|
351 | 351 | |
|
352 | 352 | Issue.visible. |
|
353 | 353 | joins(:status, :project). |
|
354 | 354 | where(statement). |
|
355 | 355 | includes(([:status, :project] + (options[:include] || [])).uniq). |
|
356 | 356 | where(options[:conditions]). |
|
357 | 357 | order(order_option). |
|
358 | 358 | joins(joins_for_order_statement(order_option.join(','))). |
|
359 | 359 | limit(options[:limit]). |
|
360 | 360 | offset(options[:offset]). |
|
361 | 361 | find_ids |
|
362 | 362 | rescue ::ActiveRecord::StatementInvalid => e |
|
363 | 363 | raise StatementInvalid.new(e.message) |
|
364 | 364 | end |
|
365 | 365 | |
|
366 | 366 | # Returns the journals |
|
367 | 367 | # Valid options are :order, :offset, :limit |
|
368 | 368 | def journals(options={}) |
|
369 | 369 | Journal.visible. |
|
370 | 370 | joins(:issue => [:project, :status]). |
|
371 | 371 | where(statement). |
|
372 | 372 | order(options[:order]). |
|
373 | 373 | limit(options[:limit]). |
|
374 | 374 | offset(options[:offset]). |
|
375 | 375 | preload(:details, :user, {:issue => [:project, :author, :tracker, :status]}). |
|
376 | 376 | all |
|
377 | 377 | rescue ::ActiveRecord::StatementInvalid => e |
|
378 | 378 | raise StatementInvalid.new(e.message) |
|
379 | 379 | end |
|
380 | 380 | |
|
381 | 381 | # Returns the versions |
|
382 | 382 | # Valid options are :conditions |
|
383 | 383 | def versions(options={}) |
|
384 | 384 | Version.visible. |
|
385 | 385 | where(project_statement). |
|
386 | 386 | where(options[:conditions]). |
|
387 | 387 | includes(:project). |
|
388 | 388 | all |
|
389 | 389 | rescue ::ActiveRecord::StatementInvalid => e |
|
390 | 390 | raise StatementInvalid.new(e.message) |
|
391 | 391 | end |
|
392 | 392 | |
|
393 | 393 | def sql_for_watcher_id_field(field, operator, value) |
|
394 | 394 | db_table = Watcher.table_name |
|
395 | 395 | "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " + |
|
396 | 396 | sql_for_field(field, '=', value, db_table, 'user_id') + ')' |
|
397 | 397 | end |
|
398 | 398 | |
|
399 | 399 | def sql_for_member_of_group_field(field, operator, value) |
|
400 | 400 | if operator == '*' # Any group |
|
401 | 401 | groups = Group.all |
|
402 | 402 | operator = '=' # Override the operator since we want to find by assigned_to |
|
403 | 403 | elsif operator == "!*" |
|
404 | 404 | groups = Group.all |
|
405 | 405 | operator = '!' # Override the operator since we want to find by assigned_to |
|
406 | 406 | else |
|
407 | 407 | groups = Group.find_all_by_id(value) |
|
408 | 408 | end |
|
409 | 409 | groups ||= [] |
|
410 | 410 | |
|
411 | 411 | members_of_groups = groups.inject([]) {|user_ids, group| |
|
412 | 412 | user_ids + group.user_ids + [group.id] |
|
413 | 413 | }.uniq.compact.sort.collect(&:to_s) |
|
414 | 414 | |
|
415 | 415 | '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')' |
|
416 | 416 | end |
|
417 | 417 | |
|
418 | 418 | def sql_for_assigned_to_role_field(field, operator, value) |
|
419 | 419 | case operator |
|
420 | 420 | when "*", "!*" # Member / Not member |
|
421 | 421 | sw = operator == "!*" ? 'NOT' : '' |
|
422 | 422 | nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
423 | 423 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" + |
|
424 | 424 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))" |
|
425 | 425 | when "=", "!" |
|
426 | 426 | role_cond = value.any? ? |
|
427 | 427 | "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" : |
|
428 | 428 | "1=0" |
|
429 | 429 | |
|
430 | 430 | sw = operator == "!" ? 'NOT' : '' |
|
431 | 431 | nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
432 | 432 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" + |
|
433 | 433 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))" |
|
434 | 434 | end |
|
435 | 435 | end |
|
436 | 436 | |
|
437 | 437 | def sql_for_is_private_field(field, operator, value) |
|
438 | 438 | op = (operator == "=" ? 'IN' : 'NOT IN') |
|
439 | 439 | va = value.map {|v| v == '0' ? connection.quoted_false : connection.quoted_true}.uniq.join(',') |
|
440 | 440 | |
|
441 | 441 | "#{Issue.table_name}.is_private #{op} (#{va})" |
|
442 | 442 | end |
|
443 | 443 | |
|
444 | 444 | def sql_for_relations(field, operator, value, options={}) |
|
445 | 445 | relation_options = IssueRelation::TYPES[field] |
|
446 | 446 | return relation_options unless relation_options |
|
447 | 447 | |
|
448 | 448 | relation_type = field |
|
449 | 449 | join_column, target_join_column = "issue_from_id", "issue_to_id" |
|
450 | 450 | if relation_options[:reverse] || options[:reverse] |
|
451 | 451 | relation_type = relation_options[:reverse] || relation_type |
|
452 | 452 | join_column, target_join_column = target_join_column, join_column |
|
453 | 453 | end |
|
454 | 454 | |
|
455 | 455 | sql = case operator |
|
456 | 456 | when "*", "!*" |
|
457 | 457 | op = (operator == "*" ? 'IN' : 'NOT IN') |
|
458 | 458 | "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}')" |
|
459 | 459 | when "=", "!" |
|
460 | 460 | op = (operator == "=" ? 'IN' : 'NOT IN') |
|
461 | 461 | "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = #{value.first.to_i})" |
|
462 | 462 | when "=p", "=!p", "!p" |
|
463 | 463 | op = (operator == "!p" ? 'NOT IN' : 'IN') |
|
464 | 464 | comp = (operator == "=!p" ? '<>' : '=') |
|
465 | 465 | "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name}, #{Issue.table_name} relissues WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = relissues.id AND relissues.project_id #{comp} #{value.first.to_i})" |
|
466 | 466 | end |
|
467 | 467 | |
|
468 | 468 | if relation_options[:sym] == field && !options[:reverse] |
|
469 | 469 | sqls = [sql, sql_for_relations(field, operator, value, :reverse => true)] |
|
470 | sqls.join(["!", "!*", "!p"].include?(operator) ? " AND " : " OR ") | |
|
471 | else | |
|
472 | sql | |
|
470 | sql = sqls.join(["!", "!*", "!p"].include?(operator) ? " AND " : " OR ") | |
|
473 | 471 | end |
|
472 | "(#{sql})" | |
|
474 | 473 | end |
|
475 | 474 | |
|
476 | 475 | IssueRelation::TYPES.keys.each do |relation_type| |
|
477 | 476 | alias_method "sql_for_#{relation_type}_field".to_sym, :sql_for_relations |
|
478 | 477 | end |
|
479 | 478 | end |
@@ -1,1363 +1,1378 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class QueryTest < ActiveSupport::TestCase |
|
21 | 21 | include Redmine::I18n |
|
22 | 22 | |
|
23 | 23 | fixtures :projects, :enabled_modules, :users, :members, |
|
24 | 24 | :member_roles, :roles, :trackers, :issue_statuses, |
|
25 | 25 | :issue_categories, :enumerations, :issues, |
|
26 | 26 | :watchers, :custom_fields, :custom_values, :versions, |
|
27 | 27 | :queries, |
|
28 | 28 | :projects_trackers, |
|
29 | 29 | :custom_fields_trackers |
|
30 | 30 | |
|
31 | 31 | def test_query_with_roles_visibility_should_validate_roles |
|
32 | 32 | set_language_if_valid 'en' |
|
33 | 33 | query = IssueQuery.new(:name => 'Query', :visibility => IssueQuery::VISIBILITY_ROLES) |
|
34 | 34 | assert !query.save |
|
35 | 35 | assert_include "Roles can't be blank", query.errors.full_messages |
|
36 | 36 | query.role_ids = [1, 2] |
|
37 | 37 | assert query.save |
|
38 | 38 | end |
|
39 | 39 | |
|
40 | 40 | def test_changing_roles_visibility_should_clear_roles |
|
41 | 41 | query = IssueQuery.create!(:name => 'Query', :visibility => IssueQuery::VISIBILITY_ROLES, :role_ids => [1, 2]) |
|
42 | 42 | assert_equal 2, query.roles.count |
|
43 | 43 | |
|
44 | 44 | query.visibility = IssueQuery::VISIBILITY_PUBLIC |
|
45 | 45 | query.save! |
|
46 | 46 | assert_equal 0, query.roles.count |
|
47 | 47 | end |
|
48 | 48 | |
|
49 | 49 | def test_available_filters_should_be_ordered |
|
50 | 50 | set_language_if_valid 'en' |
|
51 | 51 | query = IssueQuery.new |
|
52 | 52 | assert_equal 0, query.available_filters.keys.index('status_id') |
|
53 | 53 | expected_order = [ |
|
54 | 54 | "Status", |
|
55 | 55 | "Project", |
|
56 | 56 | "Tracker", |
|
57 | 57 | "Priority" |
|
58 | 58 | ] |
|
59 | 59 | assert_equal expected_order, |
|
60 | 60 | (query.available_filters.values.map{|v| v[:name]} & expected_order) |
|
61 | 61 | end |
|
62 | 62 | |
|
63 | 63 | def test_available_filters_with_custom_fields_should_be_ordered |
|
64 | 64 | set_language_if_valid 'en' |
|
65 | 65 | UserCustomField.create!( |
|
66 | 66 | :name => 'order test', :field_format => 'string', |
|
67 | 67 | :is_for_all => true, :is_filter => true |
|
68 | 68 | ) |
|
69 | 69 | query = IssueQuery.new |
|
70 | 70 | expected_order = [ |
|
71 | 71 | "Searchable field", |
|
72 | 72 | "Database", |
|
73 | 73 | "Project's Development status", |
|
74 | 74 | "Author's order test", |
|
75 | 75 | "Assignee's order test" |
|
76 | 76 | ] |
|
77 | 77 | assert_equal expected_order, |
|
78 | 78 | (query.available_filters.values.map{|v| v[:name]} & expected_order) |
|
79 | 79 | end |
|
80 | 80 | |
|
81 | 81 | def test_custom_fields_for_all_projects_should_be_available_in_global_queries |
|
82 | 82 | query = IssueQuery.new(:project => nil, :name => '_') |
|
83 | 83 | assert query.available_filters.has_key?('cf_1') |
|
84 | 84 | assert !query.available_filters.has_key?('cf_3') |
|
85 | 85 | end |
|
86 | 86 | |
|
87 | 87 | def test_system_shared_versions_should_be_available_in_global_queries |
|
88 | 88 | Version.find(2).update_attribute :sharing, 'system' |
|
89 | 89 | query = IssueQuery.new(:project => nil, :name => '_') |
|
90 | 90 | assert query.available_filters.has_key?('fixed_version_id') |
|
91 | 91 | assert query.available_filters['fixed_version_id'][:values].detect {|v| v.last == '2'} |
|
92 | 92 | end |
|
93 | 93 | |
|
94 | 94 | def test_project_filter_in_global_queries |
|
95 | 95 | query = IssueQuery.new(:project => nil, :name => '_') |
|
96 | 96 | project_filter = query.available_filters["project_id"] |
|
97 | 97 | assert_not_nil project_filter |
|
98 | 98 | project_ids = project_filter[:values].map{|p| p[1]} |
|
99 | 99 | assert project_ids.include?("1") #public project |
|
100 | 100 | assert !project_ids.include?("2") #private project user cannot see |
|
101 | 101 | end |
|
102 | 102 | |
|
103 | 103 | def find_issues_with_query(query) |
|
104 | 104 | Issue.includes([:assigned_to, :status, :tracker, :project, :priority]).where( |
|
105 | 105 | query.statement |
|
106 | 106 | ).all |
|
107 | 107 | end |
|
108 | 108 | |
|
109 | 109 | def assert_find_issues_with_query_is_successful(query) |
|
110 | 110 | assert_nothing_raised do |
|
111 | 111 | find_issues_with_query(query) |
|
112 | 112 | end |
|
113 | 113 | end |
|
114 | 114 | |
|
115 | 115 | def assert_query_statement_includes(query, condition) |
|
116 | 116 | assert_include condition, query.statement |
|
117 | 117 | end |
|
118 | 118 | |
|
119 | 119 | def assert_query_result(expected, query) |
|
120 | 120 | assert_nothing_raised do |
|
121 | 121 | assert_equal expected.map(&:id).sort, query.issues.map(&:id).sort |
|
122 | 122 | assert_equal expected.size, query.issue_count |
|
123 | 123 | end |
|
124 | 124 | end |
|
125 | 125 | |
|
126 | 126 | def test_query_should_allow_shared_versions_for_a_project_query |
|
127 | 127 | subproject_version = Version.find(4) |
|
128 | 128 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
129 | 129 | query.add_filter('fixed_version_id', '=', [subproject_version.id.to_s]) |
|
130 | 130 | |
|
131 | 131 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IN ('4')") |
|
132 | 132 | end |
|
133 | 133 | |
|
134 | 134 | def test_query_with_multiple_custom_fields |
|
135 | 135 | query = IssueQuery.find(1) |
|
136 | 136 | assert query.valid? |
|
137 | 137 | assert query.statement.include?("#{CustomValue.table_name}.value IN ('MySQL')") |
|
138 | 138 | issues = find_issues_with_query(query) |
|
139 | 139 | assert_equal 1, issues.length |
|
140 | 140 | assert_equal Issue.find(3), issues.first |
|
141 | 141 | end |
|
142 | 142 | |
|
143 | 143 | def test_operator_none |
|
144 | 144 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
145 | 145 | query.add_filter('fixed_version_id', '!*', ['']) |
|
146 | 146 | query.add_filter('cf_1', '!*', ['']) |
|
147 | 147 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NULL") |
|
148 | 148 | assert query.statement.include?("#{CustomValue.table_name}.value IS NULL OR #{CustomValue.table_name}.value = ''") |
|
149 | 149 | find_issues_with_query(query) |
|
150 | 150 | end |
|
151 | 151 | |
|
152 | 152 | def test_operator_none_for_integer |
|
153 | 153 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
154 | 154 | query.add_filter('estimated_hours', '!*', ['']) |
|
155 | 155 | issues = find_issues_with_query(query) |
|
156 | 156 | assert !issues.empty? |
|
157 | 157 | assert issues.all? {|i| !i.estimated_hours} |
|
158 | 158 | end |
|
159 | 159 | |
|
160 | 160 | def test_operator_none_for_date |
|
161 | 161 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
162 | 162 | query.add_filter('start_date', '!*', ['']) |
|
163 | 163 | issues = find_issues_with_query(query) |
|
164 | 164 | assert !issues.empty? |
|
165 | 165 | assert issues.all? {|i| i.start_date.nil?} |
|
166 | 166 | end |
|
167 | 167 | |
|
168 | 168 | def test_operator_none_for_string_custom_field |
|
169 | 169 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
170 | 170 | query.add_filter('cf_2', '!*', ['']) |
|
171 | 171 | assert query.has_filter?('cf_2') |
|
172 | 172 | issues = find_issues_with_query(query) |
|
173 | 173 | assert !issues.empty? |
|
174 | 174 | assert issues.all? {|i| i.custom_field_value(2).blank?} |
|
175 | 175 | end |
|
176 | 176 | |
|
177 | 177 | def test_operator_all |
|
178 | 178 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
179 | 179 | query.add_filter('fixed_version_id', '*', ['']) |
|
180 | 180 | query.add_filter('cf_1', '*', ['']) |
|
181 | 181 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NOT NULL") |
|
182 | 182 | assert query.statement.include?("#{CustomValue.table_name}.value IS NOT NULL AND #{CustomValue.table_name}.value <> ''") |
|
183 | 183 | find_issues_with_query(query) |
|
184 | 184 | end |
|
185 | 185 | |
|
186 | 186 | def test_operator_all_for_date |
|
187 | 187 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
188 | 188 | query.add_filter('start_date', '*', ['']) |
|
189 | 189 | issues = find_issues_with_query(query) |
|
190 | 190 | assert !issues.empty? |
|
191 | 191 | assert issues.all? {|i| i.start_date.present?} |
|
192 | 192 | end |
|
193 | 193 | |
|
194 | 194 | def test_operator_all_for_string_custom_field |
|
195 | 195 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
196 | 196 | query.add_filter('cf_2', '*', ['']) |
|
197 | 197 | assert query.has_filter?('cf_2') |
|
198 | 198 | issues = find_issues_with_query(query) |
|
199 | 199 | assert !issues.empty? |
|
200 | 200 | assert issues.all? {|i| i.custom_field_value(2).present?} |
|
201 | 201 | end |
|
202 | 202 | |
|
203 | 203 | def test_numeric_filter_should_not_accept_non_numeric_values |
|
204 | 204 | query = IssueQuery.new(:name => '_') |
|
205 | 205 | query.add_filter('estimated_hours', '=', ['a']) |
|
206 | 206 | |
|
207 | 207 | assert query.has_filter?('estimated_hours') |
|
208 | 208 | assert !query.valid? |
|
209 | 209 | end |
|
210 | 210 | |
|
211 | 211 | def test_operator_is_on_float |
|
212 | 212 | Issue.update_all("estimated_hours = 171.2", "id=2") |
|
213 | 213 | |
|
214 | 214 | query = IssueQuery.new(:name => '_') |
|
215 | 215 | query.add_filter('estimated_hours', '=', ['171.20']) |
|
216 | 216 | issues = find_issues_with_query(query) |
|
217 | 217 | assert_equal 1, issues.size |
|
218 | 218 | assert_equal 2, issues.first.id |
|
219 | 219 | end |
|
220 | 220 | |
|
221 | 221 | def test_operator_is_on_integer_custom_field |
|
222 | 222 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_for_all => true, :is_filter => true) |
|
223 | 223 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7') |
|
224 | 224 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12') |
|
225 | 225 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') |
|
226 | 226 | |
|
227 | 227 | query = IssueQuery.new(:name => '_') |
|
228 | 228 | query.add_filter("cf_#{f.id}", '=', ['12']) |
|
229 | 229 | issues = find_issues_with_query(query) |
|
230 | 230 | assert_equal 1, issues.size |
|
231 | 231 | assert_equal 2, issues.first.id |
|
232 | 232 | end |
|
233 | 233 | |
|
234 | 234 | def test_operator_is_on_integer_custom_field_should_accept_negative_value |
|
235 | 235 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_for_all => true, :is_filter => true) |
|
236 | 236 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7') |
|
237 | 237 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '-12') |
|
238 | 238 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') |
|
239 | 239 | |
|
240 | 240 | query = IssueQuery.new(:name => '_') |
|
241 | 241 | query.add_filter("cf_#{f.id}", '=', ['-12']) |
|
242 | 242 | assert query.valid? |
|
243 | 243 | issues = find_issues_with_query(query) |
|
244 | 244 | assert_equal 1, issues.size |
|
245 | 245 | assert_equal 2, issues.first.id |
|
246 | 246 | end |
|
247 | 247 | |
|
248 | 248 | def test_operator_is_on_float_custom_field |
|
249 | 249 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'float', :is_filter => true, :is_for_all => true) |
|
250 | 250 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7.3') |
|
251 | 251 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12.7') |
|
252 | 252 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') |
|
253 | 253 | |
|
254 | 254 | query = IssueQuery.new(:name => '_') |
|
255 | 255 | query.add_filter("cf_#{f.id}", '=', ['12.7']) |
|
256 | 256 | issues = find_issues_with_query(query) |
|
257 | 257 | assert_equal 1, issues.size |
|
258 | 258 | assert_equal 2, issues.first.id |
|
259 | 259 | end |
|
260 | 260 | |
|
261 | 261 | def test_operator_is_on_float_custom_field_should_accept_negative_value |
|
262 | 262 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'float', :is_filter => true, :is_for_all => true) |
|
263 | 263 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7.3') |
|
264 | 264 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '-12.7') |
|
265 | 265 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') |
|
266 | 266 | |
|
267 | 267 | query = IssueQuery.new(:name => '_') |
|
268 | 268 | query.add_filter("cf_#{f.id}", '=', ['-12.7']) |
|
269 | 269 | assert query.valid? |
|
270 | 270 | issues = find_issues_with_query(query) |
|
271 | 271 | assert_equal 1, issues.size |
|
272 | 272 | assert_equal 2, issues.first.id |
|
273 | 273 | end |
|
274 | 274 | |
|
275 | 275 | def test_operator_is_on_multi_list_custom_field |
|
276 | 276 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'list', :is_filter => true, :is_for_all => true, |
|
277 | 277 | :possible_values => ['value1', 'value2', 'value3'], :multiple => true) |
|
278 | 278 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value1') |
|
279 | 279 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value2') |
|
280 | 280 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => 'value1') |
|
281 | 281 | |
|
282 | 282 | query = IssueQuery.new(:name => '_') |
|
283 | 283 | query.add_filter("cf_#{f.id}", '=', ['value1']) |
|
284 | 284 | issues = find_issues_with_query(query) |
|
285 | 285 | assert_equal [1, 3], issues.map(&:id).sort |
|
286 | 286 | |
|
287 | 287 | query = IssueQuery.new(:name => '_') |
|
288 | 288 | query.add_filter("cf_#{f.id}", '=', ['value2']) |
|
289 | 289 | issues = find_issues_with_query(query) |
|
290 | 290 | assert_equal [1], issues.map(&:id).sort |
|
291 | 291 | end |
|
292 | 292 | |
|
293 | 293 | def test_operator_is_not_on_multi_list_custom_field |
|
294 | 294 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'list', :is_filter => true, :is_for_all => true, |
|
295 | 295 | :possible_values => ['value1', 'value2', 'value3'], :multiple => true) |
|
296 | 296 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value1') |
|
297 | 297 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value2') |
|
298 | 298 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => 'value1') |
|
299 | 299 | |
|
300 | 300 | query = IssueQuery.new(:name => '_') |
|
301 | 301 | query.add_filter("cf_#{f.id}", '!', ['value1']) |
|
302 | 302 | issues = find_issues_with_query(query) |
|
303 | 303 | assert !issues.map(&:id).include?(1) |
|
304 | 304 | assert !issues.map(&:id).include?(3) |
|
305 | 305 | |
|
306 | 306 | query = IssueQuery.new(:name => '_') |
|
307 | 307 | query.add_filter("cf_#{f.id}", '!', ['value2']) |
|
308 | 308 | issues = find_issues_with_query(query) |
|
309 | 309 | assert !issues.map(&:id).include?(1) |
|
310 | 310 | assert issues.map(&:id).include?(3) |
|
311 | 311 | end |
|
312 | 312 | |
|
313 | 313 | def test_operator_is_on_is_private_field |
|
314 | 314 | # is_private filter only available for those who can set issues private |
|
315 | 315 | User.current = User.find(2) |
|
316 | 316 | |
|
317 | 317 | query = IssueQuery.new(:name => '_') |
|
318 | 318 | assert query.available_filters.key?('is_private') |
|
319 | 319 | |
|
320 | 320 | query.add_filter("is_private", '=', ['1']) |
|
321 | 321 | issues = find_issues_with_query(query) |
|
322 | 322 | assert issues.any? |
|
323 | 323 | assert_nil issues.detect {|issue| !issue.is_private?} |
|
324 | 324 | ensure |
|
325 | 325 | User.current = nil |
|
326 | 326 | end |
|
327 | 327 | |
|
328 | 328 | def test_operator_is_not_on_is_private_field |
|
329 | 329 | # is_private filter only available for those who can set issues private |
|
330 | 330 | User.current = User.find(2) |
|
331 | 331 | |
|
332 | 332 | query = IssueQuery.new(:name => '_') |
|
333 | 333 | assert query.available_filters.key?('is_private') |
|
334 | 334 | |
|
335 | 335 | query.add_filter("is_private", '!', ['1']) |
|
336 | 336 | issues = find_issues_with_query(query) |
|
337 | 337 | assert issues.any? |
|
338 | 338 | assert_nil issues.detect {|issue| issue.is_private?} |
|
339 | 339 | ensure |
|
340 | 340 | User.current = nil |
|
341 | 341 | end |
|
342 | 342 | |
|
343 | 343 | def test_operator_greater_than |
|
344 | 344 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
345 | 345 | query.add_filter('done_ratio', '>=', ['40']) |
|
346 | 346 | assert query.statement.include?("#{Issue.table_name}.done_ratio >= 40.0") |
|
347 | 347 | find_issues_with_query(query) |
|
348 | 348 | end |
|
349 | 349 | |
|
350 | 350 | def test_operator_greater_than_a_float |
|
351 | 351 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
352 | 352 | query.add_filter('estimated_hours', '>=', ['40.5']) |
|
353 | 353 | assert query.statement.include?("#{Issue.table_name}.estimated_hours >= 40.5") |
|
354 | 354 | find_issues_with_query(query) |
|
355 | 355 | end |
|
356 | 356 | |
|
357 | 357 | def test_operator_greater_than_on_int_custom_field |
|
358 | 358 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) |
|
359 | 359 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7') |
|
360 | 360 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12') |
|
361 | 361 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') |
|
362 | 362 | |
|
363 | 363 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
364 | 364 | query.add_filter("cf_#{f.id}", '>=', ['8']) |
|
365 | 365 | issues = find_issues_with_query(query) |
|
366 | 366 | assert_equal 1, issues.size |
|
367 | 367 | assert_equal 2, issues.first.id |
|
368 | 368 | end |
|
369 | 369 | |
|
370 | 370 | def test_operator_lesser_than |
|
371 | 371 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
372 | 372 | query.add_filter('done_ratio', '<=', ['30']) |
|
373 | 373 | assert query.statement.include?("#{Issue.table_name}.done_ratio <= 30.0") |
|
374 | 374 | find_issues_with_query(query) |
|
375 | 375 | end |
|
376 | 376 | |
|
377 | 377 | def test_operator_lesser_than_on_custom_field |
|
378 | 378 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) |
|
379 | 379 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
380 | 380 | query.add_filter("cf_#{f.id}", '<=', ['30']) |
|
381 | 381 | assert_match /CAST.+ <= 30\.0/, query.statement |
|
382 | 382 | find_issues_with_query(query) |
|
383 | 383 | end |
|
384 | 384 | |
|
385 | 385 | def test_operator_lesser_than_on_date_custom_field |
|
386 | 386 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'date', :is_filter => true, :is_for_all => true) |
|
387 | 387 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '2013-04-11') |
|
388 | 388 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '2013-05-14') |
|
389 | 389 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') |
|
390 | 390 | |
|
391 | 391 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
392 | 392 | query.add_filter("cf_#{f.id}", '<=', ['2013-05-01']) |
|
393 | 393 | issue_ids = find_issues_with_query(query).map(&:id) |
|
394 | 394 | assert_include 1, issue_ids |
|
395 | 395 | assert_not_include 2, issue_ids |
|
396 | 396 | assert_not_include 3, issue_ids |
|
397 | 397 | end |
|
398 | 398 | |
|
399 | 399 | def test_operator_between |
|
400 | 400 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
401 | 401 | query.add_filter('done_ratio', '><', ['30', '40']) |
|
402 | 402 | assert_include "#{Issue.table_name}.done_ratio BETWEEN 30.0 AND 40.0", query.statement |
|
403 | 403 | find_issues_with_query(query) |
|
404 | 404 | end |
|
405 | 405 | |
|
406 | 406 | def test_operator_between_on_custom_field |
|
407 | 407 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) |
|
408 | 408 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
409 | 409 | query.add_filter("cf_#{f.id}", '><', ['30', '40']) |
|
410 | 410 | assert_match /CAST.+ BETWEEN 30.0 AND 40.0/, query.statement |
|
411 | 411 | find_issues_with_query(query) |
|
412 | 412 | end |
|
413 | 413 | |
|
414 | 414 | def test_date_filter_should_not_accept_non_date_values |
|
415 | 415 | query = IssueQuery.new(:name => '_') |
|
416 | 416 | query.add_filter('created_on', '=', ['a']) |
|
417 | 417 | |
|
418 | 418 | assert query.has_filter?('created_on') |
|
419 | 419 | assert !query.valid? |
|
420 | 420 | end |
|
421 | 421 | |
|
422 | 422 | def test_date_filter_should_not_accept_invalid_date_values |
|
423 | 423 | query = IssueQuery.new(:name => '_') |
|
424 | 424 | query.add_filter('created_on', '=', ['2011-01-34']) |
|
425 | 425 | |
|
426 | 426 | assert query.has_filter?('created_on') |
|
427 | 427 | assert !query.valid? |
|
428 | 428 | end |
|
429 | 429 | |
|
430 | 430 | def test_relative_date_filter_should_not_accept_non_integer_values |
|
431 | 431 | query = IssueQuery.new(:name => '_') |
|
432 | 432 | query.add_filter('created_on', '>t-', ['a']) |
|
433 | 433 | |
|
434 | 434 | assert query.has_filter?('created_on') |
|
435 | 435 | assert !query.valid? |
|
436 | 436 | end |
|
437 | 437 | |
|
438 | 438 | def test_operator_date_equals |
|
439 | 439 | query = IssueQuery.new(:name => '_') |
|
440 | 440 | query.add_filter('due_date', '=', ['2011-07-10']) |
|
441 | 441 | assert_match /issues\.due_date > '2011-07-09 23:59:59(\.9+)?' AND issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement |
|
442 | 442 | find_issues_with_query(query) |
|
443 | 443 | end |
|
444 | 444 | |
|
445 | 445 | def test_operator_date_lesser_than |
|
446 | 446 | query = IssueQuery.new(:name => '_') |
|
447 | 447 | query.add_filter('due_date', '<=', ['2011-07-10']) |
|
448 | 448 | assert_match /issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement |
|
449 | 449 | find_issues_with_query(query) |
|
450 | 450 | end |
|
451 | 451 | |
|
452 | 452 | def test_operator_date_greater_than |
|
453 | 453 | query = IssueQuery.new(:name => '_') |
|
454 | 454 | query.add_filter('due_date', '>=', ['2011-07-10']) |
|
455 | 455 | assert_match /issues\.due_date > '2011-07-09 23:59:59(\.9+)?'/, query.statement |
|
456 | 456 | find_issues_with_query(query) |
|
457 | 457 | end |
|
458 | 458 | |
|
459 | 459 | def test_operator_date_between |
|
460 | 460 | query = IssueQuery.new(:name => '_') |
|
461 | 461 | query.add_filter('due_date', '><', ['2011-06-23', '2011-07-10']) |
|
462 | 462 | assert_match /issues\.due_date > '2011-06-22 23:59:59(\.9+)?' AND issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement |
|
463 | 463 | find_issues_with_query(query) |
|
464 | 464 | end |
|
465 | 465 | |
|
466 | 466 | def test_operator_in_more_than |
|
467 | 467 | Issue.find(7).update_attribute(:due_date, (Date.today + 15)) |
|
468 | 468 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
469 | 469 | query.add_filter('due_date', '>t+', ['15']) |
|
470 | 470 | issues = find_issues_with_query(query) |
|
471 | 471 | assert !issues.empty? |
|
472 | 472 | issues.each {|issue| assert(issue.due_date >= (Date.today + 15))} |
|
473 | 473 | end |
|
474 | 474 | |
|
475 | 475 | def test_operator_in_less_than |
|
476 | 476 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
477 | 477 | query.add_filter('due_date', '<t+', ['15']) |
|
478 | 478 | issues = find_issues_with_query(query) |
|
479 | 479 | assert !issues.empty? |
|
480 | 480 | issues.each {|issue| assert(issue.due_date <= (Date.today + 15))} |
|
481 | 481 | end |
|
482 | 482 | |
|
483 | 483 | def test_operator_in_the_next_days |
|
484 | 484 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
485 | 485 | query.add_filter('due_date', '><t+', ['15']) |
|
486 | 486 | issues = find_issues_with_query(query) |
|
487 | 487 | assert !issues.empty? |
|
488 | 488 | issues.each {|issue| assert(issue.due_date >= Date.today && issue.due_date <= (Date.today + 15))} |
|
489 | 489 | end |
|
490 | 490 | |
|
491 | 491 | def test_operator_less_than_ago |
|
492 | 492 | Issue.find(7).update_attribute(:due_date, (Date.today - 3)) |
|
493 | 493 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
494 | 494 | query.add_filter('due_date', '>t-', ['3']) |
|
495 | 495 | issues = find_issues_with_query(query) |
|
496 | 496 | assert !issues.empty? |
|
497 | 497 | issues.each {|issue| assert(issue.due_date >= (Date.today - 3))} |
|
498 | 498 | end |
|
499 | 499 | |
|
500 | 500 | def test_operator_in_the_past_days |
|
501 | 501 | Issue.find(7).update_attribute(:due_date, (Date.today - 3)) |
|
502 | 502 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
503 | 503 | query.add_filter('due_date', '><t-', ['3']) |
|
504 | 504 | issues = find_issues_with_query(query) |
|
505 | 505 | assert !issues.empty? |
|
506 | 506 | issues.each {|issue| assert(issue.due_date >= (Date.today - 3) && issue.due_date <= Date.today)} |
|
507 | 507 | end |
|
508 | 508 | |
|
509 | 509 | def test_operator_more_than_ago |
|
510 | 510 | Issue.find(7).update_attribute(:due_date, (Date.today - 10)) |
|
511 | 511 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
512 | 512 | query.add_filter('due_date', '<t-', ['10']) |
|
513 | 513 | assert query.statement.include?("#{Issue.table_name}.due_date <=") |
|
514 | 514 | issues = find_issues_with_query(query) |
|
515 | 515 | assert !issues.empty? |
|
516 | 516 | issues.each {|issue| assert(issue.due_date <= (Date.today - 10))} |
|
517 | 517 | end |
|
518 | 518 | |
|
519 | 519 | def test_operator_in |
|
520 | 520 | Issue.find(7).update_attribute(:due_date, (Date.today + 2)) |
|
521 | 521 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
522 | 522 | query.add_filter('due_date', 't+', ['2']) |
|
523 | 523 | issues = find_issues_with_query(query) |
|
524 | 524 | assert !issues.empty? |
|
525 | 525 | issues.each {|issue| assert_equal((Date.today + 2), issue.due_date)} |
|
526 | 526 | end |
|
527 | 527 | |
|
528 | 528 | def test_operator_ago |
|
529 | 529 | Issue.find(7).update_attribute(:due_date, (Date.today - 3)) |
|
530 | 530 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
531 | 531 | query.add_filter('due_date', 't-', ['3']) |
|
532 | 532 | issues = find_issues_with_query(query) |
|
533 | 533 | assert !issues.empty? |
|
534 | 534 | issues.each {|issue| assert_equal((Date.today - 3), issue.due_date)} |
|
535 | 535 | end |
|
536 | 536 | |
|
537 | 537 | def test_operator_today |
|
538 | 538 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
539 | 539 | query.add_filter('due_date', 't', ['']) |
|
540 | 540 | issues = find_issues_with_query(query) |
|
541 | 541 | assert !issues.empty? |
|
542 | 542 | issues.each {|issue| assert_equal Date.today, issue.due_date} |
|
543 | 543 | end |
|
544 | 544 | |
|
545 | 545 | def test_operator_this_week_on_date |
|
546 | 546 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
547 | 547 | query.add_filter('due_date', 'w', ['']) |
|
548 | 548 | find_issues_with_query(query) |
|
549 | 549 | end |
|
550 | 550 | |
|
551 | 551 | def test_operator_this_week_on_datetime |
|
552 | 552 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
553 | 553 | query.add_filter('created_on', 'w', ['']) |
|
554 | 554 | find_issues_with_query(query) |
|
555 | 555 | end |
|
556 | 556 | |
|
557 | 557 | def test_operator_contains |
|
558 | 558 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
559 | 559 | query.add_filter('subject', '~', ['uNable']) |
|
560 | 560 | assert query.statement.include?("LOWER(#{Issue.table_name}.subject) LIKE '%unable%'") |
|
561 | 561 | result = find_issues_with_query(query) |
|
562 | 562 | assert result.empty? |
|
563 | 563 | result.each {|issue| assert issue.subject.downcase.include?('unable') } |
|
564 | 564 | end |
|
565 | 565 | |
|
566 | 566 | def test_range_for_this_week_with_week_starting_on_monday |
|
567 | 567 | I18n.locale = :fr |
|
568 | 568 | assert_equal '1', I18n.t(:general_first_day_of_week) |
|
569 | 569 | |
|
570 | 570 | Date.stubs(:today).returns(Date.parse('2011-04-29')) |
|
571 | 571 | |
|
572 | 572 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
573 | 573 | query.add_filter('due_date', 'w', ['']) |
|
574 | 574 | assert query.statement.match(/issues\.due_date > '2011-04-24 23:59:59(\.9+)?' AND issues\.due_date <= '2011-05-01 23:59:59(\.9+)?/), "range not found in #{query.statement}" |
|
575 | 575 | I18n.locale = :en |
|
576 | 576 | end |
|
577 | 577 | |
|
578 | 578 | def test_range_for_this_week_with_week_starting_on_sunday |
|
579 | 579 | I18n.locale = :en |
|
580 | 580 | assert_equal '7', I18n.t(:general_first_day_of_week) |
|
581 | 581 | |
|
582 | 582 | Date.stubs(:today).returns(Date.parse('2011-04-29')) |
|
583 | 583 | |
|
584 | 584 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
585 | 585 | query.add_filter('due_date', 'w', ['']) |
|
586 | 586 | assert query.statement.match(/issues\.due_date > '2011-04-23 23:59:59(\.9+)?' AND issues\.due_date <= '2011-04-30 23:59:59(\.9+)?/), "range not found in #{query.statement}" |
|
587 | 587 | end |
|
588 | 588 | |
|
589 | 589 | def test_operator_does_not_contains |
|
590 | 590 | query = IssueQuery.new(:project => Project.find(1), :name => '_') |
|
591 | 591 | query.add_filter('subject', '!~', ['uNable']) |
|
592 | 592 | assert query.statement.include?("LOWER(#{Issue.table_name}.subject) NOT LIKE '%unable%'") |
|
593 | 593 | find_issues_with_query(query) |
|
594 | 594 | end |
|
595 | 595 | |
|
596 | 596 | def test_filter_assigned_to_me |
|
597 | 597 | user = User.find(2) |
|
598 | 598 | group = Group.find(10) |
|
599 | 599 | User.current = user |
|
600 | 600 | i1 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => user) |
|
601 | 601 | i2 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => group) |
|
602 | 602 | i3 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => Group.find(11)) |
|
603 | 603 | group.users << user |
|
604 | 604 | |
|
605 | 605 | query = IssueQuery.new(:name => '_', :filters => { 'assigned_to_id' => {:operator => '=', :values => ['me']}}) |
|
606 | 606 | result = query.issues |
|
607 | 607 | assert_equal Issue.visible.where(:assigned_to_id => ([2] + user.reload.group_ids)).sort_by(&:id), result.sort_by(&:id) |
|
608 | 608 | |
|
609 | 609 | assert result.include?(i1) |
|
610 | 610 | assert result.include?(i2) |
|
611 | 611 | assert !result.include?(i3) |
|
612 | 612 | end |
|
613 | 613 | |
|
614 | 614 | def test_user_custom_field_filtered_on_me |
|
615 | 615 | User.current = User.find(2) |
|
616 | 616 | cf = IssueCustomField.create!(:field_format => 'user', :is_for_all => true, :is_filter => true, :name => 'User custom field', :tracker_ids => [1]) |
|
617 | 617 | issue1 = Issue.create!(:project_id => 1, :tracker_id => 1, :custom_field_values => {cf.id.to_s => '2'}, :subject => 'Test', :author_id => 1) |
|
618 | 618 | issue2 = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {cf.id.to_s => '3'}) |
|
619 | 619 | |
|
620 | 620 | query = IssueQuery.new(:name => '_', :project => Project.find(1)) |
|
621 | 621 | filter = query.available_filters["cf_#{cf.id}"] |
|
622 | 622 | assert_not_nil filter |
|
623 | 623 | assert_include 'me', filter[:values].map{|v| v[1]} |
|
624 | 624 | |
|
625 | 625 | query.filters = { "cf_#{cf.id}" => {:operator => '=', :values => ['me']}} |
|
626 | 626 | result = query.issues |
|
627 | 627 | assert_equal 1, result.size |
|
628 | 628 | assert_equal issue1, result.first |
|
629 | 629 | end |
|
630 | 630 | |
|
631 | 631 | def test_filter_my_projects |
|
632 | 632 | User.current = User.find(2) |
|
633 | 633 | query = IssueQuery.new(:name => '_') |
|
634 | 634 | filter = query.available_filters['project_id'] |
|
635 | 635 | assert_not_nil filter |
|
636 | 636 | assert_include 'mine', filter[:values].map{|v| v[1]} |
|
637 | 637 | |
|
638 | 638 | query.filters = { 'project_id' => {:operator => '=', :values => ['mine']}} |
|
639 | 639 | result = query.issues |
|
640 | 640 | assert_nil result.detect {|issue| !User.current.member_of?(issue.project)} |
|
641 | 641 | end |
|
642 | 642 | |
|
643 | 643 | def test_filter_watched_issues |
|
644 | 644 | User.current = User.find(1) |
|
645 | 645 | query = IssueQuery.new(:name => '_', :filters => { 'watcher_id' => {:operator => '=', :values => ['me']}}) |
|
646 | 646 | result = find_issues_with_query(query) |
|
647 | 647 | assert_not_nil result |
|
648 | 648 | assert !result.empty? |
|
649 | 649 | assert_equal Issue.visible.watched_by(User.current).sort_by(&:id), result.sort_by(&:id) |
|
650 | 650 | User.current = nil |
|
651 | 651 | end |
|
652 | 652 | |
|
653 | 653 | def test_filter_unwatched_issues |
|
654 | 654 | User.current = User.find(1) |
|
655 | 655 | query = IssueQuery.new(:name => '_', :filters => { 'watcher_id' => {:operator => '!', :values => ['me']}}) |
|
656 | 656 | result = find_issues_with_query(query) |
|
657 | 657 | assert_not_nil result |
|
658 | 658 | assert !result.empty? |
|
659 | 659 | assert_equal((Issue.visible - Issue.watched_by(User.current)).sort_by(&:id).size, result.sort_by(&:id).size) |
|
660 | 660 | User.current = nil |
|
661 | 661 | end |
|
662 | 662 | |
|
663 | 663 | def test_filter_on_project_custom_field |
|
664 | 664 | field = ProjectCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string') |
|
665 | 665 | CustomValue.create!(:custom_field => field, :customized => Project.find(3), :value => 'Foo') |
|
666 | 666 | CustomValue.create!(:custom_field => field, :customized => Project.find(5), :value => 'Foo') |
|
667 | 667 | |
|
668 | 668 | query = IssueQuery.new(:name => '_') |
|
669 | 669 | filter_name = "project.cf_#{field.id}" |
|
670 | 670 | assert_include filter_name, query.available_filters.keys |
|
671 | 671 | query.filters = {filter_name => {:operator => '=', :values => ['Foo']}} |
|
672 | 672 | assert_equal [3, 5], find_issues_with_query(query).map(&:project_id).uniq.sort |
|
673 | 673 | end |
|
674 | 674 | |
|
675 | 675 | def test_filter_on_author_custom_field |
|
676 | 676 | field = UserCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string') |
|
677 | 677 | CustomValue.create!(:custom_field => field, :customized => User.find(3), :value => 'Foo') |
|
678 | 678 | |
|
679 | 679 | query = IssueQuery.new(:name => '_') |
|
680 | 680 | filter_name = "author.cf_#{field.id}" |
|
681 | 681 | assert_include filter_name, query.available_filters.keys |
|
682 | 682 | query.filters = {filter_name => {:operator => '=', :values => ['Foo']}} |
|
683 | 683 | assert_equal [3], find_issues_with_query(query).map(&:author_id).uniq.sort |
|
684 | 684 | end |
|
685 | 685 | |
|
686 | 686 | def test_filter_on_assigned_to_custom_field |
|
687 | 687 | field = UserCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string') |
|
688 | 688 | CustomValue.create!(:custom_field => field, :customized => User.find(3), :value => 'Foo') |
|
689 | 689 | |
|
690 | 690 | query = IssueQuery.new(:name => '_') |
|
691 | 691 | filter_name = "assigned_to.cf_#{field.id}" |
|
692 | 692 | assert_include filter_name, query.available_filters.keys |
|
693 | 693 | query.filters = {filter_name => {:operator => '=', :values => ['Foo']}} |
|
694 | 694 | assert_equal [3], find_issues_with_query(query).map(&:assigned_to_id).uniq.sort |
|
695 | 695 | end |
|
696 | 696 | |
|
697 | 697 | def test_filter_on_fixed_version_custom_field |
|
698 | 698 | field = VersionCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string') |
|
699 | 699 | CustomValue.create!(:custom_field => field, :customized => Version.find(2), :value => 'Foo') |
|
700 | 700 | |
|
701 | 701 | query = IssueQuery.new(:name => '_') |
|
702 | 702 | filter_name = "fixed_version.cf_#{field.id}" |
|
703 | 703 | assert_include filter_name, query.available_filters.keys |
|
704 | 704 | query.filters = {filter_name => {:operator => '=', :values => ['Foo']}} |
|
705 | 705 | assert_equal [2], find_issues_with_query(query).map(&:fixed_version_id).uniq.sort |
|
706 | 706 | end |
|
707 | 707 | |
|
708 | 708 | def test_filter_on_relations_with_a_specific_issue |
|
709 | 709 | IssueRelation.delete_all |
|
710 | 710 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(2)) |
|
711 | 711 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(3), :issue_to => Issue.find(1)) |
|
712 | 712 | |
|
713 | 713 | query = IssueQuery.new(:name => '_') |
|
714 | 714 | query.filters = {"relates" => {:operator => '=', :values => ['1']}} |
|
715 | 715 | assert_equal [2, 3], find_issues_with_query(query).map(&:id).sort |
|
716 | 716 | |
|
717 | 717 | query = IssueQuery.new(:name => '_') |
|
718 | 718 | query.filters = {"relates" => {:operator => '=', :values => ['2']}} |
|
719 | 719 | assert_equal [1], find_issues_with_query(query).map(&:id).sort |
|
720 | 720 | end |
|
721 | 721 | |
|
722 | 722 | def test_filter_on_relations_with_any_issues_in_a_project |
|
723 | 723 | IssueRelation.delete_all |
|
724 | 724 | with_settings :cross_project_issue_relations => '1' do |
|
725 | 725 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(2).issues.first) |
|
726 | 726 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(2), :issue_to => Project.find(2).issues.first) |
|
727 | 727 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(3).issues.first) |
|
728 | 728 | end |
|
729 | 729 | |
|
730 | 730 | query = IssueQuery.new(:name => '_') |
|
731 | 731 | query.filters = {"relates" => {:operator => '=p', :values => ['2']}} |
|
732 | 732 | assert_equal [1, 2], find_issues_with_query(query).map(&:id).sort |
|
733 | 733 | |
|
734 | 734 | query = IssueQuery.new(:name => '_') |
|
735 | 735 | query.filters = {"relates" => {:operator => '=p', :values => ['3']}} |
|
736 | 736 | assert_equal [1], find_issues_with_query(query).map(&:id).sort |
|
737 | 737 | |
|
738 | 738 | query = IssueQuery.new(:name => '_') |
|
739 | 739 | query.filters = {"relates" => {:operator => '=p', :values => ['4']}} |
|
740 | 740 | assert_equal [], find_issues_with_query(query).map(&:id).sort |
|
741 | 741 | end |
|
742 | 742 | |
|
743 | 743 | def test_filter_on_relations_with_any_issues_not_in_a_project |
|
744 | 744 | IssueRelation.delete_all |
|
745 | 745 | with_settings :cross_project_issue_relations => '1' do |
|
746 | 746 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(2).issues.first) |
|
747 | 747 | #IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(2), :issue_to => Project.find(1).issues.first) |
|
748 | 748 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(3).issues.first) |
|
749 | 749 | end |
|
750 | 750 | |
|
751 | 751 | query = IssueQuery.new(:name => '_') |
|
752 | 752 | query.filters = {"relates" => {:operator => '=!p', :values => ['1']}} |
|
753 | 753 | assert_equal [1], find_issues_with_query(query).map(&:id).sort |
|
754 | 754 | end |
|
755 | 755 | |
|
756 | 756 | def test_filter_on_relations_with_no_issues_in_a_project |
|
757 | 757 | IssueRelation.delete_all |
|
758 | 758 | with_settings :cross_project_issue_relations => '1' do |
|
759 | 759 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(2).issues.first) |
|
760 | 760 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(2), :issue_to => Project.find(3).issues.first) |
|
761 | 761 | IssueRelation.create!(:relation_type => "relates", :issue_to => Project.find(2).issues.first, :issue_from => Issue.find(3)) |
|
762 | 762 | end |
|
763 | 763 | |
|
764 | 764 | query = IssueQuery.new(:name => '_') |
|
765 | 765 | query.filters = {"relates" => {:operator => '!p', :values => ['2']}} |
|
766 | 766 | ids = find_issues_with_query(query).map(&:id).sort |
|
767 | 767 | assert_include 2, ids |
|
768 | 768 | assert_not_include 1, ids |
|
769 | 769 | assert_not_include 3, ids |
|
770 | 770 | end |
|
771 | 771 | |
|
772 | 772 | def test_filter_on_relations_with_no_issues |
|
773 | 773 | IssueRelation.delete_all |
|
774 | 774 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(2)) |
|
775 | 775 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(3), :issue_to => Issue.find(1)) |
|
776 | 776 | |
|
777 | 777 | query = IssueQuery.new(:name => '_') |
|
778 | 778 | query.filters = {"relates" => {:operator => '!*', :values => ['']}} |
|
779 | 779 | ids = find_issues_with_query(query).map(&:id) |
|
780 | 780 | assert_equal [], ids & [1, 2, 3] |
|
781 | 781 | assert_include 4, ids |
|
782 | 782 | end |
|
783 | 783 | |
|
784 | 784 | def test_filter_on_relations_with_any_issues |
|
785 | 785 | IssueRelation.delete_all |
|
786 | 786 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(2)) |
|
787 | 787 | IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(3), :issue_to => Issue.find(1)) |
|
788 | 788 | |
|
789 | 789 | query = IssueQuery.new(:name => '_') |
|
790 | 790 | query.filters = {"relates" => {:operator => '*', :values => ['']}} |
|
791 | 791 | assert_equal [1, 2, 3], find_issues_with_query(query).map(&:id).sort |
|
792 | 792 | end |
|
793 | 793 | |
|
794 | def test_filter_on_relations_should_not_ignore_other_filter | |
|
795 | issue = Issue.generate! | |
|
796 | issue1 = Issue.generate!(:status_id => 1) | |
|
797 | issue2 = Issue.generate!(:status_id => 2) | |
|
798 | IssueRelation.create!(:relation_type => "relates", :issue_from => issue, :issue_to => issue1) | |
|
799 | IssueRelation.create!(:relation_type => "relates", :issue_from => issue, :issue_to => issue2) | |
|
800 | ||
|
801 | query = IssueQuery.new(:name => '_') | |
|
802 | query.filters = { | |
|
803 | "status_id" => {:operator => '=', :values => ['1']}, | |
|
804 | "relates" => {:operator => '=', :values => [issue.id.to_s]} | |
|
805 | } | |
|
806 | assert_equal [issue1], find_issues_with_query(query) | |
|
807 | end | |
|
808 | ||
|
794 | 809 | def test_statement_should_be_nil_with_no_filters |
|
795 | 810 | q = IssueQuery.new(:name => '_') |
|
796 | 811 | q.filters = {} |
|
797 | 812 | |
|
798 | 813 | assert q.valid? |
|
799 | 814 | assert_nil q.statement |
|
800 | 815 | end |
|
801 | 816 | |
|
802 | 817 | def test_default_columns |
|
803 | 818 | q = IssueQuery.new |
|
804 | 819 | assert q.columns.any? |
|
805 | 820 | assert q.inline_columns.any? |
|
806 | 821 | assert q.block_columns.empty? |
|
807 | 822 | end |
|
808 | 823 | |
|
809 | 824 | def test_set_column_names |
|
810 | 825 | q = IssueQuery.new |
|
811 | 826 | q.column_names = ['tracker', :subject, '', 'unknonw_column'] |
|
812 | 827 | assert_equal [:id, :tracker, :subject], q.columns.collect {|c| c.name} |
|
813 | 828 | end |
|
814 | 829 | |
|
815 | 830 | def test_has_column_should_accept_a_column_name |
|
816 | 831 | q = IssueQuery.new |
|
817 | 832 | q.column_names = ['tracker', :subject] |
|
818 | 833 | assert q.has_column?(:tracker) |
|
819 | 834 | assert !q.has_column?(:category) |
|
820 | 835 | end |
|
821 | 836 | |
|
822 | 837 | def test_has_column_should_accept_a_column |
|
823 | 838 | q = IssueQuery.new |
|
824 | 839 | q.column_names = ['tracker', :subject] |
|
825 | 840 | |
|
826 | 841 | tracker_column = q.available_columns.detect {|c| c.name==:tracker} |
|
827 | 842 | assert_kind_of QueryColumn, tracker_column |
|
828 | 843 | category_column = q.available_columns.detect {|c| c.name==:category} |
|
829 | 844 | assert_kind_of QueryColumn, category_column |
|
830 | 845 | |
|
831 | 846 | assert q.has_column?(tracker_column) |
|
832 | 847 | assert !q.has_column?(category_column) |
|
833 | 848 | end |
|
834 | 849 | |
|
835 | 850 | def test_inline_and_block_columns |
|
836 | 851 | q = IssueQuery.new |
|
837 | 852 | q.column_names = ['subject', 'description', 'tracker'] |
|
838 | 853 | |
|
839 | 854 | assert_equal [:id, :subject, :tracker], q.inline_columns.map(&:name) |
|
840 | 855 | assert_equal [:description], q.block_columns.map(&:name) |
|
841 | 856 | end |
|
842 | 857 | |
|
843 | 858 | def test_custom_field_columns_should_be_inline |
|
844 | 859 | q = IssueQuery.new |
|
845 | 860 | columns = q.available_columns.select {|column| column.is_a? QueryCustomFieldColumn} |
|
846 | 861 | assert columns.any? |
|
847 | 862 | assert_nil columns.detect {|column| !column.inline?} |
|
848 | 863 | end |
|
849 | 864 | |
|
850 | 865 | def test_query_should_preload_spent_hours |
|
851 | 866 | q = IssueQuery.new(:name => '_', :column_names => [:subject, :spent_hours]) |
|
852 | 867 | assert q.has_column?(:spent_hours) |
|
853 | 868 | issues = q.issues |
|
854 | 869 | assert_not_nil issues.first.instance_variable_get("@spent_hours") |
|
855 | 870 | end |
|
856 | 871 | |
|
857 | 872 | def test_groupable_columns_should_include_custom_fields |
|
858 | 873 | q = IssueQuery.new |
|
859 | 874 | column = q.groupable_columns.detect {|c| c.name == :cf_1} |
|
860 | 875 | assert_not_nil column |
|
861 | 876 | assert_kind_of QueryCustomFieldColumn, column |
|
862 | 877 | end |
|
863 | 878 | |
|
864 | 879 | def test_groupable_columns_should_not_include_multi_custom_fields |
|
865 | 880 | field = CustomField.find(1) |
|
866 | 881 | field.update_attribute :multiple, true |
|
867 | 882 | |
|
868 | 883 | q = IssueQuery.new |
|
869 | 884 | column = q.groupable_columns.detect {|c| c.name == :cf_1} |
|
870 | 885 | assert_nil column |
|
871 | 886 | end |
|
872 | 887 | |
|
873 | 888 | def test_groupable_columns_should_include_user_custom_fields |
|
874 | 889 | cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1], :field_format => 'user') |
|
875 | 890 | |
|
876 | 891 | q = IssueQuery.new |
|
877 | 892 | assert q.groupable_columns.detect {|c| c.name == "cf_#{cf.id}".to_sym} |
|
878 | 893 | end |
|
879 | 894 | |
|
880 | 895 | def test_groupable_columns_should_include_version_custom_fields |
|
881 | 896 | cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1], :field_format => 'version') |
|
882 | 897 | |
|
883 | 898 | q = IssueQuery.new |
|
884 | 899 | assert q.groupable_columns.detect {|c| c.name == "cf_#{cf.id}".to_sym} |
|
885 | 900 | end |
|
886 | 901 | |
|
887 | 902 | def test_grouped_with_valid_column |
|
888 | 903 | q = IssueQuery.new(:group_by => 'status') |
|
889 | 904 | assert q.grouped? |
|
890 | 905 | assert_not_nil q.group_by_column |
|
891 | 906 | assert_equal :status, q.group_by_column.name |
|
892 | 907 | assert_not_nil q.group_by_statement |
|
893 | 908 | assert_equal 'status', q.group_by_statement |
|
894 | 909 | end |
|
895 | 910 | |
|
896 | 911 | def test_grouped_with_invalid_column |
|
897 | 912 | q = IssueQuery.new(:group_by => 'foo') |
|
898 | 913 | assert !q.grouped? |
|
899 | 914 | assert_nil q.group_by_column |
|
900 | 915 | assert_nil q.group_by_statement |
|
901 | 916 | end |
|
902 | 917 | |
|
903 | 918 | def test_sortable_columns_should_sort_assignees_according_to_user_format_setting |
|
904 | 919 | with_settings :user_format => 'lastname_coma_firstname' do |
|
905 | 920 | q = IssueQuery.new |
|
906 | 921 | assert q.sortable_columns.has_key?('assigned_to') |
|
907 | 922 | assert_equal %w(users.lastname users.firstname users.id), q.sortable_columns['assigned_to'] |
|
908 | 923 | end |
|
909 | 924 | end |
|
910 | 925 | |
|
911 | 926 | def test_sortable_columns_should_sort_authors_according_to_user_format_setting |
|
912 | 927 | with_settings :user_format => 'lastname_coma_firstname' do |
|
913 | 928 | q = IssueQuery.new |
|
914 | 929 | assert q.sortable_columns.has_key?('author') |
|
915 | 930 | assert_equal %w(authors.lastname authors.firstname authors.id), q.sortable_columns['author'] |
|
916 | 931 | end |
|
917 | 932 | end |
|
918 | 933 | |
|
919 | 934 | def test_sortable_columns_should_include_custom_field |
|
920 | 935 | q = IssueQuery.new |
|
921 | 936 | assert q.sortable_columns['cf_1'] |
|
922 | 937 | end |
|
923 | 938 | |
|
924 | 939 | def test_sortable_columns_should_not_include_multi_custom_field |
|
925 | 940 | field = CustomField.find(1) |
|
926 | 941 | field.update_attribute :multiple, true |
|
927 | 942 | |
|
928 | 943 | q = IssueQuery.new |
|
929 | 944 | assert !q.sortable_columns['cf_1'] |
|
930 | 945 | end |
|
931 | 946 | |
|
932 | 947 | def test_default_sort |
|
933 | 948 | q = IssueQuery.new |
|
934 | 949 | assert_equal [], q.sort_criteria |
|
935 | 950 | end |
|
936 | 951 | |
|
937 | 952 | def test_set_sort_criteria_with_hash |
|
938 | 953 | q = IssueQuery.new |
|
939 | 954 | q.sort_criteria = {'0' => ['priority', 'desc'], '2' => ['tracker']} |
|
940 | 955 | assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria |
|
941 | 956 | end |
|
942 | 957 | |
|
943 | 958 | def test_set_sort_criteria_with_array |
|
944 | 959 | q = IssueQuery.new |
|
945 | 960 | q.sort_criteria = [['priority', 'desc'], 'tracker'] |
|
946 | 961 | assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria |
|
947 | 962 | end |
|
948 | 963 | |
|
949 | 964 | def test_create_query_with_sort |
|
950 | 965 | q = IssueQuery.new(:name => 'Sorted') |
|
951 | 966 | q.sort_criteria = [['priority', 'desc'], 'tracker'] |
|
952 | 967 | assert q.save |
|
953 | 968 | q.reload |
|
954 | 969 | assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria |
|
955 | 970 | end |
|
956 | 971 | |
|
957 | 972 | def test_sort_by_string_custom_field_asc |
|
958 | 973 | q = IssueQuery.new |
|
959 | 974 | c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' } |
|
960 | 975 | assert c |
|
961 | 976 | assert c.sortable |
|
962 | 977 | issues = q.issues(:order => "#{c.sortable} ASC") |
|
963 | 978 | values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s} |
|
964 | 979 | assert !values.empty? |
|
965 | 980 | assert_equal values.sort, values |
|
966 | 981 | end |
|
967 | 982 | |
|
968 | 983 | def test_sort_by_string_custom_field_desc |
|
969 | 984 | q = IssueQuery.new |
|
970 | 985 | c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' } |
|
971 | 986 | assert c |
|
972 | 987 | assert c.sortable |
|
973 | 988 | issues = q.issues(:order => "#{c.sortable} DESC") |
|
974 | 989 | values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s} |
|
975 | 990 | assert !values.empty? |
|
976 | 991 | assert_equal values.sort.reverse, values |
|
977 | 992 | end |
|
978 | 993 | |
|
979 | 994 | def test_sort_by_float_custom_field_asc |
|
980 | 995 | q = IssueQuery.new |
|
981 | 996 | c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'float' } |
|
982 | 997 | assert c |
|
983 | 998 | assert c.sortable |
|
984 | 999 | issues = q.issues(:order => "#{c.sortable} ASC") |
|
985 | 1000 | values = issues.collect {|i| begin; Kernel.Float(i.custom_value_for(c.custom_field).to_s); rescue; nil; end}.compact |
|
986 | 1001 | assert !values.empty? |
|
987 | 1002 | assert_equal values.sort, values |
|
988 | 1003 | end |
|
989 | 1004 | |
|
990 | 1005 | def test_invalid_query_should_raise_query_statement_invalid_error |
|
991 | 1006 | q = IssueQuery.new |
|
992 | 1007 | assert_raise Query::StatementInvalid do |
|
993 | 1008 | q.issues(:conditions => "foo = 1") |
|
994 | 1009 | end |
|
995 | 1010 | end |
|
996 | 1011 | |
|
997 | 1012 | def test_issue_count |
|
998 | 1013 | q = IssueQuery.new(:name => '_') |
|
999 | 1014 | issue_count = q.issue_count |
|
1000 | 1015 | assert_equal q.issues.size, issue_count |
|
1001 | 1016 | end |
|
1002 | 1017 | |
|
1003 | 1018 | def test_issue_count_with_archived_issues |
|
1004 | 1019 | p = Project.generate! do |project| |
|
1005 | 1020 | project.status = Project::STATUS_ARCHIVED |
|
1006 | 1021 | end |
|
1007 | 1022 | i = Issue.generate!( :project => p, :tracker => p.trackers.first ) |
|
1008 | 1023 | assert !i.visible? |
|
1009 | 1024 | |
|
1010 | 1025 | test_issue_count |
|
1011 | 1026 | end |
|
1012 | 1027 | |
|
1013 | 1028 | def test_issue_count_by_association_group |
|
1014 | 1029 | q = IssueQuery.new(:name => '_', :group_by => 'assigned_to') |
|
1015 | 1030 | count_by_group = q.issue_count_by_group |
|
1016 | 1031 | assert_kind_of Hash, count_by_group |
|
1017 | 1032 | assert_equal %w(NilClass User), count_by_group.keys.collect {|k| k.class.name}.uniq.sort |
|
1018 | 1033 | assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq |
|
1019 | 1034 | assert count_by_group.has_key?(User.find(3)) |
|
1020 | 1035 | end |
|
1021 | 1036 | |
|
1022 | 1037 | def test_issue_count_by_list_custom_field_group |
|
1023 | 1038 | q = IssueQuery.new(:name => '_', :group_by => 'cf_1') |
|
1024 | 1039 | count_by_group = q.issue_count_by_group |
|
1025 | 1040 | assert_kind_of Hash, count_by_group |
|
1026 | 1041 | assert_equal %w(NilClass String), count_by_group.keys.collect {|k| k.class.name}.uniq.sort |
|
1027 | 1042 | assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq |
|
1028 | 1043 | assert count_by_group.has_key?('MySQL') |
|
1029 | 1044 | end |
|
1030 | 1045 | |
|
1031 | 1046 | def test_issue_count_by_date_custom_field_group |
|
1032 | 1047 | q = IssueQuery.new(:name => '_', :group_by => 'cf_8') |
|
1033 | 1048 | count_by_group = q.issue_count_by_group |
|
1034 | 1049 | assert_kind_of Hash, count_by_group |
|
1035 | 1050 | assert_equal %w(Date NilClass), count_by_group.keys.collect {|k| k.class.name}.uniq.sort |
|
1036 | 1051 | assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq |
|
1037 | 1052 | end |
|
1038 | 1053 | |
|
1039 | 1054 | def test_issue_count_with_nil_group_only |
|
1040 | 1055 | Issue.update_all("assigned_to_id = NULL") |
|
1041 | 1056 | |
|
1042 | 1057 | q = IssueQuery.new(:name => '_', :group_by => 'assigned_to') |
|
1043 | 1058 | count_by_group = q.issue_count_by_group |
|
1044 | 1059 | assert_kind_of Hash, count_by_group |
|
1045 | 1060 | assert_equal 1, count_by_group.keys.size |
|
1046 | 1061 | assert_nil count_by_group.keys.first |
|
1047 | 1062 | end |
|
1048 | 1063 | |
|
1049 | 1064 | def test_issue_ids |
|
1050 | 1065 | q = IssueQuery.new(:name => '_') |
|
1051 | 1066 | order = "issues.subject, issues.id" |
|
1052 | 1067 | issues = q.issues(:order => order) |
|
1053 | 1068 | assert_equal issues.map(&:id), q.issue_ids(:order => order) |
|
1054 | 1069 | end |
|
1055 | 1070 | |
|
1056 | 1071 | def test_label_for |
|
1057 | 1072 | set_language_if_valid 'en' |
|
1058 | 1073 | q = IssueQuery.new |
|
1059 | 1074 | assert_equal 'Assignee', q.label_for('assigned_to_id') |
|
1060 | 1075 | end |
|
1061 | 1076 | |
|
1062 | 1077 | def test_label_for_fr |
|
1063 | 1078 | set_language_if_valid 'fr' |
|
1064 | 1079 | q = IssueQuery.new |
|
1065 | 1080 | s = "Assign\xc3\xa9 \xc3\xa0" |
|
1066 | 1081 | s.force_encoding('UTF-8') if s.respond_to?(:force_encoding) |
|
1067 | 1082 | assert_equal s, q.label_for('assigned_to_id') |
|
1068 | 1083 | end |
|
1069 | 1084 | |
|
1070 | 1085 | def test_editable_by |
|
1071 | 1086 | admin = User.find(1) |
|
1072 | 1087 | manager = User.find(2) |
|
1073 | 1088 | developer = User.find(3) |
|
1074 | 1089 | |
|
1075 | 1090 | # Public query on project 1 |
|
1076 | 1091 | q = IssueQuery.find(1) |
|
1077 | 1092 | assert q.editable_by?(admin) |
|
1078 | 1093 | assert q.editable_by?(manager) |
|
1079 | 1094 | assert !q.editable_by?(developer) |
|
1080 | 1095 | |
|
1081 | 1096 | # Private query on project 1 |
|
1082 | 1097 | q = IssueQuery.find(2) |
|
1083 | 1098 | assert q.editable_by?(admin) |
|
1084 | 1099 | assert !q.editable_by?(manager) |
|
1085 | 1100 | assert q.editable_by?(developer) |
|
1086 | 1101 | |
|
1087 | 1102 | # Private query for all projects |
|
1088 | 1103 | q = IssueQuery.find(3) |
|
1089 | 1104 | assert q.editable_by?(admin) |
|
1090 | 1105 | assert !q.editable_by?(manager) |
|
1091 | 1106 | assert q.editable_by?(developer) |
|
1092 | 1107 | |
|
1093 | 1108 | # Public query for all projects |
|
1094 | 1109 | q = IssueQuery.find(4) |
|
1095 | 1110 | assert q.editable_by?(admin) |
|
1096 | 1111 | assert !q.editable_by?(manager) |
|
1097 | 1112 | assert !q.editable_by?(developer) |
|
1098 | 1113 | end |
|
1099 | 1114 | |
|
1100 | 1115 | def test_visible_scope |
|
1101 | 1116 | query_ids = IssueQuery.visible(User.anonymous).map(&:id) |
|
1102 | 1117 | |
|
1103 | 1118 | assert query_ids.include?(1), 'public query on public project was not visible' |
|
1104 | 1119 | assert query_ids.include?(4), 'public query for all projects was not visible' |
|
1105 | 1120 | assert !query_ids.include?(2), 'private query on public project was visible' |
|
1106 | 1121 | assert !query_ids.include?(3), 'private query for all projects was visible' |
|
1107 | 1122 | assert !query_ids.include?(7), 'public query on private project was visible' |
|
1108 | 1123 | end |
|
1109 | 1124 | |
|
1110 | 1125 | def test_query_with_public_visibility_should_be_visible_to_anyone |
|
1111 | 1126 | q = IssueQuery.create!(:name => 'Query', :visibility => IssueQuery::VISIBILITY_PUBLIC) |
|
1112 | 1127 | |
|
1113 | 1128 | assert q.visible?(User.anonymous) |
|
1114 | 1129 | assert IssueQuery.visible(User.anonymous).find_by_id(q.id) |
|
1115 | 1130 | |
|
1116 | 1131 | assert q.visible?(User.find(7)) |
|
1117 | 1132 | assert IssueQuery.visible(User.find(7)).find_by_id(q.id) |
|
1118 | 1133 | |
|
1119 | 1134 | assert q.visible?(User.find(2)) |
|
1120 | 1135 | assert IssueQuery.visible(User.find(2)).find_by_id(q.id) |
|
1121 | 1136 | |
|
1122 | 1137 | assert q.visible?(User.find(1)) |
|
1123 | 1138 | assert IssueQuery.visible(User.find(1)).find_by_id(q.id) |
|
1124 | 1139 | end |
|
1125 | 1140 | |
|
1126 | 1141 | def test_query_with_roles_visibility_should_be_visible_to_user_with_role |
|
1127 | 1142 | q = IssueQuery.create!(:name => 'Query', :visibility => IssueQuery::VISIBILITY_ROLES, :role_ids => [1,2]) |
|
1128 | 1143 | |
|
1129 | 1144 | assert !q.visible?(User.anonymous) |
|
1130 | 1145 | assert_nil IssueQuery.visible(User.anonymous).find_by_id(q.id) |
|
1131 | 1146 | |
|
1132 | 1147 | assert !q.visible?(User.find(7)) |
|
1133 | 1148 | assert_nil IssueQuery.visible(User.find(7)).find_by_id(q.id) |
|
1134 | 1149 | |
|
1135 | 1150 | assert q.visible?(User.find(2)) |
|
1136 | 1151 | assert IssueQuery.visible(User.find(2)).find_by_id(q.id) |
|
1137 | 1152 | |
|
1138 | 1153 | assert q.visible?(User.find(1)) |
|
1139 | 1154 | assert IssueQuery.visible(User.find(1)).find_by_id(q.id) |
|
1140 | 1155 | end |
|
1141 | 1156 | |
|
1142 | 1157 | def test_query_with_private_visibility_should_be_visible_to_owner |
|
1143 | 1158 | q = IssueQuery.create!(:name => 'Query', :visibility => IssueQuery::VISIBILITY_PRIVATE, :user => User.find(7)) |
|
1144 | 1159 | |
|
1145 | 1160 | assert !q.visible?(User.anonymous) |
|
1146 | 1161 | assert_nil IssueQuery.visible(User.anonymous).find_by_id(q.id) |
|
1147 | 1162 | |
|
1148 | 1163 | assert q.visible?(User.find(7)) |
|
1149 | 1164 | assert IssueQuery.visible(User.find(7)).find_by_id(q.id) |
|
1150 | 1165 | |
|
1151 | 1166 | assert !q.visible?(User.find(2)) |
|
1152 | 1167 | assert_nil IssueQuery.visible(User.find(2)).find_by_id(q.id) |
|
1153 | 1168 | |
|
1154 | 1169 | assert q.visible?(User.find(1)) |
|
1155 | 1170 | assert_nil IssueQuery.visible(User.find(1)).find_by_id(q.id) |
|
1156 | 1171 | end |
|
1157 | 1172 | |
|
1158 | 1173 | test "#available_filters should include users of visible projects in cross-project view" do |
|
1159 | 1174 | users = IssueQuery.new.available_filters["assigned_to_id"] |
|
1160 | 1175 | assert_not_nil users |
|
1161 | 1176 | assert users[:values].map{|u|u[1]}.include?("3") |
|
1162 | 1177 | end |
|
1163 | 1178 | |
|
1164 | 1179 | test "#available_filters should include users of subprojects" do |
|
1165 | 1180 | user1 = User.generate! |
|
1166 | 1181 | user2 = User.generate! |
|
1167 | 1182 | project = Project.find(1) |
|
1168 | 1183 | Member.create!(:principal => user1, :project => project.children.visible.first, :role_ids => [1]) |
|
1169 | 1184 | |
|
1170 | 1185 | users = IssueQuery.new(:project => project).available_filters["assigned_to_id"] |
|
1171 | 1186 | assert_not_nil users |
|
1172 | 1187 | assert users[:values].map{|u|u[1]}.include?(user1.id.to_s) |
|
1173 | 1188 | assert !users[:values].map{|u|u[1]}.include?(user2.id.to_s) |
|
1174 | 1189 | end |
|
1175 | 1190 | |
|
1176 | 1191 | test "#available_filters should include visible projects in cross-project view" do |
|
1177 | 1192 | projects = IssueQuery.new.available_filters["project_id"] |
|
1178 | 1193 | assert_not_nil projects |
|
1179 | 1194 | assert projects[:values].map{|u|u[1]}.include?("1") |
|
1180 | 1195 | end |
|
1181 | 1196 | |
|
1182 | 1197 | test "#available_filters should include 'member_of_group' filter" do |
|
1183 | 1198 | query = IssueQuery.new |
|
1184 | 1199 | assert query.available_filters.keys.include?("member_of_group") |
|
1185 | 1200 | assert_equal :list_optional, query.available_filters["member_of_group"][:type] |
|
1186 | 1201 | assert query.available_filters["member_of_group"][:values].present? |
|
1187 | 1202 | assert_equal Group.all.sort.map {|g| [g.name, g.id.to_s]}, |
|
1188 | 1203 | query.available_filters["member_of_group"][:values].sort |
|
1189 | 1204 | end |
|
1190 | 1205 | |
|
1191 | 1206 | test "#available_filters should include 'assigned_to_role' filter" do |
|
1192 | 1207 | query = IssueQuery.new |
|
1193 | 1208 | assert query.available_filters.keys.include?("assigned_to_role") |
|
1194 | 1209 | assert_equal :list_optional, query.available_filters["assigned_to_role"][:type] |
|
1195 | 1210 | |
|
1196 | 1211 | assert query.available_filters["assigned_to_role"][:values].include?(['Manager','1']) |
|
1197 | 1212 | assert query.available_filters["assigned_to_role"][:values].include?(['Developer','2']) |
|
1198 | 1213 | assert query.available_filters["assigned_to_role"][:values].include?(['Reporter','3']) |
|
1199 | 1214 | |
|
1200 | 1215 | assert ! query.available_filters["assigned_to_role"][:values].include?(['Non member','4']) |
|
1201 | 1216 | assert ! query.available_filters["assigned_to_role"][:values].include?(['Anonymous','5']) |
|
1202 | 1217 | end |
|
1203 | 1218 | |
|
1204 | 1219 | def test_available_filters_should_include_custom_field_according_to_user_visibility |
|
1205 | 1220 | visible_field = IssueCustomField.generate!(:is_for_all => true, :is_filter => true, :visible => true) |
|
1206 | 1221 | hidden_field = IssueCustomField.generate!(:is_for_all => true, :is_filter => true, :visible => false, :role_ids => [1]) |
|
1207 | 1222 | |
|
1208 | 1223 | with_current_user User.find(3) do |
|
1209 | 1224 | query = IssueQuery.new |
|
1210 | 1225 | assert_include "cf_#{visible_field.id}", query.available_filters.keys |
|
1211 | 1226 | assert_not_include "cf_#{hidden_field.id}", query.available_filters.keys |
|
1212 | 1227 | end |
|
1213 | 1228 | end |
|
1214 | 1229 | |
|
1215 | 1230 | def test_available_columns_should_include_custom_field_according_to_user_visibility |
|
1216 | 1231 | visible_field = IssueCustomField.generate!(:is_for_all => true, :is_filter => true, :visible => true) |
|
1217 | 1232 | hidden_field = IssueCustomField.generate!(:is_for_all => true, :is_filter => true, :visible => false, :role_ids => [1]) |
|
1218 | 1233 | |
|
1219 | 1234 | with_current_user User.find(3) do |
|
1220 | 1235 | query = IssueQuery.new |
|
1221 | 1236 | assert_include :"cf_#{visible_field.id}", query.available_columns.map(&:name) |
|
1222 | 1237 | assert_not_include :"cf_#{hidden_field.id}", query.available_columns.map(&:name) |
|
1223 | 1238 | end |
|
1224 | 1239 | end |
|
1225 | 1240 | |
|
1226 | 1241 | context "#statement" do |
|
1227 | 1242 | context "with 'member_of_group' filter" do |
|
1228 | 1243 | setup do |
|
1229 | 1244 | Group.destroy_all # No fixtures |
|
1230 | 1245 | @user_in_group = User.generate! |
|
1231 | 1246 | @second_user_in_group = User.generate! |
|
1232 | 1247 | @user_in_group2 = User.generate! |
|
1233 | 1248 | @user_not_in_group = User.generate! |
|
1234 | 1249 | |
|
1235 | 1250 | @group = Group.generate!.reload |
|
1236 | 1251 | @group.users << @user_in_group |
|
1237 | 1252 | @group.users << @second_user_in_group |
|
1238 | 1253 | |
|
1239 | 1254 | @group2 = Group.generate!.reload |
|
1240 | 1255 | @group2.users << @user_in_group2 |
|
1241 | 1256 | |
|
1242 | 1257 | end |
|
1243 | 1258 | |
|
1244 | 1259 | should "search assigned to for users in the group" do |
|
1245 | 1260 | @query = IssueQuery.new(:name => '_') |
|
1246 | 1261 | @query.add_filter('member_of_group', '=', [@group.id.to_s]) |
|
1247 | 1262 | |
|
1248 | 1263 | assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@user_in_group.id}','#{@second_user_in_group.id}','#{@group.id}')" |
|
1249 | 1264 | assert_find_issues_with_query_is_successful @query |
|
1250 | 1265 | end |
|
1251 | 1266 | |
|
1252 | 1267 | should "search not assigned to any group member (none)" do |
|
1253 | 1268 | @query = IssueQuery.new(:name => '_') |
|
1254 | 1269 | @query.add_filter('member_of_group', '!*', ['']) |
|
1255 | 1270 | |
|
1256 | 1271 | # Users not in a group |
|
1257 | 1272 | assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IS NULL OR #{Issue.table_name}.assigned_to_id NOT IN ('#{@user_in_group.id}','#{@second_user_in_group.id}','#{@user_in_group2.id}','#{@group.id}','#{@group2.id}')" |
|
1258 | 1273 | assert_find_issues_with_query_is_successful @query |
|
1259 | 1274 | end |
|
1260 | 1275 | |
|
1261 | 1276 | should "search assigned to any group member (all)" do |
|
1262 | 1277 | @query = IssueQuery.new(:name => '_') |
|
1263 | 1278 | @query.add_filter('member_of_group', '*', ['']) |
|
1264 | 1279 | |
|
1265 | 1280 | # Only users in a group |
|
1266 | 1281 | assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@user_in_group.id}','#{@second_user_in_group.id}','#{@user_in_group2.id}','#{@group.id}','#{@group2.id}')" |
|
1267 | 1282 | assert_find_issues_with_query_is_successful @query |
|
1268 | 1283 | end |
|
1269 | 1284 | |
|
1270 | 1285 | should "return an empty set with = empty group" do |
|
1271 | 1286 | @empty_group = Group.generate! |
|
1272 | 1287 | @query = IssueQuery.new(:name => '_') |
|
1273 | 1288 | @query.add_filter('member_of_group', '=', [@empty_group.id.to_s]) |
|
1274 | 1289 | |
|
1275 | 1290 | assert_equal [], find_issues_with_query(@query) |
|
1276 | 1291 | end |
|
1277 | 1292 | |
|
1278 | 1293 | should "return issues with ! empty group" do |
|
1279 | 1294 | @empty_group = Group.generate! |
|
1280 | 1295 | @query = IssueQuery.new(:name => '_') |
|
1281 | 1296 | @query.add_filter('member_of_group', '!', [@empty_group.id.to_s]) |
|
1282 | 1297 | |
|
1283 | 1298 | assert_find_issues_with_query_is_successful @query |
|
1284 | 1299 | end |
|
1285 | 1300 | end |
|
1286 | 1301 | |
|
1287 | 1302 | context "with 'assigned_to_role' filter" do |
|
1288 | 1303 | setup do |
|
1289 | 1304 | @manager_role = Role.find_by_name('Manager') |
|
1290 | 1305 | @developer_role = Role.find_by_name('Developer') |
|
1291 | 1306 | |
|
1292 | 1307 | @project = Project.generate! |
|
1293 | 1308 | @manager = User.generate! |
|
1294 | 1309 | @developer = User.generate! |
|
1295 | 1310 | @boss = User.generate! |
|
1296 | 1311 | @guest = User.generate! |
|
1297 | 1312 | User.add_to_project(@manager, @project, @manager_role) |
|
1298 | 1313 | User.add_to_project(@developer, @project, @developer_role) |
|
1299 | 1314 | User.add_to_project(@boss, @project, [@manager_role, @developer_role]) |
|
1300 | 1315 | |
|
1301 | 1316 | @issue1 = Issue.generate!(:project => @project, :assigned_to_id => @manager.id) |
|
1302 | 1317 | @issue2 = Issue.generate!(:project => @project, :assigned_to_id => @developer.id) |
|
1303 | 1318 | @issue3 = Issue.generate!(:project => @project, :assigned_to_id => @boss.id) |
|
1304 | 1319 | @issue4 = Issue.generate!(:project => @project, :assigned_to_id => @guest.id) |
|
1305 | 1320 | @issue5 = Issue.generate!(:project => @project) |
|
1306 | 1321 | end |
|
1307 | 1322 | |
|
1308 | 1323 | should "search assigned to for users with the Role" do |
|
1309 | 1324 | @query = IssueQuery.new(:name => '_', :project => @project) |
|
1310 | 1325 | @query.add_filter('assigned_to_role', '=', [@manager_role.id.to_s]) |
|
1311 | 1326 | |
|
1312 | 1327 | assert_query_result [@issue1, @issue3], @query |
|
1313 | 1328 | end |
|
1314 | 1329 | |
|
1315 | 1330 | should "search assigned to for users with the Role on the issue project" do |
|
1316 | 1331 | other_project = Project.generate! |
|
1317 | 1332 | User.add_to_project(@developer, other_project, @manager_role) |
|
1318 | 1333 | |
|
1319 | 1334 | @query = IssueQuery.new(:name => '_', :project => @project) |
|
1320 | 1335 | @query.add_filter('assigned_to_role', '=', [@manager_role.id.to_s]) |
|
1321 | 1336 | |
|
1322 | 1337 | assert_query_result [@issue1, @issue3], @query |
|
1323 | 1338 | end |
|
1324 | 1339 | |
|
1325 | 1340 | should "return an empty set with empty role" do |
|
1326 | 1341 | @empty_role = Role.generate! |
|
1327 | 1342 | @query = IssueQuery.new(:name => '_', :project => @project) |
|
1328 | 1343 | @query.add_filter('assigned_to_role', '=', [@empty_role.id.to_s]) |
|
1329 | 1344 | |
|
1330 | 1345 | assert_query_result [], @query |
|
1331 | 1346 | end |
|
1332 | 1347 | |
|
1333 | 1348 | should "search assigned to for users without the Role" do |
|
1334 | 1349 | @query = IssueQuery.new(:name => '_', :project => @project) |
|
1335 | 1350 | @query.add_filter('assigned_to_role', '!', [@manager_role.id.to_s]) |
|
1336 | 1351 | |
|
1337 | 1352 | assert_query_result [@issue2, @issue4, @issue5], @query |
|
1338 | 1353 | end |
|
1339 | 1354 | |
|
1340 | 1355 | should "search assigned to for users not assigned to any Role (none)" do |
|
1341 | 1356 | @query = IssueQuery.new(:name => '_', :project => @project) |
|
1342 | 1357 | @query.add_filter('assigned_to_role', '!*', ['']) |
|
1343 | 1358 | |
|
1344 | 1359 | assert_query_result [@issue4, @issue5], @query |
|
1345 | 1360 | end |
|
1346 | 1361 | |
|
1347 | 1362 | should "search assigned to for users assigned to any Role (all)" do |
|
1348 | 1363 | @query = IssueQuery.new(:name => '_', :project => @project) |
|
1349 | 1364 | @query.add_filter('assigned_to_role', '*', ['']) |
|
1350 | 1365 | |
|
1351 | 1366 | assert_query_result [@issue1, @issue2, @issue3], @query |
|
1352 | 1367 | end |
|
1353 | 1368 | |
|
1354 | 1369 | should "return issues with ! empty role" do |
|
1355 | 1370 | @empty_role = Role.generate! |
|
1356 | 1371 | @query = IssueQuery.new(:name => '_', :project => @project) |
|
1357 | 1372 | @query.add_filter('assigned_to_role', '!', [@empty_role.id.to_s]) |
|
1358 | 1373 | |
|
1359 | 1374 | assert_query_result [@issue1, @issue2, @issue3, @issue4, @issue5], @query |
|
1360 | 1375 | end |
|
1361 | 1376 | end |
|
1362 | 1377 | end |
|
1363 | 1378 | end |
General Comments 0
You need to be logged in to leave comments.
Login now