@@ -1,871 +1,871 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 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 QueryColumn |
|
19 | 19 | attr_accessor :name, :sortable, :groupable, :default_order |
|
20 | 20 | include Redmine::I18n |
|
21 | 21 | |
|
22 | 22 | def initialize(name, options={}) |
|
23 | 23 | self.name = name |
|
24 | 24 | self.sortable = options[:sortable] |
|
25 | 25 | self.groupable = options[:groupable] || false |
|
26 | 26 | if groupable == true |
|
27 | 27 | self.groupable = name.to_s |
|
28 | 28 | end |
|
29 | 29 | self.default_order = options[:default_order] |
|
30 | 30 | @caption_key = options[:caption] || "field_#{name}" |
|
31 | 31 | end |
|
32 | 32 | |
|
33 | 33 | def caption |
|
34 | 34 | l(@caption_key) |
|
35 | 35 | end |
|
36 | 36 | |
|
37 | 37 | # Returns true if the column is sortable, otherwise false |
|
38 | 38 | def sortable? |
|
39 | 39 | !@sortable.nil? |
|
40 | 40 | end |
|
41 | 41 | |
|
42 | 42 | def sortable |
|
43 | 43 | @sortable.is_a?(Proc) ? @sortable.call : @sortable |
|
44 | 44 | end |
|
45 | 45 | |
|
46 | 46 | def value(issue) |
|
47 | 47 | issue.send name |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | def css_classes |
|
51 | 51 | name |
|
52 | 52 | end |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | class QueryCustomFieldColumn < QueryColumn |
|
56 | 56 | |
|
57 | 57 | def initialize(custom_field) |
|
58 | 58 | self.name = "cf_#{custom_field.id}".to_sym |
|
59 | 59 | self.sortable = custom_field.order_statement || false |
|
60 | 60 | if %w(list date bool int).include?(custom_field.field_format) && !custom_field.multiple? |
|
61 | 61 | self.groupable = custom_field.order_statement |
|
62 | 62 | end |
|
63 | 63 | self.groupable ||= false |
|
64 | 64 | @cf = custom_field |
|
65 | 65 | end |
|
66 | 66 | |
|
67 | 67 | def caption |
|
68 | 68 | @cf.name |
|
69 | 69 | end |
|
70 | 70 | |
|
71 | 71 | def custom_field |
|
72 | 72 | @cf |
|
73 | 73 | end |
|
74 | 74 | |
|
75 | 75 | def value(issue) |
|
76 | 76 | cv = issue.custom_values.select {|v| v.custom_field_id == @cf.id}.collect {|v| @cf.cast_value(v.value)} |
|
77 | 77 | cv.size > 1 ? cv : cv.first |
|
78 | 78 | end |
|
79 | 79 | |
|
80 | 80 | def css_classes |
|
81 | 81 | @css_classes ||= "#{name} #{@cf.field_format}" |
|
82 | 82 | end |
|
83 | 83 | end |
|
84 | 84 | |
|
85 | 85 | class Query < ActiveRecord::Base |
|
86 | 86 | class StatementInvalid < ::ActiveRecord::StatementInvalid |
|
87 | 87 | end |
|
88 | 88 | |
|
89 | 89 | belongs_to :project |
|
90 | 90 | belongs_to :user |
|
91 | 91 | serialize :filters |
|
92 | 92 | serialize :column_names |
|
93 | 93 | serialize :sort_criteria, Array |
|
94 | 94 | |
|
95 | 95 | attr_protected :project_id, :user_id |
|
96 | 96 | |
|
97 | 97 | validates_presence_of :name |
|
98 | 98 | validates_length_of :name, :maximum => 255 |
|
99 | 99 | validate :validate_query_filters |
|
100 | 100 | |
|
101 | 101 | @@operators = { "=" => :label_equals, |
|
102 | 102 | "!" => :label_not_equals, |
|
103 | 103 | "o" => :label_open_issues, |
|
104 | 104 | "c" => :label_closed_issues, |
|
105 | 105 | "!*" => :label_none, |
|
106 | 106 | "*" => :label_all, |
|
107 | 107 | ">=" => :label_greater_or_equal, |
|
108 | 108 | "<=" => :label_less_or_equal, |
|
109 | 109 | "><" => :label_between, |
|
110 | 110 | "<t+" => :label_in_less_than, |
|
111 | 111 | ">t+" => :label_in_more_than, |
|
112 | 112 | "t+" => :label_in, |
|
113 | 113 | "t" => :label_today, |
|
114 | 114 | "w" => :label_this_week, |
|
115 | 115 | ">t-" => :label_less_than_ago, |
|
116 | 116 | "<t-" => :label_more_than_ago, |
|
117 | 117 | "t-" => :label_ago, |
|
118 | 118 | "~" => :label_contains, |
|
119 | 119 | "!~" => :label_not_contains } |
|
120 | 120 | |
|
121 | 121 | cattr_reader :operators |
|
122 | 122 | |
|
123 | 123 | @@operators_by_filter_type = { :list => [ "=", "!" ], |
|
124 | 124 | :list_status => [ "o", "=", "!", "c", "*" ], |
|
125 | 125 | :list_optional => [ "=", "!", "!*", "*" ], |
|
126 | 126 | :list_subprojects => [ "*", "!*", "=" ], |
|
127 | 127 | :date => [ "=", ">=", "<=", "><", "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-", "!*", "*" ], |
|
128 | 128 | :date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "t-", "t", "w", "!*", "*" ], |
|
129 | 129 | :string => [ "=", "~", "!", "!~", "!*", "*" ], |
|
130 | 130 | :text => [ "~", "!~", "!*", "*" ], |
|
131 | 131 | :integer => [ "=", ">=", "<=", "><", "!*", "*" ], |
|
132 | 132 | :float => [ "=", ">=", "<=", "><", "!*", "*" ] } |
|
133 | 133 | |
|
134 | 134 | cattr_reader :operators_by_filter_type |
|
135 | 135 | |
|
136 | 136 | @@available_columns = [ |
|
137 | 137 | QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true), |
|
138 | 138 | QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true), |
|
139 | 139 | QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue), |
|
140 | 140 | QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true), |
|
141 | 141 | QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true), |
|
142 | 142 | QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"), |
|
143 | 143 | QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true), |
|
144 | 144 | QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true), |
|
145 | 145 | QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'), |
|
146 | 146 | QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true), |
|
147 | 147 | QueryColumn.new(:fixed_version, :sortable => ["#{Version.table_name}.effective_date", "#{Version.table_name}.name"], :default_order => 'desc', :groupable => true), |
|
148 | 148 | QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"), |
|
149 | 149 | QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"), |
|
150 | 150 | QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"), |
|
151 | 151 | QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true), |
|
152 | 152 | QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'), |
|
153 | 153 | ] |
|
154 | 154 | cattr_reader :available_columns |
|
155 | 155 | |
|
156 | 156 | scope :visible, lambda {|*args| |
|
157 | 157 | user = args.shift || User.current |
|
158 | 158 | base = Project.allowed_to_condition(user, :view_issues, *args) |
|
159 | 159 | user_id = user.logged? ? user.id : 0 |
|
160 | 160 | { |
|
161 | 161 | :conditions => ["(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id], |
|
162 | 162 | :include => :project |
|
163 | 163 | } |
|
164 | 164 | } |
|
165 | 165 | |
|
166 | 166 | def initialize(attributes=nil, *args) |
|
167 | 167 | super attributes |
|
168 | 168 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } |
|
169 | 169 | @is_for_all = project.nil? |
|
170 | 170 | end |
|
171 | 171 | |
|
172 | 172 | def validate_query_filters |
|
173 | 173 | filters.each_key do |field| |
|
174 | 174 | if values_for(field) |
|
175 | 175 | case type_for(field) |
|
176 | 176 | when :integer |
|
177 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) } | |
|
177 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+$/) } | |
|
178 | 178 | when :float |
|
179 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+(\.\d*)?$/) } | |
|
179 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+(\.\d*)?$/) } | |
|
180 | 180 | when :date, :date_past |
|
181 | 181 | case operator_for(field) |
|
182 | 182 | when "=", ">=", "<=", "><" |
|
183 | 183 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && (!v.match(/^\d{4}-\d{2}-\d{2}$/) || (Date.parse(v) rescue nil).nil?) } |
|
184 | 184 | when ">t-", "<t-", "t-" |
|
185 | 185 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) } |
|
186 | 186 | end |
|
187 | 187 | end |
|
188 | 188 | end |
|
189 | 189 | |
|
190 | 190 | add_filter_error(field, :blank) unless |
|
191 | 191 | # filter requires one or more values |
|
192 | 192 | (values_for(field) and !values_for(field).first.blank?) or |
|
193 | 193 | # filter doesn't require any value |
|
194 | 194 | ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) |
|
195 | 195 | end if filters |
|
196 | 196 | end |
|
197 | 197 | |
|
198 | 198 | def add_filter_error(field, message) |
|
199 | 199 | m = label_for(field) + " " + l(message, :scope => 'activerecord.errors.messages') |
|
200 | 200 | errors.add(:base, m) |
|
201 | 201 | end |
|
202 | 202 | |
|
203 | 203 | # Returns true if the query is visible to +user+ or the current user. |
|
204 | 204 | def visible?(user=User.current) |
|
205 | 205 | (project.nil? || user.allowed_to?(:view_issues, project)) && (self.is_public? || self.user_id == user.id) |
|
206 | 206 | end |
|
207 | 207 | |
|
208 | 208 | def editable_by?(user) |
|
209 | 209 | return false unless user |
|
210 | 210 | # Admin can edit them all and regular users can edit their private queries |
|
211 | 211 | return true if user.admin? || (!is_public && self.user_id == user.id) |
|
212 | 212 | # Members can not edit public queries that are for all project (only admin is allowed to) |
|
213 | 213 | is_public && !@is_for_all && user.allowed_to?(:manage_public_queries, project) |
|
214 | 214 | end |
|
215 | 215 | |
|
216 | 216 | def available_filters |
|
217 | 217 | return @available_filters if @available_filters |
|
218 | 218 | |
|
219 | 219 | trackers = project.nil? ? Tracker.find(:all, :order => 'position') : project.rolled_up_trackers |
|
220 | 220 | |
|
221 | 221 | @available_filters = { "status_id" => { :type => :list_status, :order => 1, :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } }, |
|
222 | 222 | "tracker_id" => { :type => :list, :order => 2, :values => trackers.collect{|s| [s.name, s.id.to_s] } }, |
|
223 | 223 | "priority_id" => { :type => :list, :order => 3, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] } }, |
|
224 | 224 | "subject" => { :type => :text, :order => 8 }, |
|
225 | 225 | "created_on" => { :type => :date_past, :order => 9 }, |
|
226 | 226 | "updated_on" => { :type => :date_past, :order => 10 }, |
|
227 | 227 | "start_date" => { :type => :date, :order => 11 }, |
|
228 | 228 | "due_date" => { :type => :date, :order => 12 }, |
|
229 | 229 | "estimated_hours" => { :type => :float, :order => 13 }, |
|
230 | 230 | "done_ratio" => { :type => :integer, :order => 14 }} |
|
231 | 231 | |
|
232 | 232 | principals = [] |
|
233 | 233 | if project |
|
234 | 234 | principals += project.principals.sort |
|
235 | 235 | unless project.leaf? |
|
236 | 236 | subprojects = project.descendants.visible.all |
|
237 | 237 | if subprojects.any? |
|
238 | 238 | @available_filters["subproject_id"] = { :type => :list_subprojects, :order => 13, :values => subprojects.collect{|s| [s.name, s.id.to_s] } } |
|
239 | 239 | principals += Principal.member_of(subprojects) |
|
240 | 240 | end |
|
241 | 241 | end |
|
242 | 242 | else |
|
243 | 243 | all_projects = Project.visible.all |
|
244 | 244 | if all_projects.any? |
|
245 | 245 | # members of visible projects |
|
246 | 246 | principals += Principal.member_of(all_projects) |
|
247 | 247 | |
|
248 | 248 | # project filter |
|
249 | 249 | project_values = [] |
|
250 | 250 | if User.current.logged? && User.current.memberships.any? |
|
251 | 251 | project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"] |
|
252 | 252 | end |
|
253 | 253 | Project.project_tree(all_projects) do |p, level| |
|
254 | 254 | prefix = (level > 0 ? ('--' * level + ' ') : '') |
|
255 | 255 | project_values << ["#{prefix}#{p.name}", p.id.to_s] |
|
256 | 256 | end |
|
257 | 257 | @available_filters["project_id"] = { :type => :list, :order => 1, :values => project_values} unless project_values.empty? |
|
258 | 258 | end |
|
259 | 259 | end |
|
260 | 260 | principals.uniq! |
|
261 | 261 | principals.sort! |
|
262 | 262 | users = principals.select {|p| p.is_a?(User)} |
|
263 | 263 | |
|
264 | 264 | assigned_to_values = [] |
|
265 | 265 | assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
266 | 266 | assigned_to_values += (Setting.issue_group_assignment? ? principals : users).collect{|s| [s.name, s.id.to_s] } |
|
267 | 267 | @available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => assigned_to_values } unless assigned_to_values.empty? |
|
268 | 268 | |
|
269 | 269 | author_values = [] |
|
270 | 270 | author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
271 | 271 | author_values += users.collect{|s| [s.name, s.id.to_s] } |
|
272 | 272 | @available_filters["author_id"] = { :type => :list, :order => 5, :values => author_values } unless author_values.empty? |
|
273 | 273 | |
|
274 | 274 | group_values = Group.all.collect {|g| [g.name, g.id.to_s] } |
|
275 | 275 | @available_filters["member_of_group"] = { :type => :list_optional, :order => 6, :values => group_values } unless group_values.empty? |
|
276 | 276 | |
|
277 | 277 | role_values = Role.givable.collect {|r| [r.name, r.id.to_s] } |
|
278 | 278 | @available_filters["assigned_to_role"] = { :type => :list_optional, :order => 7, :values => role_values } unless role_values.empty? |
|
279 | 279 | |
|
280 | 280 | if User.current.logged? |
|
281 | 281 | @available_filters["watcher_id"] = { :type => :list, :order => 15, :values => [["<< #{l(:label_me)} >>", "me"]] } |
|
282 | 282 | end |
|
283 | 283 | |
|
284 | 284 | if project |
|
285 | 285 | # project specific filters |
|
286 | 286 | categories = project.issue_categories.all |
|
287 | 287 | unless categories.empty? |
|
288 | 288 | @available_filters["category_id"] = { :type => :list_optional, :order => 6, :values => categories.collect{|s| [s.name, s.id.to_s] } } |
|
289 | 289 | end |
|
290 | 290 | versions = project.shared_versions.all |
|
291 | 291 | unless versions.empty? |
|
292 | 292 | @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } } |
|
293 | 293 | end |
|
294 | 294 | add_custom_fields_filters(project.all_issue_custom_fields) |
|
295 | 295 | else |
|
296 | 296 | # global filters for cross project issue list |
|
297 | 297 | system_shared_versions = Version.visible.find_all_by_sharing('system') |
|
298 | 298 | unless system_shared_versions.empty? |
|
299 | 299 | @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => system_shared_versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } } |
|
300 | 300 | end |
|
301 | 301 | add_custom_fields_filters(IssueCustomField.find(:all, :conditions => {:is_filter => true, :is_for_all => true})) |
|
302 | 302 | end |
|
303 | 303 | @available_filters |
|
304 | 304 | end |
|
305 | 305 | |
|
306 | 306 | def add_filter(field, operator, values) |
|
307 | 307 | # values must be an array |
|
308 | 308 | return unless values.nil? || values.is_a?(Array) |
|
309 | 309 | # check if field is defined as an available filter |
|
310 | 310 | if available_filters.has_key? field |
|
311 | 311 | filter_options = available_filters[field] |
|
312 | 312 | # check if operator is allowed for that filter |
|
313 | 313 | #if @@operators_by_filter_type[filter_options[:type]].include? operator |
|
314 | 314 | # allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]}) |
|
315 | 315 | # filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator |
|
316 | 316 | #end |
|
317 | 317 | filters[field] = {:operator => operator, :values => (values || [''])} |
|
318 | 318 | end |
|
319 | 319 | end |
|
320 | 320 | |
|
321 | 321 | def add_short_filter(field, expression) |
|
322 | 322 | return unless expression && available_filters.has_key?(field) |
|
323 | 323 | field_type = available_filters[field][:type] |
|
324 | 324 | @@operators_by_filter_type[field_type].sort.reverse.detect do |operator| |
|
325 | 325 | next unless expression =~ /^#{Regexp.escape(operator)}(.*)$/ |
|
326 | 326 | add_filter field, operator, $1.present? ? $1.split('|') : [''] |
|
327 | 327 | end || add_filter(field, '=', expression.split('|')) |
|
328 | 328 | end |
|
329 | 329 | |
|
330 | 330 | # Add multiple filters using +add_filter+ |
|
331 | 331 | def add_filters(fields, operators, values) |
|
332 | 332 | if fields.is_a?(Array) && operators.is_a?(Hash) && (values.nil? || values.is_a?(Hash)) |
|
333 | 333 | fields.each do |field| |
|
334 | 334 | add_filter(field, operators[field], values && values[field]) |
|
335 | 335 | end |
|
336 | 336 | end |
|
337 | 337 | end |
|
338 | 338 | |
|
339 | 339 | def has_filter?(field) |
|
340 | 340 | filters and filters[field] |
|
341 | 341 | end |
|
342 | 342 | |
|
343 | 343 | def type_for(field) |
|
344 | 344 | available_filters[field][:type] if available_filters.has_key?(field) |
|
345 | 345 | end |
|
346 | 346 | |
|
347 | 347 | def operator_for(field) |
|
348 | 348 | has_filter?(field) ? filters[field][:operator] : nil |
|
349 | 349 | end |
|
350 | 350 | |
|
351 | 351 | def values_for(field) |
|
352 | 352 | has_filter?(field) ? filters[field][:values] : nil |
|
353 | 353 | end |
|
354 | 354 | |
|
355 | 355 | def value_for(field, index=0) |
|
356 | 356 | (values_for(field) || [])[index] |
|
357 | 357 | end |
|
358 | 358 | |
|
359 | 359 | def label_for(field) |
|
360 | 360 | label = available_filters[field][:name] if available_filters.has_key?(field) |
|
361 | 361 | label ||= l("field_#{field.to_s.gsub(/_id$/, '')}", :default => field) |
|
362 | 362 | end |
|
363 | 363 | |
|
364 | 364 | def available_columns |
|
365 | 365 | return @available_columns if @available_columns |
|
366 | 366 | @available_columns = ::Query.available_columns.dup |
|
367 | 367 | @available_columns += (project ? |
|
368 | 368 | project.all_issue_custom_fields : |
|
369 | 369 | IssueCustomField.find(:all) |
|
370 | 370 | ).collect {|cf| QueryCustomFieldColumn.new(cf) } |
|
371 | 371 | |
|
372 | 372 | if User.current.allowed_to?(:view_time_entries, project, :global => true) |
|
373 | 373 | index = nil |
|
374 | 374 | @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours} |
|
375 | 375 | index = (index ? index + 1 : -1) |
|
376 | 376 | # insert the column after estimated_hours or at the end |
|
377 | 377 | @available_columns.insert index, QueryColumn.new(:spent_hours, |
|
378 | 378 | :sortable => "(SELECT COALESCE(SUM(hours), 0) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id)", |
|
379 | 379 | :default_order => 'desc', |
|
380 | 380 | :caption => :label_spent_time |
|
381 | 381 | ) |
|
382 | 382 | end |
|
383 | 383 | @available_columns |
|
384 | 384 | end |
|
385 | 385 | |
|
386 | 386 | def self.available_columns=(v) |
|
387 | 387 | self.available_columns = (v) |
|
388 | 388 | end |
|
389 | 389 | |
|
390 | 390 | def self.add_available_column(column) |
|
391 | 391 | self.available_columns << (column) if column.is_a?(QueryColumn) |
|
392 | 392 | end |
|
393 | 393 | |
|
394 | 394 | # Returns an array of columns that can be used to group the results |
|
395 | 395 | def groupable_columns |
|
396 | 396 | available_columns.select {|c| c.groupable} |
|
397 | 397 | end |
|
398 | 398 | |
|
399 | 399 | # Returns a Hash of columns and the key for sorting |
|
400 | 400 | def sortable_columns |
|
401 | 401 | {'id' => "#{Issue.table_name}.id"}.merge(available_columns.inject({}) {|h, column| |
|
402 | 402 | h[column.name.to_s] = column.sortable |
|
403 | 403 | h |
|
404 | 404 | }) |
|
405 | 405 | end |
|
406 | 406 | |
|
407 | 407 | def columns |
|
408 | 408 | # preserve the column_names order |
|
409 | 409 | (has_default_columns? ? default_columns_names : column_names).collect do |name| |
|
410 | 410 | available_columns.find { |col| col.name == name } |
|
411 | 411 | end.compact |
|
412 | 412 | end |
|
413 | 413 | |
|
414 | 414 | def default_columns_names |
|
415 | 415 | @default_columns_names ||= begin |
|
416 | 416 | default_columns = Setting.issue_list_default_columns.map(&:to_sym) |
|
417 | 417 | |
|
418 | 418 | project.present? ? default_columns : [:project] | default_columns |
|
419 | 419 | end |
|
420 | 420 | end |
|
421 | 421 | |
|
422 | 422 | def column_names=(names) |
|
423 | 423 | if names |
|
424 | 424 | names = names.select {|n| n.is_a?(Symbol) || !n.blank? } |
|
425 | 425 | names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym } |
|
426 | 426 | # Set column_names to nil if default columns |
|
427 | 427 | if names == default_columns_names |
|
428 | 428 | names = nil |
|
429 | 429 | end |
|
430 | 430 | end |
|
431 | 431 | write_attribute(:column_names, names) |
|
432 | 432 | end |
|
433 | 433 | |
|
434 | 434 | def has_column?(column) |
|
435 | 435 | column_names && column_names.include?(column.is_a?(QueryColumn) ? column.name : column) |
|
436 | 436 | end |
|
437 | 437 | |
|
438 | 438 | def has_default_columns? |
|
439 | 439 | column_names.nil? || column_names.empty? |
|
440 | 440 | end |
|
441 | 441 | |
|
442 | 442 | def sort_criteria=(arg) |
|
443 | 443 | c = [] |
|
444 | 444 | if arg.is_a?(Hash) |
|
445 | 445 | arg = arg.keys.sort.collect {|k| arg[k]} |
|
446 | 446 | end |
|
447 | 447 | c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, o == 'desc' ? o : 'asc']} |
|
448 | 448 | write_attribute(:sort_criteria, c) |
|
449 | 449 | end |
|
450 | 450 | |
|
451 | 451 | def sort_criteria |
|
452 | 452 | read_attribute(:sort_criteria) || [] |
|
453 | 453 | end |
|
454 | 454 | |
|
455 | 455 | def sort_criteria_key(arg) |
|
456 | 456 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].first |
|
457 | 457 | end |
|
458 | 458 | |
|
459 | 459 | def sort_criteria_order(arg) |
|
460 | 460 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].last |
|
461 | 461 | end |
|
462 | 462 | |
|
463 | 463 | # Returns the SQL sort order that should be prepended for grouping |
|
464 | 464 | def group_by_sort_order |
|
465 | 465 | if grouped? && (column = group_by_column) |
|
466 | 466 | column.sortable.is_a?(Array) ? |
|
467 | 467 | column.sortable.collect {|s| "#{s} #{column.default_order}"}.join(',') : |
|
468 | 468 | "#{column.sortable} #{column.default_order}" |
|
469 | 469 | end |
|
470 | 470 | end |
|
471 | 471 | |
|
472 | 472 | # Returns true if the query is a grouped query |
|
473 | 473 | def grouped? |
|
474 | 474 | !group_by_column.nil? |
|
475 | 475 | end |
|
476 | 476 | |
|
477 | 477 | def group_by_column |
|
478 | 478 | groupable_columns.detect {|c| c.groupable && c.name.to_s == group_by} |
|
479 | 479 | end |
|
480 | 480 | |
|
481 | 481 | def group_by_statement |
|
482 | 482 | group_by_column.try(:groupable) |
|
483 | 483 | end |
|
484 | 484 | |
|
485 | 485 | def project_statement |
|
486 | 486 | project_clauses = [] |
|
487 | 487 | if project && !project.descendants.active.empty? |
|
488 | 488 | ids = [project.id] |
|
489 | 489 | if has_filter?("subproject_id") |
|
490 | 490 | case operator_for("subproject_id") |
|
491 | 491 | when '=' |
|
492 | 492 | # include the selected subprojects |
|
493 | 493 | ids += values_for("subproject_id").each(&:to_i) |
|
494 | 494 | when '!*' |
|
495 | 495 | # main project only |
|
496 | 496 | else |
|
497 | 497 | # all subprojects |
|
498 | 498 | ids += project.descendants.collect(&:id) |
|
499 | 499 | end |
|
500 | 500 | elsif Setting.display_subprojects_issues? |
|
501 | 501 | ids += project.descendants.collect(&:id) |
|
502 | 502 | end |
|
503 | 503 | project_clauses << "#{Project.table_name}.id IN (%s)" % ids.join(',') |
|
504 | 504 | elsif project |
|
505 | 505 | project_clauses << "#{Project.table_name}.id = %d" % project.id |
|
506 | 506 | end |
|
507 | 507 | project_clauses.any? ? project_clauses.join(' AND ') : nil |
|
508 | 508 | end |
|
509 | 509 | |
|
510 | 510 | def statement |
|
511 | 511 | # filters clauses |
|
512 | 512 | filters_clauses = [] |
|
513 | 513 | filters.each_key do |field| |
|
514 | 514 | next if field == "subproject_id" |
|
515 | 515 | v = values_for(field).clone |
|
516 | 516 | next unless v and !v.empty? |
|
517 | 517 | operator = operator_for(field) |
|
518 | 518 | |
|
519 | 519 | # "me" value subsitution |
|
520 | 520 | if %w(assigned_to_id author_id watcher_id).include?(field) |
|
521 | 521 | if v.delete("me") |
|
522 | 522 | if User.current.logged? |
|
523 | 523 | v.push(User.current.id.to_s) |
|
524 | 524 | v += User.current.group_ids.map(&:to_s) if field == 'assigned_to_id' |
|
525 | 525 | else |
|
526 | 526 | v.push("0") |
|
527 | 527 | end |
|
528 | 528 | end |
|
529 | 529 | end |
|
530 | 530 | |
|
531 | 531 | if field == 'project_id' |
|
532 | 532 | if v.delete('mine') |
|
533 | 533 | v += User.current.memberships.map(&:project_id).map(&:to_s) |
|
534 | 534 | end |
|
535 | 535 | end |
|
536 | 536 | |
|
537 | 537 | if field =~ /^cf_(\d+)$/ |
|
538 | 538 | # custom field |
|
539 | 539 | filters_clauses << sql_for_custom_field(field, operator, v, $1) |
|
540 | 540 | elsif respond_to?("sql_for_#{field}_field") |
|
541 | 541 | # specific statement |
|
542 | 542 | filters_clauses << send("sql_for_#{field}_field", field, operator, v) |
|
543 | 543 | else |
|
544 | 544 | # regular field |
|
545 | 545 | filters_clauses << '(' + sql_for_field(field, operator, v, Issue.table_name, field) + ')' |
|
546 | 546 | end |
|
547 | 547 | end if filters and valid? |
|
548 | 548 | |
|
549 | 549 | filters_clauses << project_statement |
|
550 | 550 | filters_clauses.reject!(&:blank?) |
|
551 | 551 | |
|
552 | 552 | filters_clauses.any? ? filters_clauses.join(' AND ') : nil |
|
553 | 553 | end |
|
554 | 554 | |
|
555 | 555 | # Returns the issue count |
|
556 | 556 | def issue_count |
|
557 | 557 | Issue.visible.count(:include => [:status, :project], :conditions => statement) |
|
558 | 558 | rescue ::ActiveRecord::StatementInvalid => e |
|
559 | 559 | raise StatementInvalid.new(e.message) |
|
560 | 560 | end |
|
561 | 561 | |
|
562 | 562 | # Returns the issue count by group or nil if query is not grouped |
|
563 | 563 | def issue_count_by_group |
|
564 | 564 | r = nil |
|
565 | 565 | if grouped? |
|
566 | 566 | begin |
|
567 | 567 | # Rails3 will raise an (unexpected) RecordNotFound if there's only a nil group value |
|
568 | 568 | r = Issue.visible.count(:group => group_by_statement, :include => [:status, :project], :conditions => statement) |
|
569 | 569 | rescue ActiveRecord::RecordNotFound |
|
570 | 570 | r = {nil => issue_count} |
|
571 | 571 | end |
|
572 | 572 | c = group_by_column |
|
573 | 573 | if c.is_a?(QueryCustomFieldColumn) |
|
574 | 574 | r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h} |
|
575 | 575 | end |
|
576 | 576 | end |
|
577 | 577 | r |
|
578 | 578 | rescue ::ActiveRecord::StatementInvalid => e |
|
579 | 579 | raise StatementInvalid.new(e.message) |
|
580 | 580 | end |
|
581 | 581 | |
|
582 | 582 | # Returns the issues |
|
583 | 583 | # Valid options are :order, :offset, :limit, :include, :conditions |
|
584 | 584 | def issues(options={}) |
|
585 | 585 | order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') |
|
586 | 586 | order_option = nil if order_option.blank? |
|
587 | 587 | |
|
588 | 588 | joins = (order_option && order_option.include?('authors')) ? "LEFT OUTER JOIN users authors ON authors.id = #{Issue.table_name}.author_id" : nil |
|
589 | 589 | |
|
590 | 590 | issues = Issue.visible.scoped(:conditions => options[:conditions]).find :all, :include => ([:status, :project] + (options[:include] || [])).uniq, |
|
591 | 591 | :conditions => statement, |
|
592 | 592 | :order => order_option, |
|
593 | 593 | :joins => joins, |
|
594 | 594 | :limit => options[:limit], |
|
595 | 595 | :offset => options[:offset] |
|
596 | 596 | |
|
597 | 597 | if has_column?(:spent_hours) |
|
598 | 598 | Issue.load_visible_spent_hours(issues) |
|
599 | 599 | end |
|
600 | 600 | issues |
|
601 | 601 | rescue ::ActiveRecord::StatementInvalid => e |
|
602 | 602 | raise StatementInvalid.new(e.message) |
|
603 | 603 | end |
|
604 | 604 | |
|
605 | 605 | # Returns the issues ids |
|
606 | 606 | def issue_ids(options={}) |
|
607 | 607 | order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') |
|
608 | 608 | order_option = nil if order_option.blank? |
|
609 | 609 | |
|
610 | 610 | joins = (order_option && order_option.include?('authors')) ? "LEFT OUTER JOIN users authors ON authors.id = #{Issue.table_name}.author_id" : nil |
|
611 | 611 | |
|
612 | 612 | Issue.visible.scoped(:conditions => options[:conditions]).scoped(:include => ([:status, :project] + (options[:include] || [])).uniq, |
|
613 | 613 | :conditions => statement, |
|
614 | 614 | :order => order_option, |
|
615 | 615 | :joins => joins, |
|
616 | 616 | :limit => options[:limit], |
|
617 | 617 | :offset => options[:offset]).find_ids |
|
618 | 618 | rescue ::ActiveRecord::StatementInvalid => e |
|
619 | 619 | raise StatementInvalid.new(e.message) |
|
620 | 620 | end |
|
621 | 621 | |
|
622 | 622 | # Returns the journals |
|
623 | 623 | # Valid options are :order, :offset, :limit |
|
624 | 624 | def journals(options={}) |
|
625 | 625 | Journal.visible.find :all, :include => [:details, :user, {:issue => [:project, :author, :tracker, :status]}], |
|
626 | 626 | :conditions => statement, |
|
627 | 627 | :order => options[:order], |
|
628 | 628 | :limit => options[:limit], |
|
629 | 629 | :offset => options[:offset] |
|
630 | 630 | rescue ::ActiveRecord::StatementInvalid => e |
|
631 | 631 | raise StatementInvalid.new(e.message) |
|
632 | 632 | end |
|
633 | 633 | |
|
634 | 634 | # Returns the versions |
|
635 | 635 | # Valid options are :conditions |
|
636 | 636 | def versions(options={}) |
|
637 | 637 | Version.visible.scoped(:conditions => options[:conditions]).find :all, :include => :project, :conditions => project_statement |
|
638 | 638 | rescue ::ActiveRecord::StatementInvalid => e |
|
639 | 639 | raise StatementInvalid.new(e.message) |
|
640 | 640 | end |
|
641 | 641 | |
|
642 | 642 | def sql_for_watcher_id_field(field, operator, value) |
|
643 | 643 | db_table = Watcher.table_name |
|
644 | 644 | "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " + |
|
645 | 645 | sql_for_field(field, '=', value, db_table, 'user_id') + ')' |
|
646 | 646 | end |
|
647 | 647 | |
|
648 | 648 | def sql_for_member_of_group_field(field, operator, value) |
|
649 | 649 | if operator == '*' # Any group |
|
650 | 650 | groups = Group.all |
|
651 | 651 | operator = '=' # Override the operator since we want to find by assigned_to |
|
652 | 652 | elsif operator == "!*" |
|
653 | 653 | groups = Group.all |
|
654 | 654 | operator = '!' # Override the operator since we want to find by assigned_to |
|
655 | 655 | else |
|
656 | 656 | groups = Group.find_all_by_id(value) |
|
657 | 657 | end |
|
658 | 658 | groups ||= [] |
|
659 | 659 | |
|
660 | 660 | members_of_groups = groups.inject([]) {|user_ids, group| |
|
661 | 661 | if group && group.user_ids.present? |
|
662 | 662 | user_ids << group.user_ids |
|
663 | 663 | end |
|
664 | 664 | user_ids.flatten.uniq.compact |
|
665 | 665 | }.sort.collect(&:to_s) |
|
666 | 666 | |
|
667 | 667 | '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')' |
|
668 | 668 | end |
|
669 | 669 | |
|
670 | 670 | def sql_for_assigned_to_role_field(field, operator, value) |
|
671 | 671 | case operator |
|
672 | 672 | when "*", "!*" # Member / Not member |
|
673 | 673 | sw = operator == "!*" ? 'NOT' : '' |
|
674 | 674 | nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
675 | 675 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" + |
|
676 | 676 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))" |
|
677 | 677 | when "=", "!" |
|
678 | 678 | role_cond = value.any? ? |
|
679 | 679 | "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" : |
|
680 | 680 | "1=0" |
|
681 | 681 | |
|
682 | 682 | sw = operator == "!" ? 'NOT' : '' |
|
683 | 683 | nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
684 | 684 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" + |
|
685 | 685 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))" |
|
686 | 686 | end |
|
687 | 687 | end |
|
688 | 688 | |
|
689 | 689 | private |
|
690 | 690 | |
|
691 | 691 | def sql_for_custom_field(field, operator, value, custom_field_id) |
|
692 | 692 | db_table = CustomValue.table_name |
|
693 | 693 | db_field = 'value' |
|
694 | 694 | filter = @available_filters[field] |
|
695 | 695 | if filter && filter[:format] == 'user' |
|
696 | 696 | if value.delete('me') |
|
697 | 697 | value.push User.current.id.to_s |
|
698 | 698 | end |
|
699 | 699 | end |
|
700 | 700 | not_in = nil |
|
701 | 701 | if operator == '!' |
|
702 | 702 | # Makes ! operator work for custom fields with multiple values |
|
703 | 703 | operator = '=' |
|
704 | 704 | not_in = 'NOT' |
|
705 | 705 | end |
|
706 | 706 | "#{Issue.table_name}.id #{not_in} IN (SELECT #{Issue.table_name}.id FROM #{Issue.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{custom_field_id} WHERE " + |
|
707 | 707 | sql_for_field(field, operator, value, db_table, db_field, true) + ')' |
|
708 | 708 | end |
|
709 | 709 | |
|
710 | 710 | # Helper method to generate the WHERE sql for a +field+, +operator+ and a +value+ |
|
711 | 711 | def sql_for_field(field, operator, value, db_table, db_field, is_custom_filter=false) |
|
712 | 712 | sql = '' |
|
713 | 713 | case operator |
|
714 | 714 | when "=" |
|
715 | 715 | if value.any? |
|
716 | 716 | case type_for(field) |
|
717 | 717 | when :date, :date_past |
|
718 | 718 | sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), (Date.parse(value.first) rescue nil)) |
|
719 | 719 | when :integer |
|
720 | 720 | if is_custom_filter |
|
721 | 721 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) = #{value.first.to_i})" |
|
722 | 722 | else |
|
723 | 723 | sql = "#{db_table}.#{db_field} = #{value.first.to_i}" |
|
724 | 724 | end |
|
725 | 725 | when :float |
|
726 | 726 | if is_custom_filter |
|
727 | 727 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5})" |
|
728 | 728 | else |
|
729 | 729 | sql = "#{db_table}.#{db_field} BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5}" |
|
730 | 730 | end |
|
731 | 731 | else |
|
732 | 732 | sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" |
|
733 | 733 | end |
|
734 | 734 | else |
|
735 | 735 | # IN an empty set |
|
736 | 736 | sql = "1=0" |
|
737 | 737 | end |
|
738 | 738 | when "!" |
|
739 | 739 | if value.any? |
|
740 | 740 | sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))" |
|
741 | 741 | else |
|
742 | 742 | # NOT IN an empty set |
|
743 | 743 | sql = "1=1" |
|
744 | 744 | end |
|
745 | 745 | when "!*" |
|
746 | 746 | sql = "#{db_table}.#{db_field} IS NULL" |
|
747 | 747 | sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter |
|
748 | 748 | when "*" |
|
749 | 749 | sql = "#{db_table}.#{db_field} IS NOT NULL" |
|
750 | 750 | sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter |
|
751 | 751 | when ">=" |
|
752 | 752 | if [:date, :date_past].include?(type_for(field)) |
|
753 | 753 | sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), nil) |
|
754 | 754 | else |
|
755 | 755 | if is_custom_filter |
|
756 | 756 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) >= #{value.first.to_f})" |
|
757 | 757 | else |
|
758 | 758 | sql = "#{db_table}.#{db_field} >= #{value.first.to_f}" |
|
759 | 759 | end |
|
760 | 760 | end |
|
761 | 761 | when "<=" |
|
762 | 762 | if [:date, :date_past].include?(type_for(field)) |
|
763 | 763 | sql = date_clause(db_table, db_field, nil, (Date.parse(value.first) rescue nil)) |
|
764 | 764 | else |
|
765 | 765 | if is_custom_filter |
|
766 | 766 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) <= #{value.first.to_f})" |
|
767 | 767 | else |
|
768 | 768 | sql = "#{db_table}.#{db_field} <= #{value.first.to_f}" |
|
769 | 769 | end |
|
770 | 770 | end |
|
771 | 771 | when "><" |
|
772 | 772 | if [:date, :date_past].include?(type_for(field)) |
|
773 | 773 | sql = date_clause(db_table, db_field, (Date.parse(value[0]) rescue nil), (Date.parse(value[1]) rescue nil)) |
|
774 | 774 | else |
|
775 | 775 | if is_custom_filter |
|
776 | 776 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value[0].to_f} AND #{value[1].to_f})" |
|
777 | 777 | else |
|
778 | 778 | sql = "#{db_table}.#{db_field} BETWEEN #{value[0].to_f} AND #{value[1].to_f}" |
|
779 | 779 | end |
|
780 | 780 | end |
|
781 | 781 | when "o" |
|
782 | 782 | sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_false})" if field == "status_id" |
|
783 | 783 | when "c" |
|
784 | 784 | sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_true})" if field == "status_id" |
|
785 | 785 | when ">t-" |
|
786 | 786 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, 0) |
|
787 | 787 | when "<t-" |
|
788 | 788 | sql = relative_date_clause(db_table, db_field, nil, - value.first.to_i) |
|
789 | 789 | when "t-" |
|
790 | 790 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, - value.first.to_i) |
|
791 | 791 | when ">t+" |
|
792 | 792 | sql = relative_date_clause(db_table, db_field, value.first.to_i, nil) |
|
793 | 793 | when "<t+" |
|
794 | 794 | sql = relative_date_clause(db_table, db_field, 0, value.first.to_i) |
|
795 | 795 | when "t+" |
|
796 | 796 | sql = relative_date_clause(db_table, db_field, value.first.to_i, value.first.to_i) |
|
797 | 797 | when "t" |
|
798 | 798 | sql = relative_date_clause(db_table, db_field, 0, 0) |
|
799 | 799 | when "w" |
|
800 | 800 | first_day_of_week = l(:general_first_day_of_week).to_i |
|
801 | 801 | day_of_week = Date.today.cwday |
|
802 | 802 | days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week) |
|
803 | 803 | sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6) |
|
804 | 804 | when "~" |
|
805 | 805 | sql = "LOWER(#{db_table}.#{db_field}) LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'" |
|
806 | 806 | when "!~" |
|
807 | 807 | sql = "LOWER(#{db_table}.#{db_field}) NOT LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'" |
|
808 | 808 | else |
|
809 | 809 | raise "Unknown query operator #{operator}" |
|
810 | 810 | end |
|
811 | 811 | |
|
812 | 812 | return sql |
|
813 | 813 | end |
|
814 | 814 | |
|
815 | 815 | def add_custom_fields_filters(custom_fields) |
|
816 | 816 | @available_filters ||= {} |
|
817 | 817 | |
|
818 | 818 | custom_fields.select(&:is_filter?).each do |field| |
|
819 | 819 | case field.field_format |
|
820 | 820 | when "text" |
|
821 | 821 | options = { :type => :text, :order => 20 } |
|
822 | 822 | when "list" |
|
823 | 823 | options = { :type => :list_optional, :values => field.possible_values, :order => 20} |
|
824 | 824 | when "date" |
|
825 | 825 | options = { :type => :date, :order => 20 } |
|
826 | 826 | when "bool" |
|
827 | 827 | options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 } |
|
828 | 828 | when "int" |
|
829 | 829 | options = { :type => :integer, :order => 20 } |
|
830 | 830 | when "float" |
|
831 | 831 | options = { :type => :float, :order => 20 } |
|
832 | 832 | when "user", "version" |
|
833 | 833 | next unless project |
|
834 | 834 | values = field.possible_values_options(project) |
|
835 | 835 | if User.current.logged? && field.field_format == 'user' |
|
836 | 836 | values.unshift ["<< #{l(:label_me)} >>", "me"] |
|
837 | 837 | end |
|
838 | 838 | options = { :type => :list_optional, :values => values, :order => 20} |
|
839 | 839 | else |
|
840 | 840 | options = { :type => :string, :order => 20 } |
|
841 | 841 | end |
|
842 | 842 | @available_filters["cf_#{field.id}"] = options.merge({ :name => field.name, :format => field.field_format }) |
|
843 | 843 | end |
|
844 | 844 | end |
|
845 | 845 | |
|
846 | 846 | # Returns a SQL clause for a date or datetime field. |
|
847 | 847 | def date_clause(table, field, from, to) |
|
848 | 848 | s = [] |
|
849 | 849 | if from |
|
850 | 850 | from_yesterday = from - 1 |
|
851 | 851 | from_yesterday_time = Time.local(from_yesterday.year, from_yesterday.month, from_yesterday.day) |
|
852 | 852 | if self.class.default_timezone == :utc |
|
853 | 853 | from_yesterday_time = from_yesterday_time.utc |
|
854 | 854 | end |
|
855 | 855 | s << ("#{table}.#{field} > '%s'" % [connection.quoted_date(from_yesterday_time.end_of_day)]) |
|
856 | 856 | end |
|
857 | 857 | if to |
|
858 | 858 | to_time = Time.local(to.year, to.month, to.day) |
|
859 | 859 | if self.class.default_timezone == :utc |
|
860 | 860 | to_time = to_time.utc |
|
861 | 861 | end |
|
862 | 862 | s << ("#{table}.#{field} <= '%s'" % [connection.quoted_date(to_time.end_of_day)]) |
|
863 | 863 | end |
|
864 | 864 | s.join(' AND ') |
|
865 | 865 | end |
|
866 | 866 | |
|
867 | 867 | # Returns a SQL clause for a date or datetime field using relative dates. |
|
868 | 868 | def relative_date_clause(table, field, days_from, days_to) |
|
869 | 869 | date_clause(table, field, (days_from ? Date.today + days_from : nil), (days_to ? Date.today + days_to : nil)) |
|
870 | 870 | end |
|
871 | 871 | end |
@@ -1,1000 +1,1028 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 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 | fixtures :projects, :enabled_modules, :users, :members, |
|
22 | 22 | :member_roles, :roles, :trackers, :issue_statuses, |
|
23 | 23 | :issue_categories, :enumerations, :issues, |
|
24 | 24 | :watchers, :custom_fields, :custom_values, :versions, |
|
25 | 25 | :queries, |
|
26 | 26 | :projects_trackers |
|
27 | 27 | |
|
28 | 28 | def test_custom_fields_for_all_projects_should_be_available_in_global_queries |
|
29 | 29 | query = Query.new(:project => nil, :name => '_') |
|
30 | 30 | assert query.available_filters.has_key?('cf_1') |
|
31 | 31 | assert !query.available_filters.has_key?('cf_3') |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | def test_system_shared_versions_should_be_available_in_global_queries |
|
35 | 35 | Version.find(2).update_attribute :sharing, 'system' |
|
36 | 36 | query = Query.new(:project => nil, :name => '_') |
|
37 | 37 | assert query.available_filters.has_key?('fixed_version_id') |
|
38 | 38 | assert query.available_filters['fixed_version_id'][:values].detect {|v| v.last == '2'} |
|
39 | 39 | end |
|
40 | 40 | |
|
41 | 41 | def test_project_filter_in_global_queries |
|
42 | 42 | query = Query.new(:project => nil, :name => '_') |
|
43 | 43 | project_filter = query.available_filters["project_id"] |
|
44 | 44 | assert_not_nil project_filter |
|
45 | 45 | project_ids = project_filter[:values].map{|p| p[1]} |
|
46 | 46 | assert project_ids.include?("1") #public project |
|
47 | 47 | assert !project_ids.include?("2") #private project user cannot see |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | def find_issues_with_query(query) |
|
51 | 51 | Issue.includes([:assigned_to, :status, :tracker, :project, :priority]).where( |
|
52 | 52 | query.statement |
|
53 | 53 | ).all |
|
54 | 54 | end |
|
55 | 55 | |
|
56 | 56 | def assert_find_issues_with_query_is_successful(query) |
|
57 | 57 | assert_nothing_raised do |
|
58 | 58 | find_issues_with_query(query) |
|
59 | 59 | end |
|
60 | 60 | end |
|
61 | 61 | |
|
62 | 62 | def assert_query_statement_includes(query, condition) |
|
63 | 63 | assert query.statement.include?(condition), "Query statement condition not found in: #{query.statement}" |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | def assert_query_result(expected, query) |
|
67 | 67 | assert_nothing_raised do |
|
68 | 68 | assert_equal expected.map(&:id).sort, query.issues.map(&:id).sort |
|
69 | 69 | assert_equal expected.size, query.issue_count |
|
70 | 70 | end |
|
71 | 71 | end |
|
72 | 72 | |
|
73 | 73 | def test_query_should_allow_shared_versions_for_a_project_query |
|
74 | 74 | subproject_version = Version.find(4) |
|
75 | 75 | query = Query.new(:project => Project.find(1), :name => '_') |
|
76 | 76 | query.add_filter('fixed_version_id', '=', [subproject_version.id.to_s]) |
|
77 | 77 | |
|
78 | 78 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IN ('4')") |
|
79 | 79 | end |
|
80 | 80 | |
|
81 | 81 | def test_query_with_multiple_custom_fields |
|
82 | 82 | query = Query.find(1) |
|
83 | 83 | assert query.valid? |
|
84 | 84 | assert query.statement.include?("#{CustomValue.table_name}.value IN ('MySQL')") |
|
85 | 85 | issues = find_issues_with_query(query) |
|
86 | 86 | assert_equal 1, issues.length |
|
87 | 87 | assert_equal Issue.find(3), issues.first |
|
88 | 88 | end |
|
89 | 89 | |
|
90 | 90 | def test_operator_none |
|
91 | 91 | query = Query.new(:project => Project.find(1), :name => '_') |
|
92 | 92 | query.add_filter('fixed_version_id', '!*', ['']) |
|
93 | 93 | query.add_filter('cf_1', '!*', ['']) |
|
94 | 94 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NULL") |
|
95 | 95 | assert query.statement.include?("#{CustomValue.table_name}.value IS NULL OR #{CustomValue.table_name}.value = ''") |
|
96 | 96 | find_issues_with_query(query) |
|
97 | 97 | end |
|
98 | 98 | |
|
99 | 99 | def test_operator_none_for_integer |
|
100 | 100 | query = Query.new(:project => Project.find(1), :name => '_') |
|
101 | 101 | query.add_filter('estimated_hours', '!*', ['']) |
|
102 | 102 | issues = find_issues_with_query(query) |
|
103 | 103 | assert !issues.empty? |
|
104 | 104 | assert issues.all? {|i| !i.estimated_hours} |
|
105 | 105 | end |
|
106 | 106 | |
|
107 | 107 | def test_operator_none_for_date |
|
108 | 108 | query = Query.new(:project => Project.find(1), :name => '_') |
|
109 | 109 | query.add_filter('start_date', '!*', ['']) |
|
110 | 110 | issues = find_issues_with_query(query) |
|
111 | 111 | assert !issues.empty? |
|
112 | 112 | assert issues.all? {|i| i.start_date.nil?} |
|
113 | 113 | end |
|
114 | 114 | |
|
115 | 115 | def test_operator_none_for_string_custom_field |
|
116 | 116 | query = Query.new(:project => Project.find(1), :name => '_') |
|
117 | 117 | query.add_filter('cf_2', '!*', ['']) |
|
118 | 118 | assert query.has_filter?('cf_2') |
|
119 | 119 | issues = find_issues_with_query(query) |
|
120 | 120 | assert !issues.empty? |
|
121 | 121 | assert issues.all? {|i| i.custom_field_value(2).blank?} |
|
122 | 122 | end |
|
123 | 123 | |
|
124 | 124 | def test_operator_all |
|
125 | 125 | query = Query.new(:project => Project.find(1), :name => '_') |
|
126 | 126 | query.add_filter('fixed_version_id', '*', ['']) |
|
127 | 127 | query.add_filter('cf_1', '*', ['']) |
|
128 | 128 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NOT NULL") |
|
129 | 129 | assert query.statement.include?("#{CustomValue.table_name}.value IS NOT NULL AND #{CustomValue.table_name}.value <> ''") |
|
130 | 130 | find_issues_with_query(query) |
|
131 | 131 | end |
|
132 | 132 | |
|
133 | 133 | def test_operator_all_for_date |
|
134 | 134 | query = Query.new(:project => Project.find(1), :name => '_') |
|
135 | 135 | query.add_filter('start_date', '*', ['']) |
|
136 | 136 | issues = find_issues_with_query(query) |
|
137 | 137 | assert !issues.empty? |
|
138 | 138 | assert issues.all? {|i| i.start_date.present?} |
|
139 | 139 | end |
|
140 | 140 | |
|
141 | 141 | def test_operator_all_for_string_custom_field |
|
142 | 142 | query = Query.new(:project => Project.find(1), :name => '_') |
|
143 | 143 | query.add_filter('cf_2', '*', ['']) |
|
144 | 144 | assert query.has_filter?('cf_2') |
|
145 | 145 | issues = find_issues_with_query(query) |
|
146 | 146 | assert !issues.empty? |
|
147 | 147 | assert issues.all? {|i| i.custom_field_value(2).present?} |
|
148 | 148 | end |
|
149 | 149 | |
|
150 | 150 | def test_numeric_filter_should_not_accept_non_numeric_values |
|
151 | 151 | query = Query.new(:name => '_') |
|
152 | 152 | query.add_filter('estimated_hours', '=', ['a']) |
|
153 | 153 | |
|
154 | 154 | assert query.has_filter?('estimated_hours') |
|
155 | 155 | assert !query.valid? |
|
156 | 156 | end |
|
157 | 157 | |
|
158 | 158 | def test_operator_is_on_float |
|
159 | 159 | Issue.update_all("estimated_hours = 171.2", "id=2") |
|
160 | 160 | |
|
161 | 161 | query = Query.new(:name => '_') |
|
162 | 162 | query.add_filter('estimated_hours', '=', ['171.20']) |
|
163 | 163 | issues = find_issues_with_query(query) |
|
164 | 164 | assert_equal 1, issues.size |
|
165 | 165 | assert_equal 2, issues.first.id |
|
166 | 166 | end |
|
167 | 167 | |
|
168 | 168 | def test_operator_is_on_integer_custom_field |
|
169 | 169 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_for_all => true, :is_filter => true) |
|
170 | 170 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7') |
|
171 | 171 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12') |
|
172 | 172 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') |
|
173 | 173 | |
|
174 | 174 | query = Query.new(:name => '_') |
|
175 | 175 | query.add_filter("cf_#{f.id}", '=', ['12']) |
|
176 | 176 | issues = find_issues_with_query(query) |
|
177 | 177 | assert_equal 1, issues.size |
|
178 | 178 | assert_equal 2, issues.first.id |
|
179 | 179 | end |
|
180 | 180 | |
|
181 | def test_operator_is_on_integer_custom_field_should_accept_negative_value | |
|
182 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_for_all => true, :is_filter => true) | |
|
183 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7') | |
|
184 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '-12') | |
|
185 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') | |
|
186 | ||
|
187 | query = Query.new(:name => '_') | |
|
188 | query.add_filter("cf_#{f.id}", '=', ['-12']) | |
|
189 | assert query.valid? | |
|
190 | issues = find_issues_with_query(query) | |
|
191 | assert_equal 1, issues.size | |
|
192 | assert_equal 2, issues.first.id | |
|
193 | end | |
|
194 | ||
|
181 | 195 | def test_operator_is_on_float_custom_field |
|
182 | 196 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'float', :is_filter => true, :is_for_all => true) |
|
183 | 197 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7.3') |
|
184 | 198 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12.7') |
|
185 | 199 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') |
|
186 | 200 | |
|
187 | 201 | query = Query.new(:name => '_') |
|
188 | 202 | query.add_filter("cf_#{f.id}", '=', ['12.7']) |
|
189 | 203 | issues = find_issues_with_query(query) |
|
190 | 204 | assert_equal 1, issues.size |
|
191 | 205 | assert_equal 2, issues.first.id |
|
192 | 206 | end |
|
193 | 207 | |
|
208 | def test_operator_is_on_float_custom_field_should_accept_negative_value | |
|
209 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'float', :is_filter => true, :is_for_all => true) | |
|
210 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7.3') | |
|
211 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '-12.7') | |
|
212 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') | |
|
213 | ||
|
214 | query = Query.new(:name => '_') | |
|
215 | query.add_filter("cf_#{f.id}", '=', ['-12.7']) | |
|
216 | assert query.valid? | |
|
217 | issues = find_issues_with_query(query) | |
|
218 | assert_equal 1, issues.size | |
|
219 | assert_equal 2, issues.first.id | |
|
220 | end | |
|
221 | ||
|
194 | 222 | def test_operator_is_on_multi_list_custom_field |
|
195 | 223 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'list', :is_filter => true, :is_for_all => true, |
|
196 | 224 | :possible_values => ['value1', 'value2', 'value3'], :multiple => true) |
|
197 | 225 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value1') |
|
198 | 226 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value2') |
|
199 | 227 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => 'value1') |
|
200 | 228 | |
|
201 | 229 | query = Query.new(:name => '_') |
|
202 | 230 | query.add_filter("cf_#{f.id}", '=', ['value1']) |
|
203 | 231 | issues = find_issues_with_query(query) |
|
204 | 232 | assert_equal [1, 3], issues.map(&:id).sort |
|
205 | 233 | |
|
206 | 234 | query = Query.new(:name => '_') |
|
207 | 235 | query.add_filter("cf_#{f.id}", '=', ['value2']) |
|
208 | 236 | issues = find_issues_with_query(query) |
|
209 | 237 | assert_equal [1], issues.map(&:id).sort |
|
210 | 238 | end |
|
211 | 239 | |
|
212 | 240 | def test_operator_is_not_on_multi_list_custom_field |
|
213 | 241 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'list', :is_filter => true, :is_for_all => true, |
|
214 | 242 | :possible_values => ['value1', 'value2', 'value3'], :multiple => true) |
|
215 | 243 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value1') |
|
216 | 244 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value2') |
|
217 | 245 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => 'value1') |
|
218 | 246 | |
|
219 | 247 | query = Query.new(:name => '_') |
|
220 | 248 | query.add_filter("cf_#{f.id}", '!', ['value1']) |
|
221 | 249 | issues = find_issues_with_query(query) |
|
222 | 250 | assert !issues.map(&:id).include?(1) |
|
223 | 251 | assert !issues.map(&:id).include?(3) |
|
224 | 252 | |
|
225 | 253 | query = Query.new(:name => '_') |
|
226 | 254 | query.add_filter("cf_#{f.id}", '!', ['value2']) |
|
227 | 255 | issues = find_issues_with_query(query) |
|
228 | 256 | assert !issues.map(&:id).include?(1) |
|
229 | 257 | assert issues.map(&:id).include?(3) |
|
230 | 258 | end |
|
231 | 259 | |
|
232 | 260 | def test_operator_greater_than |
|
233 | 261 | query = Query.new(:project => Project.find(1), :name => '_') |
|
234 | 262 | query.add_filter('done_ratio', '>=', ['40']) |
|
235 | 263 | assert query.statement.include?("#{Issue.table_name}.done_ratio >= 40.0") |
|
236 | 264 | find_issues_with_query(query) |
|
237 | 265 | end |
|
238 | 266 | |
|
239 | 267 | def test_operator_greater_than_a_float |
|
240 | 268 | query = Query.new(:project => Project.find(1), :name => '_') |
|
241 | 269 | query.add_filter('estimated_hours', '>=', ['40.5']) |
|
242 | 270 | assert query.statement.include?("#{Issue.table_name}.estimated_hours >= 40.5") |
|
243 | 271 | find_issues_with_query(query) |
|
244 | 272 | end |
|
245 | 273 | |
|
246 | 274 | def test_operator_greater_than_on_int_custom_field |
|
247 | 275 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) |
|
248 | 276 | CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7') |
|
249 | 277 | CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12') |
|
250 | 278 | CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '') |
|
251 | 279 | |
|
252 | 280 | query = Query.new(:project => Project.find(1), :name => '_') |
|
253 | 281 | query.add_filter("cf_#{f.id}", '>=', ['8']) |
|
254 | 282 | issues = find_issues_with_query(query) |
|
255 | 283 | assert_equal 1, issues.size |
|
256 | 284 | assert_equal 2, issues.first.id |
|
257 | 285 | end |
|
258 | 286 | |
|
259 | 287 | def test_operator_lesser_than |
|
260 | 288 | query = Query.new(:project => Project.find(1), :name => '_') |
|
261 | 289 | query.add_filter('done_ratio', '<=', ['30']) |
|
262 | 290 | assert query.statement.include?("#{Issue.table_name}.done_ratio <= 30.0") |
|
263 | 291 | find_issues_with_query(query) |
|
264 | 292 | end |
|
265 | 293 | |
|
266 | 294 | def test_operator_lesser_than_on_custom_field |
|
267 | 295 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) |
|
268 | 296 | query = Query.new(:project => Project.find(1), :name => '_') |
|
269 | 297 | query.add_filter("cf_#{f.id}", '<=', ['30']) |
|
270 | 298 | assert query.statement.include?("CAST(custom_values.value AS decimal(60,3)) <= 30.0") |
|
271 | 299 | find_issues_with_query(query) |
|
272 | 300 | end |
|
273 | 301 | |
|
274 | 302 | def test_operator_between |
|
275 | 303 | query = Query.new(:project => Project.find(1), :name => '_') |
|
276 | 304 | query.add_filter('done_ratio', '><', ['30', '40']) |
|
277 | 305 | assert_include "#{Issue.table_name}.done_ratio BETWEEN 30.0 AND 40.0", query.statement |
|
278 | 306 | find_issues_with_query(query) |
|
279 | 307 | end |
|
280 | 308 | |
|
281 | 309 | def test_operator_between_on_custom_field |
|
282 | 310 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) |
|
283 | 311 | query = Query.new(:project => Project.find(1), :name => '_') |
|
284 | 312 | query.add_filter("cf_#{f.id}", '><', ['30', '40']) |
|
285 | 313 | assert_include "CAST(custom_values.value AS decimal(60,3)) BETWEEN 30.0 AND 40.0", query.statement |
|
286 | 314 | find_issues_with_query(query) |
|
287 | 315 | end |
|
288 | 316 | |
|
289 | 317 | def test_date_filter_should_not_accept_non_date_values |
|
290 | 318 | query = Query.new(:name => '_') |
|
291 | 319 | query.add_filter('created_on', '=', ['a']) |
|
292 | 320 | |
|
293 | 321 | assert query.has_filter?('created_on') |
|
294 | 322 | assert !query.valid? |
|
295 | 323 | end |
|
296 | 324 | |
|
297 | 325 | def test_date_filter_should_not_accept_invalid_date_values |
|
298 | 326 | query = Query.new(:name => '_') |
|
299 | 327 | query.add_filter('created_on', '=', ['2011-01-34']) |
|
300 | 328 | |
|
301 | 329 | assert query.has_filter?('created_on') |
|
302 | 330 | assert !query.valid? |
|
303 | 331 | end |
|
304 | 332 | |
|
305 | 333 | def test_relative_date_filter_should_not_accept_non_integer_values |
|
306 | 334 | query = Query.new(:name => '_') |
|
307 | 335 | query.add_filter('created_on', '>t-', ['a']) |
|
308 | 336 | |
|
309 | 337 | assert query.has_filter?('created_on') |
|
310 | 338 | assert !query.valid? |
|
311 | 339 | end |
|
312 | 340 | |
|
313 | 341 | def test_operator_date_equals |
|
314 | 342 | query = Query.new(:name => '_') |
|
315 | 343 | query.add_filter('due_date', '=', ['2011-07-10']) |
|
316 | 344 | 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 |
|
317 | 345 | find_issues_with_query(query) |
|
318 | 346 | end |
|
319 | 347 | |
|
320 | 348 | def test_operator_date_lesser_than |
|
321 | 349 | query = Query.new(:name => '_') |
|
322 | 350 | query.add_filter('due_date', '<=', ['2011-07-10']) |
|
323 | 351 | assert_match /issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement |
|
324 | 352 | find_issues_with_query(query) |
|
325 | 353 | end |
|
326 | 354 | |
|
327 | 355 | def test_operator_date_greater_than |
|
328 | 356 | query = Query.new(:name => '_') |
|
329 | 357 | query.add_filter('due_date', '>=', ['2011-07-10']) |
|
330 | 358 | assert_match /issues\.due_date > '2011-07-09 23:59:59(\.9+)?'/, query.statement |
|
331 | 359 | find_issues_with_query(query) |
|
332 | 360 | end |
|
333 | 361 | |
|
334 | 362 | def test_operator_date_between |
|
335 | 363 | query = Query.new(:name => '_') |
|
336 | 364 | query.add_filter('due_date', '><', ['2011-06-23', '2011-07-10']) |
|
337 | 365 | 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 |
|
338 | 366 | find_issues_with_query(query) |
|
339 | 367 | end |
|
340 | 368 | |
|
341 | 369 | def test_operator_in_more_than |
|
342 | 370 | Issue.find(7).update_attribute(:due_date, (Date.today + 15)) |
|
343 | 371 | query = Query.new(:project => Project.find(1), :name => '_') |
|
344 | 372 | query.add_filter('due_date', '>t+', ['15']) |
|
345 | 373 | issues = find_issues_with_query(query) |
|
346 | 374 | assert !issues.empty? |
|
347 | 375 | issues.each {|issue| assert(issue.due_date >= (Date.today + 15))} |
|
348 | 376 | end |
|
349 | 377 | |
|
350 | 378 | def test_operator_in_less_than |
|
351 | 379 | query = Query.new(:project => Project.find(1), :name => '_') |
|
352 | 380 | query.add_filter('due_date', '<t+', ['15']) |
|
353 | 381 | issues = find_issues_with_query(query) |
|
354 | 382 | assert !issues.empty? |
|
355 | 383 | issues.each {|issue| assert(issue.due_date >= Date.today && issue.due_date <= (Date.today + 15))} |
|
356 | 384 | end |
|
357 | 385 | |
|
358 | 386 | def test_operator_less_than_ago |
|
359 | 387 | Issue.find(7).update_attribute(:due_date, (Date.today - 3)) |
|
360 | 388 | query = Query.new(:project => Project.find(1), :name => '_') |
|
361 | 389 | query.add_filter('due_date', '>t-', ['3']) |
|
362 | 390 | issues = find_issues_with_query(query) |
|
363 | 391 | assert !issues.empty? |
|
364 | 392 | issues.each {|issue| assert(issue.due_date >= (Date.today - 3) && issue.due_date <= Date.today)} |
|
365 | 393 | end |
|
366 | 394 | |
|
367 | 395 | def test_operator_more_than_ago |
|
368 | 396 | Issue.find(7).update_attribute(:due_date, (Date.today - 10)) |
|
369 | 397 | query = Query.new(:project => Project.find(1), :name => '_') |
|
370 | 398 | query.add_filter('due_date', '<t-', ['10']) |
|
371 | 399 | assert query.statement.include?("#{Issue.table_name}.due_date <=") |
|
372 | 400 | issues = find_issues_with_query(query) |
|
373 | 401 | assert !issues.empty? |
|
374 | 402 | issues.each {|issue| assert(issue.due_date <= (Date.today - 10))} |
|
375 | 403 | end |
|
376 | 404 | |
|
377 | 405 | def test_operator_in |
|
378 | 406 | Issue.find(7).update_attribute(:due_date, (Date.today + 2)) |
|
379 | 407 | query = Query.new(:project => Project.find(1), :name => '_') |
|
380 | 408 | query.add_filter('due_date', 't+', ['2']) |
|
381 | 409 | issues = find_issues_with_query(query) |
|
382 | 410 | assert !issues.empty? |
|
383 | 411 | issues.each {|issue| assert_equal((Date.today + 2), issue.due_date)} |
|
384 | 412 | end |
|
385 | 413 | |
|
386 | 414 | def test_operator_ago |
|
387 | 415 | Issue.find(7).update_attribute(:due_date, (Date.today - 3)) |
|
388 | 416 | query = Query.new(:project => Project.find(1), :name => '_') |
|
389 | 417 | query.add_filter('due_date', 't-', ['3']) |
|
390 | 418 | issues = find_issues_with_query(query) |
|
391 | 419 | assert !issues.empty? |
|
392 | 420 | issues.each {|issue| assert_equal((Date.today - 3), issue.due_date)} |
|
393 | 421 | end |
|
394 | 422 | |
|
395 | 423 | def test_operator_today |
|
396 | 424 | query = Query.new(:project => Project.find(1), :name => '_') |
|
397 | 425 | query.add_filter('due_date', 't', ['']) |
|
398 | 426 | issues = find_issues_with_query(query) |
|
399 | 427 | assert !issues.empty? |
|
400 | 428 | issues.each {|issue| assert_equal Date.today, issue.due_date} |
|
401 | 429 | end |
|
402 | 430 | |
|
403 | 431 | def test_operator_this_week_on_date |
|
404 | 432 | query = Query.new(:project => Project.find(1), :name => '_') |
|
405 | 433 | query.add_filter('due_date', 'w', ['']) |
|
406 | 434 | find_issues_with_query(query) |
|
407 | 435 | end |
|
408 | 436 | |
|
409 | 437 | def test_operator_this_week_on_datetime |
|
410 | 438 | query = Query.new(:project => Project.find(1), :name => '_') |
|
411 | 439 | query.add_filter('created_on', 'w', ['']) |
|
412 | 440 | find_issues_with_query(query) |
|
413 | 441 | end |
|
414 | 442 | |
|
415 | 443 | def test_operator_contains |
|
416 | 444 | query = Query.new(:project => Project.find(1), :name => '_') |
|
417 | 445 | query.add_filter('subject', '~', ['uNable']) |
|
418 | 446 | assert query.statement.include?("LOWER(#{Issue.table_name}.subject) LIKE '%unable%'") |
|
419 | 447 | result = find_issues_with_query(query) |
|
420 | 448 | assert result.empty? |
|
421 | 449 | result.each {|issue| assert issue.subject.downcase.include?('unable') } |
|
422 | 450 | end |
|
423 | 451 | |
|
424 | 452 | def test_range_for_this_week_with_week_starting_on_monday |
|
425 | 453 | I18n.locale = :fr |
|
426 | 454 | assert_equal '1', I18n.t(:general_first_day_of_week) |
|
427 | 455 | |
|
428 | 456 | Date.stubs(:today).returns(Date.parse('2011-04-29')) |
|
429 | 457 | |
|
430 | 458 | query = Query.new(:project => Project.find(1), :name => '_') |
|
431 | 459 | query.add_filter('due_date', 'w', ['']) |
|
432 | 460 | 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}" |
|
433 | 461 | I18n.locale = :en |
|
434 | 462 | end |
|
435 | 463 | |
|
436 | 464 | def test_range_for_this_week_with_week_starting_on_sunday |
|
437 | 465 | I18n.locale = :en |
|
438 | 466 | assert_equal '7', I18n.t(:general_first_day_of_week) |
|
439 | 467 | |
|
440 | 468 | Date.stubs(:today).returns(Date.parse('2011-04-29')) |
|
441 | 469 | |
|
442 | 470 | query = Query.new(:project => Project.find(1), :name => '_') |
|
443 | 471 | query.add_filter('due_date', 'w', ['']) |
|
444 | 472 | 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}" |
|
445 | 473 | end |
|
446 | 474 | |
|
447 | 475 | def test_operator_does_not_contains |
|
448 | 476 | query = Query.new(:project => Project.find(1), :name => '_') |
|
449 | 477 | query.add_filter('subject', '!~', ['uNable']) |
|
450 | 478 | assert query.statement.include?("LOWER(#{Issue.table_name}.subject) NOT LIKE '%unable%'") |
|
451 | 479 | find_issues_with_query(query) |
|
452 | 480 | end |
|
453 | 481 | |
|
454 | 482 | def test_filter_assigned_to_me |
|
455 | 483 | user = User.find(2) |
|
456 | 484 | group = Group.find(10) |
|
457 | 485 | User.current = user |
|
458 | 486 | i1 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => user) |
|
459 | 487 | i2 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => group) |
|
460 | 488 | i3 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => Group.find(11)) |
|
461 | 489 | group.users << user |
|
462 | 490 | |
|
463 | 491 | query = Query.new(:name => '_', :filters => { 'assigned_to_id' => {:operator => '=', :values => ['me']}}) |
|
464 | 492 | result = query.issues |
|
465 | 493 | assert_equal Issue.visible.all(:conditions => {:assigned_to_id => ([2] + user.reload.group_ids)}).sort_by(&:id), result.sort_by(&:id) |
|
466 | 494 | |
|
467 | 495 | assert result.include?(i1) |
|
468 | 496 | assert result.include?(i2) |
|
469 | 497 | assert !result.include?(i3) |
|
470 | 498 | end |
|
471 | 499 | |
|
472 | 500 | def test_user_custom_field_filtered_on_me |
|
473 | 501 | User.current = User.find(2) |
|
474 | 502 | cf = IssueCustomField.create!(:field_format => 'user', :is_for_all => true, :is_filter => true, :name => 'User custom field', :tracker_ids => [1]) |
|
475 | 503 | issue1 = Issue.create!(:project_id => 1, :tracker_id => 1, :custom_field_values => {cf.id.to_s => '2'}, :subject => 'Test', :author_id => 1) |
|
476 | 504 | issue2 = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {cf.id.to_s => '3'}) |
|
477 | 505 | |
|
478 | 506 | query = Query.new(:name => '_', :project => Project.find(1)) |
|
479 | 507 | filter = query.available_filters["cf_#{cf.id}"] |
|
480 | 508 | assert_not_nil filter |
|
481 | 509 | assert_include 'me', filter[:values].map{|v| v[1]} |
|
482 | 510 | |
|
483 | 511 | query.filters = { "cf_#{cf.id}" => {:operator => '=', :values => ['me']}} |
|
484 | 512 | result = query.issues |
|
485 | 513 | assert_equal 1, result.size |
|
486 | 514 | assert_equal issue1, result.first |
|
487 | 515 | end |
|
488 | 516 | |
|
489 | 517 | def test_filter_my_projects |
|
490 | 518 | User.current = User.find(2) |
|
491 | 519 | query = Query.new(:name => '_') |
|
492 | 520 | filter = query.available_filters['project_id'] |
|
493 | 521 | assert_not_nil filter |
|
494 | 522 | assert_include 'mine', filter[:values].map{|v| v[1]} |
|
495 | 523 | |
|
496 | 524 | query.filters = { 'project_id' => {:operator => '=', :values => ['mine']}} |
|
497 | 525 | result = query.issues |
|
498 | 526 | assert_nil result.detect {|issue| !User.current.member_of?(issue.project)} |
|
499 | 527 | end |
|
500 | 528 | |
|
501 | 529 | def test_filter_watched_issues |
|
502 | 530 | User.current = User.find(1) |
|
503 | 531 | query = Query.new(:name => '_', :filters => { 'watcher_id' => {:operator => '=', :values => ['me']}}) |
|
504 | 532 | result = find_issues_with_query(query) |
|
505 | 533 | assert_not_nil result |
|
506 | 534 | assert !result.empty? |
|
507 | 535 | assert_equal Issue.visible.watched_by(User.current).sort_by(&:id), result.sort_by(&:id) |
|
508 | 536 | User.current = nil |
|
509 | 537 | end |
|
510 | 538 | |
|
511 | 539 | def test_filter_unwatched_issues |
|
512 | 540 | User.current = User.find(1) |
|
513 | 541 | query = Query.new(:name => '_', :filters => { 'watcher_id' => {:operator => '!', :values => ['me']}}) |
|
514 | 542 | result = find_issues_with_query(query) |
|
515 | 543 | assert_not_nil result |
|
516 | 544 | assert !result.empty? |
|
517 | 545 | assert_equal((Issue.visible - Issue.watched_by(User.current)).sort_by(&:id).size, result.sort_by(&:id).size) |
|
518 | 546 | User.current = nil |
|
519 | 547 | end |
|
520 | 548 | |
|
521 | 549 | def test_statement_should_be_nil_with_no_filters |
|
522 | 550 | q = Query.new(:name => '_') |
|
523 | 551 | q.filters = {} |
|
524 | 552 | |
|
525 | 553 | assert q.valid? |
|
526 | 554 | assert_nil q.statement |
|
527 | 555 | end |
|
528 | 556 | |
|
529 | 557 | def test_default_columns |
|
530 | 558 | q = Query.new |
|
531 | 559 | assert !q.columns.empty? |
|
532 | 560 | end |
|
533 | 561 | |
|
534 | 562 | def test_set_column_names |
|
535 | 563 | q = Query.new |
|
536 | 564 | q.column_names = ['tracker', :subject, '', 'unknonw_column'] |
|
537 | 565 | assert_equal [:tracker, :subject], q.columns.collect {|c| c.name} |
|
538 | 566 | c = q.columns.first |
|
539 | 567 | assert q.has_column?(c) |
|
540 | 568 | end |
|
541 | 569 | |
|
542 | 570 | def test_query_should_preload_spent_hours |
|
543 | 571 | q = Query.new(:name => '_', :column_names => [:subject, :spent_hours]) |
|
544 | 572 | assert q.has_column?(:spent_hours) |
|
545 | 573 | issues = q.issues |
|
546 | 574 | assert_not_nil issues.first.instance_variable_get("@spent_hours") |
|
547 | 575 | end |
|
548 | 576 | |
|
549 | 577 | def test_groupable_columns_should_include_custom_fields |
|
550 | 578 | q = Query.new |
|
551 | 579 | column = q.groupable_columns.detect {|c| c.name == :cf_1} |
|
552 | 580 | assert_not_nil column |
|
553 | 581 | assert_kind_of QueryCustomFieldColumn, column |
|
554 | 582 | end |
|
555 | 583 | |
|
556 | 584 | def test_groupable_columns_should_not_include_multi_custom_fields |
|
557 | 585 | field = CustomField.find(1) |
|
558 | 586 | field.update_attribute :multiple, true |
|
559 | 587 | |
|
560 | 588 | q = Query.new |
|
561 | 589 | column = q.groupable_columns.detect {|c| c.name == :cf_1} |
|
562 | 590 | assert_nil column |
|
563 | 591 | end |
|
564 | 592 | |
|
565 | 593 | def test_grouped_with_valid_column |
|
566 | 594 | q = Query.new(:group_by => 'status') |
|
567 | 595 | assert q.grouped? |
|
568 | 596 | assert_not_nil q.group_by_column |
|
569 | 597 | assert_equal :status, q.group_by_column.name |
|
570 | 598 | assert_not_nil q.group_by_statement |
|
571 | 599 | assert_equal 'status', q.group_by_statement |
|
572 | 600 | end |
|
573 | 601 | |
|
574 | 602 | def test_grouped_with_invalid_column |
|
575 | 603 | q = Query.new(:group_by => 'foo') |
|
576 | 604 | assert !q.grouped? |
|
577 | 605 | assert_nil q.group_by_column |
|
578 | 606 | assert_nil q.group_by_statement |
|
579 | 607 | end |
|
580 | 608 | |
|
581 | 609 | def test_sortable_columns_should_sort_assignees_according_to_user_format_setting |
|
582 | 610 | with_settings :user_format => 'lastname_coma_firstname' do |
|
583 | 611 | q = Query.new |
|
584 | 612 | assert q.sortable_columns.has_key?('assigned_to') |
|
585 | 613 | assert_equal %w(users.lastname users.firstname users.id), q.sortable_columns['assigned_to'] |
|
586 | 614 | end |
|
587 | 615 | end |
|
588 | 616 | |
|
589 | 617 | def test_sortable_columns_should_sort_authors_according_to_user_format_setting |
|
590 | 618 | with_settings :user_format => 'lastname_coma_firstname' do |
|
591 | 619 | q = Query.new |
|
592 | 620 | assert q.sortable_columns.has_key?('author') |
|
593 | 621 | assert_equal %w(authors.lastname authors.firstname authors.id), q.sortable_columns['author'] |
|
594 | 622 | end |
|
595 | 623 | end |
|
596 | 624 | |
|
597 | 625 | def test_sortable_columns_should_include_custom_field |
|
598 | 626 | q = Query.new |
|
599 | 627 | assert q.sortable_columns['cf_1'] |
|
600 | 628 | end |
|
601 | 629 | |
|
602 | 630 | def test_sortable_columns_should_not_include_multi_custom_field |
|
603 | 631 | field = CustomField.find(1) |
|
604 | 632 | field.update_attribute :multiple, true |
|
605 | 633 | |
|
606 | 634 | q = Query.new |
|
607 | 635 | assert !q.sortable_columns['cf_1'] |
|
608 | 636 | end |
|
609 | 637 | |
|
610 | 638 | def test_default_sort |
|
611 | 639 | q = Query.new |
|
612 | 640 | assert_equal [], q.sort_criteria |
|
613 | 641 | end |
|
614 | 642 | |
|
615 | 643 | def test_set_sort_criteria_with_hash |
|
616 | 644 | q = Query.new |
|
617 | 645 | q.sort_criteria = {'0' => ['priority', 'desc'], '2' => ['tracker']} |
|
618 | 646 | assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria |
|
619 | 647 | end |
|
620 | 648 | |
|
621 | 649 | def test_set_sort_criteria_with_array |
|
622 | 650 | q = Query.new |
|
623 | 651 | q.sort_criteria = [['priority', 'desc'], 'tracker'] |
|
624 | 652 | assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria |
|
625 | 653 | end |
|
626 | 654 | |
|
627 | 655 | def test_create_query_with_sort |
|
628 | 656 | q = Query.new(:name => 'Sorted') |
|
629 | 657 | q.sort_criteria = [['priority', 'desc'], 'tracker'] |
|
630 | 658 | assert q.save |
|
631 | 659 | q.reload |
|
632 | 660 | assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria |
|
633 | 661 | end |
|
634 | 662 | |
|
635 | 663 | def test_sort_by_string_custom_field_asc |
|
636 | 664 | q = Query.new |
|
637 | 665 | c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' } |
|
638 | 666 | assert c |
|
639 | 667 | assert c.sortable |
|
640 | 668 | issues = Issue.includes([:assigned_to, :status, :tracker, :project, :priority]).where( |
|
641 | 669 | q.statement |
|
642 | 670 | ).order("#{c.sortable} ASC").all |
|
643 | 671 | values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s} |
|
644 | 672 | assert !values.empty? |
|
645 | 673 | assert_equal values.sort, values |
|
646 | 674 | end |
|
647 | 675 | |
|
648 | 676 | def test_sort_by_string_custom_field_desc |
|
649 | 677 | q = Query.new |
|
650 | 678 | c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' } |
|
651 | 679 | assert c |
|
652 | 680 | assert c.sortable |
|
653 | 681 | issues = Issue.includes([:assigned_to, :status, :tracker, :project, :priority]).where( |
|
654 | 682 | q.statement |
|
655 | 683 | ).order("#{c.sortable} DESC").all |
|
656 | 684 | values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s} |
|
657 | 685 | assert !values.empty? |
|
658 | 686 | assert_equal values.sort.reverse, values |
|
659 | 687 | end |
|
660 | 688 | |
|
661 | 689 | def test_sort_by_float_custom_field_asc |
|
662 | 690 | q = Query.new |
|
663 | 691 | c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'float' } |
|
664 | 692 | assert c |
|
665 | 693 | assert c.sortable |
|
666 | 694 | issues = Issue.includes([:assigned_to, :status, :tracker, :project, :priority]).where( |
|
667 | 695 | q.statement |
|
668 | 696 | ).order("#{c.sortable} ASC").all |
|
669 | 697 | values = issues.collect {|i| begin; Kernel.Float(i.custom_value_for(c.custom_field).to_s); rescue; nil; end}.compact |
|
670 | 698 | assert !values.empty? |
|
671 | 699 | assert_equal values.sort, values |
|
672 | 700 | end |
|
673 | 701 | |
|
674 | 702 | def test_invalid_query_should_raise_query_statement_invalid_error |
|
675 | 703 | q = Query.new |
|
676 | 704 | assert_raise Query::StatementInvalid do |
|
677 | 705 | q.issues(:conditions => "foo = 1") |
|
678 | 706 | end |
|
679 | 707 | end |
|
680 | 708 | |
|
681 | 709 | def test_issue_count |
|
682 | 710 | q = Query.new(:name => '_') |
|
683 | 711 | issue_count = q.issue_count |
|
684 | 712 | assert_equal q.issues.size, issue_count |
|
685 | 713 | end |
|
686 | 714 | |
|
687 | 715 | def test_issue_count_with_archived_issues |
|
688 | 716 | p = Project.generate! do |project| |
|
689 | 717 | project.status = Project::STATUS_ARCHIVED |
|
690 | 718 | end |
|
691 | 719 | i = Issue.generate!( :project => p, :tracker => p.trackers.first ) |
|
692 | 720 | assert !i.visible? |
|
693 | 721 | |
|
694 | 722 | test_issue_count |
|
695 | 723 | end |
|
696 | 724 | |
|
697 | 725 | def test_issue_count_by_association_group |
|
698 | 726 | q = Query.new(:name => '_', :group_by => 'assigned_to') |
|
699 | 727 | count_by_group = q.issue_count_by_group |
|
700 | 728 | assert_kind_of Hash, count_by_group |
|
701 | 729 | assert_equal %w(NilClass User), count_by_group.keys.collect {|k| k.class.name}.uniq.sort |
|
702 | 730 | assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq |
|
703 | 731 | assert count_by_group.has_key?(User.find(3)) |
|
704 | 732 | end |
|
705 | 733 | |
|
706 | 734 | def test_issue_count_by_list_custom_field_group |
|
707 | 735 | q = Query.new(:name => '_', :group_by => 'cf_1') |
|
708 | 736 | count_by_group = q.issue_count_by_group |
|
709 | 737 | assert_kind_of Hash, count_by_group |
|
710 | 738 | assert_equal %w(NilClass String), count_by_group.keys.collect {|k| k.class.name}.uniq.sort |
|
711 | 739 | assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq |
|
712 | 740 | assert count_by_group.has_key?('MySQL') |
|
713 | 741 | end |
|
714 | 742 | |
|
715 | 743 | def test_issue_count_by_date_custom_field_group |
|
716 | 744 | q = Query.new(:name => '_', :group_by => 'cf_8') |
|
717 | 745 | count_by_group = q.issue_count_by_group |
|
718 | 746 | assert_kind_of Hash, count_by_group |
|
719 | 747 | assert_equal %w(Date NilClass), count_by_group.keys.collect {|k| k.class.name}.uniq.sort |
|
720 | 748 | assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq |
|
721 | 749 | end |
|
722 | 750 | |
|
723 | 751 | def test_issue_count_with_nil_group_only |
|
724 | 752 | Issue.update_all("assigned_to_id = NULL") |
|
725 | 753 | |
|
726 | 754 | q = Query.new(:name => '_', :group_by => 'assigned_to') |
|
727 | 755 | count_by_group = q.issue_count_by_group |
|
728 | 756 | assert_kind_of Hash, count_by_group |
|
729 | 757 | assert_equal 1, count_by_group.keys.size |
|
730 | 758 | assert_nil count_by_group.keys.first |
|
731 | 759 | end |
|
732 | 760 | |
|
733 | 761 | def test_issue_ids |
|
734 | 762 | q = Query.new(:name => '_') |
|
735 | 763 | order = "issues.subject, issues.id" |
|
736 | 764 | issues = q.issues(:order => order) |
|
737 | 765 | assert_equal issues.map(&:id), q.issue_ids(:order => order) |
|
738 | 766 | end |
|
739 | 767 | |
|
740 | 768 | def test_label_for |
|
741 | 769 | q = Query.new |
|
742 | 770 | assert_equal 'Assignee', q.label_for('assigned_to_id') |
|
743 | 771 | end |
|
744 | 772 | |
|
745 | 773 | def test_editable_by |
|
746 | 774 | admin = User.find(1) |
|
747 | 775 | manager = User.find(2) |
|
748 | 776 | developer = User.find(3) |
|
749 | 777 | |
|
750 | 778 | # Public query on project 1 |
|
751 | 779 | q = Query.find(1) |
|
752 | 780 | assert q.editable_by?(admin) |
|
753 | 781 | assert q.editable_by?(manager) |
|
754 | 782 | assert !q.editable_by?(developer) |
|
755 | 783 | |
|
756 | 784 | # Private query on project 1 |
|
757 | 785 | q = Query.find(2) |
|
758 | 786 | assert q.editable_by?(admin) |
|
759 | 787 | assert !q.editable_by?(manager) |
|
760 | 788 | assert q.editable_by?(developer) |
|
761 | 789 | |
|
762 | 790 | # Private query for all projects |
|
763 | 791 | q = Query.find(3) |
|
764 | 792 | assert q.editable_by?(admin) |
|
765 | 793 | assert !q.editable_by?(manager) |
|
766 | 794 | assert q.editable_by?(developer) |
|
767 | 795 | |
|
768 | 796 | # Public query for all projects |
|
769 | 797 | q = Query.find(4) |
|
770 | 798 | assert q.editable_by?(admin) |
|
771 | 799 | assert !q.editable_by?(manager) |
|
772 | 800 | assert !q.editable_by?(developer) |
|
773 | 801 | end |
|
774 | 802 | |
|
775 | 803 | def test_visible_scope |
|
776 | 804 | query_ids = Query.visible(User.anonymous).map(&:id) |
|
777 | 805 | |
|
778 | 806 | assert query_ids.include?(1), 'public query on public project was not visible' |
|
779 | 807 | assert query_ids.include?(4), 'public query for all projects was not visible' |
|
780 | 808 | assert !query_ids.include?(2), 'private query on public project was visible' |
|
781 | 809 | assert !query_ids.include?(3), 'private query for all projects was visible' |
|
782 | 810 | assert !query_ids.include?(7), 'public query on private project was visible' |
|
783 | 811 | end |
|
784 | 812 | |
|
785 | 813 | context "#available_filters" do |
|
786 | 814 | setup do |
|
787 | 815 | @query = Query.new(:name => "_") |
|
788 | 816 | end |
|
789 | 817 | |
|
790 | 818 | should "include users of visible projects in cross-project view" do |
|
791 | 819 | users = @query.available_filters["assigned_to_id"] |
|
792 | 820 | assert_not_nil users |
|
793 | 821 | assert users[:values].map{|u|u[1]}.include?("3") |
|
794 | 822 | end |
|
795 | 823 | |
|
796 | 824 | should "include users of subprojects" do |
|
797 | 825 | user1 = User.generate! |
|
798 | 826 | user2 = User.generate! |
|
799 | 827 | project = Project.find(1) |
|
800 | 828 | Member.create!(:principal => user1, :project => project.children.visible.first, :role_ids => [1]) |
|
801 | 829 | @query.project = project |
|
802 | 830 | |
|
803 | 831 | users = @query.available_filters["assigned_to_id"] |
|
804 | 832 | assert_not_nil users |
|
805 | 833 | assert users[:values].map{|u|u[1]}.include?(user1.id.to_s) |
|
806 | 834 | assert !users[:values].map{|u|u[1]}.include?(user2.id.to_s) |
|
807 | 835 | end |
|
808 | 836 | |
|
809 | 837 | should "include visible projects in cross-project view" do |
|
810 | 838 | projects = @query.available_filters["project_id"] |
|
811 | 839 | assert_not_nil projects |
|
812 | 840 | assert projects[:values].map{|u|u[1]}.include?("1") |
|
813 | 841 | end |
|
814 | 842 | |
|
815 | 843 | context "'member_of_group' filter" do |
|
816 | 844 | should "be present" do |
|
817 | 845 | assert @query.available_filters.keys.include?("member_of_group") |
|
818 | 846 | end |
|
819 | 847 | |
|
820 | 848 | should "be an optional list" do |
|
821 | 849 | assert_equal :list_optional, @query.available_filters["member_of_group"][:type] |
|
822 | 850 | end |
|
823 | 851 | |
|
824 | 852 | should "have a list of the groups as values" do |
|
825 | 853 | Group.destroy_all # No fixtures |
|
826 | 854 | group1 = Group.generate!.reload |
|
827 | 855 | group2 = Group.generate!.reload |
|
828 | 856 | |
|
829 | 857 | expected_group_list = [ |
|
830 | 858 | [group1.name, group1.id.to_s], |
|
831 | 859 | [group2.name, group2.id.to_s] |
|
832 | 860 | ] |
|
833 | 861 | assert_equal expected_group_list.sort, @query.available_filters["member_of_group"][:values].sort |
|
834 | 862 | end |
|
835 | 863 | |
|
836 | 864 | end |
|
837 | 865 | |
|
838 | 866 | context "'assigned_to_role' filter" do |
|
839 | 867 | should "be present" do |
|
840 | 868 | assert @query.available_filters.keys.include?("assigned_to_role") |
|
841 | 869 | end |
|
842 | 870 | |
|
843 | 871 | should "be an optional list" do |
|
844 | 872 | assert_equal :list_optional, @query.available_filters["assigned_to_role"][:type] |
|
845 | 873 | end |
|
846 | 874 | |
|
847 | 875 | should "have a list of the Roles as values" do |
|
848 | 876 | assert @query.available_filters["assigned_to_role"][:values].include?(['Manager','1']) |
|
849 | 877 | assert @query.available_filters["assigned_to_role"][:values].include?(['Developer','2']) |
|
850 | 878 | assert @query.available_filters["assigned_to_role"][:values].include?(['Reporter','3']) |
|
851 | 879 | end |
|
852 | 880 | |
|
853 | 881 | should "not include the built in Roles as values" do |
|
854 | 882 | assert ! @query.available_filters["assigned_to_role"][:values].include?(['Non member','4']) |
|
855 | 883 | assert ! @query.available_filters["assigned_to_role"][:values].include?(['Anonymous','5']) |
|
856 | 884 | end |
|
857 | 885 | |
|
858 | 886 | end |
|
859 | 887 | |
|
860 | 888 | end |
|
861 | 889 | |
|
862 | 890 | context "#statement" do |
|
863 | 891 | context "with 'member_of_group' filter" do |
|
864 | 892 | setup do |
|
865 | 893 | Group.destroy_all # No fixtures |
|
866 | 894 | @user_in_group = User.generate! |
|
867 | 895 | @second_user_in_group = User.generate! |
|
868 | 896 | @user_in_group2 = User.generate! |
|
869 | 897 | @user_not_in_group = User.generate! |
|
870 | 898 | |
|
871 | 899 | @group = Group.generate!.reload |
|
872 | 900 | @group.users << @user_in_group |
|
873 | 901 | @group.users << @second_user_in_group |
|
874 | 902 | |
|
875 | 903 | @group2 = Group.generate!.reload |
|
876 | 904 | @group2.users << @user_in_group2 |
|
877 | 905 | |
|
878 | 906 | end |
|
879 | 907 | |
|
880 | 908 | should "search assigned to for users in the group" do |
|
881 | 909 | @query = Query.new(:name => '_') |
|
882 | 910 | @query.add_filter('member_of_group', '=', [@group.id.to_s]) |
|
883 | 911 | |
|
884 | 912 | assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@user_in_group.id}','#{@second_user_in_group.id}')" |
|
885 | 913 | assert_find_issues_with_query_is_successful @query |
|
886 | 914 | end |
|
887 | 915 | |
|
888 | 916 | should "search not assigned to any group member (none)" do |
|
889 | 917 | @query = Query.new(:name => '_') |
|
890 | 918 | @query.add_filter('member_of_group', '!*', ['']) |
|
891 | 919 | |
|
892 | 920 | # Users not in a group |
|
893 | 921 | 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}')" |
|
894 | 922 | assert_find_issues_with_query_is_successful @query |
|
895 | 923 | end |
|
896 | 924 | |
|
897 | 925 | should "search assigned to any group member (all)" do |
|
898 | 926 | @query = Query.new(:name => '_') |
|
899 | 927 | @query.add_filter('member_of_group', '*', ['']) |
|
900 | 928 | |
|
901 | 929 | # Only users in a group |
|
902 | 930 | 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}')" |
|
903 | 931 | assert_find_issues_with_query_is_successful @query |
|
904 | 932 | end |
|
905 | 933 | |
|
906 | 934 | should "return an empty set with = empty group" do |
|
907 | 935 | @empty_group = Group.generate! |
|
908 | 936 | @query = Query.new(:name => '_') |
|
909 | 937 | @query.add_filter('member_of_group', '=', [@empty_group.id.to_s]) |
|
910 | 938 | |
|
911 | 939 | assert_equal [], find_issues_with_query(@query) |
|
912 | 940 | end |
|
913 | 941 | |
|
914 | 942 | should "return issues with ! empty group" do |
|
915 | 943 | @empty_group = Group.generate! |
|
916 | 944 | @query = Query.new(:name => '_') |
|
917 | 945 | @query.add_filter('member_of_group', '!', [@empty_group.id.to_s]) |
|
918 | 946 | |
|
919 | 947 | assert_find_issues_with_query_is_successful @query |
|
920 | 948 | end |
|
921 | 949 | end |
|
922 | 950 | |
|
923 | 951 | context "with 'assigned_to_role' filter" do |
|
924 | 952 | setup do |
|
925 | 953 | @manager_role = Role.find_by_name('Manager') |
|
926 | 954 | @developer_role = Role.find_by_name('Developer') |
|
927 | 955 | |
|
928 | 956 | @project = Project.generate! |
|
929 | 957 | @manager = User.generate! |
|
930 | 958 | @developer = User.generate! |
|
931 | 959 | @boss = User.generate! |
|
932 | 960 | @guest = User.generate! |
|
933 | 961 | User.add_to_project(@manager, @project, @manager_role) |
|
934 | 962 | User.add_to_project(@developer, @project, @developer_role) |
|
935 | 963 | User.add_to_project(@boss, @project, [@manager_role, @developer_role]) |
|
936 | 964 | |
|
937 | 965 | @issue1 = Issue.generate_for_project!(@project, :assigned_to_id => @manager.id) |
|
938 | 966 | @issue2 = Issue.generate_for_project!(@project, :assigned_to_id => @developer.id) |
|
939 | 967 | @issue3 = Issue.generate_for_project!(@project, :assigned_to_id => @boss.id) |
|
940 | 968 | @issue4 = Issue.generate_for_project!(@project, :assigned_to_id => @guest.id) |
|
941 | 969 | @issue5 = Issue.generate_for_project!(@project) |
|
942 | 970 | end |
|
943 | 971 | |
|
944 | 972 | should "search assigned to for users with the Role" do |
|
945 | 973 | @query = Query.new(:name => '_', :project => @project) |
|
946 | 974 | @query.add_filter('assigned_to_role', '=', [@manager_role.id.to_s]) |
|
947 | 975 | |
|
948 | 976 | assert_query_result [@issue1, @issue3], @query |
|
949 | 977 | end |
|
950 | 978 | |
|
951 | 979 | should "search assigned to for users with the Role on the issue project" do |
|
952 | 980 | other_project = Project.generate! |
|
953 | 981 | User.add_to_project(@developer, other_project, @manager_role) |
|
954 | 982 | |
|
955 | 983 | @query = Query.new(:name => '_', :project => @project) |
|
956 | 984 | @query.add_filter('assigned_to_role', '=', [@manager_role.id.to_s]) |
|
957 | 985 | |
|
958 | 986 | assert_query_result [@issue1, @issue3], @query |
|
959 | 987 | end |
|
960 | 988 | |
|
961 | 989 | should "return an empty set with empty role" do |
|
962 | 990 | @empty_role = Role.generate! |
|
963 | 991 | @query = Query.new(:name => '_', :project => @project) |
|
964 | 992 | @query.add_filter('assigned_to_role', '=', [@empty_role.id.to_s]) |
|
965 | 993 | |
|
966 | 994 | assert_query_result [], @query |
|
967 | 995 | end |
|
968 | 996 | |
|
969 | 997 | should "search assigned to for users without the Role" do |
|
970 | 998 | @query = Query.new(:name => '_', :project => @project) |
|
971 | 999 | @query.add_filter('assigned_to_role', '!', [@manager_role.id.to_s]) |
|
972 | 1000 | |
|
973 | 1001 | assert_query_result [@issue2, @issue4, @issue5], @query |
|
974 | 1002 | end |
|
975 | 1003 | |
|
976 | 1004 | should "search assigned to for users not assigned to any Role (none)" do |
|
977 | 1005 | @query = Query.new(:name => '_', :project => @project) |
|
978 | 1006 | @query.add_filter('assigned_to_role', '!*', ['']) |
|
979 | 1007 | |
|
980 | 1008 | assert_query_result [@issue4, @issue5], @query |
|
981 | 1009 | end |
|
982 | 1010 | |
|
983 | 1011 | should "search assigned to for users assigned to any Role (all)" do |
|
984 | 1012 | @query = Query.new(:name => '_', :project => @project) |
|
985 | 1013 | @query.add_filter('assigned_to_role', '*', ['']) |
|
986 | 1014 | |
|
987 | 1015 | assert_query_result [@issue1, @issue2, @issue3], @query |
|
988 | 1016 | end |
|
989 | 1017 | |
|
990 | 1018 | should "return issues with ! empty role" do |
|
991 | 1019 | @empty_role = Role.generate! |
|
992 | 1020 | @query = Query.new(:name => '_', :project => @project) |
|
993 | 1021 | @query.add_filter('assigned_to_role', '!', [@empty_role.id.to_s]) |
|
994 | 1022 | |
|
995 | 1023 | assert_query_result [@issue1, @issue2, @issue3, @issue4, @issue5], @query |
|
996 | 1024 | end |
|
997 | 1025 | end |
|
998 | 1026 | end |
|
999 | 1027 | |
|
1000 | 1028 | end |
General Comments 0
You need to be logged in to leave comments.
Login now