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