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