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