##// END OF EJS Templates
Refactor: makes issue id a regular QueryColumn....
Jean-Philippe Lang -
r11217:9b1ebd6808d2
parent child
Show More
@@ -1,393 +1,393
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2013 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 module IssuesHelper
21 21 include ApplicationHelper
22 22
23 23 def issue_list(issues, &block)
24 24 ancestors = []
25 25 issues.each do |issue|
26 26 while (ancestors.any? && !issue.is_descendant_of?(ancestors.last))
27 27 ancestors.pop
28 28 end
29 29 yield issue, ancestors.size
30 30 ancestors << issue unless issue.leaf?
31 31 end
32 32 end
33 33
34 34 # Renders a HTML/CSS tooltip
35 35 #
36 36 # To use, a trigger div is needed. This is a div with the class of "tooltip"
37 37 # that contains this method wrapped in a span with the class of "tip"
38 38 #
39 39 # <div class="tooltip"><%= link_to_issue(issue) %>
40 40 # <span class="tip"><%= render_issue_tooltip(issue) %></span>
41 41 # </div>
42 42 #
43 43 def render_issue_tooltip(issue)
44 44 @cached_label_status ||= l(:field_status)
45 45 @cached_label_start_date ||= l(:field_start_date)
46 46 @cached_label_due_date ||= l(:field_due_date)
47 47 @cached_label_assigned_to ||= l(:field_assigned_to)
48 48 @cached_label_priority ||= l(:field_priority)
49 49 @cached_label_project ||= l(:field_project)
50 50
51 51 link_to_issue(issue) + "<br /><br />".html_safe +
52 52 "<strong>#{@cached_label_project}</strong>: #{link_to_project(issue.project)}<br />".html_safe +
53 53 "<strong>#{@cached_label_status}</strong>: #{h(issue.status.name)}<br />".html_safe +
54 54 "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />".html_safe +
55 55 "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />".html_safe +
56 56 "<strong>#{@cached_label_assigned_to}</strong>: #{h(issue.assigned_to)}<br />".html_safe +
57 57 "<strong>#{@cached_label_priority}</strong>: #{h(issue.priority.name)}".html_safe
58 58 end
59 59
60 60 def issue_heading(issue)
61 61 h("#{issue.tracker} ##{issue.id}")
62 62 end
63 63
64 64 def render_issue_subject_with_tree(issue)
65 65 s = ''
66 66 ancestors = issue.root? ? [] : issue.ancestors.visible.all
67 67 ancestors.each do |ancestor|
68 68 s << '<div>' + content_tag('p', link_to_issue(ancestor, :project => (issue.project_id != ancestor.project_id)))
69 69 end
70 70 s << '<div>'
71 71 subject = h(issue.subject)
72 72 if issue.is_private?
73 73 subject = content_tag('span', l(:field_is_private), :class => 'private') + ' ' + subject
74 74 end
75 75 s << content_tag('h3', subject)
76 76 s << '</div>' * (ancestors.size + 1)
77 77 s.html_safe
78 78 end
79 79
80 80 def render_descendants_tree(issue)
81 81 s = '<form><table class="list issues">'
82 82 issue_list(issue.descendants.visible.sort_by(&:lft)) do |child, level|
83 83 css = "issue issue-#{child.id} hascontextmenu"
84 84 css << " idnt idnt-#{level}" if level > 0
85 85 s << content_tag('tr',
86 86 content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') +
87 87 content_tag('td', link_to_issue(child, :truncate => 60, :project => (issue.project_id != child.project_id)), :class => 'subject') +
88 88 content_tag('td', h(child.status)) +
89 89 content_tag('td', link_to_user(child.assigned_to)) +
90 90 content_tag('td', progress_bar(child.done_ratio, :width => '80px')),
91 91 :class => css)
92 92 end
93 93 s << '</table></form>'
94 94 s.html_safe
95 95 end
96 96
97 97 # Returns a link for adding a new subtask to the given issue
98 98 def link_to_new_subtask(issue)
99 99 attrs = {
100 100 :tracker_id => issue.tracker,
101 101 :parent_issue_id => issue
102 102 }
103 103 link_to(l(:button_add), new_project_issue_path(issue.project, :issue => attrs))
104 104 end
105 105
106 106 class IssueFieldsRows
107 107 include ActionView::Helpers::TagHelper
108 108
109 109 def initialize
110 110 @left = []
111 111 @right = []
112 112 end
113 113
114 114 def left(*args)
115 115 args.any? ? @left << cells(*args) : @left
116 116 end
117 117
118 118 def right(*args)
119 119 args.any? ? @right << cells(*args) : @right
120 120 end
121 121
122 122 def size
123 123 @left.size > @right.size ? @left.size : @right.size
124 124 end
125 125
126 126 def to_html
127 127 html = ''.html_safe
128 128 blank = content_tag('th', '') + content_tag('td', '')
129 129 size.times do |i|
130 130 left = @left[i] || blank
131 131 right = @right[i] || blank
132 132 html << content_tag('tr', left + right)
133 133 end
134 134 html
135 135 end
136 136
137 137 def cells(label, text, options={})
138 138 content_tag('th', "#{label}:", options) + content_tag('td', text, options)
139 139 end
140 140 end
141 141
142 142 def issue_fields_rows
143 143 r = IssueFieldsRows.new
144 144 yield r
145 145 r.to_html
146 146 end
147 147
148 148 def render_custom_fields_rows(issue)
149 149 return if issue.custom_field_values.empty?
150 150 ordered_values = []
151 151 half = (issue.custom_field_values.size / 2.0).ceil
152 152 half.times do |i|
153 153 ordered_values << issue.custom_field_values[i]
154 154 ordered_values << issue.custom_field_values[i + half]
155 155 end
156 156 s = "<tr>\n"
157 157 n = 0
158 158 ordered_values.compact.each do |value|
159 159 s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
160 160 s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
161 161 n += 1
162 162 end
163 163 s << "</tr>\n"
164 164 s.html_safe
165 165 end
166 166
167 167 def issues_destroy_confirmation_message(issues)
168 168 issues = [issues] unless issues.is_a?(Array)
169 169 message = l(:text_issues_destroy_confirmation)
170 170 descendant_count = issues.inject(0) {|memo, i| memo += (i.right - i.left - 1)/2}
171 171 if descendant_count > 0
172 172 issues.each do |issue|
173 173 next if issue.root?
174 174 issues.each do |other_issue|
175 175 descendant_count -= 1 if issue.is_descendant_of?(other_issue)
176 176 end
177 177 end
178 178 if descendant_count > 0
179 179 message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
180 180 end
181 181 end
182 182 message
183 183 end
184 184
185 185 def sidebar_queries
186 186 unless @sidebar_queries
187 187 @sidebar_queries = IssueQuery.visible.all(
188 188 :order => "#{Query.table_name}.name ASC",
189 189 # Project specific queries and global queries
190 190 :conditions => (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id])
191 191 )
192 192 end
193 193 @sidebar_queries
194 194 end
195 195
196 196 def query_links(title, queries)
197 197 # links to #index on issues/show
198 198 url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params
199 199
200 200 content_tag('h3', h(title)) +
201 201 queries.collect {|query|
202 202 css = 'query'
203 203 css << ' selected' if query == @query
204 204 link_to(h(query.name), url_params.merge(:query_id => query), :class => css)
205 205 }.join('<br />').html_safe
206 206 end
207 207
208 208 def render_sidebar_queries
209 209 out = ''.html_safe
210 210 queries = sidebar_queries.select {|q| !q.is_public?}
211 211 out << query_links(l(:label_my_queries), queries) if queries.any?
212 212 queries = sidebar_queries.select {|q| q.is_public?}
213 213 out << query_links(l(:label_query_plural), queries) if queries.any?
214 214 out
215 215 end
216 216
217 217 # Returns the textual representation of a journal details
218 218 # as an array of strings
219 219 def details_to_strings(details, no_html=false, options={})
220 220 options[:only_path] = (options[:only_path] == false ? false : true)
221 221 strings = []
222 222 values_by_field = {}
223 223 details.each do |detail|
224 224 if detail.property == 'cf'
225 225 field_id = detail.prop_key
226 226 field = CustomField.find_by_id(field_id)
227 227 if field && field.multiple?
228 228 values_by_field[field_id] ||= {:added => [], :deleted => []}
229 229 if detail.old_value
230 230 values_by_field[field_id][:deleted] << detail.old_value
231 231 end
232 232 if detail.value
233 233 values_by_field[field_id][:added] << detail.value
234 234 end
235 235 next
236 236 end
237 237 end
238 238 strings << show_detail(detail, no_html, options)
239 239 end
240 240 values_by_field.each do |field_id, changes|
241 241 detail = JournalDetail.new(:property => 'cf', :prop_key => field_id)
242 242 if changes[:added].any?
243 243 detail.value = changes[:added]
244 244 strings << show_detail(detail, no_html, options)
245 245 elsif changes[:deleted].any?
246 246 detail.old_value = changes[:deleted]
247 247 strings << show_detail(detail, no_html, options)
248 248 end
249 249 end
250 250 strings
251 251 end
252 252
253 253 # Returns the textual representation of a single journal detail
254 254 def show_detail(detail, no_html=false, options={})
255 255 multiple = false
256 256 case detail.property
257 257 when 'attr'
258 258 field = detail.prop_key.to_s.gsub(/\_id$/, "")
259 259 label = l(("field_" + field).to_sym)
260 260 case detail.prop_key
261 261 when 'due_date', 'start_date'
262 262 value = format_date(detail.value.to_date) if detail.value
263 263 old_value = format_date(detail.old_value.to_date) if detail.old_value
264 264
265 265 when 'project_id', 'status_id', 'tracker_id', 'assigned_to_id',
266 266 'priority_id', 'category_id', 'fixed_version_id'
267 267 value = find_name_by_reflection(field, detail.value)
268 268 old_value = find_name_by_reflection(field, detail.old_value)
269 269
270 270 when 'estimated_hours'
271 271 value = "%0.02f" % detail.value.to_f unless detail.value.blank?
272 272 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
273 273
274 274 when 'parent_id'
275 275 label = l(:field_parent_issue)
276 276 value = "##{detail.value}" unless detail.value.blank?
277 277 old_value = "##{detail.old_value}" unless detail.old_value.blank?
278 278
279 279 when 'is_private'
280 280 value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
281 281 old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
282 282 end
283 283 when 'cf'
284 284 custom_field = CustomField.find_by_id(detail.prop_key)
285 285 if custom_field
286 286 multiple = custom_field.multiple?
287 287 label = custom_field.name
288 288 value = format_value(detail.value, custom_field.field_format) if detail.value
289 289 old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value
290 290 end
291 291 when 'attachment'
292 292 label = l(:label_attachment)
293 293 end
294 294 call_hook(:helper_issues_show_detail_after_setting,
295 295 {:detail => detail, :label => label, :value => value, :old_value => old_value })
296 296
297 297 label ||= detail.prop_key
298 298 value ||= detail.value
299 299 old_value ||= detail.old_value
300 300
301 301 unless no_html
302 302 label = content_tag('strong', label)
303 303 old_value = content_tag("i", h(old_value)) if detail.old_value
304 304 old_value = content_tag("del", old_value) if detail.old_value and detail.value.blank?
305 305 if detail.property == 'attachment' && !value.blank? && atta = Attachment.find_by_id(detail.prop_key)
306 306 # Link to the attachment if it has not been removed
307 307 value = link_to_attachment(atta, :download => true, :only_path => options[:only_path])
308 308 if options[:only_path] != false && atta.is_text?
309 309 value += link_to(
310 310 image_tag('magnifier.png'),
311 311 :controller => 'attachments', :action => 'show',
312 312 :id => atta, :filename => atta.filename
313 313 )
314 314 end
315 315 else
316 316 value = content_tag("i", h(value)) if value
317 317 end
318 318 end
319 319
320 320 if detail.property == 'attr' && detail.prop_key == 'description'
321 321 s = l(:text_journal_changed_no_detail, :label => label)
322 322 unless no_html
323 323 diff_link = link_to 'diff',
324 324 {:controller => 'journals', :action => 'diff', :id => detail.journal_id,
325 325 :detail_id => detail.id, :only_path => options[:only_path]},
326 326 :title => l(:label_view_diff)
327 327 s << " (#{ diff_link })"
328 328 end
329 329 s.html_safe
330 330 elsif detail.value.present?
331 331 case detail.property
332 332 when 'attr', 'cf'
333 333 if detail.old_value.present?
334 334 l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
335 335 elsif multiple
336 336 l(:text_journal_added, :label => label, :value => value).html_safe
337 337 else
338 338 l(:text_journal_set_to, :label => label, :value => value).html_safe
339 339 end
340 340 when 'attachment'
341 341 l(:text_journal_added, :label => label, :value => value).html_safe
342 342 end
343 343 else
344 344 l(:text_journal_deleted, :label => label, :old => old_value).html_safe
345 345 end
346 346 end
347 347
348 348 # Find the name of an associated record stored in the field attribute
349 349 def find_name_by_reflection(field, id)
350 350 unless id.present?
351 351 return nil
352 352 end
353 353 association = Issue.reflect_on_association(field.to_sym)
354 354 if association
355 355 record = association.class_name.constantize.find_by_id(id)
356 356 return record.name if record
357 357 end
358 358 end
359 359
360 360 # Renders issue children recursively
361 361 def render_api_issue_children(issue, api)
362 362 return if issue.leaf?
363 363 api.array :children do
364 364 issue.children.each do |child|
365 365 api.issue(:id => child.id) do
366 366 api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
367 367 api.subject child.subject
368 368 render_api_issue_children(child, api)
369 369 end
370 370 end
371 371 end
372 372 end
373 373
374 374 def issues_to_csv(issues, project, query, options={})
375 375 encoding = l(:general_csv_encoding)
376 376 columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns)
377 377 if options[:description]
378 378 if description = query.available_columns.detect {|q| q.name == :description}
379 379 columns << description
380 380 end
381 381 end
382 382
383 383 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
384 384 # csv header fields
385 csv << [ "#" ] + columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) }
385 csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) }
386 386 # csv lines
387 387 issues.each do |issue|
388 csv << [ issue.id.to_s ] + columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, issue), encoding) }
388 csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, issue), encoding) }
389 389 end
390 390 end
391 391 export
392 392 end
393 393 end
@@ -1,164 +1,166
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2013 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 module QueriesHelper
21 21 def filters_options_for_select(query)
22 22 options_for_select(filters_options(query))
23 23 end
24 24
25 25 def filters_options(query)
26 26 options = [[]]
27 27 options += query.available_filters.map do |field, field_options|
28 28 [field_options[:name], field]
29 29 end
30 30 end
31 31
32 32 def available_block_columns_tags(query)
33 33 tags = ''.html_safe
34 34 query.available_block_columns.each do |column|
35 35 tags << content_tag('label', check_box_tag('c[]', column.name.to_s, query.has_column?(column)) + " #{column.caption}", :class => 'inline')
36 36 end
37 37 tags
38 38 end
39 39
40 40 def column_header(column)
41 41 column.sortable ? sort_header_tag(column.name.to_s, :caption => column.caption,
42 42 :default_order => column.default_order) :
43 43 content_tag('th', h(column.caption))
44 44 end
45 45
46 46 def column_content(column, issue)
47 47 value = column.value(issue)
48 48 if value.is_a?(Array)
49 49 value.collect {|v| column_value(column, issue, v)}.compact.join(', ').html_safe
50 50 else
51 51 column_value(column, issue, value)
52 52 end
53 53 end
54 54
55 55 def column_value(column, issue, value)
56 56 case value.class.name
57 57 when 'String'
58 58 if column.name == :subject
59 59 link_to(h(value), :controller => 'issues', :action => 'show', :id => issue)
60 60 elsif column.name == :description
61 61 issue.description? ? content_tag('div', textilizable(issue, :description), :class => "wiki") : ''
62 62 else
63 63 h(value)
64 64 end
65 65 when 'Time'
66 66 format_time(value)
67 67 when 'Date'
68 68 format_date(value)
69 69 when 'Fixnum'
70 if column.name == :done_ratio
70 if column.name == :id
71 link_to value, issue_path(issue)
72 elsif column.name == :done_ratio
71 73 progress_bar(value, :width => '80px')
72 74 else
73 75 value.to_s
74 76 end
75 77 when 'Float'
76 78 sprintf "%.2f", value
77 79 when 'User'
78 80 link_to_user value
79 81 when 'Project'
80 82 link_to_project value
81 83 when 'Version'
82 84 link_to(h(value), :controller => 'versions', :action => 'show', :id => value)
83 85 when 'TrueClass'
84 86 l(:general_text_Yes)
85 87 when 'FalseClass'
86 88 l(:general_text_No)
87 89 when 'Issue'
88 90 value.visible? ? link_to_issue(value) : "##{value.id}"
89 91 when 'IssueRelation'
90 92 other = value.other_issue(issue)
91 93 content_tag('span',
92 94 (l(value.label_for(issue)) + " " + link_to_issue(other, :subject => false, :tracker => false)).html_safe,
93 95 :class => value.css_classes_for(issue))
94 96 else
95 97 h(value)
96 98 end
97 99 end
98 100
99 101 def csv_content(column, issue)
100 102 value = column.value(issue)
101 103 if value.is_a?(Array)
102 104 value.collect {|v| csv_value(column, issue, v)}.compact.join(', ')
103 105 else
104 106 csv_value(column, issue, value)
105 107 end
106 108 end
107 109
108 110 def csv_value(column, issue, value)
109 111 case value.class.name
110 112 when 'Time'
111 113 format_time(value)
112 114 when 'Date'
113 115 format_date(value)
114 116 when 'Float'
115 117 sprintf("%.2f", value).gsub('.', l(:general_csv_decimal_separator))
116 118 when 'IssueRelation'
117 119 other = value.other_issue(issue)
118 120 l(value.label_for(issue)) + " ##{other.id}"
119 121 else
120 122 value.to_s
121 123 end
122 124 end
123 125
124 126 # Retrieve query from session or build a new query
125 127 def retrieve_query
126 128 if !params[:query_id].blank?
127 129 cond = "project_id IS NULL"
128 130 cond << " OR project_id = #{@project.id}" if @project
129 131 @query = IssueQuery.find(params[:query_id], :conditions => cond)
130 132 raise ::Unauthorized unless @query.visible?
131 133 @query.project = @project
132 134 session[:query] = {:id => @query.id, :project_id => @query.project_id}
133 135 sort_clear
134 136 elsif api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
135 137 # Give it a name, required to be valid
136 138 @query = IssueQuery.new(:name => "_")
137 139 @query.project = @project
138 140 @query.build_from_params(params)
139 141 session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names}
140 142 else
141 143 # retrieve from session
142 144 @query = IssueQuery.find_by_id(session[:query][:id]) if session[:query][:id]
143 145 @query ||= IssueQuery.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
144 146 @query.project = @project
145 147 end
146 148 end
147 149
148 150 def retrieve_query_from_session
149 151 if session[:query]
150 152 if session[:query][:id]
151 153 @query = IssueQuery.find_by_id(session[:query][:id])
152 154 return unless @query
153 155 else
154 156 @query = IssueQuery.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
155 157 end
156 158 if session[:query].has_key?(:project_id)
157 159 @query.project_id = session[:query][:project_id]
158 160 else
159 161 @query.project = @project
160 162 end
161 163 @query
162 164 end
163 165 end
164 166 end
@@ -1,408 +1,405
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class IssueQuery < Query
19 19
20 20 self.queried_class = Issue
21 21
22 22 self.available_columns = [
23 QueryColumn.new(:id, :sortable => "#{Issue.table_name}.id", :default_order => 'desc', :caption => '#', :frozen => true),
23 24 QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
24 25 QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true),
25 26 QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue),
26 27 QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true),
27 28 QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true),
28 29 QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"),
29 30 QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true),
30 31 QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
31 32 QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'),
32 33 QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true),
33 34 QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true),
34 35 QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"),
35 36 QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"),
36 37 QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"),
37 38 QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true),
38 39 QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'),
39 40 QueryColumn.new(:closed_on, :sortable => "#{Issue.table_name}.closed_on", :default_order => 'desc'),
40 41 QueryColumn.new(:relations, :caption => :label_related_issues),
41 42 QueryColumn.new(:description, :inline => false)
42 43 ]
43 44
44 45 scope :visible, lambda {|*args|
45 46 user = args.shift || User.current
46 47 base = Project.allowed_to_condition(user, :view_issues, *args)
47 48 user_id = user.logged? ? user.id : 0
48 49
49 50 includes(:project).where("(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id)
50 51 }
51 52
52 53 def initialize(attributes=nil, *args)
53 54 super attributes
54 55 self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} }
55 56 end
56 57
57 58 # Returns true if the query is visible to +user+ or the current user.
58 59 def visible?(user=User.current)
59 60 (project.nil? || user.allowed_to?(:view_issues, project)) && (self.is_public? || self.user_id == user.id)
60 61 end
61 62
62 63 def initialize_available_filters
63 64 principals = []
64 65 subprojects = []
65 66 versions = []
66 67 categories = []
67 68 issue_custom_fields = []
68 69
69 70 if project
70 71 principals += project.principals.sort
71 72 unless project.leaf?
72 73 subprojects = project.descendants.visible.all
73 74 principals += Principal.member_of(subprojects)
74 75 end
75 76 versions = project.shared_versions.all
76 77 categories = project.issue_categories.all
77 78 issue_custom_fields = project.all_issue_custom_fields
78 79 else
79 80 if all_projects.any?
80 81 principals += Principal.member_of(all_projects)
81 82 end
82 83 versions = Version.visible.find_all_by_sharing('system')
83 84 issue_custom_fields = IssueCustomField.where(:is_filter => true, :is_for_all => true).all
84 85 end
85 86 principals.uniq!
86 87 principals.sort!
87 88 users = principals.select {|p| p.is_a?(User)}
88 89
89 90
90 91 add_available_filter "status_id",
91 92 :type => :list_status, :values => IssueStatus.sorted.all.collect{|s| [s.name, s.id.to_s] }
92 93
93 94 if project.nil?
94 95 project_values = []
95 96 if User.current.logged? && User.current.memberships.any?
96 97 project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
97 98 end
98 99 project_values += all_projects_values
99 100 add_available_filter("project_id",
100 101 :type => :list, :values => project_values
101 102 ) unless project_values.empty?
102 103 end
103 104
104 105 add_available_filter "tracker_id",
105 106 :type => :list, :values => trackers.collect{|s| [s.name, s.id.to_s] }
106 107 add_available_filter "priority_id",
107 108 :type => :list, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] }
108 109
109 110 author_values = []
110 111 author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
111 112 author_values += users.collect{|s| [s.name, s.id.to_s] }
112 113 add_available_filter("author_id",
113 114 :type => :list, :values => author_values
114 115 ) unless author_values.empty?
115 116
116 117 assigned_to_values = []
117 118 assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
118 119 assigned_to_values += (Setting.issue_group_assignment? ?
119 120 principals : users).collect{|s| [s.name, s.id.to_s] }
120 121 add_available_filter("assigned_to_id",
121 122 :type => :list_optional, :values => assigned_to_values
122 123 ) unless assigned_to_values.empty?
123 124
124 125 group_values = Group.all.collect {|g| [g.name, g.id.to_s] }
125 126 add_available_filter("member_of_group",
126 127 :type => :list_optional, :values => group_values
127 128 ) unless group_values.empty?
128 129
129 130 role_values = Role.givable.collect {|r| [r.name, r.id.to_s] }
130 131 add_available_filter("assigned_to_role",
131 132 :type => :list_optional, :values => role_values
132 133 ) unless role_values.empty?
133 134
134 135 if versions.any?
135 136 add_available_filter "fixed_version_id",
136 137 :type => :list_optional,
137 138 :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] }
138 139 end
139 140
140 141 if categories.any?
141 142 add_available_filter "category_id",
142 143 :type => :list_optional,
143 144 :values => categories.collect{|s| [s.name, s.id.to_s] }
144 145 end
145 146
146 147 add_available_filter "subject", :type => :text
147 148 add_available_filter "created_on", :type => :date_past
148 149 add_available_filter "updated_on", :type => :date_past
149 150 add_available_filter "closed_on", :type => :date_past
150 151 add_available_filter "start_date", :type => :date
151 152 add_available_filter "due_date", :type => :date
152 153 add_available_filter "estimated_hours", :type => :float
153 154 add_available_filter "done_ratio", :type => :integer
154 155
155 156 if User.current.allowed_to?(:set_issues_private, nil, :global => true) ||
156 157 User.current.allowed_to?(:set_own_issues_private, nil, :global => true)
157 158 add_available_filter "is_private",
158 159 :type => :list,
159 160 :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]]
160 161 end
161 162
162 163 if User.current.logged?
163 164 add_available_filter "watcher_id",
164 165 :type => :list, :values => [["<< #{l(:label_me)} >>", "me"]]
165 166 end
166 167
167 168 if subprojects.any?
168 169 add_available_filter "subproject_id",
169 170 :type => :list_subprojects,
170 171 :values => subprojects.collect{|s| [s.name, s.id.to_s] }
171 172 end
172 173
173 174 add_custom_fields_filters(issue_custom_fields)
174 175
175 176 add_associations_custom_fields_filters :project, :author, :assigned_to, :fixed_version
176 177
177 178 IssueRelation::TYPES.each do |relation_type, options|
178 179 add_available_filter relation_type, :type => :relation, :label => options[:name]
179 180 end
180 181
181 182 Tracker.disabled_core_fields(trackers).each {|field|
182 183 delete_available_filter field
183 184 }
184 185 end
185 186
186 187 def available_columns
187 188 return @available_columns if @available_columns
188 189 @available_columns = self.class.available_columns.dup
189 190 @available_columns += (project ?
190 191 project.all_issue_custom_fields :
191 192 IssueCustomField.all
192 193 ).collect {|cf| QueryCustomFieldColumn.new(cf) }
193 194
194 195 if User.current.allowed_to?(:view_time_entries, project, :global => true)
195 196 index = nil
196 197 @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours}
197 198 index = (index ? index + 1 : -1)
198 199 # insert the column after estimated_hours or at the end
199 200 @available_columns.insert index, QueryColumn.new(:spent_hours,
200 201 :sortable => "COALESCE((SELECT SUM(hours) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id), 0)",
201 202 :default_order => 'desc',
202 203 :caption => :label_spent_time
203 204 )
204 205 end
205 206
206 207 if User.current.allowed_to?(:set_issues_private, nil, :global => true) ||
207 208 User.current.allowed_to?(:set_own_issues_private, nil, :global => true)
208 209 @available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private")
209 210 end
210 211
211 212 disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')}
212 213 @available_columns.reject! {|column|
213 214 disabled_fields.include?(column.name.to_s)
214 215 }
215 216
216 217 @available_columns
217 218 end
218 219
219 def sortable_columns
220 {'id' => "#{Issue.table_name}.id"}.merge(super)
221 end
222
223 220 def default_columns_names
224 221 @default_columns_names ||= begin
225 222 default_columns = Setting.issue_list_default_columns.map(&:to_sym)
226 223
227 224 project.present? ? default_columns : [:project] | default_columns
228 225 end
229 226 end
230 227
231 228 # Returns the issue count
232 229 def issue_count
233 230 Issue.visible.count(:include => [:status, :project], :conditions => statement)
234 231 rescue ::ActiveRecord::StatementInvalid => e
235 232 raise StatementInvalid.new(e.message)
236 233 end
237 234
238 235 # Returns the issue count by group or nil if query is not grouped
239 236 def issue_count_by_group
240 237 r = nil
241 238 if grouped?
242 239 begin
243 240 # Rails3 will raise an (unexpected) RecordNotFound if there's only a nil group value
244 241 r = Issue.visible.count(:joins => joins_for_order_statement(group_by_statement), :group => group_by_statement, :include => [:status, :project], :conditions => statement)
245 242 rescue ActiveRecord::RecordNotFound
246 243 r = {nil => issue_count}
247 244 end
248 245 c = group_by_column
249 246 if c.is_a?(QueryCustomFieldColumn)
250 247 r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h}
251 248 end
252 249 end
253 250 r
254 251 rescue ::ActiveRecord::StatementInvalid => e
255 252 raise StatementInvalid.new(e.message)
256 253 end
257 254
258 255 # Returns the issues
259 256 # Valid options are :order, :offset, :limit, :include, :conditions
260 257 def issues(options={})
261 258 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
262 259
263 260 issues = Issue.visible.where(options[:conditions]).all(
264 261 :include => ([:status, :project] + (options[:include] || [])).uniq,
265 262 :conditions => statement,
266 263 :order => order_option,
267 264 :joins => joins_for_order_statement(order_option.join(',')),
268 265 :limit => options[:limit],
269 266 :offset => options[:offset]
270 267 )
271 268
272 269 if has_column?(:spent_hours)
273 270 Issue.load_visible_spent_hours(issues)
274 271 end
275 272 if has_column?(:relations)
276 273 Issue.load_visible_relations(issues)
277 274 end
278 275 issues
279 276 rescue ::ActiveRecord::StatementInvalid => e
280 277 raise StatementInvalid.new(e.message)
281 278 end
282 279
283 280 # Returns the issues ids
284 281 def issue_ids(options={})
285 282 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
286 283
287 284 Issue.visible.scoped(:conditions => options[:conditions]).scoped(:include => ([:status, :project] + (options[:include] || [])).uniq,
288 285 :conditions => statement,
289 286 :order => order_option,
290 287 :joins => joins_for_order_statement(order_option.join(',')),
291 288 :limit => options[:limit],
292 289 :offset => options[:offset]).find_ids
293 290 rescue ::ActiveRecord::StatementInvalid => e
294 291 raise StatementInvalid.new(e.message)
295 292 end
296 293
297 294 # Returns the journals
298 295 # Valid options are :order, :offset, :limit
299 296 def journals(options={})
300 297 Journal.visible.all(
301 298 :include => [:details, :user, {:issue => [:project, :author, :tracker, :status]}],
302 299 :conditions => statement,
303 300 :order => options[:order],
304 301 :limit => options[:limit],
305 302 :offset => options[:offset]
306 303 )
307 304 rescue ::ActiveRecord::StatementInvalid => e
308 305 raise StatementInvalid.new(e.message)
309 306 end
310 307
311 308 # Returns the versions
312 309 # Valid options are :conditions
313 310 def versions(options={})
314 311 Version.visible.where(options[:conditions]).all(
315 312 :include => :project,
316 313 :conditions => project_statement
317 314 )
318 315 rescue ::ActiveRecord::StatementInvalid => e
319 316 raise StatementInvalid.new(e.message)
320 317 end
321 318
322 319 def sql_for_watcher_id_field(field, operator, value)
323 320 db_table = Watcher.table_name
324 321 "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " +
325 322 sql_for_field(field, '=', value, db_table, 'user_id') + ')'
326 323 end
327 324
328 325 def sql_for_member_of_group_field(field, operator, value)
329 326 if operator == '*' # Any group
330 327 groups = Group.all
331 328 operator = '=' # Override the operator since we want to find by assigned_to
332 329 elsif operator == "!*"
333 330 groups = Group.all
334 331 operator = '!' # Override the operator since we want to find by assigned_to
335 332 else
336 333 groups = Group.find_all_by_id(value)
337 334 end
338 335 groups ||= []
339 336
340 337 members_of_groups = groups.inject([]) {|user_ids, group|
341 338 user_ids + group.user_ids + [group.id]
342 339 }.uniq.compact.sort.collect(&:to_s)
343 340
344 341 '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')'
345 342 end
346 343
347 344 def sql_for_assigned_to_role_field(field, operator, value)
348 345 case operator
349 346 when "*", "!*" # Member / Not member
350 347 sw = operator == "!*" ? 'NOT' : ''
351 348 nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : ''
352 349 "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" +
353 350 " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))"
354 351 when "=", "!"
355 352 role_cond = value.any? ?
356 353 "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" :
357 354 "1=0"
358 355
359 356 sw = operator == "!" ? 'NOT' : ''
360 357 nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : ''
361 358 "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" +
362 359 " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))"
363 360 end
364 361 end
365 362
366 363 def sql_for_is_private_field(field, operator, value)
367 364 op = (operator == "=" ? 'IN' : 'NOT IN')
368 365 va = value.map {|v| v == '0' ? connection.quoted_false : connection.quoted_true}.uniq.join(',')
369 366
370 367 "#{Issue.table_name}.is_private #{op} (#{va})"
371 368 end
372 369
373 370 def sql_for_relations(field, operator, value, options={})
374 371 relation_options = IssueRelation::TYPES[field]
375 372 return relation_options unless relation_options
376 373
377 374 relation_type = field
378 375 join_column, target_join_column = "issue_from_id", "issue_to_id"
379 376 if relation_options[:reverse] || options[:reverse]
380 377 relation_type = relation_options[:reverse] || relation_type
381 378 join_column, target_join_column = target_join_column, join_column
382 379 end
383 380
384 381 sql = case operator
385 382 when "*", "!*"
386 383 op = (operator == "*" ? 'IN' : 'NOT IN')
387 384 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}')"
388 385 when "=", "!"
389 386 op = (operator == "=" ? 'IN' : 'NOT IN')
390 387 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = #{value.first.to_i})"
391 388 when "=p", "=!p", "!p"
392 389 op = (operator == "!p" ? 'NOT IN' : 'IN')
393 390 comp = (operator == "=!p" ? '<>' : '=')
394 391 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name}, #{Issue.table_name} relissues WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = relissues.id AND relissues.project_id #{comp} #{value.first.to_i})"
395 392 end
396 393
397 394 if relation_options[:sym] == field && !options[:reverse]
398 395 sqls = [sql, sql_for_relations(field, operator, value, :reverse => true)]
399 396 sqls.join(["!", "!*", "!p"].include?(operator) ? " AND " : " OR ")
400 397 else
401 398 sql
402 399 end
403 400 end
404 401
405 402 IssueRelation::TYPES.keys.each do |relation_type|
406 403 alias_method "sql_for_#{relation_type}_field".to_sym, :sql_for_relations
407 404 end
408 405 end
@@ -1,822 +1,828
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class QueryColumn
19 19 attr_accessor :name, :sortable, :groupable, :default_order
20 20 include Redmine::I18n
21 21
22 22 def initialize(name, options={})
23 23 self.name = name
24 24 self.sortable = options[:sortable]
25 25 self.groupable = options[:groupable] || false
26 26 if groupable == true
27 27 self.groupable = name.to_s
28 28 end
29 29 self.default_order = options[:default_order]
30 30 @inline = options.key?(:inline) ? options[:inline] : true
31 @caption_key = options[:caption] || "field_#{name}"
31 @caption_key = options[:caption] || "field_#{name}".to_sym
32 @frozen = options[:frozen]
32 33 end
33 34
34 35 def caption
35 l(@caption_key)
36 @caption_key.is_a?(Symbol) ? l(@caption_key) : @caption_key
36 37 end
37 38
38 39 # Returns true if the column is sortable, otherwise false
39 40 def sortable?
40 41 !@sortable.nil?
41 42 end
42 43
43 44 def sortable
44 45 @sortable.is_a?(Proc) ? @sortable.call : @sortable
45 46 end
46 47
47 48 def inline?
48 49 @inline
49 50 end
50 51
52 def frozen?
53 @frozen
54 end
55
51 56 def value(object)
52 57 object.send name
53 58 end
54 59
55 60 def css_classes
56 61 name
57 62 end
58 63 end
59 64
60 65 class QueryCustomFieldColumn < QueryColumn
61 66
62 67 def initialize(custom_field)
63 68 self.name = "cf_#{custom_field.id}".to_sym
64 69 self.sortable = custom_field.order_statement || false
65 70 self.groupable = custom_field.group_statement || false
66 71 @inline = true
67 72 @cf = custom_field
68 73 end
69 74
70 75 def caption
71 76 @cf.name
72 77 end
73 78
74 79 def custom_field
75 80 @cf
76 81 end
77 82
78 83 def value(object)
79 84 cv = object.custom_values.select {|v| v.custom_field_id == @cf.id}.collect {|v| @cf.cast_value(v.value)}
80 85 cv.size > 1 ? cv.sort {|a,b| a.to_s <=> b.to_s} : cv.first
81 86 end
82 87
83 88 def css_classes
84 89 @css_classes ||= "#{name} #{@cf.field_format}"
85 90 end
86 91 end
87 92
88 93 class QueryAssociationCustomFieldColumn < QueryCustomFieldColumn
89 94
90 95 def initialize(association, custom_field)
91 96 super(custom_field)
92 97 self.name = "#{association}.cf_#{custom_field.id}".to_sym
93 98 # TODO: support sorting/grouping by association custom field
94 99 self.sortable = false
95 100 self.groupable = false
96 101 @association = association
97 102 end
98 103
99 104 def value(object)
100 105 if assoc = object.send(@association)
101 106 super(assoc)
102 107 end
103 108 end
104 109
105 110 def css_classes
106 111 @css_classes ||= "#{@association}_cf_#{@cf.id} #{@cf.field_format}"
107 112 end
108 113 end
109 114
110 115 class Query < ActiveRecord::Base
111 116 class StatementInvalid < ::ActiveRecord::StatementInvalid
112 117 end
113 118
114 119 belongs_to :project
115 120 belongs_to :user
116 121 serialize :filters
117 122 serialize :column_names
118 123 serialize :sort_criteria, Array
119 124
120 125 attr_protected :project_id, :user_id
121 126
122 127 validates_presence_of :name
123 128 validates_length_of :name, :maximum => 255
124 129 validate :validate_query_filters
125 130
126 131 class_attribute :operators
127 132 self.operators = {
128 133 "=" => :label_equals,
129 134 "!" => :label_not_equals,
130 135 "o" => :label_open_issues,
131 136 "c" => :label_closed_issues,
132 137 "!*" => :label_none,
133 138 "*" => :label_any,
134 139 ">=" => :label_greater_or_equal,
135 140 "<=" => :label_less_or_equal,
136 141 "><" => :label_between,
137 142 "<t+" => :label_in_less_than,
138 143 ">t+" => :label_in_more_than,
139 144 "><t+"=> :label_in_the_next_days,
140 145 "t+" => :label_in,
141 146 "t" => :label_today,
142 147 "ld" => :label_yesterday,
143 148 "w" => :label_this_week,
144 149 "lw" => :label_last_week,
145 150 "l2w" => [:label_last_n_weeks, {:count => 2}],
146 151 "m" => :label_this_month,
147 152 "lm" => :label_last_month,
148 153 "y" => :label_this_year,
149 154 ">t-" => :label_less_than_ago,
150 155 "<t-" => :label_more_than_ago,
151 156 "><t-"=> :label_in_the_past_days,
152 157 "t-" => :label_ago,
153 158 "~" => :label_contains,
154 159 "!~" => :label_not_contains,
155 160 "=p" => :label_any_issues_in_project,
156 161 "=!p" => :label_any_issues_not_in_project,
157 162 "!p" => :label_no_issues_in_project
158 163 }
159 164
160 165 class_attribute :operators_by_filter_type
161 166 self.operators_by_filter_type = {
162 167 :list => [ "=", "!" ],
163 168 :list_status => [ "o", "=", "!", "c", "*" ],
164 169 :list_optional => [ "=", "!", "!*", "*" ],
165 170 :list_subprojects => [ "*", "!*", "=" ],
166 171 :date => [ "=", ">=", "<=", "><", "<t+", ">t+", "><t+", "t+", "t", "ld", "w", "lw", "l2w", "m", "lm", "y", ">t-", "<t-", "><t-", "t-", "!*", "*" ],
167 172 :date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "><t-", "t-", "t", "ld", "w", "lw", "l2w", "m", "lm", "y", "!*", "*" ],
168 173 :string => [ "=", "~", "!", "!~", "!*", "*" ],
169 174 :text => [ "~", "!~", "!*", "*" ],
170 175 :integer => [ "=", ">=", "<=", "><", "!*", "*" ],
171 176 :float => [ "=", ">=", "<=", "><", "!*", "*" ],
172 177 :relation => ["=", "=p", "=!p", "!p", "!*", "*"]
173 178 }
174 179
175 180 class_attribute :available_columns
176 181 self.available_columns = []
177 182
178 183 class_attribute :queried_class
179 184
180 185 def queried_table_name
181 186 @queried_table_name ||= self.class.queried_class.table_name
182 187 end
183 188
184 189 def initialize(attributes=nil, *args)
185 190 super attributes
186 191 @is_for_all = project.nil?
187 192 end
188 193
189 194 # Builds the query from the given params
190 195 def build_from_params(params)
191 196 if params[:fields] || params[:f]
192 197 self.filters = {}
193 198 add_filters(params[:fields] || params[:f], params[:operators] || params[:op], params[:values] || params[:v])
194 199 else
195 200 available_filters.keys.each do |field|
196 201 add_short_filter(field, params[field]) if params[field]
197 202 end
198 203 end
199 204 self.group_by = params[:group_by] || (params[:query] && params[:query][:group_by])
200 205 self.column_names = params[:c] || (params[:query] && params[:query][:column_names])
201 206 self
202 207 end
203 208
204 209 # Builds a new query from the given params and attributes
205 210 def self.build_from_params(params, attributes={})
206 211 new(attributes).build_from_params(params)
207 212 end
208 213
209 214 def validate_query_filters
210 215 filters.each_key do |field|
211 216 if values_for(field)
212 217 case type_for(field)
213 218 when :integer
214 219 add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+$/) }
215 220 when :float
216 221 add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+(\.\d*)?$/) }
217 222 when :date, :date_past
218 223 case operator_for(field)
219 224 when "=", ">=", "<=", "><"
220 225 add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && (!v.match(/^\d{4}-\d{2}-\d{2}$/) || (Date.parse(v) rescue nil).nil?) }
221 226 when ">t-", "<t-", "t-", ">t+", "<t+", "t+", "><t+", "><t-"
222 227 add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) }
223 228 end
224 229 end
225 230 end
226 231
227 232 add_filter_error(field, :blank) unless
228 233 # filter requires one or more values
229 234 (values_for(field) and !values_for(field).first.blank?) or
230 235 # filter doesn't require any value
231 236 ["o", "c", "!*", "*", "t", "ld", "w", "lw", "l2w", "m", "lm", "y"].include? operator_for(field)
232 237 end if filters
233 238 end
234 239
235 240 def add_filter_error(field, message)
236 241 m = label_for(field) + " " + l(message, :scope => 'activerecord.errors.messages')
237 242 errors.add(:base, m)
238 243 end
239 244
240 245 def editable_by?(user)
241 246 return false unless user
242 247 # Admin can edit them all and regular users can edit their private queries
243 248 return true if user.admin? || (!is_public && self.user_id == user.id)
244 249 # Members can not edit public queries that are for all project (only admin is allowed to)
245 250 is_public && !@is_for_all && user.allowed_to?(:manage_public_queries, project)
246 251 end
247 252
248 253 def trackers
249 254 @trackers ||= project.nil? ? Tracker.sorted.all : project.rolled_up_trackers
250 255 end
251 256
252 257 # Returns a hash of localized labels for all filter operators
253 258 def self.operators_labels
254 259 operators.inject({}) {|h, operator| h[operator.first] = l(*operator.last); h}
255 260 end
256 261
257 262 # Returns a representation of the available filters for JSON serialization
258 263 def available_filters_as_json
259 264 json = {}
260 265 available_filters.each do |field, options|
261 266 json[field] = options.slice(:type, :name, :values).stringify_keys
262 267 end
263 268 json
264 269 end
265 270
266 271 def all_projects
267 272 @all_projects ||= Project.visible.all
268 273 end
269 274
270 275 def all_projects_values
271 276 return @all_projects_values if @all_projects_values
272 277
273 278 values = []
274 279 Project.project_tree(all_projects) do |p, level|
275 280 prefix = (level > 0 ? ('--' * level + ' ') : '')
276 281 values << ["#{prefix}#{p.name}", p.id.to_s]
277 282 end
278 283 @all_projects_values = values
279 284 end
280 285
281 286 # Adds available filters
282 287 def initialize_available_filters
283 288 # implemented by sub-classes
284 289 end
285 290 protected :initialize_available_filters
286 291
287 292 # Adds an available filter
288 293 def add_available_filter(field, options)
289 294 @available_filters ||= ActiveSupport::OrderedHash.new
290 295 @available_filters[field] = options
291 296 @available_filters
292 297 end
293 298
294 299 # Removes an available filter
295 300 def delete_available_filter(field)
296 301 if @available_filters
297 302 @available_filters.delete(field)
298 303 end
299 304 end
300 305
301 306 # Return a hash of available filters
302 307 def available_filters
303 308 unless @available_filters
304 309 initialize_available_filters
305 310 @available_filters.each do |field, options|
306 311 options[:name] ||= l(options[:label] || "field_#{field}".gsub(/_id$/, ''))
307 312 end
308 313 end
309 314 @available_filters
310 315 end
311 316
312 317 def add_filter(field, operator, values=nil)
313 318 # values must be an array
314 319 return unless values.nil? || values.is_a?(Array)
315 320 # check if field is defined as an available filter
316 321 if available_filters.has_key? field
317 322 filter_options = available_filters[field]
318 323 filters[field] = {:operator => operator, :values => (values || [''])}
319 324 end
320 325 end
321 326
322 327 def add_short_filter(field, expression)
323 328 return unless expression && available_filters.has_key?(field)
324 329 field_type = available_filters[field][:type]
325 330 operators_by_filter_type[field_type].sort.reverse.detect do |operator|
326 331 next unless expression =~ /^#{Regexp.escape(operator)}(.*)$/
327 332 values = $1
328 333 add_filter field, operator, values.present? ? values.split('|') : ['']
329 334 end || add_filter(field, '=', expression.split('|'))
330 335 end
331 336
332 337 # Add multiple filters using +add_filter+
333 338 def add_filters(fields, operators, values)
334 339 if fields.is_a?(Array) && operators.is_a?(Hash) && (values.nil? || values.is_a?(Hash))
335 340 fields.each do |field|
336 341 add_filter(field, operators[field], values && values[field])
337 342 end
338 343 end
339 344 end
340 345
341 346 def has_filter?(field)
342 347 filters and filters[field]
343 348 end
344 349
345 350 def type_for(field)
346 351 available_filters[field][:type] if available_filters.has_key?(field)
347 352 end
348 353
349 354 def operator_for(field)
350 355 has_filter?(field) ? filters[field][:operator] : nil
351 356 end
352 357
353 358 def values_for(field)
354 359 has_filter?(field) ? filters[field][:values] : nil
355 360 end
356 361
357 362 def value_for(field, index=0)
358 363 (values_for(field) || [])[index]
359 364 end
360 365
361 366 def label_for(field)
362 367 label = available_filters[field][:name] if available_filters.has_key?(field)
363 368 label ||= l("field_#{field.to_s.gsub(/_id$/, '')}", :default => field)
364 369 end
365 370
366 371 def self.add_available_column(column)
367 372 self.available_columns << (column) if column.is_a?(QueryColumn)
368 373 end
369 374
370 375 # Returns an array of columns that can be used to group the results
371 376 def groupable_columns
372 377 available_columns.select {|c| c.groupable}
373 378 end
374 379
375 380 # Returns a Hash of columns and the key for sorting
376 381 def sortable_columns
377 382 available_columns.inject({}) {|h, column|
378 383 h[column.name.to_s] = column.sortable
379 384 h
380 385 }
381 386 end
382 387
383 388 def columns
384 389 # preserve the column_names order
385 (has_default_columns? ? default_columns_names : column_names).collect do |name|
390 cols = (has_default_columns? ? default_columns_names : column_names).collect do |name|
386 391 available_columns.find { |col| col.name == name }
387 392 end.compact
393 available_columns.select(&:frozen?) | cols
388 394 end
389 395
390 396 def inline_columns
391 397 columns.select(&:inline?)
392 398 end
393 399
394 400 def block_columns
395 401 columns.reject(&:inline?)
396 402 end
397 403
398 404 def available_inline_columns
399 405 available_columns.select(&:inline?)
400 406 end
401 407
402 408 def available_block_columns
403 409 available_columns.reject(&:inline?)
404 410 end
405 411
406 412 def default_columns_names
407 413 []
408 414 end
409 415
410 416 def column_names=(names)
411 417 if names
412 418 names = names.select {|n| n.is_a?(Symbol) || !n.blank? }
413 419 names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym }
414 420 # Set column_names to nil if default columns
415 421 if names == default_columns_names
416 422 names = nil
417 423 end
418 424 end
419 425 write_attribute(:column_names, names)
420 426 end
421 427
422 428 def has_column?(column)
423 429 column_names && column_names.include?(column.is_a?(QueryColumn) ? column.name : column)
424 430 end
425 431
426 432 def has_default_columns?
427 433 column_names.nil? || column_names.empty?
428 434 end
429 435
430 436 def sort_criteria=(arg)
431 437 c = []
432 438 if arg.is_a?(Hash)
433 439 arg = arg.keys.sort.collect {|k| arg[k]}
434 440 end
435 441 c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, (o == 'desc' || o == false) ? 'desc' : 'asc']}
436 442 write_attribute(:sort_criteria, c)
437 443 end
438 444
439 445 def sort_criteria
440 446 read_attribute(:sort_criteria) || []
441 447 end
442 448
443 449 def sort_criteria_key(arg)
444 450 sort_criteria && sort_criteria[arg] && sort_criteria[arg].first
445 451 end
446 452
447 453 def sort_criteria_order(arg)
448 454 sort_criteria && sort_criteria[arg] && sort_criteria[arg].last
449 455 end
450 456
451 457 def sort_criteria_order_for(key)
452 458 sort_criteria.detect {|k, order| key.to_s == k}.try(:last)
453 459 end
454 460
455 461 # Returns the SQL sort order that should be prepended for grouping
456 462 def group_by_sort_order
457 463 if grouped? && (column = group_by_column)
458 464 order = sort_criteria_order_for(column.name) || column.default_order
459 465 column.sortable.is_a?(Array) ?
460 466 column.sortable.collect {|s| "#{s} #{order}"}.join(',') :
461 467 "#{column.sortable} #{order}"
462 468 end
463 469 end
464 470
465 471 # Returns true if the query is a grouped query
466 472 def grouped?
467 473 !group_by_column.nil?
468 474 end
469 475
470 476 def group_by_column
471 477 groupable_columns.detect {|c| c.groupable && c.name.to_s == group_by}
472 478 end
473 479
474 480 def group_by_statement
475 481 group_by_column.try(:groupable)
476 482 end
477 483
478 484 def project_statement
479 485 project_clauses = []
480 486 if project && !project.descendants.active.empty?
481 487 ids = [project.id]
482 488 if has_filter?("subproject_id")
483 489 case operator_for("subproject_id")
484 490 when '='
485 491 # include the selected subprojects
486 492 ids += values_for("subproject_id").each(&:to_i)
487 493 when '!*'
488 494 # main project only
489 495 else
490 496 # all subprojects
491 497 ids += project.descendants.collect(&:id)
492 498 end
493 499 elsif Setting.display_subprojects_issues?
494 500 ids += project.descendants.collect(&:id)
495 501 end
496 502 project_clauses << "#{Project.table_name}.id IN (%s)" % ids.join(',')
497 503 elsif project
498 504 project_clauses << "#{Project.table_name}.id = %d" % project.id
499 505 end
500 506 project_clauses.any? ? project_clauses.join(' AND ') : nil
501 507 end
502 508
503 509 def statement
504 510 # filters clauses
505 511 filters_clauses = []
506 512 filters.each_key do |field|
507 513 next if field == "subproject_id"
508 514 v = values_for(field).clone
509 515 next unless v and !v.empty?
510 516 operator = operator_for(field)
511 517
512 518 # "me" value subsitution
513 519 if %w(assigned_to_id author_id user_id watcher_id).include?(field)
514 520 if v.delete("me")
515 521 if User.current.logged?
516 522 v.push(User.current.id.to_s)
517 523 v += User.current.group_ids.map(&:to_s) if field == 'assigned_to_id'
518 524 else
519 525 v.push("0")
520 526 end
521 527 end
522 528 end
523 529
524 530 if field == 'project_id'
525 531 if v.delete('mine')
526 532 v += User.current.memberships.map(&:project_id).map(&:to_s)
527 533 end
528 534 end
529 535
530 536 if field =~ /cf_(\d+)$/
531 537 # custom field
532 538 filters_clauses << sql_for_custom_field(field, operator, v, $1)
533 539 elsif respond_to?("sql_for_#{field}_field")
534 540 # specific statement
535 541 filters_clauses << send("sql_for_#{field}_field", field, operator, v)
536 542 else
537 543 # regular field
538 544 filters_clauses << '(' + sql_for_field(field, operator, v, queried_table_name, field) + ')'
539 545 end
540 546 end if filters and valid?
541 547
542 548 filters_clauses << project_statement
543 549 filters_clauses.reject!(&:blank?)
544 550
545 551 filters_clauses.any? ? filters_clauses.join(' AND ') : nil
546 552 end
547 553
548 554 private
549 555
550 556 def sql_for_custom_field(field, operator, value, custom_field_id)
551 557 db_table = CustomValue.table_name
552 558 db_field = 'value'
553 559 filter = @available_filters[field]
554 560 return nil unless filter
555 561 if filter[:format] == 'user'
556 562 if value.delete('me')
557 563 value.push User.current.id.to_s
558 564 end
559 565 end
560 566 not_in = nil
561 567 if operator == '!'
562 568 # Makes ! operator work for custom fields with multiple values
563 569 operator = '='
564 570 not_in = 'NOT'
565 571 end
566 572 customized_key = "id"
567 573 customized_class = queried_class
568 574 if field =~ /^(.+)\.cf_/
569 575 assoc = $1
570 576 customized_key = "#{assoc}_id"
571 577 customized_class = queried_class.reflect_on_association(assoc.to_sym).klass.base_class rescue nil
572 578 raise "Unknown #{queried_class.name} association #{assoc}" unless customized_class
573 579 end
574 580 "#{queried_table_name}.#{customized_key} #{not_in} IN (SELECT #{customized_class.table_name}.id FROM #{customized_class.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='#{customized_class}' AND #{db_table}.customized_id=#{customized_class.table_name}.id AND #{db_table}.custom_field_id=#{custom_field_id} WHERE " +
575 581 sql_for_field(field, operator, value, db_table, db_field, true) + ')'
576 582 end
577 583
578 584 # Helper method to generate the WHERE sql for a +field+, +operator+ and a +value+
579 585 def sql_for_field(field, operator, value, db_table, db_field, is_custom_filter=false)
580 586 sql = ''
581 587 case operator
582 588 when "="
583 589 if value.any?
584 590 case type_for(field)
585 591 when :date, :date_past
586 592 sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), (Date.parse(value.first) rescue nil))
587 593 when :integer
588 594 if is_custom_filter
589 595 sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) = #{value.first.to_i})"
590 596 else
591 597 sql = "#{db_table}.#{db_field} = #{value.first.to_i}"
592 598 end
593 599 when :float
594 600 if is_custom_filter
595 601 sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5})"
596 602 else
597 603 sql = "#{db_table}.#{db_field} BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5}"
598 604 end
599 605 else
600 606 sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")"
601 607 end
602 608 else
603 609 # IN an empty set
604 610 sql = "1=0"
605 611 end
606 612 when "!"
607 613 if value.any?
608 614 sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))"
609 615 else
610 616 # NOT IN an empty set
611 617 sql = "1=1"
612 618 end
613 619 when "!*"
614 620 sql = "#{db_table}.#{db_field} IS NULL"
615 621 sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter
616 622 when "*"
617 623 sql = "#{db_table}.#{db_field} IS NOT NULL"
618 624 sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter
619 625 when ">="
620 626 if [:date, :date_past].include?(type_for(field))
621 627 sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), nil)
622 628 else
623 629 if is_custom_filter
624 630 sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) >= #{value.first.to_f})"
625 631 else
626 632 sql = "#{db_table}.#{db_field} >= #{value.first.to_f}"
627 633 end
628 634 end
629 635 when "<="
630 636 if [:date, :date_past].include?(type_for(field))
631 637 sql = date_clause(db_table, db_field, nil, (Date.parse(value.first) rescue nil))
632 638 else
633 639 if is_custom_filter
634 640 sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) <= #{value.first.to_f})"
635 641 else
636 642 sql = "#{db_table}.#{db_field} <= #{value.first.to_f}"
637 643 end
638 644 end
639 645 when "><"
640 646 if [:date, :date_past].include?(type_for(field))
641 647 sql = date_clause(db_table, db_field, (Date.parse(value[0]) rescue nil), (Date.parse(value[1]) rescue nil))
642 648 else
643 649 if is_custom_filter
644 650 sql = "(#{db_table}.#{db_field} <> '' AND CAST(CASE #{db_table}.#{db_field} WHEN '' THEN '0' ELSE #{db_table}.#{db_field} END AS decimal(30,3)) BETWEEN #{value[0].to_f} AND #{value[1].to_f})"
645 651 else
646 652 sql = "#{db_table}.#{db_field} BETWEEN #{value[0].to_f} AND #{value[1].to_f}"
647 653 end
648 654 end
649 655 when "o"
650 656 sql = "#{queried_table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_false})" if field == "status_id"
651 657 when "c"
652 658 sql = "#{queried_table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_true})" if field == "status_id"
653 659 when "><t-"
654 660 # between today - n days and today
655 661 sql = relative_date_clause(db_table, db_field, - value.first.to_i, 0)
656 662 when ">t-"
657 663 # >= today - n days
658 664 sql = relative_date_clause(db_table, db_field, - value.first.to_i, nil)
659 665 when "<t-"
660 666 # <= today - n days
661 667 sql = relative_date_clause(db_table, db_field, nil, - value.first.to_i)
662 668 when "t-"
663 669 # = n days in past
664 670 sql = relative_date_clause(db_table, db_field, - value.first.to_i, - value.first.to_i)
665 671 when "><t+"
666 672 # between today and today + n days
667 673 sql = relative_date_clause(db_table, db_field, 0, value.first.to_i)
668 674 when ">t+"
669 675 # >= today + n days
670 676 sql = relative_date_clause(db_table, db_field, value.first.to_i, nil)
671 677 when "<t+"
672 678 # <= today + n days
673 679 sql = relative_date_clause(db_table, db_field, nil, value.first.to_i)
674 680 when "t+"
675 681 # = today + n days
676 682 sql = relative_date_clause(db_table, db_field, value.first.to_i, value.first.to_i)
677 683 when "t"
678 684 # = today
679 685 sql = relative_date_clause(db_table, db_field, 0, 0)
680 686 when "ld"
681 687 # = yesterday
682 688 sql = relative_date_clause(db_table, db_field, -1, -1)
683 689 when "w"
684 690 # = this week
685 691 first_day_of_week = l(:general_first_day_of_week).to_i
686 692 day_of_week = Date.today.cwday
687 693 days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week)
688 694 sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6)
689 695 when "lw"
690 696 # = last week
691 697 first_day_of_week = l(:general_first_day_of_week).to_i
692 698 day_of_week = Date.today.cwday
693 699 days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week)
694 700 sql = relative_date_clause(db_table, db_field, - days_ago - 7, - days_ago - 1)
695 701 when "l2w"
696 702 # = last 2 weeks
697 703 first_day_of_week = l(:general_first_day_of_week).to_i
698 704 day_of_week = Date.today.cwday
699 705 days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week)
700 706 sql = relative_date_clause(db_table, db_field, - days_ago - 14, - days_ago - 1)
701 707 when "m"
702 708 # = this month
703 709 date = Date.today
704 710 sql = date_clause(db_table, db_field, date.beginning_of_month, date.end_of_month)
705 711 when "lm"
706 712 # = last month
707 713 date = Date.today.prev_month
708 714 sql = date_clause(db_table, db_field, date.beginning_of_month, date.end_of_month)
709 715 when "y"
710 716 # = this year
711 717 date = Date.today
712 718 sql = date_clause(db_table, db_field, date.beginning_of_year, date.end_of_year)
713 719 when "~"
714 720 sql = "LOWER(#{db_table}.#{db_field}) LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'"
715 721 when "!~"
716 722 sql = "LOWER(#{db_table}.#{db_field}) NOT LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'"
717 723 else
718 724 raise "Unknown query operator #{operator}"
719 725 end
720 726
721 727 return sql
722 728 end
723 729
724 730 def add_custom_fields_filters(custom_fields, assoc=nil)
725 731 return unless custom_fields.present?
726 732
727 733 custom_fields.select(&:is_filter?).sort.each do |field|
728 734 case field.field_format
729 735 when "text"
730 736 options = { :type => :text }
731 737 when "list"
732 738 options = { :type => :list_optional, :values => field.possible_values }
733 739 when "date"
734 740 options = { :type => :date }
735 741 when "bool"
736 742 options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]] }
737 743 when "int"
738 744 options = { :type => :integer }
739 745 when "float"
740 746 options = { :type => :float }
741 747 when "user", "version"
742 748 next unless project
743 749 values = field.possible_values_options(project)
744 750 if User.current.logged? && field.field_format == 'user'
745 751 values.unshift ["<< #{l(:label_me)} >>", "me"]
746 752 end
747 753 options = { :type => :list_optional, :values => values }
748 754 else
749 755 options = { :type => :string }
750 756 end
751 757 filter_id = "cf_#{field.id}"
752 758 filter_name = field.name
753 759 if assoc.present?
754 760 filter_id = "#{assoc}.#{filter_id}"
755 761 filter_name = l("label_attribute_of_#{assoc}", :name => filter_name)
756 762 end
757 763 add_available_filter filter_id, options.merge({
758 764 :name => filter_name,
759 765 :format => field.field_format,
760 766 :field => field
761 767 })
762 768 end
763 769 end
764 770
765 771 def add_associations_custom_fields_filters(*associations)
766 772 fields_by_class = CustomField.where(:is_filter => true).group_by(&:class)
767 773 associations.each do |assoc|
768 774 association_klass = queried_class.reflect_on_association(assoc).klass
769 775 fields_by_class.each do |field_class, fields|
770 776 if field_class.customized_class <= association_klass
771 777 add_custom_fields_filters(fields, assoc)
772 778 end
773 779 end
774 780 end
775 781 end
776 782
777 783 # Returns a SQL clause for a date or datetime field.
778 784 def date_clause(table, field, from, to)
779 785 s = []
780 786 if from
781 787 from_yesterday = from - 1
782 788 from_yesterday_time = Time.local(from_yesterday.year, from_yesterday.month, from_yesterday.day)
783 789 if self.class.default_timezone == :utc
784 790 from_yesterday_time = from_yesterday_time.utc
785 791 end
786 792 s << ("#{table}.#{field} > '%s'" % [connection.quoted_date(from_yesterday_time.end_of_day)])
787 793 end
788 794 if to
789 795 to_time = Time.local(to.year, to.month, to.day)
790 796 if self.class.default_timezone == :utc
791 797 to_time = to_time.utc
792 798 end
793 799 s << ("#{table}.#{field} <= '%s'" % [connection.quoted_date(to_time.end_of_day)])
794 800 end
795 801 s.join(' AND ')
796 802 end
797 803
798 804 # Returns a SQL clause for a date or datetime field using relative dates.
799 805 def relative_date_clause(table, field, days_from, days_to)
800 806 date_clause(table, field, (days_from ? Date.today + days_from : nil), (days_to ? Date.today + days_to : nil))
801 807 end
802 808
803 809 # Additional joins required for the given sort options
804 810 def joins_for_order_statement(order_options)
805 811 joins = []
806 812
807 813 if order_options
808 814 if order_options.include?('authors')
809 815 joins << "LEFT OUTER JOIN #{User.table_name} authors ON authors.id = #{queried_table_name}.author_id"
810 816 end
811 817 order_options.scan(/cf_\d+/).uniq.each do |name|
812 818 column = available_columns.detect {|c| c.name.to_s == name}
813 819 join = column && column.custom_field.join_for_order_statement
814 820 if join
815 821 joins << join
816 822 end
817 823 end
818 824 end
819 825
820 826 joins.any? ? joins.join(' ') : nil
821 827 end
822 828 end
@@ -1,49 +1,47
1 1 <%= form_tag({}) do -%>
2 2 <%= hidden_field_tag 'back_url', url_for(params), :id => nil %>
3 3 <div class="autoscroll">
4 4 <table class="list issues">
5 5 <thead>
6 6 <tr>
7 7 <th class="checkbox hide-when-print">
8 8 <%= link_to image_tag('toggle_check.png'), {},
9 9 :onclick => 'toggleIssuesSelection(this); return false;',
10 10 :title => "#{l(:button_check_all)}/#{l(:button_uncheck_all)}" %>
11 11 </th>
12 <%= sort_header_tag('id', :caption => '#', :default_order => 'desc') %>
13 12 <% query.inline_columns.each do |column| %>
14 13 <%= column_header(column) %>
15 14 <% end %>
16 15 </tr>
17 16 </thead>
18 17 <% previous_group = false %>
19 18 <tbody>
20 19 <% issue_list(issues) do |issue, level| -%>
21 20 <% if @query.grouped? && (group = @query.group_by_column.value(issue)) != previous_group %>
22 21 <% reset_cycle %>
23 22 <tr class="group open">
24 23 <td colspan="<%= query.inline_columns.size + 2 %>">
25 24 <span class="expander" onclick="toggleRowGroup(this);">&nbsp;</span>
26 25 <%= group.blank? ? l(:label_none) : column_content(@query.group_by_column, issue) %> <span class="count"><%= @issue_count_by_group[group] %></span>
27 26 <%= link_to_function("#{l(:button_collapse_all)}/#{l(:button_expand_all)}",
28 27 "toggleAllRowGroups(this)", :class => 'toggle-all') %>
29 28 </td>
30 29 </tr>
31 30 <% previous_group = group %>
32 31 <% end %>
33 32 <tr id="issue-<%= issue.id %>" class="hascontextmenu <%= cycle('odd', 'even') %> <%= issue.css_classes %> <%= level > 0 ? "idnt idnt-#{level}" : nil %>">
34 33 <td class="checkbox hide-when-print"><%= check_box_tag("ids[]", issue.id, false, :id => nil) %></td>
35 <td class="id"><%= link_to issue.id, issue_path(issue) %></td>
36 34 <%= raw query.inline_columns.map {|column| "<td class=\"#{column.css_classes}\">#{column_content(column, issue)}</td>"}.join %>
37 35 </tr>
38 36 <% @query.block_columns.each do |column|
39 37 if (text = column_content(column, issue)) && text.present? -%>
40 38 <tr class="<%= current_cycle %>">
41 <td colspan="<%= @query.inline_columns.size + 2 %>" class="<%= column.css_classes %>"><%= text %></td>
39 <td colspan="<%= @query.inline_columns.size + 1 %>" class="<%= column.css_classes %>"><%= text %></td>
42 40 </tr>
43 41 <% end -%>
44 42 <% end -%>
45 43 <% end -%>
46 44 </tbody>
47 45 </table>
48 46 </div>
49 47 <% end -%>
@@ -1,34 +1,34
1 1 <table class="query-columns">
2 2 <tr>
3 3 <td style="padding-left:0">
4 4 <%= label_tag "available_columns", l(:description_available_columns) %>
5 5 <br />
6 6 <%= select_tag 'available_columns',
7 options_for_select((query.available_inline_columns - query.columns).collect {|column| [column.caption, column.name]}),
7 options_for_select((query.available_inline_columns - query.columns).reject(&:frozen?).collect {|column| [column.caption, column.name]}),
8 8 :multiple => true, :size => 10, :style => "width:150px",
9 9 :ondblclick => "moveOptions(this.form.available_columns, this.form.selected_columns);" %>
10 10 </td>
11 11 <td class="buttons">
12 12 <input type="button" value="&#8594;"
13 13 onclick="moveOptions(this.form.available_columns, this.form.selected_columns);" /><br />
14 14 <input type="button" value="&#8592;"
15 15 onclick="moveOptions(this.form.selected_columns, this.form.available_columns);" />
16 16 </td>
17 17 <td>
18 18 <%= label_tag "selected_columns", l(:description_selected_columns) %>
19 19 <br />
20 20 <%= select_tag((defined?(tag_name) ? tag_name : 'c[]'),
21 options_for_select(query.inline_columns.collect {|column| [column.caption, column.name]}),
21 options_for_select((query.inline_columns & query.available_inline_columns).reject(&:frozen?).collect {|column| [column.caption, column.name]}),
22 22 :id => 'selected_columns', :multiple => true, :size => 10, :style => "width:150px",
23 23 :ondblclick => "moveOptions(this.form.selected_columns, this.form.available_columns);") %>
24 24 </td>
25 25 <td class="buttons">
26 26 <input type="button" value="&#8593;" onclick="moveOptionUp(this.form.selected_columns);" /><br />
27 27 <input type="button" value="&#8595;" onclick="moveOptionDown(this.form.selected_columns);" />
28 28 </td>
29 29 </tr>
30 30 </table>
31 31
32 32 <% content_for :header_tags do %>
33 33 <%= javascript_include_tag 'select_list_move' %>
34 34 <% end %>
@@ -1,3891 +1,3891
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class IssuesControllerTest < ActionController::TestCase
21 21 fixtures :projects,
22 22 :users,
23 23 :roles,
24 24 :members,
25 25 :member_roles,
26 26 :issues,
27 27 :issue_statuses,
28 28 :versions,
29 29 :trackers,
30 30 :projects_trackers,
31 31 :issue_categories,
32 32 :enabled_modules,
33 33 :enumerations,
34 34 :attachments,
35 35 :workflows,
36 36 :custom_fields,
37 37 :custom_values,
38 38 :custom_fields_projects,
39 39 :custom_fields_trackers,
40 40 :time_entries,
41 41 :journals,
42 42 :journal_details,
43 43 :queries,
44 44 :repositories,
45 45 :changesets
46 46
47 47 include Redmine::I18n
48 48
49 49 def setup
50 50 User.current = nil
51 51 end
52 52
53 53 def test_index
54 54 with_settings :default_language => "en" do
55 55 get :index
56 56 assert_response :success
57 57 assert_template 'index'
58 58 assert_not_nil assigns(:issues)
59 59 assert_nil assigns(:project)
60 60
61 61 # links to visible issues
62 62 assert_select 'a[href=/issues/1]', :text => /Can&#x27;t print recipes/
63 63 assert_select 'a[href=/issues/5]', :text => /Subproject issue/
64 64 # private projects hidden
65 65 assert_select 'a[href=/issues/6]', 0
66 66 assert_select 'a[href=/issues/4]', 0
67 67 # project column
68 68 assert_select 'th', :text => /Project/
69 69 end
70 70 end
71 71
72 72 def test_index_should_not_list_issues_when_module_disabled
73 73 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
74 74 get :index
75 75 assert_response :success
76 76 assert_template 'index'
77 77 assert_not_nil assigns(:issues)
78 78 assert_nil assigns(:project)
79 79
80 80 assert_select 'a[href=/issues/1]', 0
81 81 assert_select 'a[href=/issues/5]', :text => /Subproject issue/
82 82 end
83 83
84 84 def test_index_should_list_visible_issues_only
85 85 get :index, :per_page => 100
86 86 assert_response :success
87 87 assert_not_nil assigns(:issues)
88 88 assert_nil assigns(:issues).detect {|issue| !issue.visible?}
89 89 end
90 90
91 91 def test_index_with_project
92 92 Setting.display_subprojects_issues = 0
93 93 get :index, :project_id => 1
94 94 assert_response :success
95 95 assert_template 'index'
96 96 assert_not_nil assigns(:issues)
97 97
98 98 assert_select 'a[href=/issues/1]', :text => /Can&#x27;t print recipes/
99 99 assert_select 'a[href=/issues/5]', 0
100 100 end
101 101
102 102 def test_index_with_project_and_subprojects
103 103 Setting.display_subprojects_issues = 1
104 104 get :index, :project_id => 1
105 105 assert_response :success
106 106 assert_template 'index'
107 107 assert_not_nil assigns(:issues)
108 108
109 109 assert_select 'a[href=/issues/1]', :text => /Can&#x27;t print recipes/
110 110 assert_select 'a[href=/issues/5]', :text => /Subproject issue/
111 111 assert_select 'a[href=/issues/6]', 0
112 112 end
113 113
114 114 def test_index_with_project_and_subprojects_should_show_private_subprojects_with_permission
115 115 @request.session[:user_id] = 2
116 116 Setting.display_subprojects_issues = 1
117 117 get :index, :project_id => 1
118 118 assert_response :success
119 119 assert_template 'index'
120 120 assert_not_nil assigns(:issues)
121 121
122 122 assert_select 'a[href=/issues/1]', :text => /Can&#x27;t print recipes/
123 123 assert_select 'a[href=/issues/5]', :text => /Subproject issue/
124 124 assert_select 'a[href=/issues/6]', :text => /Issue of a private subproject/
125 125 end
126 126
127 127 def test_index_with_project_and_default_filter
128 128 get :index, :project_id => 1, :set_filter => 1
129 129 assert_response :success
130 130 assert_template 'index'
131 131 assert_not_nil assigns(:issues)
132 132
133 133 query = assigns(:query)
134 134 assert_not_nil query
135 135 # default filter
136 136 assert_equal({'status_id' => {:operator => 'o', :values => ['']}}, query.filters)
137 137 end
138 138
139 139 def test_index_with_project_and_filter
140 140 get :index, :project_id => 1, :set_filter => 1,
141 141 :f => ['tracker_id'],
142 142 :op => {'tracker_id' => '='},
143 143 :v => {'tracker_id' => ['1']}
144 144 assert_response :success
145 145 assert_template 'index'
146 146 assert_not_nil assigns(:issues)
147 147
148 148 query = assigns(:query)
149 149 assert_not_nil query
150 150 assert_equal({'tracker_id' => {:operator => '=', :values => ['1']}}, query.filters)
151 151 end
152 152
153 153 def test_index_with_short_filters
154 154 to_test = {
155 155 'status_id' => {
156 156 'o' => { :op => 'o', :values => [''] },
157 157 'c' => { :op => 'c', :values => [''] },
158 158 '7' => { :op => '=', :values => ['7'] },
159 159 '7|3|4' => { :op => '=', :values => ['7', '3', '4'] },
160 160 '=7' => { :op => '=', :values => ['7'] },
161 161 '!3' => { :op => '!', :values => ['3'] },
162 162 '!7|3|4' => { :op => '!', :values => ['7', '3', '4'] }},
163 163 'subject' => {
164 164 'This is a subject' => { :op => '=', :values => ['This is a subject'] },
165 165 'o' => { :op => '=', :values => ['o'] },
166 166 '~This is part of a subject' => { :op => '~', :values => ['This is part of a subject'] },
167 167 '!~This is part of a subject' => { :op => '!~', :values => ['This is part of a subject'] }},
168 168 'tracker_id' => {
169 169 '3' => { :op => '=', :values => ['3'] },
170 170 '=3' => { :op => '=', :values => ['3'] }},
171 171 'start_date' => {
172 172 '2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
173 173 '=2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
174 174 '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
175 175 '<=2011-10-12' => { :op => '<=', :values => ['2011-10-12'] },
176 176 '><2011-10-01|2011-10-30' => { :op => '><', :values => ['2011-10-01', '2011-10-30'] },
177 177 '<t+2' => { :op => '<t+', :values => ['2'] },
178 178 '>t+2' => { :op => '>t+', :values => ['2'] },
179 179 't+2' => { :op => 't+', :values => ['2'] },
180 180 't' => { :op => 't', :values => [''] },
181 181 'w' => { :op => 'w', :values => [''] },
182 182 '>t-2' => { :op => '>t-', :values => ['2'] },
183 183 '<t-2' => { :op => '<t-', :values => ['2'] },
184 184 't-2' => { :op => 't-', :values => ['2'] }},
185 185 'created_on' => {
186 186 '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
187 187 '<t-2' => { :op => '<t-', :values => ['2'] },
188 188 '>t-2' => { :op => '>t-', :values => ['2'] },
189 189 't-2' => { :op => 't-', :values => ['2'] }},
190 190 'cf_1' => {
191 191 'c' => { :op => '=', :values => ['c'] },
192 192 '!c' => { :op => '!', :values => ['c'] },
193 193 '!*' => { :op => '!*', :values => [''] },
194 194 '*' => { :op => '*', :values => [''] }},
195 195 'estimated_hours' => {
196 196 '=13.4' => { :op => '=', :values => ['13.4'] },
197 197 '>=45' => { :op => '>=', :values => ['45'] },
198 198 '<=125' => { :op => '<=', :values => ['125'] },
199 199 '><10.5|20.5' => { :op => '><', :values => ['10.5', '20.5'] },
200 200 '!*' => { :op => '!*', :values => [''] },
201 201 '*' => { :op => '*', :values => [''] }}
202 202 }
203 203
204 204 default_filter = { 'status_id' => {:operator => 'o', :values => [''] }}
205 205
206 206 to_test.each do |field, expression_and_expected|
207 207 expression_and_expected.each do |filter_expression, expected|
208 208
209 209 get :index, :set_filter => 1, field => filter_expression
210 210
211 211 assert_response :success
212 212 assert_template 'index'
213 213 assert_not_nil assigns(:issues)
214 214
215 215 query = assigns(:query)
216 216 assert_not_nil query
217 217 assert query.has_filter?(field)
218 218 assert_equal(default_filter.merge({field => {:operator => expected[:op], :values => expected[:values]}}), query.filters)
219 219 end
220 220 end
221 221 end
222 222
223 223 def test_index_with_project_and_empty_filters
224 224 get :index, :project_id => 1, :set_filter => 1, :fields => ['']
225 225 assert_response :success
226 226 assert_template 'index'
227 227 assert_not_nil assigns(:issues)
228 228
229 229 query = assigns(:query)
230 230 assert_not_nil query
231 231 # no filter
232 232 assert_equal({}, query.filters)
233 233 end
234 234
235 235 def test_index_with_project_custom_field_filter
236 236 field = ProjectCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
237 237 CustomValue.create!(:custom_field => field, :customized => Project.find(3), :value => 'Foo')
238 238 CustomValue.create!(:custom_field => field, :customized => Project.find(5), :value => 'Foo')
239 239 filter_name = "project.cf_#{field.id}"
240 240 @request.session[:user_id] = 1
241 241
242 242 get :index, :set_filter => 1,
243 243 :f => [filter_name],
244 244 :op => {filter_name => '='},
245 245 :v => {filter_name => ['Foo']}
246 246 assert_response :success
247 247 assert_template 'index'
248 248 assert_equal [3, 5], assigns(:issues).map(&:project_id).uniq.sort
249 249 end
250 250
251 251 def test_index_with_query
252 252 get :index, :project_id => 1, :query_id => 5
253 253 assert_response :success
254 254 assert_template 'index'
255 255 assert_not_nil assigns(:issues)
256 256 assert_nil assigns(:issue_count_by_group)
257 257 end
258 258
259 259 def test_index_with_query_grouped_by_tracker
260 260 get :index, :project_id => 1, :query_id => 6
261 261 assert_response :success
262 262 assert_template 'index'
263 263 assert_not_nil assigns(:issues)
264 264 assert_not_nil assigns(:issue_count_by_group)
265 265 end
266 266
267 267 def test_index_with_query_grouped_by_list_custom_field
268 268 get :index, :project_id => 1, :query_id => 9
269 269 assert_response :success
270 270 assert_template 'index'
271 271 assert_not_nil assigns(:issues)
272 272 assert_not_nil assigns(:issue_count_by_group)
273 273 end
274 274
275 275 def test_index_with_query_grouped_by_user_custom_field
276 276 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
277 277 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
278 278 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
279 279 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
280 280 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
281 281
282 282 get :index, :project_id => 1, :set_filter => 1, :group_by => "cf_#{cf.id}"
283 283 assert_response :success
284 284
285 285 assert_select 'tr.group', 3
286 286 assert_select 'tr.group' do
287 287 assert_select 'a', :text => 'John Smith'
288 288 assert_select 'span.count', :text => '1'
289 289 end
290 290 assert_select 'tr.group' do
291 291 assert_select 'a', :text => 'Dave Lopper'
292 292 assert_select 'span.count', :text => '2'
293 293 end
294 294 end
295 295
296 296 def test_index_with_query_grouped_by_tracker
297 297 3.times {|i| Issue.generate!(:tracker_id => (i + 1))}
298 298
299 299 get :index, :set_filter => 1, :group_by => 'tracker', :sort => 'id:desc'
300 300 assert_response :success
301 301
302 302 trackers = assigns(:issues).map(&:tracker).uniq
303 303 assert_equal [1, 2, 3], trackers.map(&:id)
304 304 end
305 305
306 306 def test_index_with_query_grouped_by_tracker_in_reverse_order
307 307 3.times {|i| Issue.generate!(:tracker_id => (i + 1))}
308 308
309 309 get :index, :set_filter => 1, :group_by => 'tracker', :sort => 'id:desc,tracker:desc'
310 310 assert_response :success
311 311
312 312 trackers = assigns(:issues).map(&:tracker).uniq
313 313 assert_equal [3, 2, 1], trackers.map(&:id)
314 314 end
315 315
316 316 def test_index_with_query_id_and_project_id_should_set_session_query
317 317 get :index, :project_id => 1, :query_id => 4
318 318 assert_response :success
319 319 assert_kind_of Hash, session[:query]
320 320 assert_equal 4, session[:query][:id]
321 321 assert_equal 1, session[:query][:project_id]
322 322 end
323 323
324 324 def test_index_with_invalid_query_id_should_respond_404
325 325 get :index, :project_id => 1, :query_id => 999
326 326 assert_response 404
327 327 end
328 328
329 329 def test_index_with_cross_project_query_in_session_should_show_project_issues
330 330 q = IssueQuery.create!(:name => "test", :user_id => 2, :is_public => false, :project => nil)
331 331 @request.session[:query] = {:id => q.id, :project_id => 1}
332 332
333 333 with_settings :display_subprojects_issues => '0' do
334 334 get :index, :project_id => 1
335 335 end
336 336 assert_response :success
337 337 assert_not_nil assigns(:query)
338 338 assert_equal q.id, assigns(:query).id
339 339 assert_equal 1, assigns(:query).project_id
340 340 assert_equal [1], assigns(:issues).map(&:project_id).uniq
341 341 end
342 342
343 343 def test_private_query_should_not_be_available_to_other_users
344 344 q = IssueQuery.create!(:name => "private", :user => User.find(2), :is_public => false, :project => nil)
345 345 @request.session[:user_id] = 3
346 346
347 347 get :index, :query_id => q.id
348 348 assert_response 403
349 349 end
350 350
351 351 def test_private_query_should_be_available_to_its_user
352 352 q = IssueQuery.create!(:name => "private", :user => User.find(2), :is_public => false, :project => nil)
353 353 @request.session[:user_id] = 2
354 354
355 355 get :index, :query_id => q.id
356 356 assert_response :success
357 357 end
358 358
359 359 def test_public_query_should_be_available_to_other_users
360 360 q = IssueQuery.create!(:name => "private", :user => User.find(2), :is_public => true, :project => nil)
361 361 @request.session[:user_id] = 3
362 362
363 363 get :index, :query_id => q.id
364 364 assert_response :success
365 365 end
366 366
367 367 def test_index_should_omit_page_param_in_export_links
368 368 get :index, :page => 2
369 369 assert_response :success
370 370 assert_select 'a.atom[href=/issues.atom]'
371 371 assert_select 'a.csv[href=/issues.csv]'
372 372 assert_select 'a.pdf[href=/issues.pdf]'
373 373 assert_select 'form#csv-export-form[action=/issues.csv]'
374 374 end
375 375
376 376 def test_index_csv
377 377 get :index, :format => 'csv'
378 378 assert_response :success
379 379 assert_not_nil assigns(:issues)
380 380 assert_equal 'text/csv; header=present', @response.content_type
381 381 assert @response.body.starts_with?("#,")
382 382 lines = @response.body.chomp.split("\n")
383 assert_equal assigns(:query).columns.size + 1, lines[0].split(',').size
383 assert_equal assigns(:query).columns.size, lines[0].split(',').size
384 384 end
385 385
386 386 def test_index_csv_with_project
387 387 get :index, :project_id => 1, :format => 'csv'
388 388 assert_response :success
389 389 assert_not_nil assigns(:issues)
390 390 assert_equal 'text/csv; header=present', @response.content_type
391 391 end
392 392
393 393 def test_index_csv_with_description
394 394 get :index, :format => 'csv', :description => '1'
395 395 assert_response :success
396 396 assert_not_nil assigns(:issues)
397 397 assert_equal 'text/csv; header=present', @response.content_type
398 398 assert @response.body.starts_with?("#,")
399 399 lines = @response.body.chomp.split("\n")
400 assert_equal assigns(:query).columns.size + 2, lines[0].split(',').size
400 assert_equal assigns(:query).columns.size + 1, lines[0].split(',').size
401 401 end
402 402
403 403 def test_index_csv_with_spent_time_column
404 404 issue = Issue.create!(:project_id => 1, :tracker_id => 1, :subject => 'test_index_csv_with_spent_time_column', :author_id => 2)
405 405 TimeEntry.create!(:project => issue.project, :issue => issue, :hours => 7.33, :user => User.find(2), :spent_on => Date.today)
406 406
407 407 get :index, :format => 'csv', :set_filter => '1', :c => %w(subject spent_hours)
408 408 assert_response :success
409 409 assert_equal 'text/csv; header=present', @response.content_type
410 410 lines = @response.body.chomp.split("\n")
411 411 assert_include "#{issue.id},#{issue.subject},7.33", lines
412 412 end
413 413
414 414 def test_index_csv_with_all_columns
415 415 get :index, :format => 'csv', :columns => 'all'
416 416 assert_response :success
417 417 assert_not_nil assigns(:issues)
418 418 assert_equal 'text/csv; header=present', @response.content_type
419 assert @response.body.starts_with?("#,")
420 lines = @response.body.chomp.split("\n")
421 assert_equal assigns(:query).available_inline_columns.size + 1, lines[0].split(',').size
419 assert_match /\A#,/, response.body
420 lines = response.body.chomp.split("\n")
421 assert_equal assigns(:query).available_inline_columns.size, lines[0].split(',').size
422 422 end
423 423
424 424 def test_index_csv_with_multi_column_field
425 425 CustomField.find(1).update_attribute :multiple, true
426 426 issue = Issue.find(1)
427 427 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
428 428 issue.save!
429 429
430 430 get :index, :format => 'csv', :columns => 'all'
431 431 assert_response :success
432 432 lines = @response.body.chomp.split("\n")
433 433 assert lines.detect {|line| line.include?('"MySQL, Oracle"')}
434 434 end
435 435
436 436 def test_index_csv_should_format_float_custom_fields_with_csv_decimal_separator
437 437 field = IssueCustomField.create!(:name => 'Float', :is_for_all => true, :tracker_ids => [1], :field_format => 'float')
438 438 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id => '185.6'})
439 439
440 440 with_settings :default_language => 'fr' do
441 441 get :index, :format => 'csv', :columns => 'all'
442 442 assert_response :success
443 443 issue_line = response.body.chomp.split("\n").map {|line| line.split(';')}.detect {|line| line[0]==issue.id.to_s}
444 444 assert_include '185,60', issue_line
445 445 end
446 446
447 447 with_settings :default_language => 'en' do
448 448 get :index, :format => 'csv', :columns => 'all'
449 449 assert_response :success
450 450 issue_line = response.body.chomp.split("\n").map {|line| line.split(',')}.detect {|line| line[0]==issue.id.to_s}
451 451 assert_include '185.60', issue_line
452 452 end
453 453 end
454 454
455 455 def test_index_csv_big_5
456 456 with_settings :default_language => "zh-TW" do
457 457 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
458 458 str_big5 = "\xa4@\xa4\xeb"
459 459 if str_utf8.respond_to?(:force_encoding)
460 460 str_utf8.force_encoding('UTF-8')
461 461 str_big5.force_encoding('Big5')
462 462 end
463 463 issue = Issue.generate!(:subject => str_utf8)
464 464
465 465 get :index, :project_id => 1,
466 466 :f => ['subject'],
467 467 :op => '=', :values => [str_utf8],
468 468 :format => 'csv'
469 469 assert_equal 'text/csv; header=present', @response.content_type
470 470 lines = @response.body.chomp.split("\n")
471 471 s1 = "\xaa\xac\xbaA"
472 472 if str_utf8.respond_to?(:force_encoding)
473 473 s1.force_encoding('Big5')
474 474 end
475 475 assert_include s1, lines[0]
476 476 assert_include str_big5, lines[1]
477 477 end
478 478 end
479 479
480 480 def test_index_csv_cannot_convert_should_be_replaced_big_5
481 481 with_settings :default_language => "zh-TW" do
482 482 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
483 483 if str_utf8.respond_to?(:force_encoding)
484 484 str_utf8.force_encoding('UTF-8')
485 485 end
486 486 issue = Issue.generate!(:subject => str_utf8)
487 487
488 488 get :index, :project_id => 1,
489 489 :f => ['subject'],
490 490 :op => '=', :values => [str_utf8],
491 491 :c => ['status', 'subject'],
492 492 :format => 'csv',
493 493 :set_filter => 1
494 494 assert_equal 'text/csv; header=present', @response.content_type
495 495 lines = @response.body.chomp.split("\n")
496 496 s1 = "\xaa\xac\xbaA" # status
497 497 if str_utf8.respond_to?(:force_encoding)
498 498 s1.force_encoding('Big5')
499 499 end
500 500 assert lines[0].include?(s1)
501 501 s2 = lines[1].split(",")[2]
502 502 if s1.respond_to?(:force_encoding)
503 503 s3 = "\xa5H?" # subject
504 504 s3.force_encoding('Big5')
505 505 assert_equal s3, s2
506 506 elsif RUBY_PLATFORM == 'java'
507 507 assert_equal "??", s2
508 508 else
509 509 assert_equal "\xa5H???", s2
510 510 end
511 511 end
512 512 end
513 513
514 514 def test_index_csv_tw
515 515 with_settings :default_language => "zh-TW" do
516 516 str1 = "test_index_csv_tw"
517 517 issue = Issue.generate!(:subject => str1, :estimated_hours => '1234.5')
518 518
519 519 get :index, :project_id => 1,
520 520 :f => ['subject'],
521 521 :op => '=', :values => [str1],
522 522 :c => ['estimated_hours', 'subject'],
523 523 :format => 'csv',
524 524 :set_filter => 1
525 525 assert_equal 'text/csv; header=present', @response.content_type
526 526 lines = @response.body.chomp.split("\n")
527 527 assert_equal "#{issue.id},1234.50,#{str1}", lines[1]
528 528 end
529 529 end
530 530
531 531 def test_index_csv_fr
532 532 with_settings :default_language => "fr" do
533 533 str1 = "test_index_csv_fr"
534 534 issue = Issue.generate!(:subject => str1, :estimated_hours => '1234.5')
535 535
536 536 get :index, :project_id => 1,
537 537 :f => ['subject'],
538 538 :op => '=', :values => [str1],
539 539 :c => ['estimated_hours', 'subject'],
540 540 :format => 'csv',
541 541 :set_filter => 1
542 542 assert_equal 'text/csv; header=present', @response.content_type
543 543 lines = @response.body.chomp.split("\n")
544 544 assert_equal "#{issue.id};1234,50;#{str1}", lines[1]
545 545 end
546 546 end
547 547
548 548 def test_index_pdf
549 549 ["en", "zh", "zh-TW", "ja", "ko"].each do |lang|
550 550 with_settings :default_language => lang do
551 551
552 552 get :index
553 553 assert_response :success
554 554 assert_template 'index'
555 555
556 556 if lang == "ja"
557 557 if RUBY_PLATFORM != 'java'
558 558 assert_equal "CP932", l(:general_pdf_encoding)
559 559 end
560 560 if RUBY_PLATFORM == 'java' && l(:general_pdf_encoding) == "CP932"
561 561 next
562 562 end
563 563 end
564 564
565 565 get :index, :format => 'pdf'
566 566 assert_response :success
567 567 assert_not_nil assigns(:issues)
568 568 assert_equal 'application/pdf', @response.content_type
569 569
570 570 get :index, :project_id => 1, :format => 'pdf'
571 571 assert_response :success
572 572 assert_not_nil assigns(:issues)
573 573 assert_equal 'application/pdf', @response.content_type
574 574
575 575 get :index, :project_id => 1, :query_id => 6, :format => 'pdf'
576 576 assert_response :success
577 577 assert_not_nil assigns(:issues)
578 578 assert_equal 'application/pdf', @response.content_type
579 579 end
580 580 end
581 581 end
582 582
583 583 def test_index_pdf_with_query_grouped_by_list_custom_field
584 584 get :index, :project_id => 1, :query_id => 9, :format => 'pdf'
585 585 assert_response :success
586 586 assert_not_nil assigns(:issues)
587 587 assert_not_nil assigns(:issue_count_by_group)
588 588 assert_equal 'application/pdf', @response.content_type
589 589 end
590 590
591 591 def test_index_atom
592 592 get :index, :project_id => 'ecookbook', :format => 'atom'
593 593 assert_response :success
594 594 assert_template 'common/feed'
595 595 assert_equal 'application/atom+xml', response.content_type
596 596
597 597 assert_select 'feed' do
598 598 assert_select 'link[rel=self][href=?]', 'http://test.host/projects/ecookbook/issues.atom'
599 599 assert_select 'link[rel=alternate][href=?]', 'http://test.host/projects/ecookbook/issues'
600 600 assert_select 'entry link[href=?]', 'http://test.host/issues/1'
601 601 end
602 602 end
603 603
604 604 def test_index_sort
605 605 get :index, :sort => 'tracker,id:desc'
606 606 assert_response :success
607 607
608 608 sort_params = @request.session['issues_index_sort']
609 609 assert sort_params.is_a?(String)
610 610 assert_equal 'tracker,id:desc', sort_params
611 611
612 612 issues = assigns(:issues)
613 613 assert_not_nil issues
614 614 assert !issues.empty?
615 615 assert_equal issues.sort {|a,b| a.tracker == b.tracker ? b.id <=> a.id : a.tracker <=> b.tracker }.collect(&:id), issues.collect(&:id)
616 616 end
617 617
618 618 def test_index_sort_by_field_not_included_in_columns
619 619 Setting.issue_list_default_columns = %w(subject author)
620 620 get :index, :sort => 'tracker'
621 621 end
622 622
623 623 def test_index_sort_by_assigned_to
624 624 get :index, :sort => 'assigned_to'
625 625 assert_response :success
626 626 assignees = assigns(:issues).collect(&:assigned_to).compact
627 627 assert_equal assignees.sort, assignees
628 628 end
629 629
630 630 def test_index_sort_by_assigned_to_desc
631 631 get :index, :sort => 'assigned_to:desc'
632 632 assert_response :success
633 633 assignees = assigns(:issues).collect(&:assigned_to).compact
634 634 assert_equal assignees.sort.reverse, assignees
635 635 end
636 636
637 637 def test_index_group_by_assigned_to
638 638 get :index, :group_by => 'assigned_to', :sort => 'priority'
639 639 assert_response :success
640 640 end
641 641
642 642 def test_index_sort_by_author
643 643 get :index, :sort => 'author'
644 644 assert_response :success
645 645 authors = assigns(:issues).collect(&:author)
646 646 assert_equal authors.sort, authors
647 647 end
648 648
649 649 def test_index_sort_by_author_desc
650 650 get :index, :sort => 'author:desc'
651 651 assert_response :success
652 652 authors = assigns(:issues).collect(&:author)
653 653 assert_equal authors.sort.reverse, authors
654 654 end
655 655
656 656 def test_index_group_by_author
657 657 get :index, :group_by => 'author', :sort => 'priority'
658 658 assert_response :success
659 659 end
660 660
661 661 def test_index_sort_by_spent_hours
662 662 get :index, :sort => 'spent_hours:desc'
663 663 assert_response :success
664 664 hours = assigns(:issues).collect(&:spent_hours)
665 665 assert_equal hours.sort.reverse, hours
666 666 end
667 667
668 668 def test_index_sort_by_user_custom_field
669 669 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
670 670 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
671 671 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
672 672 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
673 673 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
674 674
675 675 get :index, :project_id => 1, :set_filter => 1, :sort => "cf_#{cf.id},id"
676 676 assert_response :success
677 677
678 678 assert_equal [2, 3, 1], assigns(:issues).select {|issue| issue.custom_field_value(cf).present?}.map(&:id)
679 679 end
680 680
681 681 def test_index_with_columns
682 682 columns = ['tracker', 'subject', 'assigned_to']
683 683 get :index, :set_filter => 1, :c => columns
684 684 assert_response :success
685 685
686 686 # query should use specified columns
687 687 query = assigns(:query)
688 688 assert_kind_of IssueQuery, query
689 689 assert_equal columns, query.column_names.map(&:to_s)
690 690
691 691 # columns should be stored in session
692 692 assert_kind_of Hash, session[:query]
693 693 assert_kind_of Array, session[:query][:column_names]
694 694 assert_equal columns, session[:query][:column_names].map(&:to_s)
695 695
696 696 # ensure only these columns are kept in the selected columns list
697 697 assert_select 'select#selected_columns option' do
698 698 assert_select 'option', 3
699 699 assert_select 'option[value=tracker]'
700 700 assert_select 'option[value=project]', 0
701 701 end
702 702 end
703 703
704 704 def test_index_without_project_should_implicitly_add_project_column_to_default_columns
705 705 Setting.issue_list_default_columns = ['tracker', 'subject', 'assigned_to']
706 706 get :index, :set_filter => 1
707 707
708 708 # query should use specified columns
709 709 query = assigns(:query)
710 710 assert_kind_of IssueQuery, query
711 assert_equal [:project, :tracker, :subject, :assigned_to], query.columns.map(&:name)
711 assert_equal [:id, :project, :tracker, :subject, :assigned_to], query.columns.map(&:name)
712 712 end
713 713
714 714 def test_index_without_project_and_explicit_default_columns_should_not_add_project_column
715 715 Setting.issue_list_default_columns = ['tracker', 'subject', 'assigned_to']
716 columns = ['tracker', 'subject', 'assigned_to']
716 columns = ['id', 'tracker', 'subject', 'assigned_to']
717 717 get :index, :set_filter => 1, :c => columns
718 718
719 719 # query should use specified columns
720 720 query = assigns(:query)
721 721 assert_kind_of IssueQuery, query
722 722 assert_equal columns.map(&:to_sym), query.columns.map(&:name)
723 723 end
724 724
725 725 def test_index_with_custom_field_column
726 726 columns = %w(tracker subject cf_2)
727 727 get :index, :set_filter => 1, :c => columns
728 728 assert_response :success
729 729
730 730 # query should use specified columns
731 731 query = assigns(:query)
732 732 assert_kind_of IssueQuery, query
733 733 assert_equal columns, query.column_names.map(&:to_s)
734 734
735 735 assert_select 'table.issues td.cf_2.string'
736 736 end
737 737
738 738 def test_index_with_multi_custom_field_column
739 739 field = CustomField.find(1)
740 740 field.update_attribute :multiple, true
741 741 issue = Issue.find(1)
742 742 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
743 743 issue.save!
744 744
745 745 get :index, :set_filter => 1, :c => %w(tracker subject cf_1)
746 746 assert_response :success
747 747
748 748 assert_select 'table.issues td.cf_1', :text => 'MySQL, Oracle'
749 749 end
750 750
751 751 def test_index_with_multi_user_custom_field_column
752 752 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
753 753 :tracker_ids => [1], :is_for_all => true)
754 754 issue = Issue.find(1)
755 755 issue.custom_field_values = {field.id => ['2', '3']}
756 756 issue.save!
757 757
758 758 get :index, :set_filter => 1, :c => ['tracker', 'subject', "cf_#{field.id}"]
759 759 assert_response :success
760 760
761 761 assert_select "table.issues td.cf_#{field.id}" do
762 762 assert_select 'a', 2
763 763 assert_select 'a[href=?]', '/users/2', :text => 'John Smith'
764 764 assert_select 'a[href=?]', '/users/3', :text => 'Dave Lopper'
765 765 end
766 766 end
767 767
768 768 def test_index_with_date_column
769 769 with_settings :date_format => '%d/%m/%Y' do
770 770 Issue.find(1).update_attribute :start_date, '1987-08-24'
771 771
772 772 get :index, :set_filter => 1, :c => %w(start_date)
773 773
774 774 assert_select "table.issues td.start_date", :text => '24/08/1987'
775 775 end
776 776 end
777 777
778 778 def test_index_with_done_ratio_column
779 779 Issue.find(1).update_attribute :done_ratio, 40
780 780
781 781 get :index, :set_filter => 1, :c => %w(done_ratio)
782 782
783 783 assert_select 'table.issues td.done_ratio' do
784 784 assert_select 'table.progress' do
785 785 assert_select 'td.closed[style=?]', 'width: 40%;'
786 786 end
787 787 end
788 788 end
789 789
790 790 def test_index_with_spent_hours_column
791 791 get :index, :set_filter => 1, :c => %w(subject spent_hours)
792 792
793 793 assert_select 'table.issues tr#issue-3 td.spent_hours', :text => '1.00'
794 794 end
795 795
796 796 def test_index_should_not_show_spent_hours_column_without_permission
797 797 Role.anonymous.remove_permission! :view_time_entries
798 798 get :index, :set_filter => 1, :c => %w(subject spent_hours)
799 799
800 800 assert_select 'td.spent_hours', 0
801 801 end
802 802
803 803 def test_index_with_fixed_version_column
804 804 get :index, :set_filter => 1, :c => %w(fixed_version)
805 805
806 806 assert_select 'table.issues td.fixed_version' do
807 807 assert_select 'a[href=?]', '/versions/2', :text => '1.0'
808 808 end
809 809 end
810 810
811 811 def test_index_with_relations_column
812 812 IssueRelation.delete_all
813 813 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(7))
814 814 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(8), :issue_to => Issue.find(1))
815 815 IssueRelation.create!(:relation_type => "blocks", :issue_from => Issue.find(1), :issue_to => Issue.find(11))
816 816 IssueRelation.create!(:relation_type => "blocks", :issue_from => Issue.find(12), :issue_to => Issue.find(2))
817 817
818 818 get :index, :set_filter => 1, :c => %w(subject relations)
819 819 assert_response :success
820 820 assert_select "tr#issue-1 td.relations" do
821 821 assert_select "span", 3
822 822 assert_select "span", :text => "Related to #7"
823 823 assert_select "span", :text => "Related to #8"
824 824 assert_select "span", :text => "Blocks #11"
825 825 end
826 826 assert_select "tr#issue-2 td.relations" do
827 827 assert_select "span", 1
828 828 assert_select "span", :text => "Blocked by #12"
829 829 end
830 830 assert_select "tr#issue-3 td.relations" do
831 831 assert_select "span", 0
832 832 end
833 833
834 834 get :index, :set_filter => 1, :c => %w(relations), :format => 'csv'
835 835 assert_response :success
836 836 assert_equal 'text/csv; header=present', response.content_type
837 837 lines = response.body.chomp.split("\n")
838 838 assert_include '1,"Related to #7, Related to #8, Blocks #11"', lines
839 839 assert_include '2,Blocked by #12', lines
840 840 assert_include '3,""', lines
841 841
842 842 get :index, :set_filter => 1, :c => %w(subject relations), :format => 'pdf'
843 843 assert_response :success
844 844 assert_equal 'application/pdf', response.content_type
845 845 end
846 846
847 847 def test_index_with_description_column
848 848 get :index, :set_filter => 1, :c => %w(subject description)
849 849
850 850 assert_select 'table.issues thead th', 3 # columns: chekbox + id + subject
851 851 assert_select 'td.description[colspan=3]', :text => 'Unable to print recipes'
852 852
853 853 get :index, :set_filter => 1, :c => %w(subject description), :format => 'pdf'
854 854 assert_response :success
855 855 assert_equal 'application/pdf', response.content_type
856 856 end
857 857
858 858 def test_index_send_html_if_query_is_invalid
859 859 get :index, :f => ['start_date'], :op => {:start_date => '='}
860 860 assert_equal 'text/html', @response.content_type
861 861 assert_template 'index'
862 862 end
863 863
864 864 def test_index_send_nothing_if_query_is_invalid
865 865 get :index, :f => ['start_date'], :op => {:start_date => '='}, :format => 'csv'
866 866 assert_equal 'text/csv', @response.content_type
867 867 assert @response.body.blank?
868 868 end
869 869
870 870 def test_show_by_anonymous
871 871 get :show, :id => 1
872 872 assert_response :success
873 873 assert_template 'show'
874 874 assert_equal Issue.find(1), assigns(:issue)
875 875
876 876 assert_select 'div.issue div.description', :text => /Unable to print recipes/
877 877
878 878 # anonymous role is allowed to add a note
879 879 assert_select 'form#issue-form' do
880 880 assert_select 'fieldset' do
881 881 assert_select 'legend', :text => 'Notes'
882 882 assert_select 'textarea[name=?]', 'issue[notes]'
883 883 end
884 884 end
885 885
886 886 assert_select 'title', :text => "Bug #1: Can&#x27;t print recipes - eCookbook - Redmine"
887 887 end
888 888
889 889 def test_show_by_manager
890 890 @request.session[:user_id] = 2
891 891 get :show, :id => 1
892 892 assert_response :success
893 893
894 894 assert_select 'a', :text => /Quote/
895 895
896 896 assert_select 'form#issue-form' do
897 897 assert_select 'fieldset' do
898 898 assert_select 'legend', :text => 'Change properties'
899 899 assert_select 'input[name=?]', 'issue[subject]'
900 900 end
901 901 assert_select 'fieldset' do
902 902 assert_select 'legend', :text => 'Log time'
903 903 assert_select 'input[name=?]', 'time_entry[hours]'
904 904 end
905 905 assert_select 'fieldset' do
906 906 assert_select 'legend', :text => 'Notes'
907 907 assert_select 'textarea[name=?]', 'issue[notes]'
908 908 end
909 909 end
910 910 end
911 911
912 912 def test_show_should_display_update_form
913 913 @request.session[:user_id] = 2
914 914 get :show, :id => 1
915 915 assert_response :success
916 916
917 917 assert_select 'form#issue-form' do
918 918 assert_select 'input[name=?]', 'issue[is_private]'
919 919 assert_select 'select[name=?]', 'issue[project_id]'
920 920 assert_select 'select[name=?]', 'issue[tracker_id]'
921 921 assert_select 'input[name=?]', 'issue[subject]'
922 922 assert_select 'textarea[name=?]', 'issue[description]'
923 923 assert_select 'select[name=?]', 'issue[status_id]'
924 924 assert_select 'select[name=?]', 'issue[priority_id]'
925 925 assert_select 'select[name=?]', 'issue[assigned_to_id]'
926 926 assert_select 'select[name=?]', 'issue[category_id]'
927 927 assert_select 'select[name=?]', 'issue[fixed_version_id]'
928 928 assert_select 'input[name=?]', 'issue[parent_issue_id]'
929 929 assert_select 'input[name=?]', 'issue[start_date]'
930 930 assert_select 'input[name=?]', 'issue[due_date]'
931 931 assert_select 'select[name=?]', 'issue[done_ratio]'
932 932 assert_select 'input[name=?]', 'issue[custom_field_values][2]'
933 933 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
934 934 assert_select 'textarea[name=?]', 'issue[notes]'
935 935 end
936 936 end
937 937
938 938 def test_show_should_display_update_form_with_minimal_permissions
939 939 Role.find(1).update_attribute :permissions, [:view_issues, :add_issue_notes]
940 940 WorkflowTransition.delete_all :role_id => 1
941 941
942 942 @request.session[:user_id] = 2
943 943 get :show, :id => 1
944 944 assert_response :success
945 945
946 946 assert_select 'form#issue-form' do
947 947 assert_select 'input[name=?]', 'issue[is_private]', 0
948 948 assert_select 'select[name=?]', 'issue[project_id]', 0
949 949 assert_select 'select[name=?]', 'issue[tracker_id]', 0
950 950 assert_select 'input[name=?]', 'issue[subject]', 0
951 951 assert_select 'textarea[name=?]', 'issue[description]', 0
952 952 assert_select 'select[name=?]', 'issue[status_id]', 0
953 953 assert_select 'select[name=?]', 'issue[priority_id]', 0
954 954 assert_select 'select[name=?]', 'issue[assigned_to_id]', 0
955 955 assert_select 'select[name=?]', 'issue[category_id]', 0
956 956 assert_select 'select[name=?]', 'issue[fixed_version_id]', 0
957 957 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
958 958 assert_select 'input[name=?]', 'issue[start_date]', 0
959 959 assert_select 'input[name=?]', 'issue[due_date]', 0
960 960 assert_select 'select[name=?]', 'issue[done_ratio]', 0
961 961 assert_select 'input[name=?]', 'issue[custom_field_values][2]', 0
962 962 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
963 963 assert_select 'textarea[name=?]', 'issue[notes]'
964 964 end
965 965 end
966 966
967 967 def test_show_should_display_update_form_with_workflow_permissions
968 968 Role.find(1).update_attribute :permissions, [:view_issues, :add_issue_notes]
969 969
970 970 @request.session[:user_id] = 2
971 971 get :show, :id => 1
972 972 assert_response :success
973 973
974 974 assert_select 'form#issue-form' do
975 975 assert_select 'input[name=?]', 'issue[is_private]', 0
976 976 assert_select 'select[name=?]', 'issue[project_id]', 0
977 977 assert_select 'select[name=?]', 'issue[tracker_id]', 0
978 978 assert_select 'input[name=?]', 'issue[subject]', 0
979 979 assert_select 'textarea[name=?]', 'issue[description]', 0
980 980 assert_select 'select[name=?]', 'issue[status_id]'
981 981 assert_select 'select[name=?]', 'issue[priority_id]', 0
982 982 assert_select 'select[name=?]', 'issue[assigned_to_id]'
983 983 assert_select 'select[name=?]', 'issue[category_id]', 0
984 984 assert_select 'select[name=?]', 'issue[fixed_version_id]'
985 985 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
986 986 assert_select 'input[name=?]', 'issue[start_date]', 0
987 987 assert_select 'input[name=?]', 'issue[due_date]', 0
988 988 assert_select 'select[name=?]', 'issue[done_ratio]'
989 989 assert_select 'input[name=?]', 'issue[custom_field_values][2]', 0
990 990 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
991 991 assert_select 'textarea[name=?]', 'issue[notes]'
992 992 end
993 993 end
994 994
995 995 def test_show_should_not_display_update_form_without_permissions
996 996 Role.find(1).update_attribute :permissions, [:view_issues]
997 997
998 998 @request.session[:user_id] = 2
999 999 get :show, :id => 1
1000 1000 assert_response :success
1001 1001
1002 1002 assert_select 'form#issue-form', 0
1003 1003 end
1004 1004
1005 1005 def test_update_form_should_not_display_inactive_enumerations
1006 1006 assert !IssuePriority.find(15).active?
1007 1007
1008 1008 @request.session[:user_id] = 2
1009 1009 get :show, :id => 1
1010 1010 assert_response :success
1011 1011
1012 1012 assert_select 'form#issue-form' do
1013 1013 assert_select 'select[name=?]', 'issue[priority_id]' do
1014 1014 assert_select 'option[value=4]'
1015 1015 assert_select 'option[value=15]', 0
1016 1016 end
1017 1017 end
1018 1018 end
1019 1019
1020 1020 def test_update_form_should_allow_attachment_upload
1021 1021 @request.session[:user_id] = 2
1022 1022 get :show, :id => 1
1023 1023
1024 1024 assert_select 'form#issue-form[method=post][enctype=multipart/form-data]' do
1025 1025 assert_select 'input[type=file][name=?]', 'attachments[dummy][file]'
1026 1026 end
1027 1027 end
1028 1028
1029 1029 def test_show_should_deny_anonymous_access_without_permission
1030 1030 Role.anonymous.remove_permission!(:view_issues)
1031 1031 get :show, :id => 1
1032 1032 assert_response :redirect
1033 1033 end
1034 1034
1035 1035 def test_show_should_deny_anonymous_access_to_private_issue
1036 1036 Issue.update_all(["is_private = ?", true], "id = 1")
1037 1037 get :show, :id => 1
1038 1038 assert_response :redirect
1039 1039 end
1040 1040
1041 1041 def test_show_should_deny_non_member_access_without_permission
1042 1042 Role.non_member.remove_permission!(:view_issues)
1043 1043 @request.session[:user_id] = 9
1044 1044 get :show, :id => 1
1045 1045 assert_response 403
1046 1046 end
1047 1047
1048 1048 def test_show_should_deny_non_member_access_to_private_issue
1049 1049 Issue.update_all(["is_private = ?", true], "id = 1")
1050 1050 @request.session[:user_id] = 9
1051 1051 get :show, :id => 1
1052 1052 assert_response 403
1053 1053 end
1054 1054
1055 1055 def test_show_should_deny_member_access_without_permission
1056 1056 Role.find(1).remove_permission!(:view_issues)
1057 1057 @request.session[:user_id] = 2
1058 1058 get :show, :id => 1
1059 1059 assert_response 403
1060 1060 end
1061 1061
1062 1062 def test_show_should_deny_member_access_to_private_issue_without_permission
1063 1063 Issue.update_all(["is_private = ?", true], "id = 1")
1064 1064 @request.session[:user_id] = 3
1065 1065 get :show, :id => 1
1066 1066 assert_response 403
1067 1067 end
1068 1068
1069 1069 def test_show_should_allow_author_access_to_private_issue
1070 1070 Issue.update_all(["is_private = ?, author_id = 3", true], "id = 1")
1071 1071 @request.session[:user_id] = 3
1072 1072 get :show, :id => 1
1073 1073 assert_response :success
1074 1074 end
1075 1075
1076 1076 def test_show_should_allow_assignee_access_to_private_issue
1077 1077 Issue.update_all(["is_private = ?, assigned_to_id = 3", true], "id = 1")
1078 1078 @request.session[:user_id] = 3
1079 1079 get :show, :id => 1
1080 1080 assert_response :success
1081 1081 end
1082 1082
1083 1083 def test_show_should_allow_member_access_to_private_issue_with_permission
1084 1084 Issue.update_all(["is_private = ?", true], "id = 1")
1085 1085 User.find(3).roles_for_project(Project.find(1)).first.update_attribute :issues_visibility, 'all'
1086 1086 @request.session[:user_id] = 3
1087 1087 get :show, :id => 1
1088 1088 assert_response :success
1089 1089 end
1090 1090
1091 1091 def test_show_should_not_disclose_relations_to_invisible_issues
1092 1092 Setting.cross_project_issue_relations = '1'
1093 1093 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => 'relates')
1094 1094 # Relation to a private project issue
1095 1095 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(4), :relation_type => 'relates')
1096 1096
1097 1097 get :show, :id => 1
1098 1098 assert_response :success
1099 1099
1100 1100 assert_select 'div#relations' do
1101 1101 assert_select 'a', :text => /#2$/
1102 1102 assert_select 'a', :text => /#4$/, :count => 0
1103 1103 end
1104 1104 end
1105 1105
1106 1106 def test_show_should_list_subtasks
1107 1107 Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')
1108 1108
1109 1109 get :show, :id => 1
1110 1110 assert_response :success
1111 1111
1112 1112 assert_select 'div#issue_tree' do
1113 1113 assert_select 'td.subject', :text => /Child Issue/
1114 1114 end
1115 1115 end
1116 1116
1117 1117 def test_show_should_list_parents
1118 1118 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')
1119 1119
1120 1120 get :show, :id => issue.id
1121 1121 assert_response :success
1122 1122
1123 1123 assert_select 'div.subject' do
1124 1124 assert_select 'h3', 'Child Issue'
1125 1125 assert_select 'a[href=/issues/1]'
1126 1126 end
1127 1127 end
1128 1128
1129 1129 def test_show_should_not_display_prev_next_links_without_query_in_session
1130 1130 get :show, :id => 1
1131 1131 assert_response :success
1132 1132 assert_nil assigns(:prev_issue_id)
1133 1133 assert_nil assigns(:next_issue_id)
1134 1134
1135 1135 assert_select 'div.next-prev-links', 0
1136 1136 end
1137 1137
1138 1138 def test_show_should_display_prev_next_links_with_query_in_session
1139 1139 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => nil}
1140 1140 @request.session['issues_index_sort'] = 'id'
1141 1141
1142 1142 with_settings :display_subprojects_issues => '0' do
1143 1143 get :show, :id => 3
1144 1144 end
1145 1145
1146 1146 assert_response :success
1147 1147 # Previous and next issues for all projects
1148 1148 assert_equal 2, assigns(:prev_issue_id)
1149 1149 assert_equal 5, assigns(:next_issue_id)
1150 1150
1151 1151 count = Issue.open.visible.count
1152 1152
1153 1153 assert_select 'div.next-prev-links' do
1154 1154 assert_select 'a[href=/issues/2]', :text => /Previous/
1155 1155 assert_select 'a[href=/issues/5]', :text => /Next/
1156 1156 assert_select 'span.position', :text => "3 of #{count}"
1157 1157 end
1158 1158 end
1159 1159
1160 1160 def test_show_should_display_prev_next_links_with_saved_query_in_session
1161 1161 query = IssueQuery.create!(:name => 'test', :is_public => true, :user_id => 1,
1162 1162 :filters => {'status_id' => {:values => ['5'], :operator => '='}},
1163 1163 :sort_criteria => [['id', 'asc']])
1164 1164 @request.session[:query] = {:id => query.id, :project_id => nil}
1165 1165
1166 1166 get :show, :id => 11
1167 1167
1168 1168 assert_response :success
1169 1169 assert_equal query, assigns(:query)
1170 1170 # Previous and next issues for all projects
1171 1171 assert_equal 8, assigns(:prev_issue_id)
1172 1172 assert_equal 12, assigns(:next_issue_id)
1173 1173
1174 1174 assert_select 'div.next-prev-links' do
1175 1175 assert_select 'a[href=/issues/8]', :text => /Previous/
1176 1176 assert_select 'a[href=/issues/12]', :text => /Next/
1177 1177 end
1178 1178 end
1179 1179
1180 1180 def test_show_should_display_prev_next_links_with_query_and_sort_on_association
1181 1181 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => nil}
1182 1182
1183 1183 %w(project tracker status priority author assigned_to category fixed_version).each do |assoc_sort|
1184 1184 @request.session['issues_index_sort'] = assoc_sort
1185 1185
1186 1186 get :show, :id => 3
1187 1187 assert_response :success, "Wrong response status for #{assoc_sort} sort"
1188 1188
1189 1189 assert_select 'div.next-prev-links' do
1190 1190 assert_select 'a', :text => /(Previous|Next)/
1191 1191 end
1192 1192 end
1193 1193 end
1194 1194
1195 1195 def test_show_should_display_prev_next_links_with_project_query_in_session
1196 1196 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => 1}
1197 1197 @request.session['issues_index_sort'] = 'id'
1198 1198
1199 1199 with_settings :display_subprojects_issues => '0' do
1200 1200 get :show, :id => 3
1201 1201 end
1202 1202
1203 1203 assert_response :success
1204 1204 # Previous and next issues inside project
1205 1205 assert_equal 2, assigns(:prev_issue_id)
1206 1206 assert_equal 7, assigns(:next_issue_id)
1207 1207
1208 1208 assert_select 'div.next-prev-links' do
1209 1209 assert_select 'a[href=/issues/2]', :text => /Previous/
1210 1210 assert_select 'a[href=/issues/7]', :text => /Next/
1211 1211 end
1212 1212 end
1213 1213
1214 1214 def test_show_should_not_display_prev_link_for_first_issue
1215 1215 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => 1}
1216 1216 @request.session['issues_index_sort'] = 'id'
1217 1217
1218 1218 with_settings :display_subprojects_issues => '0' do
1219 1219 get :show, :id => 1
1220 1220 end
1221 1221
1222 1222 assert_response :success
1223 1223 assert_nil assigns(:prev_issue_id)
1224 1224 assert_equal 2, assigns(:next_issue_id)
1225 1225
1226 1226 assert_select 'div.next-prev-links' do
1227 1227 assert_select 'a', :text => /Previous/, :count => 0
1228 1228 assert_select 'a[href=/issues/2]', :text => /Next/
1229 1229 end
1230 1230 end
1231 1231
1232 1232 def test_show_should_not_display_prev_next_links_for_issue_not_in_query_results
1233 1233 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'c'}}, :project_id => 1}
1234 1234 @request.session['issues_index_sort'] = 'id'
1235 1235
1236 1236 get :show, :id => 1
1237 1237
1238 1238 assert_response :success
1239 1239 assert_nil assigns(:prev_issue_id)
1240 1240 assert_nil assigns(:next_issue_id)
1241 1241
1242 1242 assert_select 'a', :text => /Previous/, :count => 0
1243 1243 assert_select 'a', :text => /Next/, :count => 0
1244 1244 end
1245 1245
1246 1246 def test_show_show_should_display_prev_next_links_with_query_sort_by_user_custom_field
1247 1247 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
1248 1248 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
1249 1249 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
1250 1250 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
1251 1251 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
1252 1252
1253 1253 query = IssueQuery.create!(:name => 'test', :is_public => true, :user_id => 1, :filters => {},
1254 1254 :sort_criteria => [["cf_#{cf.id}", 'asc'], ['id', 'asc']])
1255 1255 @request.session[:query] = {:id => query.id, :project_id => nil}
1256 1256
1257 1257 get :show, :id => 3
1258 1258 assert_response :success
1259 1259
1260 1260 assert_equal 2, assigns(:prev_issue_id)
1261 1261 assert_equal 1, assigns(:next_issue_id)
1262 1262
1263 1263 assert_select 'div.next-prev-links' do
1264 1264 assert_select 'a[href=/issues/2]', :text => /Previous/
1265 1265 assert_select 'a[href=/issues/1]', :text => /Next/
1266 1266 end
1267 1267 end
1268 1268
1269 1269 def test_show_should_display_link_to_the_assignee
1270 1270 get :show, :id => 2
1271 1271 assert_response :success
1272 1272 assert_select '.assigned-to' do
1273 1273 assert_select 'a[href=/users/3]'
1274 1274 end
1275 1275 end
1276 1276
1277 1277 def test_show_should_display_visible_changesets_from_other_projects
1278 1278 project = Project.find(2)
1279 1279 issue = project.issues.first
1280 1280 issue.changeset_ids = [102]
1281 1281 issue.save!
1282 1282 # changesets from other projects should be displayed even if repository
1283 1283 # is disabled on issue's project
1284 1284 project.disable_module! :repository
1285 1285
1286 1286 @request.session[:user_id] = 2
1287 1287 get :show, :id => issue.id
1288 1288
1289 1289 assert_select 'a[href=?]', '/projects/ecookbook/repository/revisions/3'
1290 1290 end
1291 1291
1292 1292 def test_show_should_display_watchers
1293 1293 @request.session[:user_id] = 2
1294 1294 Issue.find(1).add_watcher User.find(2)
1295 1295
1296 1296 get :show, :id => 1
1297 1297 assert_select 'div#watchers ul' do
1298 1298 assert_select 'li' do
1299 1299 assert_select 'a[href=/users/2]'
1300 1300 assert_select 'a img[alt=Delete]'
1301 1301 end
1302 1302 end
1303 1303 end
1304 1304
1305 1305 def test_show_should_display_watchers_with_gravatars
1306 1306 @request.session[:user_id] = 2
1307 1307 Issue.find(1).add_watcher User.find(2)
1308 1308
1309 1309 with_settings :gravatar_enabled => '1' do
1310 1310 get :show, :id => 1
1311 1311 end
1312 1312
1313 1313 assert_select 'div#watchers ul' do
1314 1314 assert_select 'li' do
1315 1315 assert_select 'img.gravatar'
1316 1316 assert_select 'a[href=/users/2]'
1317 1317 assert_select 'a img[alt=Delete]'
1318 1318 end
1319 1319 end
1320 1320 end
1321 1321
1322 1322 def test_show_with_thumbnails_enabled_should_display_thumbnails
1323 1323 @request.session[:user_id] = 2
1324 1324
1325 1325 with_settings :thumbnails_enabled => '1' do
1326 1326 get :show, :id => 14
1327 1327 assert_response :success
1328 1328 end
1329 1329
1330 1330 assert_select 'div.thumbnails' do
1331 1331 assert_select 'a[href=/attachments/16/testfile.png]' do
1332 1332 assert_select 'img[src=/attachments/thumbnail/16]'
1333 1333 end
1334 1334 end
1335 1335 end
1336 1336
1337 1337 def test_show_with_thumbnails_disabled_should_not_display_thumbnails
1338 1338 @request.session[:user_id] = 2
1339 1339
1340 1340 with_settings :thumbnails_enabled => '0' do
1341 1341 get :show, :id => 14
1342 1342 assert_response :success
1343 1343 end
1344 1344
1345 1345 assert_select 'div.thumbnails', 0
1346 1346 end
1347 1347
1348 1348 def test_show_with_multi_custom_field
1349 1349 field = CustomField.find(1)
1350 1350 field.update_attribute :multiple, true
1351 1351 issue = Issue.find(1)
1352 1352 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
1353 1353 issue.save!
1354 1354
1355 1355 get :show, :id => 1
1356 1356 assert_response :success
1357 1357
1358 1358 assert_select 'td', :text => 'MySQL, Oracle'
1359 1359 end
1360 1360
1361 1361 def test_show_with_multi_user_custom_field
1362 1362 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1363 1363 :tracker_ids => [1], :is_for_all => true)
1364 1364 issue = Issue.find(1)
1365 1365 issue.custom_field_values = {field.id => ['2', '3']}
1366 1366 issue.save!
1367 1367
1368 1368 get :show, :id => 1
1369 1369 assert_response :success
1370 1370
1371 1371 # TODO: should display links
1372 1372 assert_select 'td', :text => 'Dave Lopper, John Smith'
1373 1373 end
1374 1374
1375 1375 def test_show_should_display_private_notes_with_permission_only
1376 1376 journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Privates notes', :private_notes => true, :user_id => 1)
1377 1377 @request.session[:user_id] = 2
1378 1378
1379 1379 get :show, :id => 2
1380 1380 assert_response :success
1381 1381 assert_include journal, assigns(:journals)
1382 1382
1383 1383 Role.find(1).remove_permission! :view_private_notes
1384 1384 get :show, :id => 2
1385 1385 assert_response :success
1386 1386 assert_not_include journal, assigns(:journals)
1387 1387 end
1388 1388
1389 1389 def test_show_atom
1390 1390 get :show, :id => 2, :format => 'atom'
1391 1391 assert_response :success
1392 1392 assert_template 'journals/index'
1393 1393 # Inline image
1394 1394 assert_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
1395 1395 end
1396 1396
1397 1397 def test_show_export_to_pdf
1398 1398 get :show, :id => 3, :format => 'pdf'
1399 1399 assert_response :success
1400 1400 assert_equal 'application/pdf', @response.content_type
1401 1401 assert @response.body.starts_with?('%PDF')
1402 1402 assert_not_nil assigns(:issue)
1403 1403 end
1404 1404
1405 1405 def test_show_export_to_pdf_with_ancestors
1406 1406 issue = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1407 1407
1408 1408 get :show, :id => issue.id, :format => 'pdf'
1409 1409 assert_response :success
1410 1410 assert_equal 'application/pdf', @response.content_type
1411 1411 assert @response.body.starts_with?('%PDF')
1412 1412 end
1413 1413
1414 1414 def test_show_export_to_pdf_with_descendants
1415 1415 c1 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1416 1416 c2 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1417 1417 c3 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => c1.id)
1418 1418
1419 1419 get :show, :id => 1, :format => 'pdf'
1420 1420 assert_response :success
1421 1421 assert_equal 'application/pdf', @response.content_type
1422 1422 assert @response.body.starts_with?('%PDF')
1423 1423 end
1424 1424
1425 1425 def test_show_export_to_pdf_with_journals
1426 1426 get :show, :id => 1, :format => 'pdf'
1427 1427 assert_response :success
1428 1428 assert_equal 'application/pdf', @response.content_type
1429 1429 assert @response.body.starts_with?('%PDF')
1430 1430 end
1431 1431
1432 1432 def test_show_export_to_pdf_with_changesets
1433 1433 Issue.find(3).changesets = Changeset.find_all_by_id(100, 101, 102)
1434 1434
1435 1435 get :show, :id => 3, :format => 'pdf'
1436 1436 assert_response :success
1437 1437 assert_equal 'application/pdf', @response.content_type
1438 1438 assert @response.body.starts_with?('%PDF')
1439 1439 end
1440 1440
1441 1441 def test_show_invalid_should_respond_with_404
1442 1442 get :show, :id => 999
1443 1443 assert_response 404
1444 1444 end
1445 1445
1446 1446 def test_get_new
1447 1447 @request.session[:user_id] = 2
1448 1448 get :new, :project_id => 1, :tracker_id => 1
1449 1449 assert_response :success
1450 1450 assert_template 'new'
1451 1451
1452 1452 assert_select 'form#issue-form' do
1453 1453 assert_select 'input[name=?]', 'issue[is_private]'
1454 1454 assert_select 'select[name=?]', 'issue[project_id]', 0
1455 1455 assert_select 'select[name=?]', 'issue[tracker_id]'
1456 1456 assert_select 'input[name=?]', 'issue[subject]'
1457 1457 assert_select 'textarea[name=?]', 'issue[description]'
1458 1458 assert_select 'select[name=?]', 'issue[status_id]'
1459 1459 assert_select 'select[name=?]', 'issue[priority_id]'
1460 1460 assert_select 'select[name=?]', 'issue[assigned_to_id]'
1461 1461 assert_select 'select[name=?]', 'issue[category_id]'
1462 1462 assert_select 'select[name=?]', 'issue[fixed_version_id]'
1463 1463 assert_select 'input[name=?]', 'issue[parent_issue_id]'
1464 1464 assert_select 'input[name=?]', 'issue[start_date]'
1465 1465 assert_select 'input[name=?]', 'issue[due_date]'
1466 1466 assert_select 'select[name=?]', 'issue[done_ratio]'
1467 1467 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Default string'
1468 1468 assert_select 'input[name=?]', 'issue[watcher_user_ids][]'
1469 1469 end
1470 1470
1471 1471 # Be sure we don't display inactive IssuePriorities
1472 1472 assert ! IssuePriority.find(15).active?
1473 1473 assert_select 'select[name=?]', 'issue[priority_id]' do
1474 1474 assert_select 'option[value=15]', 0
1475 1475 end
1476 1476 end
1477 1477
1478 1478 def test_get_new_with_minimal_permissions
1479 1479 Role.find(1).update_attribute :permissions, [:add_issues]
1480 1480 WorkflowTransition.delete_all :role_id => 1
1481 1481
1482 1482 @request.session[:user_id] = 2
1483 1483 get :new, :project_id => 1, :tracker_id => 1
1484 1484 assert_response :success
1485 1485 assert_template 'new'
1486 1486
1487 1487 assert_select 'form#issue-form' do
1488 1488 assert_select 'input[name=?]', 'issue[is_private]', 0
1489 1489 assert_select 'select[name=?]', 'issue[project_id]', 0
1490 1490 assert_select 'select[name=?]', 'issue[tracker_id]'
1491 1491 assert_select 'input[name=?]', 'issue[subject]'
1492 1492 assert_select 'textarea[name=?]', 'issue[description]'
1493 1493 assert_select 'select[name=?]', 'issue[status_id]'
1494 1494 assert_select 'select[name=?]', 'issue[priority_id]'
1495 1495 assert_select 'select[name=?]', 'issue[assigned_to_id]'
1496 1496 assert_select 'select[name=?]', 'issue[category_id]'
1497 1497 assert_select 'select[name=?]', 'issue[fixed_version_id]'
1498 1498 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
1499 1499 assert_select 'input[name=?]', 'issue[start_date]'
1500 1500 assert_select 'input[name=?]', 'issue[due_date]'
1501 1501 assert_select 'select[name=?]', 'issue[done_ratio]'
1502 1502 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Default string'
1503 1503 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
1504 1504 end
1505 1505 end
1506 1506
1507 1507 def test_get_new_with_list_custom_field
1508 1508 @request.session[:user_id] = 2
1509 1509 get :new, :project_id => 1, :tracker_id => 1
1510 1510 assert_response :success
1511 1511 assert_template 'new'
1512 1512
1513 1513 assert_select 'select.list_cf[name=?]', 'issue[custom_field_values][1]' do
1514 1514 assert_select 'option', 4
1515 1515 assert_select 'option[value=MySQL]', :text => 'MySQL'
1516 1516 end
1517 1517 end
1518 1518
1519 1519 def test_get_new_with_multi_custom_field
1520 1520 field = IssueCustomField.find(1)
1521 1521 field.update_attribute :multiple, true
1522 1522
1523 1523 @request.session[:user_id] = 2
1524 1524 get :new, :project_id => 1, :tracker_id => 1
1525 1525 assert_response :success
1526 1526 assert_template 'new'
1527 1527
1528 1528 assert_select 'select[name=?][multiple=multiple]', 'issue[custom_field_values][1][]' do
1529 1529 assert_select 'option', 3
1530 1530 assert_select 'option[value=MySQL]', :text => 'MySQL'
1531 1531 end
1532 1532 assert_select 'input[name=?][type=hidden][value=?]', 'issue[custom_field_values][1][]', ''
1533 1533 end
1534 1534
1535 1535 def test_get_new_with_multi_user_custom_field
1536 1536 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1537 1537 :tracker_ids => [1], :is_for_all => true)
1538 1538
1539 1539 @request.session[:user_id] = 2
1540 1540 get :new, :project_id => 1, :tracker_id => 1
1541 1541 assert_response :success
1542 1542 assert_template 'new'
1543 1543
1544 1544 assert_select 'select[name=?][multiple=multiple]', "issue[custom_field_values][#{field.id}][]" do
1545 1545 assert_select 'option', Project.find(1).users.count
1546 1546 assert_select 'option[value=2]', :text => 'John Smith'
1547 1547 end
1548 1548 assert_select 'input[name=?][type=hidden][value=?]', "issue[custom_field_values][#{field.id}][]", ''
1549 1549 end
1550 1550
1551 1551 def test_get_new_with_date_custom_field
1552 1552 field = IssueCustomField.create!(:name => 'Date', :field_format => 'date', :tracker_ids => [1], :is_for_all => true)
1553 1553
1554 1554 @request.session[:user_id] = 2
1555 1555 get :new, :project_id => 1, :tracker_id => 1
1556 1556 assert_response :success
1557 1557
1558 1558 assert_select 'input[name=?]', "issue[custom_field_values][#{field.id}]"
1559 1559 end
1560 1560
1561 1561 def test_get_new_with_text_custom_field
1562 1562 field = IssueCustomField.create!(:name => 'Text', :field_format => 'text', :tracker_ids => [1], :is_for_all => true)
1563 1563
1564 1564 @request.session[:user_id] = 2
1565 1565 get :new, :project_id => 1, :tracker_id => 1
1566 1566 assert_response :success
1567 1567
1568 1568 assert_select 'textarea[name=?]', "issue[custom_field_values][#{field.id}]"
1569 1569 end
1570 1570
1571 1571 def test_get_new_without_default_start_date_is_creation_date
1572 1572 Setting.default_issue_start_date_to_creation_date = 0
1573 1573
1574 1574 @request.session[:user_id] = 2
1575 1575 get :new, :project_id => 1, :tracker_id => 1
1576 1576 assert_response :success
1577 1577 assert_template 'new'
1578 1578
1579 1579 assert_select 'input[name=?]', 'issue[start_date]'
1580 1580 assert_select 'input[name=?][value]', 'issue[start_date]', 0
1581 1581 end
1582 1582
1583 1583 def test_get_new_with_default_start_date_is_creation_date
1584 1584 Setting.default_issue_start_date_to_creation_date = 1
1585 1585
1586 1586 @request.session[:user_id] = 2
1587 1587 get :new, :project_id => 1, :tracker_id => 1
1588 1588 assert_response :success
1589 1589 assert_template 'new'
1590 1590
1591 1591 assert_select 'input[name=?][value=?]', 'issue[start_date]', Date.today.to_s
1592 1592 end
1593 1593
1594 1594 def test_get_new_form_should_allow_attachment_upload
1595 1595 @request.session[:user_id] = 2
1596 1596 get :new, :project_id => 1, :tracker_id => 1
1597 1597
1598 1598 assert_select 'form[id=issue-form][method=post][enctype=multipart/form-data]' do
1599 1599 assert_select 'input[name=?][type=file]', 'attachments[dummy][file]'
1600 1600 end
1601 1601 end
1602 1602
1603 1603 def test_get_new_should_prefill_the_form_from_params
1604 1604 @request.session[:user_id] = 2
1605 1605 get :new, :project_id => 1,
1606 1606 :issue => {:tracker_id => 3, :description => 'Prefilled', :custom_field_values => {'2' => 'Custom field value'}}
1607 1607
1608 1608 issue = assigns(:issue)
1609 1609 assert_equal 3, issue.tracker_id
1610 1610 assert_equal 'Prefilled', issue.description
1611 1611 assert_equal 'Custom field value', issue.custom_field_value(2)
1612 1612
1613 1613 assert_select 'select[name=?]', 'issue[tracker_id]' do
1614 1614 assert_select 'option[value=3][selected=selected]'
1615 1615 end
1616 1616 assert_select 'textarea[name=?]', 'issue[description]', :text => /Prefilled/
1617 1617 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Custom field value'
1618 1618 end
1619 1619
1620 1620 def test_get_new_should_mark_required_fields
1621 1621 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1622 1622 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1623 1623 WorkflowPermission.delete_all
1624 1624 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'required')
1625 1625 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'required')
1626 1626 @request.session[:user_id] = 2
1627 1627
1628 1628 get :new, :project_id => 1
1629 1629 assert_response :success
1630 1630 assert_template 'new'
1631 1631
1632 1632 assert_select 'label[for=issue_start_date]' do
1633 1633 assert_select 'span[class=required]', 0
1634 1634 end
1635 1635 assert_select 'label[for=issue_due_date]' do
1636 1636 assert_select 'span[class=required]'
1637 1637 end
1638 1638 assert_select 'label[for=?]', "issue_custom_field_values_#{cf1.id}" do
1639 1639 assert_select 'span[class=required]', 0
1640 1640 end
1641 1641 assert_select 'label[for=?]', "issue_custom_field_values_#{cf2.id}" do
1642 1642 assert_select 'span[class=required]'
1643 1643 end
1644 1644 end
1645 1645
1646 1646 def test_get_new_should_not_display_readonly_fields
1647 1647 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1648 1648 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1649 1649 WorkflowPermission.delete_all
1650 1650 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
1651 1651 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
1652 1652 @request.session[:user_id] = 2
1653 1653
1654 1654 get :new, :project_id => 1
1655 1655 assert_response :success
1656 1656 assert_template 'new'
1657 1657
1658 1658 assert_select 'input[name=?]', 'issue[start_date]'
1659 1659 assert_select 'input[name=?]', 'issue[due_date]', 0
1660 1660 assert_select 'input[name=?]', "issue[custom_field_values][#{cf1.id}]"
1661 1661 assert_select 'input[name=?]', "issue[custom_field_values][#{cf2.id}]", 0
1662 1662 end
1663 1663
1664 1664 def test_get_new_without_tracker_id
1665 1665 @request.session[:user_id] = 2
1666 1666 get :new, :project_id => 1
1667 1667 assert_response :success
1668 1668 assert_template 'new'
1669 1669
1670 1670 issue = assigns(:issue)
1671 1671 assert_not_nil issue
1672 1672 assert_equal Project.find(1).trackers.first, issue.tracker
1673 1673 end
1674 1674
1675 1675 def test_get_new_with_no_default_status_should_display_an_error
1676 1676 @request.session[:user_id] = 2
1677 1677 IssueStatus.delete_all
1678 1678
1679 1679 get :new, :project_id => 1
1680 1680 assert_response 500
1681 1681 assert_error_tag :content => /No default issue/
1682 1682 end
1683 1683
1684 1684 def test_get_new_with_no_tracker_should_display_an_error
1685 1685 @request.session[:user_id] = 2
1686 1686 Tracker.delete_all
1687 1687
1688 1688 get :new, :project_id => 1
1689 1689 assert_response 500
1690 1690 assert_error_tag :content => /No tracker/
1691 1691 end
1692 1692
1693 1693 def test_update_form_for_new_issue
1694 1694 @request.session[:user_id] = 2
1695 1695 xhr :post, :update_form, :project_id => 1,
1696 1696 :issue => {:tracker_id => 2,
1697 1697 :subject => 'This is the test_new issue',
1698 1698 :description => 'This is the description',
1699 1699 :priority_id => 5}
1700 1700 assert_response :success
1701 1701 assert_template 'update_form'
1702 1702 assert_template 'form'
1703 1703 assert_equal 'text/javascript', response.content_type
1704 1704
1705 1705 issue = assigns(:issue)
1706 1706 assert_kind_of Issue, issue
1707 1707 assert_equal 1, issue.project_id
1708 1708 assert_equal 2, issue.tracker_id
1709 1709 assert_equal 'This is the test_new issue', issue.subject
1710 1710 end
1711 1711
1712 1712 def test_update_form_for_new_issue_should_propose_transitions_based_on_initial_status
1713 1713 @request.session[:user_id] = 2
1714 1714 WorkflowTransition.delete_all
1715 1715 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 2)
1716 1716 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5)
1717 1717 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 5, :new_status_id => 4)
1718 1718
1719 1719 xhr :post, :update_form, :project_id => 1,
1720 1720 :issue => {:tracker_id => 1,
1721 1721 :status_id => 5,
1722 1722 :subject => 'This is an issue'}
1723 1723
1724 1724 assert_equal 5, assigns(:issue).status_id
1725 1725 assert_equal [1,2,5], assigns(:allowed_statuses).map(&:id).sort
1726 1726 end
1727 1727
1728 1728 def test_post_create
1729 1729 @request.session[:user_id] = 2
1730 1730 assert_difference 'Issue.count' do
1731 1731 post :create, :project_id => 1,
1732 1732 :issue => {:tracker_id => 3,
1733 1733 :status_id => 2,
1734 1734 :subject => 'This is the test_new issue',
1735 1735 :description => 'This is the description',
1736 1736 :priority_id => 5,
1737 1737 :start_date => '2010-11-07',
1738 1738 :estimated_hours => '',
1739 1739 :custom_field_values => {'2' => 'Value for field 2'}}
1740 1740 end
1741 1741 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1742 1742
1743 1743 issue = Issue.find_by_subject('This is the test_new issue')
1744 1744 assert_not_nil issue
1745 1745 assert_equal 2, issue.author_id
1746 1746 assert_equal 3, issue.tracker_id
1747 1747 assert_equal 2, issue.status_id
1748 1748 assert_equal Date.parse('2010-11-07'), issue.start_date
1749 1749 assert_nil issue.estimated_hours
1750 1750 v = issue.custom_values.where(:custom_field_id => 2).first
1751 1751 assert_not_nil v
1752 1752 assert_equal 'Value for field 2', v.value
1753 1753 end
1754 1754
1755 1755 def test_post_new_with_group_assignment
1756 1756 group = Group.find(11)
1757 1757 project = Project.find(1)
1758 1758 project.members << Member.new(:principal => group, :roles => [Role.givable.first])
1759 1759
1760 1760 with_settings :issue_group_assignment => '1' do
1761 1761 @request.session[:user_id] = 2
1762 1762 assert_difference 'Issue.count' do
1763 1763 post :create, :project_id => project.id,
1764 1764 :issue => {:tracker_id => 3,
1765 1765 :status_id => 1,
1766 1766 :subject => 'This is the test_new_with_group_assignment issue',
1767 1767 :assigned_to_id => group.id}
1768 1768 end
1769 1769 end
1770 1770 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1771 1771
1772 1772 issue = Issue.find_by_subject('This is the test_new_with_group_assignment issue')
1773 1773 assert_not_nil issue
1774 1774 assert_equal group, issue.assigned_to
1775 1775 end
1776 1776
1777 1777 def test_post_create_without_start_date_and_default_start_date_is_not_creation_date
1778 1778 Setting.default_issue_start_date_to_creation_date = 0
1779 1779
1780 1780 @request.session[:user_id] = 2
1781 1781 assert_difference 'Issue.count' do
1782 1782 post :create, :project_id => 1,
1783 1783 :issue => {:tracker_id => 3,
1784 1784 :status_id => 2,
1785 1785 :subject => 'This is the test_new issue',
1786 1786 :description => 'This is the description',
1787 1787 :priority_id => 5,
1788 1788 :estimated_hours => '',
1789 1789 :custom_field_values => {'2' => 'Value for field 2'}}
1790 1790 end
1791 1791 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1792 1792
1793 1793 issue = Issue.find_by_subject('This is the test_new issue')
1794 1794 assert_not_nil issue
1795 1795 assert_nil issue.start_date
1796 1796 end
1797 1797
1798 1798 def test_post_create_without_start_date_and_default_start_date_is_creation_date
1799 1799 Setting.default_issue_start_date_to_creation_date = 1
1800 1800
1801 1801 @request.session[:user_id] = 2
1802 1802 assert_difference 'Issue.count' do
1803 1803 post :create, :project_id => 1,
1804 1804 :issue => {:tracker_id => 3,
1805 1805 :status_id => 2,
1806 1806 :subject => 'This is the test_new issue',
1807 1807 :description => 'This is the description',
1808 1808 :priority_id => 5,
1809 1809 :estimated_hours => '',
1810 1810 :custom_field_values => {'2' => 'Value for field 2'}}
1811 1811 end
1812 1812 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1813 1813
1814 1814 issue = Issue.find_by_subject('This is the test_new issue')
1815 1815 assert_not_nil issue
1816 1816 assert_equal Date.today, issue.start_date
1817 1817 end
1818 1818
1819 1819 def test_post_create_and_continue
1820 1820 @request.session[:user_id] = 2
1821 1821 assert_difference 'Issue.count' do
1822 1822 post :create, :project_id => 1,
1823 1823 :issue => {:tracker_id => 3, :subject => 'This is first issue', :priority_id => 5},
1824 1824 :continue => ''
1825 1825 end
1826 1826
1827 1827 issue = Issue.first(:order => 'id DESC')
1828 1828 assert_redirected_to :controller => 'issues', :action => 'new', :project_id => 'ecookbook', :issue => {:tracker_id => 3}
1829 1829 assert_not_nil flash[:notice], "flash was not set"
1830 1830 assert_include %|<a href="/issues/#{issue.id}" title="This is first issue">##{issue.id}</a>|, flash[:notice], "issue link not found in the flash message"
1831 1831 end
1832 1832
1833 1833 def test_post_create_without_custom_fields_param
1834 1834 @request.session[:user_id] = 2
1835 1835 assert_difference 'Issue.count' do
1836 1836 post :create, :project_id => 1,
1837 1837 :issue => {:tracker_id => 1,
1838 1838 :subject => 'This is the test_new issue',
1839 1839 :description => 'This is the description',
1840 1840 :priority_id => 5}
1841 1841 end
1842 1842 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1843 1843 end
1844 1844
1845 1845 def test_post_create_with_multi_custom_field
1846 1846 field = IssueCustomField.find_by_name('Database')
1847 1847 field.update_attribute(:multiple, true)
1848 1848
1849 1849 @request.session[:user_id] = 2
1850 1850 assert_difference 'Issue.count' do
1851 1851 post :create, :project_id => 1,
1852 1852 :issue => {:tracker_id => 1,
1853 1853 :subject => 'This is the test_new issue',
1854 1854 :description => 'This is the description',
1855 1855 :priority_id => 5,
1856 1856 :custom_field_values => {'1' => ['', 'MySQL', 'Oracle']}}
1857 1857 end
1858 1858 assert_response 302
1859 1859 issue = Issue.first(:order => 'id DESC')
1860 1860 assert_equal ['MySQL', 'Oracle'], issue.custom_field_value(1).sort
1861 1861 end
1862 1862
1863 1863 def test_post_create_with_empty_multi_custom_field
1864 1864 field = IssueCustomField.find_by_name('Database')
1865 1865 field.update_attribute(:multiple, true)
1866 1866
1867 1867 @request.session[:user_id] = 2
1868 1868 assert_difference 'Issue.count' do
1869 1869 post :create, :project_id => 1,
1870 1870 :issue => {:tracker_id => 1,
1871 1871 :subject => 'This is the test_new issue',
1872 1872 :description => 'This is the description',
1873 1873 :priority_id => 5,
1874 1874 :custom_field_values => {'1' => ['']}}
1875 1875 end
1876 1876 assert_response 302
1877 1877 issue = Issue.first(:order => 'id DESC')
1878 1878 assert_equal [''], issue.custom_field_value(1).sort
1879 1879 end
1880 1880
1881 1881 def test_post_create_with_multi_user_custom_field
1882 1882 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1883 1883 :tracker_ids => [1], :is_for_all => true)
1884 1884
1885 1885 @request.session[:user_id] = 2
1886 1886 assert_difference 'Issue.count' do
1887 1887 post :create, :project_id => 1,
1888 1888 :issue => {:tracker_id => 1,
1889 1889 :subject => 'This is the test_new issue',
1890 1890 :description => 'This is the description',
1891 1891 :priority_id => 5,
1892 1892 :custom_field_values => {field.id.to_s => ['', '2', '3']}}
1893 1893 end
1894 1894 assert_response 302
1895 1895 issue = Issue.first(:order => 'id DESC')
1896 1896 assert_equal ['2', '3'], issue.custom_field_value(field).sort
1897 1897 end
1898 1898
1899 1899 def test_post_create_with_required_custom_field_and_without_custom_fields_param
1900 1900 field = IssueCustomField.find_by_name('Database')
1901 1901 field.update_attribute(:is_required, true)
1902 1902
1903 1903 @request.session[:user_id] = 2
1904 1904 assert_no_difference 'Issue.count' do
1905 1905 post :create, :project_id => 1,
1906 1906 :issue => {:tracker_id => 1,
1907 1907 :subject => 'This is the test_new issue',
1908 1908 :description => 'This is the description',
1909 1909 :priority_id => 5}
1910 1910 end
1911 1911 assert_response :success
1912 1912 assert_template 'new'
1913 1913 issue = assigns(:issue)
1914 1914 assert_not_nil issue
1915 1915 assert_error_tag :content => /Database can&#x27;t be blank/
1916 1916 end
1917 1917
1918 1918 def test_create_should_validate_required_fields
1919 1919 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1920 1920 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1921 1921 WorkflowPermission.delete_all
1922 1922 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'due_date', :rule => 'required')
1923 1923 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'required')
1924 1924 @request.session[:user_id] = 2
1925 1925
1926 1926 assert_no_difference 'Issue.count' do
1927 1927 post :create, :project_id => 1, :issue => {
1928 1928 :tracker_id => 2,
1929 1929 :status_id => 1,
1930 1930 :subject => 'Test',
1931 1931 :start_date => '',
1932 1932 :due_date => '',
1933 1933 :custom_field_values => {cf1.id.to_s => '', cf2.id.to_s => ''}
1934 1934 }
1935 1935 assert_response :success
1936 1936 assert_template 'new'
1937 1937 end
1938 1938
1939 1939 assert_error_tag :content => /Due date can&#x27;t be blank/i
1940 1940 assert_error_tag :content => /Bar can&#x27;t be blank/i
1941 1941 end
1942 1942
1943 1943 def test_create_should_ignore_readonly_fields
1944 1944 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1945 1945 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1946 1946 WorkflowPermission.delete_all
1947 1947 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
1948 1948 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
1949 1949 @request.session[:user_id] = 2
1950 1950
1951 1951 assert_difference 'Issue.count' do
1952 1952 post :create, :project_id => 1, :issue => {
1953 1953 :tracker_id => 2,
1954 1954 :status_id => 1,
1955 1955 :subject => 'Test',
1956 1956 :start_date => '2012-07-14',
1957 1957 :due_date => '2012-07-16',
1958 1958 :custom_field_values => {cf1.id.to_s => 'value1', cf2.id.to_s => 'value2'}
1959 1959 }
1960 1960 assert_response 302
1961 1961 end
1962 1962
1963 1963 issue = Issue.first(:order => 'id DESC')
1964 1964 assert_equal Date.parse('2012-07-14'), issue.start_date
1965 1965 assert_nil issue.due_date
1966 1966 assert_equal 'value1', issue.custom_field_value(cf1)
1967 1967 assert_nil issue.custom_field_value(cf2)
1968 1968 end
1969 1969
1970 1970 def test_post_create_with_watchers
1971 1971 @request.session[:user_id] = 2
1972 1972 ActionMailer::Base.deliveries.clear
1973 1973
1974 1974 assert_difference 'Watcher.count', 2 do
1975 1975 post :create, :project_id => 1,
1976 1976 :issue => {:tracker_id => 1,
1977 1977 :subject => 'This is a new issue with watchers',
1978 1978 :description => 'This is the description',
1979 1979 :priority_id => 5,
1980 1980 :watcher_user_ids => ['2', '3']}
1981 1981 end
1982 1982 issue = Issue.find_by_subject('This is a new issue with watchers')
1983 1983 assert_not_nil issue
1984 1984 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
1985 1985
1986 1986 # Watchers added
1987 1987 assert_equal [2, 3], issue.watcher_user_ids.sort
1988 1988 assert issue.watched_by?(User.find(3))
1989 1989 # Watchers notified
1990 1990 mail = ActionMailer::Base.deliveries.last
1991 1991 assert_not_nil mail
1992 1992 assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
1993 1993 end
1994 1994
1995 1995 def test_post_create_subissue
1996 1996 @request.session[:user_id] = 2
1997 1997
1998 1998 assert_difference 'Issue.count' do
1999 1999 post :create, :project_id => 1,
2000 2000 :issue => {:tracker_id => 1,
2001 2001 :subject => 'This is a child issue',
2002 2002 :parent_issue_id => '2'}
2003 2003 assert_response 302
2004 2004 end
2005 2005 issue = Issue.order('id DESC').first
2006 2006 assert_equal Issue.find(2), issue.parent
2007 2007 end
2008 2008
2009 2009 def test_post_create_subissue_with_sharp_parent_id
2010 2010 @request.session[:user_id] = 2
2011 2011
2012 2012 assert_difference 'Issue.count' do
2013 2013 post :create, :project_id => 1,
2014 2014 :issue => {:tracker_id => 1,
2015 2015 :subject => 'This is a child issue',
2016 2016 :parent_issue_id => '#2'}
2017 2017 assert_response 302
2018 2018 end
2019 2019 issue = Issue.order('id DESC').first
2020 2020 assert_equal Issue.find(2), issue.parent
2021 2021 end
2022 2022
2023 2023 def test_post_create_subissue_with_non_visible_parent_id_should_not_validate
2024 2024 @request.session[:user_id] = 2
2025 2025
2026 2026 assert_no_difference 'Issue.count' do
2027 2027 post :create, :project_id => 1,
2028 2028 :issue => {:tracker_id => 1,
2029 2029 :subject => 'This is a child issue',
2030 2030 :parent_issue_id => '4'}
2031 2031
2032 2032 assert_response :success
2033 2033 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', '4'
2034 2034 assert_error_tag :content => /Parent task is invalid/i
2035 2035 end
2036 2036 end
2037 2037
2038 2038 def test_post_create_subissue_with_non_numeric_parent_id_should_not_validate
2039 2039 @request.session[:user_id] = 2
2040 2040
2041 2041 assert_no_difference 'Issue.count' do
2042 2042 post :create, :project_id => 1,
2043 2043 :issue => {:tracker_id => 1,
2044 2044 :subject => 'This is a child issue',
2045 2045 :parent_issue_id => '01ABC'}
2046 2046
2047 2047 assert_response :success
2048 2048 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', '01ABC'
2049 2049 assert_error_tag :content => /Parent task is invalid/i
2050 2050 end
2051 2051 end
2052 2052
2053 2053 def test_post_create_private
2054 2054 @request.session[:user_id] = 2
2055 2055
2056 2056 assert_difference 'Issue.count' do
2057 2057 post :create, :project_id => 1,
2058 2058 :issue => {:tracker_id => 1,
2059 2059 :subject => 'This is a private issue',
2060 2060 :is_private => '1'}
2061 2061 end
2062 2062 issue = Issue.first(:order => 'id DESC')
2063 2063 assert issue.is_private?
2064 2064 end
2065 2065
2066 2066 def test_post_create_private_with_set_own_issues_private_permission
2067 2067 role = Role.find(1)
2068 2068 role.remove_permission! :set_issues_private
2069 2069 role.add_permission! :set_own_issues_private
2070 2070
2071 2071 @request.session[:user_id] = 2
2072 2072
2073 2073 assert_difference 'Issue.count' do
2074 2074 post :create, :project_id => 1,
2075 2075 :issue => {:tracker_id => 1,
2076 2076 :subject => 'This is a private issue',
2077 2077 :is_private => '1'}
2078 2078 end
2079 2079 issue = Issue.first(:order => 'id DESC')
2080 2080 assert issue.is_private?
2081 2081 end
2082 2082
2083 2083 def test_post_create_should_send_a_notification
2084 2084 ActionMailer::Base.deliveries.clear
2085 2085 @request.session[:user_id] = 2
2086 2086 assert_difference 'Issue.count' do
2087 2087 post :create, :project_id => 1,
2088 2088 :issue => {:tracker_id => 3,
2089 2089 :subject => 'This is the test_new issue',
2090 2090 :description => 'This is the description',
2091 2091 :priority_id => 5,
2092 2092 :estimated_hours => '',
2093 2093 :custom_field_values => {'2' => 'Value for field 2'}}
2094 2094 end
2095 2095 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
2096 2096
2097 2097 assert_equal 1, ActionMailer::Base.deliveries.size
2098 2098 end
2099 2099
2100 2100 def test_post_create_should_preserve_fields_values_on_validation_failure
2101 2101 @request.session[:user_id] = 2
2102 2102 post :create, :project_id => 1,
2103 2103 :issue => {:tracker_id => 1,
2104 2104 # empty subject
2105 2105 :subject => '',
2106 2106 :description => 'This is a description',
2107 2107 :priority_id => 6,
2108 2108 :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
2109 2109 assert_response :success
2110 2110 assert_template 'new'
2111 2111
2112 2112 assert_select 'textarea[name=?]', 'issue[description]', :text => 'This is a description'
2113 2113 assert_select 'select[name=?]', 'issue[priority_id]' do
2114 2114 assert_select 'option[value=6][selected=selected]', :text => 'High'
2115 2115 end
2116 2116 # Custom fields
2117 2117 assert_select 'select[name=?]', 'issue[custom_field_values][1]' do
2118 2118 assert_select 'option[value=Oracle][selected=selected]', :text => 'Oracle'
2119 2119 end
2120 2120 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Value for field 2'
2121 2121 end
2122 2122
2123 2123 def test_post_create_with_failure_should_preserve_watchers
2124 2124 assert !User.find(8).member_of?(Project.find(1))
2125 2125
2126 2126 @request.session[:user_id] = 2
2127 2127 post :create, :project_id => 1,
2128 2128 :issue => {:tracker_id => 1,
2129 2129 :watcher_user_ids => ['3', '8']}
2130 2130 assert_response :success
2131 2131 assert_template 'new'
2132 2132
2133 2133 assert_select 'input[name=?][value=2]:not(checked)', 'issue[watcher_user_ids][]'
2134 2134 assert_select 'input[name=?][value=3][checked=checked]', 'issue[watcher_user_ids][]'
2135 2135 assert_select 'input[name=?][value=8][checked=checked]', 'issue[watcher_user_ids][]'
2136 2136 end
2137 2137
2138 2138 def test_post_create_should_ignore_non_safe_attributes
2139 2139 @request.session[:user_id] = 2
2140 2140 assert_nothing_raised do
2141 2141 post :create, :project_id => 1, :issue => { :tracker => "A param can not be a Tracker" }
2142 2142 end
2143 2143 end
2144 2144
2145 2145 def test_post_create_with_attachment
2146 2146 set_tmp_attachments_directory
2147 2147 @request.session[:user_id] = 2
2148 2148
2149 2149 assert_difference 'Issue.count' do
2150 2150 assert_difference 'Attachment.count' do
2151 2151 post :create, :project_id => 1,
2152 2152 :issue => { :tracker_id => '1', :subject => 'With attachment' },
2153 2153 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2154 2154 end
2155 2155 end
2156 2156
2157 2157 issue = Issue.first(:order => 'id DESC')
2158 2158 attachment = Attachment.first(:order => 'id DESC')
2159 2159
2160 2160 assert_equal issue, attachment.container
2161 2161 assert_equal 2, attachment.author_id
2162 2162 assert_equal 'testfile.txt', attachment.filename
2163 2163 assert_equal 'text/plain', attachment.content_type
2164 2164 assert_equal 'test file', attachment.description
2165 2165 assert_equal 59, attachment.filesize
2166 2166 assert File.exists?(attachment.diskfile)
2167 2167 assert_equal 59, File.size(attachment.diskfile)
2168 2168 end
2169 2169
2170 2170 def test_post_create_with_failure_should_save_attachments
2171 2171 set_tmp_attachments_directory
2172 2172 @request.session[:user_id] = 2
2173 2173
2174 2174 assert_no_difference 'Issue.count' do
2175 2175 assert_difference 'Attachment.count' do
2176 2176 post :create, :project_id => 1,
2177 2177 :issue => { :tracker_id => '1', :subject => '' },
2178 2178 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2179 2179 assert_response :success
2180 2180 assert_template 'new'
2181 2181 end
2182 2182 end
2183 2183
2184 2184 attachment = Attachment.first(:order => 'id DESC')
2185 2185 assert_equal 'testfile.txt', attachment.filename
2186 2186 assert File.exists?(attachment.diskfile)
2187 2187 assert_nil attachment.container
2188 2188
2189 2189 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
2190 2190 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
2191 2191 end
2192 2192
2193 2193 def test_post_create_with_failure_should_keep_saved_attachments
2194 2194 set_tmp_attachments_directory
2195 2195 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
2196 2196 @request.session[:user_id] = 2
2197 2197
2198 2198 assert_no_difference 'Issue.count' do
2199 2199 assert_no_difference 'Attachment.count' do
2200 2200 post :create, :project_id => 1,
2201 2201 :issue => { :tracker_id => '1', :subject => '' },
2202 2202 :attachments => {'p0' => {'token' => attachment.token}}
2203 2203 assert_response :success
2204 2204 assert_template 'new'
2205 2205 end
2206 2206 end
2207 2207
2208 2208 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
2209 2209 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
2210 2210 end
2211 2211
2212 2212 def test_post_create_should_attach_saved_attachments
2213 2213 set_tmp_attachments_directory
2214 2214 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
2215 2215 @request.session[:user_id] = 2
2216 2216
2217 2217 assert_difference 'Issue.count' do
2218 2218 assert_no_difference 'Attachment.count' do
2219 2219 post :create, :project_id => 1,
2220 2220 :issue => { :tracker_id => '1', :subject => 'Saved attachments' },
2221 2221 :attachments => {'p0' => {'token' => attachment.token}}
2222 2222 assert_response 302
2223 2223 end
2224 2224 end
2225 2225
2226 2226 issue = Issue.first(:order => 'id DESC')
2227 2227 assert_equal 1, issue.attachments.count
2228 2228
2229 2229 attachment.reload
2230 2230 assert_equal issue, attachment.container
2231 2231 end
2232 2232
2233 2233 context "without workflow privilege" do
2234 2234 setup do
2235 2235 WorkflowTransition.delete_all(["role_id = ?", Role.anonymous.id])
2236 2236 Role.anonymous.add_permission! :add_issues, :add_issue_notes
2237 2237 end
2238 2238
2239 2239 context "#new" do
2240 2240 should "propose default status only" do
2241 2241 get :new, :project_id => 1
2242 2242 assert_response :success
2243 2243 assert_template 'new'
2244 2244 assert_select 'select[name=?]', 'issue[status_id]' do
2245 2245 assert_select 'option', 1
2246 2246 assert_select 'option[value=?]', IssueStatus.default.id.to_s
2247 2247 end
2248 2248 end
2249 2249
2250 2250 should "accept default status" do
2251 2251 assert_difference 'Issue.count' do
2252 2252 post :create, :project_id => 1,
2253 2253 :issue => {:tracker_id => 1,
2254 2254 :subject => 'This is an issue',
2255 2255 :status_id => 1}
2256 2256 end
2257 2257 issue = Issue.last(:order => 'id')
2258 2258 assert_equal IssueStatus.default, issue.status
2259 2259 end
2260 2260
2261 2261 should "ignore unauthorized status" do
2262 2262 assert_difference 'Issue.count' do
2263 2263 post :create, :project_id => 1,
2264 2264 :issue => {:tracker_id => 1,
2265 2265 :subject => 'This is an issue',
2266 2266 :status_id => 3}
2267 2267 end
2268 2268 issue = Issue.last(:order => 'id')
2269 2269 assert_equal IssueStatus.default, issue.status
2270 2270 end
2271 2271 end
2272 2272
2273 2273 context "#update" do
2274 2274 should "ignore status change" do
2275 2275 assert_difference 'Journal.count' do
2276 2276 put :update, :id => 1, :issue => {:status_id => 3, :notes => 'just trying'}
2277 2277 end
2278 2278 assert_equal 1, Issue.find(1).status_id
2279 2279 end
2280 2280
2281 2281 should "ignore attributes changes" do
2282 2282 assert_difference 'Journal.count' do
2283 2283 put :update, :id => 1, :issue => {:subject => 'changed', :assigned_to_id => 2, :notes => 'just trying'}
2284 2284 end
2285 2285 issue = Issue.find(1)
2286 2286 assert_equal "Can't print recipes", issue.subject
2287 2287 assert_nil issue.assigned_to
2288 2288 end
2289 2289 end
2290 2290 end
2291 2291
2292 2292 context "with workflow privilege" do
2293 2293 setup do
2294 2294 WorkflowTransition.delete_all(["role_id = ?", Role.anonymous.id])
2295 2295 WorkflowTransition.create!(:role => Role.anonymous, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3)
2296 2296 WorkflowTransition.create!(:role => Role.anonymous, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4)
2297 2297 Role.anonymous.add_permission! :add_issues, :add_issue_notes
2298 2298 end
2299 2299
2300 2300 context "#update" do
2301 2301 should "accept authorized status" do
2302 2302 assert_difference 'Journal.count' do
2303 2303 put :update, :id => 1, :issue => {:status_id => 3, :notes => 'just trying'}
2304 2304 end
2305 2305 assert_equal 3, Issue.find(1).status_id
2306 2306 end
2307 2307
2308 2308 should "ignore unauthorized status" do
2309 2309 assert_difference 'Journal.count' do
2310 2310 put :update, :id => 1, :issue => {:status_id => 2, :notes => 'just trying'}
2311 2311 end
2312 2312 assert_equal 1, Issue.find(1).status_id
2313 2313 end
2314 2314
2315 2315 should "accept authorized attributes changes" do
2316 2316 assert_difference 'Journal.count' do
2317 2317 put :update, :id => 1, :issue => {:assigned_to_id => 2, :notes => 'just trying'}
2318 2318 end
2319 2319 issue = Issue.find(1)
2320 2320 assert_equal 2, issue.assigned_to_id
2321 2321 end
2322 2322
2323 2323 should "ignore unauthorized attributes changes" do
2324 2324 assert_difference 'Journal.count' do
2325 2325 put :update, :id => 1, :issue => {:subject => 'changed', :notes => 'just trying'}
2326 2326 end
2327 2327 issue = Issue.find(1)
2328 2328 assert_equal "Can't print recipes", issue.subject
2329 2329 end
2330 2330 end
2331 2331
2332 2332 context "and :edit_issues permission" do
2333 2333 setup do
2334 2334 Role.anonymous.add_permission! :add_issues, :edit_issues
2335 2335 end
2336 2336
2337 2337 should "accept authorized status" do
2338 2338 assert_difference 'Journal.count' do
2339 2339 put :update, :id => 1, :issue => {:status_id => 3, :notes => 'just trying'}
2340 2340 end
2341 2341 assert_equal 3, Issue.find(1).status_id
2342 2342 end
2343 2343
2344 2344 should "ignore unauthorized status" do
2345 2345 assert_difference 'Journal.count' do
2346 2346 put :update, :id => 1, :issue => {:status_id => 2, :notes => 'just trying'}
2347 2347 end
2348 2348 assert_equal 1, Issue.find(1).status_id
2349 2349 end
2350 2350
2351 2351 should "accept authorized attributes changes" do
2352 2352 assert_difference 'Journal.count' do
2353 2353 put :update, :id => 1, :issue => {:subject => 'changed', :assigned_to_id => 2, :notes => 'just trying'}
2354 2354 end
2355 2355 issue = Issue.find(1)
2356 2356 assert_equal "changed", issue.subject
2357 2357 assert_equal 2, issue.assigned_to_id
2358 2358 end
2359 2359 end
2360 2360 end
2361 2361
2362 2362 def test_new_as_copy
2363 2363 @request.session[:user_id] = 2
2364 2364 get :new, :project_id => 1, :copy_from => 1
2365 2365
2366 2366 assert_response :success
2367 2367 assert_template 'new'
2368 2368
2369 2369 assert_not_nil assigns(:issue)
2370 2370 orig = Issue.find(1)
2371 2371 assert_equal 1, assigns(:issue).project_id
2372 2372 assert_equal orig.subject, assigns(:issue).subject
2373 2373 assert assigns(:issue).copy?
2374 2374
2375 2375 assert_select 'form[id=issue-form][action=/projects/ecookbook/issues]' do
2376 2376 assert_select 'select[name=?]', 'issue[project_id]' do
2377 2377 assert_select 'option[value=1][selected=selected]', :text => 'eCookbook'
2378 2378 assert_select 'option[value=2]:not([selected])', :text => 'OnlineStore'
2379 2379 end
2380 2380 assert_select 'input[name=copy_from][value=1]'
2381 2381 end
2382 2382
2383 2383 # "New issue" menu item should not link to copy
2384 2384 assert_select '#main-menu a.new-issue[href=/projects/ecookbook/issues/new]'
2385 2385 end
2386 2386
2387 2387 def test_new_as_copy_with_attachments_should_show_copy_attachments_checkbox
2388 2388 @request.session[:user_id] = 2
2389 2389 issue = Issue.find(3)
2390 2390 assert issue.attachments.count > 0
2391 2391 get :new, :project_id => 1, :copy_from => 3
2392 2392
2393 2393 assert_select 'input[name=copy_attachments][type=checkbox][checked=checked][value=1]'
2394 2394 end
2395 2395
2396 2396 def test_new_as_copy_without_attachments_should_not_show_copy_attachments_checkbox
2397 2397 @request.session[:user_id] = 2
2398 2398 issue = Issue.find(3)
2399 2399 issue.attachments.delete_all
2400 2400 get :new, :project_id => 1, :copy_from => 3
2401 2401
2402 2402 assert_select 'input[name=copy_attachments]', 0
2403 2403 end
2404 2404
2405 2405 def test_new_as_copy_with_subtasks_should_show_copy_subtasks_checkbox
2406 2406 @request.session[:user_id] = 2
2407 2407 issue = Issue.generate_with_descendants!
2408 2408 get :new, :project_id => 1, :copy_from => issue.id
2409 2409
2410 2410 assert_select 'input[type=checkbox][name=copy_subtasks][checked=checked][value=1]'
2411 2411 end
2412 2412
2413 2413 def test_new_as_copy_with_invalid_issue_should_respond_with_404
2414 2414 @request.session[:user_id] = 2
2415 2415 get :new, :project_id => 1, :copy_from => 99999
2416 2416 assert_response 404
2417 2417 end
2418 2418
2419 2419 def test_create_as_copy_on_different_project
2420 2420 @request.session[:user_id] = 2
2421 2421 assert_difference 'Issue.count' do
2422 2422 post :create, :project_id => 1, :copy_from => 1,
2423 2423 :issue => {:project_id => '2', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
2424 2424
2425 2425 assert_not_nil assigns(:issue)
2426 2426 assert assigns(:issue).copy?
2427 2427 end
2428 2428 issue = Issue.first(:order => 'id DESC')
2429 2429 assert_redirected_to "/issues/#{issue.id}"
2430 2430
2431 2431 assert_equal 2, issue.project_id
2432 2432 assert_equal 3, issue.tracker_id
2433 2433 assert_equal 'Copy', issue.subject
2434 2434 end
2435 2435
2436 2436 def test_create_as_copy_should_copy_attachments
2437 2437 @request.session[:user_id] = 2
2438 2438 issue = Issue.find(3)
2439 2439 count = issue.attachments.count
2440 2440 assert count > 0
2441 2441
2442 2442 assert_difference 'Issue.count' do
2443 2443 assert_difference 'Attachment.count', count do
2444 2444 assert_no_difference 'Journal.count' do
2445 2445 post :create, :project_id => 1, :copy_from => 3,
2446 2446 :issue => {:project_id => '1', :tracker_id => '3', :status_id => '1', :subject => 'Copy with attachments'},
2447 2447 :copy_attachments => '1'
2448 2448 end
2449 2449 end
2450 2450 end
2451 2451 copy = Issue.first(:order => 'id DESC')
2452 2452 assert_equal count, copy.attachments.count
2453 2453 assert_equal issue.attachments.map(&:filename).sort, copy.attachments.map(&:filename).sort
2454 2454 end
2455 2455
2456 2456 def test_create_as_copy_without_copy_attachments_option_should_not_copy_attachments
2457 2457 @request.session[:user_id] = 2
2458 2458 issue = Issue.find(3)
2459 2459 count = issue.attachments.count
2460 2460 assert count > 0
2461 2461
2462 2462 assert_difference 'Issue.count' do
2463 2463 assert_no_difference 'Attachment.count' do
2464 2464 assert_no_difference 'Journal.count' do
2465 2465 post :create, :project_id => 1, :copy_from => 3,
2466 2466 :issue => {:project_id => '1', :tracker_id => '3', :status_id => '1', :subject => 'Copy with attachments'}
2467 2467 end
2468 2468 end
2469 2469 end
2470 2470 copy = Issue.first(:order => 'id DESC')
2471 2471 assert_equal 0, copy.attachments.count
2472 2472 end
2473 2473
2474 2474 def test_create_as_copy_with_attachments_should_add_new_files
2475 2475 @request.session[:user_id] = 2
2476 2476 issue = Issue.find(3)
2477 2477 count = issue.attachments.count
2478 2478 assert count > 0
2479 2479
2480 2480 assert_difference 'Issue.count' do
2481 2481 assert_difference 'Attachment.count', count + 1 do
2482 2482 assert_no_difference 'Journal.count' do
2483 2483 post :create, :project_id => 1, :copy_from => 3,
2484 2484 :issue => {:project_id => '1', :tracker_id => '3', :status_id => '1', :subject => 'Copy with attachments'},
2485 2485 :copy_attachments => '1',
2486 2486 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2487 2487 end
2488 2488 end
2489 2489 end
2490 2490 copy = Issue.first(:order => 'id DESC')
2491 2491 assert_equal count + 1, copy.attachments.count
2492 2492 end
2493 2493
2494 2494 def test_create_as_copy_should_add_relation_with_copied_issue
2495 2495 @request.session[:user_id] = 2
2496 2496
2497 2497 assert_difference 'Issue.count' do
2498 2498 assert_difference 'IssueRelation.count' do
2499 2499 post :create, :project_id => 1, :copy_from => 1,
2500 2500 :issue => {:project_id => '1', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
2501 2501 end
2502 2502 end
2503 2503 copy = Issue.first(:order => 'id DESC')
2504 2504 assert_equal 1, copy.relations.size
2505 2505 end
2506 2506
2507 2507 def test_create_as_copy_should_copy_subtasks
2508 2508 @request.session[:user_id] = 2
2509 2509 issue = Issue.generate_with_descendants!
2510 2510 count = issue.descendants.count
2511 2511
2512 2512 assert_difference 'Issue.count', count+1 do
2513 2513 assert_no_difference 'Journal.count' do
2514 2514 post :create, :project_id => 1, :copy_from => issue.id,
2515 2515 :issue => {:project_id => '1', :tracker_id => '3', :status_id => '1', :subject => 'Copy with subtasks'},
2516 2516 :copy_subtasks => '1'
2517 2517 end
2518 2518 end
2519 2519 copy = Issue.where(:parent_id => nil).first(:order => 'id DESC')
2520 2520 assert_equal count, copy.descendants.count
2521 2521 assert_equal issue.descendants.map(&:subject).sort, copy.descendants.map(&:subject).sort
2522 2522 end
2523 2523
2524 2524 def test_create_as_copy_without_copy_subtasks_option_should_not_copy_subtasks
2525 2525 @request.session[:user_id] = 2
2526 2526 issue = Issue.generate_with_descendants!
2527 2527
2528 2528 assert_difference 'Issue.count', 1 do
2529 2529 assert_no_difference 'Journal.count' do
2530 2530 post :create, :project_id => 1, :copy_from => 3,
2531 2531 :issue => {:project_id => '1', :tracker_id => '3', :status_id => '1', :subject => 'Copy with subtasks'}
2532 2532 end
2533 2533 end
2534 2534 copy = Issue.where(:parent_id => nil).first(:order => 'id DESC')
2535 2535 assert_equal 0, copy.descendants.count
2536 2536 end
2537 2537
2538 2538 def test_create_as_copy_with_failure
2539 2539 @request.session[:user_id] = 2
2540 2540 post :create, :project_id => 1, :copy_from => 1,
2541 2541 :issue => {:project_id => '2', :tracker_id => '3', :status_id => '1', :subject => ''}
2542 2542
2543 2543 assert_response :success
2544 2544 assert_template 'new'
2545 2545
2546 2546 assert_not_nil assigns(:issue)
2547 2547 assert assigns(:issue).copy?
2548 2548
2549 2549 assert_select 'form#issue-form[action=/projects/ecookbook/issues]' do
2550 2550 assert_select 'select[name=?]', 'issue[project_id]' do
2551 2551 assert_select 'option[value=1]:not([selected])', :text => 'eCookbook'
2552 2552 assert_select 'option[value=2][selected=selected]', :text => 'OnlineStore'
2553 2553 end
2554 2554 assert_select 'input[name=copy_from][value=1]'
2555 2555 end
2556 2556 end
2557 2557
2558 2558 def test_create_as_copy_on_project_without_permission_should_ignore_target_project
2559 2559 @request.session[:user_id] = 2
2560 2560 assert !User.find(2).member_of?(Project.find(4))
2561 2561
2562 2562 assert_difference 'Issue.count' do
2563 2563 post :create, :project_id => 1, :copy_from => 1,
2564 2564 :issue => {:project_id => '4', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
2565 2565 end
2566 2566 issue = Issue.first(:order => 'id DESC')
2567 2567 assert_equal 1, issue.project_id
2568 2568 end
2569 2569
2570 2570 def test_get_edit
2571 2571 @request.session[:user_id] = 2
2572 2572 get :edit, :id => 1
2573 2573 assert_response :success
2574 2574 assert_template 'edit'
2575 2575 assert_not_nil assigns(:issue)
2576 2576 assert_equal Issue.find(1), assigns(:issue)
2577 2577
2578 2578 # Be sure we don't display inactive IssuePriorities
2579 2579 assert ! IssuePriority.find(15).active?
2580 2580 assert_select 'select[name=?]', 'issue[priority_id]' do
2581 2581 assert_select 'option[value=15]', 0
2582 2582 end
2583 2583 end
2584 2584
2585 2585 def test_get_edit_should_display_the_time_entry_form_with_log_time_permission
2586 2586 @request.session[:user_id] = 2
2587 2587 Role.find_by_name('Manager').update_attribute :permissions, [:view_issues, :edit_issues, :log_time]
2588 2588
2589 2589 get :edit, :id => 1
2590 2590 assert_select 'input[name=?]', 'time_entry[hours]'
2591 2591 end
2592 2592
2593 2593 def test_get_edit_should_not_display_the_time_entry_form_without_log_time_permission
2594 2594 @request.session[:user_id] = 2
2595 2595 Role.find_by_name('Manager').remove_permission! :log_time
2596 2596
2597 2597 get :edit, :id => 1
2598 2598 assert_select 'input[name=?]', 'time_entry[hours]', 0
2599 2599 end
2600 2600
2601 2601 def test_get_edit_with_params
2602 2602 @request.session[:user_id] = 2
2603 2603 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 },
2604 2604 :time_entry => { :hours => '2.5', :comments => 'test_get_edit_with_params', :activity_id => 10 }
2605 2605 assert_response :success
2606 2606 assert_template 'edit'
2607 2607
2608 2608 issue = assigns(:issue)
2609 2609 assert_not_nil issue
2610 2610
2611 2611 assert_equal 5, issue.status_id
2612 2612 assert_select 'select[name=?]', 'issue[status_id]' do
2613 2613 assert_select 'option[value=5][selected=selected]', :text => 'Closed'
2614 2614 end
2615 2615
2616 2616 assert_equal 7, issue.priority_id
2617 2617 assert_select 'select[name=?]', 'issue[priority_id]' do
2618 2618 assert_select 'option[value=7][selected=selected]', :text => 'Urgent'
2619 2619 end
2620 2620
2621 2621 assert_select 'input[name=?][value=2.5]', 'time_entry[hours]'
2622 2622 assert_select 'select[name=?]', 'time_entry[activity_id]' do
2623 2623 assert_select 'option[value=10][selected=selected]', :text => 'Development'
2624 2624 end
2625 2625 assert_select 'input[name=?][value=test_get_edit_with_params]', 'time_entry[comments]'
2626 2626 end
2627 2627
2628 2628 def test_get_edit_with_multi_custom_field
2629 2629 field = CustomField.find(1)
2630 2630 field.update_attribute :multiple, true
2631 2631 issue = Issue.find(1)
2632 2632 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
2633 2633 issue.save!
2634 2634
2635 2635 @request.session[:user_id] = 2
2636 2636 get :edit, :id => 1
2637 2637 assert_response :success
2638 2638 assert_template 'edit'
2639 2639
2640 2640 assert_select 'select[name=?][multiple=multiple]', 'issue[custom_field_values][1][]' do
2641 2641 assert_select 'option', 3
2642 2642 assert_select 'option[value=MySQL][selected=selected]'
2643 2643 assert_select 'option[value=Oracle][selected=selected]'
2644 2644 assert_select 'option[value=PostgreSQL]:not([selected])'
2645 2645 end
2646 2646 end
2647 2647
2648 2648 def test_update_form_for_existing_issue
2649 2649 @request.session[:user_id] = 2
2650 2650 xhr :put, :update_form, :project_id => 1,
2651 2651 :id => 1,
2652 2652 :issue => {:tracker_id => 2,
2653 2653 :subject => 'This is the test_new issue',
2654 2654 :description => 'This is the description',
2655 2655 :priority_id => 5}
2656 2656 assert_response :success
2657 2657 assert_equal 'text/javascript', response.content_type
2658 2658 assert_template 'update_form'
2659 2659 assert_template 'form'
2660 2660
2661 2661 issue = assigns(:issue)
2662 2662 assert_kind_of Issue, issue
2663 2663 assert_equal 1, issue.id
2664 2664 assert_equal 1, issue.project_id
2665 2665 assert_equal 2, issue.tracker_id
2666 2666 assert_equal 'This is the test_new issue', issue.subject
2667 2667 end
2668 2668
2669 2669 def test_update_form_for_existing_issue_should_keep_issue_author
2670 2670 @request.session[:user_id] = 3
2671 2671 xhr :put, :update_form, :project_id => 1, :id => 1, :issue => {:subject => 'Changed'}
2672 2672 assert_response :success
2673 2673 assert_equal 'text/javascript', response.content_type
2674 2674
2675 2675 issue = assigns(:issue)
2676 2676 assert_equal User.find(2), issue.author
2677 2677 assert_equal 2, issue.author_id
2678 2678 assert_not_equal User.current, issue.author
2679 2679 end
2680 2680
2681 2681 def test_update_form_for_existing_issue_should_propose_transitions_based_on_initial_status
2682 2682 @request.session[:user_id] = 2
2683 2683 WorkflowTransition.delete_all
2684 2684 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 1)
2685 2685 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 5)
2686 2686 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 5, :new_status_id => 4)
2687 2687
2688 2688 xhr :put, :update_form, :project_id => 1,
2689 2689 :id => 2,
2690 2690 :issue => {:tracker_id => 2,
2691 2691 :status_id => 5,
2692 2692 :subject => 'This is an issue'}
2693 2693
2694 2694 assert_equal 5, assigns(:issue).status_id
2695 2695 assert_equal [1,2,5], assigns(:allowed_statuses).map(&:id).sort
2696 2696 end
2697 2697
2698 2698 def test_update_form_for_existing_issue_with_project_change
2699 2699 @request.session[:user_id] = 2
2700 2700 xhr :put, :update_form, :project_id => 1,
2701 2701 :id => 1,
2702 2702 :issue => {:project_id => 2,
2703 2703 :tracker_id => 2,
2704 2704 :subject => 'This is the test_new issue',
2705 2705 :description => 'This is the description',
2706 2706 :priority_id => 5}
2707 2707 assert_response :success
2708 2708 assert_template 'form'
2709 2709
2710 2710 issue = assigns(:issue)
2711 2711 assert_kind_of Issue, issue
2712 2712 assert_equal 1, issue.id
2713 2713 assert_equal 2, issue.project_id
2714 2714 assert_equal 2, issue.tracker_id
2715 2715 assert_equal 'This is the test_new issue', issue.subject
2716 2716 end
2717 2717
2718 2718 def test_put_update_without_custom_fields_param
2719 2719 @request.session[:user_id] = 2
2720 2720 ActionMailer::Base.deliveries.clear
2721 2721
2722 2722 issue = Issue.find(1)
2723 2723 assert_equal '125', issue.custom_value_for(2).value
2724 2724 old_subject = issue.subject
2725 2725 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
2726 2726
2727 2727 assert_difference('Journal.count') do
2728 2728 assert_difference('JournalDetail.count', 2) do
2729 2729 put :update, :id => 1, :issue => {:subject => new_subject,
2730 2730 :priority_id => '6',
2731 2731 :category_id => '1' # no change
2732 2732 }
2733 2733 end
2734 2734 end
2735 2735 assert_redirected_to :action => 'show', :id => '1'
2736 2736 issue.reload
2737 2737 assert_equal new_subject, issue.subject
2738 2738 # Make sure custom fields were not cleared
2739 2739 assert_equal '125', issue.custom_value_for(2).value
2740 2740
2741 2741 mail = ActionMailer::Base.deliveries.last
2742 2742 assert_not_nil mail
2743 2743 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
2744 2744 assert_mail_body_match "Subject changed from #{old_subject} to #{new_subject}", mail
2745 2745 end
2746 2746
2747 2747 def test_put_update_with_project_change
2748 2748 @request.session[:user_id] = 2
2749 2749 ActionMailer::Base.deliveries.clear
2750 2750
2751 2751 assert_difference('Journal.count') do
2752 2752 assert_difference('JournalDetail.count', 3) do
2753 2753 put :update, :id => 1, :issue => {:project_id => '2',
2754 2754 :tracker_id => '1', # no change
2755 2755 :priority_id => '6',
2756 2756 :category_id => '3'
2757 2757 }
2758 2758 end
2759 2759 end
2760 2760 assert_redirected_to :action => 'show', :id => '1'
2761 2761 issue = Issue.find(1)
2762 2762 assert_equal 2, issue.project_id
2763 2763 assert_equal 1, issue.tracker_id
2764 2764 assert_equal 6, issue.priority_id
2765 2765 assert_equal 3, issue.category_id
2766 2766
2767 2767 mail = ActionMailer::Base.deliveries.last
2768 2768 assert_not_nil mail
2769 2769 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
2770 2770 assert_mail_body_match "Project changed from eCookbook to OnlineStore", mail
2771 2771 end
2772 2772
2773 2773 def test_put_update_with_tracker_change
2774 2774 @request.session[:user_id] = 2
2775 2775 ActionMailer::Base.deliveries.clear
2776 2776
2777 2777 assert_difference('Journal.count') do
2778 2778 assert_difference('JournalDetail.count', 2) do
2779 2779 put :update, :id => 1, :issue => {:project_id => '1',
2780 2780 :tracker_id => '2',
2781 2781 :priority_id => '6'
2782 2782 }
2783 2783 end
2784 2784 end
2785 2785 assert_redirected_to :action => 'show', :id => '1'
2786 2786 issue = Issue.find(1)
2787 2787 assert_equal 1, issue.project_id
2788 2788 assert_equal 2, issue.tracker_id
2789 2789 assert_equal 6, issue.priority_id
2790 2790 assert_equal 1, issue.category_id
2791 2791
2792 2792 mail = ActionMailer::Base.deliveries.last
2793 2793 assert_not_nil mail
2794 2794 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
2795 2795 assert_mail_body_match "Tracker changed from Bug to Feature request", mail
2796 2796 end
2797 2797
2798 2798 def test_put_update_with_custom_field_change
2799 2799 @request.session[:user_id] = 2
2800 2800 issue = Issue.find(1)
2801 2801 assert_equal '125', issue.custom_value_for(2).value
2802 2802
2803 2803 assert_difference('Journal.count') do
2804 2804 assert_difference('JournalDetail.count', 3) do
2805 2805 put :update, :id => 1, :issue => {:subject => 'Custom field change',
2806 2806 :priority_id => '6',
2807 2807 :category_id => '1', # no change
2808 2808 :custom_field_values => { '2' => 'New custom value' }
2809 2809 }
2810 2810 end
2811 2811 end
2812 2812 assert_redirected_to :action => 'show', :id => '1'
2813 2813 issue.reload
2814 2814 assert_equal 'New custom value', issue.custom_value_for(2).value
2815 2815
2816 2816 mail = ActionMailer::Base.deliveries.last
2817 2817 assert_not_nil mail
2818 2818 assert_mail_body_match "Searchable field changed from 125 to New custom value", mail
2819 2819 end
2820 2820
2821 2821 def test_put_update_with_multi_custom_field_change
2822 2822 field = CustomField.find(1)
2823 2823 field.update_attribute :multiple, true
2824 2824 issue = Issue.find(1)
2825 2825 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
2826 2826 issue.save!
2827 2827
2828 2828 @request.session[:user_id] = 2
2829 2829 assert_difference('Journal.count') do
2830 2830 assert_difference('JournalDetail.count', 3) do
2831 2831 put :update, :id => 1,
2832 2832 :issue => {
2833 2833 :subject => 'Custom field change',
2834 2834 :custom_field_values => { '1' => ['', 'Oracle', 'PostgreSQL'] }
2835 2835 }
2836 2836 end
2837 2837 end
2838 2838 assert_redirected_to :action => 'show', :id => '1'
2839 2839 assert_equal ['Oracle', 'PostgreSQL'], Issue.find(1).custom_field_value(1).sort
2840 2840 end
2841 2841
2842 2842 def test_put_update_with_status_and_assignee_change
2843 2843 issue = Issue.find(1)
2844 2844 assert_equal 1, issue.status_id
2845 2845 @request.session[:user_id] = 2
2846 2846 assert_difference('TimeEntry.count', 0) do
2847 2847 put :update,
2848 2848 :id => 1,
2849 2849 :issue => { :status_id => 2, :assigned_to_id => 3, :notes => 'Assigned to dlopper' },
2850 2850 :time_entry => { :hours => '', :comments => '', :activity_id => TimeEntryActivity.first }
2851 2851 end
2852 2852 assert_redirected_to :action => 'show', :id => '1'
2853 2853 issue.reload
2854 2854 assert_equal 2, issue.status_id
2855 2855 j = Journal.order('id DESC').first
2856 2856 assert_equal 'Assigned to dlopper', j.notes
2857 2857 assert_equal 2, j.details.size
2858 2858
2859 2859 mail = ActionMailer::Base.deliveries.last
2860 2860 assert_mail_body_match "Status changed from New to Assigned", mail
2861 2861 # subject should contain the new status
2862 2862 assert mail.subject.include?("(#{ IssueStatus.find(2).name })")
2863 2863 end
2864 2864
2865 2865 def test_put_update_with_note_only
2866 2866 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
2867 2867 # anonymous user
2868 2868 put :update,
2869 2869 :id => 1,
2870 2870 :issue => { :notes => notes }
2871 2871 assert_redirected_to :action => 'show', :id => '1'
2872 2872 j = Journal.order('id DESC').first
2873 2873 assert_equal notes, j.notes
2874 2874 assert_equal 0, j.details.size
2875 2875 assert_equal User.anonymous, j.user
2876 2876
2877 2877 mail = ActionMailer::Base.deliveries.last
2878 2878 assert_mail_body_match notes, mail
2879 2879 end
2880 2880
2881 2881 def test_put_update_with_private_note_only
2882 2882 notes = 'Private note'
2883 2883 @request.session[:user_id] = 2
2884 2884
2885 2885 assert_difference 'Journal.count' do
2886 2886 put :update, :id => 1, :issue => {:notes => notes, :private_notes => '1'}
2887 2887 assert_redirected_to :action => 'show', :id => '1'
2888 2888 end
2889 2889
2890 2890 j = Journal.order('id DESC').first
2891 2891 assert_equal notes, j.notes
2892 2892 assert_equal true, j.private_notes
2893 2893 end
2894 2894
2895 2895 def test_put_update_with_private_note_and_changes
2896 2896 notes = 'Private note'
2897 2897 @request.session[:user_id] = 2
2898 2898
2899 2899 assert_difference 'Journal.count', 2 do
2900 2900 put :update, :id => 1, :issue => {:subject => 'New subject', :notes => notes, :private_notes => '1'}
2901 2901 assert_redirected_to :action => 'show', :id => '1'
2902 2902 end
2903 2903
2904 2904 j = Journal.order('id DESC').first
2905 2905 assert_equal notes, j.notes
2906 2906 assert_equal true, j.private_notes
2907 2907 assert_equal 0, j.details.count
2908 2908
2909 2909 j = Journal.order('id DESC').offset(1).first
2910 2910 assert_nil j.notes
2911 2911 assert_equal false, j.private_notes
2912 2912 assert_equal 1, j.details.count
2913 2913 end
2914 2914
2915 2915 def test_put_update_with_note_and_spent_time
2916 2916 @request.session[:user_id] = 2
2917 2917 spent_hours_before = Issue.find(1).spent_hours
2918 2918 assert_difference('TimeEntry.count') do
2919 2919 put :update,
2920 2920 :id => 1,
2921 2921 :issue => { :notes => '2.5 hours added' },
2922 2922 :time_entry => { :hours => '2.5', :comments => 'test_put_update_with_note_and_spent_time', :activity_id => TimeEntryActivity.first.id }
2923 2923 end
2924 2924 assert_redirected_to :action => 'show', :id => '1'
2925 2925
2926 2926 issue = Issue.find(1)
2927 2927
2928 2928 j = Journal.order('id DESC').first
2929 2929 assert_equal '2.5 hours added', j.notes
2930 2930 assert_equal 0, j.details.size
2931 2931
2932 2932 t = issue.time_entries.find_by_comments('test_put_update_with_note_and_spent_time')
2933 2933 assert_not_nil t
2934 2934 assert_equal 2.5, t.hours
2935 2935 assert_equal spent_hours_before + 2.5, issue.spent_hours
2936 2936 end
2937 2937
2938 2938 def test_put_update_should_preserve_parent_issue_even_if_not_visible
2939 2939 parent = Issue.generate!(:project_id => 1, :is_private => true)
2940 2940 issue = Issue.generate!(:parent_issue_id => parent.id)
2941 2941 assert !parent.visible?(User.find(3))
2942 2942 @request.session[:user_id] = 3
2943 2943
2944 2944 get :edit, :id => issue.id
2945 2945 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', parent.id.to_s
2946 2946
2947 2947 put :update, :id => issue.id, :issue => {:subject => 'New subject', :parent_issue_id => parent.id.to_s}
2948 2948 assert_response 302
2949 2949 assert_equal parent, issue.parent
2950 2950 end
2951 2951
2952 2952 def test_put_update_with_attachment_only
2953 2953 set_tmp_attachments_directory
2954 2954
2955 2955 # Delete all fixtured journals, a race condition can occur causing the wrong
2956 2956 # journal to get fetched in the next find.
2957 2957 Journal.delete_all
2958 2958
2959 2959 # anonymous user
2960 2960 assert_difference 'Attachment.count' do
2961 2961 put :update, :id => 1,
2962 2962 :issue => {:notes => ''},
2963 2963 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2964 2964 end
2965 2965
2966 2966 assert_redirected_to :action => 'show', :id => '1'
2967 2967 j = Issue.find(1).journals.reorder('id DESC').first
2968 2968 assert j.notes.blank?
2969 2969 assert_equal 1, j.details.size
2970 2970 assert_equal 'testfile.txt', j.details.first.value
2971 2971 assert_equal User.anonymous, j.user
2972 2972
2973 2973 attachment = Attachment.first(:order => 'id DESC')
2974 2974 assert_equal Issue.find(1), attachment.container
2975 2975 assert_equal User.anonymous, attachment.author
2976 2976 assert_equal 'testfile.txt', attachment.filename
2977 2977 assert_equal 'text/plain', attachment.content_type
2978 2978 assert_equal 'test file', attachment.description
2979 2979 assert_equal 59, attachment.filesize
2980 2980 assert File.exists?(attachment.diskfile)
2981 2981 assert_equal 59, File.size(attachment.diskfile)
2982 2982
2983 2983 mail = ActionMailer::Base.deliveries.last
2984 2984 assert_mail_body_match 'testfile.txt', mail
2985 2985 end
2986 2986
2987 2987 def test_put_update_with_failure_should_save_attachments
2988 2988 set_tmp_attachments_directory
2989 2989 @request.session[:user_id] = 2
2990 2990
2991 2991 assert_no_difference 'Journal.count' do
2992 2992 assert_difference 'Attachment.count' do
2993 2993 put :update, :id => 1,
2994 2994 :issue => { :subject => '' },
2995 2995 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2996 2996 assert_response :success
2997 2997 assert_template 'edit'
2998 2998 end
2999 2999 end
3000 3000
3001 3001 attachment = Attachment.first(:order => 'id DESC')
3002 3002 assert_equal 'testfile.txt', attachment.filename
3003 3003 assert File.exists?(attachment.diskfile)
3004 3004 assert_nil attachment.container
3005 3005
3006 3006 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
3007 3007 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
3008 3008 end
3009 3009
3010 3010 def test_put_update_with_failure_should_keep_saved_attachments
3011 3011 set_tmp_attachments_directory
3012 3012 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
3013 3013 @request.session[:user_id] = 2
3014 3014
3015 3015 assert_no_difference 'Journal.count' do
3016 3016 assert_no_difference 'Attachment.count' do
3017 3017 put :update, :id => 1,
3018 3018 :issue => { :subject => '' },
3019 3019 :attachments => {'p0' => {'token' => attachment.token}}
3020 3020 assert_response :success
3021 3021 assert_template 'edit'
3022 3022 end
3023 3023 end
3024 3024
3025 3025 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
3026 3026 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
3027 3027 end
3028 3028
3029 3029 def test_put_update_should_attach_saved_attachments
3030 3030 set_tmp_attachments_directory
3031 3031 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
3032 3032 @request.session[:user_id] = 2
3033 3033
3034 3034 assert_difference 'Journal.count' do
3035 3035 assert_difference 'JournalDetail.count' do
3036 3036 assert_no_difference 'Attachment.count' do
3037 3037 put :update, :id => 1,
3038 3038 :issue => {:notes => 'Attachment added'},
3039 3039 :attachments => {'p0' => {'token' => attachment.token}}
3040 3040 assert_redirected_to '/issues/1'
3041 3041 end
3042 3042 end
3043 3043 end
3044 3044
3045 3045 attachment.reload
3046 3046 assert_equal Issue.find(1), attachment.container
3047 3047
3048 3048 journal = Journal.first(:order => 'id DESC')
3049 3049 assert_equal 1, journal.details.size
3050 3050 assert_equal 'testfile.txt', journal.details.first.value
3051 3051 end
3052 3052
3053 3053 def test_put_update_with_attachment_that_fails_to_save
3054 3054 set_tmp_attachments_directory
3055 3055
3056 3056 # Delete all fixtured journals, a race condition can occur causing the wrong
3057 3057 # journal to get fetched in the next find.
3058 3058 Journal.delete_all
3059 3059
3060 3060 # Mock out the unsaved attachment
3061 3061 Attachment.any_instance.stubs(:create).returns(Attachment.new)
3062 3062
3063 3063 # anonymous user
3064 3064 put :update,
3065 3065 :id => 1,
3066 3066 :issue => {:notes => ''},
3067 3067 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
3068 3068 assert_redirected_to :action => 'show', :id => '1'
3069 3069 assert_equal '1 file(s) could not be saved.', flash[:warning]
3070 3070 end
3071 3071
3072 3072 def test_put_update_with_no_change
3073 3073 issue = Issue.find(1)
3074 3074 issue.journals.clear
3075 3075 ActionMailer::Base.deliveries.clear
3076 3076
3077 3077 put :update,
3078 3078 :id => 1,
3079 3079 :issue => {:notes => ''}
3080 3080 assert_redirected_to :action => 'show', :id => '1'
3081 3081
3082 3082 issue.reload
3083 3083 assert issue.journals.empty?
3084 3084 # No email should be sent
3085 3085 assert ActionMailer::Base.deliveries.empty?
3086 3086 end
3087 3087
3088 3088 def test_put_update_should_send_a_notification
3089 3089 @request.session[:user_id] = 2
3090 3090 ActionMailer::Base.deliveries.clear
3091 3091 issue = Issue.find(1)
3092 3092 old_subject = issue.subject
3093 3093 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
3094 3094
3095 3095 put :update, :id => 1, :issue => {:subject => new_subject,
3096 3096 :priority_id => '6',
3097 3097 :category_id => '1' # no change
3098 3098 }
3099 3099 assert_equal 1, ActionMailer::Base.deliveries.size
3100 3100 end
3101 3101
3102 3102 def test_put_update_with_invalid_spent_time_hours_only
3103 3103 @request.session[:user_id] = 2
3104 3104 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
3105 3105
3106 3106 assert_no_difference('Journal.count') do
3107 3107 put :update,
3108 3108 :id => 1,
3109 3109 :issue => {:notes => notes},
3110 3110 :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
3111 3111 end
3112 3112 assert_response :success
3113 3113 assert_template 'edit'
3114 3114
3115 3115 assert_error_tag :descendant => {:content => /Activity can&#x27;t be blank/}
3116 3116 assert_select 'textarea[name=?]', 'issue[notes]', :text => notes
3117 3117 assert_select 'input[name=?][value=?]', 'time_entry[hours]', '2z'
3118 3118 end
3119 3119
3120 3120 def test_put_update_with_invalid_spent_time_comments_only
3121 3121 @request.session[:user_id] = 2
3122 3122 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
3123 3123
3124 3124 assert_no_difference('Journal.count') do
3125 3125 put :update,
3126 3126 :id => 1,
3127 3127 :issue => {:notes => notes},
3128 3128 :time_entry => {"comments"=>"this is my comment", "activity_id"=>"", "hours"=>""}
3129 3129 end
3130 3130 assert_response :success
3131 3131 assert_template 'edit'
3132 3132
3133 3133 assert_error_tag :descendant => {:content => /Activity can&#x27;t be blank/}
3134 3134 assert_error_tag :descendant => {:content => /Hours can&#x27;t be blank/}
3135 3135 assert_select 'textarea[name=?]', 'issue[notes]', :text => notes
3136 3136 assert_select 'input[name=?][value=?]', 'time_entry[comments]', 'this is my comment'
3137 3137 end
3138 3138
3139 3139 def test_put_update_should_allow_fixed_version_to_be_set_to_a_subproject
3140 3140 issue = Issue.find(2)
3141 3141 @request.session[:user_id] = 2
3142 3142
3143 3143 put :update,
3144 3144 :id => issue.id,
3145 3145 :issue => {
3146 3146 :fixed_version_id => 4
3147 3147 }
3148 3148
3149 3149 assert_response :redirect
3150 3150 issue.reload
3151 3151 assert_equal 4, issue.fixed_version_id
3152 3152 assert_not_equal issue.project_id, issue.fixed_version.project_id
3153 3153 end
3154 3154
3155 3155 def test_put_update_should_redirect_back_using_the_back_url_parameter
3156 3156 issue = Issue.find(2)
3157 3157 @request.session[:user_id] = 2
3158 3158
3159 3159 put :update,
3160 3160 :id => issue.id,
3161 3161 :issue => {
3162 3162 :fixed_version_id => 4
3163 3163 },
3164 3164 :back_url => '/issues'
3165 3165
3166 3166 assert_response :redirect
3167 3167 assert_redirected_to '/issues'
3168 3168 end
3169 3169
3170 3170 def test_put_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
3171 3171 issue = Issue.find(2)
3172 3172 @request.session[:user_id] = 2
3173 3173
3174 3174 put :update,
3175 3175 :id => issue.id,
3176 3176 :issue => {
3177 3177 :fixed_version_id => 4
3178 3178 },
3179 3179 :back_url => 'http://google.com'
3180 3180
3181 3181 assert_response :redirect
3182 3182 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue.id
3183 3183 end
3184 3184
3185 3185 def test_get_bulk_edit
3186 3186 @request.session[:user_id] = 2
3187 3187 get :bulk_edit, :ids => [1, 2]
3188 3188 assert_response :success
3189 3189 assert_template 'bulk_edit'
3190 3190
3191 3191 assert_select 'ul#bulk-selection' do
3192 3192 assert_select 'li', 2
3193 3193 assert_select 'li a', :text => 'Bug #1'
3194 3194 end
3195 3195
3196 3196 assert_select 'form#bulk_edit_form[action=?]', '/issues/bulk_update' do
3197 3197 assert_select 'input[name=?]', 'ids[]', 2
3198 3198 assert_select 'input[name=?][value=1][type=hidden]', 'ids[]'
3199 3199
3200 3200 assert_select 'select[name=?]', 'issue[project_id]'
3201 3201 assert_select 'input[name=?]', 'issue[parent_issue_id]'
3202 3202
3203 3203 # Project specific custom field, date type
3204 3204 field = CustomField.find(9)
3205 3205 assert !field.is_for_all?
3206 3206 assert_equal 'date', field.field_format
3207 3207 assert_select 'input[name=?]', 'issue[custom_field_values][9]'
3208 3208
3209 3209 # System wide custom field
3210 3210 assert CustomField.find(1).is_for_all?
3211 3211 assert_select 'select[name=?]', 'issue[custom_field_values][1]'
3212 3212
3213 3213 # Be sure we don't display inactive IssuePriorities
3214 3214 assert ! IssuePriority.find(15).active?
3215 3215 assert_select 'select[name=?]', 'issue[priority_id]' do
3216 3216 assert_select 'option[value=15]', 0
3217 3217 end
3218 3218 end
3219 3219 end
3220 3220
3221 3221 def test_get_bulk_edit_on_different_projects
3222 3222 @request.session[:user_id] = 2
3223 3223 get :bulk_edit, :ids => [1, 2, 6]
3224 3224 assert_response :success
3225 3225 assert_template 'bulk_edit'
3226 3226
3227 3227 # Can not set issues from different projects as children of an issue
3228 3228 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
3229 3229
3230 3230 # Project specific custom field, date type
3231 3231 field = CustomField.find(9)
3232 3232 assert !field.is_for_all?
3233 3233 assert !field.project_ids.include?(Issue.find(6).project_id)
3234 3234 assert_select 'input[name=?]', 'issue[custom_field_values][9]', 0
3235 3235 end
3236 3236
3237 3237 def test_get_bulk_edit_with_user_custom_field
3238 3238 field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user', :is_for_all => true)
3239 3239
3240 3240 @request.session[:user_id] = 2
3241 3241 get :bulk_edit, :ids => [1, 2]
3242 3242 assert_response :success
3243 3243 assert_template 'bulk_edit'
3244 3244
3245 3245 assert_select 'select.user_cf[name=?]', "issue[custom_field_values][#{field.id}]" do
3246 3246 assert_select 'option', Project.find(1).users.count + 2 # "no change" + "none" options
3247 3247 end
3248 3248 end
3249 3249
3250 3250 def test_get_bulk_edit_with_version_custom_field
3251 3251 field = IssueCustomField.create!(:name => 'Affected version', :field_format => 'version', :is_for_all => true)
3252 3252
3253 3253 @request.session[:user_id] = 2
3254 3254 get :bulk_edit, :ids => [1, 2]
3255 3255 assert_response :success
3256 3256 assert_template 'bulk_edit'
3257 3257
3258 3258 assert_select 'select.version_cf[name=?]', "issue[custom_field_values][#{field.id}]" do
3259 3259 assert_select 'option', Project.find(1).shared_versions.count + 2 # "no change" + "none" options
3260 3260 end
3261 3261 end
3262 3262
3263 3263 def test_get_bulk_edit_with_multi_custom_field
3264 3264 field = CustomField.find(1)
3265 3265 field.update_attribute :multiple, true
3266 3266
3267 3267 @request.session[:user_id] = 2
3268 3268 get :bulk_edit, :ids => [1, 2]
3269 3269 assert_response :success
3270 3270 assert_template 'bulk_edit'
3271 3271
3272 3272 assert_select 'select[name=?]', 'issue[custom_field_values][1][]' do
3273 3273 assert_select 'option', field.possible_values.size + 1 # "none" options
3274 3274 end
3275 3275 end
3276 3276
3277 3277 def test_bulk_edit_should_only_propose_statuses_allowed_for_all_issues
3278 3278 WorkflowTransition.delete_all
3279 3279 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 1)
3280 3280 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3)
3281 3281 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4)
3282 3282 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 1)
3283 3283 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 3)
3284 3284 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 5)
3285 3285 @request.session[:user_id] = 2
3286 3286 get :bulk_edit, :ids => [1, 2]
3287 3287
3288 3288 assert_response :success
3289 3289 statuses = assigns(:available_statuses)
3290 3290 assert_not_nil statuses
3291 3291 assert_equal [1, 3], statuses.map(&:id).sort
3292 3292
3293 3293 assert_select 'select[name=?]', 'issue[status_id]' do
3294 3294 assert_select 'option', 3 # 2 statuses + "no change" option
3295 3295 end
3296 3296 end
3297 3297
3298 3298 def test_bulk_edit_should_propose_target_project_open_shared_versions
3299 3299 @request.session[:user_id] = 2
3300 3300 post :bulk_edit, :ids => [1, 2, 6], :issue => {:project_id => 1}
3301 3301 assert_response :success
3302 3302 assert_template 'bulk_edit'
3303 3303 assert_equal Project.find(1).shared_versions.open.all.sort, assigns(:versions).sort
3304 3304
3305 3305 assert_select 'select[name=?]', 'issue[fixed_version_id]' do
3306 3306 assert_select 'option', :text => '2.0'
3307 3307 end
3308 3308 end
3309 3309
3310 3310 def test_bulk_edit_should_propose_target_project_categories
3311 3311 @request.session[:user_id] = 2
3312 3312 post :bulk_edit, :ids => [1, 2, 6], :issue => {:project_id => 1}
3313 3313 assert_response :success
3314 3314 assert_template 'bulk_edit'
3315 3315 assert_equal Project.find(1).issue_categories.sort, assigns(:categories).sort
3316 3316
3317 3317 assert_select 'select[name=?]', 'issue[category_id]' do
3318 3318 assert_select 'option', :text => 'Recipes'
3319 3319 end
3320 3320 end
3321 3321
3322 3322 def test_bulk_update
3323 3323 @request.session[:user_id] = 2
3324 3324 # update issues priority
3325 3325 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
3326 3326 :issue => {:priority_id => 7,
3327 3327 :assigned_to_id => '',
3328 3328 :custom_field_values => {'2' => ''}}
3329 3329
3330 3330 assert_response 302
3331 3331 # check that the issues were updated
3332 3332 assert_equal [7, 7], Issue.find_all_by_id([1, 2]).collect {|i| i.priority.id}
3333 3333
3334 3334 issue = Issue.find(1)
3335 3335 journal = issue.journals.reorder('created_on DESC').first
3336 3336 assert_equal '125', issue.custom_value_for(2).value
3337 3337 assert_equal 'Bulk editing', journal.notes
3338 3338 assert_equal 1, journal.details.size
3339 3339 end
3340 3340
3341 3341 def test_bulk_update_with_group_assignee
3342 3342 group = Group.find(11)
3343 3343 project = Project.find(1)
3344 3344 project.members << Member.new(:principal => group, :roles => [Role.givable.first])
3345 3345
3346 3346 @request.session[:user_id] = 2
3347 3347 # update issues assignee
3348 3348 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
3349 3349 :issue => {:priority_id => '',
3350 3350 :assigned_to_id => group.id,
3351 3351 :custom_field_values => {'2' => ''}}
3352 3352
3353 3353 assert_response 302
3354 3354 assert_equal [group, group], Issue.find_all_by_id([1, 2]).collect {|i| i.assigned_to}
3355 3355 end
3356 3356
3357 3357 def test_bulk_update_on_different_projects
3358 3358 @request.session[:user_id] = 2
3359 3359 # update issues priority
3360 3360 post :bulk_update, :ids => [1, 2, 6], :notes => 'Bulk editing',
3361 3361 :issue => {:priority_id => 7,
3362 3362 :assigned_to_id => '',
3363 3363 :custom_field_values => {'2' => ''}}
3364 3364
3365 3365 assert_response 302
3366 3366 # check that the issues were updated
3367 3367 assert_equal [7, 7, 7], Issue.find([1,2,6]).map(&:priority_id)
3368 3368
3369 3369 issue = Issue.find(1)
3370 3370 journal = issue.journals.reorder('created_on DESC').first
3371 3371 assert_equal '125', issue.custom_value_for(2).value
3372 3372 assert_equal 'Bulk editing', journal.notes
3373 3373 assert_equal 1, journal.details.size
3374 3374 end
3375 3375
3376 3376 def test_bulk_update_on_different_projects_without_rights
3377 3377 @request.session[:user_id] = 3
3378 3378 user = User.find(3)
3379 3379 action = { :controller => "issues", :action => "bulk_update" }
3380 3380 assert user.allowed_to?(action, Issue.find(1).project)
3381 3381 assert ! user.allowed_to?(action, Issue.find(6).project)
3382 3382 post :bulk_update, :ids => [1, 6], :notes => 'Bulk should fail',
3383 3383 :issue => {:priority_id => 7,
3384 3384 :assigned_to_id => '',
3385 3385 :custom_field_values => {'2' => ''}}
3386 3386 assert_response 403
3387 3387 assert_not_equal "Bulk should fail", Journal.last.notes
3388 3388 end
3389 3389
3390 3390 def test_bullk_update_should_send_a_notification
3391 3391 @request.session[:user_id] = 2
3392 3392 ActionMailer::Base.deliveries.clear
3393 3393 post(:bulk_update,
3394 3394 {
3395 3395 :ids => [1, 2],
3396 3396 :notes => 'Bulk editing',
3397 3397 :issue => {
3398 3398 :priority_id => 7,
3399 3399 :assigned_to_id => '',
3400 3400 :custom_field_values => {'2' => ''}
3401 3401 }
3402 3402 })
3403 3403
3404 3404 assert_response 302
3405 3405 assert_equal 2, ActionMailer::Base.deliveries.size
3406 3406 end
3407 3407
3408 3408 def test_bulk_update_project
3409 3409 @request.session[:user_id] = 2
3410 3410 post :bulk_update, :ids => [1, 2], :issue => {:project_id => '2'}
3411 3411 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3412 3412 # Issues moved to project 2
3413 3413 assert_equal 2, Issue.find(1).project_id
3414 3414 assert_equal 2, Issue.find(2).project_id
3415 3415 # No tracker change
3416 3416 assert_equal 1, Issue.find(1).tracker_id
3417 3417 assert_equal 2, Issue.find(2).tracker_id
3418 3418 end
3419 3419
3420 3420 def test_bulk_update_project_on_single_issue_should_follow_when_needed
3421 3421 @request.session[:user_id] = 2
3422 3422 post :bulk_update, :id => 1, :issue => {:project_id => '2'}, :follow => '1'
3423 3423 assert_redirected_to '/issues/1'
3424 3424 end
3425 3425
3426 3426 def test_bulk_update_project_on_multiple_issues_should_follow_when_needed
3427 3427 @request.session[:user_id] = 2
3428 3428 post :bulk_update, :id => [1, 2], :issue => {:project_id => '2'}, :follow => '1'
3429 3429 assert_redirected_to '/projects/onlinestore/issues'
3430 3430 end
3431 3431
3432 3432 def test_bulk_update_tracker
3433 3433 @request.session[:user_id] = 2
3434 3434 post :bulk_update, :ids => [1, 2], :issue => {:tracker_id => '2'}
3435 3435 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3436 3436 assert_equal 2, Issue.find(1).tracker_id
3437 3437 assert_equal 2, Issue.find(2).tracker_id
3438 3438 end
3439 3439
3440 3440 def test_bulk_update_status
3441 3441 @request.session[:user_id] = 2
3442 3442 # update issues priority
3443 3443 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing status',
3444 3444 :issue => {:priority_id => '',
3445 3445 :assigned_to_id => '',
3446 3446 :status_id => '5'}
3447 3447
3448 3448 assert_response 302
3449 3449 issue = Issue.find(1)
3450 3450 assert issue.closed?
3451 3451 end
3452 3452
3453 3453 def test_bulk_update_priority
3454 3454 @request.session[:user_id] = 2
3455 3455 post :bulk_update, :ids => [1, 2], :issue => {:priority_id => 6}
3456 3456
3457 3457 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3458 3458 assert_equal 6, Issue.find(1).priority_id
3459 3459 assert_equal 6, Issue.find(2).priority_id
3460 3460 end
3461 3461
3462 3462 def test_bulk_update_with_notes
3463 3463 @request.session[:user_id] = 2
3464 3464 post :bulk_update, :ids => [1, 2], :notes => 'Moving two issues'
3465 3465
3466 3466 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3467 3467 assert_equal 'Moving two issues', Issue.find(1).journals.sort_by(&:id).last.notes
3468 3468 assert_equal 'Moving two issues', Issue.find(2).journals.sort_by(&:id).last.notes
3469 3469 end
3470 3470
3471 3471 def test_bulk_update_parent_id
3472 3472 @request.session[:user_id] = 2
3473 3473 post :bulk_update, :ids => [1, 3],
3474 3474 :notes => 'Bulk editing parent',
3475 3475 :issue => {:priority_id => '', :assigned_to_id => '', :status_id => '', :parent_issue_id => '2'}
3476 3476
3477 3477 assert_response 302
3478 3478 parent = Issue.find(2)
3479 3479 assert_equal parent.id, Issue.find(1).parent_id
3480 3480 assert_equal parent.id, Issue.find(3).parent_id
3481 3481 assert_equal [1, 3], parent.children.collect(&:id).sort
3482 3482 end
3483 3483
3484 3484 def test_bulk_update_custom_field
3485 3485 @request.session[:user_id] = 2
3486 3486 # update issues priority
3487 3487 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing custom field',
3488 3488 :issue => {:priority_id => '',
3489 3489 :assigned_to_id => '',
3490 3490 :custom_field_values => {'2' => '777'}}
3491 3491
3492 3492 assert_response 302
3493 3493
3494 3494 issue = Issue.find(1)
3495 3495 journal = issue.journals.reorder('created_on DESC').first
3496 3496 assert_equal '777', issue.custom_value_for(2).value
3497 3497 assert_equal 1, journal.details.size
3498 3498 assert_equal '125', journal.details.first.old_value
3499 3499 assert_equal '777', journal.details.first.value
3500 3500 end
3501 3501
3502 3502 def test_bulk_update_custom_field_to_blank
3503 3503 @request.session[:user_id] = 2
3504 3504 post :bulk_update, :ids => [1, 3], :notes => 'Bulk editing custom field',
3505 3505 :issue => {:priority_id => '',
3506 3506 :assigned_to_id => '',
3507 3507 :custom_field_values => {'1' => '__none__'}}
3508 3508 assert_response 302
3509 3509 assert_equal '', Issue.find(1).custom_field_value(1)
3510 3510 assert_equal '', Issue.find(3).custom_field_value(1)
3511 3511 end
3512 3512
3513 3513 def test_bulk_update_multi_custom_field
3514 3514 field = CustomField.find(1)
3515 3515 field.update_attribute :multiple, true
3516 3516
3517 3517 @request.session[:user_id] = 2
3518 3518 post :bulk_update, :ids => [1, 2, 3], :notes => 'Bulk editing multi custom field',
3519 3519 :issue => {:priority_id => '',
3520 3520 :assigned_to_id => '',
3521 3521 :custom_field_values => {'1' => ['MySQL', 'Oracle']}}
3522 3522
3523 3523 assert_response 302
3524 3524
3525 3525 assert_equal ['MySQL', 'Oracle'], Issue.find(1).custom_field_value(1).sort
3526 3526 assert_equal ['MySQL', 'Oracle'], Issue.find(3).custom_field_value(1).sort
3527 3527 # the custom field is not associated with the issue tracker
3528 3528 assert_nil Issue.find(2).custom_field_value(1)
3529 3529 end
3530 3530
3531 3531 def test_bulk_update_multi_custom_field_to_blank
3532 3532 field = CustomField.find(1)
3533 3533 field.update_attribute :multiple, true
3534 3534
3535 3535 @request.session[:user_id] = 2
3536 3536 post :bulk_update, :ids => [1, 3], :notes => 'Bulk editing multi custom field',
3537 3537 :issue => {:priority_id => '',
3538 3538 :assigned_to_id => '',
3539 3539 :custom_field_values => {'1' => ['__none__']}}
3540 3540 assert_response 302
3541 3541 assert_equal [''], Issue.find(1).custom_field_value(1)
3542 3542 assert_equal [''], Issue.find(3).custom_field_value(1)
3543 3543 end
3544 3544
3545 3545 def test_bulk_update_unassign
3546 3546 assert_not_nil Issue.find(2).assigned_to
3547 3547 @request.session[:user_id] = 2
3548 3548 # unassign issues
3549 3549 post :bulk_update, :ids => [1, 2], :notes => 'Bulk unassigning', :issue => {:assigned_to_id => 'none'}
3550 3550 assert_response 302
3551 3551 # check that the issues were updated
3552 3552 assert_nil Issue.find(2).assigned_to
3553 3553 end
3554 3554
3555 3555 def test_post_bulk_update_should_allow_fixed_version_to_be_set_to_a_subproject
3556 3556 @request.session[:user_id] = 2
3557 3557
3558 3558 post :bulk_update, :ids => [1,2], :issue => {:fixed_version_id => 4}
3559 3559
3560 3560 assert_response :redirect
3561 3561 issues = Issue.find([1,2])
3562 3562 issues.each do |issue|
3563 3563 assert_equal 4, issue.fixed_version_id
3564 3564 assert_not_equal issue.project_id, issue.fixed_version.project_id
3565 3565 end
3566 3566 end
3567 3567
3568 3568 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
3569 3569 @request.session[:user_id] = 2
3570 3570 post :bulk_update, :ids => [1,2], :back_url => '/issues'
3571 3571
3572 3572 assert_response :redirect
3573 3573 assert_redirected_to '/issues'
3574 3574 end
3575 3575
3576 3576 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
3577 3577 @request.session[:user_id] = 2
3578 3578 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
3579 3579
3580 3580 assert_response :redirect
3581 3581 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => Project.find(1).identifier
3582 3582 end
3583 3583
3584 3584 def test_bulk_update_with_failure_should_set_flash
3585 3585 @request.session[:user_id] = 2
3586 3586 Issue.update_all("subject = ''", "id = 2") # Make it invalid
3587 3587 post :bulk_update, :ids => [1, 2], :issue => {:priority_id => 6}
3588 3588
3589 3589 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3590 3590 assert_equal 'Failed to save 1 issue(s) on 2 selected: #2.', flash[:error]
3591 3591 end
3592 3592
3593 3593 def test_get_bulk_copy
3594 3594 @request.session[:user_id] = 2
3595 3595 get :bulk_edit, :ids => [1, 2, 3], :copy => '1'
3596 3596 assert_response :success
3597 3597 assert_template 'bulk_edit'
3598 3598
3599 3599 issues = assigns(:issues)
3600 3600 assert_not_nil issues
3601 3601 assert_equal [1, 2, 3], issues.map(&:id).sort
3602 3602
3603 3603 assert_select 'input[name=copy_attachments]'
3604 3604 end
3605 3605
3606 3606 def test_bulk_copy_to_another_project
3607 3607 @request.session[:user_id] = 2
3608 3608 assert_difference 'Issue.count', 2 do
3609 3609 assert_no_difference 'Project.find(1).issues.count' do
3610 3610 post :bulk_update, :ids => [1, 2], :issue => {:project_id => '2'}, :copy => '1'
3611 3611 end
3612 3612 end
3613 3613 assert_redirected_to '/projects/ecookbook/issues'
3614 3614
3615 3615 copies = Issue.all(:order => 'id DESC', :limit => issues.size)
3616 3616 copies.each do |copy|
3617 3617 assert_equal 2, copy.project_id
3618 3618 end
3619 3619 end
3620 3620
3621 3621 def test_bulk_copy_should_allow_not_changing_the_issue_attributes
3622 3622 @request.session[:user_id] = 2
3623 3623 issues = [
3624 3624 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1, :priority_id => 2, :subject => 'issue 1', :author_id => 1, :assigned_to_id => nil),
3625 3625 Issue.create!(:project_id => 2, :tracker_id => 3, :status_id => 2, :priority_id => 1, :subject => 'issue 2', :author_id => 2, :assigned_to_id => 3)
3626 3626 ]
3627 3627
3628 3628 assert_difference 'Issue.count', issues.size do
3629 3629 post :bulk_update, :ids => issues.map(&:id), :copy => '1',
3630 3630 :issue => {
3631 3631 :project_id => '', :tracker_id => '', :assigned_to_id => '',
3632 3632 :status_id => '', :start_date => '', :due_date => ''
3633 3633 }
3634 3634 end
3635 3635
3636 3636 copies = Issue.all(:order => 'id DESC', :limit => issues.size)
3637 3637 issues.each do |orig|
3638 3638 copy = copies.detect {|c| c.subject == orig.subject}
3639 3639 assert_not_nil copy
3640 3640 assert_equal orig.project_id, copy.project_id
3641 3641 assert_equal orig.tracker_id, copy.tracker_id
3642 3642 assert_equal orig.status_id, copy.status_id
3643 3643 assert_equal orig.assigned_to_id, copy.assigned_to_id
3644 3644 assert_equal orig.priority_id, copy.priority_id
3645 3645 end
3646 3646 end
3647 3647
3648 3648 def test_bulk_copy_should_allow_changing_the_issue_attributes
3649 3649 # Fixes random test failure with Mysql
3650 3650 # where Issue.all(:limit => 2, :order => 'id desc', :conditions => {:project_id => 2})
3651 3651 # doesn't return the expected results
3652 3652 Issue.delete_all("project_id=2")
3653 3653
3654 3654 @request.session[:user_id] = 2
3655 3655 assert_difference 'Issue.count', 2 do
3656 3656 assert_no_difference 'Project.find(1).issues.count' do
3657 3657 post :bulk_update, :ids => [1, 2], :copy => '1',
3658 3658 :issue => {
3659 3659 :project_id => '2', :tracker_id => '', :assigned_to_id => '4',
3660 3660 :status_id => '1', :start_date => '2009-12-01', :due_date => '2009-12-31'
3661 3661 }
3662 3662 end
3663 3663 end
3664 3664
3665 3665 copied_issues = Issue.all(:limit => 2, :order => 'id desc', :conditions => {:project_id => 2})
3666 3666 assert_equal 2, copied_issues.size
3667 3667 copied_issues.each do |issue|
3668 3668 assert_equal 2, issue.project_id, "Project is incorrect"
3669 3669 assert_equal 4, issue.assigned_to_id, "Assigned to is incorrect"
3670 3670 assert_equal 1, issue.status_id, "Status is incorrect"
3671 3671 assert_equal '2009-12-01', issue.start_date.to_s, "Start date is incorrect"
3672 3672 assert_equal '2009-12-31', issue.due_date.to_s, "Due date is incorrect"
3673 3673 end
3674 3674 end
3675 3675
3676 3676 def test_bulk_copy_should_allow_adding_a_note
3677 3677 @request.session[:user_id] = 2
3678 3678 assert_difference 'Issue.count', 1 do
3679 3679 post :bulk_update, :ids => [1], :copy => '1',
3680 3680 :notes => 'Copying one issue',
3681 3681 :issue => {
3682 3682 :project_id => '', :tracker_id => '', :assigned_to_id => '4',
3683 3683 :status_id => '3', :start_date => '2009-12-01', :due_date => '2009-12-31'
3684 3684 }
3685 3685 end
3686 3686
3687 3687 issue = Issue.first(:order => 'id DESC')
3688 3688 assert_equal 1, issue.journals.size
3689 3689 journal = issue.journals.first
3690 3690 assert_equal 0, journal.details.size
3691 3691 assert_equal 'Copying one issue', journal.notes
3692 3692 end
3693 3693
3694 3694 def test_bulk_copy_should_allow_not_copying_the_attachments
3695 3695 attachment_count = Issue.find(3).attachments.size
3696 3696 assert attachment_count > 0
3697 3697 @request.session[:user_id] = 2
3698 3698
3699 3699 assert_difference 'Issue.count', 1 do
3700 3700 assert_no_difference 'Attachment.count' do
3701 3701 post :bulk_update, :ids => [3], :copy => '1',
3702 3702 :issue => {
3703 3703 :project_id => ''
3704 3704 }
3705 3705 end
3706 3706 end
3707 3707 end
3708 3708
3709 3709 def test_bulk_copy_should_allow_copying_the_attachments
3710 3710 attachment_count = Issue.find(3).attachments.size
3711 3711 assert attachment_count > 0
3712 3712 @request.session[:user_id] = 2
3713 3713
3714 3714 assert_difference 'Issue.count', 1 do
3715 3715 assert_difference 'Attachment.count', attachment_count do
3716 3716 post :bulk_update, :ids => [3], :copy => '1', :copy_attachments => '1',
3717 3717 :issue => {
3718 3718 :project_id => ''
3719 3719 }
3720 3720 end
3721 3721 end
3722 3722 end
3723 3723
3724 3724 def test_bulk_copy_should_add_relations_with_copied_issues
3725 3725 @request.session[:user_id] = 2
3726 3726
3727 3727 assert_difference 'Issue.count', 2 do
3728 3728 assert_difference 'IssueRelation.count', 2 do
3729 3729 post :bulk_update, :ids => [1, 3], :copy => '1',
3730 3730 :issue => {
3731 3731 :project_id => '1'
3732 3732 }
3733 3733 end
3734 3734 end
3735 3735 end
3736 3736
3737 3737 def test_bulk_copy_should_allow_not_copying_the_subtasks
3738 3738 issue = Issue.generate_with_descendants!
3739 3739 @request.session[:user_id] = 2
3740 3740
3741 3741 assert_difference 'Issue.count', 1 do
3742 3742 post :bulk_update, :ids => [issue.id], :copy => '1',
3743 3743 :issue => {
3744 3744 :project_id => ''
3745 3745 }
3746 3746 end
3747 3747 end
3748 3748
3749 3749 def test_bulk_copy_should_allow_copying_the_subtasks
3750 3750 issue = Issue.generate_with_descendants!
3751 3751 count = issue.descendants.count
3752 3752 @request.session[:user_id] = 2
3753 3753
3754 3754 assert_difference 'Issue.count', count+1 do
3755 3755 post :bulk_update, :ids => [issue.id], :copy => '1', :copy_subtasks => '1',
3756 3756 :issue => {
3757 3757 :project_id => ''
3758 3758 }
3759 3759 end
3760 3760 copy = Issue.where(:parent_id => nil).order("id DESC").first
3761 3761 assert_equal count, copy.descendants.count
3762 3762 end
3763 3763
3764 3764 def test_bulk_copy_should_not_copy_selected_subtasks_twice
3765 3765 issue = Issue.generate_with_descendants!
3766 3766 count = issue.descendants.count
3767 3767 @request.session[:user_id] = 2
3768 3768
3769 3769 assert_difference 'Issue.count', count+1 do
3770 3770 post :bulk_update, :ids => issue.self_and_descendants.map(&:id), :copy => '1', :copy_subtasks => '1',
3771 3771 :issue => {
3772 3772 :project_id => ''
3773 3773 }
3774 3774 end
3775 3775 copy = Issue.where(:parent_id => nil).order("id DESC").first
3776 3776 assert_equal count, copy.descendants.count
3777 3777 end
3778 3778
3779 3779 def test_bulk_copy_to_another_project_should_follow_when_needed
3780 3780 @request.session[:user_id] = 2
3781 3781 post :bulk_update, :ids => [1], :copy => '1', :issue => {:project_id => 2}, :follow => '1'
3782 3782 issue = Issue.first(:order => 'id DESC')
3783 3783 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
3784 3784 end
3785 3785
3786 3786 def test_destroy_issue_with_no_time_entries
3787 3787 assert_nil TimeEntry.find_by_issue_id(2)
3788 3788 @request.session[:user_id] = 2
3789 3789
3790 3790 assert_difference 'Issue.count', -1 do
3791 3791 delete :destroy, :id => 2
3792 3792 end
3793 3793 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
3794 3794 assert_nil Issue.find_by_id(2)
3795 3795 end
3796 3796
3797 3797 def test_destroy_issues_with_time_entries
3798 3798 @request.session[:user_id] = 2
3799 3799
3800 3800 assert_no_difference 'Issue.count' do
3801 3801 delete :destroy, :ids => [1, 3]
3802 3802 end
3803 3803 assert_response :success
3804 3804 assert_template 'destroy'
3805 3805 assert_not_nil assigns(:hours)
3806 3806 assert Issue.find_by_id(1) && Issue.find_by_id(3)
3807 3807
3808 3808 assert_select 'form' do
3809 3809 assert_select 'input[name=_method][value=delete]'
3810 3810 end
3811 3811 end
3812 3812
3813 3813 def test_destroy_issues_and_destroy_time_entries
3814 3814 @request.session[:user_id] = 2
3815 3815
3816 3816 assert_difference 'Issue.count', -2 do
3817 3817 assert_difference 'TimeEntry.count', -3 do
3818 3818 delete :destroy, :ids => [1, 3], :todo => 'destroy'
3819 3819 end
3820 3820 end
3821 3821 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
3822 3822 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
3823 3823 assert_nil TimeEntry.find_by_id([1, 2])
3824 3824 end
3825 3825
3826 3826 def test_destroy_issues_and_assign_time_entries_to_project
3827 3827 @request.session[:user_id] = 2
3828 3828
3829 3829 assert_difference 'Issue.count', -2 do
3830 3830 assert_no_difference 'TimeEntry.count' do
3831 3831 delete :destroy, :ids => [1, 3], :todo => 'nullify'
3832 3832 end
3833 3833 end
3834 3834 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
3835 3835 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
3836 3836 assert_nil TimeEntry.find(1).issue_id
3837 3837 assert_nil TimeEntry.find(2).issue_id
3838 3838 end
3839 3839
3840 3840 def test_destroy_issues_and_reassign_time_entries_to_another_issue
3841 3841 @request.session[:user_id] = 2
3842 3842
3843 3843 assert_difference 'Issue.count', -2 do
3844 3844 assert_no_difference 'TimeEntry.count' do
3845 3845 delete :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
3846 3846 end
3847 3847 end
3848 3848 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
3849 3849 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
3850 3850 assert_equal 2, TimeEntry.find(1).issue_id
3851 3851 assert_equal 2, TimeEntry.find(2).issue_id
3852 3852 end
3853 3853
3854 3854 def test_destroy_issues_from_different_projects
3855 3855 @request.session[:user_id] = 2
3856 3856
3857 3857 assert_difference 'Issue.count', -3 do
3858 3858 delete :destroy, :ids => [1, 2, 6], :todo => 'destroy'
3859 3859 end
3860 3860 assert_redirected_to :controller => 'issues', :action => 'index'
3861 3861 assert !(Issue.find_by_id(1) || Issue.find_by_id(2) || Issue.find_by_id(6))
3862 3862 end
3863 3863
3864 3864 def test_destroy_parent_and_child_issues
3865 3865 parent = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'Parent Issue')
3866 3866 child = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'Child Issue', :parent_issue_id => parent.id)
3867 3867 assert child.is_descendant_of?(parent.reload)
3868 3868
3869 3869 @request.session[:user_id] = 2
3870 3870 assert_difference 'Issue.count', -2 do
3871 3871 delete :destroy, :ids => [parent.id, child.id], :todo => 'destroy'
3872 3872 end
3873 3873 assert_response 302
3874 3874 end
3875 3875
3876 3876 def test_destroy_invalid_should_respond_with_404
3877 3877 @request.session[:user_id] = 2
3878 3878 assert_no_difference 'Issue.count' do
3879 3879 delete :destroy, :id => 999
3880 3880 end
3881 3881 assert_response 404
3882 3882 end
3883 3883
3884 3884 def test_default_search_scope
3885 3885 get :index
3886 3886
3887 3887 assert_select 'div#quick-search form' do
3888 3888 assert_select 'input[name=issues][value=1][type=hidden]'
3889 3889 end
3890 3890 end
3891 3891 end
@@ -1,290 +1,290
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class QueriesControllerTest < ActionController::TestCase
21 21 fixtures :projects, :users, :members, :member_roles, :roles, :trackers, :issue_statuses, :issue_categories, :enumerations, :issues, :custom_fields, :custom_values, :queries, :enabled_modules
22 22
23 23 def setup
24 24 User.current = nil
25 25 end
26 26
27 27 def test_index
28 28 get :index
29 29 # HTML response not implemented
30 30 assert_response 406
31 31 end
32 32
33 33 def test_new_project_query
34 34 @request.session[:user_id] = 2
35 35 get :new, :project_id => 1
36 36 assert_response :success
37 37 assert_template 'new'
38 38 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
39 39 :name => 'query[is_public]',
40 40 :checked => nil }
41 41 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
42 42 :name => 'query_is_for_all',
43 43 :checked => nil,
44 44 :disabled => nil }
45 45 assert_select 'select[name=?]', 'c[]' do
46 46 assert_select 'option[value=tracker]'
47 47 assert_select 'option[value=subject]'
48 48 end
49 49 end
50 50
51 51 def test_new_global_query
52 52 @request.session[:user_id] = 2
53 53 get :new
54 54 assert_response :success
55 55 assert_template 'new'
56 56 assert_no_tag :tag => 'input', :attributes => { :type => 'checkbox',
57 57 :name => 'query[is_public]' }
58 58 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
59 59 :name => 'query_is_for_all',
60 60 :checked => 'checked',
61 61 :disabled => nil }
62 62 end
63 63
64 64 def test_new_on_invalid_project
65 65 @request.session[:user_id] = 2
66 66 get :new, :project_id => 'invalid'
67 67 assert_response 404
68 68 end
69 69
70 70 def test_create_project_public_query
71 71 @request.session[:user_id] = 2
72 72 post :create,
73 73 :project_id => 'ecookbook',
74 74 :default_columns => '1',
75 75 :f => ["status_id", "assigned_to_id"],
76 76 :op => {"assigned_to_id" => "=", "status_id" => "o"},
77 77 :v => { "assigned_to_id" => ["1"], "status_id" => ["1"]},
78 78 :query => {"name" => "test_new_project_public_query", "is_public" => "1"}
79 79
80 80 q = Query.find_by_name('test_new_project_public_query')
81 81 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook', :query_id => q
82 82 assert q.is_public?
83 83 assert q.has_default_columns?
84 84 assert q.valid?
85 85 end
86 86
87 87 def test_create_project_private_query
88 88 @request.session[:user_id] = 3
89 89 post :create,
90 90 :project_id => 'ecookbook',
91 91 :default_columns => '1',
92 92 :fields => ["status_id", "assigned_to_id"],
93 93 :operators => {"assigned_to_id" => "=", "status_id" => "o"},
94 94 :values => { "assigned_to_id" => ["1"], "status_id" => ["1"]},
95 95 :query => {"name" => "test_new_project_private_query", "is_public" => "1"}
96 96
97 97 q = Query.find_by_name('test_new_project_private_query')
98 98 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook', :query_id => q
99 99 assert !q.is_public?
100 100 assert q.has_default_columns?
101 101 assert q.valid?
102 102 end
103 103
104 104 def test_create_global_private_query_with_custom_columns
105 105 @request.session[:user_id] = 3
106 106 post :create,
107 107 :fields => ["status_id", "assigned_to_id"],
108 108 :operators => {"assigned_to_id" => "=", "status_id" => "o"},
109 109 :values => { "assigned_to_id" => ["me"], "status_id" => ["1"]},
110 110 :query => {"name" => "test_new_global_private_query", "is_public" => "1"},
111 111 :c => ["", "tracker", "subject", "priority", "category"]
112 112
113 113 q = Query.find_by_name('test_new_global_private_query')
114 114 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => nil, :query_id => q
115 115 assert !q.is_public?
116 116 assert !q.has_default_columns?
117 assert_equal [:tracker, :subject, :priority, :category], q.columns.collect {|c| c.name}
117 assert_equal [:id, :tracker, :subject, :priority, :category], q.columns.collect {|c| c.name}
118 118 assert q.valid?
119 119 end
120 120
121 121 def test_create_global_query_with_custom_filters
122 122 @request.session[:user_id] = 3
123 123 post :create,
124 124 :fields => ["assigned_to_id"],
125 125 :operators => {"assigned_to_id" => "="},
126 126 :values => { "assigned_to_id" => ["me"]},
127 127 :query => {"name" => "test_new_global_query"}
128 128
129 129 q = Query.find_by_name('test_new_global_query')
130 130 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => nil, :query_id => q
131 131 assert !q.has_filter?(:status_id)
132 132 assert_equal ['assigned_to_id'], q.filters.keys
133 133 assert q.valid?
134 134 end
135 135
136 136 def test_create_with_sort
137 137 @request.session[:user_id] = 1
138 138 post :create,
139 139 :default_columns => '1',
140 140 :operators => {"status_id" => "o"},
141 141 :values => {"status_id" => ["1"]},
142 142 :query => {:name => "test_new_with_sort",
143 143 :is_public => "1",
144 144 :sort_criteria => {"0" => ["due_date", "desc"], "1" => ["tracker", ""]}}
145 145
146 146 query = Query.find_by_name("test_new_with_sort")
147 147 assert_not_nil query
148 148 assert_equal [['due_date', 'desc'], ['tracker', 'asc']], query.sort_criteria
149 149 end
150 150
151 151 def test_create_with_failure
152 152 @request.session[:user_id] = 2
153 153 assert_no_difference '::Query.count' do
154 154 post :create, :project_id => 'ecookbook', :query => {:name => ''}
155 155 end
156 156 assert_response :success
157 157 assert_template 'new'
158 158 assert_select 'input[name=?]', 'query[name]'
159 159 end
160 160
161 161 def test_edit_global_public_query
162 162 @request.session[:user_id] = 1
163 163 get :edit, :id => 4
164 164 assert_response :success
165 165 assert_template 'edit'
166 166 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
167 167 :name => 'query[is_public]',
168 168 :checked => 'checked' }
169 169 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
170 170 :name => 'query_is_for_all',
171 171 :checked => 'checked',
172 172 :disabled => 'disabled' }
173 173 end
174 174
175 175 def test_edit_global_private_query
176 176 @request.session[:user_id] = 3
177 177 get :edit, :id => 3
178 178 assert_response :success
179 179 assert_template 'edit'
180 180 assert_no_tag :tag => 'input', :attributes => { :type => 'checkbox',
181 181 :name => 'query[is_public]' }
182 182 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
183 183 :name => 'query_is_for_all',
184 184 :checked => 'checked',
185 185 :disabled => 'disabled' }
186 186 end
187 187
188 188 def test_edit_project_private_query
189 189 @request.session[:user_id] = 3
190 190 get :edit, :id => 2
191 191 assert_response :success
192 192 assert_template 'edit'
193 193 assert_no_tag :tag => 'input', :attributes => { :type => 'checkbox',
194 194 :name => 'query[is_public]' }
195 195 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
196 196 :name => 'query_is_for_all',
197 197 :checked => nil,
198 198 :disabled => nil }
199 199 end
200 200
201 201 def test_edit_project_public_query
202 202 @request.session[:user_id] = 2
203 203 get :edit, :id => 1
204 204 assert_response :success
205 205 assert_template 'edit'
206 206 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
207 207 :name => 'query[is_public]',
208 208 :checked => 'checked'
209 209 }
210 210 assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
211 211 :name => 'query_is_for_all',
212 212 :checked => nil,
213 213 :disabled => 'disabled' }
214 214 end
215 215
216 216 def test_edit_sort_criteria
217 217 @request.session[:user_id] = 1
218 218 get :edit, :id => 5
219 219 assert_response :success
220 220 assert_template 'edit'
221 221 assert_tag :tag => 'select', :attributes => { :name => 'query[sort_criteria][0][]' },
222 222 :child => { :tag => 'option', :attributes => { :value => 'priority',
223 223 :selected => 'selected' } }
224 224 assert_tag :tag => 'select', :attributes => { :name => 'query[sort_criteria][0][]' },
225 225 :child => { :tag => 'option', :attributes => { :value => 'desc',
226 226 :selected => 'selected' } }
227 227 end
228 228
229 229 def test_edit_invalid_query
230 230 @request.session[:user_id] = 2
231 231 get :edit, :id => 99
232 232 assert_response 404
233 233 end
234 234
235 235 def test_udpate_global_private_query
236 236 @request.session[:user_id] = 3
237 237 put :update,
238 238 :id => 3,
239 239 :default_columns => '1',
240 240 :fields => ["status_id", "assigned_to_id"],
241 241 :operators => {"assigned_to_id" => "=", "status_id" => "o"},
242 242 :values => { "assigned_to_id" => ["me"], "status_id" => ["1"]},
243 243 :query => {"name" => "test_edit_global_private_query", "is_public" => "1"}
244 244
245 245 assert_redirected_to :controller => 'issues', :action => 'index', :query_id => 3
246 246 q = Query.find_by_name('test_edit_global_private_query')
247 247 assert !q.is_public?
248 248 assert q.has_default_columns?
249 249 assert q.valid?
250 250 end
251 251
252 252 def test_update_global_public_query
253 253 @request.session[:user_id] = 1
254 254 put :update,
255 255 :id => 4,
256 256 :default_columns => '1',
257 257 :fields => ["status_id", "assigned_to_id"],
258 258 :operators => {"assigned_to_id" => "=", "status_id" => "o"},
259 259 :values => { "assigned_to_id" => ["1"], "status_id" => ["1"]},
260 260 :query => {"name" => "test_edit_global_public_query", "is_public" => "1"}
261 261
262 262 assert_redirected_to :controller => 'issues', :action => 'index', :query_id => 4
263 263 q = Query.find_by_name('test_edit_global_public_query')
264 264 assert q.is_public?
265 265 assert q.has_default_columns?
266 266 assert q.valid?
267 267 end
268 268
269 269 def test_update_with_failure
270 270 @request.session[:user_id] = 1
271 271 put :update, :id => 4, :query => {:name => ''}
272 272 assert_response :success
273 273 assert_template 'edit'
274 274 end
275 275
276 276 def test_destroy
277 277 @request.session[:user_id] = 2
278 278 delete :destroy, :id => 1
279 279 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook', :set_filter => 1, :query_id => nil
280 280 assert_nil Query.find_by_id(1)
281 281 end
282 282
283 283 def test_backslash_should_be_escaped_in_filters
284 284 @request.session[:user_id] = 2
285 285 get :new, :subject => 'foo/bar'
286 286 assert_response :success
287 287 assert_template 'new'
288 288 assert_include 'addFilter("subject", "=", ["foo\/bar"]);', response.body
289 289 end
290 290 end
@@ -1,1216 +1,1234
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class QueryTest < ActiveSupport::TestCase
21 21 include Redmine::I18n
22 22
23 23 fixtures :projects, :enabled_modules, :users, :members,
24 24 :member_roles, :roles, :trackers, :issue_statuses,
25 25 :issue_categories, :enumerations, :issues,
26 26 :watchers, :custom_fields, :custom_values, :versions,
27 27 :queries,
28 28 :projects_trackers,
29 29 :custom_fields_trackers
30 30
31 31 def test_available_filters_should_be_ordered
32 32 query = IssueQuery.new
33 33 assert_equal 0, query.available_filters.keys.index('status_id')
34 34 end
35 35
36 36 def test_custom_fields_for_all_projects_should_be_available_in_global_queries
37 37 query = IssueQuery.new(:project => nil, :name => '_')
38 38 assert query.available_filters.has_key?('cf_1')
39 39 assert !query.available_filters.has_key?('cf_3')
40 40 end
41 41
42 42 def test_system_shared_versions_should_be_available_in_global_queries
43 43 Version.find(2).update_attribute :sharing, 'system'
44 44 query = IssueQuery.new(:project => nil, :name => '_')
45 45 assert query.available_filters.has_key?('fixed_version_id')
46 46 assert query.available_filters['fixed_version_id'][:values].detect {|v| v.last == '2'}
47 47 end
48 48
49 49 def test_project_filter_in_global_queries
50 50 query = IssueQuery.new(:project => nil, :name => '_')
51 51 project_filter = query.available_filters["project_id"]
52 52 assert_not_nil project_filter
53 53 project_ids = project_filter[:values].map{|p| p[1]}
54 54 assert project_ids.include?("1") #public project
55 55 assert !project_ids.include?("2") #private project user cannot see
56 56 end
57 57
58 58 def find_issues_with_query(query)
59 59 Issue.includes([:assigned_to, :status, :tracker, :project, :priority]).where(
60 60 query.statement
61 61 ).all
62 62 end
63 63
64 64 def assert_find_issues_with_query_is_successful(query)
65 65 assert_nothing_raised do
66 66 find_issues_with_query(query)
67 67 end
68 68 end
69 69
70 70 def assert_query_statement_includes(query, condition)
71 71 assert_include condition, query.statement
72 72 end
73 73
74 74 def assert_query_result(expected, query)
75 75 assert_nothing_raised do
76 76 assert_equal expected.map(&:id).sort, query.issues.map(&:id).sort
77 77 assert_equal expected.size, query.issue_count
78 78 end
79 79 end
80 80
81 81 def test_query_should_allow_shared_versions_for_a_project_query
82 82 subproject_version = Version.find(4)
83 83 query = IssueQuery.new(:project => Project.find(1), :name => '_')
84 84 query.add_filter('fixed_version_id', '=', [subproject_version.id.to_s])
85 85
86 86 assert query.statement.include?("#{Issue.table_name}.fixed_version_id IN ('4')")
87 87 end
88 88
89 89 def test_query_with_multiple_custom_fields
90 90 query = IssueQuery.find(1)
91 91 assert query.valid?
92 92 assert query.statement.include?("#{CustomValue.table_name}.value IN ('MySQL')")
93 93 issues = find_issues_with_query(query)
94 94 assert_equal 1, issues.length
95 95 assert_equal Issue.find(3), issues.first
96 96 end
97 97
98 98 def test_operator_none
99 99 query = IssueQuery.new(:project => Project.find(1), :name => '_')
100 100 query.add_filter('fixed_version_id', '!*', [''])
101 101 query.add_filter('cf_1', '!*', [''])
102 102 assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NULL")
103 103 assert query.statement.include?("#{CustomValue.table_name}.value IS NULL OR #{CustomValue.table_name}.value = ''")
104 104 find_issues_with_query(query)
105 105 end
106 106
107 107 def test_operator_none_for_integer
108 108 query = IssueQuery.new(:project => Project.find(1), :name => '_')
109 109 query.add_filter('estimated_hours', '!*', [''])
110 110 issues = find_issues_with_query(query)
111 111 assert !issues.empty?
112 112 assert issues.all? {|i| !i.estimated_hours}
113 113 end
114 114
115 115 def test_operator_none_for_date
116 116 query = IssueQuery.new(:project => Project.find(1), :name => '_')
117 117 query.add_filter('start_date', '!*', [''])
118 118 issues = find_issues_with_query(query)
119 119 assert !issues.empty?
120 120 assert issues.all? {|i| i.start_date.nil?}
121 121 end
122 122
123 123 def test_operator_none_for_string_custom_field
124 124 query = IssueQuery.new(:project => Project.find(1), :name => '_')
125 125 query.add_filter('cf_2', '!*', [''])
126 126 assert query.has_filter?('cf_2')
127 127 issues = find_issues_with_query(query)
128 128 assert !issues.empty?
129 129 assert issues.all? {|i| i.custom_field_value(2).blank?}
130 130 end
131 131
132 132 def test_operator_all
133 133 query = IssueQuery.new(:project => Project.find(1), :name => '_')
134 134 query.add_filter('fixed_version_id', '*', [''])
135 135 query.add_filter('cf_1', '*', [''])
136 136 assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NOT NULL")
137 137 assert query.statement.include?("#{CustomValue.table_name}.value IS NOT NULL AND #{CustomValue.table_name}.value <> ''")
138 138 find_issues_with_query(query)
139 139 end
140 140
141 141 def test_operator_all_for_date
142 142 query = IssueQuery.new(:project => Project.find(1), :name => '_')
143 143 query.add_filter('start_date', '*', [''])
144 144 issues = find_issues_with_query(query)
145 145 assert !issues.empty?
146 146 assert issues.all? {|i| i.start_date.present?}
147 147 end
148 148
149 149 def test_operator_all_for_string_custom_field
150 150 query = IssueQuery.new(:project => Project.find(1), :name => '_')
151 151 query.add_filter('cf_2', '*', [''])
152 152 assert query.has_filter?('cf_2')
153 153 issues = find_issues_with_query(query)
154 154 assert !issues.empty?
155 155 assert issues.all? {|i| i.custom_field_value(2).present?}
156 156 end
157 157
158 158 def test_numeric_filter_should_not_accept_non_numeric_values
159 159 query = IssueQuery.new(:name => '_')
160 160 query.add_filter('estimated_hours', '=', ['a'])
161 161
162 162 assert query.has_filter?('estimated_hours')
163 163 assert !query.valid?
164 164 end
165 165
166 166 def test_operator_is_on_float
167 167 Issue.update_all("estimated_hours = 171.2", "id=2")
168 168
169 169 query = IssueQuery.new(:name => '_')
170 170 query.add_filter('estimated_hours', '=', ['171.20'])
171 171 issues = find_issues_with_query(query)
172 172 assert_equal 1, issues.size
173 173 assert_equal 2, issues.first.id
174 174 end
175 175
176 176 def test_operator_is_on_integer_custom_field
177 177 f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_for_all => true, :is_filter => true)
178 178 CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7')
179 179 CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12')
180 180 CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '')
181 181
182 182 query = IssueQuery.new(:name => '_')
183 183 query.add_filter("cf_#{f.id}", '=', ['12'])
184 184 issues = find_issues_with_query(query)
185 185 assert_equal 1, issues.size
186 186 assert_equal 2, issues.first.id
187 187 end
188 188
189 189 def test_operator_is_on_integer_custom_field_should_accept_negative_value
190 190 f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_for_all => true, :is_filter => true)
191 191 CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7')
192 192 CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '-12')
193 193 CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '')
194 194
195 195 query = IssueQuery.new(:name => '_')
196 196 query.add_filter("cf_#{f.id}", '=', ['-12'])
197 197 assert query.valid?
198 198 issues = find_issues_with_query(query)
199 199 assert_equal 1, issues.size
200 200 assert_equal 2, issues.first.id
201 201 end
202 202
203 203 def test_operator_is_on_float_custom_field
204 204 f = IssueCustomField.create!(:name => 'filter', :field_format => 'float', :is_filter => true, :is_for_all => true)
205 205 CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7.3')
206 206 CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12.7')
207 207 CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '')
208 208
209 209 query = IssueQuery.new(:name => '_')
210 210 query.add_filter("cf_#{f.id}", '=', ['12.7'])
211 211 issues = find_issues_with_query(query)
212 212 assert_equal 1, issues.size
213 213 assert_equal 2, issues.first.id
214 214 end
215 215
216 216 def test_operator_is_on_float_custom_field_should_accept_negative_value
217 217 f = IssueCustomField.create!(:name => 'filter', :field_format => 'float', :is_filter => true, :is_for_all => true)
218 218 CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7.3')
219 219 CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '-12.7')
220 220 CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '')
221 221
222 222 query = IssueQuery.new(:name => '_')
223 223 query.add_filter("cf_#{f.id}", '=', ['-12.7'])
224 224 assert query.valid?
225 225 issues = find_issues_with_query(query)
226 226 assert_equal 1, issues.size
227 227 assert_equal 2, issues.first.id
228 228 end
229 229
230 230 def test_operator_is_on_multi_list_custom_field
231 231 f = IssueCustomField.create!(:name => 'filter', :field_format => 'list', :is_filter => true, :is_for_all => true,
232 232 :possible_values => ['value1', 'value2', 'value3'], :multiple => true)
233 233 CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value1')
234 234 CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value2')
235 235 CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => 'value1')
236 236
237 237 query = IssueQuery.new(:name => '_')
238 238 query.add_filter("cf_#{f.id}", '=', ['value1'])
239 239 issues = find_issues_with_query(query)
240 240 assert_equal [1, 3], issues.map(&:id).sort
241 241
242 242 query = IssueQuery.new(:name => '_')
243 243 query.add_filter("cf_#{f.id}", '=', ['value2'])
244 244 issues = find_issues_with_query(query)
245 245 assert_equal [1], issues.map(&:id).sort
246 246 end
247 247
248 248 def test_operator_is_not_on_multi_list_custom_field
249 249 f = IssueCustomField.create!(:name => 'filter', :field_format => 'list', :is_filter => true, :is_for_all => true,
250 250 :possible_values => ['value1', 'value2', 'value3'], :multiple => true)
251 251 CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value1')
252 252 CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => 'value2')
253 253 CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => 'value1')
254 254
255 255 query = IssueQuery.new(:name => '_')
256 256 query.add_filter("cf_#{f.id}", '!', ['value1'])
257 257 issues = find_issues_with_query(query)
258 258 assert !issues.map(&:id).include?(1)
259 259 assert !issues.map(&:id).include?(3)
260 260
261 261 query = IssueQuery.new(:name => '_')
262 262 query.add_filter("cf_#{f.id}", '!', ['value2'])
263 263 issues = find_issues_with_query(query)
264 264 assert !issues.map(&:id).include?(1)
265 265 assert issues.map(&:id).include?(3)
266 266 end
267 267
268 268 def test_operator_is_on_is_private_field
269 269 # is_private filter only available for those who can set issues private
270 270 User.current = User.find(2)
271 271
272 272 query = IssueQuery.new(:name => '_')
273 273 assert query.available_filters.key?('is_private')
274 274
275 275 query.add_filter("is_private", '=', ['1'])
276 276 issues = find_issues_with_query(query)
277 277 assert issues.any?
278 278 assert_nil issues.detect {|issue| !issue.is_private?}
279 279 ensure
280 280 User.current = nil
281 281 end
282 282
283 283 def test_operator_is_not_on_is_private_field
284 284 # is_private filter only available for those who can set issues private
285 285 User.current = User.find(2)
286 286
287 287 query = IssueQuery.new(:name => '_')
288 288 assert query.available_filters.key?('is_private')
289 289
290 290 query.add_filter("is_private", '!', ['1'])
291 291 issues = find_issues_with_query(query)
292 292 assert issues.any?
293 293 assert_nil issues.detect {|issue| issue.is_private?}
294 294 ensure
295 295 User.current = nil
296 296 end
297 297
298 298 def test_operator_greater_than
299 299 query = IssueQuery.new(:project => Project.find(1), :name => '_')
300 300 query.add_filter('done_ratio', '>=', ['40'])
301 301 assert query.statement.include?("#{Issue.table_name}.done_ratio >= 40.0")
302 302 find_issues_with_query(query)
303 303 end
304 304
305 305 def test_operator_greater_than_a_float
306 306 query = IssueQuery.new(:project => Project.find(1), :name => '_')
307 307 query.add_filter('estimated_hours', '>=', ['40.5'])
308 308 assert query.statement.include?("#{Issue.table_name}.estimated_hours >= 40.5")
309 309 find_issues_with_query(query)
310 310 end
311 311
312 312 def test_operator_greater_than_on_int_custom_field
313 313 f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true)
314 314 CustomValue.create!(:custom_field => f, :customized => Issue.find(1), :value => '7')
315 315 CustomValue.create!(:custom_field => f, :customized => Issue.find(2), :value => '12')
316 316 CustomValue.create!(:custom_field => f, :customized => Issue.find(3), :value => '')
317 317
318 318 query = IssueQuery.new(:project => Project.find(1), :name => '_')
319 319 query.add_filter("cf_#{f.id}", '>=', ['8'])
320 320 issues = find_issues_with_query(query)
321 321 assert_equal 1, issues.size
322 322 assert_equal 2, issues.first.id
323 323 end
324 324
325 325 def test_operator_lesser_than
326 326 query = IssueQuery.new(:project => Project.find(1), :name => '_')
327 327 query.add_filter('done_ratio', '<=', ['30'])
328 328 assert query.statement.include?("#{Issue.table_name}.done_ratio <= 30.0")
329 329 find_issues_with_query(query)
330 330 end
331 331
332 332 def test_operator_lesser_than_on_custom_field
333 333 f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true)
334 334 query = IssueQuery.new(:project => Project.find(1), :name => '_')
335 335 query.add_filter("cf_#{f.id}", '<=', ['30'])
336 336 assert_match /CAST.+ <= 30\.0/, query.statement
337 337 find_issues_with_query(query)
338 338 end
339 339
340 340 def test_operator_between
341 341 query = IssueQuery.new(:project => Project.find(1), :name => '_')
342 342 query.add_filter('done_ratio', '><', ['30', '40'])
343 343 assert_include "#{Issue.table_name}.done_ratio BETWEEN 30.0 AND 40.0", query.statement
344 344 find_issues_with_query(query)
345 345 end
346 346
347 347 def test_operator_between_on_custom_field
348 348 f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true)
349 349 query = IssueQuery.new(:project => Project.find(1), :name => '_')
350 350 query.add_filter("cf_#{f.id}", '><', ['30', '40'])
351 351 assert_match /CAST.+ BETWEEN 30.0 AND 40.0/, query.statement
352 352 find_issues_with_query(query)
353 353 end
354 354
355 355 def test_date_filter_should_not_accept_non_date_values
356 356 query = IssueQuery.new(:name => '_')
357 357 query.add_filter('created_on', '=', ['a'])
358 358
359 359 assert query.has_filter?('created_on')
360 360 assert !query.valid?
361 361 end
362 362
363 363 def test_date_filter_should_not_accept_invalid_date_values
364 364 query = IssueQuery.new(:name => '_')
365 365 query.add_filter('created_on', '=', ['2011-01-34'])
366 366
367 367 assert query.has_filter?('created_on')
368 368 assert !query.valid?
369 369 end
370 370
371 371 def test_relative_date_filter_should_not_accept_non_integer_values
372 372 query = IssueQuery.new(:name => '_')
373 373 query.add_filter('created_on', '>t-', ['a'])
374 374
375 375 assert query.has_filter?('created_on')
376 376 assert !query.valid?
377 377 end
378 378
379 379 def test_operator_date_equals
380 380 query = IssueQuery.new(:name => '_')
381 381 query.add_filter('due_date', '=', ['2011-07-10'])
382 382 assert_match /issues\.due_date > '2011-07-09 23:59:59(\.9+)?' AND issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement
383 383 find_issues_with_query(query)
384 384 end
385 385
386 386 def test_operator_date_lesser_than
387 387 query = IssueQuery.new(:name => '_')
388 388 query.add_filter('due_date', '<=', ['2011-07-10'])
389 389 assert_match /issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement
390 390 find_issues_with_query(query)
391 391 end
392 392
393 393 def test_operator_date_greater_than
394 394 query = IssueQuery.new(:name => '_')
395 395 query.add_filter('due_date', '>=', ['2011-07-10'])
396 396 assert_match /issues\.due_date > '2011-07-09 23:59:59(\.9+)?'/, query.statement
397 397 find_issues_with_query(query)
398 398 end
399 399
400 400 def test_operator_date_between
401 401 query = IssueQuery.new(:name => '_')
402 402 query.add_filter('due_date', '><', ['2011-06-23', '2011-07-10'])
403 403 assert_match /issues\.due_date > '2011-06-22 23:59:59(\.9+)?' AND issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement
404 404 find_issues_with_query(query)
405 405 end
406 406
407 407 def test_operator_in_more_than
408 408 Issue.find(7).update_attribute(:due_date, (Date.today + 15))
409 409 query = IssueQuery.new(:project => Project.find(1), :name => '_')
410 410 query.add_filter('due_date', '>t+', ['15'])
411 411 issues = find_issues_with_query(query)
412 412 assert !issues.empty?
413 413 issues.each {|issue| assert(issue.due_date >= (Date.today + 15))}
414 414 end
415 415
416 416 def test_operator_in_less_than
417 417 query = IssueQuery.new(:project => Project.find(1), :name => '_')
418 418 query.add_filter('due_date', '<t+', ['15'])
419 419 issues = find_issues_with_query(query)
420 420 assert !issues.empty?
421 421 issues.each {|issue| assert(issue.due_date <= (Date.today + 15))}
422 422 end
423 423
424 424 def test_operator_in_the_next_days
425 425 query = IssueQuery.new(:project => Project.find(1), :name => '_')
426 426 query.add_filter('due_date', '><t+', ['15'])
427 427 issues = find_issues_with_query(query)
428 428 assert !issues.empty?
429 429 issues.each {|issue| assert(issue.due_date >= Date.today && issue.due_date <= (Date.today + 15))}
430 430 end
431 431
432 432 def test_operator_less_than_ago
433 433 Issue.find(7).update_attribute(:due_date, (Date.today - 3))
434 434 query = IssueQuery.new(:project => Project.find(1), :name => '_')
435 435 query.add_filter('due_date', '>t-', ['3'])
436 436 issues = find_issues_with_query(query)
437 437 assert !issues.empty?
438 438 issues.each {|issue| assert(issue.due_date >= (Date.today - 3))}
439 439 end
440 440
441 441 def test_operator_in_the_past_days
442 442 Issue.find(7).update_attribute(:due_date, (Date.today - 3))
443 443 query = IssueQuery.new(:project => Project.find(1), :name => '_')
444 444 query.add_filter('due_date', '><t-', ['3'])
445 445 issues = find_issues_with_query(query)
446 446 assert !issues.empty?
447 447 issues.each {|issue| assert(issue.due_date >= (Date.today - 3) && issue.due_date <= Date.today)}
448 448 end
449 449
450 450 def test_operator_more_than_ago
451 451 Issue.find(7).update_attribute(:due_date, (Date.today - 10))
452 452 query = IssueQuery.new(:project => Project.find(1), :name => '_')
453 453 query.add_filter('due_date', '<t-', ['10'])
454 454 assert query.statement.include?("#{Issue.table_name}.due_date <=")
455 455 issues = find_issues_with_query(query)
456 456 assert !issues.empty?
457 457 issues.each {|issue| assert(issue.due_date <= (Date.today - 10))}
458 458 end
459 459
460 460 def test_operator_in
461 461 Issue.find(7).update_attribute(:due_date, (Date.today + 2))
462 462 query = IssueQuery.new(:project => Project.find(1), :name => '_')
463 463 query.add_filter('due_date', 't+', ['2'])
464 464 issues = find_issues_with_query(query)
465 465 assert !issues.empty?
466 466 issues.each {|issue| assert_equal((Date.today + 2), issue.due_date)}
467 467 end
468 468
469 469 def test_operator_ago
470 470 Issue.find(7).update_attribute(:due_date, (Date.today - 3))
471 471 query = IssueQuery.new(:project => Project.find(1), :name => '_')
472 472 query.add_filter('due_date', 't-', ['3'])
473 473 issues = find_issues_with_query(query)
474 474 assert !issues.empty?
475 475 issues.each {|issue| assert_equal((Date.today - 3), issue.due_date)}
476 476 end
477 477
478 478 def test_operator_today
479 479 query = IssueQuery.new(:project => Project.find(1), :name => '_')
480 480 query.add_filter('due_date', 't', [''])
481 481 issues = find_issues_with_query(query)
482 482 assert !issues.empty?
483 483 issues.each {|issue| assert_equal Date.today, issue.due_date}
484 484 end
485 485
486 486 def test_operator_this_week_on_date
487 487 query = IssueQuery.new(:project => Project.find(1), :name => '_')
488 488 query.add_filter('due_date', 'w', [''])
489 489 find_issues_with_query(query)
490 490 end
491 491
492 492 def test_operator_this_week_on_datetime
493 493 query = IssueQuery.new(:project => Project.find(1), :name => '_')
494 494 query.add_filter('created_on', 'w', [''])
495 495 find_issues_with_query(query)
496 496 end
497 497
498 498 def test_operator_contains
499 499 query = IssueQuery.new(:project => Project.find(1), :name => '_')
500 500 query.add_filter('subject', '~', ['uNable'])
501 501 assert query.statement.include?("LOWER(#{Issue.table_name}.subject) LIKE '%unable%'")
502 502 result = find_issues_with_query(query)
503 503 assert result.empty?
504 504 result.each {|issue| assert issue.subject.downcase.include?('unable') }
505 505 end
506 506
507 507 def test_range_for_this_week_with_week_starting_on_monday
508 508 I18n.locale = :fr
509 509 assert_equal '1', I18n.t(:general_first_day_of_week)
510 510
511 511 Date.stubs(:today).returns(Date.parse('2011-04-29'))
512 512
513 513 query = IssueQuery.new(:project => Project.find(1), :name => '_')
514 514 query.add_filter('due_date', 'w', [''])
515 515 assert query.statement.match(/issues\.due_date > '2011-04-24 23:59:59(\.9+)?' AND issues\.due_date <= '2011-05-01 23:59:59(\.9+)?/), "range not found in #{query.statement}"
516 516 I18n.locale = :en
517 517 end
518 518
519 519 def test_range_for_this_week_with_week_starting_on_sunday
520 520 I18n.locale = :en
521 521 assert_equal '7', I18n.t(:general_first_day_of_week)
522 522
523 523 Date.stubs(:today).returns(Date.parse('2011-04-29'))
524 524
525 525 query = IssueQuery.new(:project => Project.find(1), :name => '_')
526 526 query.add_filter('due_date', 'w', [''])
527 527 assert query.statement.match(/issues\.due_date > '2011-04-23 23:59:59(\.9+)?' AND issues\.due_date <= '2011-04-30 23:59:59(\.9+)?/), "range not found in #{query.statement}"
528 528 end
529 529
530 530 def test_operator_does_not_contains
531 531 query = IssueQuery.new(:project => Project.find(1), :name => '_')
532 532 query.add_filter('subject', '!~', ['uNable'])
533 533 assert query.statement.include?("LOWER(#{Issue.table_name}.subject) NOT LIKE '%unable%'")
534 534 find_issues_with_query(query)
535 535 end
536 536
537 537 def test_filter_assigned_to_me
538 538 user = User.find(2)
539 539 group = Group.find(10)
540 540 User.current = user
541 541 i1 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => user)
542 542 i2 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => group)
543 543 i3 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => Group.find(11))
544 544 group.users << user
545 545
546 546 query = IssueQuery.new(:name => '_', :filters => { 'assigned_to_id' => {:operator => '=', :values => ['me']}})
547 547 result = query.issues
548 548 assert_equal Issue.visible.all(:conditions => {:assigned_to_id => ([2] + user.reload.group_ids)}).sort_by(&:id), result.sort_by(&:id)
549 549
550 550 assert result.include?(i1)
551 551 assert result.include?(i2)
552 552 assert !result.include?(i3)
553 553 end
554 554
555 555 def test_user_custom_field_filtered_on_me
556 556 User.current = User.find(2)
557 557 cf = IssueCustomField.create!(:field_format => 'user', :is_for_all => true, :is_filter => true, :name => 'User custom field', :tracker_ids => [1])
558 558 issue1 = Issue.create!(:project_id => 1, :tracker_id => 1, :custom_field_values => {cf.id.to_s => '2'}, :subject => 'Test', :author_id => 1)
559 559 issue2 = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {cf.id.to_s => '3'})
560 560
561 561 query = IssueQuery.new(:name => '_', :project => Project.find(1))
562 562 filter = query.available_filters["cf_#{cf.id}"]
563 563 assert_not_nil filter
564 564 assert_include 'me', filter[:values].map{|v| v[1]}
565 565
566 566 query.filters = { "cf_#{cf.id}" => {:operator => '=', :values => ['me']}}
567 567 result = query.issues
568 568 assert_equal 1, result.size
569 569 assert_equal issue1, result.first
570 570 end
571 571
572 572 def test_filter_my_projects
573 573 User.current = User.find(2)
574 574 query = IssueQuery.new(:name => '_')
575 575 filter = query.available_filters['project_id']
576 576 assert_not_nil filter
577 577 assert_include 'mine', filter[:values].map{|v| v[1]}
578 578
579 579 query.filters = { 'project_id' => {:operator => '=', :values => ['mine']}}
580 580 result = query.issues
581 581 assert_nil result.detect {|issue| !User.current.member_of?(issue.project)}
582 582 end
583 583
584 584 def test_filter_watched_issues
585 585 User.current = User.find(1)
586 586 query = IssueQuery.new(:name => '_', :filters => { 'watcher_id' => {:operator => '=', :values => ['me']}})
587 587 result = find_issues_with_query(query)
588 588 assert_not_nil result
589 589 assert !result.empty?
590 590 assert_equal Issue.visible.watched_by(User.current).sort_by(&:id), result.sort_by(&:id)
591 591 User.current = nil
592 592 end
593 593
594 594 def test_filter_unwatched_issues
595 595 User.current = User.find(1)
596 596 query = IssueQuery.new(:name => '_', :filters => { 'watcher_id' => {:operator => '!', :values => ['me']}})
597 597 result = find_issues_with_query(query)
598 598 assert_not_nil result
599 599 assert !result.empty?
600 600 assert_equal((Issue.visible - Issue.watched_by(User.current)).sort_by(&:id).size, result.sort_by(&:id).size)
601 601 User.current = nil
602 602 end
603 603
604 604 def test_filter_on_project_custom_field
605 605 field = ProjectCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
606 606 CustomValue.create!(:custom_field => field, :customized => Project.find(3), :value => 'Foo')
607 607 CustomValue.create!(:custom_field => field, :customized => Project.find(5), :value => 'Foo')
608 608
609 609 query = IssueQuery.new(:name => '_')
610 610 filter_name = "project.cf_#{field.id}"
611 611 assert_include filter_name, query.available_filters.keys
612 612 query.filters = {filter_name => {:operator => '=', :values => ['Foo']}}
613 613 assert_equal [3, 5], find_issues_with_query(query).map(&:project_id).uniq.sort
614 614 end
615 615
616 616 def test_filter_on_author_custom_field
617 617 field = UserCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
618 618 CustomValue.create!(:custom_field => field, :customized => User.find(3), :value => 'Foo')
619 619
620 620 query = IssueQuery.new(:name => '_')
621 621 filter_name = "author.cf_#{field.id}"
622 622 assert_include filter_name, query.available_filters.keys
623 623 query.filters = {filter_name => {:operator => '=', :values => ['Foo']}}
624 624 assert_equal [3], find_issues_with_query(query).map(&:author_id).uniq.sort
625 625 end
626 626
627 627 def test_filter_on_assigned_to_custom_field
628 628 field = UserCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
629 629 CustomValue.create!(:custom_field => field, :customized => User.find(3), :value => 'Foo')
630 630
631 631 query = IssueQuery.new(:name => '_')
632 632 filter_name = "assigned_to.cf_#{field.id}"
633 633 assert_include filter_name, query.available_filters.keys
634 634 query.filters = {filter_name => {:operator => '=', :values => ['Foo']}}
635 635 assert_equal [3], find_issues_with_query(query).map(&:assigned_to_id).uniq.sort
636 636 end
637 637
638 638 def test_filter_on_fixed_version_custom_field
639 639 field = VersionCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
640 640 CustomValue.create!(:custom_field => field, :customized => Version.find(2), :value => 'Foo')
641 641
642 642 query = IssueQuery.new(:name => '_')
643 643 filter_name = "fixed_version.cf_#{field.id}"
644 644 assert_include filter_name, query.available_filters.keys
645 645 query.filters = {filter_name => {:operator => '=', :values => ['Foo']}}
646 646 assert_equal [2], find_issues_with_query(query).map(&:fixed_version_id).uniq.sort
647 647 end
648 648
649 649 def test_filter_on_relations_with_a_specific_issue
650 650 IssueRelation.delete_all
651 651 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(2))
652 652 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(3), :issue_to => Issue.find(1))
653 653
654 654 query = IssueQuery.new(:name => '_')
655 655 query.filters = {"relates" => {:operator => '=', :values => ['1']}}
656 656 assert_equal [2, 3], find_issues_with_query(query).map(&:id).sort
657 657
658 658 query = IssueQuery.new(:name => '_')
659 659 query.filters = {"relates" => {:operator => '=', :values => ['2']}}
660 660 assert_equal [1], find_issues_with_query(query).map(&:id).sort
661 661 end
662 662
663 663 def test_filter_on_relations_with_any_issues_in_a_project
664 664 IssueRelation.delete_all
665 665 with_settings :cross_project_issue_relations => '1' do
666 666 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(2).issues.first)
667 667 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(2), :issue_to => Project.find(2).issues.first)
668 668 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(3).issues.first)
669 669 end
670 670
671 671 query = IssueQuery.new(:name => '_')
672 672 query.filters = {"relates" => {:operator => '=p', :values => ['2']}}
673 673 assert_equal [1, 2], find_issues_with_query(query).map(&:id).sort
674 674
675 675 query = IssueQuery.new(:name => '_')
676 676 query.filters = {"relates" => {:operator => '=p', :values => ['3']}}
677 677 assert_equal [1], find_issues_with_query(query).map(&:id).sort
678 678
679 679 query = IssueQuery.new(:name => '_')
680 680 query.filters = {"relates" => {:operator => '=p', :values => ['4']}}
681 681 assert_equal [], find_issues_with_query(query).map(&:id).sort
682 682 end
683 683
684 684 def test_filter_on_relations_with_any_issues_not_in_a_project
685 685 IssueRelation.delete_all
686 686 with_settings :cross_project_issue_relations => '1' do
687 687 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(2).issues.first)
688 688 #IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(2), :issue_to => Project.find(1).issues.first)
689 689 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(3).issues.first)
690 690 end
691 691
692 692 query = IssueQuery.new(:name => '_')
693 693 query.filters = {"relates" => {:operator => '=!p', :values => ['1']}}
694 694 assert_equal [1], find_issues_with_query(query).map(&:id).sort
695 695 end
696 696
697 697 def test_filter_on_relations_with_no_issues_in_a_project
698 698 IssueRelation.delete_all
699 699 with_settings :cross_project_issue_relations => '1' do
700 700 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Project.find(2).issues.first)
701 701 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(2), :issue_to => Project.find(3).issues.first)
702 702 IssueRelation.create!(:relation_type => "relates", :issue_to => Project.find(2).issues.first, :issue_from => Issue.find(3))
703 703 end
704 704
705 705 query = IssueQuery.new(:name => '_')
706 706 query.filters = {"relates" => {:operator => '!p', :values => ['2']}}
707 707 ids = find_issues_with_query(query).map(&:id).sort
708 708 assert_include 2, ids
709 709 assert_not_include 1, ids
710 710 assert_not_include 3, ids
711 711 end
712 712
713 713 def test_filter_on_relations_with_no_issues
714 714 IssueRelation.delete_all
715 715 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(2))
716 716 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(3), :issue_to => Issue.find(1))
717 717
718 718 query = IssueQuery.new(:name => '_')
719 719 query.filters = {"relates" => {:operator => '!*', :values => ['']}}
720 720 ids = find_issues_with_query(query).map(&:id)
721 721 assert_equal [], ids & [1, 2, 3]
722 722 assert_include 4, ids
723 723 end
724 724
725 725 def test_filter_on_relations_with_any_issues
726 726 IssueRelation.delete_all
727 727 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(2))
728 728 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(3), :issue_to => Issue.find(1))
729 729
730 730 query = IssueQuery.new(:name => '_')
731 731 query.filters = {"relates" => {:operator => '*', :values => ['']}}
732 732 assert_equal [1, 2, 3], find_issues_with_query(query).map(&:id).sort
733 733 end
734 734
735 735 def test_statement_should_be_nil_with_no_filters
736 736 q = IssueQuery.new(:name => '_')
737 737 q.filters = {}
738 738
739 739 assert q.valid?
740 740 assert_nil q.statement
741 741 end
742 742
743 743 def test_default_columns
744 744 q = IssueQuery.new
745 745 assert q.columns.any?
746 746 assert q.inline_columns.any?
747 747 assert q.block_columns.empty?
748 748 end
749 749
750 750 def test_set_column_names
751 751 q = IssueQuery.new
752 752 q.column_names = ['tracker', :subject, '', 'unknonw_column']
753 assert_equal [:tracker, :subject], q.columns.collect {|c| c.name}
754 c = q.columns.first
755 assert q.has_column?(c)
753 assert_equal [:id, :tracker, :subject], q.columns.collect {|c| c.name}
754 end
755
756 def test_has_column_should_accept_a_column_name
757 q = IssueQuery.new
758 q.column_names = ['tracker', :subject]
759 assert q.has_column?(:tracker)
760 assert !q.has_column?(:category)
761 end
762
763 def test_has_column_should_accept_a_column
764 q = IssueQuery.new
765 q.column_names = ['tracker', :subject]
766
767 tracker_column = q.available_columns.detect {|c| c.name==:tracker}
768 assert_kind_of QueryColumn, tracker_column
769 category_column = q.available_columns.detect {|c| c.name==:category}
770 assert_kind_of QueryColumn, category_column
771
772 assert q.has_column?(tracker_column)
773 assert !q.has_column?(category_column)
756 774 end
757 775
758 776 def test_inline_and_block_columns
759 777 q = IssueQuery.new
760 778 q.column_names = ['subject', 'description', 'tracker']
761 779
762 assert_equal [:subject, :tracker], q.inline_columns.map(&:name)
780 assert_equal [:id, :subject, :tracker], q.inline_columns.map(&:name)
763 781 assert_equal [:description], q.block_columns.map(&:name)
764 782 end
765 783
766 784 def test_custom_field_columns_should_be_inline
767 785 q = IssueQuery.new
768 786 columns = q.available_columns.select {|column| column.is_a? QueryCustomFieldColumn}
769 787 assert columns.any?
770 788 assert_nil columns.detect {|column| !column.inline?}
771 789 end
772 790
773 791 def test_query_should_preload_spent_hours
774 792 q = IssueQuery.new(:name => '_', :column_names => [:subject, :spent_hours])
775 793 assert q.has_column?(:spent_hours)
776 794 issues = q.issues
777 795 assert_not_nil issues.first.instance_variable_get("@spent_hours")
778 796 end
779 797
780 798 def test_groupable_columns_should_include_custom_fields
781 799 q = IssueQuery.new
782 800 column = q.groupable_columns.detect {|c| c.name == :cf_1}
783 801 assert_not_nil column
784 802 assert_kind_of QueryCustomFieldColumn, column
785 803 end
786 804
787 805 def test_groupable_columns_should_not_include_multi_custom_fields
788 806 field = CustomField.find(1)
789 807 field.update_attribute :multiple, true
790 808
791 809 q = IssueQuery.new
792 810 column = q.groupable_columns.detect {|c| c.name == :cf_1}
793 811 assert_nil column
794 812 end
795 813
796 814 def test_groupable_columns_should_include_user_custom_fields
797 815 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1], :field_format => 'user')
798 816
799 817 q = IssueQuery.new
800 818 assert q.groupable_columns.detect {|c| c.name == "cf_#{cf.id}".to_sym}
801 819 end
802 820
803 821 def test_groupable_columns_should_include_version_custom_fields
804 822 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1], :field_format => 'version')
805 823
806 824 q = IssueQuery.new
807 825 assert q.groupable_columns.detect {|c| c.name == "cf_#{cf.id}".to_sym}
808 826 end
809 827
810 828 def test_grouped_with_valid_column
811 829 q = IssueQuery.new(:group_by => 'status')
812 830 assert q.grouped?
813 831 assert_not_nil q.group_by_column
814 832 assert_equal :status, q.group_by_column.name
815 833 assert_not_nil q.group_by_statement
816 834 assert_equal 'status', q.group_by_statement
817 835 end
818 836
819 837 def test_grouped_with_invalid_column
820 838 q = IssueQuery.new(:group_by => 'foo')
821 839 assert !q.grouped?
822 840 assert_nil q.group_by_column
823 841 assert_nil q.group_by_statement
824 842 end
825 843
826 844 def test_sortable_columns_should_sort_assignees_according_to_user_format_setting
827 845 with_settings :user_format => 'lastname_coma_firstname' do
828 846 q = IssueQuery.new
829 847 assert q.sortable_columns.has_key?('assigned_to')
830 848 assert_equal %w(users.lastname users.firstname users.id), q.sortable_columns['assigned_to']
831 849 end
832 850 end
833 851
834 852 def test_sortable_columns_should_sort_authors_according_to_user_format_setting
835 853 with_settings :user_format => 'lastname_coma_firstname' do
836 854 q = IssueQuery.new
837 855 assert q.sortable_columns.has_key?('author')
838 856 assert_equal %w(authors.lastname authors.firstname authors.id), q.sortable_columns['author']
839 857 end
840 858 end
841 859
842 860 def test_sortable_columns_should_include_custom_field
843 861 q = IssueQuery.new
844 862 assert q.sortable_columns['cf_1']
845 863 end
846 864
847 865 def test_sortable_columns_should_not_include_multi_custom_field
848 866 field = CustomField.find(1)
849 867 field.update_attribute :multiple, true
850 868
851 869 q = IssueQuery.new
852 870 assert !q.sortable_columns['cf_1']
853 871 end
854 872
855 873 def test_default_sort
856 874 q = IssueQuery.new
857 875 assert_equal [], q.sort_criteria
858 876 end
859 877
860 878 def test_set_sort_criteria_with_hash
861 879 q = IssueQuery.new
862 880 q.sort_criteria = {'0' => ['priority', 'desc'], '2' => ['tracker']}
863 881 assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria
864 882 end
865 883
866 884 def test_set_sort_criteria_with_array
867 885 q = IssueQuery.new
868 886 q.sort_criteria = [['priority', 'desc'], 'tracker']
869 887 assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria
870 888 end
871 889
872 890 def test_create_query_with_sort
873 891 q = IssueQuery.new(:name => 'Sorted')
874 892 q.sort_criteria = [['priority', 'desc'], 'tracker']
875 893 assert q.save
876 894 q.reload
877 895 assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria
878 896 end
879 897
880 898 def test_sort_by_string_custom_field_asc
881 899 q = IssueQuery.new
882 900 c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' }
883 901 assert c
884 902 assert c.sortable
885 903 issues = q.issues(:order => "#{c.sortable} ASC")
886 904 values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s}
887 905 assert !values.empty?
888 906 assert_equal values.sort, values
889 907 end
890 908
891 909 def test_sort_by_string_custom_field_desc
892 910 q = IssueQuery.new
893 911 c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' }
894 912 assert c
895 913 assert c.sortable
896 914 issues = q.issues(:order => "#{c.sortable} DESC")
897 915 values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s}
898 916 assert !values.empty?
899 917 assert_equal values.sort.reverse, values
900 918 end
901 919
902 920 def test_sort_by_float_custom_field_asc
903 921 q = IssueQuery.new
904 922 c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'float' }
905 923 assert c
906 924 assert c.sortable
907 925 issues = q.issues(:order => "#{c.sortable} ASC")
908 926 values = issues.collect {|i| begin; Kernel.Float(i.custom_value_for(c.custom_field).to_s); rescue; nil; end}.compact
909 927 assert !values.empty?
910 928 assert_equal values.sort, values
911 929 end
912 930
913 931 def test_invalid_query_should_raise_query_statement_invalid_error
914 932 q = IssueQuery.new
915 933 assert_raise Query::StatementInvalid do
916 934 q.issues(:conditions => "foo = 1")
917 935 end
918 936 end
919 937
920 938 def test_issue_count
921 939 q = IssueQuery.new(:name => '_')
922 940 issue_count = q.issue_count
923 941 assert_equal q.issues.size, issue_count
924 942 end
925 943
926 944 def test_issue_count_with_archived_issues
927 945 p = Project.generate! do |project|
928 946 project.status = Project::STATUS_ARCHIVED
929 947 end
930 948 i = Issue.generate!( :project => p, :tracker => p.trackers.first )
931 949 assert !i.visible?
932 950
933 951 test_issue_count
934 952 end
935 953
936 954 def test_issue_count_by_association_group
937 955 q = IssueQuery.new(:name => '_', :group_by => 'assigned_to')
938 956 count_by_group = q.issue_count_by_group
939 957 assert_kind_of Hash, count_by_group
940 958 assert_equal %w(NilClass User), count_by_group.keys.collect {|k| k.class.name}.uniq.sort
941 959 assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq
942 960 assert count_by_group.has_key?(User.find(3))
943 961 end
944 962
945 963 def test_issue_count_by_list_custom_field_group
946 964 q = IssueQuery.new(:name => '_', :group_by => 'cf_1')
947 965 count_by_group = q.issue_count_by_group
948 966 assert_kind_of Hash, count_by_group
949 967 assert_equal %w(NilClass String), count_by_group.keys.collect {|k| k.class.name}.uniq.sort
950 968 assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq
951 969 assert count_by_group.has_key?('MySQL')
952 970 end
953 971
954 972 def test_issue_count_by_date_custom_field_group
955 973 q = IssueQuery.new(:name => '_', :group_by => 'cf_8')
956 974 count_by_group = q.issue_count_by_group
957 975 assert_kind_of Hash, count_by_group
958 976 assert_equal %w(Date NilClass), count_by_group.keys.collect {|k| k.class.name}.uniq.sort
959 977 assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq
960 978 end
961 979
962 980 def test_issue_count_with_nil_group_only
963 981 Issue.update_all("assigned_to_id = NULL")
964 982
965 983 q = IssueQuery.new(:name => '_', :group_by => 'assigned_to')
966 984 count_by_group = q.issue_count_by_group
967 985 assert_kind_of Hash, count_by_group
968 986 assert_equal 1, count_by_group.keys.size
969 987 assert_nil count_by_group.keys.first
970 988 end
971 989
972 990 def test_issue_ids
973 991 q = IssueQuery.new(:name => '_')
974 992 order = "issues.subject, issues.id"
975 993 issues = q.issues(:order => order)
976 994 assert_equal issues.map(&:id), q.issue_ids(:order => order)
977 995 end
978 996
979 997 def test_label_for
980 998 set_language_if_valid 'en'
981 999 q = IssueQuery.new
982 1000 assert_equal 'Assignee', q.label_for('assigned_to_id')
983 1001 end
984 1002
985 1003 def test_label_for_fr
986 1004 set_language_if_valid 'fr'
987 1005 q = IssueQuery.new
988 1006 s = "Assign\xc3\xa9 \xc3\xa0"
989 1007 s.force_encoding('UTF-8') if s.respond_to?(:force_encoding)
990 1008 assert_equal s, q.label_for('assigned_to_id')
991 1009 end
992 1010
993 1011 def test_editable_by
994 1012 admin = User.find(1)
995 1013 manager = User.find(2)
996 1014 developer = User.find(3)
997 1015
998 1016 # Public query on project 1
999 1017 q = IssueQuery.find(1)
1000 1018 assert q.editable_by?(admin)
1001 1019 assert q.editable_by?(manager)
1002 1020 assert !q.editable_by?(developer)
1003 1021
1004 1022 # Private query on project 1
1005 1023 q = IssueQuery.find(2)
1006 1024 assert q.editable_by?(admin)
1007 1025 assert !q.editable_by?(manager)
1008 1026 assert q.editable_by?(developer)
1009 1027
1010 1028 # Private query for all projects
1011 1029 q = IssueQuery.find(3)
1012 1030 assert q.editable_by?(admin)
1013 1031 assert !q.editable_by?(manager)
1014 1032 assert q.editable_by?(developer)
1015 1033
1016 1034 # Public query for all projects
1017 1035 q = IssueQuery.find(4)
1018 1036 assert q.editable_by?(admin)
1019 1037 assert !q.editable_by?(manager)
1020 1038 assert !q.editable_by?(developer)
1021 1039 end
1022 1040
1023 1041 def test_visible_scope
1024 1042 query_ids = IssueQuery.visible(User.anonymous).map(&:id)
1025 1043
1026 1044 assert query_ids.include?(1), 'public query on public project was not visible'
1027 1045 assert query_ids.include?(4), 'public query for all projects was not visible'
1028 1046 assert !query_ids.include?(2), 'private query on public project was visible'
1029 1047 assert !query_ids.include?(3), 'private query for all projects was visible'
1030 1048 assert !query_ids.include?(7), 'public query on private project was visible'
1031 1049 end
1032 1050
1033 1051 test "#available_filters should include users of visible projects in cross-project view" do
1034 1052 users = IssueQuery.new.available_filters["assigned_to_id"]
1035 1053 assert_not_nil users
1036 1054 assert users[:values].map{|u|u[1]}.include?("3")
1037 1055 end
1038 1056
1039 1057 test "#available_filters should include users of subprojects" do
1040 1058 user1 = User.generate!
1041 1059 user2 = User.generate!
1042 1060 project = Project.find(1)
1043 1061 Member.create!(:principal => user1, :project => project.children.visible.first, :role_ids => [1])
1044 1062
1045 1063 users = IssueQuery.new(:project => project).available_filters["assigned_to_id"]
1046 1064 assert_not_nil users
1047 1065 assert users[:values].map{|u|u[1]}.include?(user1.id.to_s)
1048 1066 assert !users[:values].map{|u|u[1]}.include?(user2.id.to_s)
1049 1067 end
1050 1068
1051 1069 test "#available_filters should include visible projects in cross-project view" do
1052 1070 projects = IssueQuery.new.available_filters["project_id"]
1053 1071 assert_not_nil projects
1054 1072 assert projects[:values].map{|u|u[1]}.include?("1")
1055 1073 end
1056 1074
1057 1075 test "#available_filters should include 'member_of_group' filter" do
1058 1076 query = IssueQuery.new
1059 1077 assert query.available_filters.keys.include?("member_of_group")
1060 1078 assert_equal :list_optional, query.available_filters["member_of_group"][:type]
1061 1079 assert query.available_filters["member_of_group"][:values].present?
1062 1080 assert_equal Group.all.sort.map {|g| [g.name, g.id.to_s]},
1063 1081 query.available_filters["member_of_group"][:values].sort
1064 1082 end
1065 1083
1066 1084 test "#available_filters should include 'assigned_to_role' filter" do
1067 1085 query = IssueQuery.new
1068 1086 assert query.available_filters.keys.include?("assigned_to_role")
1069 1087 assert_equal :list_optional, query.available_filters["assigned_to_role"][:type]
1070 1088
1071 1089 assert query.available_filters["assigned_to_role"][:values].include?(['Manager','1'])
1072 1090 assert query.available_filters["assigned_to_role"][:values].include?(['Developer','2'])
1073 1091 assert query.available_filters["assigned_to_role"][:values].include?(['Reporter','3'])
1074 1092
1075 1093 assert ! query.available_filters["assigned_to_role"][:values].include?(['Non member','4'])
1076 1094 assert ! query.available_filters["assigned_to_role"][:values].include?(['Anonymous','5'])
1077 1095 end
1078 1096
1079 1097 context "#statement" do
1080 1098 context "with 'member_of_group' filter" do
1081 1099 setup do
1082 1100 Group.destroy_all # No fixtures
1083 1101 @user_in_group = User.generate!
1084 1102 @second_user_in_group = User.generate!
1085 1103 @user_in_group2 = User.generate!
1086 1104 @user_not_in_group = User.generate!
1087 1105
1088 1106 @group = Group.generate!.reload
1089 1107 @group.users << @user_in_group
1090 1108 @group.users << @second_user_in_group
1091 1109
1092 1110 @group2 = Group.generate!.reload
1093 1111 @group2.users << @user_in_group2
1094 1112
1095 1113 end
1096 1114
1097 1115 should "search assigned to for users in the group" do
1098 1116 @query = IssueQuery.new(:name => '_')
1099 1117 @query.add_filter('member_of_group', '=', [@group.id.to_s])
1100 1118
1101 1119 assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@user_in_group.id}','#{@second_user_in_group.id}','#{@group.id}')"
1102 1120 assert_find_issues_with_query_is_successful @query
1103 1121 end
1104 1122
1105 1123 should "search not assigned to any group member (none)" do
1106 1124 @query = IssueQuery.new(:name => '_')
1107 1125 @query.add_filter('member_of_group', '!*', [''])
1108 1126
1109 1127 # Users not in a group
1110 1128 assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IS NULL OR #{Issue.table_name}.assigned_to_id NOT IN ('#{@user_in_group.id}','#{@second_user_in_group.id}','#{@user_in_group2.id}','#{@group.id}','#{@group2.id}')"
1111 1129 assert_find_issues_with_query_is_successful @query
1112 1130 end
1113 1131
1114 1132 should "search assigned to any group member (all)" do
1115 1133 @query = IssueQuery.new(:name => '_')
1116 1134 @query.add_filter('member_of_group', '*', [''])
1117 1135
1118 1136 # Only users in a group
1119 1137 assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@user_in_group.id}','#{@second_user_in_group.id}','#{@user_in_group2.id}','#{@group.id}','#{@group2.id}')"
1120 1138 assert_find_issues_with_query_is_successful @query
1121 1139 end
1122 1140
1123 1141 should "return an empty set with = empty group" do
1124 1142 @empty_group = Group.generate!
1125 1143 @query = IssueQuery.new(:name => '_')
1126 1144 @query.add_filter('member_of_group', '=', [@empty_group.id.to_s])
1127 1145
1128 1146 assert_equal [], find_issues_with_query(@query)
1129 1147 end
1130 1148
1131 1149 should "return issues with ! empty group" do
1132 1150 @empty_group = Group.generate!
1133 1151 @query = IssueQuery.new(:name => '_')
1134 1152 @query.add_filter('member_of_group', '!', [@empty_group.id.to_s])
1135 1153
1136 1154 assert_find_issues_with_query_is_successful @query
1137 1155 end
1138 1156 end
1139 1157
1140 1158 context "with 'assigned_to_role' filter" do
1141 1159 setup do
1142 1160 @manager_role = Role.find_by_name('Manager')
1143 1161 @developer_role = Role.find_by_name('Developer')
1144 1162
1145 1163 @project = Project.generate!
1146 1164 @manager = User.generate!
1147 1165 @developer = User.generate!
1148 1166 @boss = User.generate!
1149 1167 @guest = User.generate!
1150 1168 User.add_to_project(@manager, @project, @manager_role)
1151 1169 User.add_to_project(@developer, @project, @developer_role)
1152 1170 User.add_to_project(@boss, @project, [@manager_role, @developer_role])
1153 1171
1154 1172 @issue1 = Issue.generate!(:project => @project, :assigned_to_id => @manager.id)
1155 1173 @issue2 = Issue.generate!(:project => @project, :assigned_to_id => @developer.id)
1156 1174 @issue3 = Issue.generate!(:project => @project, :assigned_to_id => @boss.id)
1157 1175 @issue4 = Issue.generate!(:project => @project, :assigned_to_id => @guest.id)
1158 1176 @issue5 = Issue.generate!(:project => @project)
1159 1177 end
1160 1178
1161 1179 should "search assigned to for users with the Role" do
1162 1180 @query = IssueQuery.new(:name => '_', :project => @project)
1163 1181 @query.add_filter('assigned_to_role', '=', [@manager_role.id.to_s])
1164 1182
1165 1183 assert_query_result [@issue1, @issue3], @query
1166 1184 end
1167 1185
1168 1186 should "search assigned to for users with the Role on the issue project" do
1169 1187 other_project = Project.generate!
1170 1188 User.add_to_project(@developer, other_project, @manager_role)
1171 1189
1172 1190 @query = IssueQuery.new(:name => '_', :project => @project)
1173 1191 @query.add_filter('assigned_to_role', '=', [@manager_role.id.to_s])
1174 1192
1175 1193 assert_query_result [@issue1, @issue3], @query
1176 1194 end
1177 1195
1178 1196 should "return an empty set with empty role" do
1179 1197 @empty_role = Role.generate!
1180 1198 @query = IssueQuery.new(:name => '_', :project => @project)
1181 1199 @query.add_filter('assigned_to_role', '=', [@empty_role.id.to_s])
1182 1200
1183 1201 assert_query_result [], @query
1184 1202 end
1185 1203
1186 1204 should "search assigned to for users without the Role" do
1187 1205 @query = IssueQuery.new(:name => '_', :project => @project)
1188 1206 @query.add_filter('assigned_to_role', '!', [@manager_role.id.to_s])
1189 1207
1190 1208 assert_query_result [@issue2, @issue4, @issue5], @query
1191 1209 end
1192 1210
1193 1211 should "search assigned to for users not assigned to any Role (none)" do
1194 1212 @query = IssueQuery.new(:name => '_', :project => @project)
1195 1213 @query.add_filter('assigned_to_role', '!*', [''])
1196 1214
1197 1215 assert_query_result [@issue4, @issue5], @query
1198 1216 end
1199 1217
1200 1218 should "search assigned to for users assigned to any Role (all)" do
1201 1219 @query = IssueQuery.new(:name => '_', :project => @project)
1202 1220 @query.add_filter('assigned_to_role', '*', [''])
1203 1221
1204 1222 assert_query_result [@issue1, @issue2, @issue3], @query
1205 1223 end
1206 1224
1207 1225 should "return issues with ! empty role" do
1208 1226 @empty_role = Role.generate!
1209 1227 @query = IssueQuery.new(:name => '_', :project => @project)
1210 1228 @query.add_filter('assigned_to_role', '!', [@empty_role.id.to_s])
1211 1229
1212 1230 assert_query_result [@issue1, @issue2, @issue3, @issue4, @issue5], @query
1213 1231 end
1214 1232 end
1215 1233 end
1216 1234 end
General Comments 0
You need to be logged in to leave comments. Login now