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