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