##// END OF EJS Templates
Restored project name as version prefix when different (#19348)....
Jean-Philippe Lang -
r13726:5c43b4860e75
parent child
Show More
@@ -1,1321 +1,1321
1 # encoding: utf-8
1 # encoding: utf-8
2 #
2 #
3 # Redmine - project management software
3 # Redmine - project management software
4 # Copyright (C) 2006-2015 Jean-Philippe Lang
4 # Copyright (C) 2006-2015 Jean-Philippe Lang
5 #
5 #
6 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
9 # of the License, or (at your option) any later version.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
20 require 'forwardable'
20 require 'forwardable'
21 require 'cgi'
21 require 'cgi'
22
22
23 module ApplicationHelper
23 module ApplicationHelper
24 include Redmine::WikiFormatting::Macros::Definitions
24 include Redmine::WikiFormatting::Macros::Definitions
25 include Redmine::I18n
25 include Redmine::I18n
26 include GravatarHelper::PublicMethods
26 include GravatarHelper::PublicMethods
27 include Redmine::Pagination::Helper
27 include Redmine::Pagination::Helper
28
28
29 extend Forwardable
29 extend Forwardable
30 def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
30 def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
31
31
32 # Return true if user is authorized for controller/action, otherwise false
32 # Return true if user is authorized for controller/action, otherwise false
33 def authorize_for(controller, action)
33 def authorize_for(controller, action)
34 User.current.allowed_to?({:controller => controller, :action => action}, @project)
34 User.current.allowed_to?({:controller => controller, :action => action}, @project)
35 end
35 end
36
36
37 # Display a link if user is authorized
37 # Display a link if user is authorized
38 #
38 #
39 # @param [String] name Anchor text (passed to link_to)
39 # @param [String] name Anchor text (passed to link_to)
40 # @param [Hash] options Hash params. This will checked by authorize_for to see if the user is authorized
40 # @param [Hash] options Hash params. This will checked by authorize_for to see if the user is authorized
41 # @param [optional, Hash] html_options Options passed to link_to
41 # @param [optional, Hash] html_options Options passed to link_to
42 # @param [optional, Hash] parameters_for_method_reference Extra parameters for link_to
42 # @param [optional, Hash] parameters_for_method_reference Extra parameters for link_to
43 def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
43 def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
44 link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
44 link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
45 end
45 end
46
46
47 # Displays a link to user's account page if active
47 # Displays a link to user's account page if active
48 def link_to_user(user, options={})
48 def link_to_user(user, options={})
49 if user.is_a?(User)
49 if user.is_a?(User)
50 name = h(user.name(options[:format]))
50 name = h(user.name(options[:format]))
51 if user.active? || (User.current.admin? && user.logged?)
51 if user.active? || (User.current.admin? && user.logged?)
52 link_to name, user_path(user), :class => user.css_classes
52 link_to name, user_path(user), :class => user.css_classes
53 else
53 else
54 name
54 name
55 end
55 end
56 else
56 else
57 h(user.to_s)
57 h(user.to_s)
58 end
58 end
59 end
59 end
60
60
61 # Displays a link to +issue+ with its subject.
61 # Displays a link to +issue+ with its subject.
62 # Examples:
62 # Examples:
63 #
63 #
64 # link_to_issue(issue) # => Defect #6: This is the subject
64 # link_to_issue(issue) # => Defect #6: This is the subject
65 # link_to_issue(issue, :truncate => 6) # => Defect #6: This i...
65 # link_to_issue(issue, :truncate => 6) # => Defect #6: This i...
66 # link_to_issue(issue, :subject => false) # => Defect #6
66 # link_to_issue(issue, :subject => false) # => Defect #6
67 # link_to_issue(issue, :project => true) # => Foo - Defect #6
67 # link_to_issue(issue, :project => true) # => Foo - Defect #6
68 # link_to_issue(issue, :subject => false, :tracker => false) # => #6
68 # link_to_issue(issue, :subject => false, :tracker => false) # => #6
69 #
69 #
70 def link_to_issue(issue, options={})
70 def link_to_issue(issue, options={})
71 title = nil
71 title = nil
72 subject = nil
72 subject = nil
73 text = options[:tracker] == false ? "##{issue.id}" : "#{issue.tracker} ##{issue.id}"
73 text = options[:tracker] == false ? "##{issue.id}" : "#{issue.tracker} ##{issue.id}"
74 if options[:subject] == false
74 if options[:subject] == false
75 title = issue.subject.truncate(60)
75 title = issue.subject.truncate(60)
76 else
76 else
77 subject = issue.subject
77 subject = issue.subject
78 if truncate_length = options[:truncate]
78 if truncate_length = options[:truncate]
79 subject = subject.truncate(truncate_length)
79 subject = subject.truncate(truncate_length)
80 end
80 end
81 end
81 end
82 only_path = options[:only_path].nil? ? true : options[:only_path]
82 only_path = options[:only_path].nil? ? true : options[:only_path]
83 s = link_to(text, issue_url(issue, :only_path => only_path),
83 s = link_to(text, issue_url(issue, :only_path => only_path),
84 :class => issue.css_classes, :title => title)
84 :class => issue.css_classes, :title => title)
85 s << h(": #{subject}") if subject
85 s << h(": #{subject}") if subject
86 s = h("#{issue.project} - ") + s if options[:project]
86 s = h("#{issue.project} - ") + s if options[:project]
87 s
87 s
88 end
88 end
89
89
90 # Generates a link to an attachment.
90 # Generates a link to an attachment.
91 # Options:
91 # Options:
92 # * :text - Link text (default to attachment filename)
92 # * :text - Link text (default to attachment filename)
93 # * :download - Force download (default: false)
93 # * :download - Force download (default: false)
94 def link_to_attachment(attachment, options={})
94 def link_to_attachment(attachment, options={})
95 text = options.delete(:text) || attachment.filename
95 text = options.delete(:text) || attachment.filename
96 route_method = options.delete(:download) ? :download_named_attachment_url : :named_attachment_url
96 route_method = options.delete(:download) ? :download_named_attachment_url : :named_attachment_url
97 html_options = options.slice!(:only_path)
97 html_options = options.slice!(:only_path)
98 options[:only_path] = true unless options.key?(:only_path)
98 options[:only_path] = true unless options.key?(:only_path)
99 url = send(route_method, attachment, attachment.filename, options)
99 url = send(route_method, attachment, attachment.filename, options)
100 link_to text, url, html_options
100 link_to text, url, html_options
101 end
101 end
102
102
103 # Generates a link to a SCM revision
103 # Generates a link to a SCM revision
104 # Options:
104 # Options:
105 # * :text - Link text (default to the formatted revision)
105 # * :text - Link text (default to the formatted revision)
106 def link_to_revision(revision, repository, options={})
106 def link_to_revision(revision, repository, options={})
107 if repository.is_a?(Project)
107 if repository.is_a?(Project)
108 repository = repository.repository
108 repository = repository.repository
109 end
109 end
110 text = options.delete(:text) || format_revision(revision)
110 text = options.delete(:text) || format_revision(revision)
111 rev = revision.respond_to?(:identifier) ? revision.identifier : revision
111 rev = revision.respond_to?(:identifier) ? revision.identifier : revision
112 link_to(
112 link_to(
113 h(text),
113 h(text),
114 {:controller => 'repositories', :action => 'revision', :id => repository.project, :repository_id => repository.identifier_param, :rev => rev},
114 {:controller => 'repositories', :action => 'revision', :id => repository.project, :repository_id => repository.identifier_param, :rev => rev},
115 :title => l(:label_revision_id, format_revision(revision)),
115 :title => l(:label_revision_id, format_revision(revision)),
116 :accesskey => options[:accesskey]
116 :accesskey => options[:accesskey]
117 )
117 )
118 end
118 end
119
119
120 # Generates a link to a message
120 # Generates a link to a message
121 def link_to_message(message, options={}, html_options = nil)
121 def link_to_message(message, options={}, html_options = nil)
122 link_to(
122 link_to(
123 message.subject.truncate(60),
123 message.subject.truncate(60),
124 board_message_url(message.board_id, message.parent_id || message.id, {
124 board_message_url(message.board_id, message.parent_id || message.id, {
125 :r => (message.parent_id && message.id),
125 :r => (message.parent_id && message.id),
126 :anchor => (message.parent_id ? "message-#{message.id}" : nil),
126 :anchor => (message.parent_id ? "message-#{message.id}" : nil),
127 :only_path => true
127 :only_path => true
128 }.merge(options)),
128 }.merge(options)),
129 html_options
129 html_options
130 )
130 )
131 end
131 end
132
132
133 # Generates a link to a project if active
133 # Generates a link to a project if active
134 # Examples:
134 # Examples:
135 #
135 #
136 # link_to_project(project) # => link to the specified project overview
136 # link_to_project(project) # => link to the specified project overview
137 # link_to_project(project, {:only_path => false}, :class => "project") # => 3rd arg adds html options
137 # link_to_project(project, {:only_path => false}, :class => "project") # => 3rd arg adds html options
138 # link_to_project(project, {}, :class => "project") # => html options with default url (project overview)
138 # link_to_project(project, {}, :class => "project") # => html options with default url (project overview)
139 #
139 #
140 def link_to_project(project, options={}, html_options = nil)
140 def link_to_project(project, options={}, html_options = nil)
141 if project.archived?
141 if project.archived?
142 h(project.name)
142 h(project.name)
143 else
143 else
144 link_to project.name,
144 link_to project.name,
145 project_url(project, {:only_path => true}.merge(options)),
145 project_url(project, {:only_path => true}.merge(options)),
146 html_options
146 html_options
147 end
147 end
148 end
148 end
149
149
150 # Generates a link to a project settings if active
150 # Generates a link to a project settings if active
151 def link_to_project_settings(project, options={}, html_options=nil)
151 def link_to_project_settings(project, options={}, html_options=nil)
152 if project.active?
152 if project.active?
153 link_to project.name, settings_project_path(project, options), html_options
153 link_to project.name, settings_project_path(project, options), html_options
154 elsif project.archived?
154 elsif project.archived?
155 h(project.name)
155 h(project.name)
156 else
156 else
157 link_to project.name, project_path(project, options), html_options
157 link_to project.name, project_path(project, options), html_options
158 end
158 end
159 end
159 end
160
160
161 # Generates a link to a version
161 # Generates a link to a version
162 def link_to_version(version, options = {})
162 def link_to_version(version, options = {})
163 return '' unless version && version.is_a?(Version)
163 return '' unless version && version.is_a?(Version)
164 options = {:title => format_date(version.effective_date)}.merge(options)
164 options = {:title => format_date(version.effective_date)}.merge(options)
165 link_to_if version.visible?, format_version_name(version), version_path(version), options
165 link_to_if version.visible?, format_version_name(version), version_path(version), options
166 end
166 end
167
167
168 # Helper that formats object for html or text rendering
168 # Helper that formats object for html or text rendering
169 def format_object(object, html=true, &block)
169 def format_object(object, html=true, &block)
170 if block_given?
170 if block_given?
171 object = yield object
171 object = yield object
172 end
172 end
173 case object.class.name
173 case object.class.name
174 when 'Array'
174 when 'Array'
175 object.map {|o| format_object(o, html)}.join(', ').html_safe
175 object.map {|o| format_object(o, html)}.join(', ').html_safe
176 when 'Time'
176 when 'Time'
177 format_time(object)
177 format_time(object)
178 when 'Date'
178 when 'Date'
179 format_date(object)
179 format_date(object)
180 when 'Fixnum'
180 when 'Fixnum'
181 object.to_s
181 object.to_s
182 when 'Float'
182 when 'Float'
183 sprintf "%.2f", object
183 sprintf "%.2f", object
184 when 'User'
184 when 'User'
185 html ? link_to_user(object) : object.to_s
185 html ? link_to_user(object) : object.to_s
186 when 'Project'
186 when 'Project'
187 html ? link_to_project(object) : object.to_s
187 html ? link_to_project(object) : object.to_s
188 when 'Version'
188 when 'Version'
189 html ? link_to_version(object) : object.to_s
189 html ? link_to_version(object) : object.to_s
190 when 'TrueClass'
190 when 'TrueClass'
191 l(:general_text_Yes)
191 l(:general_text_Yes)
192 when 'FalseClass'
192 when 'FalseClass'
193 l(:general_text_No)
193 l(:general_text_No)
194 when 'Issue'
194 when 'Issue'
195 object.visible? && html ? link_to_issue(object) : "##{object.id}"
195 object.visible? && html ? link_to_issue(object) : "##{object.id}"
196 when 'CustomValue', 'CustomFieldValue'
196 when 'CustomValue', 'CustomFieldValue'
197 if object.custom_field
197 if object.custom_field
198 f = object.custom_field.format.formatted_custom_value(self, object, html)
198 f = object.custom_field.format.formatted_custom_value(self, object, html)
199 if f.nil? || f.is_a?(String)
199 if f.nil? || f.is_a?(String)
200 f
200 f
201 else
201 else
202 format_object(f, html, &block)
202 format_object(f, html, &block)
203 end
203 end
204 else
204 else
205 object.value.to_s
205 object.value.to_s
206 end
206 end
207 else
207 else
208 html ? h(object) : object.to_s
208 html ? h(object) : object.to_s
209 end
209 end
210 end
210 end
211
211
212 def wiki_page_path(page, options={})
212 def wiki_page_path(page, options={})
213 url_for({:controller => 'wiki', :action => 'show', :project_id => page.project, :id => page.title}.merge(options))
213 url_for({:controller => 'wiki', :action => 'show', :project_id => page.project, :id => page.title}.merge(options))
214 end
214 end
215
215
216 def thumbnail_tag(attachment)
216 def thumbnail_tag(attachment)
217 link_to image_tag(thumbnail_path(attachment)),
217 link_to image_tag(thumbnail_path(attachment)),
218 named_attachment_path(attachment, attachment.filename),
218 named_attachment_path(attachment, attachment.filename),
219 :title => attachment.filename
219 :title => attachment.filename
220 end
220 end
221
221
222 def toggle_link(name, id, options={})
222 def toggle_link(name, id, options={})
223 onclick = "$('##{id}').toggle(); "
223 onclick = "$('##{id}').toggle(); "
224 onclick << (options[:focus] ? "$('##{options[:focus]}').focus(); " : "this.blur(); ")
224 onclick << (options[:focus] ? "$('##{options[:focus]}').focus(); " : "this.blur(); ")
225 onclick << "return false;"
225 onclick << "return false;"
226 link_to(name, "#", :onclick => onclick)
226 link_to(name, "#", :onclick => onclick)
227 end
227 end
228
228
229 def format_activity_title(text)
229 def format_activity_title(text)
230 h(truncate_single_line_raw(text, 100))
230 h(truncate_single_line_raw(text, 100))
231 end
231 end
232
232
233 def format_activity_day(date)
233 def format_activity_day(date)
234 date == User.current.today ? l(:label_today).titleize : format_date(date)
234 date == User.current.today ? l(:label_today).titleize : format_date(date)
235 end
235 end
236
236
237 def format_activity_description(text)
237 def format_activity_description(text)
238 h(text.to_s.truncate(120).gsub(%r{[\r\n]*<(pre|code)>.*$}m, '...')
238 h(text.to_s.truncate(120).gsub(%r{[\r\n]*<(pre|code)>.*$}m, '...')
239 ).gsub(/[\r\n]+/, "<br />").html_safe
239 ).gsub(/[\r\n]+/, "<br />").html_safe
240 end
240 end
241
241
242 def format_version_name(version)
242 def format_version_name(version)
243 if !version.shared? || version.project == @project
243 if version.project == @project
244 h(version)
244 h(version)
245 else
245 else
246 h("#{version.project} - #{version}")
246 h("#{version.project} - #{version}")
247 end
247 end
248 end
248 end
249
249
250 def due_date_distance_in_words(date)
250 def due_date_distance_in_words(date)
251 if date
251 if date
252 l((date < Date.today ? :label_roadmap_overdue : :label_roadmap_due_in), distance_of_date_in_words(Date.today, date))
252 l((date < Date.today ? :label_roadmap_overdue : :label_roadmap_due_in), distance_of_date_in_words(Date.today, date))
253 end
253 end
254 end
254 end
255
255
256 # Renders a tree of projects as a nested set of unordered lists
256 # Renders a tree of projects as a nested set of unordered lists
257 # The given collection may be a subset of the whole project tree
257 # The given collection may be a subset of the whole project tree
258 # (eg. some intermediate nodes are private and can not be seen)
258 # (eg. some intermediate nodes are private and can not be seen)
259 def render_project_nested_lists(projects, &block)
259 def render_project_nested_lists(projects, &block)
260 s = ''
260 s = ''
261 if projects.any?
261 if projects.any?
262 ancestors = []
262 ancestors = []
263 original_project = @project
263 original_project = @project
264 projects.sort_by(&:lft).each do |project|
264 projects.sort_by(&:lft).each do |project|
265 # set the project environment to please macros.
265 # set the project environment to please macros.
266 @project = project
266 @project = project
267 if (ancestors.empty? || project.is_descendant_of?(ancestors.last))
267 if (ancestors.empty? || project.is_descendant_of?(ancestors.last))
268 s << "<ul class='projects #{ ancestors.empty? ? 'root' : nil}'>\n"
268 s << "<ul class='projects #{ ancestors.empty? ? 'root' : nil}'>\n"
269 else
269 else
270 ancestors.pop
270 ancestors.pop
271 s << "</li>"
271 s << "</li>"
272 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
272 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
273 ancestors.pop
273 ancestors.pop
274 s << "</ul></li>\n"
274 s << "</ul></li>\n"
275 end
275 end
276 end
276 end
277 classes = (ancestors.empty? ? 'root' : 'child')
277 classes = (ancestors.empty? ? 'root' : 'child')
278 s << "<li class='#{classes}'><div class='#{classes}'>"
278 s << "<li class='#{classes}'><div class='#{classes}'>"
279 s << h(block_given? ? capture(project, &block) : project.name)
279 s << h(block_given? ? capture(project, &block) : project.name)
280 s << "</div>\n"
280 s << "</div>\n"
281 ancestors << project
281 ancestors << project
282 end
282 end
283 s << ("</li></ul>\n" * ancestors.size)
283 s << ("</li></ul>\n" * ancestors.size)
284 @project = original_project
284 @project = original_project
285 end
285 end
286 s.html_safe
286 s.html_safe
287 end
287 end
288
288
289 def render_page_hierarchy(pages, node=nil, options={})
289 def render_page_hierarchy(pages, node=nil, options={})
290 content = ''
290 content = ''
291 if pages[node]
291 if pages[node]
292 content << "<ul class=\"pages-hierarchy\">\n"
292 content << "<ul class=\"pages-hierarchy\">\n"
293 pages[node].each do |page|
293 pages[node].each do |page|
294 content << "<li>"
294 content << "<li>"
295 content << link_to(h(page.pretty_title), {:controller => 'wiki', :action => 'show', :project_id => page.project, :id => page.title, :version => nil},
295 content << link_to(h(page.pretty_title), {:controller => 'wiki', :action => 'show', :project_id => page.project, :id => page.title, :version => nil},
296 :title => (options[:timestamp] && page.updated_on ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil))
296 :title => (options[:timestamp] && page.updated_on ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil))
297 content << "\n" + render_page_hierarchy(pages, page.id, options) if pages[page.id]
297 content << "\n" + render_page_hierarchy(pages, page.id, options) if pages[page.id]
298 content << "</li>\n"
298 content << "</li>\n"
299 end
299 end
300 content << "</ul>\n"
300 content << "</ul>\n"
301 end
301 end
302 content.html_safe
302 content.html_safe
303 end
303 end
304
304
305 # Renders flash messages
305 # Renders flash messages
306 def render_flash_messages
306 def render_flash_messages
307 s = ''
307 s = ''
308 flash.each do |k,v|
308 flash.each do |k,v|
309 s << content_tag('div', v.html_safe, :class => "flash #{k}", :id => "flash_#{k}")
309 s << content_tag('div', v.html_safe, :class => "flash #{k}", :id => "flash_#{k}")
310 end
310 end
311 s.html_safe
311 s.html_safe
312 end
312 end
313
313
314 # Renders tabs and their content
314 # Renders tabs and their content
315 def render_tabs(tabs, selected=params[:tab])
315 def render_tabs(tabs, selected=params[:tab])
316 if tabs.any?
316 if tabs.any?
317 unless tabs.detect {|tab| tab[:name] == selected}
317 unless tabs.detect {|tab| tab[:name] == selected}
318 selected = nil
318 selected = nil
319 end
319 end
320 selected ||= tabs.first[:name]
320 selected ||= tabs.first[:name]
321 render :partial => 'common/tabs', :locals => {:tabs => tabs, :selected_tab => selected}
321 render :partial => 'common/tabs', :locals => {:tabs => tabs, :selected_tab => selected}
322 else
322 else
323 content_tag 'p', l(:label_no_data), :class => "nodata"
323 content_tag 'p', l(:label_no_data), :class => "nodata"
324 end
324 end
325 end
325 end
326
326
327 # Renders the project quick-jump box
327 # Renders the project quick-jump box
328 def render_project_jump_box
328 def render_project_jump_box
329 return unless User.current.logged?
329 return unless User.current.logged?
330 projects = User.current.projects.active.select(:id, :name, :identifier, :lft, :rgt).to_a
330 projects = User.current.projects.active.select(:id, :name, :identifier, :lft, :rgt).to_a
331 if projects.any?
331 if projects.any?
332 options =
332 options =
333 ("<option value=''>#{ l(:label_jump_to_a_project) }</option>" +
333 ("<option value=''>#{ l(:label_jump_to_a_project) }</option>" +
334 '<option value="" disabled="disabled">---</option>').html_safe
334 '<option value="" disabled="disabled">---</option>').html_safe
335
335
336 options << project_tree_options_for_select(projects, :selected => @project) do |p|
336 options << project_tree_options_for_select(projects, :selected => @project) do |p|
337 { :value => project_path(:id => p, :jump => current_menu_item) }
337 { :value => project_path(:id => p, :jump => current_menu_item) }
338 end
338 end
339
339
340 select_tag('project_quick_jump_box', options, :onchange => 'if (this.value != \'\') { window.location = this.value; }')
340 select_tag('project_quick_jump_box', options, :onchange => 'if (this.value != \'\') { window.location = this.value; }')
341 end
341 end
342 end
342 end
343
343
344 def project_tree_options_for_select(projects, options = {})
344 def project_tree_options_for_select(projects, options = {})
345 s = ''.html_safe
345 s = ''.html_safe
346 if blank_text = options[:include_blank]
346 if blank_text = options[:include_blank]
347 if blank_text == true
347 if blank_text == true
348 blank_text = '&nbsp;'.html_safe
348 blank_text = '&nbsp;'.html_safe
349 end
349 end
350 s << content_tag('option', blank_text, :value => '')
350 s << content_tag('option', blank_text, :value => '')
351 end
351 end
352 project_tree(projects) do |project, level|
352 project_tree(projects) do |project, level|
353 name_prefix = (level > 0 ? '&nbsp;' * 2 * level + '&#187; ' : '').html_safe
353 name_prefix = (level > 0 ? '&nbsp;' * 2 * level + '&#187; ' : '').html_safe
354 tag_options = {:value => project.id}
354 tag_options = {:value => project.id}
355 if project == options[:selected] || (options[:selected].respond_to?(:include?) && options[:selected].include?(project))
355 if project == options[:selected] || (options[:selected].respond_to?(:include?) && options[:selected].include?(project))
356 tag_options[:selected] = 'selected'
356 tag_options[:selected] = 'selected'
357 else
357 else
358 tag_options[:selected] = nil
358 tag_options[:selected] = nil
359 end
359 end
360 tag_options.merge!(yield(project)) if block_given?
360 tag_options.merge!(yield(project)) if block_given?
361 s << content_tag('option', name_prefix + h(project), tag_options)
361 s << content_tag('option', name_prefix + h(project), tag_options)
362 end
362 end
363 s.html_safe
363 s.html_safe
364 end
364 end
365
365
366 # Yields the given block for each project with its level in the tree
366 # Yields the given block for each project with its level in the tree
367 #
367 #
368 # Wrapper for Project#project_tree
368 # Wrapper for Project#project_tree
369 def project_tree(projects, &block)
369 def project_tree(projects, &block)
370 Project.project_tree(projects, &block)
370 Project.project_tree(projects, &block)
371 end
371 end
372
372
373 def principals_check_box_tags(name, principals)
373 def principals_check_box_tags(name, principals)
374 s = ''
374 s = ''
375 principals.each do |principal|
375 principals.each do |principal|
376 s << "<label>#{ check_box_tag name, principal.id, false, :id => nil } #{h principal}</label>\n"
376 s << "<label>#{ check_box_tag name, principal.id, false, :id => nil } #{h principal}</label>\n"
377 end
377 end
378 s.html_safe
378 s.html_safe
379 end
379 end
380
380
381 # Returns a string for users/groups option tags
381 # Returns a string for users/groups option tags
382 def principals_options_for_select(collection, selected=nil)
382 def principals_options_for_select(collection, selected=nil)
383 s = ''
383 s = ''
384 if collection.include?(User.current)
384 if collection.include?(User.current)
385 s << content_tag('option', "<< #{l(:label_me)} >>", :value => User.current.id)
385 s << content_tag('option', "<< #{l(:label_me)} >>", :value => User.current.id)
386 end
386 end
387 groups = ''
387 groups = ''
388 collection.sort.each do |element|
388 collection.sort.each do |element|
389 selected_attribute = ' selected="selected"' if option_value_selected?(element, selected) || element.id.to_s == selected
389 selected_attribute = ' selected="selected"' if option_value_selected?(element, selected) || element.id.to_s == selected
390 (element.is_a?(Group) ? groups : s) << %(<option value="#{element.id}"#{selected_attribute}>#{h element.name}</option>)
390 (element.is_a?(Group) ? groups : s) << %(<option value="#{element.id}"#{selected_attribute}>#{h element.name}</option>)
391 end
391 end
392 unless groups.empty?
392 unless groups.empty?
393 s << %(<optgroup label="#{h(l(:label_group_plural))}">#{groups}</optgroup>)
393 s << %(<optgroup label="#{h(l(:label_group_plural))}">#{groups}</optgroup>)
394 end
394 end
395 s.html_safe
395 s.html_safe
396 end
396 end
397
397
398 def option_tag(name, text, value, selected=nil, options={})
398 def option_tag(name, text, value, selected=nil, options={})
399 content_tag 'option', value, options.merge(:value => value, :selected => (value == selected))
399 content_tag 'option', value, options.merge(:value => value, :selected => (value == selected))
400 end
400 end
401
401
402 def truncate_single_line_raw(string, length)
402 def truncate_single_line_raw(string, length)
403 string.to_s.truncate(length).gsub(%r{[\r\n]+}m, ' ')
403 string.to_s.truncate(length).gsub(%r{[\r\n]+}m, ' ')
404 end
404 end
405
405
406 # Truncates at line break after 250 characters or options[:length]
406 # Truncates at line break after 250 characters or options[:length]
407 def truncate_lines(string, options={})
407 def truncate_lines(string, options={})
408 length = options[:length] || 250
408 length = options[:length] || 250
409 if string.to_s =~ /\A(.{#{length}}.*?)$/m
409 if string.to_s =~ /\A(.{#{length}}.*?)$/m
410 "#{$1}..."
410 "#{$1}..."
411 else
411 else
412 string
412 string
413 end
413 end
414 end
414 end
415
415
416 def anchor(text)
416 def anchor(text)
417 text.to_s.gsub(' ', '_')
417 text.to_s.gsub(' ', '_')
418 end
418 end
419
419
420 def html_hours(text)
420 def html_hours(text)
421 text.gsub(%r{(\d+)\.(\d+)}, '<span class="hours hours-int">\1</span><span class="hours hours-dec">.\2</span>').html_safe
421 text.gsub(%r{(\d+)\.(\d+)}, '<span class="hours hours-int">\1</span><span class="hours hours-dec">.\2</span>').html_safe
422 end
422 end
423
423
424 def authoring(created, author, options={})
424 def authoring(created, author, options={})
425 l(options[:label] || :label_added_time_by, :author => link_to_user(author), :age => time_tag(created)).html_safe
425 l(options[:label] || :label_added_time_by, :author => link_to_user(author), :age => time_tag(created)).html_safe
426 end
426 end
427
427
428 def time_tag(time)
428 def time_tag(time)
429 text = distance_of_time_in_words(Time.now, time)
429 text = distance_of_time_in_words(Time.now, time)
430 if @project
430 if @project
431 link_to(text, project_activity_path(@project, :from => User.current.time_to_date(time)), :title => format_time(time))
431 link_to(text, project_activity_path(@project, :from => User.current.time_to_date(time)), :title => format_time(time))
432 else
432 else
433 content_tag('abbr', text, :title => format_time(time))
433 content_tag('abbr', text, :title => format_time(time))
434 end
434 end
435 end
435 end
436
436
437 def syntax_highlight_lines(name, content)
437 def syntax_highlight_lines(name, content)
438 lines = []
438 lines = []
439 syntax_highlight(name, content).each_line { |line| lines << line }
439 syntax_highlight(name, content).each_line { |line| lines << line }
440 lines
440 lines
441 end
441 end
442
442
443 def syntax_highlight(name, content)
443 def syntax_highlight(name, content)
444 Redmine::SyntaxHighlighting.highlight_by_filename(content, name)
444 Redmine::SyntaxHighlighting.highlight_by_filename(content, name)
445 end
445 end
446
446
447 def to_path_param(path)
447 def to_path_param(path)
448 str = path.to_s.split(%r{[/\\]}).select{|p| !p.blank?}.join("/")
448 str = path.to_s.split(%r{[/\\]}).select{|p| !p.blank?}.join("/")
449 str.blank? ? nil : str
449 str.blank? ? nil : str
450 end
450 end
451
451
452 def reorder_links(name, url, method = :post)
452 def reorder_links(name, url, method = :post)
453 link_to(image_tag('2uparrow.png', :alt => l(:label_sort_highest)),
453 link_to(image_tag('2uparrow.png', :alt => l(:label_sort_highest)),
454 url.merge({"#{name}[move_to]" => 'highest'}),
454 url.merge({"#{name}[move_to]" => 'highest'}),
455 :method => method, :title => l(:label_sort_highest)) +
455 :method => method, :title => l(:label_sort_highest)) +
456 link_to(image_tag('1uparrow.png', :alt => l(:label_sort_higher)),
456 link_to(image_tag('1uparrow.png', :alt => l(:label_sort_higher)),
457 url.merge({"#{name}[move_to]" => 'higher'}),
457 url.merge({"#{name}[move_to]" => 'higher'}),
458 :method => method, :title => l(:label_sort_higher)) +
458 :method => method, :title => l(:label_sort_higher)) +
459 link_to(image_tag('1downarrow.png', :alt => l(:label_sort_lower)),
459 link_to(image_tag('1downarrow.png', :alt => l(:label_sort_lower)),
460 url.merge({"#{name}[move_to]" => 'lower'}),
460 url.merge({"#{name}[move_to]" => 'lower'}),
461 :method => method, :title => l(:label_sort_lower)) +
461 :method => method, :title => l(:label_sort_lower)) +
462 link_to(image_tag('2downarrow.png', :alt => l(:label_sort_lowest)),
462 link_to(image_tag('2downarrow.png', :alt => l(:label_sort_lowest)),
463 url.merge({"#{name}[move_to]" => 'lowest'}),
463 url.merge({"#{name}[move_to]" => 'lowest'}),
464 :method => method, :title => l(:label_sort_lowest))
464 :method => method, :title => l(:label_sort_lowest))
465 end
465 end
466
466
467 def breadcrumb(*args)
467 def breadcrumb(*args)
468 elements = args.flatten
468 elements = args.flatten
469 elements.any? ? content_tag('p', (args.join(" \xc2\xbb ") + " \xc2\xbb ").html_safe, :class => 'breadcrumb') : nil
469 elements.any? ? content_tag('p', (args.join(" \xc2\xbb ") + " \xc2\xbb ").html_safe, :class => 'breadcrumb') : nil
470 end
470 end
471
471
472 def other_formats_links(&block)
472 def other_formats_links(&block)
473 concat('<p class="other-formats">'.html_safe + l(:label_export_to))
473 concat('<p class="other-formats">'.html_safe + l(:label_export_to))
474 yield Redmine::Views::OtherFormatsBuilder.new(self)
474 yield Redmine::Views::OtherFormatsBuilder.new(self)
475 concat('</p>'.html_safe)
475 concat('</p>'.html_safe)
476 end
476 end
477
477
478 def page_header_title
478 def page_header_title
479 if @project.nil? || @project.new_record?
479 if @project.nil? || @project.new_record?
480 h(Setting.app_title)
480 h(Setting.app_title)
481 else
481 else
482 b = []
482 b = []
483 ancestors = (@project.root? ? [] : @project.ancestors.visible.to_a)
483 ancestors = (@project.root? ? [] : @project.ancestors.visible.to_a)
484 if ancestors.any?
484 if ancestors.any?
485 root = ancestors.shift
485 root = ancestors.shift
486 b << link_to_project(root, {:jump => current_menu_item}, :class => 'root')
486 b << link_to_project(root, {:jump => current_menu_item}, :class => 'root')
487 if ancestors.size > 2
487 if ancestors.size > 2
488 b << "\xe2\x80\xa6"
488 b << "\xe2\x80\xa6"
489 ancestors = ancestors[-2, 2]
489 ancestors = ancestors[-2, 2]
490 end
490 end
491 b += ancestors.collect {|p| link_to_project(p, {:jump => current_menu_item}, :class => 'ancestor') }
491 b += ancestors.collect {|p| link_to_project(p, {:jump => current_menu_item}, :class => 'ancestor') }
492 end
492 end
493 b << h(@project)
493 b << h(@project)
494 b.join(" \xc2\xbb ").html_safe
494 b.join(" \xc2\xbb ").html_safe
495 end
495 end
496 end
496 end
497
497
498 # Returns a h2 tag and sets the html title with the given arguments
498 # Returns a h2 tag and sets the html title with the given arguments
499 def title(*args)
499 def title(*args)
500 strings = args.map do |arg|
500 strings = args.map do |arg|
501 if arg.is_a?(Array) && arg.size >= 2
501 if arg.is_a?(Array) && arg.size >= 2
502 link_to(*arg)
502 link_to(*arg)
503 else
503 else
504 h(arg.to_s)
504 h(arg.to_s)
505 end
505 end
506 end
506 end
507 html_title args.reverse.map {|s| (s.is_a?(Array) ? s.first : s).to_s}
507 html_title args.reverse.map {|s| (s.is_a?(Array) ? s.first : s).to_s}
508 content_tag('h2', strings.join(' &#187; ').html_safe)
508 content_tag('h2', strings.join(' &#187; ').html_safe)
509 end
509 end
510
510
511 # Sets the html title
511 # Sets the html title
512 # Returns the html title when called without arguments
512 # Returns the html title when called without arguments
513 # Current project name and app_title and automatically appended
513 # Current project name and app_title and automatically appended
514 # Exemples:
514 # Exemples:
515 # html_title 'Foo', 'Bar'
515 # html_title 'Foo', 'Bar'
516 # html_title # => 'Foo - Bar - My Project - Redmine'
516 # html_title # => 'Foo - Bar - My Project - Redmine'
517 def html_title(*args)
517 def html_title(*args)
518 if args.empty?
518 if args.empty?
519 title = @html_title || []
519 title = @html_title || []
520 title << @project.name if @project
520 title << @project.name if @project
521 title << Setting.app_title unless Setting.app_title == title.last
521 title << Setting.app_title unless Setting.app_title == title.last
522 title.reject(&:blank?).join(' - ')
522 title.reject(&:blank?).join(' - ')
523 else
523 else
524 @html_title ||= []
524 @html_title ||= []
525 @html_title += args
525 @html_title += args
526 end
526 end
527 end
527 end
528
528
529 # Returns the theme, controller name, and action as css classes for the
529 # Returns the theme, controller name, and action as css classes for the
530 # HTML body.
530 # HTML body.
531 def body_css_classes
531 def body_css_classes
532 css = []
532 css = []
533 if theme = Redmine::Themes.theme(Setting.ui_theme)
533 if theme = Redmine::Themes.theme(Setting.ui_theme)
534 css << 'theme-' + theme.name
534 css << 'theme-' + theme.name
535 end
535 end
536
536
537 css << 'project-' + @project.identifier if @project && @project.identifier.present?
537 css << 'project-' + @project.identifier if @project && @project.identifier.present?
538 css << 'controller-' + controller_name
538 css << 'controller-' + controller_name
539 css << 'action-' + action_name
539 css << 'action-' + action_name
540 css.join(' ')
540 css.join(' ')
541 end
541 end
542
542
543 def accesskey(s)
543 def accesskey(s)
544 @used_accesskeys ||= []
544 @used_accesskeys ||= []
545 key = Redmine::AccessKeys.key_for(s)
545 key = Redmine::AccessKeys.key_for(s)
546 return nil if @used_accesskeys.include?(key)
546 return nil if @used_accesskeys.include?(key)
547 @used_accesskeys << key
547 @used_accesskeys << key
548 key
548 key
549 end
549 end
550
550
551 # Formats text according to system settings.
551 # Formats text according to system settings.
552 # 2 ways to call this method:
552 # 2 ways to call this method:
553 # * with a String: textilizable(text, options)
553 # * with a String: textilizable(text, options)
554 # * with an object and one of its attribute: textilizable(issue, :description, options)
554 # * with an object and one of its attribute: textilizable(issue, :description, options)
555 def textilizable(*args)
555 def textilizable(*args)
556 options = args.last.is_a?(Hash) ? args.pop : {}
556 options = args.last.is_a?(Hash) ? args.pop : {}
557 case args.size
557 case args.size
558 when 1
558 when 1
559 obj = options[:object]
559 obj = options[:object]
560 text = args.shift
560 text = args.shift
561 when 2
561 when 2
562 obj = args.shift
562 obj = args.shift
563 attr = args.shift
563 attr = args.shift
564 text = obj.send(attr).to_s
564 text = obj.send(attr).to_s
565 else
565 else
566 raise ArgumentError, 'invalid arguments to textilizable'
566 raise ArgumentError, 'invalid arguments to textilizable'
567 end
567 end
568 return '' if text.blank?
568 return '' if text.blank?
569 project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
569 project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
570 @only_path = only_path = options.delete(:only_path) == false ? false : true
570 @only_path = only_path = options.delete(:only_path) == false ? false : true
571
571
572 text = text.dup
572 text = text.dup
573 macros = catch_macros(text)
573 macros = catch_macros(text)
574 text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr)
574 text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr)
575
575
576 @parsed_headings = []
576 @parsed_headings = []
577 @heading_anchors = {}
577 @heading_anchors = {}
578 @current_section = 0 if options[:edit_section_links]
578 @current_section = 0 if options[:edit_section_links]
579
579
580 parse_sections(text, project, obj, attr, only_path, options)
580 parse_sections(text, project, obj, attr, only_path, options)
581 text = parse_non_pre_blocks(text, obj, macros) do |text|
581 text = parse_non_pre_blocks(text, obj, macros) do |text|
582 [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links].each do |method_name|
582 [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links].each do |method_name|
583 send method_name, text, project, obj, attr, only_path, options
583 send method_name, text, project, obj, attr, only_path, options
584 end
584 end
585 end
585 end
586 parse_headings(text, project, obj, attr, only_path, options)
586 parse_headings(text, project, obj, attr, only_path, options)
587
587
588 if @parsed_headings.any?
588 if @parsed_headings.any?
589 replace_toc(text, @parsed_headings)
589 replace_toc(text, @parsed_headings)
590 end
590 end
591
591
592 text.html_safe
592 text.html_safe
593 end
593 end
594
594
595 def parse_non_pre_blocks(text, obj, macros)
595 def parse_non_pre_blocks(text, obj, macros)
596 s = StringScanner.new(text)
596 s = StringScanner.new(text)
597 tags = []
597 tags = []
598 parsed = ''
598 parsed = ''
599 while !s.eos?
599 while !s.eos?
600 s.scan(/(.*?)(<(\/)?(pre|code)(.*?)>|\z)/im)
600 s.scan(/(.*?)(<(\/)?(pre|code)(.*?)>|\z)/im)
601 text, full_tag, closing, tag = s[1], s[2], s[3], s[4]
601 text, full_tag, closing, tag = s[1], s[2], s[3], s[4]
602 if tags.empty?
602 if tags.empty?
603 yield text
603 yield text
604 inject_macros(text, obj, macros) if macros.any?
604 inject_macros(text, obj, macros) if macros.any?
605 else
605 else
606 inject_macros(text, obj, macros, false) if macros.any?
606 inject_macros(text, obj, macros, false) if macros.any?
607 end
607 end
608 parsed << text
608 parsed << text
609 if tag
609 if tag
610 if closing
610 if closing
611 if tags.last == tag.downcase
611 if tags.last == tag.downcase
612 tags.pop
612 tags.pop
613 end
613 end
614 else
614 else
615 tags << tag.downcase
615 tags << tag.downcase
616 end
616 end
617 parsed << full_tag
617 parsed << full_tag
618 end
618 end
619 end
619 end
620 # Close any non closing tags
620 # Close any non closing tags
621 while tag = tags.pop
621 while tag = tags.pop
622 parsed << "</#{tag}>"
622 parsed << "</#{tag}>"
623 end
623 end
624 parsed
624 parsed
625 end
625 end
626
626
627 def parse_inline_attachments(text, project, obj, attr, only_path, options)
627 def parse_inline_attachments(text, project, obj, attr, only_path, options)
628 return if options[:inline_attachments] == false
628 return if options[:inline_attachments] == false
629
629
630 # when using an image link, try to use an attachment, if possible
630 # when using an image link, try to use an attachment, if possible
631 attachments = options[:attachments] || []
631 attachments = options[:attachments] || []
632 attachments += obj.attachments if obj.respond_to?(:attachments)
632 attachments += obj.attachments if obj.respond_to?(:attachments)
633 if attachments.present?
633 if attachments.present?
634 text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m|
634 text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m|
635 filename, ext, alt, alttext = $1.downcase, $2, $3, $4
635 filename, ext, alt, alttext = $1.downcase, $2, $3, $4
636 # search for the picture in attachments
636 # search for the picture in attachments
637 if found = Attachment.latest_attach(attachments, CGI.unescape(filename))
637 if found = Attachment.latest_attach(attachments, CGI.unescape(filename))
638 image_url = download_named_attachment_url(found, found.filename, :only_path => only_path)
638 image_url = download_named_attachment_url(found, found.filename, :only_path => only_path)
639 desc = found.description.to_s.gsub('"', '')
639 desc = found.description.to_s.gsub('"', '')
640 if !desc.blank? && alttext.blank?
640 if !desc.blank? && alttext.blank?
641 alt = " title=\"#{desc}\" alt=\"#{desc}\""
641 alt = " title=\"#{desc}\" alt=\"#{desc}\""
642 end
642 end
643 "src=\"#{image_url}\"#{alt}"
643 "src=\"#{image_url}\"#{alt}"
644 else
644 else
645 m
645 m
646 end
646 end
647 end
647 end
648 end
648 end
649 end
649 end
650
650
651 # Wiki links
651 # Wiki links
652 #
652 #
653 # Examples:
653 # Examples:
654 # [[mypage]]
654 # [[mypage]]
655 # [[mypage|mytext]]
655 # [[mypage|mytext]]
656 # wiki links can refer other project wikis, using project name or identifier:
656 # wiki links can refer other project wikis, using project name or identifier:
657 # [[project:]] -> wiki starting page
657 # [[project:]] -> wiki starting page
658 # [[project:|mytext]]
658 # [[project:|mytext]]
659 # [[project:mypage]]
659 # [[project:mypage]]
660 # [[project:mypage|mytext]]
660 # [[project:mypage|mytext]]
661 def parse_wiki_links(text, project, obj, attr, only_path, options)
661 def parse_wiki_links(text, project, obj, attr, only_path, options)
662 text.gsub!(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m|
662 text.gsub!(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m|
663 link_project = project
663 link_project = project
664 esc, all, page, title = $1, $2, $3, $5
664 esc, all, page, title = $1, $2, $3, $5
665 if esc.nil?
665 if esc.nil?
666 if page =~ /^([^\:]+)\:(.*)$/
666 if page =~ /^([^\:]+)\:(.*)$/
667 identifier, page = $1, $2
667 identifier, page = $1, $2
668 link_project = Project.find_by_identifier(identifier) || Project.find_by_name(identifier)
668 link_project = Project.find_by_identifier(identifier) || Project.find_by_name(identifier)
669 title ||= identifier if page.blank?
669 title ||= identifier if page.blank?
670 end
670 end
671
671
672 if link_project && link_project.wiki
672 if link_project && link_project.wiki
673 # extract anchor
673 # extract anchor
674 anchor = nil
674 anchor = nil
675 if page =~ /^(.+?)\#(.+)$/
675 if page =~ /^(.+?)\#(.+)$/
676 page, anchor = $1, $2
676 page, anchor = $1, $2
677 end
677 end
678 anchor = sanitize_anchor_name(anchor) if anchor.present?
678 anchor = sanitize_anchor_name(anchor) if anchor.present?
679 # check if page exists
679 # check if page exists
680 wiki_page = link_project.wiki.find_page(page)
680 wiki_page = link_project.wiki.find_page(page)
681 url = if anchor.present? && wiki_page.present? && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version)) && obj.page == wiki_page
681 url = if anchor.present? && wiki_page.present? && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version)) && obj.page == wiki_page
682 "##{anchor}"
682 "##{anchor}"
683 else
683 else
684 case options[:wiki_links]
684 case options[:wiki_links]
685 when :local; "#{page.present? ? Wiki.titleize(page) : ''}.html" + (anchor.present? ? "##{anchor}" : '')
685 when :local; "#{page.present? ? Wiki.titleize(page) : ''}.html" + (anchor.present? ? "##{anchor}" : '')
686 when :anchor; "##{page.present? ? Wiki.titleize(page) : title}" + (anchor.present? ? "_#{anchor}" : '') # used for single-file wiki export
686 when :anchor; "##{page.present? ? Wiki.titleize(page) : title}" + (anchor.present? ? "_#{anchor}" : '') # used for single-file wiki export
687 else
687 else
688 wiki_page_id = page.present? ? Wiki.titleize(page) : nil
688 wiki_page_id = page.present? ? Wiki.titleize(page) : nil
689 parent = wiki_page.nil? && obj.is_a?(WikiContent) && obj.page && project == link_project ? obj.page.title : nil
689 parent = wiki_page.nil? && obj.is_a?(WikiContent) && obj.page && project == link_project ? obj.page.title : nil
690 url_for(:only_path => only_path, :controller => 'wiki', :action => 'show', :project_id => link_project,
690 url_for(:only_path => only_path, :controller => 'wiki', :action => 'show', :project_id => link_project,
691 :id => wiki_page_id, :version => nil, :anchor => anchor, :parent => parent)
691 :id => wiki_page_id, :version => nil, :anchor => anchor, :parent => parent)
692 end
692 end
693 end
693 end
694 link_to(title.present? ? title.html_safe : h(page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new')))
694 link_to(title.present? ? title.html_safe : h(page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new')))
695 else
695 else
696 # project or wiki doesn't exist
696 # project or wiki doesn't exist
697 all
697 all
698 end
698 end
699 else
699 else
700 all
700 all
701 end
701 end
702 end
702 end
703 end
703 end
704
704
705 # Redmine links
705 # Redmine links
706 #
706 #
707 # Examples:
707 # Examples:
708 # Issues:
708 # Issues:
709 # #52 -> Link to issue #52
709 # #52 -> Link to issue #52
710 # Changesets:
710 # Changesets:
711 # r52 -> Link to revision 52
711 # r52 -> Link to revision 52
712 # commit:a85130f -> Link to scmid starting with a85130f
712 # commit:a85130f -> Link to scmid starting with a85130f
713 # Documents:
713 # Documents:
714 # document#17 -> Link to document with id 17
714 # document#17 -> Link to document with id 17
715 # document:Greetings -> Link to the document with title "Greetings"
715 # document:Greetings -> Link to the document with title "Greetings"
716 # document:"Some document" -> Link to the document with title "Some document"
716 # document:"Some document" -> Link to the document with title "Some document"
717 # Versions:
717 # Versions:
718 # version#3 -> Link to version with id 3
718 # version#3 -> Link to version with id 3
719 # version:1.0.0 -> Link to version named "1.0.0"
719 # version:1.0.0 -> Link to version named "1.0.0"
720 # version:"1.0 beta 2" -> Link to version named "1.0 beta 2"
720 # version:"1.0 beta 2" -> Link to version named "1.0 beta 2"
721 # Attachments:
721 # Attachments:
722 # attachment:file.zip -> Link to the attachment of the current object named file.zip
722 # attachment:file.zip -> Link to the attachment of the current object named file.zip
723 # Source files:
723 # Source files:
724 # source:some/file -> Link to the file located at /some/file in the project's repository
724 # source:some/file -> Link to the file located at /some/file in the project's repository
725 # source:some/file@52 -> Link to the file's revision 52
725 # source:some/file@52 -> Link to the file's revision 52
726 # source:some/file#L120 -> Link to line 120 of the file
726 # source:some/file#L120 -> Link to line 120 of the file
727 # source:some/file@52#L120 -> Link to line 120 of the file's revision 52
727 # source:some/file@52#L120 -> Link to line 120 of the file's revision 52
728 # export:some/file -> Force the download of the file
728 # export:some/file -> Force the download of the file
729 # Forum messages:
729 # Forum messages:
730 # message#1218 -> Link to message with id 1218
730 # message#1218 -> Link to message with id 1218
731 # Projects:
731 # Projects:
732 # project:someproject -> Link to project named "someproject"
732 # project:someproject -> Link to project named "someproject"
733 # project#3 -> Link to project with id 3
733 # project#3 -> Link to project with id 3
734 #
734 #
735 # Links can refer other objects from other projects, using project identifier:
735 # Links can refer other objects from other projects, using project identifier:
736 # identifier:r52
736 # identifier:r52
737 # identifier:document:"Some document"
737 # identifier:document:"Some document"
738 # identifier:version:1.0.0
738 # identifier:version:1.0.0
739 # identifier:source:some/file
739 # identifier:source:some/file
740 def parse_redmine_links(text, default_project, obj, attr, only_path, options)
740 def parse_redmine_links(text, default_project, obj, attr, only_path, options)
741 text.gsub!(%r{<a( [^>]+?)?>(.*?)</a>|([\s\(,\-\[\>]|^)(!)?(([a-z0-9\-_]+):)?(attachment|document|version|forum|news|message|project|commit|source|export)?(((#)|((([a-z0-9\-_]+)\|)?(r)))((\d+)((#note)?-(\d+))?)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]][^A-Za-z0-9_/])|,|\s|\]|<|$)}) do |m|
741 text.gsub!(%r{<a( [^>]+?)?>(.*?)</a>|([\s\(,\-\[\>]|^)(!)?(([a-z0-9\-_]+):)?(attachment|document|version|forum|news|message|project|commit|source|export)?(((#)|((([a-z0-9\-_]+)\|)?(r)))((\d+)((#note)?-(\d+))?)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]][^A-Za-z0-9_/])|,|\s|\]|<|$)}) do |m|
742 tag_content, leading, esc, project_prefix, project_identifier, prefix, repo_prefix, repo_identifier, sep, identifier, comment_suffix, comment_id = $1, $3, $4, $5, $6, $7, $12, $13, $10 || $14 || $20, $16 || $21, $17, $19
742 tag_content, leading, esc, project_prefix, project_identifier, prefix, repo_prefix, repo_identifier, sep, identifier, comment_suffix, comment_id = $1, $3, $4, $5, $6, $7, $12, $13, $10 || $14 || $20, $16 || $21, $17, $19
743 if tag_content
743 if tag_content
744 $&
744 $&
745 else
745 else
746 link = nil
746 link = nil
747 project = default_project
747 project = default_project
748 if project_identifier
748 if project_identifier
749 project = Project.visible.find_by_identifier(project_identifier)
749 project = Project.visible.find_by_identifier(project_identifier)
750 end
750 end
751 if esc.nil?
751 if esc.nil?
752 if prefix.nil? && sep == 'r'
752 if prefix.nil? && sep == 'r'
753 if project
753 if project
754 repository = nil
754 repository = nil
755 if repo_identifier
755 if repo_identifier
756 repository = project.repositories.detect {|repo| repo.identifier == repo_identifier}
756 repository = project.repositories.detect {|repo| repo.identifier == repo_identifier}
757 else
757 else
758 repository = project.repository
758 repository = project.repository
759 end
759 end
760 # project.changesets.visible raises an SQL error because of a double join on repositories
760 # project.changesets.visible raises an SQL error because of a double join on repositories
761 if repository &&
761 if repository &&
762 (changeset = Changeset.visible.
762 (changeset = Changeset.visible.
763 find_by_repository_id_and_revision(repository.id, identifier))
763 find_by_repository_id_and_revision(repository.id, identifier))
764 link = link_to(h("#{project_prefix}#{repo_prefix}r#{identifier}"),
764 link = link_to(h("#{project_prefix}#{repo_prefix}r#{identifier}"),
765 {:only_path => only_path, :controller => 'repositories',
765 {:only_path => only_path, :controller => 'repositories',
766 :action => 'revision', :id => project,
766 :action => 'revision', :id => project,
767 :repository_id => repository.identifier_param,
767 :repository_id => repository.identifier_param,
768 :rev => changeset.revision},
768 :rev => changeset.revision},
769 :class => 'changeset',
769 :class => 'changeset',
770 :title => truncate_single_line_raw(changeset.comments, 100))
770 :title => truncate_single_line_raw(changeset.comments, 100))
771 end
771 end
772 end
772 end
773 elsif sep == '#'
773 elsif sep == '#'
774 oid = identifier.to_i
774 oid = identifier.to_i
775 case prefix
775 case prefix
776 when nil
776 when nil
777 if oid.to_s == identifier &&
777 if oid.to_s == identifier &&
778 issue = Issue.visible.find_by_id(oid)
778 issue = Issue.visible.find_by_id(oid)
779 anchor = comment_id ? "note-#{comment_id}" : nil
779 anchor = comment_id ? "note-#{comment_id}" : nil
780 link = link_to("##{oid}#{comment_suffix}",
780 link = link_to("##{oid}#{comment_suffix}",
781 issue_url(issue, :only_path => only_path, :anchor => anchor),
781 issue_url(issue, :only_path => only_path, :anchor => anchor),
782 :class => issue.css_classes,
782 :class => issue.css_classes,
783 :title => "#{issue.subject.truncate(100)} (#{issue.status.name})")
783 :title => "#{issue.subject.truncate(100)} (#{issue.status.name})")
784 end
784 end
785 when 'document'
785 when 'document'
786 if document = Document.visible.find_by_id(oid)
786 if document = Document.visible.find_by_id(oid)
787 link = link_to(document.title, document_url(document, :only_path => only_path), :class => 'document')
787 link = link_to(document.title, document_url(document, :only_path => only_path), :class => 'document')
788 end
788 end
789 when 'version'
789 when 'version'
790 if version = Version.visible.find_by_id(oid)
790 if version = Version.visible.find_by_id(oid)
791 link = link_to(version.name, version_url(version, :only_path => only_path), :class => 'version')
791 link = link_to(version.name, version_url(version, :only_path => only_path), :class => 'version')
792 end
792 end
793 when 'message'
793 when 'message'
794 if message = Message.visible.find_by_id(oid)
794 if message = Message.visible.find_by_id(oid)
795 link = link_to_message(message, {:only_path => only_path}, :class => 'message')
795 link = link_to_message(message, {:only_path => only_path}, :class => 'message')
796 end
796 end
797 when 'forum'
797 when 'forum'
798 if board = Board.visible.find_by_id(oid)
798 if board = Board.visible.find_by_id(oid)
799 link = link_to(board.name, project_board_url(board.project, board, :only_path => only_path), :class => 'board')
799 link = link_to(board.name, project_board_url(board.project, board, :only_path => only_path), :class => 'board')
800 end
800 end
801 when 'news'
801 when 'news'
802 if news = News.visible.find_by_id(oid)
802 if news = News.visible.find_by_id(oid)
803 link = link_to(news.title, news_url(news, :only_path => only_path), :class => 'news')
803 link = link_to(news.title, news_url(news, :only_path => only_path), :class => 'news')
804 end
804 end
805 when 'project'
805 when 'project'
806 if p = Project.visible.find_by_id(oid)
806 if p = Project.visible.find_by_id(oid)
807 link = link_to_project(p, {:only_path => only_path}, :class => 'project')
807 link = link_to_project(p, {:only_path => only_path}, :class => 'project')
808 end
808 end
809 end
809 end
810 elsif sep == ':'
810 elsif sep == ':'
811 # removes the double quotes if any
811 # removes the double quotes if any
812 name = identifier.gsub(%r{^"(.*)"$}, "\\1")
812 name = identifier.gsub(%r{^"(.*)"$}, "\\1")
813 name = CGI.unescapeHTML(name)
813 name = CGI.unescapeHTML(name)
814 case prefix
814 case prefix
815 when 'document'
815 when 'document'
816 if project && document = project.documents.visible.find_by_title(name)
816 if project && document = project.documents.visible.find_by_title(name)
817 link = link_to(document.title, document_url(document, :only_path => only_path), :class => 'document')
817 link = link_to(document.title, document_url(document, :only_path => only_path), :class => 'document')
818 end
818 end
819 when 'version'
819 when 'version'
820 if project && version = project.versions.visible.find_by_name(name)
820 if project && version = project.versions.visible.find_by_name(name)
821 link = link_to(version.name, version_url(version, :only_path => only_path), :class => 'version')
821 link = link_to(version.name, version_url(version, :only_path => only_path), :class => 'version')
822 end
822 end
823 when 'forum'
823 when 'forum'
824 if project && board = project.boards.visible.find_by_name(name)
824 if project && board = project.boards.visible.find_by_name(name)
825 link = link_to(board.name, project_board_url(board.project, board, :only_path => only_path), :class => 'board')
825 link = link_to(board.name, project_board_url(board.project, board, :only_path => only_path), :class => 'board')
826 end
826 end
827 when 'news'
827 when 'news'
828 if project && news = project.news.visible.find_by_title(name)
828 if project && news = project.news.visible.find_by_title(name)
829 link = link_to(news.title, news_url(news, :only_path => only_path), :class => 'news')
829 link = link_to(news.title, news_url(news, :only_path => only_path), :class => 'news')
830 end
830 end
831 when 'commit', 'source', 'export'
831 when 'commit', 'source', 'export'
832 if project
832 if project
833 repository = nil
833 repository = nil
834 if name =~ %r{^(([a-z0-9\-_]+)\|)(.+)$}
834 if name =~ %r{^(([a-z0-9\-_]+)\|)(.+)$}
835 repo_prefix, repo_identifier, name = $1, $2, $3
835 repo_prefix, repo_identifier, name = $1, $2, $3
836 repository = project.repositories.detect {|repo| repo.identifier == repo_identifier}
836 repository = project.repositories.detect {|repo| repo.identifier == repo_identifier}
837 else
837 else
838 repository = project.repository
838 repository = project.repository
839 end
839 end
840 if prefix == 'commit'
840 if prefix == 'commit'
841 if repository && (changeset = Changeset.visible.where("repository_id = ? AND scmid LIKE ?", repository.id, "#{name}%").first)
841 if repository && (changeset = Changeset.visible.where("repository_id = ? AND scmid LIKE ?", repository.id, "#{name}%").first)
842 link = link_to h("#{project_prefix}#{repo_prefix}#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :repository_id => repository.identifier_param, :rev => changeset.identifier},
842 link = link_to h("#{project_prefix}#{repo_prefix}#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :repository_id => repository.identifier_param, :rev => changeset.identifier},
843 :class => 'changeset',
843 :class => 'changeset',
844 :title => truncate_single_line_raw(changeset.comments, 100)
844 :title => truncate_single_line_raw(changeset.comments, 100)
845 end
845 end
846 else
846 else
847 if repository && User.current.allowed_to?(:browse_repository, project)
847 if repository && User.current.allowed_to?(:browse_repository, project)
848 name =~ %r{^[/\\]*(.*?)(@([^/\\@]+?))?(#(L\d+))?$}
848 name =~ %r{^[/\\]*(.*?)(@([^/\\@]+?))?(#(L\d+))?$}
849 path, rev, anchor = $1, $3, $5
849 path, rev, anchor = $1, $3, $5
850 link = link_to h("#{project_prefix}#{prefix}:#{repo_prefix}#{name}"), {:only_path => only_path, :controller => 'repositories', :action => (prefix == 'export' ? 'raw' : 'entry'), :id => project, :repository_id => repository.identifier_param,
850 link = link_to h("#{project_prefix}#{prefix}:#{repo_prefix}#{name}"), {:only_path => only_path, :controller => 'repositories', :action => (prefix == 'export' ? 'raw' : 'entry'), :id => project, :repository_id => repository.identifier_param,
851 :path => to_path_param(path),
851 :path => to_path_param(path),
852 :rev => rev,
852 :rev => rev,
853 :anchor => anchor},
853 :anchor => anchor},
854 :class => (prefix == 'export' ? 'source download' : 'source')
854 :class => (prefix == 'export' ? 'source download' : 'source')
855 end
855 end
856 end
856 end
857 repo_prefix = nil
857 repo_prefix = nil
858 end
858 end
859 when 'attachment'
859 when 'attachment'
860 attachments = options[:attachments] || []
860 attachments = options[:attachments] || []
861 attachments += obj.attachments if obj.respond_to?(:attachments)
861 attachments += obj.attachments if obj.respond_to?(:attachments)
862 if attachments && attachment = Attachment.latest_attach(attachments, name)
862 if attachments && attachment = Attachment.latest_attach(attachments, name)
863 link = link_to_attachment(attachment, :only_path => only_path, :download => true, :class => 'attachment')
863 link = link_to_attachment(attachment, :only_path => only_path, :download => true, :class => 'attachment')
864 end
864 end
865 when 'project'
865 when 'project'
866 if p = Project.visible.where("identifier = :s OR LOWER(name) = :s", :s => name.downcase).first
866 if p = Project.visible.where("identifier = :s OR LOWER(name) = :s", :s => name.downcase).first
867 link = link_to_project(p, {:only_path => only_path}, :class => 'project')
867 link = link_to_project(p, {:only_path => only_path}, :class => 'project')
868 end
868 end
869 end
869 end
870 end
870 end
871 end
871 end
872 (leading + (link || "#{project_prefix}#{prefix}#{repo_prefix}#{sep}#{identifier}#{comment_suffix}"))
872 (leading + (link || "#{project_prefix}#{prefix}#{repo_prefix}#{sep}#{identifier}#{comment_suffix}"))
873 end
873 end
874 end
874 end
875 end
875 end
876
876
877 HEADING_RE = /(<h(\d)( [^>]+)?>(.+?)<\/h(\d)>)/i unless const_defined?(:HEADING_RE)
877 HEADING_RE = /(<h(\d)( [^>]+)?>(.+?)<\/h(\d)>)/i unless const_defined?(:HEADING_RE)
878
878
879 def parse_sections(text, project, obj, attr, only_path, options)
879 def parse_sections(text, project, obj, attr, only_path, options)
880 return unless options[:edit_section_links]
880 return unless options[:edit_section_links]
881 text.gsub!(HEADING_RE) do
881 text.gsub!(HEADING_RE) do
882 heading = $1
882 heading = $1
883 @current_section += 1
883 @current_section += 1
884 if @current_section > 1
884 if @current_section > 1
885 content_tag('div',
885 content_tag('div',
886 link_to(image_tag('edit.png'), options[:edit_section_links].merge(:section => @current_section)),
886 link_to(image_tag('edit.png'), options[:edit_section_links].merge(:section => @current_section)),
887 :class => 'contextual',
887 :class => 'contextual',
888 :title => l(:button_edit_section),
888 :title => l(:button_edit_section),
889 :id => "section-#{@current_section}") + heading.html_safe
889 :id => "section-#{@current_section}") + heading.html_safe
890 else
890 else
891 heading
891 heading
892 end
892 end
893 end
893 end
894 end
894 end
895
895
896 # Headings and TOC
896 # Headings and TOC
897 # Adds ids and links to headings unless options[:headings] is set to false
897 # Adds ids and links to headings unless options[:headings] is set to false
898 def parse_headings(text, project, obj, attr, only_path, options)
898 def parse_headings(text, project, obj, attr, only_path, options)
899 return if options[:headings] == false
899 return if options[:headings] == false
900
900
901 text.gsub!(HEADING_RE) do
901 text.gsub!(HEADING_RE) do
902 level, attrs, content = $2.to_i, $3, $4
902 level, attrs, content = $2.to_i, $3, $4
903 item = strip_tags(content).strip
903 item = strip_tags(content).strip
904 anchor = sanitize_anchor_name(item)
904 anchor = sanitize_anchor_name(item)
905 # used for single-file wiki export
905 # used for single-file wiki export
906 anchor = "#{obj.page.title}_#{anchor}" if options[:wiki_links] == :anchor && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version))
906 anchor = "#{obj.page.title}_#{anchor}" if options[:wiki_links] == :anchor && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version))
907 @heading_anchors[anchor] ||= 0
907 @heading_anchors[anchor] ||= 0
908 idx = (@heading_anchors[anchor] += 1)
908 idx = (@heading_anchors[anchor] += 1)
909 if idx > 1
909 if idx > 1
910 anchor = "#{anchor}-#{idx}"
910 anchor = "#{anchor}-#{idx}"
911 end
911 end
912 @parsed_headings << [level, anchor, item]
912 @parsed_headings << [level, anchor, item]
913 "<a name=\"#{anchor}\"></a>\n<h#{level} #{attrs}>#{content}<a href=\"##{anchor}\" class=\"wiki-anchor\">&para;</a></h#{level}>"
913 "<a name=\"#{anchor}\"></a>\n<h#{level} #{attrs}>#{content}<a href=\"##{anchor}\" class=\"wiki-anchor\">&para;</a></h#{level}>"
914 end
914 end
915 end
915 end
916
916
917 MACROS_RE = /(
917 MACROS_RE = /(
918 (!)? # escaping
918 (!)? # escaping
919 (
919 (
920 \{\{ # opening tag
920 \{\{ # opening tag
921 ([\w]+) # macro name
921 ([\w]+) # macro name
922 (\(([^\n\r]*?)\))? # optional arguments
922 (\(([^\n\r]*?)\))? # optional arguments
923 ([\n\r].*?[\n\r])? # optional block of text
923 ([\n\r].*?[\n\r])? # optional block of text
924 \}\} # closing tag
924 \}\} # closing tag
925 )
925 )
926 )/mx unless const_defined?(:MACROS_RE)
926 )/mx unless const_defined?(:MACROS_RE)
927
927
928 MACRO_SUB_RE = /(
928 MACRO_SUB_RE = /(
929 \{\{
929 \{\{
930 macro\((\d+)\)
930 macro\((\d+)\)
931 \}\}
931 \}\}
932 )/x unless const_defined?(:MACRO_SUB_RE)
932 )/x unless const_defined?(:MACRO_SUB_RE)
933
933
934 # Extracts macros from text
934 # Extracts macros from text
935 def catch_macros(text)
935 def catch_macros(text)
936 macros = {}
936 macros = {}
937 text.gsub!(MACROS_RE) do
937 text.gsub!(MACROS_RE) do
938 all, macro = $1, $4.downcase
938 all, macro = $1, $4.downcase
939 if macro_exists?(macro) || all =~ MACRO_SUB_RE
939 if macro_exists?(macro) || all =~ MACRO_SUB_RE
940 index = macros.size
940 index = macros.size
941 macros[index] = all
941 macros[index] = all
942 "{{macro(#{index})}}"
942 "{{macro(#{index})}}"
943 else
943 else
944 all
944 all
945 end
945 end
946 end
946 end
947 macros
947 macros
948 end
948 end
949
949
950 # Executes and replaces macros in text
950 # Executes and replaces macros in text
951 def inject_macros(text, obj, macros, execute=true)
951 def inject_macros(text, obj, macros, execute=true)
952 text.gsub!(MACRO_SUB_RE) do
952 text.gsub!(MACRO_SUB_RE) do
953 all, index = $1, $2.to_i
953 all, index = $1, $2.to_i
954 orig = macros.delete(index)
954 orig = macros.delete(index)
955 if execute && orig && orig =~ MACROS_RE
955 if execute && orig && orig =~ MACROS_RE
956 esc, all, macro, args, block = $2, $3, $4.downcase, $6.to_s, $7.try(:strip)
956 esc, all, macro, args, block = $2, $3, $4.downcase, $6.to_s, $7.try(:strip)
957 if esc.nil?
957 if esc.nil?
958 h(exec_macro(macro, obj, args, block) || all)
958 h(exec_macro(macro, obj, args, block) || all)
959 else
959 else
960 h(all)
960 h(all)
961 end
961 end
962 elsif orig
962 elsif orig
963 h(orig)
963 h(orig)
964 else
964 else
965 h(all)
965 h(all)
966 end
966 end
967 end
967 end
968 end
968 end
969
969
970 TOC_RE = /<p>\{\{((<|&lt;)|(>|&gt;))?toc\}\}<\/p>/i unless const_defined?(:TOC_RE)
970 TOC_RE = /<p>\{\{((<|&lt;)|(>|&gt;))?toc\}\}<\/p>/i unless const_defined?(:TOC_RE)
971
971
972 # Renders the TOC with given headings
972 # Renders the TOC with given headings
973 def replace_toc(text, headings)
973 def replace_toc(text, headings)
974 text.gsub!(TOC_RE) do
974 text.gsub!(TOC_RE) do
975 left_align, right_align = $2, $3
975 left_align, right_align = $2, $3
976 # Keep only the 4 first levels
976 # Keep only the 4 first levels
977 headings = headings.select{|level, anchor, item| level <= 4}
977 headings = headings.select{|level, anchor, item| level <= 4}
978 if headings.empty?
978 if headings.empty?
979 ''
979 ''
980 else
980 else
981 div_class = 'toc'
981 div_class = 'toc'
982 div_class << ' right' if right_align
982 div_class << ' right' if right_align
983 div_class << ' left' if left_align
983 div_class << ' left' if left_align
984 out = "<ul class=\"#{div_class}\"><li>"
984 out = "<ul class=\"#{div_class}\"><li>"
985 root = headings.map(&:first).min
985 root = headings.map(&:first).min
986 current = root
986 current = root
987 started = false
987 started = false
988 headings.each do |level, anchor, item|
988 headings.each do |level, anchor, item|
989 if level > current
989 if level > current
990 out << '<ul><li>' * (level - current)
990 out << '<ul><li>' * (level - current)
991 elsif level < current
991 elsif level < current
992 out << "</li></ul>\n" * (current - level) + "</li><li>"
992 out << "</li></ul>\n" * (current - level) + "</li><li>"
993 elsif started
993 elsif started
994 out << '</li><li>'
994 out << '</li><li>'
995 end
995 end
996 out << "<a href=\"##{anchor}\">#{item}</a>"
996 out << "<a href=\"##{anchor}\">#{item}</a>"
997 current = level
997 current = level
998 started = true
998 started = true
999 end
999 end
1000 out << '</li></ul>' * (current - root)
1000 out << '</li></ul>' * (current - root)
1001 out << '</li></ul>'
1001 out << '</li></ul>'
1002 end
1002 end
1003 end
1003 end
1004 end
1004 end
1005
1005
1006 # Same as Rails' simple_format helper without using paragraphs
1006 # Same as Rails' simple_format helper without using paragraphs
1007 def simple_format_without_paragraph(text)
1007 def simple_format_without_paragraph(text)
1008 text.to_s.
1008 text.to_s.
1009 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
1009 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
1010 gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br
1010 gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br
1011 gsub(/([^\n]\n)(?=[^\n])/, '\1<br />'). # 1 newline -> br
1011 gsub(/([^\n]\n)(?=[^\n])/, '\1<br />'). # 1 newline -> br
1012 html_safe
1012 html_safe
1013 end
1013 end
1014
1014
1015 def lang_options_for_select(blank=true)
1015 def lang_options_for_select(blank=true)
1016 (blank ? [["(auto)", ""]] : []) + languages_options
1016 (blank ? [["(auto)", ""]] : []) + languages_options
1017 end
1017 end
1018
1018
1019 def labelled_form_for(*args, &proc)
1019 def labelled_form_for(*args, &proc)
1020 args << {} unless args.last.is_a?(Hash)
1020 args << {} unless args.last.is_a?(Hash)
1021 options = args.last
1021 options = args.last
1022 if args.first.is_a?(Symbol)
1022 if args.first.is_a?(Symbol)
1023 options.merge!(:as => args.shift)
1023 options.merge!(:as => args.shift)
1024 end
1024 end
1025 options.merge!({:builder => Redmine::Views::LabelledFormBuilder})
1025 options.merge!({:builder => Redmine::Views::LabelledFormBuilder})
1026 form_for(*args, &proc)
1026 form_for(*args, &proc)
1027 end
1027 end
1028
1028
1029 def labelled_fields_for(*args, &proc)
1029 def labelled_fields_for(*args, &proc)
1030 args << {} unless args.last.is_a?(Hash)
1030 args << {} unless args.last.is_a?(Hash)
1031 options = args.last
1031 options = args.last
1032 options.merge!({:builder => Redmine::Views::LabelledFormBuilder})
1032 options.merge!({:builder => Redmine::Views::LabelledFormBuilder})
1033 fields_for(*args, &proc)
1033 fields_for(*args, &proc)
1034 end
1034 end
1035
1035
1036 def error_messages_for(*objects)
1036 def error_messages_for(*objects)
1037 html = ""
1037 html = ""
1038 objects = objects.map {|o| o.is_a?(String) ? instance_variable_get("@#{o}") : o}.compact
1038 objects = objects.map {|o| o.is_a?(String) ? instance_variable_get("@#{o}") : o}.compact
1039 errors = objects.map {|o| o.errors.full_messages}.flatten
1039 errors = objects.map {|o| o.errors.full_messages}.flatten
1040 if errors.any?
1040 if errors.any?
1041 html << "<div id='errorExplanation'><ul>\n"
1041 html << "<div id='errorExplanation'><ul>\n"
1042 errors.each do |error|
1042 errors.each do |error|
1043 html << "<li>#{h error}</li>\n"
1043 html << "<li>#{h error}</li>\n"
1044 end
1044 end
1045 html << "</ul></div>\n"
1045 html << "</ul></div>\n"
1046 end
1046 end
1047 html.html_safe
1047 html.html_safe
1048 end
1048 end
1049
1049
1050 def delete_link(url, options={})
1050 def delete_link(url, options={})
1051 options = {
1051 options = {
1052 :method => :delete,
1052 :method => :delete,
1053 :data => {:confirm => l(:text_are_you_sure)},
1053 :data => {:confirm => l(:text_are_you_sure)},
1054 :class => 'icon icon-del'
1054 :class => 'icon icon-del'
1055 }.merge(options)
1055 }.merge(options)
1056
1056
1057 link_to l(:button_delete), url, options
1057 link_to l(:button_delete), url, options
1058 end
1058 end
1059
1059
1060 def preview_link(url, form, target='preview', options={})
1060 def preview_link(url, form, target='preview', options={})
1061 content_tag 'a', l(:label_preview), {
1061 content_tag 'a', l(:label_preview), {
1062 :href => "#",
1062 :href => "#",
1063 :onclick => %|submitPreview("#{escape_javascript url_for(url)}", "#{escape_javascript form}", "#{escape_javascript target}"); return false;|,
1063 :onclick => %|submitPreview("#{escape_javascript url_for(url)}", "#{escape_javascript form}", "#{escape_javascript target}"); return false;|,
1064 :accesskey => accesskey(:preview)
1064 :accesskey => accesskey(:preview)
1065 }.merge(options)
1065 }.merge(options)
1066 end
1066 end
1067
1067
1068 def link_to_function(name, function, html_options={})
1068 def link_to_function(name, function, html_options={})
1069 content_tag(:a, name, {:href => '#', :onclick => "#{function}; return false;"}.merge(html_options))
1069 content_tag(:a, name, {:href => '#', :onclick => "#{function}; return false;"}.merge(html_options))
1070 end
1070 end
1071
1071
1072 # Helper to render JSON in views
1072 # Helper to render JSON in views
1073 def raw_json(arg)
1073 def raw_json(arg)
1074 arg.to_json.to_s.gsub('/', '\/').html_safe
1074 arg.to_json.to_s.gsub('/', '\/').html_safe
1075 end
1075 end
1076
1076
1077 def back_url
1077 def back_url
1078 url = params[:back_url]
1078 url = params[:back_url]
1079 if url.nil? && referer = request.env['HTTP_REFERER']
1079 if url.nil? && referer = request.env['HTTP_REFERER']
1080 url = CGI.unescape(referer.to_s)
1080 url = CGI.unescape(referer.to_s)
1081 end
1081 end
1082 url
1082 url
1083 end
1083 end
1084
1084
1085 def back_url_hidden_field_tag
1085 def back_url_hidden_field_tag
1086 url = back_url
1086 url = back_url
1087 hidden_field_tag('back_url', url, :id => nil) unless url.blank?
1087 hidden_field_tag('back_url', url, :id => nil) unless url.blank?
1088 end
1088 end
1089
1089
1090 def check_all_links(form_name)
1090 def check_all_links(form_name)
1091 link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
1091 link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
1092 " | ".html_safe +
1092 " | ".html_safe +
1093 link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
1093 link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
1094 end
1094 end
1095
1095
1096 def toggle_checkboxes_link(selector)
1096 def toggle_checkboxes_link(selector)
1097 link_to_function image_tag('toggle_check.png'),
1097 link_to_function image_tag('toggle_check.png'),
1098 "toggleCheckboxesBySelector('#{selector}')",
1098 "toggleCheckboxesBySelector('#{selector}')",
1099 :title => "#{l(:button_check_all)} / #{l(:button_uncheck_all)}"
1099 :title => "#{l(:button_check_all)} / #{l(:button_uncheck_all)}"
1100 end
1100 end
1101
1101
1102 def progress_bar(pcts, options={})
1102 def progress_bar(pcts, options={})
1103 pcts = [pcts, pcts] unless pcts.is_a?(Array)
1103 pcts = [pcts, pcts] unless pcts.is_a?(Array)
1104 pcts = pcts.collect(&:round)
1104 pcts = pcts.collect(&:round)
1105 pcts[1] = pcts[1] - pcts[0]
1105 pcts[1] = pcts[1] - pcts[0]
1106 pcts << (100 - pcts[1] - pcts[0])
1106 pcts << (100 - pcts[1] - pcts[0])
1107 width = options[:width] || '100px;'
1107 width = options[:width] || '100px;'
1108 legend = options[:legend] || ''
1108 legend = options[:legend] || ''
1109 content_tag('table',
1109 content_tag('table',
1110 content_tag('tr',
1110 content_tag('tr',
1111 (pcts[0] > 0 ? content_tag('td', '', :style => "width: #{pcts[0]}%;", :class => 'closed') : ''.html_safe) +
1111 (pcts[0] > 0 ? content_tag('td', '', :style => "width: #{pcts[0]}%;", :class => 'closed') : ''.html_safe) +
1112 (pcts[1] > 0 ? content_tag('td', '', :style => "width: #{pcts[1]}%;", :class => 'done') : ''.html_safe) +
1112 (pcts[1] > 0 ? content_tag('td', '', :style => "width: #{pcts[1]}%;", :class => 'done') : ''.html_safe) +
1113 (pcts[2] > 0 ? content_tag('td', '', :style => "width: #{pcts[2]}%;", :class => 'todo') : ''.html_safe)
1113 (pcts[2] > 0 ? content_tag('td', '', :style => "width: #{pcts[2]}%;", :class => 'todo') : ''.html_safe)
1114 ), :class => "progress progress-#{pcts[0]}", :style => "width: #{width};").html_safe +
1114 ), :class => "progress progress-#{pcts[0]}", :style => "width: #{width};").html_safe +
1115 content_tag('p', legend, :class => 'percent').html_safe
1115 content_tag('p', legend, :class => 'percent').html_safe
1116 end
1116 end
1117
1117
1118 def checked_image(checked=true)
1118 def checked_image(checked=true)
1119 if checked
1119 if checked
1120 image_tag 'toggle_check.png'
1120 image_tag 'toggle_check.png'
1121 end
1121 end
1122 end
1122 end
1123
1123
1124 def context_menu(url)
1124 def context_menu(url)
1125 unless @context_menu_included
1125 unless @context_menu_included
1126 content_for :header_tags do
1126 content_for :header_tags do
1127 javascript_include_tag('context_menu') +
1127 javascript_include_tag('context_menu') +
1128 stylesheet_link_tag('context_menu')
1128 stylesheet_link_tag('context_menu')
1129 end
1129 end
1130 if l(:direction) == 'rtl'
1130 if l(:direction) == 'rtl'
1131 content_for :header_tags do
1131 content_for :header_tags do
1132 stylesheet_link_tag('context_menu_rtl')
1132 stylesheet_link_tag('context_menu_rtl')
1133 end
1133 end
1134 end
1134 end
1135 @context_menu_included = true
1135 @context_menu_included = true
1136 end
1136 end
1137 javascript_tag "contextMenuInit('#{ url_for(url) }')"
1137 javascript_tag "contextMenuInit('#{ url_for(url) }')"
1138 end
1138 end
1139
1139
1140 def calendar_for(field_id)
1140 def calendar_for(field_id)
1141 include_calendar_headers_tags
1141 include_calendar_headers_tags
1142 javascript_tag("$(function() { $('##{field_id}').datepicker(datepickerOptions); });")
1142 javascript_tag("$(function() { $('##{field_id}').datepicker(datepickerOptions); });")
1143 end
1143 end
1144
1144
1145 def include_calendar_headers_tags
1145 def include_calendar_headers_tags
1146 unless @calendar_headers_tags_included
1146 unless @calendar_headers_tags_included
1147 tags = ''.html_safe
1147 tags = ''.html_safe
1148 @calendar_headers_tags_included = true
1148 @calendar_headers_tags_included = true
1149 content_for :header_tags do
1149 content_for :header_tags do
1150 start_of_week = Setting.start_of_week
1150 start_of_week = Setting.start_of_week
1151 start_of_week = l(:general_first_day_of_week, :default => '1') if start_of_week.blank?
1151 start_of_week = l(:general_first_day_of_week, :default => '1') if start_of_week.blank?
1152 # Redmine uses 1..7 (monday..sunday) in settings and locales
1152 # Redmine uses 1..7 (monday..sunday) in settings and locales
1153 # JQuery uses 0..6 (sunday..saturday), 7 needs to be changed to 0
1153 # JQuery uses 0..6 (sunday..saturday), 7 needs to be changed to 0
1154 start_of_week = start_of_week.to_i % 7
1154 start_of_week = start_of_week.to_i % 7
1155 tags << javascript_tag(
1155 tags << javascript_tag(
1156 "var datepickerOptions={dateFormat: 'yy-mm-dd', firstDay: #{start_of_week}, " +
1156 "var datepickerOptions={dateFormat: 'yy-mm-dd', firstDay: #{start_of_week}, " +
1157 "showOn: 'button', buttonImageOnly: true, buttonImage: '" +
1157 "showOn: 'button', buttonImageOnly: true, buttonImage: '" +
1158 path_to_image('/images/calendar.png') +
1158 path_to_image('/images/calendar.png') +
1159 "', showButtonPanel: true, showWeek: true, showOtherMonths: true, " +
1159 "', showButtonPanel: true, showWeek: true, showOtherMonths: true, " +
1160 "selectOtherMonths: true, changeMonth: true, changeYear: true, " +
1160 "selectOtherMonths: true, changeMonth: true, changeYear: true, " +
1161 "beforeShow: beforeShowDatePicker};")
1161 "beforeShow: beforeShowDatePicker};")
1162 jquery_locale = l('jquery.locale', :default => current_language.to_s)
1162 jquery_locale = l('jquery.locale', :default => current_language.to_s)
1163 unless jquery_locale == 'en'
1163 unless jquery_locale == 'en'
1164 tags << javascript_include_tag("i18n/datepicker-#{jquery_locale}.js")
1164 tags << javascript_include_tag("i18n/datepicker-#{jquery_locale}.js")
1165 end
1165 end
1166 tags
1166 tags
1167 end
1167 end
1168 end
1168 end
1169 end
1169 end
1170
1170
1171 # Overrides Rails' stylesheet_link_tag with themes and plugins support.
1171 # Overrides Rails' stylesheet_link_tag with themes and plugins support.
1172 # Examples:
1172 # Examples:
1173 # stylesheet_link_tag('styles') # => picks styles.css from the current theme or defaults
1173 # stylesheet_link_tag('styles') # => picks styles.css from the current theme or defaults
1174 # stylesheet_link_tag('styles', :plugin => 'foo) # => picks styles.css from plugin's assets
1174 # stylesheet_link_tag('styles', :plugin => 'foo) # => picks styles.css from plugin's assets
1175 #
1175 #
1176 def stylesheet_link_tag(*sources)
1176 def stylesheet_link_tag(*sources)
1177 options = sources.last.is_a?(Hash) ? sources.pop : {}
1177 options = sources.last.is_a?(Hash) ? sources.pop : {}
1178 plugin = options.delete(:plugin)
1178 plugin = options.delete(:plugin)
1179 sources = sources.map do |source|
1179 sources = sources.map do |source|
1180 if plugin
1180 if plugin
1181 "/plugin_assets/#{plugin}/stylesheets/#{source}"
1181 "/plugin_assets/#{plugin}/stylesheets/#{source}"
1182 elsif current_theme && current_theme.stylesheets.include?(source)
1182 elsif current_theme && current_theme.stylesheets.include?(source)
1183 current_theme.stylesheet_path(source)
1183 current_theme.stylesheet_path(source)
1184 else
1184 else
1185 source
1185 source
1186 end
1186 end
1187 end
1187 end
1188 super *sources, options
1188 super *sources, options
1189 end
1189 end
1190
1190
1191 # Overrides Rails' image_tag with themes and plugins support.
1191 # Overrides Rails' image_tag with themes and plugins support.
1192 # Examples:
1192 # Examples:
1193 # image_tag('image.png') # => picks image.png from the current theme or defaults
1193 # image_tag('image.png') # => picks image.png from the current theme or defaults
1194 # image_tag('image.png', :plugin => 'foo) # => picks image.png from plugin's assets
1194 # image_tag('image.png', :plugin => 'foo) # => picks image.png from plugin's assets
1195 #
1195 #
1196 def image_tag(source, options={})
1196 def image_tag(source, options={})
1197 if plugin = options.delete(:plugin)
1197 if plugin = options.delete(:plugin)
1198 source = "/plugin_assets/#{plugin}/images/#{source}"
1198 source = "/plugin_assets/#{plugin}/images/#{source}"
1199 elsif current_theme && current_theme.images.include?(source)
1199 elsif current_theme && current_theme.images.include?(source)
1200 source = current_theme.image_path(source)
1200 source = current_theme.image_path(source)
1201 end
1201 end
1202 super source, options
1202 super source, options
1203 end
1203 end
1204
1204
1205 # Overrides Rails' javascript_include_tag with plugins support
1205 # Overrides Rails' javascript_include_tag with plugins support
1206 # Examples:
1206 # Examples:
1207 # javascript_include_tag('scripts') # => picks scripts.js from defaults
1207 # javascript_include_tag('scripts') # => picks scripts.js from defaults
1208 # javascript_include_tag('scripts', :plugin => 'foo) # => picks scripts.js from plugin's assets
1208 # javascript_include_tag('scripts', :plugin => 'foo) # => picks scripts.js from plugin's assets
1209 #
1209 #
1210 def javascript_include_tag(*sources)
1210 def javascript_include_tag(*sources)
1211 options = sources.last.is_a?(Hash) ? sources.pop : {}
1211 options = sources.last.is_a?(Hash) ? sources.pop : {}
1212 if plugin = options.delete(:plugin)
1212 if plugin = options.delete(:plugin)
1213 sources = sources.map do |source|
1213 sources = sources.map do |source|
1214 if plugin
1214 if plugin
1215 "/plugin_assets/#{plugin}/javascripts/#{source}"
1215 "/plugin_assets/#{plugin}/javascripts/#{source}"
1216 else
1216 else
1217 source
1217 source
1218 end
1218 end
1219 end
1219 end
1220 end
1220 end
1221 super *sources, options
1221 super *sources, options
1222 end
1222 end
1223
1223
1224 def sidebar_content?
1224 def sidebar_content?
1225 content_for?(:sidebar) || view_layouts_base_sidebar_hook_response.present?
1225 content_for?(:sidebar) || view_layouts_base_sidebar_hook_response.present?
1226 end
1226 end
1227
1227
1228 def view_layouts_base_sidebar_hook_response
1228 def view_layouts_base_sidebar_hook_response
1229 @view_layouts_base_sidebar_hook_response ||= call_hook(:view_layouts_base_sidebar)
1229 @view_layouts_base_sidebar_hook_response ||= call_hook(:view_layouts_base_sidebar)
1230 end
1230 end
1231
1231
1232 def email_delivery_enabled?
1232 def email_delivery_enabled?
1233 !!ActionMailer::Base.perform_deliveries
1233 !!ActionMailer::Base.perform_deliveries
1234 end
1234 end
1235
1235
1236 # Returns the avatar image tag for the given +user+ if avatars are enabled
1236 # Returns the avatar image tag for the given +user+ if avatars are enabled
1237 # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
1237 # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
1238 def avatar(user, options = { })
1238 def avatar(user, options = { })
1239 if Setting.gravatar_enabled?
1239 if Setting.gravatar_enabled?
1240 options.merge!({:ssl => (request && request.ssl?), :default => Setting.gravatar_default})
1240 options.merge!({:ssl => (request && request.ssl?), :default => Setting.gravatar_default})
1241 email = nil
1241 email = nil
1242 if user.respond_to?(:mail)
1242 if user.respond_to?(:mail)
1243 email = user.mail
1243 email = user.mail
1244 elsif user.to_s =~ %r{<(.+?)>}
1244 elsif user.to_s =~ %r{<(.+?)>}
1245 email = $1
1245 email = $1
1246 end
1246 end
1247 return gravatar(email.to_s.downcase, options) unless email.blank? rescue nil
1247 return gravatar(email.to_s.downcase, options) unless email.blank? rescue nil
1248 else
1248 else
1249 ''
1249 ''
1250 end
1250 end
1251 end
1251 end
1252
1252
1253 def sanitize_anchor_name(anchor)
1253 def sanitize_anchor_name(anchor)
1254 anchor.gsub(%r{[^\s\-\p{Word}]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
1254 anchor.gsub(%r{[^\s\-\p{Word}]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
1255 end
1255 end
1256
1256
1257 # Returns the javascript tags that are included in the html layout head
1257 # Returns the javascript tags that are included in the html layout head
1258 def javascript_heads
1258 def javascript_heads
1259 tags = javascript_include_tag('jquery-1.11.1-ui-1.11.0-ujs-3.1.1', 'application')
1259 tags = javascript_include_tag('jquery-1.11.1-ui-1.11.0-ujs-3.1.1', 'application')
1260 unless User.current.pref.warn_on_leaving_unsaved == '0'
1260 unless User.current.pref.warn_on_leaving_unsaved == '0'
1261 tags << "\n".html_safe + javascript_tag("$(window).load(function(){ warnLeavingUnsaved('#{escape_javascript l(:text_warn_on_leaving_unsaved)}'); });")
1261 tags << "\n".html_safe + javascript_tag("$(window).load(function(){ warnLeavingUnsaved('#{escape_javascript l(:text_warn_on_leaving_unsaved)}'); });")
1262 end
1262 end
1263 tags
1263 tags
1264 end
1264 end
1265
1265
1266 def favicon
1266 def favicon
1267 "<link rel='shortcut icon' href='#{favicon_path}' />".html_safe
1267 "<link rel='shortcut icon' href='#{favicon_path}' />".html_safe
1268 end
1268 end
1269
1269
1270 # Returns the path to the favicon
1270 # Returns the path to the favicon
1271 def favicon_path
1271 def favicon_path
1272 icon = (current_theme && current_theme.favicon?) ? current_theme.favicon_path : '/favicon.ico'
1272 icon = (current_theme && current_theme.favicon?) ? current_theme.favicon_path : '/favicon.ico'
1273 image_path(icon)
1273 image_path(icon)
1274 end
1274 end
1275
1275
1276 # Returns the full URL to the favicon
1276 # Returns the full URL to the favicon
1277 def favicon_url
1277 def favicon_url
1278 # TODO: use #image_url introduced in Rails4
1278 # TODO: use #image_url introduced in Rails4
1279 path = favicon_path
1279 path = favicon_path
1280 base = url_for(:controller => 'welcome', :action => 'index', :only_path => false)
1280 base = url_for(:controller => 'welcome', :action => 'index', :only_path => false)
1281 base.sub(%r{/+$},'') + '/' + path.sub(%r{^/+},'')
1281 base.sub(%r{/+$},'') + '/' + path.sub(%r{^/+},'')
1282 end
1282 end
1283
1283
1284 def robot_exclusion_tag
1284 def robot_exclusion_tag
1285 '<meta name="robots" content="noindex,follow,noarchive" />'.html_safe
1285 '<meta name="robots" content="noindex,follow,noarchive" />'.html_safe
1286 end
1286 end
1287
1287
1288 # Returns true if arg is expected in the API response
1288 # Returns true if arg is expected in the API response
1289 def include_in_api_response?(arg)
1289 def include_in_api_response?(arg)
1290 unless @included_in_api_response
1290 unless @included_in_api_response
1291 param = params[:include]
1291 param = params[:include]
1292 @included_in_api_response = param.is_a?(Array) ? param.collect(&:to_s) : param.to_s.split(',')
1292 @included_in_api_response = param.is_a?(Array) ? param.collect(&:to_s) : param.to_s.split(',')
1293 @included_in_api_response.collect!(&:strip)
1293 @included_in_api_response.collect!(&:strip)
1294 end
1294 end
1295 @included_in_api_response.include?(arg.to_s)
1295 @included_in_api_response.include?(arg.to_s)
1296 end
1296 end
1297
1297
1298 # Returns options or nil if nometa param or X-Redmine-Nometa header
1298 # Returns options or nil if nometa param or X-Redmine-Nometa header
1299 # was set in the request
1299 # was set in the request
1300 def api_meta(options)
1300 def api_meta(options)
1301 if params[:nometa].present? || request.headers['X-Redmine-Nometa']
1301 if params[:nometa].present? || request.headers['X-Redmine-Nometa']
1302 # compatibility mode for activeresource clients that raise
1302 # compatibility mode for activeresource clients that raise
1303 # an error when deserializing an array with attributes
1303 # an error when deserializing an array with attributes
1304 nil
1304 nil
1305 else
1305 else
1306 options
1306 options
1307 end
1307 end
1308 end
1308 end
1309
1309
1310 private
1310 private
1311
1311
1312 def wiki_helper
1312 def wiki_helper
1313 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
1313 helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
1314 extend helper
1314 extend helper
1315 return self
1315 return self
1316 end
1316 end
1317
1317
1318 def link_to_content_update(text, url_params = {}, html_options = {})
1318 def link_to_content_update(text, url_params = {}, html_options = {})
1319 link_to(text, url_params, html_options)
1319 link_to(text, url_params, html_options)
1320 end
1320 end
1321 end
1321 end
@@ -1,4214 +1,4214
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class IssuesControllerTest < ActionController::TestCase
20 class IssuesControllerTest < ActionController::TestCase
21 fixtures :projects,
21 fixtures :projects,
22 :users, :email_addresses,
22 :users, :email_addresses,
23 :roles,
23 :roles,
24 :members,
24 :members,
25 :member_roles,
25 :member_roles,
26 :issues,
26 :issues,
27 :issue_statuses,
27 :issue_statuses,
28 :issue_relations,
28 :issue_relations,
29 :versions,
29 :versions,
30 :trackers,
30 :trackers,
31 :projects_trackers,
31 :projects_trackers,
32 :issue_categories,
32 :issue_categories,
33 :enabled_modules,
33 :enabled_modules,
34 :enumerations,
34 :enumerations,
35 :attachments,
35 :attachments,
36 :workflows,
36 :workflows,
37 :custom_fields,
37 :custom_fields,
38 :custom_values,
38 :custom_values,
39 :custom_fields_projects,
39 :custom_fields_projects,
40 :custom_fields_trackers,
40 :custom_fields_trackers,
41 :time_entries,
41 :time_entries,
42 :journals,
42 :journals,
43 :journal_details,
43 :journal_details,
44 :queries,
44 :queries,
45 :repositories,
45 :repositories,
46 :changesets
46 :changesets
47
47
48 include Redmine::I18n
48 include Redmine::I18n
49
49
50 def setup
50 def setup
51 User.current = nil
51 User.current = nil
52 end
52 end
53
53
54 def test_index
54 def test_index
55 with_settings :default_language => "en" do
55 with_settings :default_language => "en" do
56 get :index
56 get :index
57 assert_response :success
57 assert_response :success
58 assert_template 'index'
58 assert_template 'index'
59 assert_not_nil assigns(:issues)
59 assert_not_nil assigns(:issues)
60 assert_nil assigns(:project)
60 assert_nil assigns(:project)
61
61
62 # links to visible issues
62 # links to visible issues
63 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
63 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
64 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
64 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
65 # private projects hidden
65 # private projects hidden
66 assert_select 'a[href="/issues/6"]', 0
66 assert_select 'a[href="/issues/6"]', 0
67 assert_select 'a[href="/issues/4"]', 0
67 assert_select 'a[href="/issues/4"]', 0
68 # project column
68 # project column
69 assert_select 'th', :text => /Project/
69 assert_select 'th', :text => /Project/
70 end
70 end
71 end
71 end
72
72
73 def test_index_should_not_list_issues_when_module_disabled
73 def test_index_should_not_list_issues_when_module_disabled
74 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
74 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
75 get :index
75 get :index
76 assert_response :success
76 assert_response :success
77 assert_template 'index'
77 assert_template 'index'
78 assert_not_nil assigns(:issues)
78 assert_not_nil assigns(:issues)
79 assert_nil assigns(:project)
79 assert_nil assigns(:project)
80
80
81 assert_select 'a[href="/issues/1"]', 0
81 assert_select 'a[href="/issues/1"]', 0
82 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
82 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
83 end
83 end
84
84
85 def test_index_should_list_visible_issues_only
85 def test_index_should_list_visible_issues_only
86 get :index, :per_page => 100
86 get :index, :per_page => 100
87 assert_response :success
87 assert_response :success
88 assert_not_nil assigns(:issues)
88 assert_not_nil assigns(:issues)
89 assert_nil assigns(:issues).detect {|issue| !issue.visible?}
89 assert_nil assigns(:issues).detect {|issue| !issue.visible?}
90 end
90 end
91
91
92 def test_index_with_project
92 def test_index_with_project
93 Setting.display_subprojects_issues = 0
93 Setting.display_subprojects_issues = 0
94 get :index, :project_id => 1
94 get :index, :project_id => 1
95 assert_response :success
95 assert_response :success
96 assert_template 'index'
96 assert_template 'index'
97 assert_not_nil assigns(:issues)
97 assert_not_nil assigns(:issues)
98
98
99 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
99 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
100 assert_select 'a[href="/issues/5"]', 0
100 assert_select 'a[href="/issues/5"]', 0
101 end
101 end
102
102
103 def test_index_with_project_and_subprojects
103 def test_index_with_project_and_subprojects
104 Setting.display_subprojects_issues = 1
104 Setting.display_subprojects_issues = 1
105 get :index, :project_id => 1
105 get :index, :project_id => 1
106 assert_response :success
106 assert_response :success
107 assert_template 'index'
107 assert_template 'index'
108 assert_not_nil assigns(:issues)
108 assert_not_nil assigns(:issues)
109
109
110 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
110 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
111 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
111 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
112 assert_select 'a[href="/issues/6"]', 0
112 assert_select 'a[href="/issues/6"]', 0
113 end
113 end
114
114
115 def test_index_with_project_and_subprojects_should_show_private_subprojects_with_permission
115 def test_index_with_project_and_subprojects_should_show_private_subprojects_with_permission
116 @request.session[:user_id] = 2
116 @request.session[:user_id] = 2
117 Setting.display_subprojects_issues = 1
117 Setting.display_subprojects_issues = 1
118 get :index, :project_id => 1
118 get :index, :project_id => 1
119 assert_response :success
119 assert_response :success
120 assert_template 'index'
120 assert_template 'index'
121 assert_not_nil assigns(:issues)
121 assert_not_nil assigns(:issues)
122
122
123 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
123 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
124 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
124 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
125 assert_select 'a[href="/issues/6"]', :text => /Issue of a private subproject/
125 assert_select 'a[href="/issues/6"]', :text => /Issue of a private subproject/
126 end
126 end
127
127
128 def test_index_with_project_and_default_filter
128 def test_index_with_project_and_default_filter
129 get :index, :project_id => 1, :set_filter => 1
129 get :index, :project_id => 1, :set_filter => 1
130 assert_response :success
130 assert_response :success
131 assert_template 'index'
131 assert_template 'index'
132 assert_not_nil assigns(:issues)
132 assert_not_nil assigns(:issues)
133
133
134 query = assigns(:query)
134 query = assigns(:query)
135 assert_not_nil query
135 assert_not_nil query
136 # default filter
136 # default filter
137 assert_equal({'status_id' => {:operator => 'o', :values => ['']}}, query.filters)
137 assert_equal({'status_id' => {:operator => 'o', :values => ['']}}, query.filters)
138 end
138 end
139
139
140 def test_index_with_project_and_filter
140 def test_index_with_project_and_filter
141 get :index, :project_id => 1, :set_filter => 1,
141 get :index, :project_id => 1, :set_filter => 1,
142 :f => ['tracker_id'],
142 :f => ['tracker_id'],
143 :op => {'tracker_id' => '='},
143 :op => {'tracker_id' => '='},
144 :v => {'tracker_id' => ['1']}
144 :v => {'tracker_id' => ['1']}
145 assert_response :success
145 assert_response :success
146 assert_template 'index'
146 assert_template 'index'
147 assert_not_nil assigns(:issues)
147 assert_not_nil assigns(:issues)
148
148
149 query = assigns(:query)
149 query = assigns(:query)
150 assert_not_nil query
150 assert_not_nil query
151 assert_equal({'tracker_id' => {:operator => '=', :values => ['1']}}, query.filters)
151 assert_equal({'tracker_id' => {:operator => '=', :values => ['1']}}, query.filters)
152 end
152 end
153
153
154 def test_index_with_short_filters
154 def test_index_with_short_filters
155 to_test = {
155 to_test = {
156 'status_id' => {
156 'status_id' => {
157 'o' => { :op => 'o', :values => [''] },
157 'o' => { :op => 'o', :values => [''] },
158 'c' => { :op => 'c', :values => [''] },
158 'c' => { :op => 'c', :values => [''] },
159 '7' => { :op => '=', :values => ['7'] },
159 '7' => { :op => '=', :values => ['7'] },
160 '7|3|4' => { :op => '=', :values => ['7', '3', '4'] },
160 '7|3|4' => { :op => '=', :values => ['7', '3', '4'] },
161 '=7' => { :op => '=', :values => ['7'] },
161 '=7' => { :op => '=', :values => ['7'] },
162 '!3' => { :op => '!', :values => ['3'] },
162 '!3' => { :op => '!', :values => ['3'] },
163 '!7|3|4' => { :op => '!', :values => ['7', '3', '4'] }},
163 '!7|3|4' => { :op => '!', :values => ['7', '3', '4'] }},
164 'subject' => {
164 'subject' => {
165 'This is a subject' => { :op => '=', :values => ['This is a subject'] },
165 'This is a subject' => { :op => '=', :values => ['This is a subject'] },
166 'o' => { :op => '=', :values => ['o'] },
166 'o' => { :op => '=', :values => ['o'] },
167 '~This is part of a subject' => { :op => '~', :values => ['This is part of a subject'] },
167 '~This is part of a subject' => { :op => '~', :values => ['This is part of a subject'] },
168 '!~This is part of a subject' => { :op => '!~', :values => ['This is part of a subject'] }},
168 '!~This is part of a subject' => { :op => '!~', :values => ['This is part of a subject'] }},
169 'tracker_id' => {
169 'tracker_id' => {
170 '3' => { :op => '=', :values => ['3'] },
170 '3' => { :op => '=', :values => ['3'] },
171 '=3' => { :op => '=', :values => ['3'] }},
171 '=3' => { :op => '=', :values => ['3'] }},
172 'start_date' => {
172 'start_date' => {
173 '2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
173 '2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
174 '=2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
174 '=2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
175 '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
175 '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
176 '<=2011-10-12' => { :op => '<=', :values => ['2011-10-12'] },
176 '<=2011-10-12' => { :op => '<=', :values => ['2011-10-12'] },
177 '><2011-10-01|2011-10-30' => { :op => '><', :values => ['2011-10-01', '2011-10-30'] },
177 '><2011-10-01|2011-10-30' => { :op => '><', :values => ['2011-10-01', '2011-10-30'] },
178 '<t+2' => { :op => '<t+', :values => ['2'] },
178 '<t+2' => { :op => '<t+', :values => ['2'] },
179 '>t+2' => { :op => '>t+', :values => ['2'] },
179 '>t+2' => { :op => '>t+', :values => ['2'] },
180 't+2' => { :op => 't+', :values => ['2'] },
180 't+2' => { :op => 't+', :values => ['2'] },
181 't' => { :op => 't', :values => [''] },
181 't' => { :op => 't', :values => [''] },
182 'w' => { :op => 'w', :values => [''] },
182 'w' => { :op => 'w', :values => [''] },
183 '>t-2' => { :op => '>t-', :values => ['2'] },
183 '>t-2' => { :op => '>t-', :values => ['2'] },
184 '<t-2' => { :op => '<t-', :values => ['2'] },
184 '<t-2' => { :op => '<t-', :values => ['2'] },
185 't-2' => { :op => 't-', :values => ['2'] }},
185 't-2' => { :op => 't-', :values => ['2'] }},
186 'created_on' => {
186 'created_on' => {
187 '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
187 '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
188 '<t-2' => { :op => '<t-', :values => ['2'] },
188 '<t-2' => { :op => '<t-', :values => ['2'] },
189 '>t-2' => { :op => '>t-', :values => ['2'] },
189 '>t-2' => { :op => '>t-', :values => ['2'] },
190 't-2' => { :op => 't-', :values => ['2'] }},
190 't-2' => { :op => 't-', :values => ['2'] }},
191 'cf_1' => {
191 'cf_1' => {
192 'c' => { :op => '=', :values => ['c'] },
192 'c' => { :op => '=', :values => ['c'] },
193 '!c' => { :op => '!', :values => ['c'] },
193 '!c' => { :op => '!', :values => ['c'] },
194 '!*' => { :op => '!*', :values => [''] },
194 '!*' => { :op => '!*', :values => [''] },
195 '*' => { :op => '*', :values => [''] }},
195 '*' => { :op => '*', :values => [''] }},
196 'estimated_hours' => {
196 'estimated_hours' => {
197 '=13.4' => { :op => '=', :values => ['13.4'] },
197 '=13.4' => { :op => '=', :values => ['13.4'] },
198 '>=45' => { :op => '>=', :values => ['45'] },
198 '>=45' => { :op => '>=', :values => ['45'] },
199 '<=125' => { :op => '<=', :values => ['125'] },
199 '<=125' => { :op => '<=', :values => ['125'] },
200 '><10.5|20.5' => { :op => '><', :values => ['10.5', '20.5'] },
200 '><10.5|20.5' => { :op => '><', :values => ['10.5', '20.5'] },
201 '!*' => { :op => '!*', :values => [''] },
201 '!*' => { :op => '!*', :values => [''] },
202 '*' => { :op => '*', :values => [''] }}
202 '*' => { :op => '*', :values => [''] }}
203 }
203 }
204
204
205 default_filter = { 'status_id' => {:operator => 'o', :values => [''] }}
205 default_filter = { 'status_id' => {:operator => 'o', :values => [''] }}
206
206
207 to_test.each do |field, expression_and_expected|
207 to_test.each do |field, expression_and_expected|
208 expression_and_expected.each do |filter_expression, expected|
208 expression_and_expected.each do |filter_expression, expected|
209
209
210 get :index, :set_filter => 1, field => filter_expression
210 get :index, :set_filter => 1, field => filter_expression
211
211
212 assert_response :success
212 assert_response :success
213 assert_template 'index'
213 assert_template 'index'
214 assert_not_nil assigns(:issues)
214 assert_not_nil assigns(:issues)
215
215
216 query = assigns(:query)
216 query = assigns(:query)
217 assert_not_nil query
217 assert_not_nil query
218 assert query.has_filter?(field)
218 assert query.has_filter?(field)
219 assert_equal(default_filter.merge({field => {:operator => expected[:op], :values => expected[:values]}}), query.filters)
219 assert_equal(default_filter.merge({field => {:operator => expected[:op], :values => expected[:values]}}), query.filters)
220 end
220 end
221 end
221 end
222 end
222 end
223
223
224 def test_index_with_project_and_empty_filters
224 def test_index_with_project_and_empty_filters
225 get :index, :project_id => 1, :set_filter => 1, :fields => ['']
225 get :index, :project_id => 1, :set_filter => 1, :fields => ['']
226 assert_response :success
226 assert_response :success
227 assert_template 'index'
227 assert_template 'index'
228 assert_not_nil assigns(:issues)
228 assert_not_nil assigns(:issues)
229
229
230 query = assigns(:query)
230 query = assigns(:query)
231 assert_not_nil query
231 assert_not_nil query
232 # no filter
232 # no filter
233 assert_equal({}, query.filters)
233 assert_equal({}, query.filters)
234 end
234 end
235
235
236 def test_index_with_project_custom_field_filter
236 def test_index_with_project_custom_field_filter
237 field = ProjectCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
237 field = ProjectCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
238 CustomValue.create!(:custom_field => field, :customized => Project.find(3), :value => 'Foo')
238 CustomValue.create!(:custom_field => field, :customized => Project.find(3), :value => 'Foo')
239 CustomValue.create!(:custom_field => field, :customized => Project.find(5), :value => 'Foo')
239 CustomValue.create!(:custom_field => field, :customized => Project.find(5), :value => 'Foo')
240 filter_name = "project.cf_#{field.id}"
240 filter_name = "project.cf_#{field.id}"
241 @request.session[:user_id] = 1
241 @request.session[:user_id] = 1
242
242
243 get :index, :set_filter => 1,
243 get :index, :set_filter => 1,
244 :f => [filter_name],
244 :f => [filter_name],
245 :op => {filter_name => '='},
245 :op => {filter_name => '='},
246 :v => {filter_name => ['Foo']}
246 :v => {filter_name => ['Foo']}
247 assert_response :success
247 assert_response :success
248 assert_template 'index'
248 assert_template 'index'
249 assert_equal [3, 5], assigns(:issues).map(&:project_id).uniq.sort
249 assert_equal [3, 5], assigns(:issues).map(&:project_id).uniq.sort
250 end
250 end
251
251
252 def test_index_with_query
252 def test_index_with_query
253 get :index, :project_id => 1, :query_id => 5
253 get :index, :project_id => 1, :query_id => 5
254 assert_response :success
254 assert_response :success
255 assert_template 'index'
255 assert_template 'index'
256 assert_not_nil assigns(:issues)
256 assert_not_nil assigns(:issues)
257 assert_nil assigns(:issue_count_by_group)
257 assert_nil assigns(:issue_count_by_group)
258 end
258 end
259
259
260 def test_index_with_query_grouped_by_tracker
260 def test_index_with_query_grouped_by_tracker
261 get :index, :project_id => 1, :query_id => 6
261 get :index, :project_id => 1, :query_id => 6
262 assert_response :success
262 assert_response :success
263 assert_template 'index'
263 assert_template 'index'
264 assert_not_nil assigns(:issues)
264 assert_not_nil assigns(:issues)
265 assert_not_nil assigns(:issue_count_by_group)
265 assert_not_nil assigns(:issue_count_by_group)
266 end
266 end
267
267
268 def test_index_with_query_grouped_by_list_custom_field
268 def test_index_with_query_grouped_by_list_custom_field
269 get :index, :project_id => 1, :query_id => 9
269 get :index, :project_id => 1, :query_id => 9
270 assert_response :success
270 assert_response :success
271 assert_template 'index'
271 assert_template 'index'
272 assert_not_nil assigns(:issues)
272 assert_not_nil assigns(:issues)
273 assert_not_nil assigns(:issue_count_by_group)
273 assert_not_nil assigns(:issue_count_by_group)
274 end
274 end
275
275
276 def test_index_with_query_grouped_by_user_custom_field
276 def test_index_with_query_grouped_by_user_custom_field
277 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
277 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
278 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
278 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
279 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
279 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
280 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
280 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
281 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
281 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
282
282
283 get :index, :project_id => 1, :set_filter => 1, :group_by => "cf_#{cf.id}"
283 get :index, :project_id => 1, :set_filter => 1, :group_by => "cf_#{cf.id}"
284 assert_response :success
284 assert_response :success
285
285
286 assert_select 'tr.group', 3
286 assert_select 'tr.group', 3
287 assert_select 'tr.group' do
287 assert_select 'tr.group' do
288 assert_select 'a', :text => 'John Smith'
288 assert_select 'a', :text => 'John Smith'
289 assert_select 'span.count', :text => '1'
289 assert_select 'span.count', :text => '1'
290 end
290 end
291 assert_select 'tr.group' do
291 assert_select 'tr.group' do
292 assert_select 'a', :text => 'Dave Lopper'
292 assert_select 'a', :text => 'Dave Lopper'
293 assert_select 'span.count', :text => '2'
293 assert_select 'span.count', :text => '2'
294 end
294 end
295 end
295 end
296
296
297 def test_index_grouped_by_boolean_custom_field_should_distinguish_blank_and_false_values
297 def test_index_grouped_by_boolean_custom_field_should_distinguish_blank_and_false_values
298 cf = IssueCustomField.create!(:name => 'Bool', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'bool')
298 cf = IssueCustomField.create!(:name => 'Bool', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'bool')
299 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '1')
299 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '1')
300 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '0')
300 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '0')
301 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '')
301 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '')
302
302
303 with_settings :default_language => 'en' do
303 with_settings :default_language => 'en' do
304 get :index, :project_id => 1, :set_filter => 1, :group_by => "cf_#{cf.id}"
304 get :index, :project_id => 1, :set_filter => 1, :group_by => "cf_#{cf.id}"
305 assert_response :success
305 assert_response :success
306 end
306 end
307
307
308 assert_select 'tr.group', 3
308 assert_select 'tr.group', 3
309 assert_select 'tr.group', :text => /Yes/
309 assert_select 'tr.group', :text => /Yes/
310 assert_select 'tr.group', :text => /No/
310 assert_select 'tr.group', :text => /No/
311 assert_select 'tr.group', :text => /blank/
311 assert_select 'tr.group', :text => /blank/
312 end
312 end
313
313
314 def test_index_grouped_by_boolean_custom_field_with_false_group_in_first_position_should_show_the_group
314 def test_index_grouped_by_boolean_custom_field_with_false_group_in_first_position_should_show_the_group
315 cf = IssueCustomField.create!(:name => 'Bool', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'bool', :is_filter => true)
315 cf = IssueCustomField.create!(:name => 'Bool', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'bool', :is_filter => true)
316 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '0')
316 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '0')
317 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '0')
317 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '0')
318
318
319 with_settings :default_language => 'en' do
319 with_settings :default_language => 'en' do
320 get :index, :project_id => 1, :set_filter => 1, "cf_#{cf.id}" => "*", :group_by => "cf_#{cf.id}"
320 get :index, :project_id => 1, :set_filter => 1, "cf_#{cf.id}" => "*", :group_by => "cf_#{cf.id}"
321 assert_response :success
321 assert_response :success
322 assert_equal [1, 2], assigns(:issues).map(&:id).sort
322 assert_equal [1, 2], assigns(:issues).map(&:id).sort
323 end
323 end
324
324
325 assert_select 'tr.group', 1
325 assert_select 'tr.group', 1
326 assert_select 'tr.group', :text => /No/
326 assert_select 'tr.group', :text => /No/
327 end
327 end
328
328
329 def test_index_with_query_grouped_by_tracker_in_normal_order
329 def test_index_with_query_grouped_by_tracker_in_normal_order
330 3.times {|i| Issue.generate!(:tracker_id => (i + 1))}
330 3.times {|i| Issue.generate!(:tracker_id => (i + 1))}
331
331
332 get :index, :set_filter => 1, :group_by => 'tracker', :sort => 'id:desc'
332 get :index, :set_filter => 1, :group_by => 'tracker', :sort => 'id:desc'
333 assert_response :success
333 assert_response :success
334
334
335 trackers = assigns(:issues).map(&:tracker).uniq
335 trackers = assigns(:issues).map(&:tracker).uniq
336 assert_equal [1, 2, 3], trackers.map(&:id)
336 assert_equal [1, 2, 3], trackers.map(&:id)
337 end
337 end
338
338
339 def test_index_with_query_grouped_by_tracker_in_reverse_order
339 def test_index_with_query_grouped_by_tracker_in_reverse_order
340 3.times {|i| Issue.generate!(:tracker_id => (i + 1))}
340 3.times {|i| Issue.generate!(:tracker_id => (i + 1))}
341
341
342 get :index, :set_filter => 1, :group_by => 'tracker', :sort => 'id:desc,tracker:desc'
342 get :index, :set_filter => 1, :group_by => 'tracker', :sort => 'id:desc,tracker:desc'
343 assert_response :success
343 assert_response :success
344
344
345 trackers = assigns(:issues).map(&:tracker).uniq
345 trackers = assigns(:issues).map(&:tracker).uniq
346 assert_equal [3, 2, 1], trackers.map(&:id)
346 assert_equal [3, 2, 1], trackers.map(&:id)
347 end
347 end
348
348
349 def test_index_with_query_id_and_project_id_should_set_session_query
349 def test_index_with_query_id_and_project_id_should_set_session_query
350 get :index, :project_id => 1, :query_id => 4
350 get :index, :project_id => 1, :query_id => 4
351 assert_response :success
351 assert_response :success
352 assert_kind_of Hash, session[:query]
352 assert_kind_of Hash, session[:query]
353 assert_equal 4, session[:query][:id]
353 assert_equal 4, session[:query][:id]
354 assert_equal 1, session[:query][:project_id]
354 assert_equal 1, session[:query][:project_id]
355 end
355 end
356
356
357 def test_index_with_invalid_query_id_should_respond_404
357 def test_index_with_invalid_query_id_should_respond_404
358 get :index, :project_id => 1, :query_id => 999
358 get :index, :project_id => 1, :query_id => 999
359 assert_response 404
359 assert_response 404
360 end
360 end
361
361
362 def test_index_with_cross_project_query_in_session_should_show_project_issues
362 def test_index_with_cross_project_query_in_session_should_show_project_issues
363 q = IssueQuery.create!(:name => "test", :user_id => 2, :visibility => IssueQuery::VISIBILITY_PRIVATE, :project => nil)
363 q = IssueQuery.create!(:name => "test", :user_id => 2, :visibility => IssueQuery::VISIBILITY_PRIVATE, :project => nil)
364 @request.session[:query] = {:id => q.id, :project_id => 1}
364 @request.session[:query] = {:id => q.id, :project_id => 1}
365
365
366 with_settings :display_subprojects_issues => '0' do
366 with_settings :display_subprojects_issues => '0' do
367 get :index, :project_id => 1
367 get :index, :project_id => 1
368 end
368 end
369 assert_response :success
369 assert_response :success
370 assert_not_nil assigns(:query)
370 assert_not_nil assigns(:query)
371 assert_equal q.id, assigns(:query).id
371 assert_equal q.id, assigns(:query).id
372 assert_equal 1, assigns(:query).project_id
372 assert_equal 1, assigns(:query).project_id
373 assert_equal [1], assigns(:issues).map(&:project_id).uniq
373 assert_equal [1], assigns(:issues).map(&:project_id).uniq
374 end
374 end
375
375
376 def test_private_query_should_not_be_available_to_other_users
376 def test_private_query_should_not_be_available_to_other_users
377 q = IssueQuery.create!(:name => "private", :user => User.find(2), :visibility => IssueQuery::VISIBILITY_PRIVATE, :project => nil)
377 q = IssueQuery.create!(:name => "private", :user => User.find(2), :visibility => IssueQuery::VISIBILITY_PRIVATE, :project => nil)
378 @request.session[:user_id] = 3
378 @request.session[:user_id] = 3
379
379
380 get :index, :query_id => q.id
380 get :index, :query_id => q.id
381 assert_response 403
381 assert_response 403
382 end
382 end
383
383
384 def test_private_query_should_be_available_to_its_user
384 def test_private_query_should_be_available_to_its_user
385 q = IssueQuery.create!(:name => "private", :user => User.find(2), :visibility => IssueQuery::VISIBILITY_PRIVATE, :project => nil)
385 q = IssueQuery.create!(:name => "private", :user => User.find(2), :visibility => IssueQuery::VISIBILITY_PRIVATE, :project => nil)
386 @request.session[:user_id] = 2
386 @request.session[:user_id] = 2
387
387
388 get :index, :query_id => q.id
388 get :index, :query_id => q.id
389 assert_response :success
389 assert_response :success
390 end
390 end
391
391
392 def test_public_query_should_be_available_to_other_users
392 def test_public_query_should_be_available_to_other_users
393 q = IssueQuery.create!(:name => "private", :user => User.find(2), :visibility => IssueQuery::VISIBILITY_PUBLIC, :project => nil)
393 q = IssueQuery.create!(:name => "private", :user => User.find(2), :visibility => IssueQuery::VISIBILITY_PUBLIC, :project => nil)
394 @request.session[:user_id] = 3
394 @request.session[:user_id] = 3
395
395
396 get :index, :query_id => q.id
396 get :index, :query_id => q.id
397 assert_response :success
397 assert_response :success
398 end
398 end
399
399
400 def test_index_should_omit_page_param_in_export_links
400 def test_index_should_omit_page_param_in_export_links
401 get :index, :page => 2
401 get :index, :page => 2
402 assert_response :success
402 assert_response :success
403 assert_select 'a.atom[href="/issues.atom"]'
403 assert_select 'a.atom[href="/issues.atom"]'
404 assert_select 'a.csv[href="/issues.csv"]'
404 assert_select 'a.csv[href="/issues.csv"]'
405 assert_select 'a.pdf[href="/issues.pdf"]'
405 assert_select 'a.pdf[href="/issues.pdf"]'
406 assert_select 'form#csv-export-form[action="/issues.csv"]'
406 assert_select 'form#csv-export-form[action="/issues.csv"]'
407 end
407 end
408
408
409 def test_index_should_not_warn_when_not_exceeding_export_limit
409 def test_index_should_not_warn_when_not_exceeding_export_limit
410 with_settings :issues_export_limit => 200 do
410 with_settings :issues_export_limit => 200 do
411 get :index
411 get :index
412 assert_select '#csv-export-options p.icon-warning', 0
412 assert_select '#csv-export-options p.icon-warning', 0
413 end
413 end
414 end
414 end
415
415
416 def test_index_should_warn_when_exceeding_export_limit
416 def test_index_should_warn_when_exceeding_export_limit
417 with_settings :issues_export_limit => 2 do
417 with_settings :issues_export_limit => 2 do
418 get :index
418 get :index
419 assert_select '#csv-export-options p.icon-warning', :text => %r{limit: 2}
419 assert_select '#csv-export-options p.icon-warning', :text => %r{limit: 2}
420 end
420 end
421 end
421 end
422
422
423 def test_index_csv
423 def test_index_csv
424 get :index, :format => 'csv'
424 get :index, :format => 'csv'
425 assert_response :success
425 assert_response :success
426 assert_not_nil assigns(:issues)
426 assert_not_nil assigns(:issues)
427 assert_equal 'text/csv; header=present', @response.content_type
427 assert_equal 'text/csv; header=present', @response.content_type
428 assert @response.body.starts_with?("#,")
428 assert @response.body.starts_with?("#,")
429 lines = @response.body.chomp.split("\n")
429 lines = @response.body.chomp.split("\n")
430 assert_equal assigns(:query).columns.size, lines[0].split(',').size
430 assert_equal assigns(:query).columns.size, lines[0].split(',').size
431 end
431 end
432
432
433 def test_index_csv_with_project
433 def test_index_csv_with_project
434 get :index, :project_id => 1, :format => 'csv'
434 get :index, :project_id => 1, :format => 'csv'
435 assert_response :success
435 assert_response :success
436 assert_not_nil assigns(:issues)
436 assert_not_nil assigns(:issues)
437 assert_equal 'text/csv; header=present', @response.content_type
437 assert_equal 'text/csv; header=present', @response.content_type
438 end
438 end
439
439
440 def test_index_csv_with_description
440 def test_index_csv_with_description
441 Issue.generate!(:description => 'test_index_csv_with_description')
441 Issue.generate!(:description => 'test_index_csv_with_description')
442
442
443 with_settings :default_language => 'en' do
443 with_settings :default_language => 'en' do
444 get :index, :format => 'csv', :description => '1'
444 get :index, :format => 'csv', :description => '1'
445 assert_response :success
445 assert_response :success
446 assert_not_nil assigns(:issues)
446 assert_not_nil assigns(:issues)
447 end
447 end
448
448
449 assert_equal 'text/csv; header=present', response.content_type
449 assert_equal 'text/csv; header=present', response.content_type
450 headers = response.body.chomp.split("\n").first.split(',')
450 headers = response.body.chomp.split("\n").first.split(',')
451 assert_include 'Description', headers
451 assert_include 'Description', headers
452 assert_include 'test_index_csv_with_description', response.body
452 assert_include 'test_index_csv_with_description', response.body
453 end
453 end
454
454
455 def test_index_csv_with_spent_time_column
455 def test_index_csv_with_spent_time_column
456 issue = Issue.create!(:project_id => 1, :tracker_id => 1, :subject => 'test_index_csv_with_spent_time_column', :author_id => 2)
456 issue = Issue.create!(:project_id => 1, :tracker_id => 1, :subject => 'test_index_csv_with_spent_time_column', :author_id => 2)
457 TimeEntry.create!(:project => issue.project, :issue => issue, :hours => 7.33, :user => User.find(2), :spent_on => Date.today)
457 TimeEntry.create!(:project => issue.project, :issue => issue, :hours => 7.33, :user => User.find(2), :spent_on => Date.today)
458
458
459 get :index, :format => 'csv', :set_filter => '1', :c => %w(subject spent_hours)
459 get :index, :format => 'csv', :set_filter => '1', :c => %w(subject spent_hours)
460 assert_response :success
460 assert_response :success
461 assert_equal 'text/csv; header=present', @response.content_type
461 assert_equal 'text/csv; header=present', @response.content_type
462 lines = @response.body.chomp.split("\n")
462 lines = @response.body.chomp.split("\n")
463 assert_include "#{issue.id},#{issue.subject},7.33", lines
463 assert_include "#{issue.id},#{issue.subject},7.33", lines
464 end
464 end
465
465
466 def test_index_csv_with_all_columns
466 def test_index_csv_with_all_columns
467 get :index, :format => 'csv', :columns => 'all'
467 get :index, :format => 'csv', :columns => 'all'
468 assert_response :success
468 assert_response :success
469 assert_not_nil assigns(:issues)
469 assert_not_nil assigns(:issues)
470 assert_equal 'text/csv; header=present', @response.content_type
470 assert_equal 'text/csv; header=present', @response.content_type
471 assert_match /\A#,/, response.body
471 assert_match /\A#,/, response.body
472 lines = response.body.chomp.split("\n")
472 lines = response.body.chomp.split("\n")
473 assert_equal assigns(:query).available_inline_columns.size, lines[0].split(',').size
473 assert_equal assigns(:query).available_inline_columns.size, lines[0].split(',').size
474 end
474 end
475
475
476 def test_index_csv_with_multi_column_field
476 def test_index_csv_with_multi_column_field
477 CustomField.find(1).update_attribute :multiple, true
477 CustomField.find(1).update_attribute :multiple, true
478 issue = Issue.find(1)
478 issue = Issue.find(1)
479 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
479 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
480 issue.save!
480 issue.save!
481
481
482 get :index, :format => 'csv', :columns => 'all'
482 get :index, :format => 'csv', :columns => 'all'
483 assert_response :success
483 assert_response :success
484 lines = @response.body.chomp.split("\n")
484 lines = @response.body.chomp.split("\n")
485 assert lines.detect {|line| line.include?('"MySQL, Oracle"')}
485 assert lines.detect {|line| line.include?('"MySQL, Oracle"')}
486 end
486 end
487
487
488 def test_index_csv_should_format_float_custom_fields_with_csv_decimal_separator
488 def test_index_csv_should_format_float_custom_fields_with_csv_decimal_separator
489 field = IssueCustomField.create!(:name => 'Float', :is_for_all => true, :tracker_ids => [1], :field_format => 'float')
489 field = IssueCustomField.create!(:name => 'Float', :is_for_all => true, :tracker_ids => [1], :field_format => 'float')
490 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id => '185.6'})
490 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id => '185.6'})
491
491
492 with_settings :default_language => 'fr' do
492 with_settings :default_language => 'fr' do
493 get :index, :format => 'csv', :columns => 'all'
493 get :index, :format => 'csv', :columns => 'all'
494 assert_response :success
494 assert_response :success
495 issue_line = response.body.chomp.split("\n").map {|line| line.split(';')}.detect {|line| line[0]==issue.id.to_s}
495 issue_line = response.body.chomp.split("\n").map {|line| line.split(';')}.detect {|line| line[0]==issue.id.to_s}
496 assert_include '185,60', issue_line
496 assert_include '185,60', issue_line
497 end
497 end
498
498
499 with_settings :default_language => 'en' do
499 with_settings :default_language => 'en' do
500 get :index, :format => 'csv', :columns => 'all'
500 get :index, :format => 'csv', :columns => 'all'
501 assert_response :success
501 assert_response :success
502 issue_line = response.body.chomp.split("\n").map {|line| line.split(',')}.detect {|line| line[0]==issue.id.to_s}
502 issue_line = response.body.chomp.split("\n").map {|line| line.split(',')}.detect {|line| line[0]==issue.id.to_s}
503 assert_include '185.60', issue_line
503 assert_include '185.60', issue_line
504 end
504 end
505 end
505 end
506
506
507 def test_index_csv_should_fill_parent_column_with_parent_id
507 def test_index_csv_should_fill_parent_column_with_parent_id
508 Issue.delete_all
508 Issue.delete_all
509 parent = Issue.generate!
509 parent = Issue.generate!
510 child = Issue.generate!(:parent_issue_id => parent.id)
510 child = Issue.generate!(:parent_issue_id => parent.id)
511
511
512 with_settings :default_language => 'en' do
512 with_settings :default_language => 'en' do
513 get :index, :format => 'csv', :c => %w(parent)
513 get :index, :format => 'csv', :c => %w(parent)
514 end
514 end
515 lines = response.body.split("\n")
515 lines = response.body.split("\n")
516 assert_include "#{child.id},#{parent.id}", lines
516 assert_include "#{child.id},#{parent.id}", lines
517 end
517 end
518
518
519 def test_index_csv_big_5
519 def test_index_csv_big_5
520 with_settings :default_language => "zh-TW" do
520 with_settings :default_language => "zh-TW" do
521 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88".force_encoding('UTF-8')
521 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88".force_encoding('UTF-8')
522 str_big5 = "\xa4@\xa4\xeb".force_encoding('Big5')
522 str_big5 = "\xa4@\xa4\xeb".force_encoding('Big5')
523 issue = Issue.generate!(:subject => str_utf8)
523 issue = Issue.generate!(:subject => str_utf8)
524
524
525 get :index, :project_id => 1,
525 get :index, :project_id => 1,
526 :f => ['subject'],
526 :f => ['subject'],
527 :op => '=', :values => [str_utf8],
527 :op => '=', :values => [str_utf8],
528 :format => 'csv'
528 :format => 'csv'
529 assert_equal 'text/csv; header=present', @response.content_type
529 assert_equal 'text/csv; header=present', @response.content_type
530 lines = @response.body.chomp.split("\n")
530 lines = @response.body.chomp.split("\n")
531 header = lines[0]
531 header = lines[0]
532 issue_line = lines.find {|l| l =~ /^#{issue.id},/}
532 issue_line = lines.find {|l| l =~ /^#{issue.id},/}
533 s1 = "\xaa\xac\xbaA".force_encoding('Big5')
533 s1 = "\xaa\xac\xbaA".force_encoding('Big5')
534 assert_include s1, header
534 assert_include s1, header
535 assert_include str_big5, issue_line
535 assert_include str_big5, issue_line
536 end
536 end
537 end
537 end
538
538
539 def test_index_csv_cannot_convert_should_be_replaced_big_5
539 def test_index_csv_cannot_convert_should_be_replaced_big_5
540 with_settings :default_language => "zh-TW" do
540 with_settings :default_language => "zh-TW" do
541 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85".force_encoding('UTF-8')
541 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85".force_encoding('UTF-8')
542 issue = Issue.generate!(:subject => str_utf8)
542 issue = Issue.generate!(:subject => str_utf8)
543
543
544 get :index, :project_id => 1,
544 get :index, :project_id => 1,
545 :f => ['subject'],
545 :f => ['subject'],
546 :op => '=', :values => [str_utf8],
546 :op => '=', :values => [str_utf8],
547 :c => ['status', 'subject'],
547 :c => ['status', 'subject'],
548 :format => 'csv',
548 :format => 'csv',
549 :set_filter => 1
549 :set_filter => 1
550 assert_equal 'text/csv; header=present', @response.content_type
550 assert_equal 'text/csv; header=present', @response.content_type
551 lines = @response.body.chomp.split("\n")
551 lines = @response.body.chomp.split("\n")
552 header = lines[0]
552 header = lines[0]
553 issue_line = lines.find {|l| l =~ /^#{issue.id},/}
553 issue_line = lines.find {|l| l =~ /^#{issue.id},/}
554 s1 = "\xaa\xac\xbaA".force_encoding('Big5') # status
554 s1 = "\xaa\xac\xbaA".force_encoding('Big5') # status
555 assert header.include?(s1)
555 assert header.include?(s1)
556 s2 = issue_line.split(",")[2]
556 s2 = issue_line.split(",")[2]
557 s3 = "\xa5H?".force_encoding('Big5') # subject
557 s3 = "\xa5H?".force_encoding('Big5') # subject
558 assert_equal s3, s2
558 assert_equal s3, s2
559 end
559 end
560 end
560 end
561
561
562 def test_index_csv_tw
562 def test_index_csv_tw
563 with_settings :default_language => "zh-TW" do
563 with_settings :default_language => "zh-TW" do
564 str1 = "test_index_csv_tw"
564 str1 = "test_index_csv_tw"
565 issue = Issue.generate!(:subject => str1, :estimated_hours => '1234.5')
565 issue = Issue.generate!(:subject => str1, :estimated_hours => '1234.5')
566
566
567 get :index, :project_id => 1,
567 get :index, :project_id => 1,
568 :f => ['subject'],
568 :f => ['subject'],
569 :op => '=', :values => [str1],
569 :op => '=', :values => [str1],
570 :c => ['estimated_hours', 'subject'],
570 :c => ['estimated_hours', 'subject'],
571 :format => 'csv',
571 :format => 'csv',
572 :set_filter => 1
572 :set_filter => 1
573 assert_equal 'text/csv; header=present', @response.content_type
573 assert_equal 'text/csv; header=present', @response.content_type
574 lines = @response.body.chomp.split("\n")
574 lines = @response.body.chomp.split("\n")
575 assert_include "#{issue.id},1234.50,#{str1}", lines
575 assert_include "#{issue.id},1234.50,#{str1}", lines
576 end
576 end
577 end
577 end
578
578
579 def test_index_csv_fr
579 def test_index_csv_fr
580 with_settings :default_language => "fr" do
580 with_settings :default_language => "fr" do
581 str1 = "test_index_csv_fr"
581 str1 = "test_index_csv_fr"
582 issue = Issue.generate!(:subject => str1, :estimated_hours => '1234.5')
582 issue = Issue.generate!(:subject => str1, :estimated_hours => '1234.5')
583
583
584 get :index, :project_id => 1,
584 get :index, :project_id => 1,
585 :f => ['subject'],
585 :f => ['subject'],
586 :op => '=', :values => [str1],
586 :op => '=', :values => [str1],
587 :c => ['estimated_hours', 'subject'],
587 :c => ['estimated_hours', 'subject'],
588 :format => 'csv',
588 :format => 'csv',
589 :set_filter => 1
589 :set_filter => 1
590 assert_equal 'text/csv; header=present', @response.content_type
590 assert_equal 'text/csv; header=present', @response.content_type
591 lines = @response.body.chomp.split("\n")
591 lines = @response.body.chomp.split("\n")
592 assert_include "#{issue.id};1234,50;#{str1}", lines
592 assert_include "#{issue.id};1234,50;#{str1}", lines
593 end
593 end
594 end
594 end
595
595
596 def test_index_pdf
596 def test_index_pdf
597 ["en", "zh", "zh-TW", "ja", "ko"].each do |lang|
597 ["en", "zh", "zh-TW", "ja", "ko"].each do |lang|
598 with_settings :default_language => lang do
598 with_settings :default_language => lang do
599
599
600 get :index
600 get :index
601 assert_response :success
601 assert_response :success
602 assert_template 'index'
602 assert_template 'index'
603
603
604 get :index, :format => 'pdf'
604 get :index, :format => 'pdf'
605 assert_response :success
605 assert_response :success
606 assert_not_nil assigns(:issues)
606 assert_not_nil assigns(:issues)
607 assert_equal 'application/pdf', @response.content_type
607 assert_equal 'application/pdf', @response.content_type
608
608
609 get :index, :project_id => 1, :format => 'pdf'
609 get :index, :project_id => 1, :format => 'pdf'
610 assert_response :success
610 assert_response :success
611 assert_not_nil assigns(:issues)
611 assert_not_nil assigns(:issues)
612 assert_equal 'application/pdf', @response.content_type
612 assert_equal 'application/pdf', @response.content_type
613
613
614 get :index, :project_id => 1, :query_id => 6, :format => 'pdf'
614 get :index, :project_id => 1, :query_id => 6, :format => 'pdf'
615 assert_response :success
615 assert_response :success
616 assert_not_nil assigns(:issues)
616 assert_not_nil assigns(:issues)
617 assert_equal 'application/pdf', @response.content_type
617 assert_equal 'application/pdf', @response.content_type
618 end
618 end
619 end
619 end
620 end
620 end
621
621
622 def test_index_pdf_with_query_grouped_by_list_custom_field
622 def test_index_pdf_with_query_grouped_by_list_custom_field
623 get :index, :project_id => 1, :query_id => 9, :format => 'pdf'
623 get :index, :project_id => 1, :query_id => 9, :format => 'pdf'
624 assert_response :success
624 assert_response :success
625 assert_not_nil assigns(:issues)
625 assert_not_nil assigns(:issues)
626 assert_not_nil assigns(:issue_count_by_group)
626 assert_not_nil assigns(:issue_count_by_group)
627 assert_equal 'application/pdf', @response.content_type
627 assert_equal 'application/pdf', @response.content_type
628 end
628 end
629
629
630 def test_index_atom
630 def test_index_atom
631 get :index, :project_id => 'ecookbook', :format => 'atom'
631 get :index, :project_id => 'ecookbook', :format => 'atom'
632 assert_response :success
632 assert_response :success
633 assert_template 'common/feed'
633 assert_template 'common/feed'
634 assert_equal 'application/atom+xml', response.content_type
634 assert_equal 'application/atom+xml', response.content_type
635
635
636 assert_select 'feed' do
636 assert_select 'feed' do
637 assert_select 'link[rel=self][href=?]', 'http://test.host/projects/ecookbook/issues.atom'
637 assert_select 'link[rel=self][href=?]', 'http://test.host/projects/ecookbook/issues.atom'
638 assert_select 'link[rel=alternate][href=?]', 'http://test.host/projects/ecookbook/issues'
638 assert_select 'link[rel=alternate][href=?]', 'http://test.host/projects/ecookbook/issues'
639 assert_select 'entry link[href=?]', 'http://test.host/issues/1'
639 assert_select 'entry link[href=?]', 'http://test.host/issues/1'
640 end
640 end
641 end
641 end
642
642
643 def test_index_sort
643 def test_index_sort
644 get :index, :sort => 'tracker,id:desc'
644 get :index, :sort => 'tracker,id:desc'
645 assert_response :success
645 assert_response :success
646
646
647 sort_params = @request.session['issues_index_sort']
647 sort_params = @request.session['issues_index_sort']
648 assert sort_params.is_a?(String)
648 assert sort_params.is_a?(String)
649 assert_equal 'tracker,id:desc', sort_params
649 assert_equal 'tracker,id:desc', sort_params
650
650
651 issues = assigns(:issues)
651 issues = assigns(:issues)
652 assert_not_nil issues
652 assert_not_nil issues
653 assert !issues.empty?
653 assert !issues.empty?
654 assert_equal issues.sort {|a,b| a.tracker == b.tracker ? b.id <=> a.id : a.tracker <=> b.tracker }.collect(&:id), issues.collect(&:id)
654 assert_equal issues.sort {|a,b| a.tracker == b.tracker ? b.id <=> a.id : a.tracker <=> b.tracker }.collect(&:id), issues.collect(&:id)
655 assert_select 'table.issues.sort-by-tracker.sort-asc'
655 assert_select 'table.issues.sort-by-tracker.sort-asc'
656 end
656 end
657
657
658 def test_index_sort_by_field_not_included_in_columns
658 def test_index_sort_by_field_not_included_in_columns
659 Setting.issue_list_default_columns = %w(subject author)
659 Setting.issue_list_default_columns = %w(subject author)
660 get :index, :sort => 'tracker'
660 get :index, :sort => 'tracker'
661 end
661 end
662
662
663 def test_index_sort_by_assigned_to
663 def test_index_sort_by_assigned_to
664 get :index, :sort => 'assigned_to'
664 get :index, :sort => 'assigned_to'
665 assert_response :success
665 assert_response :success
666 assignees = assigns(:issues).collect(&:assigned_to).compact
666 assignees = assigns(:issues).collect(&:assigned_to).compact
667 assert_equal assignees.sort, assignees
667 assert_equal assignees.sort, assignees
668 assert_select 'table.issues.sort-by-assigned-to.sort-asc'
668 assert_select 'table.issues.sort-by-assigned-to.sort-asc'
669 end
669 end
670
670
671 def test_index_sort_by_assigned_to_desc
671 def test_index_sort_by_assigned_to_desc
672 get :index, :sort => 'assigned_to:desc'
672 get :index, :sort => 'assigned_to:desc'
673 assert_response :success
673 assert_response :success
674 assignees = assigns(:issues).collect(&:assigned_to).compact
674 assignees = assigns(:issues).collect(&:assigned_to).compact
675 assert_equal assignees.sort.reverse, assignees
675 assert_equal assignees.sort.reverse, assignees
676 assert_select 'table.issues.sort-by-assigned-to.sort-desc'
676 assert_select 'table.issues.sort-by-assigned-to.sort-desc'
677 end
677 end
678
678
679 def test_index_group_by_assigned_to
679 def test_index_group_by_assigned_to
680 get :index, :group_by => 'assigned_to', :sort => 'priority'
680 get :index, :group_by => 'assigned_to', :sort => 'priority'
681 assert_response :success
681 assert_response :success
682 end
682 end
683
683
684 def test_index_sort_by_author
684 def test_index_sort_by_author
685 get :index, :sort => 'author'
685 get :index, :sort => 'author'
686 assert_response :success
686 assert_response :success
687 authors = assigns(:issues).collect(&:author)
687 authors = assigns(:issues).collect(&:author)
688 assert_equal authors.sort, authors
688 assert_equal authors.sort, authors
689 end
689 end
690
690
691 def test_index_sort_by_author_desc
691 def test_index_sort_by_author_desc
692 get :index, :sort => 'author:desc'
692 get :index, :sort => 'author:desc'
693 assert_response :success
693 assert_response :success
694 authors = assigns(:issues).collect(&:author)
694 authors = assigns(:issues).collect(&:author)
695 assert_equal authors.sort.reverse, authors
695 assert_equal authors.sort.reverse, authors
696 end
696 end
697
697
698 def test_index_group_by_author
698 def test_index_group_by_author
699 get :index, :group_by => 'author', :sort => 'priority'
699 get :index, :group_by => 'author', :sort => 'priority'
700 assert_response :success
700 assert_response :success
701 end
701 end
702
702
703 def test_index_sort_by_spent_hours
703 def test_index_sort_by_spent_hours
704 get :index, :sort => 'spent_hours:desc'
704 get :index, :sort => 'spent_hours:desc'
705 assert_response :success
705 assert_response :success
706 hours = assigns(:issues).collect(&:spent_hours)
706 hours = assigns(:issues).collect(&:spent_hours)
707 assert_equal hours.sort.reverse, hours
707 assert_equal hours.sort.reverse, hours
708 end
708 end
709
709
710 def test_index_sort_by_user_custom_field
710 def test_index_sort_by_user_custom_field
711 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
711 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
712 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
712 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
713 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
713 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
714 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
714 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
715 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
715 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
716
716
717 get :index, :project_id => 1, :set_filter => 1, :sort => "cf_#{cf.id},id"
717 get :index, :project_id => 1, :set_filter => 1, :sort => "cf_#{cf.id},id"
718 assert_response :success
718 assert_response :success
719
719
720 assert_equal [2, 3, 1], assigns(:issues).select {|issue| issue.custom_field_value(cf).present?}.map(&:id)
720 assert_equal [2, 3, 1], assigns(:issues).select {|issue| issue.custom_field_value(cf).present?}.map(&:id)
721 end
721 end
722
722
723 def test_index_with_columns
723 def test_index_with_columns
724 columns = ['tracker', 'subject', 'assigned_to']
724 columns = ['tracker', 'subject', 'assigned_to']
725 get :index, :set_filter => 1, :c => columns
725 get :index, :set_filter => 1, :c => columns
726 assert_response :success
726 assert_response :success
727
727
728 # query should use specified columns
728 # query should use specified columns
729 query = assigns(:query)
729 query = assigns(:query)
730 assert_kind_of IssueQuery, query
730 assert_kind_of IssueQuery, query
731 assert_equal columns, query.column_names.map(&:to_s)
731 assert_equal columns, query.column_names.map(&:to_s)
732
732
733 # columns should be stored in session
733 # columns should be stored in session
734 assert_kind_of Hash, session[:query]
734 assert_kind_of Hash, session[:query]
735 assert_kind_of Array, session[:query][:column_names]
735 assert_kind_of Array, session[:query][:column_names]
736 assert_equal columns, session[:query][:column_names].map(&:to_s)
736 assert_equal columns, session[:query][:column_names].map(&:to_s)
737
737
738 # ensure only these columns are kept in the selected columns list
738 # ensure only these columns are kept in the selected columns list
739 assert_select 'select#selected_columns option' do
739 assert_select 'select#selected_columns option' do
740 assert_select 'option', 3
740 assert_select 'option', 3
741 assert_select 'option[value=tracker]'
741 assert_select 'option[value=tracker]'
742 assert_select 'option[value=project]', 0
742 assert_select 'option[value=project]', 0
743 end
743 end
744 end
744 end
745
745
746 def test_index_without_project_should_implicitly_add_project_column_to_default_columns
746 def test_index_without_project_should_implicitly_add_project_column_to_default_columns
747 Setting.issue_list_default_columns = ['tracker', 'subject', 'assigned_to']
747 Setting.issue_list_default_columns = ['tracker', 'subject', 'assigned_to']
748 get :index, :set_filter => 1
748 get :index, :set_filter => 1
749
749
750 # query should use specified columns
750 # query should use specified columns
751 query = assigns(:query)
751 query = assigns(:query)
752 assert_kind_of IssueQuery, query
752 assert_kind_of IssueQuery, query
753 assert_equal [:id, :project, :tracker, :subject, :assigned_to], query.columns.map(&:name)
753 assert_equal [:id, :project, :tracker, :subject, :assigned_to], query.columns.map(&:name)
754 end
754 end
755
755
756 def test_index_without_project_and_explicit_default_columns_should_not_add_project_column
756 def test_index_without_project_and_explicit_default_columns_should_not_add_project_column
757 Setting.issue_list_default_columns = ['tracker', 'subject', 'assigned_to']
757 Setting.issue_list_default_columns = ['tracker', 'subject', 'assigned_to']
758 columns = ['id', 'tracker', 'subject', 'assigned_to']
758 columns = ['id', 'tracker', 'subject', 'assigned_to']
759 get :index, :set_filter => 1, :c => columns
759 get :index, :set_filter => 1, :c => columns
760
760
761 # query should use specified columns
761 # query should use specified columns
762 query = assigns(:query)
762 query = assigns(:query)
763 assert_kind_of IssueQuery, query
763 assert_kind_of IssueQuery, query
764 assert_equal columns.map(&:to_sym), query.columns.map(&:name)
764 assert_equal columns.map(&:to_sym), query.columns.map(&:name)
765 end
765 end
766
766
767 def test_index_with_default_columns_should_respect_default_columns_order
767 def test_index_with_default_columns_should_respect_default_columns_order
768 columns = ['assigned_to', 'subject', 'status', 'tracker']
768 columns = ['assigned_to', 'subject', 'status', 'tracker']
769 with_settings :issue_list_default_columns => columns do
769 with_settings :issue_list_default_columns => columns do
770 get :index, :project_id => 1, :set_filter => 1
770 get :index, :project_id => 1, :set_filter => 1
771
771
772 query = assigns(:query)
772 query = assigns(:query)
773 assert_equal (['id'] + columns).map(&:to_sym), query.columns.map(&:name)
773 assert_equal (['id'] + columns).map(&:to_sym), query.columns.map(&:name)
774 end
774 end
775 end
775 end
776
776
777 def test_index_with_custom_field_column
777 def test_index_with_custom_field_column
778 columns = %w(tracker subject cf_2)
778 columns = %w(tracker subject cf_2)
779 get :index, :set_filter => 1, :c => columns
779 get :index, :set_filter => 1, :c => columns
780 assert_response :success
780 assert_response :success
781
781
782 # query should use specified columns
782 # query should use specified columns
783 query = assigns(:query)
783 query = assigns(:query)
784 assert_kind_of IssueQuery, query
784 assert_kind_of IssueQuery, query
785 assert_equal columns, query.column_names.map(&:to_s)
785 assert_equal columns, query.column_names.map(&:to_s)
786
786
787 assert_select 'table.issues td.cf_2.string'
787 assert_select 'table.issues td.cf_2.string'
788 end
788 end
789
789
790 def test_index_with_multi_custom_field_column
790 def test_index_with_multi_custom_field_column
791 field = CustomField.find(1)
791 field = CustomField.find(1)
792 field.update_attribute :multiple, true
792 field.update_attribute :multiple, true
793 issue = Issue.find(1)
793 issue = Issue.find(1)
794 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
794 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
795 issue.save!
795 issue.save!
796
796
797 get :index, :set_filter => 1, :c => %w(tracker subject cf_1)
797 get :index, :set_filter => 1, :c => %w(tracker subject cf_1)
798 assert_response :success
798 assert_response :success
799
799
800 assert_select 'table.issues td.cf_1', :text => 'MySQL, Oracle'
800 assert_select 'table.issues td.cf_1', :text => 'MySQL, Oracle'
801 end
801 end
802
802
803 def test_index_with_multi_user_custom_field_column
803 def test_index_with_multi_user_custom_field_column
804 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
804 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
805 :tracker_ids => [1], :is_for_all => true)
805 :tracker_ids => [1], :is_for_all => true)
806 issue = Issue.find(1)
806 issue = Issue.find(1)
807 issue.custom_field_values = {field.id => ['2', '3']}
807 issue.custom_field_values = {field.id => ['2', '3']}
808 issue.save!
808 issue.save!
809
809
810 get :index, :set_filter => 1, :c => ['tracker', 'subject', "cf_#{field.id}"]
810 get :index, :set_filter => 1, :c => ['tracker', 'subject', "cf_#{field.id}"]
811 assert_response :success
811 assert_response :success
812
812
813 assert_select "table.issues td.cf_#{field.id}" do
813 assert_select "table.issues td.cf_#{field.id}" do
814 assert_select 'a', 2
814 assert_select 'a', 2
815 assert_select 'a[href=?]', '/users/2', :text => 'John Smith'
815 assert_select 'a[href=?]', '/users/2', :text => 'John Smith'
816 assert_select 'a[href=?]', '/users/3', :text => 'Dave Lopper'
816 assert_select 'a[href=?]', '/users/3', :text => 'Dave Lopper'
817 end
817 end
818 end
818 end
819
819
820 def test_index_with_date_column
820 def test_index_with_date_column
821 with_settings :date_format => '%d/%m/%Y' do
821 with_settings :date_format => '%d/%m/%Y' do
822 Issue.find(1).update_attribute :start_date, '1987-08-24'
822 Issue.find(1).update_attribute :start_date, '1987-08-24'
823 get :index, :set_filter => 1, :c => %w(start_date)
823 get :index, :set_filter => 1, :c => %w(start_date)
824 assert_select "table.issues td.start_date", :text => '24/08/1987'
824 assert_select "table.issues td.start_date", :text => '24/08/1987'
825 end
825 end
826 end
826 end
827
827
828 def test_index_with_done_ratio_column
828 def test_index_with_done_ratio_column
829 Issue.find(1).update_attribute :done_ratio, 40
829 Issue.find(1).update_attribute :done_ratio, 40
830 get :index, :set_filter => 1, :c => %w(done_ratio)
830 get :index, :set_filter => 1, :c => %w(done_ratio)
831 assert_select 'table.issues td.done_ratio' do
831 assert_select 'table.issues td.done_ratio' do
832 assert_select 'table.progress' do
832 assert_select 'table.progress' do
833 assert_select 'td.closed[style=?]', 'width: 40%;'
833 assert_select 'td.closed[style=?]', 'width: 40%;'
834 end
834 end
835 end
835 end
836 end
836 end
837
837
838 def test_index_with_spent_hours_column
838 def test_index_with_spent_hours_column
839 get :index, :set_filter => 1, :c => %w(subject spent_hours)
839 get :index, :set_filter => 1, :c => %w(subject spent_hours)
840 assert_select 'table.issues tr#issue-3 td.spent_hours', :text => '1.00'
840 assert_select 'table.issues tr#issue-3 td.spent_hours', :text => '1.00'
841 end
841 end
842
842
843 def test_index_should_not_show_spent_hours_column_without_permission
843 def test_index_should_not_show_spent_hours_column_without_permission
844 Role.anonymous.remove_permission! :view_time_entries
844 Role.anonymous.remove_permission! :view_time_entries
845 get :index, :set_filter => 1, :c => %w(subject spent_hours)
845 get :index, :set_filter => 1, :c => %w(subject spent_hours)
846 assert_select 'td.spent_hours', 0
846 assert_select 'td.spent_hours', 0
847 end
847 end
848
848
849 def test_index_with_fixed_version_column
849 def test_index_with_fixed_version_column
850 get :index, :set_filter => 1, :c => %w(fixed_version)
850 get :index, :set_filter => 1, :c => %w(fixed_version)
851 assert_select 'table.issues td.fixed_version' do
851 assert_select 'table.issues td.fixed_version' do
852 assert_select 'a[href=?]', '/versions/2', :text => '1.0'
852 assert_select 'a[href=?]', '/versions/2', :text => 'eCookbook - 1.0'
853 end
853 end
854 end
854 end
855
855
856 def test_index_with_relations_column
856 def test_index_with_relations_column
857 IssueRelation.delete_all
857 IssueRelation.delete_all
858 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(7))
858 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(7))
859 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(8), :issue_to => Issue.find(1))
859 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(8), :issue_to => Issue.find(1))
860 IssueRelation.create!(:relation_type => "blocks", :issue_from => Issue.find(1), :issue_to => Issue.find(11))
860 IssueRelation.create!(:relation_type => "blocks", :issue_from => Issue.find(1), :issue_to => Issue.find(11))
861 IssueRelation.create!(:relation_type => "blocks", :issue_from => Issue.find(12), :issue_to => Issue.find(2))
861 IssueRelation.create!(:relation_type => "blocks", :issue_from => Issue.find(12), :issue_to => Issue.find(2))
862
862
863 get :index, :set_filter => 1, :c => %w(subject relations)
863 get :index, :set_filter => 1, :c => %w(subject relations)
864 assert_response :success
864 assert_response :success
865 assert_select "tr#issue-1 td.relations" do
865 assert_select "tr#issue-1 td.relations" do
866 assert_select "span", 3
866 assert_select "span", 3
867 assert_select "span", :text => "Related to #7"
867 assert_select "span", :text => "Related to #7"
868 assert_select "span", :text => "Related to #8"
868 assert_select "span", :text => "Related to #8"
869 assert_select "span", :text => "Blocks #11"
869 assert_select "span", :text => "Blocks #11"
870 end
870 end
871 assert_select "tr#issue-2 td.relations" do
871 assert_select "tr#issue-2 td.relations" do
872 assert_select "span", 1
872 assert_select "span", 1
873 assert_select "span", :text => "Blocked by #12"
873 assert_select "span", :text => "Blocked by #12"
874 end
874 end
875 assert_select "tr#issue-3 td.relations" do
875 assert_select "tr#issue-3 td.relations" do
876 assert_select "span", 0
876 assert_select "span", 0
877 end
877 end
878
878
879 get :index, :set_filter => 1, :c => %w(relations), :format => 'csv'
879 get :index, :set_filter => 1, :c => %w(relations), :format => 'csv'
880 assert_response :success
880 assert_response :success
881 assert_equal 'text/csv; header=present', response.content_type
881 assert_equal 'text/csv; header=present', response.content_type
882 lines = response.body.chomp.split("\n")
882 lines = response.body.chomp.split("\n")
883 assert_include '1,"Related to #7, Related to #8, Blocks #11"', lines
883 assert_include '1,"Related to #7, Related to #8, Blocks #11"', lines
884 assert_include '2,Blocked by #12', lines
884 assert_include '2,Blocked by #12', lines
885 assert_include '3,""', lines
885 assert_include '3,""', lines
886
886
887 get :index, :set_filter => 1, :c => %w(subject relations), :format => 'pdf'
887 get :index, :set_filter => 1, :c => %w(subject relations), :format => 'pdf'
888 assert_response :success
888 assert_response :success
889 assert_equal 'application/pdf', response.content_type
889 assert_equal 'application/pdf', response.content_type
890 end
890 end
891
891
892 def test_index_with_description_column
892 def test_index_with_description_column
893 get :index, :set_filter => 1, :c => %w(subject description)
893 get :index, :set_filter => 1, :c => %w(subject description)
894
894
895 assert_select 'table.issues thead th', 3 # columns: chekbox + id + subject
895 assert_select 'table.issues thead th', 3 # columns: chekbox + id + subject
896 assert_select 'td.description[colspan="3"]', :text => 'Unable to print recipes'
896 assert_select 'td.description[colspan="3"]', :text => 'Unable to print recipes'
897
897
898 get :index, :set_filter => 1, :c => %w(subject description), :format => 'pdf'
898 get :index, :set_filter => 1, :c => %w(subject description), :format => 'pdf'
899 assert_response :success
899 assert_response :success
900 assert_equal 'application/pdf', response.content_type
900 assert_equal 'application/pdf', response.content_type
901 end
901 end
902
902
903 def test_index_with_parent_column
903 def test_index_with_parent_column
904 Issue.delete_all
904 Issue.delete_all
905 parent = Issue.generate!
905 parent = Issue.generate!
906 child = Issue.generate!(:parent_issue_id => parent.id)
906 child = Issue.generate!(:parent_issue_id => parent.id)
907
907
908 get :index, :c => %w(parent)
908 get :index, :c => %w(parent)
909
909
910 assert_select 'td.parent', :text => "#{parent.tracker} ##{parent.id}"
910 assert_select 'td.parent', :text => "#{parent.tracker} ##{parent.id}"
911 assert_select 'td.parent a[title=?]', parent.subject
911 assert_select 'td.parent a[title=?]', parent.subject
912 end
912 end
913
913
914 def test_index_send_html_if_query_is_invalid
914 def test_index_send_html_if_query_is_invalid
915 get :index, :f => ['start_date'], :op => {:start_date => '='}
915 get :index, :f => ['start_date'], :op => {:start_date => '='}
916 assert_equal 'text/html', @response.content_type
916 assert_equal 'text/html', @response.content_type
917 assert_template 'index'
917 assert_template 'index'
918 end
918 end
919
919
920 def test_index_send_nothing_if_query_is_invalid
920 def test_index_send_nothing_if_query_is_invalid
921 get :index, :f => ['start_date'], :op => {:start_date => '='}, :format => 'csv'
921 get :index, :f => ['start_date'], :op => {:start_date => '='}, :format => 'csv'
922 assert_equal 'text/csv', @response.content_type
922 assert_equal 'text/csv', @response.content_type
923 assert @response.body.blank?
923 assert @response.body.blank?
924 end
924 end
925
925
926 def test_show_by_anonymous
926 def test_show_by_anonymous
927 get :show, :id => 1
927 get :show, :id => 1
928 assert_response :success
928 assert_response :success
929 assert_template 'show'
929 assert_template 'show'
930 assert_equal Issue.find(1), assigns(:issue)
930 assert_equal Issue.find(1), assigns(:issue)
931 assert_select 'div.issue div.description', :text => /Unable to print recipes/
931 assert_select 'div.issue div.description', :text => /Unable to print recipes/
932 # anonymous role is allowed to add a note
932 # anonymous role is allowed to add a note
933 assert_select 'form#issue-form' do
933 assert_select 'form#issue-form' do
934 assert_select 'fieldset' do
934 assert_select 'fieldset' do
935 assert_select 'legend', :text => 'Notes'
935 assert_select 'legend', :text => 'Notes'
936 assert_select 'textarea[name=?]', 'issue[notes]'
936 assert_select 'textarea[name=?]', 'issue[notes]'
937 end
937 end
938 end
938 end
939 assert_select 'title', :text => "Bug #1: Cannot print recipes - eCookbook - Redmine"
939 assert_select 'title', :text => "Bug #1: Cannot print recipes - eCookbook - Redmine"
940 end
940 end
941
941
942 def test_show_by_manager
942 def test_show_by_manager
943 @request.session[:user_id] = 2
943 @request.session[:user_id] = 2
944 get :show, :id => 1
944 get :show, :id => 1
945 assert_response :success
945 assert_response :success
946 assert_select 'a', :text => /Quote/
946 assert_select 'a', :text => /Quote/
947 assert_select 'form#issue-form' do
947 assert_select 'form#issue-form' do
948 assert_select 'fieldset' do
948 assert_select 'fieldset' do
949 assert_select 'legend', :text => 'Change properties'
949 assert_select 'legend', :text => 'Change properties'
950 assert_select 'input[name=?]', 'issue[subject]'
950 assert_select 'input[name=?]', 'issue[subject]'
951 end
951 end
952 assert_select 'fieldset' do
952 assert_select 'fieldset' do
953 assert_select 'legend', :text => 'Log time'
953 assert_select 'legend', :text => 'Log time'
954 assert_select 'input[name=?]', 'time_entry[hours]'
954 assert_select 'input[name=?]', 'time_entry[hours]'
955 end
955 end
956 assert_select 'fieldset' do
956 assert_select 'fieldset' do
957 assert_select 'legend', :text => 'Notes'
957 assert_select 'legend', :text => 'Notes'
958 assert_select 'textarea[name=?]', 'issue[notes]'
958 assert_select 'textarea[name=?]', 'issue[notes]'
959 end
959 end
960 end
960 end
961 end
961 end
962
962
963 def test_show_should_display_update_form
963 def test_show_should_display_update_form
964 @request.session[:user_id] = 2
964 @request.session[:user_id] = 2
965 get :show, :id => 1
965 get :show, :id => 1
966 assert_response :success
966 assert_response :success
967
967
968 assert_select 'form#issue-form' do
968 assert_select 'form#issue-form' do
969 assert_select 'input[name=?]', 'issue[is_private]'
969 assert_select 'input[name=?]', 'issue[is_private]'
970 assert_select 'select[name=?]', 'issue[project_id]'
970 assert_select 'select[name=?]', 'issue[project_id]'
971 assert_select 'select[name=?]', 'issue[tracker_id]'
971 assert_select 'select[name=?]', 'issue[tracker_id]'
972 assert_select 'input[name=?]', 'issue[subject]'
972 assert_select 'input[name=?]', 'issue[subject]'
973 assert_select 'textarea[name=?]', 'issue[description]'
973 assert_select 'textarea[name=?]', 'issue[description]'
974 assert_select 'select[name=?]', 'issue[status_id]'
974 assert_select 'select[name=?]', 'issue[status_id]'
975 assert_select 'select[name=?]', 'issue[priority_id]'
975 assert_select 'select[name=?]', 'issue[priority_id]'
976 assert_select 'select[name=?]', 'issue[assigned_to_id]'
976 assert_select 'select[name=?]', 'issue[assigned_to_id]'
977 assert_select 'select[name=?]', 'issue[category_id]'
977 assert_select 'select[name=?]', 'issue[category_id]'
978 assert_select 'select[name=?]', 'issue[fixed_version_id]'
978 assert_select 'select[name=?]', 'issue[fixed_version_id]'
979 assert_select 'input[name=?]', 'issue[parent_issue_id]'
979 assert_select 'input[name=?]', 'issue[parent_issue_id]'
980 assert_select 'input[name=?]', 'issue[start_date]'
980 assert_select 'input[name=?]', 'issue[start_date]'
981 assert_select 'input[name=?]', 'issue[due_date]'
981 assert_select 'input[name=?]', 'issue[due_date]'
982 assert_select 'select[name=?]', 'issue[done_ratio]'
982 assert_select 'select[name=?]', 'issue[done_ratio]'
983 assert_select 'input[name=?]', 'issue[custom_field_values][2]'
983 assert_select 'input[name=?]', 'issue[custom_field_values][2]'
984 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
984 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
985 assert_select 'textarea[name=?]', 'issue[notes]'
985 assert_select 'textarea[name=?]', 'issue[notes]'
986 end
986 end
987 end
987 end
988
988
989 def test_show_should_display_update_form_with_minimal_permissions
989 def test_show_should_display_update_form_with_minimal_permissions
990 Role.find(1).update_attribute :permissions, [:view_issues, :add_issue_notes]
990 Role.find(1).update_attribute :permissions, [:view_issues, :add_issue_notes]
991 WorkflowTransition.delete_all :role_id => 1
991 WorkflowTransition.delete_all :role_id => 1
992
992
993 @request.session[:user_id] = 2
993 @request.session[:user_id] = 2
994 get :show, :id => 1
994 get :show, :id => 1
995 assert_response :success
995 assert_response :success
996
996
997 assert_select 'form#issue-form' do
997 assert_select 'form#issue-form' do
998 assert_select 'input[name=?]', 'issue[is_private]', 0
998 assert_select 'input[name=?]', 'issue[is_private]', 0
999 assert_select 'select[name=?]', 'issue[project_id]', 0
999 assert_select 'select[name=?]', 'issue[project_id]', 0
1000 assert_select 'select[name=?]', 'issue[tracker_id]', 0
1000 assert_select 'select[name=?]', 'issue[tracker_id]', 0
1001 assert_select 'input[name=?]', 'issue[subject]', 0
1001 assert_select 'input[name=?]', 'issue[subject]', 0
1002 assert_select 'textarea[name=?]', 'issue[description]', 0
1002 assert_select 'textarea[name=?]', 'issue[description]', 0
1003 assert_select 'select[name=?]', 'issue[status_id]', 0
1003 assert_select 'select[name=?]', 'issue[status_id]', 0
1004 assert_select 'select[name=?]', 'issue[priority_id]', 0
1004 assert_select 'select[name=?]', 'issue[priority_id]', 0
1005 assert_select 'select[name=?]', 'issue[assigned_to_id]', 0
1005 assert_select 'select[name=?]', 'issue[assigned_to_id]', 0
1006 assert_select 'select[name=?]', 'issue[category_id]', 0
1006 assert_select 'select[name=?]', 'issue[category_id]', 0
1007 assert_select 'select[name=?]', 'issue[fixed_version_id]', 0
1007 assert_select 'select[name=?]', 'issue[fixed_version_id]', 0
1008 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
1008 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
1009 assert_select 'input[name=?]', 'issue[start_date]', 0
1009 assert_select 'input[name=?]', 'issue[start_date]', 0
1010 assert_select 'input[name=?]', 'issue[due_date]', 0
1010 assert_select 'input[name=?]', 'issue[due_date]', 0
1011 assert_select 'select[name=?]', 'issue[done_ratio]', 0
1011 assert_select 'select[name=?]', 'issue[done_ratio]', 0
1012 assert_select 'input[name=?]', 'issue[custom_field_values][2]', 0
1012 assert_select 'input[name=?]', 'issue[custom_field_values][2]', 0
1013 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
1013 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
1014 assert_select 'textarea[name=?]', 'issue[notes]'
1014 assert_select 'textarea[name=?]', 'issue[notes]'
1015 end
1015 end
1016 end
1016 end
1017
1017
1018 def test_show_should_not_display_update_form_without_permissions
1018 def test_show_should_not_display_update_form_without_permissions
1019 Role.find(1).update_attribute :permissions, [:view_issues]
1019 Role.find(1).update_attribute :permissions, [:view_issues]
1020
1020
1021 @request.session[:user_id] = 2
1021 @request.session[:user_id] = 2
1022 get :show, :id => 1
1022 get :show, :id => 1
1023 assert_response :success
1023 assert_response :success
1024
1024
1025 assert_select 'form#issue-form', 0
1025 assert_select 'form#issue-form', 0
1026 end
1026 end
1027
1027
1028 def test_update_form_should_not_display_inactive_enumerations
1028 def test_update_form_should_not_display_inactive_enumerations
1029 assert !IssuePriority.find(15).active?
1029 assert !IssuePriority.find(15).active?
1030
1030
1031 @request.session[:user_id] = 2
1031 @request.session[:user_id] = 2
1032 get :show, :id => 1
1032 get :show, :id => 1
1033 assert_response :success
1033 assert_response :success
1034
1034
1035 assert_select 'form#issue-form' do
1035 assert_select 'form#issue-form' do
1036 assert_select 'select[name=?]', 'issue[priority_id]' do
1036 assert_select 'select[name=?]', 'issue[priority_id]' do
1037 assert_select 'option[value="4"]'
1037 assert_select 'option[value="4"]'
1038 assert_select 'option[value="15"]', 0
1038 assert_select 'option[value="15"]', 0
1039 end
1039 end
1040 end
1040 end
1041 end
1041 end
1042
1042
1043 def test_update_form_should_allow_attachment_upload
1043 def test_update_form_should_allow_attachment_upload
1044 @request.session[:user_id] = 2
1044 @request.session[:user_id] = 2
1045 get :show, :id => 1
1045 get :show, :id => 1
1046
1046
1047 assert_select 'form#issue-form[method=post][enctype="multipart/form-data"]' do
1047 assert_select 'form#issue-form[method=post][enctype="multipart/form-data"]' do
1048 assert_select 'input[type=file][name=?]', 'attachments[dummy][file]'
1048 assert_select 'input[type=file][name=?]', 'attachments[dummy][file]'
1049 end
1049 end
1050 end
1050 end
1051
1051
1052 def test_show_should_deny_anonymous_access_without_permission
1052 def test_show_should_deny_anonymous_access_without_permission
1053 Role.anonymous.remove_permission!(:view_issues)
1053 Role.anonymous.remove_permission!(:view_issues)
1054 get :show, :id => 1
1054 get :show, :id => 1
1055 assert_response :redirect
1055 assert_response :redirect
1056 end
1056 end
1057
1057
1058 def test_show_should_deny_anonymous_access_to_private_issue
1058 def test_show_should_deny_anonymous_access_to_private_issue
1059 Issue.where(:id => 1).update_all(["is_private = ?", true])
1059 Issue.where(:id => 1).update_all(["is_private = ?", true])
1060 get :show, :id => 1
1060 get :show, :id => 1
1061 assert_response :redirect
1061 assert_response :redirect
1062 end
1062 end
1063
1063
1064 def test_show_should_deny_non_member_access_without_permission
1064 def test_show_should_deny_non_member_access_without_permission
1065 Role.non_member.remove_permission!(:view_issues)
1065 Role.non_member.remove_permission!(:view_issues)
1066 @request.session[:user_id] = 9
1066 @request.session[:user_id] = 9
1067 get :show, :id => 1
1067 get :show, :id => 1
1068 assert_response 403
1068 assert_response 403
1069 end
1069 end
1070
1070
1071 def test_show_should_deny_non_member_access_to_private_issue
1071 def test_show_should_deny_non_member_access_to_private_issue
1072 Issue.where(:id => 1).update_all(["is_private = ?", true])
1072 Issue.where(:id => 1).update_all(["is_private = ?", true])
1073 @request.session[:user_id] = 9
1073 @request.session[:user_id] = 9
1074 get :show, :id => 1
1074 get :show, :id => 1
1075 assert_response 403
1075 assert_response 403
1076 end
1076 end
1077
1077
1078 def test_show_should_deny_member_access_without_permission
1078 def test_show_should_deny_member_access_without_permission
1079 Role.find(1).remove_permission!(:view_issues)
1079 Role.find(1).remove_permission!(:view_issues)
1080 @request.session[:user_id] = 2
1080 @request.session[:user_id] = 2
1081 get :show, :id => 1
1081 get :show, :id => 1
1082 assert_response 403
1082 assert_response 403
1083 end
1083 end
1084
1084
1085 def test_show_should_deny_member_access_to_private_issue_without_permission
1085 def test_show_should_deny_member_access_to_private_issue_without_permission
1086 Issue.where(:id => 1).update_all(["is_private = ?", true])
1086 Issue.where(:id => 1).update_all(["is_private = ?", true])
1087 @request.session[:user_id] = 3
1087 @request.session[:user_id] = 3
1088 get :show, :id => 1
1088 get :show, :id => 1
1089 assert_response 403
1089 assert_response 403
1090 end
1090 end
1091
1091
1092 def test_show_should_allow_author_access_to_private_issue
1092 def test_show_should_allow_author_access_to_private_issue
1093 Issue.where(:id => 1).update_all(["is_private = ?, author_id = 3", true])
1093 Issue.where(:id => 1).update_all(["is_private = ?, author_id = 3", true])
1094 @request.session[:user_id] = 3
1094 @request.session[:user_id] = 3
1095 get :show, :id => 1
1095 get :show, :id => 1
1096 assert_response :success
1096 assert_response :success
1097 end
1097 end
1098
1098
1099 def test_show_should_allow_assignee_access_to_private_issue
1099 def test_show_should_allow_assignee_access_to_private_issue
1100 Issue.where(:id => 1).update_all(["is_private = ?, assigned_to_id = 3", true])
1100 Issue.where(:id => 1).update_all(["is_private = ?, assigned_to_id = 3", true])
1101 @request.session[:user_id] = 3
1101 @request.session[:user_id] = 3
1102 get :show, :id => 1
1102 get :show, :id => 1
1103 assert_response :success
1103 assert_response :success
1104 end
1104 end
1105
1105
1106 def test_show_should_allow_member_access_to_private_issue_with_permission
1106 def test_show_should_allow_member_access_to_private_issue_with_permission
1107 Issue.where(:id => 1).update_all(["is_private = ?", true])
1107 Issue.where(:id => 1).update_all(["is_private = ?", true])
1108 User.find(3).roles_for_project(Project.find(1)).first.update_attribute :issues_visibility, 'all'
1108 User.find(3).roles_for_project(Project.find(1)).first.update_attribute :issues_visibility, 'all'
1109 @request.session[:user_id] = 3
1109 @request.session[:user_id] = 3
1110 get :show, :id => 1
1110 get :show, :id => 1
1111 assert_response :success
1111 assert_response :success
1112 end
1112 end
1113
1113
1114 def test_show_should_not_disclose_relations_to_invisible_issues
1114 def test_show_should_not_disclose_relations_to_invisible_issues
1115 Setting.cross_project_issue_relations = '1'
1115 Setting.cross_project_issue_relations = '1'
1116 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => 'relates')
1116 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => 'relates')
1117 # Relation to a private project issue
1117 # Relation to a private project issue
1118 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(4), :relation_type => 'relates')
1118 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(4), :relation_type => 'relates')
1119
1119
1120 get :show, :id => 1
1120 get :show, :id => 1
1121 assert_response :success
1121 assert_response :success
1122
1122
1123 assert_select 'div#relations' do
1123 assert_select 'div#relations' do
1124 assert_select 'a', :text => /#2$/
1124 assert_select 'a', :text => /#2$/
1125 assert_select 'a', :text => /#4$/, :count => 0
1125 assert_select 'a', :text => /#4$/, :count => 0
1126 end
1126 end
1127 end
1127 end
1128
1128
1129 def test_show_should_list_subtasks
1129 def test_show_should_list_subtasks
1130 Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')
1130 Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')
1131
1131
1132 get :show, :id => 1
1132 get :show, :id => 1
1133 assert_response :success
1133 assert_response :success
1134
1134
1135 assert_select 'div#issue_tree' do
1135 assert_select 'div#issue_tree' do
1136 assert_select 'td.subject', :text => /Child Issue/
1136 assert_select 'td.subject', :text => /Child Issue/
1137 end
1137 end
1138 end
1138 end
1139
1139
1140 def test_show_should_list_parents
1140 def test_show_should_list_parents
1141 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')
1141 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')
1142
1142
1143 get :show, :id => issue.id
1143 get :show, :id => issue.id
1144 assert_response :success
1144 assert_response :success
1145
1145
1146 assert_select 'div.subject' do
1146 assert_select 'div.subject' do
1147 assert_select 'h3', 'Child Issue'
1147 assert_select 'h3', 'Child Issue'
1148 assert_select 'a[href="/issues/1"]'
1148 assert_select 'a[href="/issues/1"]'
1149 end
1149 end
1150 end
1150 end
1151
1151
1152 def test_show_should_not_display_prev_next_links_without_query_in_session
1152 def test_show_should_not_display_prev_next_links_without_query_in_session
1153 get :show, :id => 1
1153 get :show, :id => 1
1154 assert_response :success
1154 assert_response :success
1155 assert_nil assigns(:prev_issue_id)
1155 assert_nil assigns(:prev_issue_id)
1156 assert_nil assigns(:next_issue_id)
1156 assert_nil assigns(:next_issue_id)
1157
1157
1158 assert_select 'div.next-prev-links', 0
1158 assert_select 'div.next-prev-links', 0
1159 end
1159 end
1160
1160
1161 def test_show_should_display_prev_next_links_with_query_in_session
1161 def test_show_should_display_prev_next_links_with_query_in_session
1162 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => nil}
1162 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => nil}
1163 @request.session['issues_index_sort'] = 'id'
1163 @request.session['issues_index_sort'] = 'id'
1164
1164
1165 with_settings :display_subprojects_issues => '0' do
1165 with_settings :display_subprojects_issues => '0' do
1166 get :show, :id => 3
1166 get :show, :id => 3
1167 end
1167 end
1168
1168
1169 assert_response :success
1169 assert_response :success
1170 # Previous and next issues for all projects
1170 # Previous and next issues for all projects
1171 assert_equal 2, assigns(:prev_issue_id)
1171 assert_equal 2, assigns(:prev_issue_id)
1172 assert_equal 5, assigns(:next_issue_id)
1172 assert_equal 5, assigns(:next_issue_id)
1173
1173
1174 count = Issue.open.visible.count
1174 count = Issue.open.visible.count
1175
1175
1176 assert_select 'div.next-prev-links' do
1176 assert_select 'div.next-prev-links' do
1177 assert_select 'a[href="/issues/2"]', :text => /Previous/
1177 assert_select 'a[href="/issues/2"]', :text => /Previous/
1178 assert_select 'a[href="/issues/5"]', :text => /Next/
1178 assert_select 'a[href="/issues/5"]', :text => /Next/
1179 assert_select 'span.position', :text => "3 of #{count}"
1179 assert_select 'span.position', :text => "3 of #{count}"
1180 end
1180 end
1181 end
1181 end
1182
1182
1183 def test_show_should_display_prev_next_links_with_saved_query_in_session
1183 def test_show_should_display_prev_next_links_with_saved_query_in_session
1184 query = IssueQuery.create!(:name => 'test', :visibility => IssueQuery::VISIBILITY_PUBLIC, :user_id => 1,
1184 query = IssueQuery.create!(:name => 'test', :visibility => IssueQuery::VISIBILITY_PUBLIC, :user_id => 1,
1185 :filters => {'status_id' => {:values => ['5'], :operator => '='}},
1185 :filters => {'status_id' => {:values => ['5'], :operator => '='}},
1186 :sort_criteria => [['id', 'asc']])
1186 :sort_criteria => [['id', 'asc']])
1187 @request.session[:query] = {:id => query.id, :project_id => nil}
1187 @request.session[:query] = {:id => query.id, :project_id => nil}
1188
1188
1189 get :show, :id => 11
1189 get :show, :id => 11
1190
1190
1191 assert_response :success
1191 assert_response :success
1192 assert_equal query, assigns(:query)
1192 assert_equal query, assigns(:query)
1193 # Previous and next issues for all projects
1193 # Previous and next issues for all projects
1194 assert_equal 8, assigns(:prev_issue_id)
1194 assert_equal 8, assigns(:prev_issue_id)
1195 assert_equal 12, assigns(:next_issue_id)
1195 assert_equal 12, assigns(:next_issue_id)
1196
1196
1197 assert_select 'div.next-prev-links' do
1197 assert_select 'div.next-prev-links' do
1198 assert_select 'a[href="/issues/8"]', :text => /Previous/
1198 assert_select 'a[href="/issues/8"]', :text => /Previous/
1199 assert_select 'a[href="/issues/12"]', :text => /Next/
1199 assert_select 'a[href="/issues/12"]', :text => /Next/
1200 end
1200 end
1201 end
1201 end
1202
1202
1203 def test_show_should_display_prev_next_links_with_query_and_sort_on_association
1203 def test_show_should_display_prev_next_links_with_query_and_sort_on_association
1204 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => nil}
1204 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => nil}
1205
1205
1206 %w(project tracker status priority author assigned_to category fixed_version).each do |assoc_sort|
1206 %w(project tracker status priority author assigned_to category fixed_version).each do |assoc_sort|
1207 @request.session['issues_index_sort'] = assoc_sort
1207 @request.session['issues_index_sort'] = assoc_sort
1208
1208
1209 get :show, :id => 3
1209 get :show, :id => 3
1210 assert_response :success, "Wrong response status for #{assoc_sort} sort"
1210 assert_response :success, "Wrong response status for #{assoc_sort} sort"
1211
1211
1212 assert_select 'div.next-prev-links' do
1212 assert_select 'div.next-prev-links' do
1213 assert_select 'a', :text => /(Previous|Next)/
1213 assert_select 'a', :text => /(Previous|Next)/
1214 end
1214 end
1215 end
1215 end
1216 end
1216 end
1217
1217
1218 def test_show_should_display_prev_next_links_with_project_query_in_session
1218 def test_show_should_display_prev_next_links_with_project_query_in_session
1219 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => 1}
1219 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => 1}
1220 @request.session['issues_index_sort'] = 'id'
1220 @request.session['issues_index_sort'] = 'id'
1221
1221
1222 with_settings :display_subprojects_issues => '0' do
1222 with_settings :display_subprojects_issues => '0' do
1223 get :show, :id => 3
1223 get :show, :id => 3
1224 end
1224 end
1225
1225
1226 assert_response :success
1226 assert_response :success
1227 # Previous and next issues inside project
1227 # Previous and next issues inside project
1228 assert_equal 2, assigns(:prev_issue_id)
1228 assert_equal 2, assigns(:prev_issue_id)
1229 assert_equal 7, assigns(:next_issue_id)
1229 assert_equal 7, assigns(:next_issue_id)
1230
1230
1231 assert_select 'div.next-prev-links' do
1231 assert_select 'div.next-prev-links' do
1232 assert_select 'a[href="/issues/2"]', :text => /Previous/
1232 assert_select 'a[href="/issues/2"]', :text => /Previous/
1233 assert_select 'a[href="/issues/7"]', :text => /Next/
1233 assert_select 'a[href="/issues/7"]', :text => /Next/
1234 end
1234 end
1235 end
1235 end
1236
1236
1237 def test_show_should_not_display_prev_link_for_first_issue
1237 def test_show_should_not_display_prev_link_for_first_issue
1238 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => 1}
1238 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => 1}
1239 @request.session['issues_index_sort'] = 'id'
1239 @request.session['issues_index_sort'] = 'id'
1240
1240
1241 with_settings :display_subprojects_issues => '0' do
1241 with_settings :display_subprojects_issues => '0' do
1242 get :show, :id => 1
1242 get :show, :id => 1
1243 end
1243 end
1244
1244
1245 assert_response :success
1245 assert_response :success
1246 assert_nil assigns(:prev_issue_id)
1246 assert_nil assigns(:prev_issue_id)
1247 assert_equal 2, assigns(:next_issue_id)
1247 assert_equal 2, assigns(:next_issue_id)
1248
1248
1249 assert_select 'div.next-prev-links' do
1249 assert_select 'div.next-prev-links' do
1250 assert_select 'a', :text => /Previous/, :count => 0
1250 assert_select 'a', :text => /Previous/, :count => 0
1251 assert_select 'a[href="/issues/2"]', :text => /Next/
1251 assert_select 'a[href="/issues/2"]', :text => /Next/
1252 end
1252 end
1253 end
1253 end
1254
1254
1255 def test_show_should_not_display_prev_next_links_for_issue_not_in_query_results
1255 def test_show_should_not_display_prev_next_links_for_issue_not_in_query_results
1256 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'c'}}, :project_id => 1}
1256 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'c'}}, :project_id => 1}
1257 @request.session['issues_index_sort'] = 'id'
1257 @request.session['issues_index_sort'] = 'id'
1258
1258
1259 get :show, :id => 1
1259 get :show, :id => 1
1260
1260
1261 assert_response :success
1261 assert_response :success
1262 assert_nil assigns(:prev_issue_id)
1262 assert_nil assigns(:prev_issue_id)
1263 assert_nil assigns(:next_issue_id)
1263 assert_nil assigns(:next_issue_id)
1264
1264
1265 assert_select 'a', :text => /Previous/, :count => 0
1265 assert_select 'a', :text => /Previous/, :count => 0
1266 assert_select 'a', :text => /Next/, :count => 0
1266 assert_select 'a', :text => /Next/, :count => 0
1267 end
1267 end
1268
1268
1269 def test_show_show_should_display_prev_next_links_with_query_sort_by_user_custom_field
1269 def test_show_show_should_display_prev_next_links_with_query_sort_by_user_custom_field
1270 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
1270 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
1271 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
1271 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
1272 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
1272 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
1273 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
1273 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
1274 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
1274 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
1275
1275
1276 query = IssueQuery.create!(:name => 'test', :visibility => IssueQuery::VISIBILITY_PUBLIC, :user_id => 1, :filters => {},
1276 query = IssueQuery.create!(:name => 'test', :visibility => IssueQuery::VISIBILITY_PUBLIC, :user_id => 1, :filters => {},
1277 :sort_criteria => [["cf_#{cf.id}", 'asc'], ['id', 'asc']])
1277 :sort_criteria => [["cf_#{cf.id}", 'asc'], ['id', 'asc']])
1278 @request.session[:query] = {:id => query.id, :project_id => nil}
1278 @request.session[:query] = {:id => query.id, :project_id => nil}
1279
1279
1280 get :show, :id => 3
1280 get :show, :id => 3
1281 assert_response :success
1281 assert_response :success
1282
1282
1283 assert_equal 2, assigns(:prev_issue_id)
1283 assert_equal 2, assigns(:prev_issue_id)
1284 assert_equal 1, assigns(:next_issue_id)
1284 assert_equal 1, assigns(:next_issue_id)
1285
1285
1286 assert_select 'div.next-prev-links' do
1286 assert_select 'div.next-prev-links' do
1287 assert_select 'a[href="/issues/2"]', :text => /Previous/
1287 assert_select 'a[href="/issues/2"]', :text => /Previous/
1288 assert_select 'a[href="/issues/1"]', :text => /Next/
1288 assert_select 'a[href="/issues/1"]', :text => /Next/
1289 end
1289 end
1290 end
1290 end
1291
1291
1292 def test_show_should_display_link_to_the_assignee
1292 def test_show_should_display_link_to_the_assignee
1293 get :show, :id => 2
1293 get :show, :id => 2
1294 assert_response :success
1294 assert_response :success
1295 assert_select '.assigned-to' do
1295 assert_select '.assigned-to' do
1296 assert_select 'a[href="/users/3"]'
1296 assert_select 'a[href="/users/3"]'
1297 end
1297 end
1298 end
1298 end
1299
1299
1300 def test_show_should_display_visible_changesets_from_other_projects
1300 def test_show_should_display_visible_changesets_from_other_projects
1301 project = Project.find(2)
1301 project = Project.find(2)
1302 issue = project.issues.first
1302 issue = project.issues.first
1303 issue.changeset_ids = [102]
1303 issue.changeset_ids = [102]
1304 issue.save!
1304 issue.save!
1305 # changesets from other projects should be displayed even if repository
1305 # changesets from other projects should be displayed even if repository
1306 # is disabled on issue's project
1306 # is disabled on issue's project
1307 project.disable_module! :repository
1307 project.disable_module! :repository
1308
1308
1309 @request.session[:user_id] = 2
1309 @request.session[:user_id] = 2
1310 get :show, :id => issue.id
1310 get :show, :id => issue.id
1311
1311
1312 assert_select 'a[href=?]', '/projects/ecookbook/repository/revisions/3'
1312 assert_select 'a[href=?]', '/projects/ecookbook/repository/revisions/3'
1313 end
1313 end
1314
1314
1315 def test_show_should_display_watchers
1315 def test_show_should_display_watchers
1316 @request.session[:user_id] = 2
1316 @request.session[:user_id] = 2
1317 Issue.find(1).add_watcher User.find(2)
1317 Issue.find(1).add_watcher User.find(2)
1318
1318
1319 get :show, :id => 1
1319 get :show, :id => 1
1320 assert_select 'div#watchers ul' do
1320 assert_select 'div#watchers ul' do
1321 assert_select 'li' do
1321 assert_select 'li' do
1322 assert_select 'a[href="/users/2"]'
1322 assert_select 'a[href="/users/2"]'
1323 assert_select 'a img[alt=Delete]'
1323 assert_select 'a img[alt=Delete]'
1324 end
1324 end
1325 end
1325 end
1326 end
1326 end
1327
1327
1328 def test_show_should_display_watchers_with_gravatars
1328 def test_show_should_display_watchers_with_gravatars
1329 @request.session[:user_id] = 2
1329 @request.session[:user_id] = 2
1330 Issue.find(1).add_watcher User.find(2)
1330 Issue.find(1).add_watcher User.find(2)
1331
1331
1332 with_settings :gravatar_enabled => '1' do
1332 with_settings :gravatar_enabled => '1' do
1333 get :show, :id => 1
1333 get :show, :id => 1
1334 end
1334 end
1335
1335
1336 assert_select 'div#watchers ul' do
1336 assert_select 'div#watchers ul' do
1337 assert_select 'li' do
1337 assert_select 'li' do
1338 assert_select 'img.gravatar'
1338 assert_select 'img.gravatar'
1339 assert_select 'a[href="/users/2"]'
1339 assert_select 'a[href="/users/2"]'
1340 assert_select 'a img[alt=Delete]'
1340 assert_select 'a img[alt=Delete]'
1341 end
1341 end
1342 end
1342 end
1343 end
1343 end
1344
1344
1345 def test_show_with_thumbnails_enabled_should_display_thumbnails
1345 def test_show_with_thumbnails_enabled_should_display_thumbnails
1346 @request.session[:user_id] = 2
1346 @request.session[:user_id] = 2
1347
1347
1348 with_settings :thumbnails_enabled => '1' do
1348 with_settings :thumbnails_enabled => '1' do
1349 get :show, :id => 14
1349 get :show, :id => 14
1350 assert_response :success
1350 assert_response :success
1351 end
1351 end
1352
1352
1353 assert_select 'div.thumbnails' do
1353 assert_select 'div.thumbnails' do
1354 assert_select 'a[href="/attachments/16/testfile.png"]' do
1354 assert_select 'a[href="/attachments/16/testfile.png"]' do
1355 assert_select 'img[src="/attachments/thumbnail/16"]'
1355 assert_select 'img[src="/attachments/thumbnail/16"]'
1356 end
1356 end
1357 end
1357 end
1358 end
1358 end
1359
1359
1360 def test_show_with_thumbnails_disabled_should_not_display_thumbnails
1360 def test_show_with_thumbnails_disabled_should_not_display_thumbnails
1361 @request.session[:user_id] = 2
1361 @request.session[:user_id] = 2
1362
1362
1363 with_settings :thumbnails_enabled => '0' do
1363 with_settings :thumbnails_enabled => '0' do
1364 get :show, :id => 14
1364 get :show, :id => 14
1365 assert_response :success
1365 assert_response :success
1366 end
1366 end
1367
1367
1368 assert_select 'div.thumbnails', 0
1368 assert_select 'div.thumbnails', 0
1369 end
1369 end
1370
1370
1371 def test_show_with_multi_custom_field
1371 def test_show_with_multi_custom_field
1372 field = CustomField.find(1)
1372 field = CustomField.find(1)
1373 field.update_attribute :multiple, true
1373 field.update_attribute :multiple, true
1374 issue = Issue.find(1)
1374 issue = Issue.find(1)
1375 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
1375 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
1376 issue.save!
1376 issue.save!
1377
1377
1378 get :show, :id => 1
1378 get :show, :id => 1
1379 assert_response :success
1379 assert_response :success
1380
1380
1381 assert_select 'td', :text => 'MySQL, Oracle'
1381 assert_select 'td', :text => 'MySQL, Oracle'
1382 end
1382 end
1383
1383
1384 def test_show_with_multi_user_custom_field
1384 def test_show_with_multi_user_custom_field
1385 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1385 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1386 :tracker_ids => [1], :is_for_all => true)
1386 :tracker_ids => [1], :is_for_all => true)
1387 issue = Issue.find(1)
1387 issue = Issue.find(1)
1388 issue.custom_field_values = {field.id => ['2', '3']}
1388 issue.custom_field_values = {field.id => ['2', '3']}
1389 issue.save!
1389 issue.save!
1390
1390
1391 get :show, :id => 1
1391 get :show, :id => 1
1392 assert_response :success
1392 assert_response :success
1393
1393
1394 assert_select "td.cf_#{field.id}", :text => 'Dave Lopper, John Smith' do
1394 assert_select "td.cf_#{field.id}", :text => 'Dave Lopper, John Smith' do
1395 assert_select 'a', :text => 'Dave Lopper'
1395 assert_select 'a', :text => 'Dave Lopper'
1396 assert_select 'a', :text => 'John Smith'
1396 assert_select 'a', :text => 'John Smith'
1397 end
1397 end
1398 end
1398 end
1399
1399
1400 def test_show_should_display_private_notes_with_permission_only
1400 def test_show_should_display_private_notes_with_permission_only
1401 journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Privates notes', :private_notes => true, :user_id => 1)
1401 journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Privates notes', :private_notes => true, :user_id => 1)
1402 @request.session[:user_id] = 2
1402 @request.session[:user_id] = 2
1403
1403
1404 get :show, :id => 2
1404 get :show, :id => 2
1405 assert_response :success
1405 assert_response :success
1406 assert_include journal, assigns(:journals)
1406 assert_include journal, assigns(:journals)
1407
1407
1408 Role.find(1).remove_permission! :view_private_notes
1408 Role.find(1).remove_permission! :view_private_notes
1409 get :show, :id => 2
1409 get :show, :id => 2
1410 assert_response :success
1410 assert_response :success
1411 assert_not_include journal, assigns(:journals)
1411 assert_not_include journal, assigns(:journals)
1412 end
1412 end
1413
1413
1414 def test_show_atom
1414 def test_show_atom
1415 get :show, :id => 2, :format => 'atom'
1415 get :show, :id => 2, :format => 'atom'
1416 assert_response :success
1416 assert_response :success
1417 assert_template 'journals/index'
1417 assert_template 'journals/index'
1418 # Inline image
1418 # Inline image
1419 assert_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
1419 assert_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
1420 end
1420 end
1421
1421
1422 def test_show_export_to_pdf
1422 def test_show_export_to_pdf
1423 issue = Issue.find(3)
1423 issue = Issue.find(3)
1424 assert issue.relations.select{|r| r.other_issue(issue).visible?}.present?
1424 assert issue.relations.select{|r| r.other_issue(issue).visible?}.present?
1425 get :show, :id => 3, :format => 'pdf'
1425 get :show, :id => 3, :format => 'pdf'
1426 assert_response :success
1426 assert_response :success
1427 assert_equal 'application/pdf', @response.content_type
1427 assert_equal 'application/pdf', @response.content_type
1428 assert @response.body.starts_with?('%PDF')
1428 assert @response.body.starts_with?('%PDF')
1429 assert_not_nil assigns(:issue)
1429 assert_not_nil assigns(:issue)
1430 end
1430 end
1431
1431
1432 def test_export_to_pdf_with_utf8_u_fffd
1432 def test_export_to_pdf_with_utf8_u_fffd
1433 # U+FFFD
1433 # U+FFFD
1434 s = "\xef\xbf\xbd"
1434 s = "\xef\xbf\xbd"
1435 s.force_encoding('UTF-8') if s.respond_to?(:force_encoding)
1435 s.force_encoding('UTF-8') if s.respond_to?(:force_encoding)
1436 issue = Issue.generate!(:subject => s)
1436 issue = Issue.generate!(:subject => s)
1437 ["en", "zh", "zh-TW", "ja", "ko"].each do |lang|
1437 ["en", "zh", "zh-TW", "ja", "ko"].each do |lang|
1438 with_settings :default_language => lang do
1438 with_settings :default_language => lang do
1439 get :show, :id => issue.id, :format => 'pdf'
1439 get :show, :id => issue.id, :format => 'pdf'
1440 assert_response :success
1440 assert_response :success
1441 assert_equal 'application/pdf', @response.content_type
1441 assert_equal 'application/pdf', @response.content_type
1442 assert @response.body.starts_with?('%PDF')
1442 assert @response.body.starts_with?('%PDF')
1443 assert_not_nil assigns(:issue)
1443 assert_not_nil assigns(:issue)
1444 end
1444 end
1445 end
1445 end
1446 end
1446 end
1447
1447
1448 def test_show_export_to_pdf_with_ancestors
1448 def test_show_export_to_pdf_with_ancestors
1449 issue = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1449 issue = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1450
1450
1451 get :show, :id => issue.id, :format => 'pdf'
1451 get :show, :id => issue.id, :format => 'pdf'
1452 assert_response :success
1452 assert_response :success
1453 assert_equal 'application/pdf', @response.content_type
1453 assert_equal 'application/pdf', @response.content_type
1454 assert @response.body.starts_with?('%PDF')
1454 assert @response.body.starts_with?('%PDF')
1455 end
1455 end
1456
1456
1457 def test_show_export_to_pdf_with_descendants
1457 def test_show_export_to_pdf_with_descendants
1458 c1 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1458 c1 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1459 c2 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1459 c2 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1460 c3 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => c1.id)
1460 c3 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => c1.id)
1461
1461
1462 get :show, :id => 1, :format => 'pdf'
1462 get :show, :id => 1, :format => 'pdf'
1463 assert_response :success
1463 assert_response :success
1464 assert_equal 'application/pdf', @response.content_type
1464 assert_equal 'application/pdf', @response.content_type
1465 assert @response.body.starts_with?('%PDF')
1465 assert @response.body.starts_with?('%PDF')
1466 end
1466 end
1467
1467
1468 def test_show_export_to_pdf_with_journals
1468 def test_show_export_to_pdf_with_journals
1469 get :show, :id => 1, :format => 'pdf'
1469 get :show, :id => 1, :format => 'pdf'
1470 assert_response :success
1470 assert_response :success
1471 assert_equal 'application/pdf', @response.content_type
1471 assert_equal 'application/pdf', @response.content_type
1472 assert @response.body.starts_with?('%PDF')
1472 assert @response.body.starts_with?('%PDF')
1473 end
1473 end
1474
1474
1475 def test_show_export_to_pdf_with_changesets
1475 def test_show_export_to_pdf_with_changesets
1476 [[100], [100, 101], [100, 101, 102]].each do |cs|
1476 [[100], [100, 101], [100, 101, 102]].each do |cs|
1477 issue1 = Issue.find(3)
1477 issue1 = Issue.find(3)
1478 issue1.changesets = Changeset.find(cs)
1478 issue1.changesets = Changeset.find(cs)
1479 issue1.save!
1479 issue1.save!
1480 issue = Issue.find(3)
1480 issue = Issue.find(3)
1481 assert_equal issue.changesets.count, cs.size
1481 assert_equal issue.changesets.count, cs.size
1482 get :show, :id => 3, :format => 'pdf'
1482 get :show, :id => 3, :format => 'pdf'
1483 assert_response :success
1483 assert_response :success
1484 assert_equal 'application/pdf', @response.content_type
1484 assert_equal 'application/pdf', @response.content_type
1485 assert @response.body.starts_with?('%PDF')
1485 assert @response.body.starts_with?('%PDF')
1486 end
1486 end
1487 end
1487 end
1488
1488
1489 def test_show_invalid_should_respond_with_404
1489 def test_show_invalid_should_respond_with_404
1490 get :show, :id => 999
1490 get :show, :id => 999
1491 assert_response 404
1491 assert_response 404
1492 end
1492 end
1493
1493
1494 def test_get_new
1494 def test_get_new
1495 @request.session[:user_id] = 2
1495 @request.session[:user_id] = 2
1496 get :new, :project_id => 1, :tracker_id => 1
1496 get :new, :project_id => 1, :tracker_id => 1
1497 assert_response :success
1497 assert_response :success
1498 assert_template 'new'
1498 assert_template 'new'
1499
1499
1500 assert_select 'form#issue-form[action=?]', '/projects/ecookbook/issues'
1500 assert_select 'form#issue-form[action=?]', '/projects/ecookbook/issues'
1501 assert_select 'form#issue-form' do
1501 assert_select 'form#issue-form' do
1502 assert_select 'input[name=?]', 'issue[is_private]'
1502 assert_select 'input[name=?]', 'issue[is_private]'
1503 assert_select 'select[name=?]', 'issue[project_id]', 0
1503 assert_select 'select[name=?]', 'issue[project_id]', 0
1504 assert_select 'select[name=?]', 'issue[tracker_id]'
1504 assert_select 'select[name=?]', 'issue[tracker_id]'
1505 assert_select 'input[name=?]', 'issue[subject]'
1505 assert_select 'input[name=?]', 'issue[subject]'
1506 assert_select 'textarea[name=?]', 'issue[description]'
1506 assert_select 'textarea[name=?]', 'issue[description]'
1507 assert_select 'select[name=?]', 'issue[status_id]'
1507 assert_select 'select[name=?]', 'issue[status_id]'
1508 assert_select 'select[name=?]', 'issue[priority_id]'
1508 assert_select 'select[name=?]', 'issue[priority_id]'
1509 assert_select 'select[name=?]', 'issue[assigned_to_id]'
1509 assert_select 'select[name=?]', 'issue[assigned_to_id]'
1510 assert_select 'select[name=?]', 'issue[category_id]'
1510 assert_select 'select[name=?]', 'issue[category_id]'
1511 assert_select 'select[name=?]', 'issue[fixed_version_id]'
1511 assert_select 'select[name=?]', 'issue[fixed_version_id]'
1512 assert_select 'input[name=?]', 'issue[parent_issue_id]'
1512 assert_select 'input[name=?]', 'issue[parent_issue_id]'
1513 assert_select 'input[name=?]', 'issue[start_date]'
1513 assert_select 'input[name=?]', 'issue[start_date]'
1514 assert_select 'input[name=?]', 'issue[due_date]'
1514 assert_select 'input[name=?]', 'issue[due_date]'
1515 assert_select 'select[name=?]', 'issue[done_ratio]'
1515 assert_select 'select[name=?]', 'issue[done_ratio]'
1516 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Default string'
1516 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Default string'
1517 assert_select 'input[name=?]', 'issue[watcher_user_ids][]'
1517 assert_select 'input[name=?]', 'issue[watcher_user_ids][]'
1518 end
1518 end
1519
1519
1520 # Be sure we don't display inactive IssuePriorities
1520 # Be sure we don't display inactive IssuePriorities
1521 assert ! IssuePriority.find(15).active?
1521 assert ! IssuePriority.find(15).active?
1522 assert_select 'select[name=?]', 'issue[priority_id]' do
1522 assert_select 'select[name=?]', 'issue[priority_id]' do
1523 assert_select 'option[value="15"]', 0
1523 assert_select 'option[value="15"]', 0
1524 end
1524 end
1525 end
1525 end
1526
1526
1527 def test_get_new_with_minimal_permissions
1527 def test_get_new_with_minimal_permissions
1528 Role.find(1).update_attribute :permissions, [:add_issues]
1528 Role.find(1).update_attribute :permissions, [:add_issues]
1529 WorkflowTransition.delete_all :role_id => 1
1529 WorkflowTransition.delete_all :role_id => 1
1530
1530
1531 @request.session[:user_id] = 2
1531 @request.session[:user_id] = 2
1532 get :new, :project_id => 1, :tracker_id => 1
1532 get :new, :project_id => 1, :tracker_id => 1
1533 assert_response :success
1533 assert_response :success
1534 assert_template 'new'
1534 assert_template 'new'
1535
1535
1536 assert_select 'form#issue-form' do
1536 assert_select 'form#issue-form' do
1537 assert_select 'input[name=?]', 'issue[is_private]', 0
1537 assert_select 'input[name=?]', 'issue[is_private]', 0
1538 assert_select 'select[name=?]', 'issue[project_id]', 0
1538 assert_select 'select[name=?]', 'issue[project_id]', 0
1539 assert_select 'select[name=?]', 'issue[tracker_id]'
1539 assert_select 'select[name=?]', 'issue[tracker_id]'
1540 assert_select 'input[name=?]', 'issue[subject]'
1540 assert_select 'input[name=?]', 'issue[subject]'
1541 assert_select 'textarea[name=?]', 'issue[description]'
1541 assert_select 'textarea[name=?]', 'issue[description]'
1542 assert_select 'select[name=?]', 'issue[status_id]'
1542 assert_select 'select[name=?]', 'issue[status_id]'
1543 assert_select 'select[name=?]', 'issue[priority_id]'
1543 assert_select 'select[name=?]', 'issue[priority_id]'
1544 assert_select 'select[name=?]', 'issue[assigned_to_id]'
1544 assert_select 'select[name=?]', 'issue[assigned_to_id]'
1545 assert_select 'select[name=?]', 'issue[category_id]'
1545 assert_select 'select[name=?]', 'issue[category_id]'
1546 assert_select 'select[name=?]', 'issue[fixed_version_id]'
1546 assert_select 'select[name=?]', 'issue[fixed_version_id]'
1547 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
1547 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
1548 assert_select 'input[name=?]', 'issue[start_date]'
1548 assert_select 'input[name=?]', 'issue[start_date]'
1549 assert_select 'input[name=?]', 'issue[due_date]'
1549 assert_select 'input[name=?]', 'issue[due_date]'
1550 assert_select 'select[name=?]', 'issue[done_ratio]'
1550 assert_select 'select[name=?]', 'issue[done_ratio]'
1551 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Default string'
1551 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Default string'
1552 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
1552 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
1553 end
1553 end
1554 end
1554 end
1555
1555
1556 def test_new_without_project_id
1556 def test_new_without_project_id
1557 @request.session[:user_id] = 2
1557 @request.session[:user_id] = 2
1558 get :new
1558 get :new
1559 assert_response :success
1559 assert_response :success
1560 assert_template 'new'
1560 assert_template 'new'
1561
1561
1562 assert_select 'form#issue-form[action=?]', '/issues'
1562 assert_select 'form#issue-form[action=?]', '/issues'
1563 assert_select 'form#issue-form' do
1563 assert_select 'form#issue-form' do
1564 assert_select 'select[name=?]', 'issue[project_id]'
1564 assert_select 'select[name=?]', 'issue[project_id]'
1565 end
1565 end
1566
1566
1567 assert_nil assigns(:project)
1567 assert_nil assigns(:project)
1568 assert_not_nil assigns(:issue)
1568 assert_not_nil assigns(:issue)
1569 end
1569 end
1570
1570
1571 def test_new_should_select_default_status
1571 def test_new_should_select_default_status
1572 @request.session[:user_id] = 2
1572 @request.session[:user_id] = 2
1573
1573
1574 get :new, :project_id => 1
1574 get :new, :project_id => 1
1575 assert_response :success
1575 assert_response :success
1576 assert_template 'new'
1576 assert_template 'new'
1577 assert_select 'select[name=?]', 'issue[status_id]' do
1577 assert_select 'select[name=?]', 'issue[status_id]' do
1578 assert_select 'option[value="1"][selected=selected]'
1578 assert_select 'option[value="1"][selected=selected]'
1579 end
1579 end
1580 assert_select 'input[name=was_default_status][value="1"]'
1580 assert_select 'input[name=was_default_status][value="1"]'
1581 end
1581 end
1582
1582
1583 def test_get_new_with_list_custom_field
1583 def test_get_new_with_list_custom_field
1584 @request.session[:user_id] = 2
1584 @request.session[:user_id] = 2
1585 get :new, :project_id => 1, :tracker_id => 1
1585 get :new, :project_id => 1, :tracker_id => 1
1586 assert_response :success
1586 assert_response :success
1587 assert_template 'new'
1587 assert_template 'new'
1588
1588
1589 assert_select 'select.list_cf[name=?]', 'issue[custom_field_values][1]' do
1589 assert_select 'select.list_cf[name=?]', 'issue[custom_field_values][1]' do
1590 assert_select 'option', 4
1590 assert_select 'option', 4
1591 assert_select 'option[value=MySQL]', :text => 'MySQL'
1591 assert_select 'option[value=MySQL]', :text => 'MySQL'
1592 end
1592 end
1593 end
1593 end
1594
1594
1595 def test_get_new_with_multi_custom_field
1595 def test_get_new_with_multi_custom_field
1596 field = IssueCustomField.find(1)
1596 field = IssueCustomField.find(1)
1597 field.update_attribute :multiple, true
1597 field.update_attribute :multiple, true
1598
1598
1599 @request.session[:user_id] = 2
1599 @request.session[:user_id] = 2
1600 get :new, :project_id => 1, :tracker_id => 1
1600 get :new, :project_id => 1, :tracker_id => 1
1601 assert_response :success
1601 assert_response :success
1602 assert_template 'new'
1602 assert_template 'new'
1603
1603
1604 assert_select 'select[name=?][multiple=multiple]', 'issue[custom_field_values][1][]' do
1604 assert_select 'select[name=?][multiple=multiple]', 'issue[custom_field_values][1][]' do
1605 assert_select 'option', 3
1605 assert_select 'option', 3
1606 assert_select 'option[value=MySQL]', :text => 'MySQL'
1606 assert_select 'option[value=MySQL]', :text => 'MySQL'
1607 end
1607 end
1608 assert_select 'input[name=?][type=hidden][value=?]', 'issue[custom_field_values][1][]', ''
1608 assert_select 'input[name=?][type=hidden][value=?]', 'issue[custom_field_values][1][]', ''
1609 end
1609 end
1610
1610
1611 def test_get_new_with_multi_user_custom_field
1611 def test_get_new_with_multi_user_custom_field
1612 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1612 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1613 :tracker_ids => [1], :is_for_all => true)
1613 :tracker_ids => [1], :is_for_all => true)
1614
1614
1615 @request.session[:user_id] = 2
1615 @request.session[:user_id] = 2
1616 get :new, :project_id => 1, :tracker_id => 1
1616 get :new, :project_id => 1, :tracker_id => 1
1617 assert_response :success
1617 assert_response :success
1618 assert_template 'new'
1618 assert_template 'new'
1619
1619
1620 assert_select 'select[name=?][multiple=multiple]', "issue[custom_field_values][#{field.id}][]" do
1620 assert_select 'select[name=?][multiple=multiple]', "issue[custom_field_values][#{field.id}][]" do
1621 assert_select 'option', Project.find(1).users.count
1621 assert_select 'option', Project.find(1).users.count
1622 assert_select 'option[value="2"]', :text => 'John Smith'
1622 assert_select 'option[value="2"]', :text => 'John Smith'
1623 end
1623 end
1624 assert_select 'input[name=?][type=hidden][value=?]', "issue[custom_field_values][#{field.id}][]", ''
1624 assert_select 'input[name=?][type=hidden][value=?]', "issue[custom_field_values][#{field.id}][]", ''
1625 end
1625 end
1626
1626
1627 def test_get_new_with_date_custom_field
1627 def test_get_new_with_date_custom_field
1628 field = IssueCustomField.create!(:name => 'Date', :field_format => 'date', :tracker_ids => [1], :is_for_all => true)
1628 field = IssueCustomField.create!(:name => 'Date', :field_format => 'date', :tracker_ids => [1], :is_for_all => true)
1629
1629
1630 @request.session[:user_id] = 2
1630 @request.session[:user_id] = 2
1631 get :new, :project_id => 1, :tracker_id => 1
1631 get :new, :project_id => 1, :tracker_id => 1
1632 assert_response :success
1632 assert_response :success
1633
1633
1634 assert_select 'input[name=?]', "issue[custom_field_values][#{field.id}]"
1634 assert_select 'input[name=?]', "issue[custom_field_values][#{field.id}]"
1635 end
1635 end
1636
1636
1637 def test_get_new_with_text_custom_field
1637 def test_get_new_with_text_custom_field
1638 field = IssueCustomField.create!(:name => 'Text', :field_format => 'text', :tracker_ids => [1], :is_for_all => true)
1638 field = IssueCustomField.create!(:name => 'Text', :field_format => 'text', :tracker_ids => [1], :is_for_all => true)
1639
1639
1640 @request.session[:user_id] = 2
1640 @request.session[:user_id] = 2
1641 get :new, :project_id => 1, :tracker_id => 1
1641 get :new, :project_id => 1, :tracker_id => 1
1642 assert_response :success
1642 assert_response :success
1643
1643
1644 assert_select 'textarea[name=?]', "issue[custom_field_values][#{field.id}]"
1644 assert_select 'textarea[name=?]', "issue[custom_field_values][#{field.id}]"
1645 end
1645 end
1646
1646
1647 def test_get_new_without_default_start_date_is_creation_date
1647 def test_get_new_without_default_start_date_is_creation_date
1648 with_settings :default_issue_start_date_to_creation_date => 0 do
1648 with_settings :default_issue_start_date_to_creation_date => 0 do
1649 @request.session[:user_id] = 2
1649 @request.session[:user_id] = 2
1650 get :new, :project_id => 1, :tracker_id => 1
1650 get :new, :project_id => 1, :tracker_id => 1
1651 assert_response :success
1651 assert_response :success
1652 assert_template 'new'
1652 assert_template 'new'
1653 assert_select 'input[name=?]', 'issue[start_date]'
1653 assert_select 'input[name=?]', 'issue[start_date]'
1654 assert_select 'input[name=?][value]', 'issue[start_date]', 0
1654 assert_select 'input[name=?][value]', 'issue[start_date]', 0
1655 end
1655 end
1656 end
1656 end
1657
1657
1658 def test_get_new_with_default_start_date_is_creation_date
1658 def test_get_new_with_default_start_date_is_creation_date
1659 with_settings :default_issue_start_date_to_creation_date => 1 do
1659 with_settings :default_issue_start_date_to_creation_date => 1 do
1660 @request.session[:user_id] = 2
1660 @request.session[:user_id] = 2
1661 get :new, :project_id => 1, :tracker_id => 1
1661 get :new, :project_id => 1, :tracker_id => 1
1662 assert_response :success
1662 assert_response :success
1663 assert_template 'new'
1663 assert_template 'new'
1664 assert_select 'input[name=?][value=?]', 'issue[start_date]',
1664 assert_select 'input[name=?][value=?]', 'issue[start_date]',
1665 Date.today.to_s
1665 Date.today.to_s
1666 end
1666 end
1667 end
1667 end
1668
1668
1669 def test_get_new_form_should_allow_attachment_upload
1669 def test_get_new_form_should_allow_attachment_upload
1670 @request.session[:user_id] = 2
1670 @request.session[:user_id] = 2
1671 get :new, :project_id => 1, :tracker_id => 1
1671 get :new, :project_id => 1, :tracker_id => 1
1672
1672
1673 assert_select 'form[id=issue-form][method=post][enctype="multipart/form-data"]' do
1673 assert_select 'form[id=issue-form][method=post][enctype="multipart/form-data"]' do
1674 assert_select 'input[name=?][type=file]', 'attachments[dummy][file]'
1674 assert_select 'input[name=?][type=file]', 'attachments[dummy][file]'
1675 end
1675 end
1676 end
1676 end
1677
1677
1678 def test_get_new_should_prefill_the_form_from_params
1678 def test_get_new_should_prefill_the_form_from_params
1679 @request.session[:user_id] = 2
1679 @request.session[:user_id] = 2
1680 get :new, :project_id => 1,
1680 get :new, :project_id => 1,
1681 :issue => {:tracker_id => 3, :description => 'Prefilled', :custom_field_values => {'2' => 'Custom field value'}}
1681 :issue => {:tracker_id => 3, :description => 'Prefilled', :custom_field_values => {'2' => 'Custom field value'}}
1682
1682
1683 issue = assigns(:issue)
1683 issue = assigns(:issue)
1684 assert_equal 3, issue.tracker_id
1684 assert_equal 3, issue.tracker_id
1685 assert_equal 'Prefilled', issue.description
1685 assert_equal 'Prefilled', issue.description
1686 assert_equal 'Custom field value', issue.custom_field_value(2)
1686 assert_equal 'Custom field value', issue.custom_field_value(2)
1687
1687
1688 assert_select 'select[name=?]', 'issue[tracker_id]' do
1688 assert_select 'select[name=?]', 'issue[tracker_id]' do
1689 assert_select 'option[value="3"][selected=selected]'
1689 assert_select 'option[value="3"][selected=selected]'
1690 end
1690 end
1691 assert_select 'textarea[name=?]', 'issue[description]', :text => /Prefilled/
1691 assert_select 'textarea[name=?]', 'issue[description]', :text => /Prefilled/
1692 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Custom field value'
1692 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Custom field value'
1693 end
1693 end
1694
1694
1695 def test_get_new_should_mark_required_fields
1695 def test_get_new_should_mark_required_fields
1696 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1696 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1697 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1697 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1698 WorkflowPermission.delete_all
1698 WorkflowPermission.delete_all
1699 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'required')
1699 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'required')
1700 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'required')
1700 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'required')
1701 @request.session[:user_id] = 2
1701 @request.session[:user_id] = 2
1702
1702
1703 get :new, :project_id => 1
1703 get :new, :project_id => 1
1704 assert_response :success
1704 assert_response :success
1705 assert_template 'new'
1705 assert_template 'new'
1706
1706
1707 assert_select 'label[for=issue_start_date]' do
1707 assert_select 'label[for=issue_start_date]' do
1708 assert_select 'span[class=required]', 0
1708 assert_select 'span[class=required]', 0
1709 end
1709 end
1710 assert_select 'label[for=issue_due_date]' do
1710 assert_select 'label[for=issue_due_date]' do
1711 assert_select 'span[class=required]'
1711 assert_select 'span[class=required]'
1712 end
1712 end
1713 assert_select 'label[for=?]', "issue_custom_field_values_#{cf1.id}" do
1713 assert_select 'label[for=?]', "issue_custom_field_values_#{cf1.id}" do
1714 assert_select 'span[class=required]', 0
1714 assert_select 'span[class=required]', 0
1715 end
1715 end
1716 assert_select 'label[for=?]', "issue_custom_field_values_#{cf2.id}" do
1716 assert_select 'label[for=?]', "issue_custom_field_values_#{cf2.id}" do
1717 assert_select 'span[class=required]'
1717 assert_select 'span[class=required]'
1718 end
1718 end
1719 end
1719 end
1720
1720
1721 def test_get_new_should_not_display_readonly_fields
1721 def test_get_new_should_not_display_readonly_fields
1722 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1722 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1723 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1723 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1724 WorkflowPermission.delete_all
1724 WorkflowPermission.delete_all
1725 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
1725 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
1726 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
1726 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
1727 @request.session[:user_id] = 2
1727 @request.session[:user_id] = 2
1728
1728
1729 get :new, :project_id => 1
1729 get :new, :project_id => 1
1730 assert_response :success
1730 assert_response :success
1731 assert_template 'new'
1731 assert_template 'new'
1732
1732
1733 assert_select 'input[name=?]', 'issue[start_date]'
1733 assert_select 'input[name=?]', 'issue[start_date]'
1734 assert_select 'input[name=?]', 'issue[due_date]', 0
1734 assert_select 'input[name=?]', 'issue[due_date]', 0
1735 assert_select 'input[name=?]', "issue[custom_field_values][#{cf1.id}]"
1735 assert_select 'input[name=?]', "issue[custom_field_values][#{cf1.id}]"
1736 assert_select 'input[name=?]', "issue[custom_field_values][#{cf2.id}]", 0
1736 assert_select 'input[name=?]', "issue[custom_field_values][#{cf2.id}]", 0
1737 end
1737 end
1738
1738
1739 def test_get_new_without_tracker_id
1739 def test_get_new_without_tracker_id
1740 @request.session[:user_id] = 2
1740 @request.session[:user_id] = 2
1741 get :new, :project_id => 1
1741 get :new, :project_id => 1
1742 assert_response :success
1742 assert_response :success
1743 assert_template 'new'
1743 assert_template 'new'
1744
1744
1745 issue = assigns(:issue)
1745 issue = assigns(:issue)
1746 assert_not_nil issue
1746 assert_not_nil issue
1747 assert_equal Project.find(1).trackers.first, issue.tracker
1747 assert_equal Project.find(1).trackers.first, issue.tracker
1748 end
1748 end
1749
1749
1750 def test_get_new_with_no_default_status_should_display_an_error
1750 def test_get_new_with_no_default_status_should_display_an_error
1751 @request.session[:user_id] = 2
1751 @request.session[:user_id] = 2
1752 IssueStatus.delete_all
1752 IssueStatus.delete_all
1753
1753
1754 get :new, :project_id => 1
1754 get :new, :project_id => 1
1755 assert_response 500
1755 assert_response 500
1756 assert_select_error /No default issue/
1756 assert_select_error /No default issue/
1757 end
1757 end
1758
1758
1759 def test_get_new_with_no_tracker_should_display_an_error
1759 def test_get_new_with_no_tracker_should_display_an_error
1760 @request.session[:user_id] = 2
1760 @request.session[:user_id] = 2
1761 Tracker.delete_all
1761 Tracker.delete_all
1762
1762
1763 get :new, :project_id => 1
1763 get :new, :project_id => 1
1764 assert_response 500
1764 assert_response 500
1765 assert_select_error /No tracker/
1765 assert_select_error /No tracker/
1766 end
1766 end
1767
1767
1768 def test_new_with_invalid_project_id
1768 def test_new_with_invalid_project_id
1769 @request.session[:user_id] = 1
1769 @request.session[:user_id] = 1
1770 get :new, :project_id => 'invalid'
1770 get :new, :project_id => 'invalid'
1771 assert_response 404
1771 assert_response 404
1772 end
1772 end
1773
1773
1774 def test_update_form_for_new_issue
1774 def test_update_form_for_new_issue
1775 @request.session[:user_id] = 2
1775 @request.session[:user_id] = 2
1776 xhr :post, :new, :project_id => 1,
1776 xhr :post, :new, :project_id => 1,
1777 :issue => {:tracker_id => 2,
1777 :issue => {:tracker_id => 2,
1778 :subject => 'This is the test_new issue',
1778 :subject => 'This is the test_new issue',
1779 :description => 'This is the description',
1779 :description => 'This is the description',
1780 :priority_id => 5}
1780 :priority_id => 5}
1781 assert_response :success
1781 assert_response :success
1782 assert_template 'new'
1782 assert_template 'new'
1783 assert_template :partial => '_form'
1783 assert_template :partial => '_form'
1784 assert_equal 'text/javascript', response.content_type
1784 assert_equal 'text/javascript', response.content_type
1785
1785
1786 issue = assigns(:issue)
1786 issue = assigns(:issue)
1787 assert_kind_of Issue, issue
1787 assert_kind_of Issue, issue
1788 assert_equal 1, issue.project_id
1788 assert_equal 1, issue.project_id
1789 assert_equal 2, issue.tracker_id
1789 assert_equal 2, issue.tracker_id
1790 assert_equal 'This is the test_new issue', issue.subject
1790 assert_equal 'This is the test_new issue', issue.subject
1791 end
1791 end
1792
1792
1793 def test_update_form_for_new_issue_should_propose_transitions_based_on_initial_status
1793 def test_update_form_for_new_issue_should_propose_transitions_based_on_initial_status
1794 @request.session[:user_id] = 2
1794 @request.session[:user_id] = 2
1795 WorkflowTransition.delete_all
1795 WorkflowTransition.delete_all
1796 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 2)
1796 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 2)
1797 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5)
1797 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5)
1798 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 5, :new_status_id => 4)
1798 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 5, :new_status_id => 4)
1799
1799
1800 xhr :post, :new, :project_id => 1,
1800 xhr :post, :new, :project_id => 1,
1801 :issue => {:tracker_id => 1,
1801 :issue => {:tracker_id => 1,
1802 :status_id => 5,
1802 :status_id => 5,
1803 :subject => 'This is an issue'}
1803 :subject => 'This is an issue'}
1804
1804
1805 assert_equal 5, assigns(:issue).status_id
1805 assert_equal 5, assigns(:issue).status_id
1806 assert_equal [1,2,5], assigns(:allowed_statuses).map(&:id).sort
1806 assert_equal [1,2,5], assigns(:allowed_statuses).map(&:id).sort
1807 end
1807 end
1808
1808
1809 def test_update_form_with_default_status_should_ignore_submitted_status_id_if_equals
1809 def test_update_form_with_default_status_should_ignore_submitted_status_id_if_equals
1810 @request.session[:user_id] = 2
1810 @request.session[:user_id] = 2
1811 tracker = Tracker.find(2)
1811 tracker = Tracker.find(2)
1812 tracker.update! :default_status_id => 2
1812 tracker.update! :default_status_id => 2
1813 tracker.generate_transitions! 2, 1, :clear => true
1813 tracker.generate_transitions! 2, 1, :clear => true
1814
1814
1815 xhr :post, :new, :project_id => 1,
1815 xhr :post, :new, :project_id => 1,
1816 :issue => {:tracker_id => 2,
1816 :issue => {:tracker_id => 2,
1817 :status_id => 1},
1817 :status_id => 1},
1818 :was_default_status => 1
1818 :was_default_status => 1
1819
1819
1820 assert_equal 2, assigns(:issue).status_id
1820 assert_equal 2, assigns(:issue).status_id
1821 end
1821 end
1822
1822
1823 def test_post_create
1823 def test_post_create
1824 @request.session[:user_id] = 2
1824 @request.session[:user_id] = 2
1825 assert_difference 'Issue.count' do
1825 assert_difference 'Issue.count' do
1826 assert_no_difference 'Journal.count' do
1826 assert_no_difference 'Journal.count' do
1827 post :create, :project_id => 1,
1827 post :create, :project_id => 1,
1828 :issue => {:tracker_id => 3,
1828 :issue => {:tracker_id => 3,
1829 :status_id => 2,
1829 :status_id => 2,
1830 :subject => 'This is the test_new issue',
1830 :subject => 'This is the test_new issue',
1831 :description => 'This is the description',
1831 :description => 'This is the description',
1832 :priority_id => 5,
1832 :priority_id => 5,
1833 :start_date => '2010-11-07',
1833 :start_date => '2010-11-07',
1834 :estimated_hours => '',
1834 :estimated_hours => '',
1835 :custom_field_values => {'2' => 'Value for field 2'}}
1835 :custom_field_values => {'2' => 'Value for field 2'}}
1836 end
1836 end
1837 end
1837 end
1838 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1838 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1839
1839
1840 issue = Issue.find_by_subject('This is the test_new issue')
1840 issue = Issue.find_by_subject('This is the test_new issue')
1841 assert_not_nil issue
1841 assert_not_nil issue
1842 assert_equal 2, issue.author_id
1842 assert_equal 2, issue.author_id
1843 assert_equal 3, issue.tracker_id
1843 assert_equal 3, issue.tracker_id
1844 assert_equal 2, issue.status_id
1844 assert_equal 2, issue.status_id
1845 assert_equal Date.parse('2010-11-07'), issue.start_date
1845 assert_equal Date.parse('2010-11-07'), issue.start_date
1846 assert_nil issue.estimated_hours
1846 assert_nil issue.estimated_hours
1847 v = issue.custom_values.where(:custom_field_id => 2).first
1847 v = issue.custom_values.where(:custom_field_id => 2).first
1848 assert_not_nil v
1848 assert_not_nil v
1849 assert_equal 'Value for field 2', v.value
1849 assert_equal 'Value for field 2', v.value
1850 end
1850 end
1851
1851
1852 def test_post_new_with_group_assignment
1852 def test_post_new_with_group_assignment
1853 group = Group.find(11)
1853 group = Group.find(11)
1854 project = Project.find(1)
1854 project = Project.find(1)
1855 project.members << Member.new(:principal => group, :roles => [Role.givable.first])
1855 project.members << Member.new(:principal => group, :roles => [Role.givable.first])
1856
1856
1857 with_settings :issue_group_assignment => '1' do
1857 with_settings :issue_group_assignment => '1' do
1858 @request.session[:user_id] = 2
1858 @request.session[:user_id] = 2
1859 assert_difference 'Issue.count' do
1859 assert_difference 'Issue.count' do
1860 post :create, :project_id => project.id,
1860 post :create, :project_id => project.id,
1861 :issue => {:tracker_id => 3,
1861 :issue => {:tracker_id => 3,
1862 :status_id => 1,
1862 :status_id => 1,
1863 :subject => 'This is the test_new_with_group_assignment issue',
1863 :subject => 'This is the test_new_with_group_assignment issue',
1864 :assigned_to_id => group.id}
1864 :assigned_to_id => group.id}
1865 end
1865 end
1866 end
1866 end
1867 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1867 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1868
1868
1869 issue = Issue.find_by_subject('This is the test_new_with_group_assignment issue')
1869 issue = Issue.find_by_subject('This is the test_new_with_group_assignment issue')
1870 assert_not_nil issue
1870 assert_not_nil issue
1871 assert_equal group, issue.assigned_to
1871 assert_equal group, issue.assigned_to
1872 end
1872 end
1873
1873
1874 def test_post_create_without_start_date_and_default_start_date_is_not_creation_date
1874 def test_post_create_without_start_date_and_default_start_date_is_not_creation_date
1875 with_settings :default_issue_start_date_to_creation_date => 0 do
1875 with_settings :default_issue_start_date_to_creation_date => 0 do
1876 @request.session[:user_id] = 2
1876 @request.session[:user_id] = 2
1877 assert_difference 'Issue.count' do
1877 assert_difference 'Issue.count' do
1878 post :create, :project_id => 1,
1878 post :create, :project_id => 1,
1879 :issue => {:tracker_id => 3,
1879 :issue => {:tracker_id => 3,
1880 :status_id => 2,
1880 :status_id => 2,
1881 :subject => 'This is the test_new issue',
1881 :subject => 'This is the test_new issue',
1882 :description => 'This is the description',
1882 :description => 'This is the description',
1883 :priority_id => 5,
1883 :priority_id => 5,
1884 :estimated_hours => '',
1884 :estimated_hours => '',
1885 :custom_field_values => {'2' => 'Value for field 2'}}
1885 :custom_field_values => {'2' => 'Value for field 2'}}
1886 end
1886 end
1887 assert_redirected_to :controller => 'issues', :action => 'show',
1887 assert_redirected_to :controller => 'issues', :action => 'show',
1888 :id => Issue.last.id
1888 :id => Issue.last.id
1889 issue = Issue.find_by_subject('This is the test_new issue')
1889 issue = Issue.find_by_subject('This is the test_new issue')
1890 assert_not_nil issue
1890 assert_not_nil issue
1891 assert_nil issue.start_date
1891 assert_nil issue.start_date
1892 end
1892 end
1893 end
1893 end
1894
1894
1895 def test_post_create_without_start_date_and_default_start_date_is_creation_date
1895 def test_post_create_without_start_date_and_default_start_date_is_creation_date
1896 with_settings :default_issue_start_date_to_creation_date => 1 do
1896 with_settings :default_issue_start_date_to_creation_date => 1 do
1897 @request.session[:user_id] = 2
1897 @request.session[:user_id] = 2
1898 assert_difference 'Issue.count' do
1898 assert_difference 'Issue.count' do
1899 post :create, :project_id => 1,
1899 post :create, :project_id => 1,
1900 :issue => {:tracker_id => 3,
1900 :issue => {:tracker_id => 3,
1901 :status_id => 2,
1901 :status_id => 2,
1902 :subject => 'This is the test_new issue',
1902 :subject => 'This is the test_new issue',
1903 :description => 'This is the description',
1903 :description => 'This is the description',
1904 :priority_id => 5,
1904 :priority_id => 5,
1905 :estimated_hours => '',
1905 :estimated_hours => '',
1906 :custom_field_values => {'2' => 'Value for field 2'}}
1906 :custom_field_values => {'2' => 'Value for field 2'}}
1907 end
1907 end
1908 assert_redirected_to :controller => 'issues', :action => 'show',
1908 assert_redirected_to :controller => 'issues', :action => 'show',
1909 :id => Issue.last.id
1909 :id => Issue.last.id
1910 issue = Issue.find_by_subject('This is the test_new issue')
1910 issue = Issue.find_by_subject('This is the test_new issue')
1911 assert_not_nil issue
1911 assert_not_nil issue
1912 assert_equal Date.today, issue.start_date
1912 assert_equal Date.today, issue.start_date
1913 end
1913 end
1914 end
1914 end
1915
1915
1916 def test_post_create_and_continue
1916 def test_post_create_and_continue
1917 @request.session[:user_id] = 2
1917 @request.session[:user_id] = 2
1918 assert_difference 'Issue.count' do
1918 assert_difference 'Issue.count' do
1919 post :create, :project_id => 1,
1919 post :create, :project_id => 1,
1920 :issue => {:tracker_id => 3, :subject => 'This is first issue', :priority_id => 5},
1920 :issue => {:tracker_id => 3, :subject => 'This is first issue', :priority_id => 5},
1921 :continue => ''
1921 :continue => ''
1922 end
1922 end
1923
1923
1924 issue = Issue.order('id DESC').first
1924 issue = Issue.order('id DESC').first
1925 assert_redirected_to :controller => 'issues', :action => 'new', :project_id => 'ecookbook', :issue => {:tracker_id => 3}
1925 assert_redirected_to :controller => 'issues', :action => 'new', :project_id => 'ecookbook', :issue => {:tracker_id => 3}
1926 assert_not_nil flash[:notice], "flash was not set"
1926 assert_not_nil flash[:notice], "flash was not set"
1927 assert_select_in flash[:notice],
1927 assert_select_in flash[:notice],
1928 'a[href=?][title=?]', "/issues/#{issue.id}", "This is first issue", :text => "##{issue.id}"
1928 'a[href=?][title=?]', "/issues/#{issue.id}", "This is first issue", :text => "##{issue.id}"
1929 end
1929 end
1930
1930
1931 def test_post_create_without_custom_fields_param
1931 def test_post_create_without_custom_fields_param
1932 @request.session[:user_id] = 2
1932 @request.session[:user_id] = 2
1933 assert_difference 'Issue.count' do
1933 assert_difference 'Issue.count' do
1934 post :create, :project_id => 1,
1934 post :create, :project_id => 1,
1935 :issue => {:tracker_id => 1,
1935 :issue => {:tracker_id => 1,
1936 :subject => 'This is the test_new issue',
1936 :subject => 'This is the test_new issue',
1937 :description => 'This is the description',
1937 :description => 'This is the description',
1938 :priority_id => 5}
1938 :priority_id => 5}
1939 end
1939 end
1940 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1940 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1941 end
1941 end
1942
1942
1943 def test_post_create_with_multi_custom_field
1943 def test_post_create_with_multi_custom_field
1944 field = IssueCustomField.find_by_name('Database')
1944 field = IssueCustomField.find_by_name('Database')
1945 field.update_attribute(:multiple, true)
1945 field.update_attribute(:multiple, true)
1946
1946
1947 @request.session[:user_id] = 2
1947 @request.session[:user_id] = 2
1948 assert_difference 'Issue.count' do
1948 assert_difference 'Issue.count' do
1949 post :create, :project_id => 1,
1949 post :create, :project_id => 1,
1950 :issue => {:tracker_id => 1,
1950 :issue => {:tracker_id => 1,
1951 :subject => 'This is the test_new issue',
1951 :subject => 'This is the test_new issue',
1952 :description => 'This is the description',
1952 :description => 'This is the description',
1953 :priority_id => 5,
1953 :priority_id => 5,
1954 :custom_field_values => {'1' => ['', 'MySQL', 'Oracle']}}
1954 :custom_field_values => {'1' => ['', 'MySQL', 'Oracle']}}
1955 end
1955 end
1956 assert_response 302
1956 assert_response 302
1957 issue = Issue.order('id DESC').first
1957 issue = Issue.order('id DESC').first
1958 assert_equal ['MySQL', 'Oracle'], issue.custom_field_value(1).sort
1958 assert_equal ['MySQL', 'Oracle'], issue.custom_field_value(1).sort
1959 end
1959 end
1960
1960
1961 def test_post_create_with_empty_multi_custom_field
1961 def test_post_create_with_empty_multi_custom_field
1962 field = IssueCustomField.find_by_name('Database')
1962 field = IssueCustomField.find_by_name('Database')
1963 field.update_attribute(:multiple, true)
1963 field.update_attribute(:multiple, true)
1964
1964
1965 @request.session[:user_id] = 2
1965 @request.session[:user_id] = 2
1966 assert_difference 'Issue.count' do
1966 assert_difference 'Issue.count' do
1967 post :create, :project_id => 1,
1967 post :create, :project_id => 1,
1968 :issue => {:tracker_id => 1,
1968 :issue => {:tracker_id => 1,
1969 :subject => 'This is the test_new issue',
1969 :subject => 'This is the test_new issue',
1970 :description => 'This is the description',
1970 :description => 'This is the description',
1971 :priority_id => 5,
1971 :priority_id => 5,
1972 :custom_field_values => {'1' => ['']}}
1972 :custom_field_values => {'1' => ['']}}
1973 end
1973 end
1974 assert_response 302
1974 assert_response 302
1975 issue = Issue.order('id DESC').first
1975 issue = Issue.order('id DESC').first
1976 assert_equal [''], issue.custom_field_value(1).sort
1976 assert_equal [''], issue.custom_field_value(1).sort
1977 end
1977 end
1978
1978
1979 def test_post_create_with_multi_user_custom_field
1979 def test_post_create_with_multi_user_custom_field
1980 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1980 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1981 :tracker_ids => [1], :is_for_all => true)
1981 :tracker_ids => [1], :is_for_all => true)
1982
1982
1983 @request.session[:user_id] = 2
1983 @request.session[:user_id] = 2
1984 assert_difference 'Issue.count' do
1984 assert_difference 'Issue.count' do
1985 post :create, :project_id => 1,
1985 post :create, :project_id => 1,
1986 :issue => {:tracker_id => 1,
1986 :issue => {:tracker_id => 1,
1987 :subject => 'This is the test_new issue',
1987 :subject => 'This is the test_new issue',
1988 :description => 'This is the description',
1988 :description => 'This is the description',
1989 :priority_id => 5,
1989 :priority_id => 5,
1990 :custom_field_values => {field.id.to_s => ['', '2', '3']}}
1990 :custom_field_values => {field.id.to_s => ['', '2', '3']}}
1991 end
1991 end
1992 assert_response 302
1992 assert_response 302
1993 issue = Issue.order('id DESC').first
1993 issue = Issue.order('id DESC').first
1994 assert_equal ['2', '3'], issue.custom_field_value(field).sort
1994 assert_equal ['2', '3'], issue.custom_field_value(field).sort
1995 end
1995 end
1996
1996
1997 def test_post_create_with_required_custom_field_and_without_custom_fields_param
1997 def test_post_create_with_required_custom_field_and_without_custom_fields_param
1998 field = IssueCustomField.find_by_name('Database')
1998 field = IssueCustomField.find_by_name('Database')
1999 field.update_attribute(:is_required, true)
1999 field.update_attribute(:is_required, true)
2000
2000
2001 @request.session[:user_id] = 2
2001 @request.session[:user_id] = 2
2002 assert_no_difference 'Issue.count' do
2002 assert_no_difference 'Issue.count' do
2003 post :create, :project_id => 1,
2003 post :create, :project_id => 1,
2004 :issue => {:tracker_id => 1,
2004 :issue => {:tracker_id => 1,
2005 :subject => 'This is the test_new issue',
2005 :subject => 'This is the test_new issue',
2006 :description => 'This is the description',
2006 :description => 'This is the description',
2007 :priority_id => 5}
2007 :priority_id => 5}
2008 end
2008 end
2009 assert_response :success
2009 assert_response :success
2010 assert_template 'new'
2010 assert_template 'new'
2011 issue = assigns(:issue)
2011 issue = assigns(:issue)
2012 assert_not_nil issue
2012 assert_not_nil issue
2013 assert_select_error /Database cannot be blank/
2013 assert_select_error /Database cannot be blank/
2014 end
2014 end
2015
2015
2016 def test_create_should_validate_required_fields
2016 def test_create_should_validate_required_fields
2017 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2017 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2018 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2018 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2019 WorkflowPermission.delete_all
2019 WorkflowPermission.delete_all
2020 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'due_date', :rule => 'required')
2020 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'due_date', :rule => 'required')
2021 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'required')
2021 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'required')
2022 @request.session[:user_id] = 2
2022 @request.session[:user_id] = 2
2023
2023
2024 assert_no_difference 'Issue.count' do
2024 assert_no_difference 'Issue.count' do
2025 post :create, :project_id => 1, :issue => {
2025 post :create, :project_id => 1, :issue => {
2026 :tracker_id => 2,
2026 :tracker_id => 2,
2027 :status_id => 1,
2027 :status_id => 1,
2028 :subject => 'Test',
2028 :subject => 'Test',
2029 :start_date => '',
2029 :start_date => '',
2030 :due_date => '',
2030 :due_date => '',
2031 :custom_field_values => {cf1.id.to_s => '', cf2.id.to_s => ''}
2031 :custom_field_values => {cf1.id.to_s => '', cf2.id.to_s => ''}
2032 }
2032 }
2033 assert_response :success
2033 assert_response :success
2034 assert_template 'new'
2034 assert_template 'new'
2035 end
2035 end
2036
2036
2037 assert_select_error /Due date cannot be blank/i
2037 assert_select_error /Due date cannot be blank/i
2038 assert_select_error /Bar cannot be blank/i
2038 assert_select_error /Bar cannot be blank/i
2039 end
2039 end
2040
2040
2041 def test_create_should_ignore_readonly_fields
2041 def test_create_should_ignore_readonly_fields
2042 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2042 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2043 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2043 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2044 WorkflowPermission.delete_all
2044 WorkflowPermission.delete_all
2045 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
2045 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
2046 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
2046 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
2047 @request.session[:user_id] = 2
2047 @request.session[:user_id] = 2
2048
2048
2049 assert_difference 'Issue.count' do
2049 assert_difference 'Issue.count' do
2050 post :create, :project_id => 1, :issue => {
2050 post :create, :project_id => 1, :issue => {
2051 :tracker_id => 2,
2051 :tracker_id => 2,
2052 :status_id => 1,
2052 :status_id => 1,
2053 :subject => 'Test',
2053 :subject => 'Test',
2054 :start_date => '2012-07-14',
2054 :start_date => '2012-07-14',
2055 :due_date => '2012-07-16',
2055 :due_date => '2012-07-16',
2056 :custom_field_values => {cf1.id.to_s => 'value1', cf2.id.to_s => 'value2'}
2056 :custom_field_values => {cf1.id.to_s => 'value1', cf2.id.to_s => 'value2'}
2057 }
2057 }
2058 assert_response 302
2058 assert_response 302
2059 end
2059 end
2060
2060
2061 issue = Issue.order('id DESC').first
2061 issue = Issue.order('id DESC').first
2062 assert_equal Date.parse('2012-07-14'), issue.start_date
2062 assert_equal Date.parse('2012-07-14'), issue.start_date
2063 assert_nil issue.due_date
2063 assert_nil issue.due_date
2064 assert_equal 'value1', issue.custom_field_value(cf1)
2064 assert_equal 'value1', issue.custom_field_value(cf1)
2065 assert_nil issue.custom_field_value(cf2)
2065 assert_nil issue.custom_field_value(cf2)
2066 end
2066 end
2067
2067
2068 def test_post_create_with_watchers
2068 def test_post_create_with_watchers
2069 @request.session[:user_id] = 2
2069 @request.session[:user_id] = 2
2070 ActionMailer::Base.deliveries.clear
2070 ActionMailer::Base.deliveries.clear
2071
2071
2072 with_settings :notified_events => %w(issue_added) do
2072 with_settings :notified_events => %w(issue_added) do
2073 assert_difference 'Watcher.count', 2 do
2073 assert_difference 'Watcher.count', 2 do
2074 post :create, :project_id => 1,
2074 post :create, :project_id => 1,
2075 :issue => {:tracker_id => 1,
2075 :issue => {:tracker_id => 1,
2076 :subject => 'This is a new issue with watchers',
2076 :subject => 'This is a new issue with watchers',
2077 :description => 'This is the description',
2077 :description => 'This is the description',
2078 :priority_id => 5,
2078 :priority_id => 5,
2079 :watcher_user_ids => ['2', '3']}
2079 :watcher_user_ids => ['2', '3']}
2080 end
2080 end
2081 end
2081 end
2082 issue = Issue.find_by_subject('This is a new issue with watchers')
2082 issue = Issue.find_by_subject('This is a new issue with watchers')
2083 assert_not_nil issue
2083 assert_not_nil issue
2084 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
2084 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
2085
2085
2086 # Watchers added
2086 # Watchers added
2087 assert_equal [2, 3], issue.watcher_user_ids.sort
2087 assert_equal [2, 3], issue.watcher_user_ids.sort
2088 assert issue.watched_by?(User.find(3))
2088 assert issue.watched_by?(User.find(3))
2089 # Watchers notified
2089 # Watchers notified
2090 mail = ActionMailer::Base.deliveries.last
2090 mail = ActionMailer::Base.deliveries.last
2091 assert_not_nil mail
2091 assert_not_nil mail
2092 assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
2092 assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
2093 end
2093 end
2094
2094
2095 def test_post_create_subissue
2095 def test_post_create_subissue
2096 @request.session[:user_id] = 2
2096 @request.session[:user_id] = 2
2097
2097
2098 assert_difference 'Issue.count' do
2098 assert_difference 'Issue.count' do
2099 post :create, :project_id => 1,
2099 post :create, :project_id => 1,
2100 :issue => {:tracker_id => 1,
2100 :issue => {:tracker_id => 1,
2101 :subject => 'This is a child issue',
2101 :subject => 'This is a child issue',
2102 :parent_issue_id => '2'}
2102 :parent_issue_id => '2'}
2103 assert_response 302
2103 assert_response 302
2104 end
2104 end
2105 issue = Issue.order('id DESC').first
2105 issue = Issue.order('id DESC').first
2106 assert_equal Issue.find(2), issue.parent
2106 assert_equal Issue.find(2), issue.parent
2107 end
2107 end
2108
2108
2109 def test_post_create_subissue_with_sharp_parent_id
2109 def test_post_create_subissue_with_sharp_parent_id
2110 @request.session[:user_id] = 2
2110 @request.session[:user_id] = 2
2111
2111
2112 assert_difference 'Issue.count' do
2112 assert_difference 'Issue.count' do
2113 post :create, :project_id => 1,
2113 post :create, :project_id => 1,
2114 :issue => {:tracker_id => 1,
2114 :issue => {:tracker_id => 1,
2115 :subject => 'This is a child issue',
2115 :subject => 'This is a child issue',
2116 :parent_issue_id => '#2'}
2116 :parent_issue_id => '#2'}
2117 assert_response 302
2117 assert_response 302
2118 end
2118 end
2119 issue = Issue.order('id DESC').first
2119 issue = Issue.order('id DESC').first
2120 assert_equal Issue.find(2), issue.parent
2120 assert_equal Issue.find(2), issue.parent
2121 end
2121 end
2122
2122
2123 def test_post_create_subissue_with_non_visible_parent_id_should_not_validate
2123 def test_post_create_subissue_with_non_visible_parent_id_should_not_validate
2124 @request.session[:user_id] = 2
2124 @request.session[:user_id] = 2
2125
2125
2126 assert_no_difference 'Issue.count' do
2126 assert_no_difference 'Issue.count' do
2127 post :create, :project_id => 1,
2127 post :create, :project_id => 1,
2128 :issue => {:tracker_id => 1,
2128 :issue => {:tracker_id => 1,
2129 :subject => 'This is a child issue',
2129 :subject => 'This is a child issue',
2130 :parent_issue_id => '4'}
2130 :parent_issue_id => '4'}
2131
2131
2132 assert_response :success
2132 assert_response :success
2133 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', '4'
2133 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', '4'
2134 assert_select_error /Parent task is invalid/i
2134 assert_select_error /Parent task is invalid/i
2135 end
2135 end
2136 end
2136 end
2137
2137
2138 def test_post_create_subissue_with_non_numeric_parent_id_should_not_validate
2138 def test_post_create_subissue_with_non_numeric_parent_id_should_not_validate
2139 @request.session[:user_id] = 2
2139 @request.session[:user_id] = 2
2140
2140
2141 assert_no_difference 'Issue.count' do
2141 assert_no_difference 'Issue.count' do
2142 post :create, :project_id => 1,
2142 post :create, :project_id => 1,
2143 :issue => {:tracker_id => 1,
2143 :issue => {:tracker_id => 1,
2144 :subject => 'This is a child issue',
2144 :subject => 'This is a child issue',
2145 :parent_issue_id => '01ABC'}
2145 :parent_issue_id => '01ABC'}
2146
2146
2147 assert_response :success
2147 assert_response :success
2148 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', '01ABC'
2148 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', '01ABC'
2149 assert_select_error /Parent task is invalid/i
2149 assert_select_error /Parent task is invalid/i
2150 end
2150 end
2151 end
2151 end
2152
2152
2153 def test_post_create_private
2153 def test_post_create_private
2154 @request.session[:user_id] = 2
2154 @request.session[:user_id] = 2
2155
2155
2156 assert_difference 'Issue.count' do
2156 assert_difference 'Issue.count' do
2157 post :create, :project_id => 1,
2157 post :create, :project_id => 1,
2158 :issue => {:tracker_id => 1,
2158 :issue => {:tracker_id => 1,
2159 :subject => 'This is a private issue',
2159 :subject => 'This is a private issue',
2160 :is_private => '1'}
2160 :is_private => '1'}
2161 end
2161 end
2162 issue = Issue.order('id DESC').first
2162 issue = Issue.order('id DESC').first
2163 assert issue.is_private?
2163 assert issue.is_private?
2164 end
2164 end
2165
2165
2166 def test_post_create_private_with_set_own_issues_private_permission
2166 def test_post_create_private_with_set_own_issues_private_permission
2167 role = Role.find(1)
2167 role = Role.find(1)
2168 role.remove_permission! :set_issues_private
2168 role.remove_permission! :set_issues_private
2169 role.add_permission! :set_own_issues_private
2169 role.add_permission! :set_own_issues_private
2170
2170
2171 @request.session[:user_id] = 2
2171 @request.session[:user_id] = 2
2172
2172
2173 assert_difference 'Issue.count' do
2173 assert_difference 'Issue.count' do
2174 post :create, :project_id => 1,
2174 post :create, :project_id => 1,
2175 :issue => {:tracker_id => 1,
2175 :issue => {:tracker_id => 1,
2176 :subject => 'This is a private issue',
2176 :subject => 'This is a private issue',
2177 :is_private => '1'}
2177 :is_private => '1'}
2178 end
2178 end
2179 issue = Issue.order('id DESC').first
2179 issue = Issue.order('id DESC').first
2180 assert issue.is_private?
2180 assert issue.is_private?
2181 end
2181 end
2182
2182
2183 def test_create_without_project_id
2183 def test_create_without_project_id
2184 @request.session[:user_id] = 2
2184 @request.session[:user_id] = 2
2185
2185
2186 assert_difference 'Issue.count' do
2186 assert_difference 'Issue.count' do
2187 post :create,
2187 post :create,
2188 :issue => {:project_id => 3,
2188 :issue => {:project_id => 3,
2189 :tracker_id => 2,
2189 :tracker_id => 2,
2190 :subject => 'Foo'}
2190 :subject => 'Foo'}
2191 assert_response 302
2191 assert_response 302
2192 end
2192 end
2193 issue = Issue.order('id DESC').first
2193 issue = Issue.order('id DESC').first
2194 assert_equal 3, issue.project_id
2194 assert_equal 3, issue.project_id
2195 assert_equal 2, issue.tracker_id
2195 assert_equal 2, issue.tracker_id
2196 end
2196 end
2197
2197
2198 def test_create_without_project_id_and_continue_should_redirect_without_project_id
2198 def test_create_without_project_id_and_continue_should_redirect_without_project_id
2199 @request.session[:user_id] = 2
2199 @request.session[:user_id] = 2
2200
2200
2201 assert_difference 'Issue.count' do
2201 assert_difference 'Issue.count' do
2202 post :create,
2202 post :create,
2203 :issue => {:project_id => 3,
2203 :issue => {:project_id => 3,
2204 :tracker_id => 2,
2204 :tracker_id => 2,
2205 :subject => 'Foo'},
2205 :subject => 'Foo'},
2206 :continue => '1'
2206 :continue => '1'
2207 assert_redirected_to '/issues/new?issue%5Bproject_id%5D=3&issue%5Btracker_id%5D=2'
2207 assert_redirected_to '/issues/new?issue%5Bproject_id%5D=3&issue%5Btracker_id%5D=2'
2208 end
2208 end
2209 end
2209 end
2210
2210
2211 def test_create_without_project_id_should_be_denied_without_permission
2211 def test_create_without_project_id_should_be_denied_without_permission
2212 Role.non_member.remove_permission! :add_issues
2212 Role.non_member.remove_permission! :add_issues
2213 Role.anonymous.remove_permission! :add_issues
2213 Role.anonymous.remove_permission! :add_issues
2214 @request.session[:user_id] = 2
2214 @request.session[:user_id] = 2
2215
2215
2216 assert_no_difference 'Issue.count' do
2216 assert_no_difference 'Issue.count' do
2217 post :create,
2217 post :create,
2218 :issue => {:project_id => 3,
2218 :issue => {:project_id => 3,
2219 :tracker_id => 2,
2219 :tracker_id => 2,
2220 :subject => 'Foo'}
2220 :subject => 'Foo'}
2221 assert_response 403
2221 assert_response 403
2222 end
2222 end
2223 end
2223 end
2224
2224
2225 def test_create_without_project_id_with_failure
2225 def test_create_without_project_id_with_failure
2226 @request.session[:user_id] = 2
2226 @request.session[:user_id] = 2
2227
2227
2228 post :create,
2228 post :create,
2229 :issue => {:project_id => 3,
2229 :issue => {:project_id => 3,
2230 :tracker_id => 2,
2230 :tracker_id => 2,
2231 :subject => ''}
2231 :subject => ''}
2232 assert_response :success
2232 assert_response :success
2233 assert_nil assigns(:project)
2233 assert_nil assigns(:project)
2234 end
2234 end
2235
2235
2236 def test_post_create_should_send_a_notification
2236 def test_post_create_should_send_a_notification
2237 ActionMailer::Base.deliveries.clear
2237 ActionMailer::Base.deliveries.clear
2238 @request.session[:user_id] = 2
2238 @request.session[:user_id] = 2
2239 with_settings :notified_events => %w(issue_added) do
2239 with_settings :notified_events => %w(issue_added) do
2240 assert_difference 'Issue.count' do
2240 assert_difference 'Issue.count' do
2241 post :create, :project_id => 1,
2241 post :create, :project_id => 1,
2242 :issue => {:tracker_id => 3,
2242 :issue => {:tracker_id => 3,
2243 :subject => 'This is the test_new issue',
2243 :subject => 'This is the test_new issue',
2244 :description => 'This is the description',
2244 :description => 'This is the description',
2245 :priority_id => 5,
2245 :priority_id => 5,
2246 :estimated_hours => '',
2246 :estimated_hours => '',
2247 :custom_field_values => {'2' => 'Value for field 2'}}
2247 :custom_field_values => {'2' => 'Value for field 2'}}
2248 end
2248 end
2249 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
2249 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
2250
2250
2251 assert_equal 1, ActionMailer::Base.deliveries.size
2251 assert_equal 1, ActionMailer::Base.deliveries.size
2252 end
2252 end
2253 end
2253 end
2254
2254
2255 def test_post_create_should_preserve_fields_values_on_validation_failure
2255 def test_post_create_should_preserve_fields_values_on_validation_failure
2256 @request.session[:user_id] = 2
2256 @request.session[:user_id] = 2
2257 post :create, :project_id => 1,
2257 post :create, :project_id => 1,
2258 :issue => {:tracker_id => 1,
2258 :issue => {:tracker_id => 1,
2259 # empty subject
2259 # empty subject
2260 :subject => '',
2260 :subject => '',
2261 :description => 'This is a description',
2261 :description => 'This is a description',
2262 :priority_id => 6,
2262 :priority_id => 6,
2263 :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
2263 :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
2264 assert_response :success
2264 assert_response :success
2265 assert_template 'new'
2265 assert_template 'new'
2266
2266
2267 assert_select 'textarea[name=?]', 'issue[description]', :text => 'This is a description'
2267 assert_select 'textarea[name=?]', 'issue[description]', :text => 'This is a description'
2268 assert_select 'select[name=?]', 'issue[priority_id]' do
2268 assert_select 'select[name=?]', 'issue[priority_id]' do
2269 assert_select 'option[value="6"][selected=selected]', :text => 'High'
2269 assert_select 'option[value="6"][selected=selected]', :text => 'High'
2270 end
2270 end
2271 # Custom fields
2271 # Custom fields
2272 assert_select 'select[name=?]', 'issue[custom_field_values][1]' do
2272 assert_select 'select[name=?]', 'issue[custom_field_values][1]' do
2273 assert_select 'option[value=Oracle][selected=selected]', :text => 'Oracle'
2273 assert_select 'option[value=Oracle][selected=selected]', :text => 'Oracle'
2274 end
2274 end
2275 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Value for field 2'
2275 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Value for field 2'
2276 end
2276 end
2277
2277
2278 def test_post_create_with_failure_should_preserve_watchers
2278 def test_post_create_with_failure_should_preserve_watchers
2279 assert !User.find(8).member_of?(Project.find(1))
2279 assert !User.find(8).member_of?(Project.find(1))
2280
2280
2281 @request.session[:user_id] = 2
2281 @request.session[:user_id] = 2
2282 post :create, :project_id => 1,
2282 post :create, :project_id => 1,
2283 :issue => {:tracker_id => 1,
2283 :issue => {:tracker_id => 1,
2284 :watcher_user_ids => ['3', '8']}
2284 :watcher_user_ids => ['3', '8']}
2285 assert_response :success
2285 assert_response :success
2286 assert_template 'new'
2286 assert_template 'new'
2287
2287
2288 assert_select 'input[name=?][value="2"]:not(checked)', 'issue[watcher_user_ids][]'
2288 assert_select 'input[name=?][value="2"]:not(checked)', 'issue[watcher_user_ids][]'
2289 assert_select 'input[name=?][value="3"][checked=checked]', 'issue[watcher_user_ids][]'
2289 assert_select 'input[name=?][value="3"][checked=checked]', 'issue[watcher_user_ids][]'
2290 assert_select 'input[name=?][value="8"][checked=checked]', 'issue[watcher_user_ids][]'
2290 assert_select 'input[name=?][value="8"][checked=checked]', 'issue[watcher_user_ids][]'
2291 end
2291 end
2292
2292
2293 def test_post_create_should_ignore_non_safe_attributes
2293 def test_post_create_should_ignore_non_safe_attributes
2294 @request.session[:user_id] = 2
2294 @request.session[:user_id] = 2
2295 assert_nothing_raised do
2295 assert_nothing_raised do
2296 post :create, :project_id => 1, :issue => { :tracker => "A param can not be a Tracker" }
2296 post :create, :project_id => 1, :issue => { :tracker => "A param can not be a Tracker" }
2297 end
2297 end
2298 end
2298 end
2299
2299
2300 def test_post_create_with_attachment
2300 def test_post_create_with_attachment
2301 set_tmp_attachments_directory
2301 set_tmp_attachments_directory
2302 @request.session[:user_id] = 2
2302 @request.session[:user_id] = 2
2303
2303
2304 assert_difference 'Issue.count' do
2304 assert_difference 'Issue.count' do
2305 assert_difference 'Attachment.count' do
2305 assert_difference 'Attachment.count' do
2306 assert_no_difference 'Journal.count' do
2306 assert_no_difference 'Journal.count' do
2307 post :create, :project_id => 1,
2307 post :create, :project_id => 1,
2308 :issue => { :tracker_id => '1', :subject => 'With attachment' },
2308 :issue => { :tracker_id => '1', :subject => 'With attachment' },
2309 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2309 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2310 end
2310 end
2311 end
2311 end
2312 end
2312 end
2313
2313
2314 issue = Issue.order('id DESC').first
2314 issue = Issue.order('id DESC').first
2315 attachment = Attachment.order('id DESC').first
2315 attachment = Attachment.order('id DESC').first
2316
2316
2317 assert_equal issue, attachment.container
2317 assert_equal issue, attachment.container
2318 assert_equal 2, attachment.author_id
2318 assert_equal 2, attachment.author_id
2319 assert_equal 'testfile.txt', attachment.filename
2319 assert_equal 'testfile.txt', attachment.filename
2320 assert_equal 'text/plain', attachment.content_type
2320 assert_equal 'text/plain', attachment.content_type
2321 assert_equal 'test file', attachment.description
2321 assert_equal 'test file', attachment.description
2322 assert_equal 59, attachment.filesize
2322 assert_equal 59, attachment.filesize
2323 assert File.exists?(attachment.diskfile)
2323 assert File.exists?(attachment.diskfile)
2324 assert_equal 59, File.size(attachment.diskfile)
2324 assert_equal 59, File.size(attachment.diskfile)
2325 end
2325 end
2326
2326
2327 def test_post_create_with_attachment_should_notify_with_attachments
2327 def test_post_create_with_attachment_should_notify_with_attachments
2328 ActionMailer::Base.deliveries.clear
2328 ActionMailer::Base.deliveries.clear
2329 set_tmp_attachments_directory
2329 set_tmp_attachments_directory
2330 @request.session[:user_id] = 2
2330 @request.session[:user_id] = 2
2331
2331
2332 with_settings :host_name => 'mydomain.foo', :protocol => 'http', :notified_events => %w(issue_added) do
2332 with_settings :host_name => 'mydomain.foo', :protocol => 'http', :notified_events => %w(issue_added) do
2333 assert_difference 'Issue.count' do
2333 assert_difference 'Issue.count' do
2334 post :create, :project_id => 1,
2334 post :create, :project_id => 1,
2335 :issue => { :tracker_id => '1', :subject => 'With attachment' },
2335 :issue => { :tracker_id => '1', :subject => 'With attachment' },
2336 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2336 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2337 end
2337 end
2338 end
2338 end
2339
2339
2340 assert_not_nil ActionMailer::Base.deliveries.last
2340 assert_not_nil ActionMailer::Base.deliveries.last
2341 assert_select_email do
2341 assert_select_email do
2342 assert_select 'a[href^=?]', 'http://mydomain.foo/attachments/download', 'testfile.txt'
2342 assert_select 'a[href^=?]', 'http://mydomain.foo/attachments/download', 'testfile.txt'
2343 end
2343 end
2344 end
2344 end
2345
2345
2346 def test_post_create_with_failure_should_save_attachments
2346 def test_post_create_with_failure_should_save_attachments
2347 set_tmp_attachments_directory
2347 set_tmp_attachments_directory
2348 @request.session[:user_id] = 2
2348 @request.session[:user_id] = 2
2349
2349
2350 assert_no_difference 'Issue.count' do
2350 assert_no_difference 'Issue.count' do
2351 assert_difference 'Attachment.count' do
2351 assert_difference 'Attachment.count' do
2352 post :create, :project_id => 1,
2352 post :create, :project_id => 1,
2353 :issue => { :tracker_id => '1', :subject => '' },
2353 :issue => { :tracker_id => '1', :subject => '' },
2354 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2354 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2355 assert_response :success
2355 assert_response :success
2356 assert_template 'new'
2356 assert_template 'new'
2357 end
2357 end
2358 end
2358 end
2359
2359
2360 attachment = Attachment.order('id DESC').first
2360 attachment = Attachment.order('id DESC').first
2361 assert_equal 'testfile.txt', attachment.filename
2361 assert_equal 'testfile.txt', attachment.filename
2362 assert File.exists?(attachment.diskfile)
2362 assert File.exists?(attachment.diskfile)
2363 assert_nil attachment.container
2363 assert_nil attachment.container
2364
2364
2365 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
2365 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
2366 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
2366 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
2367 end
2367 end
2368
2368
2369 def test_post_create_with_failure_should_keep_saved_attachments
2369 def test_post_create_with_failure_should_keep_saved_attachments
2370 set_tmp_attachments_directory
2370 set_tmp_attachments_directory
2371 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
2371 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
2372 @request.session[:user_id] = 2
2372 @request.session[:user_id] = 2
2373
2373
2374 assert_no_difference 'Issue.count' do
2374 assert_no_difference 'Issue.count' do
2375 assert_no_difference 'Attachment.count' do
2375 assert_no_difference 'Attachment.count' do
2376 post :create, :project_id => 1,
2376 post :create, :project_id => 1,
2377 :issue => { :tracker_id => '1', :subject => '' },
2377 :issue => { :tracker_id => '1', :subject => '' },
2378 :attachments => {'p0' => {'token' => attachment.token}}
2378 :attachments => {'p0' => {'token' => attachment.token}}
2379 assert_response :success
2379 assert_response :success
2380 assert_template 'new'
2380 assert_template 'new'
2381 end
2381 end
2382 end
2382 end
2383
2383
2384 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
2384 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
2385 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
2385 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
2386 end
2386 end
2387
2387
2388 def test_post_create_should_attach_saved_attachments
2388 def test_post_create_should_attach_saved_attachments
2389 set_tmp_attachments_directory
2389 set_tmp_attachments_directory
2390 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
2390 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
2391 @request.session[:user_id] = 2
2391 @request.session[:user_id] = 2
2392
2392
2393 assert_difference 'Issue.count' do
2393 assert_difference 'Issue.count' do
2394 assert_no_difference 'Attachment.count' do
2394 assert_no_difference 'Attachment.count' do
2395 post :create, :project_id => 1,
2395 post :create, :project_id => 1,
2396 :issue => { :tracker_id => '1', :subject => 'Saved attachments' },
2396 :issue => { :tracker_id => '1', :subject => 'Saved attachments' },
2397 :attachments => {'p0' => {'token' => attachment.token}}
2397 :attachments => {'p0' => {'token' => attachment.token}}
2398 assert_response 302
2398 assert_response 302
2399 end
2399 end
2400 end
2400 end
2401
2401
2402 issue = Issue.order('id DESC').first
2402 issue = Issue.order('id DESC').first
2403 assert_equal 1, issue.attachments.count
2403 assert_equal 1, issue.attachments.count
2404
2404
2405 attachment.reload
2405 attachment.reload
2406 assert_equal issue, attachment.container
2406 assert_equal issue, attachment.container
2407 end
2407 end
2408
2408
2409 def setup_without_workflow_privilege
2409 def setup_without_workflow_privilege
2410 WorkflowTransition.delete_all(["role_id = ?", Role.anonymous.id])
2410 WorkflowTransition.delete_all(["role_id = ?", Role.anonymous.id])
2411 Role.anonymous.add_permission! :add_issues, :add_issue_notes
2411 Role.anonymous.add_permission! :add_issues, :add_issue_notes
2412 end
2412 end
2413 private :setup_without_workflow_privilege
2413 private :setup_without_workflow_privilege
2414
2414
2415 test "without workflow privilege #new should propose default status only" do
2415 test "without workflow privilege #new should propose default status only" do
2416 setup_without_workflow_privilege
2416 setup_without_workflow_privilege
2417 get :new, :project_id => 1
2417 get :new, :project_id => 1
2418 assert_response :success
2418 assert_response :success
2419 assert_template 'new'
2419 assert_template 'new'
2420
2420
2421 issue = assigns(:issue)
2421 issue = assigns(:issue)
2422 assert_not_nil issue.default_status
2422 assert_not_nil issue.default_status
2423
2423
2424 assert_select 'select[name=?]', 'issue[status_id]' do
2424 assert_select 'select[name=?]', 'issue[status_id]' do
2425 assert_select 'option', 1
2425 assert_select 'option', 1
2426 assert_select 'option[value=?]', issue.default_status.id.to_s
2426 assert_select 'option[value=?]', issue.default_status.id.to_s
2427 end
2427 end
2428 end
2428 end
2429
2429
2430 test "without workflow privilege #create should accept default status" do
2430 test "without workflow privilege #create should accept default status" do
2431 setup_without_workflow_privilege
2431 setup_without_workflow_privilege
2432 assert_difference 'Issue.count' do
2432 assert_difference 'Issue.count' do
2433 post :create, :project_id => 1,
2433 post :create, :project_id => 1,
2434 :issue => {:tracker_id => 1,
2434 :issue => {:tracker_id => 1,
2435 :subject => 'This is an issue',
2435 :subject => 'This is an issue',
2436 :status_id => 1}
2436 :status_id => 1}
2437 end
2437 end
2438 issue = Issue.order('id').last
2438 issue = Issue.order('id').last
2439 assert_not_nil issue.default_status
2439 assert_not_nil issue.default_status
2440 assert_equal issue.default_status, issue.status
2440 assert_equal issue.default_status, issue.status
2441 end
2441 end
2442
2442
2443 test "without workflow privilege #create should ignore unauthorized status" do
2443 test "without workflow privilege #create should ignore unauthorized status" do
2444 setup_without_workflow_privilege
2444 setup_without_workflow_privilege
2445 assert_difference 'Issue.count' do
2445 assert_difference 'Issue.count' do
2446 post :create, :project_id => 1,
2446 post :create, :project_id => 1,
2447 :issue => {:tracker_id => 1,
2447 :issue => {:tracker_id => 1,
2448 :subject => 'This is an issue',
2448 :subject => 'This is an issue',
2449 :status_id => 3}
2449 :status_id => 3}
2450 end
2450 end
2451 issue = Issue.order('id').last
2451 issue = Issue.order('id').last
2452 assert_not_nil issue.default_status
2452 assert_not_nil issue.default_status
2453 assert_equal issue.default_status, issue.status
2453 assert_equal issue.default_status, issue.status
2454 end
2454 end
2455
2455
2456 test "without workflow privilege #update should ignore status change" do
2456 test "without workflow privilege #update should ignore status change" do
2457 setup_without_workflow_privilege
2457 setup_without_workflow_privilege
2458 assert_difference 'Journal.count' do
2458 assert_difference 'Journal.count' do
2459 put :update, :id => 1, :issue => {:status_id => 3, :notes => 'just trying'}
2459 put :update, :id => 1, :issue => {:status_id => 3, :notes => 'just trying'}
2460 end
2460 end
2461 assert_equal 1, Issue.find(1).status_id
2461 assert_equal 1, Issue.find(1).status_id
2462 end
2462 end
2463
2463
2464 test "without workflow privilege #update ignore attributes changes" do
2464 test "without workflow privilege #update ignore attributes changes" do
2465 setup_without_workflow_privilege
2465 setup_without_workflow_privilege
2466 assert_difference 'Journal.count' do
2466 assert_difference 'Journal.count' do
2467 put :update, :id => 1,
2467 put :update, :id => 1,
2468 :issue => {:subject => 'changed', :assigned_to_id => 2,
2468 :issue => {:subject => 'changed', :assigned_to_id => 2,
2469 :notes => 'just trying'}
2469 :notes => 'just trying'}
2470 end
2470 end
2471 issue = Issue.find(1)
2471 issue = Issue.find(1)
2472 assert_equal "Cannot print recipes", issue.subject
2472 assert_equal "Cannot print recipes", issue.subject
2473 assert_nil issue.assigned_to
2473 assert_nil issue.assigned_to
2474 end
2474 end
2475
2475
2476 def setup_with_workflow_privilege
2476 def setup_with_workflow_privilege
2477 WorkflowTransition.delete_all(["role_id = ?", Role.anonymous.id])
2477 WorkflowTransition.delete_all(["role_id = ?", Role.anonymous.id])
2478 WorkflowTransition.create!(:role => Role.anonymous, :tracker_id => 1,
2478 WorkflowTransition.create!(:role => Role.anonymous, :tracker_id => 1,
2479 :old_status_id => 1, :new_status_id => 3)
2479 :old_status_id => 1, :new_status_id => 3)
2480 WorkflowTransition.create!(:role => Role.anonymous, :tracker_id => 1,
2480 WorkflowTransition.create!(:role => Role.anonymous, :tracker_id => 1,
2481 :old_status_id => 1, :new_status_id => 4)
2481 :old_status_id => 1, :new_status_id => 4)
2482 Role.anonymous.add_permission! :add_issues, :add_issue_notes
2482 Role.anonymous.add_permission! :add_issues, :add_issue_notes
2483 end
2483 end
2484 private :setup_with_workflow_privilege
2484 private :setup_with_workflow_privilege
2485
2485
2486 def setup_with_workflow_privilege_and_edit_issues_permission
2486 def setup_with_workflow_privilege_and_edit_issues_permission
2487 setup_with_workflow_privilege
2487 setup_with_workflow_privilege
2488 Role.anonymous.add_permission! :add_issues, :edit_issues
2488 Role.anonymous.add_permission! :add_issues, :edit_issues
2489 end
2489 end
2490 private :setup_with_workflow_privilege_and_edit_issues_permission
2490 private :setup_with_workflow_privilege_and_edit_issues_permission
2491
2491
2492 test "with workflow privilege and :edit_issues permission should accept authorized status" do
2492 test "with workflow privilege and :edit_issues permission should accept authorized status" do
2493 setup_with_workflow_privilege_and_edit_issues_permission
2493 setup_with_workflow_privilege_and_edit_issues_permission
2494 assert_difference 'Journal.count' do
2494 assert_difference 'Journal.count' do
2495 put :update, :id => 1, :issue => {:status_id => 3, :notes => 'just trying'}
2495 put :update, :id => 1, :issue => {:status_id => 3, :notes => 'just trying'}
2496 end
2496 end
2497 assert_equal 3, Issue.find(1).status_id
2497 assert_equal 3, Issue.find(1).status_id
2498 end
2498 end
2499
2499
2500 test "with workflow privilege and :edit_issues permission should ignore unauthorized status" do
2500 test "with workflow privilege and :edit_issues permission should ignore unauthorized status" do
2501 setup_with_workflow_privilege_and_edit_issues_permission
2501 setup_with_workflow_privilege_and_edit_issues_permission
2502 assert_difference 'Journal.count' do
2502 assert_difference 'Journal.count' do
2503 put :update, :id => 1, :issue => {:status_id => 2, :notes => 'just trying'}
2503 put :update, :id => 1, :issue => {:status_id => 2, :notes => 'just trying'}
2504 end
2504 end
2505 assert_equal 1, Issue.find(1).status_id
2505 assert_equal 1, Issue.find(1).status_id
2506 end
2506 end
2507
2507
2508 test "with workflow privilege and :edit_issues permission should accept authorized attributes changes" do
2508 test "with workflow privilege and :edit_issues permission should accept authorized attributes changes" do
2509 setup_with_workflow_privilege_and_edit_issues_permission
2509 setup_with_workflow_privilege_and_edit_issues_permission
2510 assert_difference 'Journal.count' do
2510 assert_difference 'Journal.count' do
2511 put :update, :id => 1,
2511 put :update, :id => 1,
2512 :issue => {:subject => 'changed', :assigned_to_id => 2,
2512 :issue => {:subject => 'changed', :assigned_to_id => 2,
2513 :notes => 'just trying'}
2513 :notes => 'just trying'}
2514 end
2514 end
2515 issue = Issue.find(1)
2515 issue = Issue.find(1)
2516 assert_equal "changed", issue.subject
2516 assert_equal "changed", issue.subject
2517 assert_equal 2, issue.assigned_to_id
2517 assert_equal 2, issue.assigned_to_id
2518 end
2518 end
2519
2519
2520 def test_new_as_copy
2520 def test_new_as_copy
2521 @request.session[:user_id] = 2
2521 @request.session[:user_id] = 2
2522 get :new, :project_id => 1, :copy_from => 1
2522 get :new, :project_id => 1, :copy_from => 1
2523
2523
2524 assert_response :success
2524 assert_response :success
2525 assert_template 'new'
2525 assert_template 'new'
2526
2526
2527 assert_not_nil assigns(:issue)
2527 assert_not_nil assigns(:issue)
2528 orig = Issue.find(1)
2528 orig = Issue.find(1)
2529 assert_equal 1, assigns(:issue).project_id
2529 assert_equal 1, assigns(:issue).project_id
2530 assert_equal orig.subject, assigns(:issue).subject
2530 assert_equal orig.subject, assigns(:issue).subject
2531 assert assigns(:issue).copy?
2531 assert assigns(:issue).copy?
2532
2532
2533 assert_select 'form[id=issue-form][action="/projects/ecookbook/issues"]' do
2533 assert_select 'form[id=issue-form][action="/projects/ecookbook/issues"]' do
2534 assert_select 'select[name=?]', 'issue[project_id]' do
2534 assert_select 'select[name=?]', 'issue[project_id]' do
2535 assert_select 'option[value="1"][selected=selected]', :text => 'eCookbook'
2535 assert_select 'option[value="1"][selected=selected]', :text => 'eCookbook'
2536 assert_select 'option[value="2"]:not([selected])', :text => 'OnlineStore'
2536 assert_select 'option[value="2"]:not([selected])', :text => 'OnlineStore'
2537 end
2537 end
2538 assert_select 'input[name=copy_from][value="1"]'
2538 assert_select 'input[name=copy_from][value="1"]'
2539 end
2539 end
2540
2540
2541 # "New issue" menu item should not link to copy
2541 # "New issue" menu item should not link to copy
2542 assert_select '#main-menu a.new-issue[href="/projects/ecookbook/issues/new"]'
2542 assert_select '#main-menu a.new-issue[href="/projects/ecookbook/issues/new"]'
2543 end
2543 end
2544
2544
2545 def test_new_as_copy_without_add_issues_permission_should_not_propose_current_project_as_target
2545 def test_new_as_copy_without_add_issues_permission_should_not_propose_current_project_as_target
2546 user = setup_user_with_copy_but_not_add_permission
2546 user = setup_user_with_copy_but_not_add_permission
2547
2547
2548 @request.session[:user_id] = user.id
2548 @request.session[:user_id] = user.id
2549 get :new, :project_id => 1, :copy_from => 1
2549 get :new, :project_id => 1, :copy_from => 1
2550
2550
2551 assert_response :success
2551 assert_response :success
2552 assert_template 'new'
2552 assert_template 'new'
2553 assert_select 'select[name=?]', 'issue[project_id]' do
2553 assert_select 'select[name=?]', 'issue[project_id]' do
2554 assert_select 'option[value="1"]', 0
2554 assert_select 'option[value="1"]', 0
2555 assert_select 'option[value="2"]', :text => 'OnlineStore'
2555 assert_select 'option[value="2"]', :text => 'OnlineStore'
2556 end
2556 end
2557 end
2557 end
2558
2558
2559 def test_new_as_copy_with_attachments_should_show_copy_attachments_checkbox
2559 def test_new_as_copy_with_attachments_should_show_copy_attachments_checkbox
2560 @request.session[:user_id] = 2
2560 @request.session[:user_id] = 2
2561 issue = Issue.find(3)
2561 issue = Issue.find(3)
2562 assert issue.attachments.count > 0
2562 assert issue.attachments.count > 0
2563 get :new, :project_id => 1, :copy_from => 3
2563 get :new, :project_id => 1, :copy_from => 3
2564
2564
2565 assert_select 'input[name=copy_attachments][type=checkbox][checked=checked][value="1"]'
2565 assert_select 'input[name=copy_attachments][type=checkbox][checked=checked][value="1"]'
2566 end
2566 end
2567
2567
2568 def test_new_as_copy_without_attachments_should_not_show_copy_attachments_checkbox
2568 def test_new_as_copy_without_attachments_should_not_show_copy_attachments_checkbox
2569 @request.session[:user_id] = 2
2569 @request.session[:user_id] = 2
2570 issue = Issue.find(3)
2570 issue = Issue.find(3)
2571 issue.attachments.delete_all
2571 issue.attachments.delete_all
2572 get :new, :project_id => 1, :copy_from => 3
2572 get :new, :project_id => 1, :copy_from => 3
2573
2573
2574 assert_select 'input[name=copy_attachments]', 0
2574 assert_select 'input[name=copy_attachments]', 0
2575 end
2575 end
2576
2576
2577 def test_new_as_copy_with_subtasks_should_show_copy_subtasks_checkbox
2577 def test_new_as_copy_with_subtasks_should_show_copy_subtasks_checkbox
2578 @request.session[:user_id] = 2
2578 @request.session[:user_id] = 2
2579 issue = Issue.generate_with_descendants!
2579 issue = Issue.generate_with_descendants!
2580 get :new, :project_id => 1, :copy_from => issue.id
2580 get :new, :project_id => 1, :copy_from => issue.id
2581
2581
2582 assert_select 'input[type=checkbox][name=copy_subtasks][checked=checked][value="1"]'
2582 assert_select 'input[type=checkbox][name=copy_subtasks][checked=checked][value="1"]'
2583 end
2583 end
2584
2584
2585 def test_new_as_copy_with_invalid_issue_should_respond_with_404
2585 def test_new_as_copy_with_invalid_issue_should_respond_with_404
2586 @request.session[:user_id] = 2
2586 @request.session[:user_id] = 2
2587 get :new, :project_id => 1, :copy_from => 99999
2587 get :new, :project_id => 1, :copy_from => 99999
2588 assert_response 404
2588 assert_response 404
2589 end
2589 end
2590
2590
2591 def test_create_as_copy_on_different_project
2591 def test_create_as_copy_on_different_project
2592 @request.session[:user_id] = 2
2592 @request.session[:user_id] = 2
2593 assert_difference 'Issue.count' do
2593 assert_difference 'Issue.count' do
2594 post :create, :project_id => 1, :copy_from => 1,
2594 post :create, :project_id => 1, :copy_from => 1,
2595 :issue => {:project_id => '2', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
2595 :issue => {:project_id => '2', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
2596
2596
2597 assert_not_nil assigns(:issue)
2597 assert_not_nil assigns(:issue)
2598 assert assigns(:issue).copy?
2598 assert assigns(:issue).copy?
2599 end
2599 end
2600 issue = Issue.order('id DESC').first
2600 issue = Issue.order('id DESC').first
2601 assert_redirected_to "/issues/#{issue.id}"
2601 assert_redirected_to "/issues/#{issue.id}"
2602
2602
2603 assert_equal 2, issue.project_id
2603 assert_equal 2, issue.project_id
2604 assert_equal 3, issue.tracker_id
2604 assert_equal 3, issue.tracker_id
2605 assert_equal 'Copy', issue.subject
2605 assert_equal 'Copy', issue.subject
2606 end
2606 end
2607
2607
2608 def test_create_as_copy_should_copy_attachments
2608 def test_create_as_copy_should_copy_attachments
2609 @request.session[:user_id] = 2
2609 @request.session[:user_id] = 2
2610 issue = Issue.find(3)
2610 issue = Issue.find(3)
2611 count = issue.attachments.count
2611 count = issue.attachments.count
2612 assert count > 0
2612 assert count > 0
2613 assert_difference 'Issue.count' do
2613 assert_difference 'Issue.count' do
2614 assert_difference 'Attachment.count', count do
2614 assert_difference 'Attachment.count', count do
2615 post :create, :project_id => 1, :copy_from => 3,
2615 post :create, :project_id => 1, :copy_from => 3,
2616 :issue => {:project_id => '1', :tracker_id => '3',
2616 :issue => {:project_id => '1', :tracker_id => '3',
2617 :status_id => '1', :subject => 'Copy with attachments'},
2617 :status_id => '1', :subject => 'Copy with attachments'},
2618 :copy_attachments => '1'
2618 :copy_attachments => '1'
2619 end
2619 end
2620 end
2620 end
2621 copy = Issue.order('id DESC').first
2621 copy = Issue.order('id DESC').first
2622 assert_equal count, copy.attachments.count
2622 assert_equal count, copy.attachments.count
2623 assert_equal issue.attachments.map(&:filename).sort, copy.attachments.map(&:filename).sort
2623 assert_equal issue.attachments.map(&:filename).sort, copy.attachments.map(&:filename).sort
2624 end
2624 end
2625
2625
2626 def test_create_as_copy_without_copy_attachments_option_should_not_copy_attachments
2626 def test_create_as_copy_without_copy_attachments_option_should_not_copy_attachments
2627 @request.session[:user_id] = 2
2627 @request.session[:user_id] = 2
2628 issue = Issue.find(3)
2628 issue = Issue.find(3)
2629 count = issue.attachments.count
2629 count = issue.attachments.count
2630 assert count > 0
2630 assert count > 0
2631 assert_difference 'Issue.count' do
2631 assert_difference 'Issue.count' do
2632 assert_no_difference 'Attachment.count' do
2632 assert_no_difference 'Attachment.count' do
2633 post :create, :project_id => 1, :copy_from => 3,
2633 post :create, :project_id => 1, :copy_from => 3,
2634 :issue => {:project_id => '1', :tracker_id => '3',
2634 :issue => {:project_id => '1', :tracker_id => '3',
2635 :status_id => '1', :subject => 'Copy with attachments'}
2635 :status_id => '1', :subject => 'Copy with attachments'}
2636 end
2636 end
2637 end
2637 end
2638 copy = Issue.order('id DESC').first
2638 copy = Issue.order('id DESC').first
2639 assert_equal 0, copy.attachments.count
2639 assert_equal 0, copy.attachments.count
2640 end
2640 end
2641
2641
2642 def test_create_as_copy_with_attachments_should_also_add_new_files
2642 def test_create_as_copy_with_attachments_should_also_add_new_files
2643 @request.session[:user_id] = 2
2643 @request.session[:user_id] = 2
2644 issue = Issue.find(3)
2644 issue = Issue.find(3)
2645 count = issue.attachments.count
2645 count = issue.attachments.count
2646 assert count > 0
2646 assert count > 0
2647 assert_difference 'Issue.count' do
2647 assert_difference 'Issue.count' do
2648 assert_difference 'Attachment.count', count + 1 do
2648 assert_difference 'Attachment.count', count + 1 do
2649 post :create, :project_id => 1, :copy_from => 3,
2649 post :create, :project_id => 1, :copy_from => 3,
2650 :issue => {:project_id => '1', :tracker_id => '3',
2650 :issue => {:project_id => '1', :tracker_id => '3',
2651 :status_id => '1', :subject => 'Copy with attachments'},
2651 :status_id => '1', :subject => 'Copy with attachments'},
2652 :copy_attachments => '1',
2652 :copy_attachments => '1',
2653 :attachments => {'1' =>
2653 :attachments => {'1' =>
2654 {'file' => uploaded_test_file('testfile.txt', 'text/plain'),
2654 {'file' => uploaded_test_file('testfile.txt', 'text/plain'),
2655 'description' => 'test file'}}
2655 'description' => 'test file'}}
2656 end
2656 end
2657 end
2657 end
2658 copy = Issue.order('id DESC').first
2658 copy = Issue.order('id DESC').first
2659 assert_equal count + 1, copy.attachments.count
2659 assert_equal count + 1, copy.attachments.count
2660 end
2660 end
2661
2661
2662 def test_create_as_copy_should_add_relation_with_copied_issue
2662 def test_create_as_copy_should_add_relation_with_copied_issue
2663 @request.session[:user_id] = 2
2663 @request.session[:user_id] = 2
2664 assert_difference 'Issue.count' do
2664 assert_difference 'Issue.count' do
2665 assert_difference 'IssueRelation.count' do
2665 assert_difference 'IssueRelation.count' do
2666 post :create, :project_id => 1, :copy_from => 1, :link_copy => '1',
2666 post :create, :project_id => 1, :copy_from => 1, :link_copy => '1',
2667 :issue => {:project_id => '1', :tracker_id => '3',
2667 :issue => {:project_id => '1', :tracker_id => '3',
2668 :status_id => '1', :subject => 'Copy'}
2668 :status_id => '1', :subject => 'Copy'}
2669 end
2669 end
2670 end
2670 end
2671 copy = Issue.order('id DESC').first
2671 copy = Issue.order('id DESC').first
2672 assert_equal 1, copy.relations.size
2672 assert_equal 1, copy.relations.size
2673 end
2673 end
2674
2674
2675 def test_create_as_copy_should_allow_not_to_add_relation_with_copied_issue
2675 def test_create_as_copy_should_allow_not_to_add_relation_with_copied_issue
2676 @request.session[:user_id] = 2
2676 @request.session[:user_id] = 2
2677 assert_difference 'Issue.count' do
2677 assert_difference 'Issue.count' do
2678 assert_no_difference 'IssueRelation.count' do
2678 assert_no_difference 'IssueRelation.count' do
2679 post :create, :project_id => 1, :copy_from => 1,
2679 post :create, :project_id => 1, :copy_from => 1,
2680 :issue => {:subject => 'Copy'}
2680 :issue => {:subject => 'Copy'}
2681 end
2681 end
2682 end
2682 end
2683 end
2683 end
2684
2684
2685 def test_create_as_copy_should_always_add_relation_with_copied_issue_by_setting
2685 def test_create_as_copy_should_always_add_relation_with_copied_issue_by_setting
2686 with_settings :link_copied_issue => 'yes' do
2686 with_settings :link_copied_issue => 'yes' do
2687 @request.session[:user_id] = 2
2687 @request.session[:user_id] = 2
2688 assert_difference 'Issue.count' do
2688 assert_difference 'Issue.count' do
2689 assert_difference 'IssueRelation.count' do
2689 assert_difference 'IssueRelation.count' do
2690 post :create, :project_id => 1, :copy_from => 1,
2690 post :create, :project_id => 1, :copy_from => 1,
2691 :issue => {:subject => 'Copy'}
2691 :issue => {:subject => 'Copy'}
2692 end
2692 end
2693 end
2693 end
2694 end
2694 end
2695 end
2695 end
2696
2696
2697 def test_create_as_copy_should_never_add_relation_with_copied_issue_by_setting
2697 def test_create_as_copy_should_never_add_relation_with_copied_issue_by_setting
2698 with_settings :link_copied_issue => 'no' do
2698 with_settings :link_copied_issue => 'no' do
2699 @request.session[:user_id] = 2
2699 @request.session[:user_id] = 2
2700 assert_difference 'Issue.count' do
2700 assert_difference 'Issue.count' do
2701 assert_no_difference 'IssueRelation.count' do
2701 assert_no_difference 'IssueRelation.count' do
2702 post :create, :project_id => 1, :copy_from => 1, :link_copy => '1',
2702 post :create, :project_id => 1, :copy_from => 1, :link_copy => '1',
2703 :issue => {:subject => 'Copy'}
2703 :issue => {:subject => 'Copy'}
2704 end
2704 end
2705 end
2705 end
2706 end
2706 end
2707 end
2707 end
2708
2708
2709 def test_create_as_copy_should_copy_subtasks
2709 def test_create_as_copy_should_copy_subtasks
2710 @request.session[:user_id] = 2
2710 @request.session[:user_id] = 2
2711 issue = Issue.generate_with_descendants!
2711 issue = Issue.generate_with_descendants!
2712 count = issue.descendants.count
2712 count = issue.descendants.count
2713 assert_difference 'Issue.count', count + 1 do
2713 assert_difference 'Issue.count', count + 1 do
2714 post :create, :project_id => 1, :copy_from => issue.id,
2714 post :create, :project_id => 1, :copy_from => issue.id,
2715 :issue => {:project_id => '1', :tracker_id => '3',
2715 :issue => {:project_id => '1', :tracker_id => '3',
2716 :status_id => '1', :subject => 'Copy with subtasks'},
2716 :status_id => '1', :subject => 'Copy with subtasks'},
2717 :copy_subtasks => '1'
2717 :copy_subtasks => '1'
2718 end
2718 end
2719 copy = Issue.where(:parent_id => nil).order('id DESC').first
2719 copy = Issue.where(:parent_id => nil).order('id DESC').first
2720 assert_equal count, copy.descendants.count
2720 assert_equal count, copy.descendants.count
2721 assert_equal issue.descendants.map(&:subject).sort, copy.descendants.map(&:subject).sort
2721 assert_equal issue.descendants.map(&:subject).sort, copy.descendants.map(&:subject).sort
2722 end
2722 end
2723
2723
2724 def test_create_as_copy_without_copy_subtasks_option_should_not_copy_subtasks
2724 def test_create_as_copy_without_copy_subtasks_option_should_not_copy_subtasks
2725 @request.session[:user_id] = 2
2725 @request.session[:user_id] = 2
2726 issue = Issue.generate_with_descendants!
2726 issue = Issue.generate_with_descendants!
2727 assert_difference 'Issue.count', 1 do
2727 assert_difference 'Issue.count', 1 do
2728 post :create, :project_id => 1, :copy_from => 3,
2728 post :create, :project_id => 1, :copy_from => 3,
2729 :issue => {:project_id => '1', :tracker_id => '3',
2729 :issue => {:project_id => '1', :tracker_id => '3',
2730 :status_id => '1', :subject => 'Copy with subtasks'}
2730 :status_id => '1', :subject => 'Copy with subtasks'}
2731 end
2731 end
2732 copy = Issue.where(:parent_id => nil).order('id DESC').first
2732 copy = Issue.where(:parent_id => nil).order('id DESC').first
2733 assert_equal 0, copy.descendants.count
2733 assert_equal 0, copy.descendants.count
2734 end
2734 end
2735
2735
2736 def test_create_as_copy_with_failure
2736 def test_create_as_copy_with_failure
2737 @request.session[:user_id] = 2
2737 @request.session[:user_id] = 2
2738 post :create, :project_id => 1, :copy_from => 1,
2738 post :create, :project_id => 1, :copy_from => 1,
2739 :issue => {:project_id => '2', :tracker_id => '3', :status_id => '1', :subject => ''}
2739 :issue => {:project_id => '2', :tracker_id => '3', :status_id => '1', :subject => ''}
2740
2740
2741 assert_response :success
2741 assert_response :success
2742 assert_template 'new'
2742 assert_template 'new'
2743
2743
2744 assert_not_nil assigns(:issue)
2744 assert_not_nil assigns(:issue)
2745 assert assigns(:issue).copy?
2745 assert assigns(:issue).copy?
2746
2746
2747 assert_select 'form#issue-form[action="/projects/ecookbook/issues"]' do
2747 assert_select 'form#issue-form[action="/projects/ecookbook/issues"]' do
2748 assert_select 'select[name=?]', 'issue[project_id]' do
2748 assert_select 'select[name=?]', 'issue[project_id]' do
2749 assert_select 'option[value="1"]:not([selected])', :text => 'eCookbook'
2749 assert_select 'option[value="1"]:not([selected])', :text => 'eCookbook'
2750 assert_select 'option[value="2"][selected=selected]', :text => 'OnlineStore'
2750 assert_select 'option[value="2"][selected=selected]', :text => 'OnlineStore'
2751 end
2751 end
2752 assert_select 'input[name=copy_from][value="1"]'
2752 assert_select 'input[name=copy_from][value="1"]'
2753 end
2753 end
2754 end
2754 end
2755
2755
2756 def test_create_as_copy_on_project_without_permission_should_ignore_target_project
2756 def test_create_as_copy_on_project_without_permission_should_ignore_target_project
2757 @request.session[:user_id] = 2
2757 @request.session[:user_id] = 2
2758 assert !User.find(2).member_of?(Project.find(4))
2758 assert !User.find(2).member_of?(Project.find(4))
2759
2759
2760 assert_difference 'Issue.count' do
2760 assert_difference 'Issue.count' do
2761 post :create, :project_id => 1, :copy_from => 1,
2761 post :create, :project_id => 1, :copy_from => 1,
2762 :issue => {:project_id => '4', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
2762 :issue => {:project_id => '4', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
2763 end
2763 end
2764 issue = Issue.order('id DESC').first
2764 issue = Issue.order('id DESC').first
2765 assert_equal 1, issue.project_id
2765 assert_equal 1, issue.project_id
2766 end
2766 end
2767
2767
2768 def test_get_edit
2768 def test_get_edit
2769 @request.session[:user_id] = 2
2769 @request.session[:user_id] = 2
2770 get :edit, :id => 1
2770 get :edit, :id => 1
2771 assert_response :success
2771 assert_response :success
2772 assert_template 'edit'
2772 assert_template 'edit'
2773 assert_not_nil assigns(:issue)
2773 assert_not_nil assigns(:issue)
2774 assert_equal Issue.find(1), assigns(:issue)
2774 assert_equal Issue.find(1), assigns(:issue)
2775
2775
2776 # Be sure we don't display inactive IssuePriorities
2776 # Be sure we don't display inactive IssuePriorities
2777 assert ! IssuePriority.find(15).active?
2777 assert ! IssuePriority.find(15).active?
2778 assert_select 'select[name=?]', 'issue[priority_id]' do
2778 assert_select 'select[name=?]', 'issue[priority_id]' do
2779 assert_select 'option[value="15"]', 0
2779 assert_select 'option[value="15"]', 0
2780 end
2780 end
2781 end
2781 end
2782
2782
2783 def test_get_edit_should_display_the_time_entry_form_with_log_time_permission
2783 def test_get_edit_should_display_the_time_entry_form_with_log_time_permission
2784 @request.session[:user_id] = 2
2784 @request.session[:user_id] = 2
2785 Role.find_by_name('Manager').update_attribute :permissions, [:view_issues, :edit_issues, :log_time]
2785 Role.find_by_name('Manager').update_attribute :permissions, [:view_issues, :edit_issues, :log_time]
2786
2786
2787 get :edit, :id => 1
2787 get :edit, :id => 1
2788 assert_select 'input[name=?]', 'time_entry[hours]'
2788 assert_select 'input[name=?]', 'time_entry[hours]'
2789 end
2789 end
2790
2790
2791 def test_get_edit_should_not_display_the_time_entry_form_without_log_time_permission
2791 def test_get_edit_should_not_display_the_time_entry_form_without_log_time_permission
2792 @request.session[:user_id] = 2
2792 @request.session[:user_id] = 2
2793 Role.find_by_name('Manager').remove_permission! :log_time
2793 Role.find_by_name('Manager').remove_permission! :log_time
2794
2794
2795 get :edit, :id => 1
2795 get :edit, :id => 1
2796 assert_select 'input[name=?]', 'time_entry[hours]', 0
2796 assert_select 'input[name=?]', 'time_entry[hours]', 0
2797 end
2797 end
2798
2798
2799 def test_get_edit_with_params
2799 def test_get_edit_with_params
2800 @request.session[:user_id] = 2
2800 @request.session[:user_id] = 2
2801 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 },
2801 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 },
2802 :time_entry => { :hours => '2.5', :comments => 'test_get_edit_with_params', :activity_id => 10 }
2802 :time_entry => { :hours => '2.5', :comments => 'test_get_edit_with_params', :activity_id => 10 }
2803 assert_response :success
2803 assert_response :success
2804 assert_template 'edit'
2804 assert_template 'edit'
2805
2805
2806 issue = assigns(:issue)
2806 issue = assigns(:issue)
2807 assert_not_nil issue
2807 assert_not_nil issue
2808
2808
2809 assert_equal 5, issue.status_id
2809 assert_equal 5, issue.status_id
2810 assert_select 'select[name=?]', 'issue[status_id]' do
2810 assert_select 'select[name=?]', 'issue[status_id]' do
2811 assert_select 'option[value="5"][selected=selected]', :text => 'Closed'
2811 assert_select 'option[value="5"][selected=selected]', :text => 'Closed'
2812 end
2812 end
2813
2813
2814 assert_equal 7, issue.priority_id
2814 assert_equal 7, issue.priority_id
2815 assert_select 'select[name=?]', 'issue[priority_id]' do
2815 assert_select 'select[name=?]', 'issue[priority_id]' do
2816 assert_select 'option[value="7"][selected=selected]', :text => 'Urgent'
2816 assert_select 'option[value="7"][selected=selected]', :text => 'Urgent'
2817 end
2817 end
2818
2818
2819 assert_select 'input[name=?][value="2.5"]', 'time_entry[hours]'
2819 assert_select 'input[name=?][value="2.5"]', 'time_entry[hours]'
2820 assert_select 'select[name=?]', 'time_entry[activity_id]' do
2820 assert_select 'select[name=?]', 'time_entry[activity_id]' do
2821 assert_select 'option[value="10"][selected=selected]', :text => 'Development'
2821 assert_select 'option[value="10"][selected=selected]', :text => 'Development'
2822 end
2822 end
2823 assert_select 'input[name=?][value=test_get_edit_with_params]', 'time_entry[comments]'
2823 assert_select 'input[name=?][value=test_get_edit_with_params]', 'time_entry[comments]'
2824 end
2824 end
2825
2825
2826 def test_get_edit_with_multi_custom_field
2826 def test_get_edit_with_multi_custom_field
2827 field = CustomField.find(1)
2827 field = CustomField.find(1)
2828 field.update_attribute :multiple, true
2828 field.update_attribute :multiple, true
2829 issue = Issue.find(1)
2829 issue = Issue.find(1)
2830 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
2830 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
2831 issue.save!
2831 issue.save!
2832
2832
2833 @request.session[:user_id] = 2
2833 @request.session[:user_id] = 2
2834 get :edit, :id => 1
2834 get :edit, :id => 1
2835 assert_response :success
2835 assert_response :success
2836 assert_template 'edit'
2836 assert_template 'edit'
2837
2837
2838 assert_select 'select[name=?][multiple=multiple]', 'issue[custom_field_values][1][]' do
2838 assert_select 'select[name=?][multiple=multiple]', 'issue[custom_field_values][1][]' do
2839 assert_select 'option', 3
2839 assert_select 'option', 3
2840 assert_select 'option[value=MySQL][selected=selected]'
2840 assert_select 'option[value=MySQL][selected=selected]'
2841 assert_select 'option[value=Oracle][selected=selected]'
2841 assert_select 'option[value=Oracle][selected=selected]'
2842 assert_select 'option[value=PostgreSQL]:not([selected])'
2842 assert_select 'option[value=PostgreSQL]:not([selected])'
2843 end
2843 end
2844 end
2844 end
2845
2845
2846 def test_update_form_for_existing_issue
2846 def test_update_form_for_existing_issue
2847 @request.session[:user_id] = 2
2847 @request.session[:user_id] = 2
2848 xhr :patch, :edit, :id => 1,
2848 xhr :patch, :edit, :id => 1,
2849 :issue => {:tracker_id => 2,
2849 :issue => {:tracker_id => 2,
2850 :subject => 'This is the test_new issue',
2850 :subject => 'This is the test_new issue',
2851 :description => 'This is the description',
2851 :description => 'This is the description',
2852 :priority_id => 5}
2852 :priority_id => 5}
2853 assert_response :success
2853 assert_response :success
2854 assert_equal 'text/javascript', response.content_type
2854 assert_equal 'text/javascript', response.content_type
2855 assert_template 'edit'
2855 assert_template 'edit'
2856 assert_template :partial => '_form'
2856 assert_template :partial => '_form'
2857
2857
2858 issue = assigns(:issue)
2858 issue = assigns(:issue)
2859 assert_kind_of Issue, issue
2859 assert_kind_of Issue, issue
2860 assert_equal 1, issue.id
2860 assert_equal 1, issue.id
2861 assert_equal 1, issue.project_id
2861 assert_equal 1, issue.project_id
2862 assert_equal 2, issue.tracker_id
2862 assert_equal 2, issue.tracker_id
2863 assert_equal 'This is the test_new issue', issue.subject
2863 assert_equal 'This is the test_new issue', issue.subject
2864 end
2864 end
2865
2865
2866 def test_update_form_for_existing_issue_should_keep_issue_author
2866 def test_update_form_for_existing_issue_should_keep_issue_author
2867 @request.session[:user_id] = 3
2867 @request.session[:user_id] = 3
2868 xhr :patch, :edit, :id => 1, :issue => {:subject => 'Changed'}
2868 xhr :patch, :edit, :id => 1, :issue => {:subject => 'Changed'}
2869 assert_response :success
2869 assert_response :success
2870 assert_equal 'text/javascript', response.content_type
2870 assert_equal 'text/javascript', response.content_type
2871
2871
2872 issue = assigns(:issue)
2872 issue = assigns(:issue)
2873 assert_equal User.find(2), issue.author
2873 assert_equal User.find(2), issue.author
2874 assert_equal 2, issue.author_id
2874 assert_equal 2, issue.author_id
2875 assert_not_equal User.current, issue.author
2875 assert_not_equal User.current, issue.author
2876 end
2876 end
2877
2877
2878 def test_update_form_for_existing_issue_should_propose_transitions_based_on_initial_status
2878 def test_update_form_for_existing_issue_should_propose_transitions_based_on_initial_status
2879 @request.session[:user_id] = 2
2879 @request.session[:user_id] = 2
2880 WorkflowTransition.delete_all
2880 WorkflowTransition.delete_all
2881 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 1)
2881 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 1)
2882 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 5)
2882 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 5)
2883 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 5, :new_status_id => 4)
2883 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 5, :new_status_id => 4)
2884
2884
2885 xhr :patch, :edit, :id => 2,
2885 xhr :patch, :edit, :id => 2,
2886 :issue => {:tracker_id => 2,
2886 :issue => {:tracker_id => 2,
2887 :status_id => 5,
2887 :status_id => 5,
2888 :subject => 'This is an issue'}
2888 :subject => 'This is an issue'}
2889
2889
2890 assert_equal 5, assigns(:issue).status_id
2890 assert_equal 5, assigns(:issue).status_id
2891 assert_equal [1,2,5], assigns(:allowed_statuses).map(&:id).sort
2891 assert_equal [1,2,5], assigns(:allowed_statuses).map(&:id).sort
2892 end
2892 end
2893
2893
2894 def test_update_form_for_existing_issue_with_project_change
2894 def test_update_form_for_existing_issue_with_project_change
2895 @request.session[:user_id] = 2
2895 @request.session[:user_id] = 2
2896 xhr :patch, :edit, :id => 1,
2896 xhr :patch, :edit, :id => 1,
2897 :issue => {:project_id => 2,
2897 :issue => {:project_id => 2,
2898 :tracker_id => 2,
2898 :tracker_id => 2,
2899 :subject => 'This is the test_new issue',
2899 :subject => 'This is the test_new issue',
2900 :description => 'This is the description',
2900 :description => 'This is the description',
2901 :priority_id => 5}
2901 :priority_id => 5}
2902 assert_response :success
2902 assert_response :success
2903 assert_template :partial => '_form'
2903 assert_template :partial => '_form'
2904
2904
2905 issue = assigns(:issue)
2905 issue = assigns(:issue)
2906 assert_kind_of Issue, issue
2906 assert_kind_of Issue, issue
2907 assert_equal 1, issue.id
2907 assert_equal 1, issue.id
2908 assert_equal 2, issue.project_id
2908 assert_equal 2, issue.project_id
2909 assert_equal 2, issue.tracker_id
2909 assert_equal 2, issue.tracker_id
2910 assert_equal 'This is the test_new issue', issue.subject
2910 assert_equal 'This is the test_new issue', issue.subject
2911 end
2911 end
2912
2912
2913 def test_update_form_should_propose_default_status_for_existing_issue
2913 def test_update_form_should_propose_default_status_for_existing_issue
2914 @request.session[:user_id] = 2
2914 @request.session[:user_id] = 2
2915 WorkflowTransition.delete_all
2915 WorkflowTransition.delete_all
2916 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 3)
2916 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 3)
2917
2917
2918 xhr :patch, :edit, :id => 2
2918 xhr :patch, :edit, :id => 2
2919 assert_response :success
2919 assert_response :success
2920 assert_equal [2,3], assigns(:allowed_statuses).map(&:id).sort
2920 assert_equal [2,3], assigns(:allowed_statuses).map(&:id).sort
2921 end
2921 end
2922
2922
2923 def test_put_update_without_custom_fields_param
2923 def test_put_update_without_custom_fields_param
2924 @request.session[:user_id] = 2
2924 @request.session[:user_id] = 2
2925
2925
2926 issue = Issue.find(1)
2926 issue = Issue.find(1)
2927 assert_equal '125', issue.custom_value_for(2).value
2927 assert_equal '125', issue.custom_value_for(2).value
2928
2928
2929 assert_difference('Journal.count') do
2929 assert_difference('Journal.count') do
2930 assert_difference('JournalDetail.count') do
2930 assert_difference('JournalDetail.count') do
2931 put :update, :id => 1, :issue => {:subject => 'New subject'}
2931 put :update, :id => 1, :issue => {:subject => 'New subject'}
2932 end
2932 end
2933 end
2933 end
2934 assert_redirected_to :action => 'show', :id => '1'
2934 assert_redirected_to :action => 'show', :id => '1'
2935 issue.reload
2935 issue.reload
2936 assert_equal 'New subject', issue.subject
2936 assert_equal 'New subject', issue.subject
2937 # Make sure custom fields were not cleared
2937 # Make sure custom fields were not cleared
2938 assert_equal '125', issue.custom_value_for(2).value
2938 assert_equal '125', issue.custom_value_for(2).value
2939 end
2939 end
2940
2940
2941 def test_put_update_with_project_change
2941 def test_put_update_with_project_change
2942 @request.session[:user_id] = 2
2942 @request.session[:user_id] = 2
2943 ActionMailer::Base.deliveries.clear
2943 ActionMailer::Base.deliveries.clear
2944
2944
2945 with_settings :notified_events => %w(issue_updated) do
2945 with_settings :notified_events => %w(issue_updated) do
2946 assert_difference('Journal.count') do
2946 assert_difference('Journal.count') do
2947 assert_difference('JournalDetail.count', 3) do
2947 assert_difference('JournalDetail.count', 3) do
2948 put :update, :id => 1, :issue => {:project_id => '2',
2948 put :update, :id => 1, :issue => {:project_id => '2',
2949 :tracker_id => '1', # no change
2949 :tracker_id => '1', # no change
2950 :priority_id => '6',
2950 :priority_id => '6',
2951 :category_id => '3'
2951 :category_id => '3'
2952 }
2952 }
2953 end
2953 end
2954 end
2954 end
2955 end
2955 end
2956 assert_redirected_to :action => 'show', :id => '1'
2956 assert_redirected_to :action => 'show', :id => '1'
2957 issue = Issue.find(1)
2957 issue = Issue.find(1)
2958 assert_equal 2, issue.project_id
2958 assert_equal 2, issue.project_id
2959 assert_equal 1, issue.tracker_id
2959 assert_equal 1, issue.tracker_id
2960 assert_equal 6, issue.priority_id
2960 assert_equal 6, issue.priority_id
2961 assert_equal 3, issue.category_id
2961 assert_equal 3, issue.category_id
2962
2962
2963 mail = ActionMailer::Base.deliveries.last
2963 mail = ActionMailer::Base.deliveries.last
2964 assert_not_nil mail
2964 assert_not_nil mail
2965 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
2965 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
2966 assert_mail_body_match "Project changed from eCookbook to OnlineStore", mail
2966 assert_mail_body_match "Project changed from eCookbook to OnlineStore", mail
2967 end
2967 end
2968
2968
2969 def test_put_update_with_tracker_change
2969 def test_put_update_with_tracker_change
2970 @request.session[:user_id] = 2
2970 @request.session[:user_id] = 2
2971 ActionMailer::Base.deliveries.clear
2971 ActionMailer::Base.deliveries.clear
2972
2972
2973 with_settings :notified_events => %w(issue_updated) do
2973 with_settings :notified_events => %w(issue_updated) do
2974 assert_difference('Journal.count') do
2974 assert_difference('Journal.count') do
2975 assert_difference('JournalDetail.count', 2) do
2975 assert_difference('JournalDetail.count', 2) do
2976 put :update, :id => 1, :issue => {:project_id => '1',
2976 put :update, :id => 1, :issue => {:project_id => '1',
2977 :tracker_id => '2',
2977 :tracker_id => '2',
2978 :priority_id => '6'
2978 :priority_id => '6'
2979 }
2979 }
2980 end
2980 end
2981 end
2981 end
2982 end
2982 end
2983 assert_redirected_to :action => 'show', :id => '1'
2983 assert_redirected_to :action => 'show', :id => '1'
2984 issue = Issue.find(1)
2984 issue = Issue.find(1)
2985 assert_equal 1, issue.project_id
2985 assert_equal 1, issue.project_id
2986 assert_equal 2, issue.tracker_id
2986 assert_equal 2, issue.tracker_id
2987 assert_equal 6, issue.priority_id
2987 assert_equal 6, issue.priority_id
2988 assert_equal 1, issue.category_id
2988 assert_equal 1, issue.category_id
2989
2989
2990 mail = ActionMailer::Base.deliveries.last
2990 mail = ActionMailer::Base.deliveries.last
2991 assert_not_nil mail
2991 assert_not_nil mail
2992 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
2992 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
2993 assert_mail_body_match "Tracker changed from Bug to Feature request", mail
2993 assert_mail_body_match "Tracker changed from Bug to Feature request", mail
2994 end
2994 end
2995
2995
2996 def test_put_update_with_custom_field_change
2996 def test_put_update_with_custom_field_change
2997 @request.session[:user_id] = 2
2997 @request.session[:user_id] = 2
2998 issue = Issue.find(1)
2998 issue = Issue.find(1)
2999 assert_equal '125', issue.custom_value_for(2).value
2999 assert_equal '125', issue.custom_value_for(2).value
3000
3000
3001 with_settings :notified_events => %w(issue_updated) do
3001 with_settings :notified_events => %w(issue_updated) do
3002 assert_difference('Journal.count') do
3002 assert_difference('Journal.count') do
3003 assert_difference('JournalDetail.count', 3) do
3003 assert_difference('JournalDetail.count', 3) do
3004 put :update, :id => 1, :issue => {:subject => 'Custom field change',
3004 put :update, :id => 1, :issue => {:subject => 'Custom field change',
3005 :priority_id => '6',
3005 :priority_id => '6',
3006 :category_id => '1', # no change
3006 :category_id => '1', # no change
3007 :custom_field_values => { '2' => 'New custom value' }
3007 :custom_field_values => { '2' => 'New custom value' }
3008 }
3008 }
3009 end
3009 end
3010 end
3010 end
3011 end
3011 end
3012 assert_redirected_to :action => 'show', :id => '1'
3012 assert_redirected_to :action => 'show', :id => '1'
3013 issue.reload
3013 issue.reload
3014 assert_equal 'New custom value', issue.custom_value_for(2).value
3014 assert_equal 'New custom value', issue.custom_value_for(2).value
3015
3015
3016 mail = ActionMailer::Base.deliveries.last
3016 mail = ActionMailer::Base.deliveries.last
3017 assert_not_nil mail
3017 assert_not_nil mail
3018 assert_mail_body_match "Searchable field changed from 125 to New custom value", mail
3018 assert_mail_body_match "Searchable field changed from 125 to New custom value", mail
3019 end
3019 end
3020
3020
3021 def test_put_update_with_multi_custom_field_change
3021 def test_put_update_with_multi_custom_field_change
3022 field = CustomField.find(1)
3022 field = CustomField.find(1)
3023 field.update_attribute :multiple, true
3023 field.update_attribute :multiple, true
3024 issue = Issue.find(1)
3024 issue = Issue.find(1)
3025 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
3025 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
3026 issue.save!
3026 issue.save!
3027
3027
3028 @request.session[:user_id] = 2
3028 @request.session[:user_id] = 2
3029 assert_difference('Journal.count') do
3029 assert_difference('Journal.count') do
3030 assert_difference('JournalDetail.count', 3) do
3030 assert_difference('JournalDetail.count', 3) do
3031 put :update, :id => 1,
3031 put :update, :id => 1,
3032 :issue => {
3032 :issue => {
3033 :subject => 'Custom field change',
3033 :subject => 'Custom field change',
3034 :custom_field_values => { '1' => ['', 'Oracle', 'PostgreSQL'] }
3034 :custom_field_values => { '1' => ['', 'Oracle', 'PostgreSQL'] }
3035 }
3035 }
3036 end
3036 end
3037 end
3037 end
3038 assert_redirected_to :action => 'show', :id => '1'
3038 assert_redirected_to :action => 'show', :id => '1'
3039 assert_equal ['Oracle', 'PostgreSQL'], Issue.find(1).custom_field_value(1).sort
3039 assert_equal ['Oracle', 'PostgreSQL'], Issue.find(1).custom_field_value(1).sort
3040 end
3040 end
3041
3041
3042 def test_put_update_with_status_and_assignee_change
3042 def test_put_update_with_status_and_assignee_change
3043 issue = Issue.find(1)
3043 issue = Issue.find(1)
3044 assert_equal 1, issue.status_id
3044 assert_equal 1, issue.status_id
3045 @request.session[:user_id] = 2
3045 @request.session[:user_id] = 2
3046
3046
3047 with_settings :notified_events => %w(issue_updated) do
3047 with_settings :notified_events => %w(issue_updated) do
3048 assert_difference('TimeEntry.count', 0) do
3048 assert_difference('TimeEntry.count', 0) do
3049 put :update,
3049 put :update,
3050 :id => 1,
3050 :id => 1,
3051 :issue => { :status_id => 2, :assigned_to_id => 3, :notes => 'Assigned to dlopper' },
3051 :issue => { :status_id => 2, :assigned_to_id => 3, :notes => 'Assigned to dlopper' },
3052 :time_entry => { :hours => '', :comments => '', :activity_id => TimeEntryActivity.first }
3052 :time_entry => { :hours => '', :comments => '', :activity_id => TimeEntryActivity.first }
3053 end
3053 end
3054 end
3054 end
3055 assert_redirected_to :action => 'show', :id => '1'
3055 assert_redirected_to :action => 'show', :id => '1'
3056 issue.reload
3056 issue.reload
3057 assert_equal 2, issue.status_id
3057 assert_equal 2, issue.status_id
3058 j = Journal.order('id DESC').first
3058 j = Journal.order('id DESC').first
3059 assert_equal 'Assigned to dlopper', j.notes
3059 assert_equal 'Assigned to dlopper', j.notes
3060 assert_equal 2, j.details.size
3060 assert_equal 2, j.details.size
3061
3061
3062 mail = ActionMailer::Base.deliveries.last
3062 mail = ActionMailer::Base.deliveries.last
3063 assert_mail_body_match "Status changed from New to Assigned", mail
3063 assert_mail_body_match "Status changed from New to Assigned", mail
3064 # subject should contain the new status
3064 # subject should contain the new status
3065 assert mail.subject.include?("(#{ IssueStatus.find(2).name })")
3065 assert mail.subject.include?("(#{ IssueStatus.find(2).name })")
3066 end
3066 end
3067
3067
3068 def test_put_update_with_note_only
3068 def test_put_update_with_note_only
3069 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
3069 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
3070
3070
3071 with_settings :notified_events => %w(issue_updated) do
3071 with_settings :notified_events => %w(issue_updated) do
3072 # anonymous user
3072 # anonymous user
3073 put :update,
3073 put :update,
3074 :id => 1,
3074 :id => 1,
3075 :issue => { :notes => notes }
3075 :issue => { :notes => notes }
3076 end
3076 end
3077 assert_redirected_to :action => 'show', :id => '1'
3077 assert_redirected_to :action => 'show', :id => '1'
3078 j = Journal.order('id DESC').first
3078 j = Journal.order('id DESC').first
3079 assert_equal notes, j.notes
3079 assert_equal notes, j.notes
3080 assert_equal 0, j.details.size
3080 assert_equal 0, j.details.size
3081 assert_equal User.anonymous, j.user
3081 assert_equal User.anonymous, j.user
3082
3082
3083 mail = ActionMailer::Base.deliveries.last
3083 mail = ActionMailer::Base.deliveries.last
3084 assert_mail_body_match notes, mail
3084 assert_mail_body_match notes, mail
3085 end
3085 end
3086
3086
3087 def test_put_update_with_private_note_only
3087 def test_put_update_with_private_note_only
3088 notes = 'Private note'
3088 notes = 'Private note'
3089 @request.session[:user_id] = 2
3089 @request.session[:user_id] = 2
3090
3090
3091 assert_difference 'Journal.count' do
3091 assert_difference 'Journal.count' do
3092 put :update, :id => 1, :issue => {:notes => notes, :private_notes => '1'}
3092 put :update, :id => 1, :issue => {:notes => notes, :private_notes => '1'}
3093 assert_redirected_to :action => 'show', :id => '1'
3093 assert_redirected_to :action => 'show', :id => '1'
3094 end
3094 end
3095
3095
3096 j = Journal.order('id DESC').first
3096 j = Journal.order('id DESC').first
3097 assert_equal notes, j.notes
3097 assert_equal notes, j.notes
3098 assert_equal true, j.private_notes
3098 assert_equal true, j.private_notes
3099 end
3099 end
3100
3100
3101 def test_put_update_with_private_note_and_changes
3101 def test_put_update_with_private_note_and_changes
3102 notes = 'Private note'
3102 notes = 'Private note'
3103 @request.session[:user_id] = 2
3103 @request.session[:user_id] = 2
3104
3104
3105 assert_difference 'Journal.count', 2 do
3105 assert_difference 'Journal.count', 2 do
3106 put :update, :id => 1, :issue => {:subject => 'New subject', :notes => notes, :private_notes => '1'}
3106 put :update, :id => 1, :issue => {:subject => 'New subject', :notes => notes, :private_notes => '1'}
3107 assert_redirected_to :action => 'show', :id => '1'
3107 assert_redirected_to :action => 'show', :id => '1'
3108 end
3108 end
3109
3109
3110 j = Journal.order('id DESC').first
3110 j = Journal.order('id DESC').first
3111 assert_equal notes, j.notes
3111 assert_equal notes, j.notes
3112 assert_equal true, j.private_notes
3112 assert_equal true, j.private_notes
3113 assert_equal 0, j.details.count
3113 assert_equal 0, j.details.count
3114
3114
3115 j = Journal.order('id DESC').offset(1).first
3115 j = Journal.order('id DESC').offset(1).first
3116 assert_nil j.notes
3116 assert_nil j.notes
3117 assert_equal false, j.private_notes
3117 assert_equal false, j.private_notes
3118 assert_equal 1, j.details.count
3118 assert_equal 1, j.details.count
3119 end
3119 end
3120
3120
3121 def test_put_update_with_note_and_spent_time
3121 def test_put_update_with_note_and_spent_time
3122 @request.session[:user_id] = 2
3122 @request.session[:user_id] = 2
3123 spent_hours_before = Issue.find(1).spent_hours
3123 spent_hours_before = Issue.find(1).spent_hours
3124 assert_difference('TimeEntry.count') do
3124 assert_difference('TimeEntry.count') do
3125 put :update,
3125 put :update,
3126 :id => 1,
3126 :id => 1,
3127 :issue => { :notes => '2.5 hours added' },
3127 :issue => { :notes => '2.5 hours added' },
3128 :time_entry => { :hours => '2.5', :comments => 'test_put_update_with_note_and_spent_time', :activity_id => TimeEntryActivity.first.id }
3128 :time_entry => { :hours => '2.5', :comments => 'test_put_update_with_note_and_spent_time', :activity_id => TimeEntryActivity.first.id }
3129 end
3129 end
3130 assert_redirected_to :action => 'show', :id => '1'
3130 assert_redirected_to :action => 'show', :id => '1'
3131
3131
3132 issue = Issue.find(1)
3132 issue = Issue.find(1)
3133
3133
3134 j = Journal.order('id DESC').first
3134 j = Journal.order('id DESC').first
3135 assert_equal '2.5 hours added', j.notes
3135 assert_equal '2.5 hours added', j.notes
3136 assert_equal 0, j.details.size
3136 assert_equal 0, j.details.size
3137
3137
3138 t = issue.time_entries.find_by_comments('test_put_update_with_note_and_spent_time')
3138 t = issue.time_entries.find_by_comments('test_put_update_with_note_and_spent_time')
3139 assert_not_nil t
3139 assert_not_nil t
3140 assert_equal 2.5, t.hours
3140 assert_equal 2.5, t.hours
3141 assert_equal spent_hours_before + 2.5, issue.spent_hours
3141 assert_equal spent_hours_before + 2.5, issue.spent_hours
3142 end
3142 end
3143
3143
3144 def test_put_update_should_preserve_parent_issue_even_if_not_visible
3144 def test_put_update_should_preserve_parent_issue_even_if_not_visible
3145 parent = Issue.generate!(:project_id => 1, :is_private => true)
3145 parent = Issue.generate!(:project_id => 1, :is_private => true)
3146 issue = Issue.generate!(:parent_issue_id => parent.id)
3146 issue = Issue.generate!(:parent_issue_id => parent.id)
3147 assert !parent.visible?(User.find(3))
3147 assert !parent.visible?(User.find(3))
3148 @request.session[:user_id] = 3
3148 @request.session[:user_id] = 3
3149
3149
3150 get :edit, :id => issue.id
3150 get :edit, :id => issue.id
3151 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', parent.id.to_s
3151 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', parent.id.to_s
3152
3152
3153 put :update, :id => issue.id, :issue => {:subject => 'New subject', :parent_issue_id => parent.id.to_s}
3153 put :update, :id => issue.id, :issue => {:subject => 'New subject', :parent_issue_id => parent.id.to_s}
3154 assert_response 302
3154 assert_response 302
3155 assert_equal parent, issue.parent
3155 assert_equal parent, issue.parent
3156 end
3156 end
3157
3157
3158 def test_put_update_with_attachment_only
3158 def test_put_update_with_attachment_only
3159 set_tmp_attachments_directory
3159 set_tmp_attachments_directory
3160
3160
3161 # Delete all fixtured journals, a race condition can occur causing the wrong
3161 # Delete all fixtured journals, a race condition can occur causing the wrong
3162 # journal to get fetched in the next find.
3162 # journal to get fetched in the next find.
3163 Journal.delete_all
3163 Journal.delete_all
3164
3164
3165 with_settings :notified_events => %w(issue_updated) do
3165 with_settings :notified_events => %w(issue_updated) do
3166 # anonymous user
3166 # anonymous user
3167 assert_difference 'Attachment.count' do
3167 assert_difference 'Attachment.count' do
3168 put :update, :id => 1,
3168 put :update, :id => 1,
3169 :issue => {:notes => ''},
3169 :issue => {:notes => ''},
3170 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
3170 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
3171 end
3171 end
3172 end
3172 end
3173
3173
3174 assert_redirected_to :action => 'show', :id => '1'
3174 assert_redirected_to :action => 'show', :id => '1'
3175 j = Issue.find(1).journals.reorder('id DESC').first
3175 j = Issue.find(1).journals.reorder('id DESC').first
3176 assert j.notes.blank?
3176 assert j.notes.blank?
3177 assert_equal 1, j.details.size
3177 assert_equal 1, j.details.size
3178 assert_equal 'testfile.txt', j.details.first.value
3178 assert_equal 'testfile.txt', j.details.first.value
3179 assert_equal User.anonymous, j.user
3179 assert_equal User.anonymous, j.user
3180
3180
3181 attachment = Attachment.order('id DESC').first
3181 attachment = Attachment.order('id DESC').first
3182 assert_equal Issue.find(1), attachment.container
3182 assert_equal Issue.find(1), attachment.container
3183 assert_equal User.anonymous, attachment.author
3183 assert_equal User.anonymous, attachment.author
3184 assert_equal 'testfile.txt', attachment.filename
3184 assert_equal 'testfile.txt', attachment.filename
3185 assert_equal 'text/plain', attachment.content_type
3185 assert_equal 'text/plain', attachment.content_type
3186 assert_equal 'test file', attachment.description
3186 assert_equal 'test file', attachment.description
3187 assert_equal 59, attachment.filesize
3187 assert_equal 59, attachment.filesize
3188 assert File.exists?(attachment.diskfile)
3188 assert File.exists?(attachment.diskfile)
3189 assert_equal 59, File.size(attachment.diskfile)
3189 assert_equal 59, File.size(attachment.diskfile)
3190
3190
3191 mail = ActionMailer::Base.deliveries.last
3191 mail = ActionMailer::Base.deliveries.last
3192 assert_mail_body_match 'testfile.txt', mail
3192 assert_mail_body_match 'testfile.txt', mail
3193 end
3193 end
3194
3194
3195 def test_put_update_with_failure_should_save_attachments
3195 def test_put_update_with_failure_should_save_attachments
3196 set_tmp_attachments_directory
3196 set_tmp_attachments_directory
3197 @request.session[:user_id] = 2
3197 @request.session[:user_id] = 2
3198
3198
3199 assert_no_difference 'Journal.count' do
3199 assert_no_difference 'Journal.count' do
3200 assert_difference 'Attachment.count' do
3200 assert_difference 'Attachment.count' do
3201 put :update, :id => 1,
3201 put :update, :id => 1,
3202 :issue => { :subject => '' },
3202 :issue => { :subject => '' },
3203 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
3203 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
3204 assert_response :success
3204 assert_response :success
3205 assert_template 'edit'
3205 assert_template 'edit'
3206 end
3206 end
3207 end
3207 end
3208
3208
3209 attachment = Attachment.order('id DESC').first
3209 attachment = Attachment.order('id DESC').first
3210 assert_equal 'testfile.txt', attachment.filename
3210 assert_equal 'testfile.txt', attachment.filename
3211 assert File.exists?(attachment.diskfile)
3211 assert File.exists?(attachment.diskfile)
3212 assert_nil attachment.container
3212 assert_nil attachment.container
3213
3213
3214 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
3214 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
3215 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
3215 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
3216 end
3216 end
3217
3217
3218 def test_put_update_with_failure_should_keep_saved_attachments
3218 def test_put_update_with_failure_should_keep_saved_attachments
3219 set_tmp_attachments_directory
3219 set_tmp_attachments_directory
3220 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
3220 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
3221 @request.session[:user_id] = 2
3221 @request.session[:user_id] = 2
3222
3222
3223 assert_no_difference 'Journal.count' do
3223 assert_no_difference 'Journal.count' do
3224 assert_no_difference 'Attachment.count' do
3224 assert_no_difference 'Attachment.count' do
3225 put :update, :id => 1,
3225 put :update, :id => 1,
3226 :issue => { :subject => '' },
3226 :issue => { :subject => '' },
3227 :attachments => {'p0' => {'token' => attachment.token}}
3227 :attachments => {'p0' => {'token' => attachment.token}}
3228 assert_response :success
3228 assert_response :success
3229 assert_template 'edit'
3229 assert_template 'edit'
3230 end
3230 end
3231 end
3231 end
3232
3232
3233 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
3233 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
3234 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
3234 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
3235 end
3235 end
3236
3236
3237 def test_put_update_should_attach_saved_attachments
3237 def test_put_update_should_attach_saved_attachments
3238 set_tmp_attachments_directory
3238 set_tmp_attachments_directory
3239 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
3239 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
3240 @request.session[:user_id] = 2
3240 @request.session[:user_id] = 2
3241
3241
3242 assert_difference 'Journal.count' do
3242 assert_difference 'Journal.count' do
3243 assert_difference 'JournalDetail.count' do
3243 assert_difference 'JournalDetail.count' do
3244 assert_no_difference 'Attachment.count' do
3244 assert_no_difference 'Attachment.count' do
3245 put :update, :id => 1,
3245 put :update, :id => 1,
3246 :issue => {:notes => 'Attachment added'},
3246 :issue => {:notes => 'Attachment added'},
3247 :attachments => {'p0' => {'token' => attachment.token}}
3247 :attachments => {'p0' => {'token' => attachment.token}}
3248 assert_redirected_to '/issues/1'
3248 assert_redirected_to '/issues/1'
3249 end
3249 end
3250 end
3250 end
3251 end
3251 end
3252
3252
3253 attachment.reload
3253 attachment.reload
3254 assert_equal Issue.find(1), attachment.container
3254 assert_equal Issue.find(1), attachment.container
3255
3255
3256 journal = Journal.order('id DESC').first
3256 journal = Journal.order('id DESC').first
3257 assert_equal 1, journal.details.size
3257 assert_equal 1, journal.details.size
3258 assert_equal 'testfile.txt', journal.details.first.value
3258 assert_equal 'testfile.txt', journal.details.first.value
3259 end
3259 end
3260
3260
3261 def test_put_update_with_attachment_that_fails_to_save
3261 def test_put_update_with_attachment_that_fails_to_save
3262 set_tmp_attachments_directory
3262 set_tmp_attachments_directory
3263
3263
3264 # anonymous user
3264 # anonymous user
3265 with_settings :attachment_max_size => 0 do
3265 with_settings :attachment_max_size => 0 do
3266 put :update,
3266 put :update,
3267 :id => 1,
3267 :id => 1,
3268 :issue => {:notes => ''},
3268 :issue => {:notes => ''},
3269 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
3269 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
3270 assert_redirected_to :action => 'show', :id => '1'
3270 assert_redirected_to :action => 'show', :id => '1'
3271 assert_equal '1 file(s) could not be saved.', flash[:warning]
3271 assert_equal '1 file(s) could not be saved.', flash[:warning]
3272 end
3272 end
3273 end
3273 end
3274
3274
3275 def test_put_update_with_no_change
3275 def test_put_update_with_no_change
3276 issue = Issue.find(1)
3276 issue = Issue.find(1)
3277 issue.journals.clear
3277 issue.journals.clear
3278 ActionMailer::Base.deliveries.clear
3278 ActionMailer::Base.deliveries.clear
3279
3279
3280 put :update,
3280 put :update,
3281 :id => 1,
3281 :id => 1,
3282 :issue => {:notes => ''}
3282 :issue => {:notes => ''}
3283 assert_redirected_to :action => 'show', :id => '1'
3283 assert_redirected_to :action => 'show', :id => '1'
3284
3284
3285 issue.reload
3285 issue.reload
3286 assert issue.journals.empty?
3286 assert issue.journals.empty?
3287 # No email should be sent
3287 # No email should be sent
3288 assert ActionMailer::Base.deliveries.empty?
3288 assert ActionMailer::Base.deliveries.empty?
3289 end
3289 end
3290
3290
3291 def test_put_update_should_send_a_notification
3291 def test_put_update_should_send_a_notification
3292 @request.session[:user_id] = 2
3292 @request.session[:user_id] = 2
3293 ActionMailer::Base.deliveries.clear
3293 ActionMailer::Base.deliveries.clear
3294 issue = Issue.find(1)
3294 issue = Issue.find(1)
3295 old_subject = issue.subject
3295 old_subject = issue.subject
3296 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
3296 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
3297
3297
3298 with_settings :notified_events => %w(issue_updated) do
3298 with_settings :notified_events => %w(issue_updated) do
3299 put :update, :id => 1, :issue => {:subject => new_subject,
3299 put :update, :id => 1, :issue => {:subject => new_subject,
3300 :priority_id => '6',
3300 :priority_id => '6',
3301 :category_id => '1' # no change
3301 :category_id => '1' # no change
3302 }
3302 }
3303 assert_equal 1, ActionMailer::Base.deliveries.size
3303 assert_equal 1, ActionMailer::Base.deliveries.size
3304 end
3304 end
3305 end
3305 end
3306
3306
3307 def test_put_update_with_invalid_spent_time_hours_only
3307 def test_put_update_with_invalid_spent_time_hours_only
3308 @request.session[:user_id] = 2
3308 @request.session[:user_id] = 2
3309 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
3309 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
3310
3310
3311 assert_no_difference('Journal.count') do
3311 assert_no_difference('Journal.count') do
3312 put :update,
3312 put :update,
3313 :id => 1,
3313 :id => 1,
3314 :issue => {:notes => notes},
3314 :issue => {:notes => notes},
3315 :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
3315 :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
3316 end
3316 end
3317 assert_response :success
3317 assert_response :success
3318 assert_template 'edit'
3318 assert_template 'edit'
3319
3319
3320 assert_select_error /Activity cannot be blank/
3320 assert_select_error /Activity cannot be blank/
3321 assert_select 'textarea[name=?]', 'issue[notes]', :text => notes
3321 assert_select 'textarea[name=?]', 'issue[notes]', :text => notes
3322 assert_select 'input[name=?][value=?]', 'time_entry[hours]', '2z'
3322 assert_select 'input[name=?][value=?]', 'time_entry[hours]', '2z'
3323 end
3323 end
3324
3324
3325 def test_put_update_with_invalid_spent_time_comments_only
3325 def test_put_update_with_invalid_spent_time_comments_only
3326 @request.session[:user_id] = 2
3326 @request.session[:user_id] = 2
3327 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
3327 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
3328
3328
3329 assert_no_difference('Journal.count') do
3329 assert_no_difference('Journal.count') do
3330 put :update,
3330 put :update,
3331 :id => 1,
3331 :id => 1,
3332 :issue => {:notes => notes},
3332 :issue => {:notes => notes},
3333 :time_entry => {"comments"=>"this is my comment", "activity_id"=>"", "hours"=>""}
3333 :time_entry => {"comments"=>"this is my comment", "activity_id"=>"", "hours"=>""}
3334 end
3334 end
3335 assert_response :success
3335 assert_response :success
3336 assert_template 'edit'
3336 assert_template 'edit'
3337
3337
3338 assert_select_error /Activity cannot be blank/
3338 assert_select_error /Activity cannot be blank/
3339 assert_select_error /Hours cannot be blank/
3339 assert_select_error /Hours cannot be blank/
3340 assert_select 'textarea[name=?]', 'issue[notes]', :text => notes
3340 assert_select 'textarea[name=?]', 'issue[notes]', :text => notes
3341 assert_select 'input[name=?][value=?]', 'time_entry[comments]', 'this is my comment'
3341 assert_select 'input[name=?][value=?]', 'time_entry[comments]', 'this is my comment'
3342 end
3342 end
3343
3343
3344 def test_put_update_should_allow_fixed_version_to_be_set_to_a_subproject
3344 def test_put_update_should_allow_fixed_version_to_be_set_to_a_subproject
3345 issue = Issue.find(2)
3345 issue = Issue.find(2)
3346 @request.session[:user_id] = 2
3346 @request.session[:user_id] = 2
3347
3347
3348 put :update,
3348 put :update,
3349 :id => issue.id,
3349 :id => issue.id,
3350 :issue => {
3350 :issue => {
3351 :fixed_version_id => 4
3351 :fixed_version_id => 4
3352 }
3352 }
3353
3353
3354 assert_response :redirect
3354 assert_response :redirect
3355 issue.reload
3355 issue.reload
3356 assert_equal 4, issue.fixed_version_id
3356 assert_equal 4, issue.fixed_version_id
3357 assert_not_equal issue.project_id, issue.fixed_version.project_id
3357 assert_not_equal issue.project_id, issue.fixed_version.project_id
3358 end
3358 end
3359
3359
3360 def test_put_update_should_redirect_back_using_the_back_url_parameter
3360 def test_put_update_should_redirect_back_using_the_back_url_parameter
3361 issue = Issue.find(2)
3361 issue = Issue.find(2)
3362 @request.session[:user_id] = 2
3362 @request.session[:user_id] = 2
3363
3363
3364 put :update,
3364 put :update,
3365 :id => issue.id,
3365 :id => issue.id,
3366 :issue => {
3366 :issue => {
3367 :fixed_version_id => 4
3367 :fixed_version_id => 4
3368 },
3368 },
3369 :back_url => '/issues'
3369 :back_url => '/issues'
3370
3370
3371 assert_response :redirect
3371 assert_response :redirect
3372 assert_redirected_to '/issues'
3372 assert_redirected_to '/issues'
3373 end
3373 end
3374
3374
3375 def test_put_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
3375 def test_put_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
3376 issue = Issue.find(2)
3376 issue = Issue.find(2)
3377 @request.session[:user_id] = 2
3377 @request.session[:user_id] = 2
3378
3378
3379 put :update,
3379 put :update,
3380 :id => issue.id,
3380 :id => issue.id,
3381 :issue => {
3381 :issue => {
3382 :fixed_version_id => 4
3382 :fixed_version_id => 4
3383 },
3383 },
3384 :back_url => 'http://google.com'
3384 :back_url => 'http://google.com'
3385
3385
3386 assert_response :redirect
3386 assert_response :redirect
3387 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue.id
3387 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue.id
3388 end
3388 end
3389
3389
3390 def test_get_bulk_edit
3390 def test_get_bulk_edit
3391 @request.session[:user_id] = 2
3391 @request.session[:user_id] = 2
3392 get :bulk_edit, :ids => [1, 2]
3392 get :bulk_edit, :ids => [1, 2]
3393 assert_response :success
3393 assert_response :success
3394 assert_template 'bulk_edit'
3394 assert_template 'bulk_edit'
3395
3395
3396 assert_select 'ul#bulk-selection' do
3396 assert_select 'ul#bulk-selection' do
3397 assert_select 'li', 2
3397 assert_select 'li', 2
3398 assert_select 'li a', :text => 'Bug #1'
3398 assert_select 'li a', :text => 'Bug #1'
3399 end
3399 end
3400
3400
3401 assert_select 'form#bulk_edit_form[action=?]', '/issues/bulk_update' do
3401 assert_select 'form#bulk_edit_form[action=?]', '/issues/bulk_update' do
3402 assert_select 'input[name=?]', 'ids[]', 2
3402 assert_select 'input[name=?]', 'ids[]', 2
3403 assert_select 'input[name=?][value="1"][type=hidden]', 'ids[]'
3403 assert_select 'input[name=?][value="1"][type=hidden]', 'ids[]'
3404
3404
3405 assert_select 'select[name=?]', 'issue[project_id]'
3405 assert_select 'select[name=?]', 'issue[project_id]'
3406 assert_select 'input[name=?]', 'issue[parent_issue_id]'
3406 assert_select 'input[name=?]', 'issue[parent_issue_id]'
3407
3407
3408 # Project specific custom field, date type
3408 # Project specific custom field, date type
3409 field = CustomField.find(9)
3409 field = CustomField.find(9)
3410 assert !field.is_for_all?
3410 assert !field.is_for_all?
3411 assert_equal 'date', field.field_format
3411 assert_equal 'date', field.field_format
3412 assert_select 'input[name=?]', 'issue[custom_field_values][9]'
3412 assert_select 'input[name=?]', 'issue[custom_field_values][9]'
3413
3413
3414 # System wide custom field
3414 # System wide custom field
3415 assert CustomField.find(1).is_for_all?
3415 assert CustomField.find(1).is_for_all?
3416 assert_select 'select[name=?]', 'issue[custom_field_values][1]'
3416 assert_select 'select[name=?]', 'issue[custom_field_values][1]'
3417
3417
3418 # Be sure we don't display inactive IssuePriorities
3418 # Be sure we don't display inactive IssuePriorities
3419 assert ! IssuePriority.find(15).active?
3419 assert ! IssuePriority.find(15).active?
3420 assert_select 'select[name=?]', 'issue[priority_id]' do
3420 assert_select 'select[name=?]', 'issue[priority_id]' do
3421 assert_select 'option[value="15"]', 0
3421 assert_select 'option[value="15"]', 0
3422 end
3422 end
3423 end
3423 end
3424 end
3424 end
3425
3425
3426 def test_get_bulk_edit_on_different_projects
3426 def test_get_bulk_edit_on_different_projects
3427 @request.session[:user_id] = 2
3427 @request.session[:user_id] = 2
3428 get :bulk_edit, :ids => [1, 2, 6]
3428 get :bulk_edit, :ids => [1, 2, 6]
3429 assert_response :success
3429 assert_response :success
3430 assert_template 'bulk_edit'
3430 assert_template 'bulk_edit'
3431
3431
3432 # Can not set issues from different projects as children of an issue
3432 # Can not set issues from different projects as children of an issue
3433 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
3433 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
3434
3434
3435 # Project specific custom field, date type
3435 # Project specific custom field, date type
3436 field = CustomField.find(9)
3436 field = CustomField.find(9)
3437 assert !field.is_for_all?
3437 assert !field.is_for_all?
3438 assert !field.project_ids.include?(Issue.find(6).project_id)
3438 assert !field.project_ids.include?(Issue.find(6).project_id)
3439 assert_select 'input[name=?]', 'issue[custom_field_values][9]', 0
3439 assert_select 'input[name=?]', 'issue[custom_field_values][9]', 0
3440 end
3440 end
3441
3441
3442 def test_get_bulk_edit_with_user_custom_field
3442 def test_get_bulk_edit_with_user_custom_field
3443 field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user', :is_for_all => true)
3443 field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user', :is_for_all => true)
3444
3444
3445 @request.session[:user_id] = 2
3445 @request.session[:user_id] = 2
3446 get :bulk_edit, :ids => [1, 2]
3446 get :bulk_edit, :ids => [1, 2]
3447 assert_response :success
3447 assert_response :success
3448 assert_template 'bulk_edit'
3448 assert_template 'bulk_edit'
3449
3449
3450 assert_select 'select.user_cf[name=?]', "issue[custom_field_values][#{field.id}]" do
3450 assert_select 'select.user_cf[name=?]', "issue[custom_field_values][#{field.id}]" do
3451 assert_select 'option', Project.find(1).users.count + 2 # "no change" + "none" options
3451 assert_select 'option', Project.find(1).users.count + 2 # "no change" + "none" options
3452 end
3452 end
3453 end
3453 end
3454
3454
3455 def test_get_bulk_edit_with_version_custom_field
3455 def test_get_bulk_edit_with_version_custom_field
3456 field = IssueCustomField.create!(:name => 'Affected version', :field_format => 'version', :is_for_all => true)
3456 field = IssueCustomField.create!(:name => 'Affected version', :field_format => 'version', :is_for_all => true)
3457
3457
3458 @request.session[:user_id] = 2
3458 @request.session[:user_id] = 2
3459 get :bulk_edit, :ids => [1, 2]
3459 get :bulk_edit, :ids => [1, 2]
3460 assert_response :success
3460 assert_response :success
3461 assert_template 'bulk_edit'
3461 assert_template 'bulk_edit'
3462
3462
3463 assert_select 'select.version_cf[name=?]', "issue[custom_field_values][#{field.id}]" do
3463 assert_select 'select.version_cf[name=?]', "issue[custom_field_values][#{field.id}]" do
3464 assert_select 'option', Project.find(1).shared_versions.count + 2 # "no change" + "none" options
3464 assert_select 'option', Project.find(1).shared_versions.count + 2 # "no change" + "none" options
3465 end
3465 end
3466 end
3466 end
3467
3467
3468 def test_get_bulk_edit_with_multi_custom_field
3468 def test_get_bulk_edit_with_multi_custom_field
3469 field = CustomField.find(1)
3469 field = CustomField.find(1)
3470 field.update_attribute :multiple, true
3470 field.update_attribute :multiple, true
3471
3471
3472 @request.session[:user_id] = 2
3472 @request.session[:user_id] = 2
3473 get :bulk_edit, :ids => [1, 2]
3473 get :bulk_edit, :ids => [1, 2]
3474 assert_response :success
3474 assert_response :success
3475 assert_template 'bulk_edit'
3475 assert_template 'bulk_edit'
3476
3476
3477 assert_select 'select[name=?]', 'issue[custom_field_values][1][]' do
3477 assert_select 'select[name=?]', 'issue[custom_field_values][1][]' do
3478 assert_select 'option', field.possible_values.size + 1 # "none" options
3478 assert_select 'option', field.possible_values.size + 1 # "none" options
3479 end
3479 end
3480 end
3480 end
3481
3481
3482 def test_bulk_edit_should_propose_to_clear_text_custom_fields
3482 def test_bulk_edit_should_propose_to_clear_text_custom_fields
3483 @request.session[:user_id] = 2
3483 @request.session[:user_id] = 2
3484 get :bulk_edit, :ids => [1, 3]
3484 get :bulk_edit, :ids => [1, 3]
3485 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', '__none__'
3485 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', '__none__'
3486 end
3486 end
3487
3487
3488 def test_bulk_edit_should_only_propose_statuses_allowed_for_all_issues
3488 def test_bulk_edit_should_only_propose_statuses_allowed_for_all_issues
3489 WorkflowTransition.delete_all
3489 WorkflowTransition.delete_all
3490 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
3490 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
3491 :old_status_id => 1, :new_status_id => 1)
3491 :old_status_id => 1, :new_status_id => 1)
3492 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
3492 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
3493 :old_status_id => 1, :new_status_id => 3)
3493 :old_status_id => 1, :new_status_id => 3)
3494 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
3494 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
3495 :old_status_id => 1, :new_status_id => 4)
3495 :old_status_id => 1, :new_status_id => 4)
3496 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2,
3496 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2,
3497 :old_status_id => 2, :new_status_id => 1)
3497 :old_status_id => 2, :new_status_id => 1)
3498 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2,
3498 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2,
3499 :old_status_id => 2, :new_status_id => 3)
3499 :old_status_id => 2, :new_status_id => 3)
3500 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2,
3500 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2,
3501 :old_status_id => 2, :new_status_id => 5)
3501 :old_status_id => 2, :new_status_id => 5)
3502 @request.session[:user_id] = 2
3502 @request.session[:user_id] = 2
3503 get :bulk_edit, :ids => [1, 2]
3503 get :bulk_edit, :ids => [1, 2]
3504
3504
3505 assert_response :success
3505 assert_response :success
3506 statuses = assigns(:available_statuses)
3506 statuses = assigns(:available_statuses)
3507 assert_not_nil statuses
3507 assert_not_nil statuses
3508 assert_equal [1, 3], statuses.map(&:id).sort
3508 assert_equal [1, 3], statuses.map(&:id).sort
3509
3509
3510 assert_select 'select[name=?]', 'issue[status_id]' do
3510 assert_select 'select[name=?]', 'issue[status_id]' do
3511 assert_select 'option', 3 # 2 statuses + "no change" option
3511 assert_select 'option', 3 # 2 statuses + "no change" option
3512 end
3512 end
3513 end
3513 end
3514
3514
3515 def test_bulk_edit_should_propose_target_project_open_shared_versions
3515 def test_bulk_edit_should_propose_target_project_open_shared_versions
3516 @request.session[:user_id] = 2
3516 @request.session[:user_id] = 2
3517 post :bulk_edit, :ids => [1, 2, 6], :issue => {:project_id => 1}
3517 post :bulk_edit, :ids => [1, 2, 6], :issue => {:project_id => 1}
3518 assert_response :success
3518 assert_response :success
3519 assert_template 'bulk_edit'
3519 assert_template 'bulk_edit'
3520 assert_equal Project.find(1).shared_versions.open.to_a.sort, assigns(:versions).sort
3520 assert_equal Project.find(1).shared_versions.open.to_a.sort, assigns(:versions).sort
3521
3521
3522 assert_select 'select[name=?]', 'issue[fixed_version_id]' do
3522 assert_select 'select[name=?]', 'issue[fixed_version_id]' do
3523 assert_select 'option', :text => '2.0'
3523 assert_select 'option', :text => '2.0'
3524 end
3524 end
3525 end
3525 end
3526
3526
3527 def test_bulk_edit_should_propose_target_project_categories
3527 def test_bulk_edit_should_propose_target_project_categories
3528 @request.session[:user_id] = 2
3528 @request.session[:user_id] = 2
3529 post :bulk_edit, :ids => [1, 2, 6], :issue => {:project_id => 1}
3529 post :bulk_edit, :ids => [1, 2, 6], :issue => {:project_id => 1}
3530 assert_response :success
3530 assert_response :success
3531 assert_template 'bulk_edit'
3531 assert_template 'bulk_edit'
3532 assert_equal Project.find(1).issue_categories.sort, assigns(:categories).sort
3532 assert_equal Project.find(1).issue_categories.sort, assigns(:categories).sort
3533
3533
3534 assert_select 'select[name=?]', 'issue[category_id]' do
3534 assert_select 'select[name=?]', 'issue[category_id]' do
3535 assert_select 'option', :text => 'Recipes'
3535 assert_select 'option', :text => 'Recipes'
3536 end
3536 end
3537 end
3537 end
3538
3538
3539 def test_bulk_update
3539 def test_bulk_update
3540 @request.session[:user_id] = 2
3540 @request.session[:user_id] = 2
3541 # update issues priority
3541 # update issues priority
3542 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
3542 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
3543 :issue => {:priority_id => 7,
3543 :issue => {:priority_id => 7,
3544 :assigned_to_id => '',
3544 :assigned_to_id => '',
3545 :custom_field_values => {'2' => ''}}
3545 :custom_field_values => {'2' => ''}}
3546
3546
3547 assert_response 302
3547 assert_response 302
3548 # check that the issues were updated
3548 # check that the issues were updated
3549 assert_equal [7, 7], Issue.where(:id =>[1, 2]).collect {|i| i.priority.id}
3549 assert_equal [7, 7], Issue.where(:id =>[1, 2]).collect {|i| i.priority.id}
3550
3550
3551 issue = Issue.find(1)
3551 issue = Issue.find(1)
3552 journal = issue.journals.reorder('created_on DESC').first
3552 journal = issue.journals.reorder('created_on DESC').first
3553 assert_equal '125', issue.custom_value_for(2).value
3553 assert_equal '125', issue.custom_value_for(2).value
3554 assert_equal 'Bulk editing', journal.notes
3554 assert_equal 'Bulk editing', journal.notes
3555 assert_equal 1, journal.details.size
3555 assert_equal 1, journal.details.size
3556 end
3556 end
3557
3557
3558 def test_bulk_update_with_group_assignee
3558 def test_bulk_update_with_group_assignee
3559 group = Group.find(11)
3559 group = Group.find(11)
3560 project = Project.find(1)
3560 project = Project.find(1)
3561 project.members << Member.new(:principal => group, :roles => [Role.givable.first])
3561 project.members << Member.new(:principal => group, :roles => [Role.givable.first])
3562
3562
3563 @request.session[:user_id] = 2
3563 @request.session[:user_id] = 2
3564 # update issues assignee
3564 # update issues assignee
3565 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
3565 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
3566 :issue => {:priority_id => '',
3566 :issue => {:priority_id => '',
3567 :assigned_to_id => group.id,
3567 :assigned_to_id => group.id,
3568 :custom_field_values => {'2' => ''}}
3568 :custom_field_values => {'2' => ''}}
3569
3569
3570 assert_response 302
3570 assert_response 302
3571 assert_equal [group, group], Issue.where(:id => [1, 2]).collect {|i| i.assigned_to}
3571 assert_equal [group, group], Issue.where(:id => [1, 2]).collect {|i| i.assigned_to}
3572 end
3572 end
3573
3573
3574 def test_bulk_update_on_different_projects
3574 def test_bulk_update_on_different_projects
3575 @request.session[:user_id] = 2
3575 @request.session[:user_id] = 2
3576 # update issues priority
3576 # update issues priority
3577 post :bulk_update, :ids => [1, 2, 6], :notes => 'Bulk editing',
3577 post :bulk_update, :ids => [1, 2, 6], :notes => 'Bulk editing',
3578 :issue => {:priority_id => 7,
3578 :issue => {:priority_id => 7,
3579 :assigned_to_id => '',
3579 :assigned_to_id => '',
3580 :custom_field_values => {'2' => ''}}
3580 :custom_field_values => {'2' => ''}}
3581
3581
3582 assert_response 302
3582 assert_response 302
3583 # check that the issues were updated
3583 # check that the issues were updated
3584 assert_equal [7, 7, 7], Issue.find([1,2,6]).map(&:priority_id)
3584 assert_equal [7, 7, 7], Issue.find([1,2,6]).map(&:priority_id)
3585
3585
3586 issue = Issue.find(1)
3586 issue = Issue.find(1)
3587 journal = issue.journals.reorder('created_on DESC').first
3587 journal = issue.journals.reorder('created_on DESC').first
3588 assert_equal '125', issue.custom_value_for(2).value
3588 assert_equal '125', issue.custom_value_for(2).value
3589 assert_equal 'Bulk editing', journal.notes
3589 assert_equal 'Bulk editing', journal.notes
3590 assert_equal 1, journal.details.size
3590 assert_equal 1, journal.details.size
3591 end
3591 end
3592
3592
3593 def test_bulk_update_on_different_projects_without_rights
3593 def test_bulk_update_on_different_projects_without_rights
3594 @request.session[:user_id] = 3
3594 @request.session[:user_id] = 3
3595 user = User.find(3)
3595 user = User.find(3)
3596 action = { :controller => "issues", :action => "bulk_update" }
3596 action = { :controller => "issues", :action => "bulk_update" }
3597 assert user.allowed_to?(action, Issue.find(1).project)
3597 assert user.allowed_to?(action, Issue.find(1).project)
3598 assert ! user.allowed_to?(action, Issue.find(6).project)
3598 assert ! user.allowed_to?(action, Issue.find(6).project)
3599 post :bulk_update, :ids => [1, 6], :notes => 'Bulk should fail',
3599 post :bulk_update, :ids => [1, 6], :notes => 'Bulk should fail',
3600 :issue => {:priority_id => 7,
3600 :issue => {:priority_id => 7,
3601 :assigned_to_id => '',
3601 :assigned_to_id => '',
3602 :custom_field_values => {'2' => ''}}
3602 :custom_field_values => {'2' => ''}}
3603 assert_response 403
3603 assert_response 403
3604 assert_not_equal "Bulk should fail", Journal.last.notes
3604 assert_not_equal "Bulk should fail", Journal.last.notes
3605 end
3605 end
3606
3606
3607 def test_bullk_update_should_send_a_notification
3607 def test_bullk_update_should_send_a_notification
3608 @request.session[:user_id] = 2
3608 @request.session[:user_id] = 2
3609 ActionMailer::Base.deliveries.clear
3609 ActionMailer::Base.deliveries.clear
3610 with_settings :notified_events => %w(issue_updated) do
3610 with_settings :notified_events => %w(issue_updated) do
3611 post(:bulk_update,
3611 post(:bulk_update,
3612 {
3612 {
3613 :ids => [1, 2],
3613 :ids => [1, 2],
3614 :notes => 'Bulk editing',
3614 :notes => 'Bulk editing',
3615 :issue => {
3615 :issue => {
3616 :priority_id => 7,
3616 :priority_id => 7,
3617 :assigned_to_id => '',
3617 :assigned_to_id => '',
3618 :custom_field_values => {'2' => ''}
3618 :custom_field_values => {'2' => ''}
3619 }
3619 }
3620 })
3620 })
3621 assert_response 302
3621 assert_response 302
3622 assert_equal 2, ActionMailer::Base.deliveries.size
3622 assert_equal 2, ActionMailer::Base.deliveries.size
3623 end
3623 end
3624 end
3624 end
3625
3625
3626 def test_bulk_update_project
3626 def test_bulk_update_project
3627 @request.session[:user_id] = 2
3627 @request.session[:user_id] = 2
3628 post :bulk_update, :ids => [1, 2], :issue => {:project_id => '2'}
3628 post :bulk_update, :ids => [1, 2], :issue => {:project_id => '2'}
3629 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3629 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3630 # Issues moved to project 2
3630 # Issues moved to project 2
3631 assert_equal 2, Issue.find(1).project_id
3631 assert_equal 2, Issue.find(1).project_id
3632 assert_equal 2, Issue.find(2).project_id
3632 assert_equal 2, Issue.find(2).project_id
3633 # No tracker change
3633 # No tracker change
3634 assert_equal 1, Issue.find(1).tracker_id
3634 assert_equal 1, Issue.find(1).tracker_id
3635 assert_equal 2, Issue.find(2).tracker_id
3635 assert_equal 2, Issue.find(2).tracker_id
3636 end
3636 end
3637
3637
3638 def test_bulk_update_project_on_single_issue_should_follow_when_needed
3638 def test_bulk_update_project_on_single_issue_should_follow_when_needed
3639 @request.session[:user_id] = 2
3639 @request.session[:user_id] = 2
3640 post :bulk_update, :id => 1, :issue => {:project_id => '2'}, :follow => '1'
3640 post :bulk_update, :id => 1, :issue => {:project_id => '2'}, :follow => '1'
3641 assert_redirected_to '/issues/1'
3641 assert_redirected_to '/issues/1'
3642 end
3642 end
3643
3643
3644 def test_bulk_update_project_on_multiple_issues_should_follow_when_needed
3644 def test_bulk_update_project_on_multiple_issues_should_follow_when_needed
3645 @request.session[:user_id] = 2
3645 @request.session[:user_id] = 2
3646 post :bulk_update, :id => [1, 2], :issue => {:project_id => '2'}, :follow => '1'
3646 post :bulk_update, :id => [1, 2], :issue => {:project_id => '2'}, :follow => '1'
3647 assert_redirected_to '/projects/onlinestore/issues'
3647 assert_redirected_to '/projects/onlinestore/issues'
3648 end
3648 end
3649
3649
3650 def test_bulk_update_tracker
3650 def test_bulk_update_tracker
3651 @request.session[:user_id] = 2
3651 @request.session[:user_id] = 2
3652 post :bulk_update, :ids => [1, 2], :issue => {:tracker_id => '2'}
3652 post :bulk_update, :ids => [1, 2], :issue => {:tracker_id => '2'}
3653 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3653 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3654 assert_equal 2, Issue.find(1).tracker_id
3654 assert_equal 2, Issue.find(1).tracker_id
3655 assert_equal 2, Issue.find(2).tracker_id
3655 assert_equal 2, Issue.find(2).tracker_id
3656 end
3656 end
3657
3657
3658 def test_bulk_update_status
3658 def test_bulk_update_status
3659 @request.session[:user_id] = 2
3659 @request.session[:user_id] = 2
3660 # update issues priority
3660 # update issues priority
3661 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing status',
3661 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing status',
3662 :issue => {:priority_id => '',
3662 :issue => {:priority_id => '',
3663 :assigned_to_id => '',
3663 :assigned_to_id => '',
3664 :status_id => '5'}
3664 :status_id => '5'}
3665
3665
3666 assert_response 302
3666 assert_response 302
3667 issue = Issue.find(1)
3667 issue = Issue.find(1)
3668 assert issue.closed?
3668 assert issue.closed?
3669 end
3669 end
3670
3670
3671 def test_bulk_update_priority
3671 def test_bulk_update_priority
3672 @request.session[:user_id] = 2
3672 @request.session[:user_id] = 2
3673 post :bulk_update, :ids => [1, 2], :issue => {:priority_id => 6}
3673 post :bulk_update, :ids => [1, 2], :issue => {:priority_id => 6}
3674
3674
3675 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3675 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3676 assert_equal 6, Issue.find(1).priority_id
3676 assert_equal 6, Issue.find(1).priority_id
3677 assert_equal 6, Issue.find(2).priority_id
3677 assert_equal 6, Issue.find(2).priority_id
3678 end
3678 end
3679
3679
3680 def test_bulk_update_with_notes
3680 def test_bulk_update_with_notes
3681 @request.session[:user_id] = 2
3681 @request.session[:user_id] = 2
3682 post :bulk_update, :ids => [1, 2], :notes => 'Moving two issues'
3682 post :bulk_update, :ids => [1, 2], :notes => 'Moving two issues'
3683
3683
3684 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3684 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3685 assert_equal 'Moving two issues', Issue.find(1).journals.sort_by(&:id).last.notes
3685 assert_equal 'Moving two issues', Issue.find(1).journals.sort_by(&:id).last.notes
3686 assert_equal 'Moving two issues', Issue.find(2).journals.sort_by(&:id).last.notes
3686 assert_equal 'Moving two issues', Issue.find(2).journals.sort_by(&:id).last.notes
3687 end
3687 end
3688
3688
3689 def test_bulk_update_parent_id
3689 def test_bulk_update_parent_id
3690 IssueRelation.delete_all
3690 IssueRelation.delete_all
3691 @request.session[:user_id] = 2
3691 @request.session[:user_id] = 2
3692 post :bulk_update, :ids => [1, 3],
3692 post :bulk_update, :ids => [1, 3],
3693 :notes => 'Bulk editing parent',
3693 :notes => 'Bulk editing parent',
3694 :issue => {:priority_id => '', :assigned_to_id => '',
3694 :issue => {:priority_id => '', :assigned_to_id => '',
3695 :status_id => '', :parent_issue_id => '2'}
3695 :status_id => '', :parent_issue_id => '2'}
3696 assert_response 302
3696 assert_response 302
3697 parent = Issue.find(2)
3697 parent = Issue.find(2)
3698 assert_equal parent.id, Issue.find(1).parent_id
3698 assert_equal parent.id, Issue.find(1).parent_id
3699 assert_equal parent.id, Issue.find(3).parent_id
3699 assert_equal parent.id, Issue.find(3).parent_id
3700 assert_equal [1, 3], parent.children.collect(&:id).sort
3700 assert_equal [1, 3], parent.children.collect(&:id).sort
3701 end
3701 end
3702
3702
3703 def test_bulk_update_custom_field
3703 def test_bulk_update_custom_field
3704 @request.session[:user_id] = 2
3704 @request.session[:user_id] = 2
3705 # update issues priority
3705 # update issues priority
3706 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing custom field',
3706 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing custom field',
3707 :issue => {:priority_id => '',
3707 :issue => {:priority_id => '',
3708 :assigned_to_id => '',
3708 :assigned_to_id => '',
3709 :custom_field_values => {'2' => '777'}}
3709 :custom_field_values => {'2' => '777'}}
3710
3710
3711 assert_response 302
3711 assert_response 302
3712
3712
3713 issue = Issue.find(1)
3713 issue = Issue.find(1)
3714 journal = issue.journals.reorder('created_on DESC').first
3714 journal = issue.journals.reorder('created_on DESC').first
3715 assert_equal '777', issue.custom_value_for(2).value
3715 assert_equal '777', issue.custom_value_for(2).value
3716 assert_equal 1, journal.details.size
3716 assert_equal 1, journal.details.size
3717 assert_equal '125', journal.details.first.old_value
3717 assert_equal '125', journal.details.first.old_value
3718 assert_equal '777', journal.details.first.value
3718 assert_equal '777', journal.details.first.value
3719 end
3719 end
3720
3720
3721 def test_bulk_update_custom_field_to_blank
3721 def test_bulk_update_custom_field_to_blank
3722 @request.session[:user_id] = 2
3722 @request.session[:user_id] = 2
3723 post :bulk_update, :ids => [1, 3], :notes => 'Bulk editing custom field',
3723 post :bulk_update, :ids => [1, 3], :notes => 'Bulk editing custom field',
3724 :issue => {:priority_id => '',
3724 :issue => {:priority_id => '',
3725 :assigned_to_id => '',
3725 :assigned_to_id => '',
3726 :custom_field_values => {'1' => '__none__'}}
3726 :custom_field_values => {'1' => '__none__'}}
3727 assert_response 302
3727 assert_response 302
3728 assert_equal '', Issue.find(1).custom_field_value(1)
3728 assert_equal '', Issue.find(1).custom_field_value(1)
3729 assert_equal '', Issue.find(3).custom_field_value(1)
3729 assert_equal '', Issue.find(3).custom_field_value(1)
3730 end
3730 end
3731
3731
3732 def test_bulk_update_multi_custom_field
3732 def test_bulk_update_multi_custom_field
3733 field = CustomField.find(1)
3733 field = CustomField.find(1)
3734 field.update_attribute :multiple, true
3734 field.update_attribute :multiple, true
3735
3735
3736 @request.session[:user_id] = 2
3736 @request.session[:user_id] = 2
3737 post :bulk_update, :ids => [1, 2, 3], :notes => 'Bulk editing multi custom field',
3737 post :bulk_update, :ids => [1, 2, 3], :notes => 'Bulk editing multi custom field',
3738 :issue => {:priority_id => '',
3738 :issue => {:priority_id => '',
3739 :assigned_to_id => '',
3739 :assigned_to_id => '',
3740 :custom_field_values => {'1' => ['MySQL', 'Oracle']}}
3740 :custom_field_values => {'1' => ['MySQL', 'Oracle']}}
3741
3741
3742 assert_response 302
3742 assert_response 302
3743
3743
3744 assert_equal ['MySQL', 'Oracle'], Issue.find(1).custom_field_value(1).sort
3744 assert_equal ['MySQL', 'Oracle'], Issue.find(1).custom_field_value(1).sort
3745 assert_equal ['MySQL', 'Oracle'], Issue.find(3).custom_field_value(1).sort
3745 assert_equal ['MySQL', 'Oracle'], Issue.find(3).custom_field_value(1).sort
3746 # the custom field is not associated with the issue tracker
3746 # the custom field is not associated with the issue tracker
3747 assert_nil Issue.find(2).custom_field_value(1)
3747 assert_nil Issue.find(2).custom_field_value(1)
3748 end
3748 end
3749
3749
3750 def test_bulk_update_multi_custom_field_to_blank
3750 def test_bulk_update_multi_custom_field_to_blank
3751 field = CustomField.find(1)
3751 field = CustomField.find(1)
3752 field.update_attribute :multiple, true
3752 field.update_attribute :multiple, true
3753
3753
3754 @request.session[:user_id] = 2
3754 @request.session[:user_id] = 2
3755 post :bulk_update, :ids => [1, 3], :notes => 'Bulk editing multi custom field',
3755 post :bulk_update, :ids => [1, 3], :notes => 'Bulk editing multi custom field',
3756 :issue => {:priority_id => '',
3756 :issue => {:priority_id => '',
3757 :assigned_to_id => '',
3757 :assigned_to_id => '',
3758 :custom_field_values => {'1' => ['__none__']}}
3758 :custom_field_values => {'1' => ['__none__']}}
3759 assert_response 302
3759 assert_response 302
3760 assert_equal [''], Issue.find(1).custom_field_value(1)
3760 assert_equal [''], Issue.find(1).custom_field_value(1)
3761 assert_equal [''], Issue.find(3).custom_field_value(1)
3761 assert_equal [''], Issue.find(3).custom_field_value(1)
3762 end
3762 end
3763
3763
3764 def test_bulk_update_unassign
3764 def test_bulk_update_unassign
3765 assert_not_nil Issue.find(2).assigned_to
3765 assert_not_nil Issue.find(2).assigned_to
3766 @request.session[:user_id] = 2
3766 @request.session[:user_id] = 2
3767 # unassign issues
3767 # unassign issues
3768 post :bulk_update, :ids => [1, 2], :notes => 'Bulk unassigning', :issue => {:assigned_to_id => 'none'}
3768 post :bulk_update, :ids => [1, 2], :notes => 'Bulk unassigning', :issue => {:assigned_to_id => 'none'}
3769 assert_response 302
3769 assert_response 302
3770 # check that the issues were updated
3770 # check that the issues were updated
3771 assert_nil Issue.find(2).assigned_to
3771 assert_nil Issue.find(2).assigned_to
3772 end
3772 end
3773
3773
3774 def test_post_bulk_update_should_allow_fixed_version_to_be_set_to_a_subproject
3774 def test_post_bulk_update_should_allow_fixed_version_to_be_set_to_a_subproject
3775 @request.session[:user_id] = 2
3775 @request.session[:user_id] = 2
3776
3776
3777 post :bulk_update, :ids => [1,2], :issue => {:fixed_version_id => 4}
3777 post :bulk_update, :ids => [1,2], :issue => {:fixed_version_id => 4}
3778
3778
3779 assert_response :redirect
3779 assert_response :redirect
3780 issues = Issue.find([1,2])
3780 issues = Issue.find([1,2])
3781 issues.each do |issue|
3781 issues.each do |issue|
3782 assert_equal 4, issue.fixed_version_id
3782 assert_equal 4, issue.fixed_version_id
3783 assert_not_equal issue.project_id, issue.fixed_version.project_id
3783 assert_not_equal issue.project_id, issue.fixed_version.project_id
3784 end
3784 end
3785 end
3785 end
3786
3786
3787 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
3787 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
3788 @request.session[:user_id] = 2
3788 @request.session[:user_id] = 2
3789 post :bulk_update, :ids => [1,2], :back_url => '/issues'
3789 post :bulk_update, :ids => [1,2], :back_url => '/issues'
3790
3790
3791 assert_response :redirect
3791 assert_response :redirect
3792 assert_redirected_to '/issues'
3792 assert_redirected_to '/issues'
3793 end
3793 end
3794
3794
3795 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
3795 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
3796 @request.session[:user_id] = 2
3796 @request.session[:user_id] = 2
3797 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
3797 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
3798
3798
3799 assert_response :redirect
3799 assert_response :redirect
3800 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => Project.find(1).identifier
3800 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => Project.find(1).identifier
3801 end
3801 end
3802
3802
3803 def test_bulk_update_with_all_failures_should_show_errors
3803 def test_bulk_update_with_all_failures_should_show_errors
3804 @request.session[:user_id] = 2
3804 @request.session[:user_id] = 2
3805 post :bulk_update, :ids => [1, 2], :issue => {:start_date => 'foo'}
3805 post :bulk_update, :ids => [1, 2], :issue => {:start_date => 'foo'}
3806
3806
3807 assert_response :success
3807 assert_response :success
3808 assert_template 'bulk_edit'
3808 assert_template 'bulk_edit'
3809 assert_select '#errorExplanation span', :text => 'Failed to save 2 issue(s) on 2 selected: #1, #2.'
3809 assert_select '#errorExplanation span', :text => 'Failed to save 2 issue(s) on 2 selected: #1, #2.'
3810 assert_select '#errorExplanation ul li', :text => 'Start date is not a valid date: #1, #2'
3810 assert_select '#errorExplanation ul li', :text => 'Start date is not a valid date: #1, #2'
3811
3811
3812 assert_equal [1, 2], assigns[:issues].map(&:id)
3812 assert_equal [1, 2], assigns[:issues].map(&:id)
3813 end
3813 end
3814
3814
3815 def test_bulk_update_with_some_failures_should_show_errors
3815 def test_bulk_update_with_some_failures_should_show_errors
3816 issue1 = Issue.generate!(:start_date => '2013-05-12')
3816 issue1 = Issue.generate!(:start_date => '2013-05-12')
3817 issue2 = Issue.generate!(:start_date => '2013-05-15')
3817 issue2 = Issue.generate!(:start_date => '2013-05-15')
3818 issue3 = Issue.generate!
3818 issue3 = Issue.generate!
3819 @request.session[:user_id] = 2
3819 @request.session[:user_id] = 2
3820 post :bulk_update, :ids => [issue1.id, issue2.id, issue3.id],
3820 post :bulk_update, :ids => [issue1.id, issue2.id, issue3.id],
3821 :issue => {:due_date => '2013-05-01'}
3821 :issue => {:due_date => '2013-05-01'}
3822 assert_response :success
3822 assert_response :success
3823 assert_template 'bulk_edit'
3823 assert_template 'bulk_edit'
3824 assert_select '#errorExplanation span',
3824 assert_select '#errorExplanation span',
3825 :text => "Failed to save 2 issue(s) on 3 selected: ##{issue1.id}, ##{issue2.id}."
3825 :text => "Failed to save 2 issue(s) on 3 selected: ##{issue1.id}, ##{issue2.id}."
3826 assert_select '#errorExplanation ul li',
3826 assert_select '#errorExplanation ul li',
3827 :text => "Due date must be greater than start date: ##{issue1.id}, ##{issue2.id}"
3827 :text => "Due date must be greater than start date: ##{issue1.id}, ##{issue2.id}"
3828 assert_equal [issue1.id, issue2.id], assigns[:issues].map(&:id)
3828 assert_equal [issue1.id, issue2.id], assigns[:issues].map(&:id)
3829 end
3829 end
3830
3830
3831 def test_bulk_update_with_failure_should_preserved_form_values
3831 def test_bulk_update_with_failure_should_preserved_form_values
3832 @request.session[:user_id] = 2
3832 @request.session[:user_id] = 2
3833 post :bulk_update, :ids => [1, 2], :issue => {:tracker_id => '2', :start_date => 'foo'}
3833 post :bulk_update, :ids => [1, 2], :issue => {:tracker_id => '2', :start_date => 'foo'}
3834
3834
3835 assert_response :success
3835 assert_response :success
3836 assert_template 'bulk_edit'
3836 assert_template 'bulk_edit'
3837 assert_select 'select[name=?]', 'issue[tracker_id]' do
3837 assert_select 'select[name=?]', 'issue[tracker_id]' do
3838 assert_select 'option[value="2"][selected=selected]'
3838 assert_select 'option[value="2"][selected=selected]'
3839 end
3839 end
3840 assert_select 'input[name=?][value=?]', 'issue[start_date]', 'foo'
3840 assert_select 'input[name=?][value=?]', 'issue[start_date]', 'foo'
3841 end
3841 end
3842
3842
3843 def test_get_bulk_copy
3843 def test_get_bulk_copy
3844 @request.session[:user_id] = 2
3844 @request.session[:user_id] = 2
3845 get :bulk_edit, :ids => [1, 2, 3], :copy => '1'
3845 get :bulk_edit, :ids => [1, 2, 3], :copy => '1'
3846 assert_response :success
3846 assert_response :success
3847 assert_template 'bulk_edit'
3847 assert_template 'bulk_edit'
3848
3848
3849 issues = assigns(:issues)
3849 issues = assigns(:issues)
3850 assert_not_nil issues
3850 assert_not_nil issues
3851 assert_equal [1, 2, 3], issues.map(&:id).sort
3851 assert_equal [1, 2, 3], issues.map(&:id).sort
3852
3852
3853 assert_select 'select[name=?]', 'issue[project_id]' do
3853 assert_select 'select[name=?]', 'issue[project_id]' do
3854 assert_select 'option[value=""]'
3854 assert_select 'option[value=""]'
3855 end
3855 end
3856 assert_select 'input[name=copy_attachments]'
3856 assert_select 'input[name=copy_attachments]'
3857 end
3857 end
3858
3858
3859 def test_get_bulk_copy_without_add_issues_permission_should_not_propose_current_project_as_target
3859 def test_get_bulk_copy_without_add_issues_permission_should_not_propose_current_project_as_target
3860 user = setup_user_with_copy_but_not_add_permission
3860 user = setup_user_with_copy_but_not_add_permission
3861 @request.session[:user_id] = user.id
3861 @request.session[:user_id] = user.id
3862
3862
3863 get :bulk_edit, :ids => [1, 2, 3], :copy => '1'
3863 get :bulk_edit, :ids => [1, 2, 3], :copy => '1'
3864 assert_response :success
3864 assert_response :success
3865 assert_template 'bulk_edit'
3865 assert_template 'bulk_edit'
3866
3866
3867 assert_select 'select[name=?]', 'issue[project_id]' do
3867 assert_select 'select[name=?]', 'issue[project_id]' do
3868 assert_select 'option[value=""]', 0
3868 assert_select 'option[value=""]', 0
3869 assert_select 'option[value="2"]'
3869 assert_select 'option[value="2"]'
3870 end
3870 end
3871 end
3871 end
3872
3872
3873 def test_bulk_copy_to_another_project
3873 def test_bulk_copy_to_another_project
3874 @request.session[:user_id] = 2
3874 @request.session[:user_id] = 2
3875 assert_difference 'Issue.count', 2 do
3875 assert_difference 'Issue.count', 2 do
3876 assert_no_difference 'Project.find(1).issues.count' do
3876 assert_no_difference 'Project.find(1).issues.count' do
3877 post :bulk_update, :ids => [1, 2], :issue => {:project_id => '2'}, :copy => '1'
3877 post :bulk_update, :ids => [1, 2], :issue => {:project_id => '2'}, :copy => '1'
3878 end
3878 end
3879 end
3879 end
3880 assert_redirected_to '/projects/ecookbook/issues'
3880 assert_redirected_to '/projects/ecookbook/issues'
3881
3881
3882 copies = Issue.order('id DESC').limit(issues.size)
3882 copies = Issue.order('id DESC').limit(issues.size)
3883 copies.each do |copy|
3883 copies.each do |copy|
3884 assert_equal 2, copy.project_id
3884 assert_equal 2, copy.project_id
3885 end
3885 end
3886 end
3886 end
3887
3887
3888 def test_bulk_copy_without_add_issues_permission_should_be_allowed_on_project_with_permission
3888 def test_bulk_copy_without_add_issues_permission_should_be_allowed_on_project_with_permission
3889 user = setup_user_with_copy_but_not_add_permission
3889 user = setup_user_with_copy_but_not_add_permission
3890 @request.session[:user_id] = user.id
3890 @request.session[:user_id] = user.id
3891
3891
3892 assert_difference 'Issue.count', 3 do
3892 assert_difference 'Issue.count', 3 do
3893 post :bulk_update, :ids => [1, 2, 3], :issue => {:project_id => '2'}, :copy => '1'
3893 post :bulk_update, :ids => [1, 2, 3], :issue => {:project_id => '2'}, :copy => '1'
3894 assert_response 302
3894 assert_response 302
3895 end
3895 end
3896 end
3896 end
3897
3897
3898 def test_bulk_copy_on_same_project_without_add_issues_permission_should_be_denied
3898 def test_bulk_copy_on_same_project_without_add_issues_permission_should_be_denied
3899 user = setup_user_with_copy_but_not_add_permission
3899 user = setup_user_with_copy_but_not_add_permission
3900 @request.session[:user_id] = user.id
3900 @request.session[:user_id] = user.id
3901
3901
3902 post :bulk_update, :ids => [1, 2, 3], :issue => {:project_id => ''}, :copy => '1'
3902 post :bulk_update, :ids => [1, 2, 3], :issue => {:project_id => ''}, :copy => '1'
3903 assert_response 403
3903 assert_response 403
3904 end
3904 end
3905
3905
3906 def test_bulk_copy_on_different_project_without_add_issues_permission_should_be_denied
3906 def test_bulk_copy_on_different_project_without_add_issues_permission_should_be_denied
3907 user = setup_user_with_copy_but_not_add_permission
3907 user = setup_user_with_copy_but_not_add_permission
3908 @request.session[:user_id] = user.id
3908 @request.session[:user_id] = user.id
3909
3909
3910 post :bulk_update, :ids => [1, 2, 3], :issue => {:project_id => '1'}, :copy => '1'
3910 post :bulk_update, :ids => [1, 2, 3], :issue => {:project_id => '1'}, :copy => '1'
3911 assert_response 403
3911 assert_response 403
3912 end
3912 end
3913
3913
3914 def test_bulk_copy_should_allow_not_changing_the_issue_attributes
3914 def test_bulk_copy_should_allow_not_changing_the_issue_attributes
3915 @request.session[:user_id] = 2
3915 @request.session[:user_id] = 2
3916 issues = [
3916 issues = [
3917 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1,
3917 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1,
3918 :priority_id => 2, :subject => 'issue 1', :author_id => 1,
3918 :priority_id => 2, :subject => 'issue 1', :author_id => 1,
3919 :assigned_to_id => nil),
3919 :assigned_to_id => nil),
3920 Issue.create!(:project_id => 2, :tracker_id => 3, :status_id => 2,
3920 Issue.create!(:project_id => 2, :tracker_id => 3, :status_id => 2,
3921 :priority_id => 1, :subject => 'issue 2', :author_id => 2,
3921 :priority_id => 1, :subject => 'issue 2', :author_id => 2,
3922 :assigned_to_id => 3)
3922 :assigned_to_id => 3)
3923 ]
3923 ]
3924 assert_difference 'Issue.count', issues.size do
3924 assert_difference 'Issue.count', issues.size do
3925 post :bulk_update, :ids => issues.map(&:id), :copy => '1',
3925 post :bulk_update, :ids => issues.map(&:id), :copy => '1',
3926 :issue => {
3926 :issue => {
3927 :project_id => '', :tracker_id => '', :assigned_to_id => '',
3927 :project_id => '', :tracker_id => '', :assigned_to_id => '',
3928 :status_id => '', :start_date => '', :due_date => ''
3928 :status_id => '', :start_date => '', :due_date => ''
3929 }
3929 }
3930 end
3930 end
3931
3931
3932 copies = Issue.order('id DESC').limit(issues.size)
3932 copies = Issue.order('id DESC').limit(issues.size)
3933 issues.each do |orig|
3933 issues.each do |orig|
3934 copy = copies.detect {|c| c.subject == orig.subject}
3934 copy = copies.detect {|c| c.subject == orig.subject}
3935 assert_not_nil copy
3935 assert_not_nil copy
3936 assert_equal orig.project_id, copy.project_id
3936 assert_equal orig.project_id, copy.project_id
3937 assert_equal orig.tracker_id, copy.tracker_id
3937 assert_equal orig.tracker_id, copy.tracker_id
3938 assert_equal orig.status_id, copy.status_id
3938 assert_equal orig.status_id, copy.status_id
3939 assert_equal orig.assigned_to_id, copy.assigned_to_id
3939 assert_equal orig.assigned_to_id, copy.assigned_to_id
3940 assert_equal orig.priority_id, copy.priority_id
3940 assert_equal orig.priority_id, copy.priority_id
3941 end
3941 end
3942 end
3942 end
3943
3943
3944 def test_bulk_copy_should_allow_changing_the_issue_attributes
3944 def test_bulk_copy_should_allow_changing_the_issue_attributes
3945 # Fixes random test failure with Mysql
3945 # Fixes random test failure with Mysql
3946 # where Issue.where(:project_id => 2).limit(2).order('id desc')
3946 # where Issue.where(:project_id => 2).limit(2).order('id desc')
3947 # doesn't return the expected results
3947 # doesn't return the expected results
3948 Issue.delete_all("project_id=2")
3948 Issue.delete_all("project_id=2")
3949
3949
3950 @request.session[:user_id] = 2
3950 @request.session[:user_id] = 2
3951 assert_difference 'Issue.count', 2 do
3951 assert_difference 'Issue.count', 2 do
3952 assert_no_difference 'Project.find(1).issues.count' do
3952 assert_no_difference 'Project.find(1).issues.count' do
3953 post :bulk_update, :ids => [1, 2], :copy => '1',
3953 post :bulk_update, :ids => [1, 2], :copy => '1',
3954 :issue => {
3954 :issue => {
3955 :project_id => '2', :tracker_id => '', :assigned_to_id => '4',
3955 :project_id => '2', :tracker_id => '', :assigned_to_id => '4',
3956 :status_id => '1', :start_date => '2009-12-01', :due_date => '2009-12-31'
3956 :status_id => '1', :start_date => '2009-12-01', :due_date => '2009-12-31'
3957 }
3957 }
3958 end
3958 end
3959 end
3959 end
3960
3960
3961 copied_issues = Issue.where(:project_id => 2).limit(2).order('id desc').to_a
3961 copied_issues = Issue.where(:project_id => 2).limit(2).order('id desc').to_a
3962 assert_equal 2, copied_issues.size
3962 assert_equal 2, copied_issues.size
3963 copied_issues.each do |issue|
3963 copied_issues.each do |issue|
3964 assert_equal 2, issue.project_id, "Project is incorrect"
3964 assert_equal 2, issue.project_id, "Project is incorrect"
3965 assert_equal 4, issue.assigned_to_id, "Assigned to is incorrect"
3965 assert_equal 4, issue.assigned_to_id, "Assigned to is incorrect"
3966 assert_equal 1, issue.status_id, "Status is incorrect"
3966 assert_equal 1, issue.status_id, "Status is incorrect"
3967 assert_equal '2009-12-01', issue.start_date.to_s, "Start date is incorrect"
3967 assert_equal '2009-12-01', issue.start_date.to_s, "Start date is incorrect"
3968 assert_equal '2009-12-31', issue.due_date.to_s, "Due date is incorrect"
3968 assert_equal '2009-12-31', issue.due_date.to_s, "Due date is incorrect"
3969 end
3969 end
3970 end
3970 end
3971
3971
3972 def test_bulk_copy_should_allow_adding_a_note
3972 def test_bulk_copy_should_allow_adding_a_note
3973 @request.session[:user_id] = 2
3973 @request.session[:user_id] = 2
3974 assert_difference 'Issue.count', 1 do
3974 assert_difference 'Issue.count', 1 do
3975 post :bulk_update, :ids => [1], :copy => '1',
3975 post :bulk_update, :ids => [1], :copy => '1',
3976 :notes => 'Copying one issue',
3976 :notes => 'Copying one issue',
3977 :issue => {
3977 :issue => {
3978 :project_id => '', :tracker_id => '', :assigned_to_id => '4',
3978 :project_id => '', :tracker_id => '', :assigned_to_id => '4',
3979 :status_id => '3', :start_date => '2009-12-01', :due_date => '2009-12-31'
3979 :status_id => '3', :start_date => '2009-12-01', :due_date => '2009-12-31'
3980 }
3980 }
3981 end
3981 end
3982 issue = Issue.order('id DESC').first
3982 issue = Issue.order('id DESC').first
3983 assert_equal 1, issue.journals.size
3983 assert_equal 1, issue.journals.size
3984 journal = issue.journals.first
3984 journal = issue.journals.first
3985 assert_equal 'Copying one issue', journal.notes
3985 assert_equal 'Copying one issue', journal.notes
3986 end
3986 end
3987
3987
3988 def test_bulk_copy_should_allow_not_copying_the_attachments
3988 def test_bulk_copy_should_allow_not_copying_the_attachments
3989 attachment_count = Issue.find(3).attachments.size
3989 attachment_count = Issue.find(3).attachments.size
3990 assert attachment_count > 0
3990 assert attachment_count > 0
3991 @request.session[:user_id] = 2
3991 @request.session[:user_id] = 2
3992
3992
3993 assert_difference 'Issue.count', 1 do
3993 assert_difference 'Issue.count', 1 do
3994 assert_no_difference 'Attachment.count' do
3994 assert_no_difference 'Attachment.count' do
3995 post :bulk_update, :ids => [3], :copy => '1',
3995 post :bulk_update, :ids => [3], :copy => '1',
3996 :issue => {
3996 :issue => {
3997 :project_id => ''
3997 :project_id => ''
3998 }
3998 }
3999 end
3999 end
4000 end
4000 end
4001 end
4001 end
4002
4002
4003 def test_bulk_copy_should_allow_copying_the_attachments
4003 def test_bulk_copy_should_allow_copying_the_attachments
4004 attachment_count = Issue.find(3).attachments.size
4004 attachment_count = Issue.find(3).attachments.size
4005 assert attachment_count > 0
4005 assert attachment_count > 0
4006 @request.session[:user_id] = 2
4006 @request.session[:user_id] = 2
4007
4007
4008 assert_difference 'Issue.count', 1 do
4008 assert_difference 'Issue.count', 1 do
4009 assert_difference 'Attachment.count', attachment_count do
4009 assert_difference 'Attachment.count', attachment_count do
4010 post :bulk_update, :ids => [3], :copy => '1', :copy_attachments => '1',
4010 post :bulk_update, :ids => [3], :copy => '1', :copy_attachments => '1',
4011 :issue => {
4011 :issue => {
4012 :project_id => ''
4012 :project_id => ''
4013 }
4013 }
4014 end
4014 end
4015 end
4015 end
4016 end
4016 end
4017
4017
4018 def test_bulk_copy_should_add_relations_with_copied_issues
4018 def test_bulk_copy_should_add_relations_with_copied_issues
4019 @request.session[:user_id] = 2
4019 @request.session[:user_id] = 2
4020
4020
4021 assert_difference 'Issue.count', 2 do
4021 assert_difference 'Issue.count', 2 do
4022 assert_difference 'IssueRelation.count', 2 do
4022 assert_difference 'IssueRelation.count', 2 do
4023 post :bulk_update, :ids => [1, 3], :copy => '1', :link_copy => '1',
4023 post :bulk_update, :ids => [1, 3], :copy => '1', :link_copy => '1',
4024 :issue => {
4024 :issue => {
4025 :project_id => '1'
4025 :project_id => '1'
4026 }
4026 }
4027 end
4027 end
4028 end
4028 end
4029 end
4029 end
4030
4030
4031 def test_bulk_copy_should_allow_not_copying_the_subtasks
4031 def test_bulk_copy_should_allow_not_copying_the_subtasks
4032 issue = Issue.generate_with_descendants!
4032 issue = Issue.generate_with_descendants!
4033 @request.session[:user_id] = 2
4033 @request.session[:user_id] = 2
4034
4034
4035 assert_difference 'Issue.count', 1 do
4035 assert_difference 'Issue.count', 1 do
4036 post :bulk_update, :ids => [issue.id], :copy => '1',
4036 post :bulk_update, :ids => [issue.id], :copy => '1',
4037 :issue => {
4037 :issue => {
4038 :project_id => ''
4038 :project_id => ''
4039 }
4039 }
4040 end
4040 end
4041 end
4041 end
4042
4042
4043 def test_bulk_copy_should_allow_copying_the_subtasks
4043 def test_bulk_copy_should_allow_copying_the_subtasks
4044 issue = Issue.generate_with_descendants!
4044 issue = Issue.generate_with_descendants!
4045 count = issue.descendants.count
4045 count = issue.descendants.count
4046 @request.session[:user_id] = 2
4046 @request.session[:user_id] = 2
4047
4047
4048 assert_difference 'Issue.count', count+1 do
4048 assert_difference 'Issue.count', count+1 do
4049 post :bulk_update, :ids => [issue.id], :copy => '1', :copy_subtasks => '1',
4049 post :bulk_update, :ids => [issue.id], :copy => '1', :copy_subtasks => '1',
4050 :issue => {
4050 :issue => {
4051 :project_id => ''
4051 :project_id => ''
4052 }
4052 }
4053 end
4053 end
4054 copy = Issue.where(:parent_id => nil).order("id DESC").first
4054 copy = Issue.where(:parent_id => nil).order("id DESC").first
4055 assert_equal count, copy.descendants.count
4055 assert_equal count, copy.descendants.count
4056 end
4056 end
4057
4057
4058 def test_bulk_copy_should_not_copy_selected_subtasks_twice
4058 def test_bulk_copy_should_not_copy_selected_subtasks_twice
4059 issue = Issue.generate_with_descendants!
4059 issue = Issue.generate_with_descendants!
4060 count = issue.descendants.count
4060 count = issue.descendants.count
4061 @request.session[:user_id] = 2
4061 @request.session[:user_id] = 2
4062
4062
4063 assert_difference 'Issue.count', count+1 do
4063 assert_difference 'Issue.count', count+1 do
4064 post :bulk_update, :ids => issue.self_and_descendants.map(&:id), :copy => '1', :copy_subtasks => '1',
4064 post :bulk_update, :ids => issue.self_and_descendants.map(&:id), :copy => '1', :copy_subtasks => '1',
4065 :issue => {
4065 :issue => {
4066 :project_id => ''
4066 :project_id => ''
4067 }
4067 }
4068 end
4068 end
4069 copy = Issue.where(:parent_id => nil).order("id DESC").first
4069 copy = Issue.where(:parent_id => nil).order("id DESC").first
4070 assert_equal count, copy.descendants.count
4070 assert_equal count, copy.descendants.count
4071 end
4071 end
4072
4072
4073 def test_bulk_copy_to_another_project_should_follow_when_needed
4073 def test_bulk_copy_to_another_project_should_follow_when_needed
4074 @request.session[:user_id] = 2
4074 @request.session[:user_id] = 2
4075 post :bulk_update, :ids => [1], :copy => '1', :issue => {:project_id => 2}, :follow => '1'
4075 post :bulk_update, :ids => [1], :copy => '1', :issue => {:project_id => 2}, :follow => '1'
4076 issue = Issue.order('id DESC').first
4076 issue = Issue.order('id DESC').first
4077 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
4077 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
4078 end
4078 end
4079
4079
4080 def test_bulk_copy_with_all_failures_should_display_errors
4080 def test_bulk_copy_with_all_failures_should_display_errors
4081 @request.session[:user_id] = 2
4081 @request.session[:user_id] = 2
4082 post :bulk_update, :ids => [1, 2], :copy => '1', :issue => {:start_date => 'foo'}
4082 post :bulk_update, :ids => [1, 2], :copy => '1', :issue => {:start_date => 'foo'}
4083
4083
4084 assert_response :success
4084 assert_response :success
4085 end
4085 end
4086
4086
4087 def test_destroy_issue_with_no_time_entries
4087 def test_destroy_issue_with_no_time_entries
4088 assert_nil TimeEntry.find_by_issue_id(2)
4088 assert_nil TimeEntry.find_by_issue_id(2)
4089 @request.session[:user_id] = 2
4089 @request.session[:user_id] = 2
4090
4090
4091 assert_difference 'Issue.count', -1 do
4091 assert_difference 'Issue.count', -1 do
4092 delete :destroy, :id => 2
4092 delete :destroy, :id => 2
4093 end
4093 end
4094 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4094 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4095 assert_nil Issue.find_by_id(2)
4095 assert_nil Issue.find_by_id(2)
4096 end
4096 end
4097
4097
4098 def test_destroy_issues_with_time_entries
4098 def test_destroy_issues_with_time_entries
4099 @request.session[:user_id] = 2
4099 @request.session[:user_id] = 2
4100
4100
4101 assert_no_difference 'Issue.count' do
4101 assert_no_difference 'Issue.count' do
4102 delete :destroy, :ids => [1, 3]
4102 delete :destroy, :ids => [1, 3]
4103 end
4103 end
4104 assert_response :success
4104 assert_response :success
4105 assert_template 'destroy'
4105 assert_template 'destroy'
4106 assert_not_nil assigns(:hours)
4106 assert_not_nil assigns(:hours)
4107 assert Issue.find_by_id(1) && Issue.find_by_id(3)
4107 assert Issue.find_by_id(1) && Issue.find_by_id(3)
4108
4108
4109 assert_select 'form' do
4109 assert_select 'form' do
4110 assert_select 'input[name=_method][value=delete]'
4110 assert_select 'input[name=_method][value=delete]'
4111 end
4111 end
4112 end
4112 end
4113
4113
4114 def test_destroy_issues_and_destroy_time_entries
4114 def test_destroy_issues_and_destroy_time_entries
4115 @request.session[:user_id] = 2
4115 @request.session[:user_id] = 2
4116
4116
4117 assert_difference 'Issue.count', -2 do
4117 assert_difference 'Issue.count', -2 do
4118 assert_difference 'TimeEntry.count', -3 do
4118 assert_difference 'TimeEntry.count', -3 do
4119 delete :destroy, :ids => [1, 3], :todo => 'destroy'
4119 delete :destroy, :ids => [1, 3], :todo => 'destroy'
4120 end
4120 end
4121 end
4121 end
4122 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4122 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4123 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
4123 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
4124 assert_nil TimeEntry.find_by_id([1, 2])
4124 assert_nil TimeEntry.find_by_id([1, 2])
4125 end
4125 end
4126
4126
4127 def test_destroy_issues_and_assign_time_entries_to_project
4127 def test_destroy_issues_and_assign_time_entries_to_project
4128 @request.session[:user_id] = 2
4128 @request.session[:user_id] = 2
4129
4129
4130 assert_difference 'Issue.count', -2 do
4130 assert_difference 'Issue.count', -2 do
4131 assert_no_difference 'TimeEntry.count' do
4131 assert_no_difference 'TimeEntry.count' do
4132 delete :destroy, :ids => [1, 3], :todo => 'nullify'
4132 delete :destroy, :ids => [1, 3], :todo => 'nullify'
4133 end
4133 end
4134 end
4134 end
4135 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4135 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4136 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
4136 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
4137 assert_nil TimeEntry.find(1).issue_id
4137 assert_nil TimeEntry.find(1).issue_id
4138 assert_nil TimeEntry.find(2).issue_id
4138 assert_nil TimeEntry.find(2).issue_id
4139 end
4139 end
4140
4140
4141 def test_destroy_issues_and_reassign_time_entries_to_another_issue
4141 def test_destroy_issues_and_reassign_time_entries_to_another_issue
4142 @request.session[:user_id] = 2
4142 @request.session[:user_id] = 2
4143
4143
4144 assert_difference 'Issue.count', -2 do
4144 assert_difference 'Issue.count', -2 do
4145 assert_no_difference 'TimeEntry.count' do
4145 assert_no_difference 'TimeEntry.count' do
4146 delete :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
4146 delete :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
4147 end
4147 end
4148 end
4148 end
4149 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4149 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4150 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
4150 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
4151 assert_equal 2, TimeEntry.find(1).issue_id
4151 assert_equal 2, TimeEntry.find(1).issue_id
4152 assert_equal 2, TimeEntry.find(2).issue_id
4152 assert_equal 2, TimeEntry.find(2).issue_id
4153 end
4153 end
4154
4154
4155 def test_destroy_issues_and_reassign_time_entries_to_an_invalid_issue_should_fail
4155 def test_destroy_issues_and_reassign_time_entries_to_an_invalid_issue_should_fail
4156 @request.session[:user_id] = 2
4156 @request.session[:user_id] = 2
4157
4157
4158 assert_no_difference 'Issue.count' do
4158 assert_no_difference 'Issue.count' do
4159 assert_no_difference 'TimeEntry.count' do
4159 assert_no_difference 'TimeEntry.count' do
4160 # try to reassign time to an issue of another project
4160 # try to reassign time to an issue of another project
4161 delete :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 4
4161 delete :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 4
4162 end
4162 end
4163 end
4163 end
4164 assert_response :success
4164 assert_response :success
4165 assert_template 'destroy'
4165 assert_template 'destroy'
4166 end
4166 end
4167
4167
4168 def test_destroy_issues_from_different_projects
4168 def test_destroy_issues_from_different_projects
4169 @request.session[:user_id] = 2
4169 @request.session[:user_id] = 2
4170
4170
4171 assert_difference 'Issue.count', -3 do
4171 assert_difference 'Issue.count', -3 do
4172 delete :destroy, :ids => [1, 2, 6], :todo => 'destroy'
4172 delete :destroy, :ids => [1, 2, 6], :todo => 'destroy'
4173 end
4173 end
4174 assert_redirected_to :controller => 'issues', :action => 'index'
4174 assert_redirected_to :controller => 'issues', :action => 'index'
4175 assert !(Issue.find_by_id(1) || Issue.find_by_id(2) || Issue.find_by_id(6))
4175 assert !(Issue.find_by_id(1) || Issue.find_by_id(2) || Issue.find_by_id(6))
4176 end
4176 end
4177
4177
4178 def test_destroy_parent_and_child_issues
4178 def test_destroy_parent_and_child_issues
4179 parent = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'Parent Issue')
4179 parent = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'Parent Issue')
4180 child = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'Child Issue', :parent_issue_id => parent.id)
4180 child = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'Child Issue', :parent_issue_id => parent.id)
4181 assert child.is_descendant_of?(parent.reload)
4181 assert child.is_descendant_of?(parent.reload)
4182
4182
4183 @request.session[:user_id] = 2
4183 @request.session[:user_id] = 2
4184 assert_difference 'Issue.count', -2 do
4184 assert_difference 'Issue.count', -2 do
4185 delete :destroy, :ids => [parent.id, child.id], :todo => 'destroy'
4185 delete :destroy, :ids => [parent.id, child.id], :todo => 'destroy'
4186 end
4186 end
4187 assert_response 302
4187 assert_response 302
4188 end
4188 end
4189
4189
4190 def test_destroy_invalid_should_respond_with_404
4190 def test_destroy_invalid_should_respond_with_404
4191 @request.session[:user_id] = 2
4191 @request.session[:user_id] = 2
4192 assert_no_difference 'Issue.count' do
4192 assert_no_difference 'Issue.count' do
4193 delete :destroy, :id => 999
4193 delete :destroy, :id => 999
4194 end
4194 end
4195 assert_response 404
4195 assert_response 404
4196 end
4196 end
4197
4197
4198 def test_default_search_scope
4198 def test_default_search_scope
4199 get :index
4199 get :index
4200
4200
4201 assert_select 'div#quick-search form' do
4201 assert_select 'div#quick-search form' do
4202 assert_select 'input[name=issues][value="1"][type=hidden]'
4202 assert_select 'input[name=issues][value="1"][type=hidden]'
4203 end
4203 end
4204 end
4204 end
4205
4205
4206 def setup_user_with_copy_but_not_add_permission
4206 def setup_user_with_copy_but_not_add_permission
4207 Role.all.each {|r| r.remove_permission! :add_issues}
4207 Role.all.each {|r| r.remove_permission! :add_issues}
4208 Role.find_by_name('Manager').add_permission! :add_issues
4208 Role.find_by_name('Manager').add_permission! :add_issues
4209 user = User.generate!
4209 user = User.generate!
4210 User.add_to_project(user, Project.find(1), Role.find_by_name('Developer'))
4210 User.add_to_project(user, Project.find(1), Role.find_by_name('Developer'))
4211 User.add_to_project(user, Project.find(2), Role.find_by_name('Manager'))
4211 User.add_to_project(user, Project.find(2), Role.find_by_name('Manager'))
4212 user
4212 user
4213 end
4213 end
4214 end
4214 end
@@ -1,94 +1,85
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../../test_helper', __FILE__)
18 require File.expand_path('../../../test_helper', __FILE__)
19
19
20 class ProjectsHelperTest < ActionView::TestCase
20 class ProjectsHelperTest < ActionView::TestCase
21 include ApplicationHelper
21 include ApplicationHelper
22 include ProjectsHelper
22 include ProjectsHelper
23 include Redmine::I18n
23 include Redmine::I18n
24 include ERB::Util
24 include ERB::Util
25 include Rails.application.routes.url_helpers
25 include Rails.application.routes.url_helpers
26
26
27 fixtures :projects, :trackers, :issue_statuses, :issues,
27 fixtures :projects, :trackers, :issue_statuses, :issues,
28 :enumerations, :users, :issue_categories,
28 :enumerations, :users, :issue_categories,
29 :versions,
29 :versions,
30 :projects_trackers,
30 :projects_trackers,
31 :member_roles,
31 :member_roles,
32 :members,
32 :members,
33 :groups_users,
33 :groups_users,
34 :enabled_modules
34 :enabled_modules
35
35
36 def setup
36 def setup
37 super
37 super
38 set_language_if_valid('en')
38 set_language_if_valid('en')
39 User.current = nil
39 User.current = nil
40 end
40 end
41
41
42 def test_link_to_version_within_project
42 def test_link_to_version_within_project
43 @project = Project.find(2)
43 @project = Project.find(2)
44 User.current = User.find(1)
44 User.current = User.find(1)
45 assert_equal '<a title="07/01/2006" href="/versions/5">Alpha</a>', link_to_version(Version.find(5))
45 assert_equal '<a title="07/01/2006" href="/versions/5">Alpha</a>', link_to_version(Version.find(5))
46 end
46 end
47
47
48 def test_link_to_version
48 def test_link_to_version
49 User.current = User.find(1)
49 User.current = User.find(1)
50 assert_equal '<a title="07/01/2006" href="/versions/5">Alpha</a>', link_to_version(Version.find(5))
50 assert_equal '<a title="07/01/2006" href="/versions/5">OnlineStore - Alpha</a>', link_to_version(Version.find(5))
51 end
51 end
52
52
53 def test_link_to_version_without_effective_date
53 def test_link_to_version_without_effective_date
54 User.current = User.find(1)
54 User.current = User.find(1)
55 version = Version.find(5)
55 version = Version.find(5)
56 version.effective_date = nil
56 version.effective_date = nil
57 assert_equal '<a href="/versions/5">Alpha</a>', link_to_version(version)
57 assert_equal '<a href="/versions/5">OnlineStore - Alpha</a>', link_to_version(version)
58 end
58 end
59
59
60 def test_link_to_private_version
60 def test_link_to_private_version
61 assert_equal 'Alpha', link_to_version(Version.find(5))
61 assert_equal 'OnlineStore - Alpha', link_to_version(Version.find(5))
62 end
62 end
63
63
64 def test_link_to_version_invalid_version
64 def test_link_to_version_invalid_version
65 assert_equal '', link_to_version(Object)
65 assert_equal '', link_to_version(Object)
66 end
66 end
67
67
68 def test_format_version_name_within_project
68 def test_format_version_name_within_project
69 @project = Project.find(1)
69 @project = Project.find(1)
70 assert_equal "0.1", format_version_name(Version.find(1))
70 assert_equal "0.1", format_version_name(Version.find(1))
71 end
71 end
72
72
73 def test_format_version_name
73 def test_format_version_name
74 assert_equal "0.1", format_version_name(Version.find(1))
74 assert_equal "eCookbook - 0.1", format_version_name(Version.find(1))
75 end
76
77 def test_format_version_name_for_shared_version_within_project_should_not_display_project_name
78 @project = Project.find(1)
79 version = Version.find(1)
80 version.sharing = 'system'
81 assert_equal "0.1", format_version_name(version)
82 end
75 end
83
76
84 def test_format_version_name_for_shared_version_should_display_project_name
77 def test_format_version_name_for_system_version
85 version = Version.find(1)
78 assert_equal "OnlineStore - Systemwide visible version", format_version_name(Version.find(7))
86 version.sharing = 'system'
87 assert_equal "eCookbook - 0.1", format_version_name(version)
88 end
79 end
89
80
90 def test_version_options_for_select_with_no_versions
81 def test_version_options_for_select_with_no_versions
91 assert_equal '', version_options_for_select([])
82 assert_equal '', version_options_for_select([])
92 assert_equal '', version_options_for_select([], Version.find(1))
83 assert_equal '', version_options_for_select([], Version.find(1))
93 end
84 end
94 end
85 end
General Comments 0
You need to be logged in to leave comments. Login now