##// END OF EJS Templates
Merged r14165 and r14166 (#19544)....
Jean-Philippe Lang -
r13825:873dff0d364e
parent child
Show More
@@ -1,256 +1,261
1 1 # encoding: utf-8
2 2 #
3 3 # Helpers to sort tables using clickable column headers.
4 4 #
5 5 # Author: Stuart Rackham <srackham@methods.co.nz>, March 2005.
6 6 # Jean-Philippe Lang, 2009
7 7 # License: This source code is released under the MIT license.
8 8 #
9 9 # - Consecutive clicks toggle the column's sort order.
10 10 # - Sort state is maintained by a session hash entry.
11 11 # - CSS classes identify sort column and state.
12 12 # - Typically used in conjunction with the Pagination module.
13 13 #
14 14 # Example code snippets:
15 15 #
16 16 # Controller:
17 17 #
18 18 # helper :sort
19 19 # include SortHelper
20 20 #
21 21 # def list
22 22 # sort_init 'last_name'
23 23 # sort_update %w(first_name last_name)
24 24 # @items = Contact.find_all nil, sort_clause
25 25 # end
26 26 #
27 27 # Controller (using Pagination module):
28 28 #
29 29 # helper :sort
30 30 # include SortHelper
31 31 #
32 32 # def list
33 33 # sort_init 'last_name'
34 34 # sort_update %w(first_name last_name)
35 35 # @contact_pages, @items = paginate :contacts,
36 36 # :order_by => sort_clause,
37 37 # :per_page => 10
38 38 # end
39 39 #
40 40 # View (table header in list.rhtml):
41 41 #
42 42 # <thead>
43 43 # <tr>
44 44 # <%= sort_header_tag('id', :title => 'Sort by contact ID') %>
45 45 # <%= sort_header_tag('last_name', :caption => 'Name') %>
46 46 # <%= sort_header_tag('phone') %>
47 47 # <%= sort_header_tag('address', :width => 200) %>
48 48 # </tr>
49 49 # </thead>
50 50 #
51 51 # - Introduces instance variables: @sort_default, @sort_criteria
52 52 # - Introduces param :sort
53 53 #
54 54
55 55 module SortHelper
56 56 class SortCriteria
57 57
58 58 def initialize
59 59 @criteria = []
60 60 end
61 61
62 62 def available_criteria=(criteria)
63 63 unless criteria.is_a?(Hash)
64 64 criteria = criteria.inject({}) {|h,k| h[k] = k; h}
65 65 end
66 66 @available_criteria = criteria
67 67 end
68 68
69 69 def from_param(param)
70 70 @criteria = param.to_s.split(',').collect {|s| s.split(':')[0..1]}
71 71 normalize!
72 72 end
73 73
74 74 def criteria=(arg)
75 75 @criteria = arg
76 76 normalize!
77 77 end
78 78
79 79 def to_param
80 80 @criteria.collect {|k,o| k + (o ? '' : ':desc')}.join(',')
81 81 end
82 82
83 83 # Returns an array of SQL fragments used to sort the list
84 84 def to_sql
85 85 sql = @criteria.collect do |k,o|
86 86 if s = @available_criteria[k]
87 87 s = [s] unless s.is_a?(Array)
88 (o ? s : s.collect {|c| append_desc(c)})
88 s.collect {|c| append_order(c, o ? "ASC" : "DESC")}
89 89 end
90 90 end.flatten.compact
91 91 sql.blank? ? nil : sql
92 92 end
93 93
94 94 def to_a
95 95 @criteria.dup
96 96 end
97 97
98 98 def add!(key, asc)
99 99 @criteria.delete_if {|k,o| k == key}
100 100 @criteria = [[key, asc]] + @criteria
101 101 normalize!
102 102 end
103 103
104 104 def add(*args)
105 105 r = self.class.new.from_param(to_param)
106 106 r.add!(*args)
107 107 r
108 108 end
109 109
110 110 def first_key
111 111 @criteria.first && @criteria.first.first
112 112 end
113 113
114 114 def first_asc?
115 115 @criteria.first && @criteria.first.last
116 116 end
117 117
118 118 def empty?
119 119 @criteria.empty?
120 120 end
121 121
122 122 private
123 123
124 124 def normalize!
125 125 @criteria ||= []
126 126 @criteria = @criteria.collect {|s| s = Array(s); [s.first, (s.last == false || s.last == 'desc') ? false : true]}
127 127 @criteria = @criteria.select {|k,o| @available_criteria.has_key?(k)} if @available_criteria
128 128 @criteria.slice!(3)
129 129 self
130 130 end
131 131
132 # Appends DESC to the sort criterion unless it has a fixed order
133 def append_desc(criterion)
132 # Appends ASC/DESC to the sort criterion unless it has a fixed order
133 def append_order(criterion, order)
134 134 if criterion =~ / (asc|desc)$/i
135 135 criterion
136 136 else
137 "#{criterion} DESC"
137 "#{criterion} #{order}"
138 138 end
139 139 end
140
141 # Appends DESC to the sort criterion unless it has a fixed order
142 def append_desc(criterion)
143 append_order(criterion, "DESC")
144 end
140 145 end
141 146
142 147 def sort_name
143 148 controller_name + '_' + action_name + '_sort'
144 149 end
145 150
146 151 # Initializes the default sort.
147 152 # Examples:
148 153 #
149 154 # sort_init 'name'
150 155 # sort_init 'id', 'desc'
151 156 # sort_init ['name', ['id', 'desc']]
152 157 # sort_init [['name', 'desc'], ['id', 'desc']]
153 158 #
154 159 def sort_init(*args)
155 160 case args.size
156 161 when 1
157 162 @sort_default = args.first.is_a?(Array) ? args.first : [[args.first]]
158 163 when 2
159 164 @sort_default = [[args.first, args.last]]
160 165 else
161 166 raise ArgumentError
162 167 end
163 168 end
164 169
165 170 # Updates the sort state. Call this in the controller prior to calling
166 171 # sort_clause.
167 172 # - criteria can be either an array or a hash of allowed keys
168 173 #
169 174 def sort_update(criteria, sort_name=nil)
170 175 sort_name ||= self.sort_name
171 176 @sort_criteria = SortCriteria.new
172 177 @sort_criteria.available_criteria = criteria
173 178 @sort_criteria.from_param(params[:sort] || session[sort_name])
174 179 @sort_criteria.criteria = @sort_default if @sort_criteria.empty?
175 180 session[sort_name] = @sort_criteria.to_param
176 181 end
177 182
178 183 # Clears the sort criteria session data
179 184 #
180 185 def sort_clear
181 186 session[sort_name] = nil
182 187 end
183 188
184 189 # Returns an SQL sort clause corresponding to the current sort state.
185 190 # Use this to sort the controller's table items collection.
186 191 #
187 192 def sort_clause()
188 193 @sort_criteria.to_sql
189 194 end
190 195
191 196 def sort_criteria
192 197 @sort_criteria
193 198 end
194 199
195 200 # Returns a link which sorts by the named column.
196 201 #
197 202 # - column is the name of an attribute in the sorted record collection.
198 203 # - the optional caption explicitly specifies the displayed link text.
199 204 # - 2 CSS classes reflect the state of the link: sort and asc or desc
200 205 #
201 206 def sort_link(column, caption, default_order)
202 207 css, order = nil, default_order
203 208
204 209 if column.to_s == @sort_criteria.first_key
205 210 if @sort_criteria.first_asc?
206 211 css = 'sort asc'
207 212 order = 'desc'
208 213 else
209 214 css = 'sort desc'
210 215 order = 'asc'
211 216 end
212 217 end
213 218 caption = column.to_s.humanize unless caption
214 219
215 220 sort_options = { :sort => @sort_criteria.add(column.to_s, order).to_param }
216 221 url_options = params.merge(sort_options)
217 222
218 223 # Add project_id to url_options
219 224 url_options = url_options.merge(:project_id => params[:project_id]) if params.has_key?(:project_id)
220 225
221 226 link_to_content_update(h(caption), url_options, :class => css)
222 227 end
223 228
224 229 # Returns a table header <th> tag with a sort link for the named column
225 230 # attribute.
226 231 #
227 232 # Options:
228 233 # :caption The displayed link name (defaults to titleized column name).
229 234 # :title The tag's 'title' attribute (defaults to 'Sort by :caption').
230 235 #
231 236 # Other options hash entries generate additional table header tag attributes.
232 237 #
233 238 # Example:
234 239 #
235 240 # <%= sort_header_tag('id', :title => 'Sort by contact ID', :width => 40) %>
236 241 #
237 242 def sort_header_tag(column, options = {})
238 243 caption = options.delete(:caption) || column.to_s.humanize
239 244 default_order = options.delete(:default_order) || 'asc'
240 245 options[:title] = l(:label_sort_by, "\"#{caption}\"") unless options[:title]
241 246 content_tag('th', sort_link(column, caption, default_order), options)
242 247 end
243 248
244 249 # Returns the css classes for the current sort order
245 250 #
246 251 # Example:
247 252 #
248 253 # sort_css_classes
249 254 # # => "sort-by-created-on sort-desc"
250 255 def sort_css_classes
251 256 if @sort_criteria.first_key
252 257 "sort-by-#{@sort_criteria.first_key.to_s.dasherize} sort-#{@sort_criteria.first_asc? ? 'asc' : 'desc'}"
253 258 end
254 259 end
255 260 end
256 261
@@ -1,4225 +1,4233
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class IssuesControllerTest < ActionController::TestCase
21 21 fixtures :projects,
22 22 :users, :email_addresses,
23 23 :roles,
24 24 :members,
25 25 :member_roles,
26 26 :issues,
27 27 :issue_statuses,
28 28 :issue_relations,
29 29 :versions,
30 30 :trackers,
31 31 :projects_trackers,
32 32 :issue_categories,
33 33 :enabled_modules,
34 34 :enumerations,
35 35 :attachments,
36 36 :workflows,
37 37 :custom_fields,
38 38 :custom_values,
39 39 :custom_fields_projects,
40 40 :custom_fields_trackers,
41 41 :time_entries,
42 42 :journals,
43 43 :journal_details,
44 44 :queries,
45 45 :repositories,
46 46 :changesets
47 47
48 48 include Redmine::I18n
49 49
50 50 def setup
51 51 User.current = nil
52 52 end
53 53
54 54 def test_index
55 55 with_settings :default_language => "en" do
56 56 get :index
57 57 assert_response :success
58 58 assert_template 'index'
59 59 assert_not_nil assigns(:issues)
60 60 assert_nil assigns(:project)
61 61
62 62 # links to visible issues
63 63 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
64 64 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
65 65 # private projects hidden
66 66 assert_select 'a[href="/issues/6"]', 0
67 67 assert_select 'a[href="/issues/4"]', 0
68 68 # project column
69 69 assert_select 'th', :text => /Project/
70 70 end
71 71 end
72 72
73 73 def test_index_should_not_list_issues_when_module_disabled
74 74 EnabledModule.delete_all("name = 'issue_tracking' AND project_id = 1")
75 75 get :index
76 76 assert_response :success
77 77 assert_template 'index'
78 78 assert_not_nil assigns(:issues)
79 79 assert_nil assigns(:project)
80 80
81 81 assert_select 'a[href="/issues/1"]', 0
82 82 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
83 83 end
84 84
85 85 def test_index_should_list_visible_issues_only
86 86 get :index, :per_page => 100
87 87 assert_response :success
88 88 assert_not_nil assigns(:issues)
89 89 assert_nil assigns(:issues).detect {|issue| !issue.visible?}
90 90 end
91 91
92 92 def test_index_with_project
93 93 Setting.display_subprojects_issues = 0
94 94 get :index, :project_id => 1
95 95 assert_response :success
96 96 assert_template 'index'
97 97 assert_not_nil assigns(:issues)
98 98
99 99 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
100 100 assert_select 'a[href="/issues/5"]', 0
101 101 end
102 102
103 103 def test_index_with_project_and_subprojects
104 104 Setting.display_subprojects_issues = 1
105 105 get :index, :project_id => 1
106 106 assert_response :success
107 107 assert_template 'index'
108 108 assert_not_nil assigns(:issues)
109 109
110 110 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
111 111 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
112 112 assert_select 'a[href="/issues/6"]', 0
113 113 end
114 114
115 115 def test_index_with_project_and_subprojects_should_show_private_subprojects_with_permission
116 116 @request.session[:user_id] = 2
117 117 Setting.display_subprojects_issues = 1
118 118 get :index, :project_id => 1
119 119 assert_response :success
120 120 assert_template 'index'
121 121 assert_not_nil assigns(:issues)
122 122
123 123 assert_select 'a[href="/issues/1"]', :text => /Cannot print recipes/
124 124 assert_select 'a[href="/issues/5"]', :text => /Subproject issue/
125 125 assert_select 'a[href="/issues/6"]', :text => /Issue of a private subproject/
126 126 end
127 127
128 128 def test_index_with_project_and_default_filter
129 129 get :index, :project_id => 1, :set_filter => 1
130 130 assert_response :success
131 131 assert_template 'index'
132 132 assert_not_nil assigns(:issues)
133 133
134 134 query = assigns(:query)
135 135 assert_not_nil query
136 136 # default filter
137 137 assert_equal({'status_id' => {:operator => 'o', :values => ['']}}, query.filters)
138 138 end
139 139
140 140 def test_index_with_project_and_filter
141 141 get :index, :project_id => 1, :set_filter => 1,
142 142 :f => ['tracker_id'],
143 143 :op => {'tracker_id' => '='},
144 144 :v => {'tracker_id' => ['1']}
145 145 assert_response :success
146 146 assert_template 'index'
147 147 assert_not_nil assigns(:issues)
148 148
149 149 query = assigns(:query)
150 150 assert_not_nil query
151 151 assert_equal({'tracker_id' => {:operator => '=', :values => ['1']}}, query.filters)
152 152 end
153 153
154 154 def test_index_with_short_filters
155 155 to_test = {
156 156 'status_id' => {
157 157 'o' => { :op => 'o', :values => [''] },
158 158 'c' => { :op => 'c', :values => [''] },
159 159 '7' => { :op => '=', :values => ['7'] },
160 160 '7|3|4' => { :op => '=', :values => ['7', '3', '4'] },
161 161 '=7' => { :op => '=', :values => ['7'] },
162 162 '!3' => { :op => '!', :values => ['3'] },
163 163 '!7|3|4' => { :op => '!', :values => ['7', '3', '4'] }},
164 164 'subject' => {
165 165 'This is a subject' => { :op => '=', :values => ['This is a subject'] },
166 166 'o' => { :op => '=', :values => ['o'] },
167 167 '~This is part of a subject' => { :op => '~', :values => ['This is part of a subject'] },
168 168 '!~This is part of a subject' => { :op => '!~', :values => ['This is part of a subject'] }},
169 169 'tracker_id' => {
170 170 '3' => { :op => '=', :values => ['3'] },
171 171 '=3' => { :op => '=', :values => ['3'] }},
172 172 'start_date' => {
173 173 '2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
174 174 '=2011-10-12' => { :op => '=', :values => ['2011-10-12'] },
175 175 '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
176 176 '<=2011-10-12' => { :op => '<=', :values => ['2011-10-12'] },
177 177 '><2011-10-01|2011-10-30' => { :op => '><', :values => ['2011-10-01', '2011-10-30'] },
178 178 '<t+2' => { :op => '<t+', :values => ['2'] },
179 179 '>t+2' => { :op => '>t+', :values => ['2'] },
180 180 't+2' => { :op => 't+', :values => ['2'] },
181 181 't' => { :op => 't', :values => [''] },
182 182 'w' => { :op => 'w', :values => [''] },
183 183 '>t-2' => { :op => '>t-', :values => ['2'] },
184 184 '<t-2' => { :op => '<t-', :values => ['2'] },
185 185 't-2' => { :op => 't-', :values => ['2'] }},
186 186 'created_on' => {
187 187 '>=2011-10-12' => { :op => '>=', :values => ['2011-10-12'] },
188 188 '<t-2' => { :op => '<t-', :values => ['2'] },
189 189 '>t-2' => { :op => '>t-', :values => ['2'] },
190 190 't-2' => { :op => 't-', :values => ['2'] }},
191 191 'cf_1' => {
192 192 'c' => { :op => '=', :values => ['c'] },
193 193 '!c' => { :op => '!', :values => ['c'] },
194 194 '!*' => { :op => '!*', :values => [''] },
195 195 '*' => { :op => '*', :values => [''] }},
196 196 'estimated_hours' => {
197 197 '=13.4' => { :op => '=', :values => ['13.4'] },
198 198 '>=45' => { :op => '>=', :values => ['45'] },
199 199 '<=125' => { :op => '<=', :values => ['125'] },
200 200 '><10.5|20.5' => { :op => '><', :values => ['10.5', '20.5'] },
201 201 '!*' => { :op => '!*', :values => [''] },
202 202 '*' => { :op => '*', :values => [''] }}
203 203 }
204 204
205 205 default_filter = { 'status_id' => {:operator => 'o', :values => [''] }}
206 206
207 207 to_test.each do |field, expression_and_expected|
208 208 expression_and_expected.each do |filter_expression, expected|
209 209
210 210 get :index, :set_filter => 1, field => filter_expression
211 211
212 212 assert_response :success
213 213 assert_template 'index'
214 214 assert_not_nil assigns(:issues)
215 215
216 216 query = assigns(:query)
217 217 assert_not_nil query
218 218 assert query.has_filter?(field)
219 219 assert_equal(default_filter.merge({field => {:operator => expected[:op], :values => expected[:values]}}), query.filters)
220 220 end
221 221 end
222 222 end
223 223
224 224 def test_index_with_project_and_empty_filters
225 225 get :index, :project_id => 1, :set_filter => 1, :fields => ['']
226 226 assert_response :success
227 227 assert_template 'index'
228 228 assert_not_nil assigns(:issues)
229 229
230 230 query = assigns(:query)
231 231 assert_not_nil query
232 232 # no filter
233 233 assert_equal({}, query.filters)
234 234 end
235 235
236 236 def test_index_with_project_custom_field_filter
237 237 field = ProjectCustomField.create!(:name => 'Client', :is_filter => true, :field_format => 'string')
238 238 CustomValue.create!(:custom_field => field, :customized => Project.find(3), :value => 'Foo')
239 239 CustomValue.create!(:custom_field => field, :customized => Project.find(5), :value => 'Foo')
240 240 filter_name = "project.cf_#{field.id}"
241 241 @request.session[:user_id] = 1
242 242
243 243 get :index, :set_filter => 1,
244 244 :f => [filter_name],
245 245 :op => {filter_name => '='},
246 246 :v => {filter_name => ['Foo']}
247 247 assert_response :success
248 248 assert_template 'index'
249 249 assert_equal [3, 5], assigns(:issues).map(&:project_id).uniq.sort
250 250 end
251 251
252 252 def test_index_with_query
253 253 get :index, :project_id => 1, :query_id => 5
254 254 assert_response :success
255 255 assert_template 'index'
256 256 assert_not_nil assigns(:issues)
257 257 assert_nil assigns(:issue_count_by_group)
258 258 end
259 259
260 260 def test_index_with_query_grouped_by_tracker
261 261 get :index, :project_id => 1, :query_id => 6
262 262 assert_response :success
263 263 assert_template 'index'
264 264 assert_not_nil assigns(:issues)
265 265 assert_not_nil assigns(:issue_count_by_group)
266 266 end
267 267
268 def test_index_with_query_grouped_and_sorted_by_category
269 get :index, :project_id => 1, :set_filter => 1, :group_by => "category", :sort => "category"
270 assert_response :success
271 assert_template 'index'
272 assert_not_nil assigns(:issues)
273 assert_not_nil assigns(:issue_count_by_group)
274 end
275
268 276 def test_index_with_query_grouped_by_list_custom_field
269 277 get :index, :project_id => 1, :query_id => 9
270 278 assert_response :success
271 279 assert_template 'index'
272 280 assert_not_nil assigns(:issues)
273 281 assert_not_nil assigns(:issue_count_by_group)
274 282 end
275 283
276 284 def test_index_with_query_grouped_by_user_custom_field
277 285 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
278 286 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
279 287 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
280 288 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
281 289 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
282 290
283 291 get :index, :project_id => 1, :set_filter => 1, :group_by => "cf_#{cf.id}"
284 292 assert_response :success
285 293
286 294 assert_select 'tr.group', 3
287 295 assert_select 'tr.group' do
288 296 assert_select 'a', :text => 'John Smith'
289 297 assert_select 'span.count', :text => '1'
290 298 end
291 299 assert_select 'tr.group' do
292 300 assert_select 'a', :text => 'Dave Lopper'
293 301 assert_select 'span.count', :text => '2'
294 302 end
295 303 end
296 304
297 305 def test_index_grouped_by_boolean_custom_field_should_distinguish_blank_and_false_values
298 306 cf = IssueCustomField.create!(:name => 'Bool', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'bool')
299 307 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '1')
300 308 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '0')
301 309 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '')
302 310
303 311 with_settings :default_language => 'en' do
304 312 get :index, :project_id => 1, :set_filter => 1, :group_by => "cf_#{cf.id}"
305 313 assert_response :success
306 314 end
307 315
308 316 assert_select 'tr.group', 3
309 317 assert_select 'tr.group', :text => /Yes/
310 318 assert_select 'tr.group', :text => /No/
311 319 assert_select 'tr.group', :text => /blank/
312 320 end
313 321
314 322 def test_index_grouped_by_boolean_custom_field_with_false_group_in_first_position_should_show_the_group
315 323 cf = IssueCustomField.create!(:name => 'Bool', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'bool', :is_filter => true)
316 324 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '0')
317 325 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '0')
318 326
319 327 with_settings :default_language => 'en' do
320 328 get :index, :project_id => 1, :set_filter => 1, "cf_#{cf.id}" => "*", :group_by => "cf_#{cf.id}"
321 329 assert_response :success
322 330 assert_equal [1, 2], assigns(:issues).map(&:id).sort
323 331 end
324 332
325 333 assert_select 'tr.group', 1
326 334 assert_select 'tr.group', :text => /No/
327 335 end
328 336
329 337 def test_index_with_query_grouped_by_tracker_in_normal_order
330 338 3.times {|i| Issue.generate!(:tracker_id => (i + 1))}
331 339
332 340 get :index, :set_filter => 1, :group_by => 'tracker', :sort => 'id:desc'
333 341 assert_response :success
334 342
335 343 trackers = assigns(:issues).map(&:tracker).uniq
336 344 assert_equal [1, 2, 3], trackers.map(&:id)
337 345 end
338 346
339 347 def test_index_with_query_grouped_by_tracker_in_reverse_order
340 348 3.times {|i| Issue.generate!(:tracker_id => (i + 1))}
341 349
342 350 get :index, :set_filter => 1, :group_by => 'tracker', :sort => 'id:desc,tracker:desc'
343 351 assert_response :success
344 352
345 353 trackers = assigns(:issues).map(&:tracker).uniq
346 354 assert_equal [3, 2, 1], trackers.map(&:id)
347 355 end
348 356
349 357 def test_index_with_query_id_and_project_id_should_set_session_query
350 358 get :index, :project_id => 1, :query_id => 4
351 359 assert_response :success
352 360 assert_kind_of Hash, session[:query]
353 361 assert_equal 4, session[:query][:id]
354 362 assert_equal 1, session[:query][:project_id]
355 363 end
356 364
357 365 def test_index_with_invalid_query_id_should_respond_404
358 366 get :index, :project_id => 1, :query_id => 999
359 367 assert_response 404
360 368 end
361 369
362 370 def test_index_with_cross_project_query_in_session_should_show_project_issues
363 371 q = IssueQuery.create!(:name => "test", :user_id => 2, :visibility => IssueQuery::VISIBILITY_PRIVATE, :project => nil)
364 372 @request.session[:query] = {:id => q.id, :project_id => 1}
365 373
366 374 with_settings :display_subprojects_issues => '0' do
367 375 get :index, :project_id => 1
368 376 end
369 377 assert_response :success
370 378 assert_not_nil assigns(:query)
371 379 assert_equal q.id, assigns(:query).id
372 380 assert_equal 1, assigns(:query).project_id
373 381 assert_equal [1], assigns(:issues).map(&:project_id).uniq
374 382 end
375 383
376 384 def test_private_query_should_not_be_available_to_other_users
377 385 q = IssueQuery.create!(:name => "private", :user => User.find(2), :visibility => IssueQuery::VISIBILITY_PRIVATE, :project => nil)
378 386 @request.session[:user_id] = 3
379 387
380 388 get :index, :query_id => q.id
381 389 assert_response 403
382 390 end
383 391
384 392 def test_private_query_should_be_available_to_its_user
385 393 q = IssueQuery.create!(:name => "private", :user => User.find(2), :visibility => IssueQuery::VISIBILITY_PRIVATE, :project => nil)
386 394 @request.session[:user_id] = 2
387 395
388 396 get :index, :query_id => q.id
389 397 assert_response :success
390 398 end
391 399
392 400 def test_public_query_should_be_available_to_other_users
393 401 q = IssueQuery.create!(:name => "private", :user => User.find(2), :visibility => IssueQuery::VISIBILITY_PUBLIC, :project => nil)
394 402 @request.session[:user_id] = 3
395 403
396 404 get :index, :query_id => q.id
397 405 assert_response :success
398 406 end
399 407
400 408 def test_index_should_omit_page_param_in_export_links
401 409 get :index, :page => 2
402 410 assert_response :success
403 411 assert_select 'a.atom[href="/issues.atom"]'
404 412 assert_select 'a.csv[href="/issues.csv"]'
405 413 assert_select 'a.pdf[href="/issues.pdf"]'
406 414 assert_select 'form#csv-export-form[action="/issues.csv"]'
407 415 end
408 416
409 417 def test_index_should_not_warn_when_not_exceeding_export_limit
410 418 with_settings :issues_export_limit => 200 do
411 419 get :index
412 420 assert_select '#csv-export-options p.icon-warning', 0
413 421 end
414 422 end
415 423
416 424 def test_index_should_warn_when_exceeding_export_limit
417 425 with_settings :issues_export_limit => 2 do
418 426 get :index
419 427 assert_select '#csv-export-options p.icon-warning', :text => %r{limit: 2}
420 428 end
421 429 end
422 430
423 431 def test_index_csv
424 432 get :index, :format => 'csv'
425 433 assert_response :success
426 434 assert_not_nil assigns(:issues)
427 435 assert_equal 'text/csv; header=present', @response.content_type
428 436 assert @response.body.starts_with?("#,")
429 437 lines = @response.body.chomp.split("\n")
430 438 assert_equal assigns(:query).columns.size, lines[0].split(',').size
431 439 end
432 440
433 441 def test_index_csv_with_project
434 442 get :index, :project_id => 1, :format => 'csv'
435 443 assert_response :success
436 444 assert_not_nil assigns(:issues)
437 445 assert_equal 'text/csv; header=present', @response.content_type
438 446 end
439 447
440 448 def test_index_csv_with_description
441 449 Issue.generate!(:description => 'test_index_csv_with_description')
442 450
443 451 with_settings :default_language => 'en' do
444 452 get :index, :format => 'csv', :description => '1'
445 453 assert_response :success
446 454 assert_not_nil assigns(:issues)
447 455 end
448 456
449 457 assert_equal 'text/csv; header=present', response.content_type
450 458 headers = response.body.chomp.split("\n").first.split(',')
451 459 assert_include 'Description', headers
452 460 assert_include 'test_index_csv_with_description', response.body
453 461 end
454 462
455 463 def test_index_csv_with_spent_time_column
456 464 issue = Issue.create!(:project_id => 1, :tracker_id => 1, :subject => 'test_index_csv_with_spent_time_column', :author_id => 2)
457 465 TimeEntry.create!(:project => issue.project, :issue => issue, :hours => 7.33, :user => User.find(2), :spent_on => Date.today)
458 466
459 467 get :index, :format => 'csv', :set_filter => '1', :c => %w(subject spent_hours)
460 468 assert_response :success
461 469 assert_equal 'text/csv; header=present', @response.content_type
462 470 lines = @response.body.chomp.split("\n")
463 471 assert_include "#{issue.id},#{issue.subject},7.33", lines
464 472 end
465 473
466 474 def test_index_csv_with_all_columns
467 475 get :index, :format => 'csv', :columns => 'all'
468 476 assert_response :success
469 477 assert_not_nil assigns(:issues)
470 478 assert_equal 'text/csv; header=present', @response.content_type
471 479 assert_match /\A#,/, response.body
472 480 lines = response.body.chomp.split("\n")
473 481 assert_equal assigns(:query).available_inline_columns.size, lines[0].split(',').size
474 482 end
475 483
476 484 def test_index_csv_with_multi_column_field
477 485 CustomField.find(1).update_attribute :multiple, true
478 486 issue = Issue.find(1)
479 487 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
480 488 issue.save!
481 489
482 490 get :index, :format => 'csv', :columns => 'all'
483 491 assert_response :success
484 492 lines = @response.body.chomp.split("\n")
485 493 assert lines.detect {|line| line.include?('"MySQL, Oracle"')}
486 494 end
487 495
488 496 def test_index_csv_should_format_float_custom_fields_with_csv_decimal_separator
489 497 field = IssueCustomField.create!(:name => 'Float', :is_for_all => true, :tracker_ids => [1], :field_format => 'float')
490 498 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {field.id => '185.6'})
491 499
492 500 with_settings :default_language => 'fr' do
493 501 get :index, :format => 'csv', :columns => 'all'
494 502 assert_response :success
495 503 issue_line = response.body.chomp.split("\n").map {|line| line.split(';')}.detect {|line| line[0]==issue.id.to_s}
496 504 assert_include '185,60', issue_line
497 505 end
498 506
499 507 with_settings :default_language => 'en' do
500 508 get :index, :format => 'csv', :columns => 'all'
501 509 assert_response :success
502 510 issue_line = response.body.chomp.split("\n").map {|line| line.split(',')}.detect {|line| line[0]==issue.id.to_s}
503 511 assert_include '185.60', issue_line
504 512 end
505 513 end
506 514
507 515 def test_index_csv_should_fill_parent_column_with_parent_id
508 516 Issue.delete_all
509 517 parent = Issue.generate!
510 518 child = Issue.generate!(:parent_issue_id => parent.id)
511 519
512 520 with_settings :default_language => 'en' do
513 521 get :index, :format => 'csv', :c => %w(parent)
514 522 end
515 523 lines = response.body.split("\n")
516 524 assert_include "#{child.id},#{parent.id}", lines
517 525 end
518 526
519 527 def test_index_csv_big_5
520 528 with_settings :default_language => "zh-TW" do
521 529 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88".force_encoding('UTF-8')
522 530 str_big5 = "\xa4@\xa4\xeb".force_encoding('Big5')
523 531 issue = Issue.generate!(:subject => str_utf8)
524 532
525 533 get :index, :project_id => 1,
526 534 :f => ['subject'],
527 535 :op => '=', :values => [str_utf8],
528 536 :format => 'csv'
529 537 assert_equal 'text/csv; header=present', @response.content_type
530 538 lines = @response.body.chomp.split("\n")
531 539 header = lines[0]
532 540 issue_line = lines.find {|l| l =~ /^#{issue.id},/}
533 541 s1 = "\xaa\xac\xbaA".force_encoding('Big5')
534 542 assert_include s1, header
535 543 assert_include str_big5, issue_line
536 544 end
537 545 end
538 546
539 547 def test_index_csv_cannot_convert_should_be_replaced_big_5
540 548 with_settings :default_language => "zh-TW" do
541 549 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85".force_encoding('UTF-8')
542 550 issue = Issue.generate!(:subject => str_utf8)
543 551
544 552 get :index, :project_id => 1,
545 553 :f => ['subject'],
546 554 :op => '=', :values => [str_utf8],
547 555 :c => ['status', 'subject'],
548 556 :format => 'csv',
549 557 :set_filter => 1
550 558 assert_equal 'text/csv; header=present', @response.content_type
551 559 lines = @response.body.chomp.split("\n")
552 560 header = lines[0]
553 561 issue_line = lines.find {|l| l =~ /^#{issue.id},/}
554 562 s1 = "\xaa\xac\xbaA".force_encoding('Big5') # status
555 563 assert header.include?(s1)
556 564 s2 = issue_line.split(",")[2]
557 565 s3 = "\xa5H?".force_encoding('Big5') # subject
558 566 assert_equal s3, s2
559 567 end
560 568 end
561 569
562 570 def test_index_csv_tw
563 571 with_settings :default_language => "zh-TW" do
564 572 str1 = "test_index_csv_tw"
565 573 issue = Issue.generate!(:subject => str1, :estimated_hours => '1234.5')
566 574
567 575 get :index, :project_id => 1,
568 576 :f => ['subject'],
569 577 :op => '=', :values => [str1],
570 578 :c => ['estimated_hours', 'subject'],
571 579 :format => 'csv',
572 580 :set_filter => 1
573 581 assert_equal 'text/csv; header=present', @response.content_type
574 582 lines = @response.body.chomp.split("\n")
575 583 assert_include "#{issue.id},1234.50,#{str1}", lines
576 584 end
577 585 end
578 586
579 587 def test_index_csv_fr
580 588 with_settings :default_language => "fr" do
581 589 str1 = "test_index_csv_fr"
582 590 issue = Issue.generate!(:subject => str1, :estimated_hours => '1234.5')
583 591
584 592 get :index, :project_id => 1,
585 593 :f => ['subject'],
586 594 :op => '=', :values => [str1],
587 595 :c => ['estimated_hours', 'subject'],
588 596 :format => 'csv',
589 597 :set_filter => 1
590 598 assert_equal 'text/csv; header=present', @response.content_type
591 599 lines = @response.body.chomp.split("\n")
592 600 assert_include "#{issue.id};1234,50;#{str1}", lines
593 601 end
594 602 end
595 603
596 604 def test_index_pdf
597 605 ["en", "zh", "zh-TW", "ja", "ko"].each do |lang|
598 606 with_settings :default_language => lang do
599 607
600 608 get :index
601 609 assert_response :success
602 610 assert_template 'index'
603 611
604 612 get :index, :format => 'pdf'
605 613 assert_response :success
606 614 assert_not_nil assigns(:issues)
607 615 assert_equal 'application/pdf', @response.content_type
608 616
609 617 get :index, :project_id => 1, :format => 'pdf'
610 618 assert_response :success
611 619 assert_not_nil assigns(:issues)
612 620 assert_equal 'application/pdf', @response.content_type
613 621
614 622 get :index, :project_id => 1, :query_id => 6, :format => 'pdf'
615 623 assert_response :success
616 624 assert_not_nil assigns(:issues)
617 625 assert_equal 'application/pdf', @response.content_type
618 626 end
619 627 end
620 628 end
621 629
622 630 def test_index_pdf_with_query_grouped_by_list_custom_field
623 631 get :index, :project_id => 1, :query_id => 9, :format => 'pdf'
624 632 assert_response :success
625 633 assert_not_nil assigns(:issues)
626 634 assert_not_nil assigns(:issue_count_by_group)
627 635 assert_equal 'application/pdf', @response.content_type
628 636 end
629 637
630 638 def test_index_atom
631 639 get :index, :project_id => 'ecookbook', :format => 'atom'
632 640 assert_response :success
633 641 assert_template 'common/feed'
634 642 assert_equal 'application/atom+xml', response.content_type
635 643
636 644 assert_select 'feed' do
637 645 assert_select 'link[rel=self][href=?]', 'http://test.host/projects/ecookbook/issues.atom'
638 646 assert_select 'link[rel=alternate][href=?]', 'http://test.host/projects/ecookbook/issues'
639 647 assert_select 'entry link[href=?]', 'http://test.host/issues/1'
640 648 end
641 649 end
642 650
643 651 def test_index_sort
644 652 get :index, :sort => 'tracker,id:desc'
645 653 assert_response :success
646 654
647 655 sort_params = @request.session['issues_index_sort']
648 656 assert sort_params.is_a?(String)
649 657 assert_equal 'tracker,id:desc', sort_params
650 658
651 659 issues = assigns(:issues)
652 660 assert_not_nil issues
653 661 assert !issues.empty?
654 662 assert_equal issues.sort {|a,b| a.tracker == b.tracker ? b.id <=> a.id : a.tracker <=> b.tracker }.collect(&:id), issues.collect(&:id)
655 663 assert_select 'table.issues.sort-by-tracker.sort-asc'
656 664 end
657 665
658 666 def test_index_sort_by_field_not_included_in_columns
659 667 Setting.issue_list_default_columns = %w(subject author)
660 668 get :index, :sort => 'tracker'
661 669 end
662 670
663 671 def test_index_sort_by_assigned_to
664 672 get :index, :sort => 'assigned_to'
665 673 assert_response :success
666 674 assignees = assigns(:issues).collect(&:assigned_to).compact
667 675 assert_equal assignees.sort, assignees
668 676 assert_select 'table.issues.sort-by-assigned-to.sort-asc'
669 677 end
670 678
671 679 def test_index_sort_by_assigned_to_desc
672 680 get :index, :sort => 'assigned_to:desc'
673 681 assert_response :success
674 682 assignees = assigns(:issues).collect(&:assigned_to).compact
675 683 assert_equal assignees.sort.reverse, assignees
676 684 assert_select 'table.issues.sort-by-assigned-to.sort-desc'
677 685 end
678 686
679 687 def test_index_group_by_assigned_to
680 688 get :index, :group_by => 'assigned_to', :sort => 'priority'
681 689 assert_response :success
682 690 end
683 691
684 692 def test_index_sort_by_author
685 693 get :index, :sort => 'author'
686 694 assert_response :success
687 695 authors = assigns(:issues).collect(&:author)
688 696 assert_equal authors.sort, authors
689 697 end
690 698
691 699 def test_index_sort_by_author_desc
692 700 get :index, :sort => 'author:desc'
693 701 assert_response :success
694 702 authors = assigns(:issues).collect(&:author)
695 703 assert_equal authors.sort.reverse, authors
696 704 end
697 705
698 706 def test_index_group_by_author
699 707 get :index, :group_by => 'author', :sort => 'priority'
700 708 assert_response :success
701 709 end
702 710
703 711 def test_index_sort_by_spent_hours
704 712 get :index, :sort => 'spent_hours:desc'
705 713 assert_response :success
706 714 hours = assigns(:issues).collect(&:spent_hours)
707 715 assert_equal hours.sort.reverse, hours
708 716 end
709 717
710 718 def test_index_sort_by_user_custom_field
711 719 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
712 720 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
713 721 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
714 722 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
715 723 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
716 724
717 725 get :index, :project_id => 1, :set_filter => 1, :sort => "cf_#{cf.id},id"
718 726 assert_response :success
719 727
720 728 assert_equal [2, 3, 1], assigns(:issues).select {|issue| issue.custom_field_value(cf).present?}.map(&:id)
721 729 end
722 730
723 731 def test_index_with_columns
724 732 columns = ['tracker', 'subject', 'assigned_to']
725 733 get :index, :set_filter => 1, :c => columns
726 734 assert_response :success
727 735
728 736 # query should use specified columns
729 737 query = assigns(:query)
730 738 assert_kind_of IssueQuery, query
731 739 assert_equal columns, query.column_names.map(&:to_s)
732 740
733 741 # columns should be stored in session
734 742 assert_kind_of Hash, session[:query]
735 743 assert_kind_of Array, session[:query][:column_names]
736 744 assert_equal columns, session[:query][:column_names].map(&:to_s)
737 745
738 746 # ensure only these columns are kept in the selected columns list
739 747 assert_select 'select#selected_columns option' do
740 748 assert_select 'option', 3
741 749 assert_select 'option[value=tracker]'
742 750 assert_select 'option[value=project]', 0
743 751 end
744 752 end
745 753
746 754 def test_index_without_project_should_implicitly_add_project_column_to_default_columns
747 755 Setting.issue_list_default_columns = ['tracker', 'subject', 'assigned_to']
748 756 get :index, :set_filter => 1
749 757
750 758 # query should use specified columns
751 759 query = assigns(:query)
752 760 assert_kind_of IssueQuery, query
753 761 assert_equal [:id, :project, :tracker, :subject, :assigned_to], query.columns.map(&:name)
754 762 end
755 763
756 764 def test_index_without_project_and_explicit_default_columns_should_not_add_project_column
757 765 Setting.issue_list_default_columns = ['tracker', 'subject', 'assigned_to']
758 766 columns = ['id', 'tracker', 'subject', 'assigned_to']
759 767 get :index, :set_filter => 1, :c => columns
760 768
761 769 # query should use specified columns
762 770 query = assigns(:query)
763 771 assert_kind_of IssueQuery, query
764 772 assert_equal columns.map(&:to_sym), query.columns.map(&:name)
765 773 end
766 774
767 775 def test_index_with_default_columns_should_respect_default_columns_order
768 776 columns = ['assigned_to', 'subject', 'status', 'tracker']
769 777 with_settings :issue_list_default_columns => columns do
770 778 get :index, :project_id => 1, :set_filter => 1
771 779
772 780 query = assigns(:query)
773 781 assert_equal (['id'] + columns).map(&:to_sym), query.columns.map(&:name)
774 782 end
775 783 end
776 784
777 785 def test_index_with_custom_field_column
778 786 columns = %w(tracker subject cf_2)
779 787 get :index, :set_filter => 1, :c => columns
780 788 assert_response :success
781 789
782 790 # query should use specified columns
783 791 query = assigns(:query)
784 792 assert_kind_of IssueQuery, query
785 793 assert_equal columns, query.column_names.map(&:to_s)
786 794
787 795 assert_select 'table.issues td.cf_2.string'
788 796 end
789 797
790 798 def test_index_with_multi_custom_field_column
791 799 field = CustomField.find(1)
792 800 field.update_attribute :multiple, true
793 801 issue = Issue.find(1)
794 802 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
795 803 issue.save!
796 804
797 805 get :index, :set_filter => 1, :c => %w(tracker subject cf_1)
798 806 assert_response :success
799 807
800 808 assert_select 'table.issues td.cf_1', :text => 'MySQL, Oracle'
801 809 end
802 810
803 811 def test_index_with_multi_user_custom_field_column
804 812 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
805 813 :tracker_ids => [1], :is_for_all => true)
806 814 issue = Issue.find(1)
807 815 issue.custom_field_values = {field.id => ['2', '3']}
808 816 issue.save!
809 817
810 818 get :index, :set_filter => 1, :c => ['tracker', 'subject', "cf_#{field.id}"]
811 819 assert_response :success
812 820
813 821 assert_select "table.issues td.cf_#{field.id}" do
814 822 assert_select 'a', 2
815 823 assert_select 'a[href=?]', '/users/2', :text => 'John Smith'
816 824 assert_select 'a[href=?]', '/users/3', :text => 'Dave Lopper'
817 825 end
818 826 end
819 827
820 828 def test_index_with_date_column
821 829 with_settings :date_format => '%d/%m/%Y' do
822 830 Issue.find(1).update_attribute :start_date, '1987-08-24'
823 831 get :index, :set_filter => 1, :c => %w(start_date)
824 832 assert_select "table.issues td.start_date", :text => '24/08/1987'
825 833 end
826 834 end
827 835
828 836 def test_index_with_done_ratio_column
829 837 Issue.find(1).update_attribute :done_ratio, 40
830 838 get :index, :set_filter => 1, :c => %w(done_ratio)
831 839 assert_select 'table.issues td.done_ratio' do
832 840 assert_select 'table.progress' do
833 841 assert_select 'td.closed[style=?]', 'width: 40%;'
834 842 end
835 843 end
836 844 end
837 845
838 846 def test_index_with_spent_hours_column
839 847 get :index, :set_filter => 1, :c => %w(subject spent_hours)
840 848 assert_select 'table.issues tr#issue-3 td.spent_hours', :text => '1.00'
841 849 end
842 850
843 851 def test_index_should_not_show_spent_hours_column_without_permission
844 852 Role.anonymous.remove_permission! :view_time_entries
845 853 get :index, :set_filter => 1, :c => %w(subject spent_hours)
846 854 assert_select 'td.spent_hours', 0
847 855 end
848 856
849 857 def test_index_with_fixed_version_column
850 858 get :index, :set_filter => 1, :c => %w(fixed_version)
851 859 assert_select 'table.issues td.fixed_version' do
852 860 assert_select 'a[href=?]', '/versions/2', :text => 'eCookbook - 1.0'
853 861 end
854 862 end
855 863
856 864 def test_index_with_relations_column
857 865 IssueRelation.delete_all
858 866 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(1), :issue_to => Issue.find(7))
859 867 IssueRelation.create!(:relation_type => "relates", :issue_from => Issue.find(8), :issue_to => Issue.find(1))
860 868 IssueRelation.create!(:relation_type => "blocks", :issue_from => Issue.find(1), :issue_to => Issue.find(11))
861 869 IssueRelation.create!(:relation_type => "blocks", :issue_from => Issue.find(12), :issue_to => Issue.find(2))
862 870
863 871 get :index, :set_filter => 1, :c => %w(subject relations)
864 872 assert_response :success
865 873 assert_select "tr#issue-1 td.relations" do
866 874 assert_select "span", 3
867 875 assert_select "span", :text => "Related to #7"
868 876 assert_select "span", :text => "Related to #8"
869 877 assert_select "span", :text => "Blocks #11"
870 878 end
871 879 assert_select "tr#issue-2 td.relations" do
872 880 assert_select "span", 1
873 881 assert_select "span", :text => "Blocked by #12"
874 882 end
875 883 assert_select "tr#issue-3 td.relations" do
876 884 assert_select "span", 0
877 885 end
878 886
879 887 get :index, :set_filter => 1, :c => %w(relations), :format => 'csv'
880 888 assert_response :success
881 889 assert_equal 'text/csv; header=present', response.content_type
882 890 lines = response.body.chomp.split("\n")
883 891 assert_include '1,"Related to #7, Related to #8, Blocks #11"', lines
884 892 assert_include '2,Blocked by #12', lines
885 893 assert_include '3,""', lines
886 894
887 895 get :index, :set_filter => 1, :c => %w(subject relations), :format => 'pdf'
888 896 assert_response :success
889 897 assert_equal 'application/pdf', response.content_type
890 898 end
891 899
892 900 def test_index_with_description_column
893 901 get :index, :set_filter => 1, :c => %w(subject description)
894 902
895 903 assert_select 'table.issues thead th', 3 # columns: chekbox + id + subject
896 904 assert_select 'td.description[colspan="3"]', :text => 'Unable to print recipes'
897 905
898 906 get :index, :set_filter => 1, :c => %w(subject description), :format => 'pdf'
899 907 assert_response :success
900 908 assert_equal 'application/pdf', response.content_type
901 909 end
902 910
903 911 def test_index_with_parent_column
904 912 Issue.delete_all
905 913 parent = Issue.generate!
906 914 child = Issue.generate!(:parent_issue_id => parent.id)
907 915
908 916 get :index, :c => %w(parent)
909 917
910 918 assert_select 'td.parent', :text => "#{parent.tracker} ##{parent.id}"
911 919 assert_select 'td.parent a[title=?]', parent.subject
912 920 end
913 921
914 922 def test_index_send_html_if_query_is_invalid
915 923 get :index, :f => ['start_date'], :op => {:start_date => '='}
916 924 assert_equal 'text/html', @response.content_type
917 925 assert_template 'index'
918 926 end
919 927
920 928 def test_index_send_nothing_if_query_is_invalid
921 929 get :index, :f => ['start_date'], :op => {:start_date => '='}, :format => 'csv'
922 930 assert_equal 'text/csv', @response.content_type
923 931 assert @response.body.blank?
924 932 end
925 933
926 934 def test_show_by_anonymous
927 935 get :show, :id => 1
928 936 assert_response :success
929 937 assert_template 'show'
930 938 assert_equal Issue.find(1), assigns(:issue)
931 939 assert_select 'div.issue div.description', :text => /Unable to print recipes/
932 940 # anonymous role is allowed to add a note
933 941 assert_select 'form#issue-form' do
934 942 assert_select 'fieldset' do
935 943 assert_select 'legend', :text => 'Notes'
936 944 assert_select 'textarea[name=?]', 'issue[notes]'
937 945 end
938 946 end
939 947 assert_select 'title', :text => "Bug #1: Cannot print recipes - eCookbook - Redmine"
940 948 end
941 949
942 950 def test_show_by_manager
943 951 @request.session[:user_id] = 2
944 952 get :show, :id => 1
945 953 assert_response :success
946 954 assert_select 'a', :text => /Quote/
947 955 assert_select 'form#issue-form' do
948 956 assert_select 'fieldset' do
949 957 assert_select 'legend', :text => 'Change properties'
950 958 assert_select 'input[name=?]', 'issue[subject]'
951 959 end
952 960 assert_select 'fieldset' do
953 961 assert_select 'legend', :text => 'Log time'
954 962 assert_select 'input[name=?]', 'time_entry[hours]'
955 963 end
956 964 assert_select 'fieldset' do
957 965 assert_select 'legend', :text => 'Notes'
958 966 assert_select 'textarea[name=?]', 'issue[notes]'
959 967 end
960 968 end
961 969 end
962 970
963 971 def test_show_should_display_update_form
964 972 @request.session[:user_id] = 2
965 973 get :show, :id => 1
966 974 assert_response :success
967 975
968 976 assert_select 'form#issue-form' do
969 977 assert_select 'input[name=?]', 'issue[is_private]'
970 978 assert_select 'select[name=?]', 'issue[project_id]'
971 979 assert_select 'select[name=?]', 'issue[tracker_id]'
972 980 assert_select 'input[name=?]', 'issue[subject]'
973 981 assert_select 'textarea[name=?]', 'issue[description]'
974 982 assert_select 'select[name=?]', 'issue[status_id]'
975 983 assert_select 'select[name=?]', 'issue[priority_id]'
976 984 assert_select 'select[name=?]', 'issue[assigned_to_id]'
977 985 assert_select 'select[name=?]', 'issue[category_id]'
978 986 assert_select 'select[name=?]', 'issue[fixed_version_id]'
979 987 assert_select 'input[name=?]', 'issue[parent_issue_id]'
980 988 assert_select 'input[name=?]', 'issue[start_date]'
981 989 assert_select 'input[name=?]', 'issue[due_date]'
982 990 assert_select 'select[name=?]', 'issue[done_ratio]'
983 991 assert_select 'input[name=?]', 'issue[custom_field_values][2]'
984 992 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
985 993 assert_select 'textarea[name=?]', 'issue[notes]'
986 994 end
987 995 end
988 996
989 997 def test_show_should_display_update_form_with_minimal_permissions
990 998 Role.find(1).update_attribute :permissions, [:view_issues, :add_issue_notes]
991 999 WorkflowTransition.delete_all :role_id => 1
992 1000
993 1001 @request.session[:user_id] = 2
994 1002 get :show, :id => 1
995 1003 assert_response :success
996 1004
997 1005 assert_select 'form#issue-form' do
998 1006 assert_select 'input[name=?]', 'issue[is_private]', 0
999 1007 assert_select 'select[name=?]', 'issue[project_id]', 0
1000 1008 assert_select 'select[name=?]', 'issue[tracker_id]', 0
1001 1009 assert_select 'input[name=?]', 'issue[subject]', 0
1002 1010 assert_select 'textarea[name=?]', 'issue[description]', 0
1003 1011 assert_select 'select[name=?]', 'issue[status_id]', 0
1004 1012 assert_select 'select[name=?]', 'issue[priority_id]', 0
1005 1013 assert_select 'select[name=?]', 'issue[assigned_to_id]', 0
1006 1014 assert_select 'select[name=?]', 'issue[category_id]', 0
1007 1015 assert_select 'select[name=?]', 'issue[fixed_version_id]', 0
1008 1016 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
1009 1017 assert_select 'input[name=?]', 'issue[start_date]', 0
1010 1018 assert_select 'input[name=?]', 'issue[due_date]', 0
1011 1019 assert_select 'select[name=?]', 'issue[done_ratio]', 0
1012 1020 assert_select 'input[name=?]', 'issue[custom_field_values][2]', 0
1013 1021 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
1014 1022 assert_select 'textarea[name=?]', 'issue[notes]'
1015 1023 end
1016 1024 end
1017 1025
1018 1026 def test_show_should_not_display_update_form_without_permissions
1019 1027 Role.find(1).update_attribute :permissions, [:view_issues]
1020 1028
1021 1029 @request.session[:user_id] = 2
1022 1030 get :show, :id => 1
1023 1031 assert_response :success
1024 1032
1025 1033 assert_select 'form#issue-form', 0
1026 1034 end
1027 1035
1028 1036 def test_update_form_should_not_display_inactive_enumerations
1029 1037 assert !IssuePriority.find(15).active?
1030 1038
1031 1039 @request.session[:user_id] = 2
1032 1040 get :show, :id => 1
1033 1041 assert_response :success
1034 1042
1035 1043 assert_select 'form#issue-form' do
1036 1044 assert_select 'select[name=?]', 'issue[priority_id]' do
1037 1045 assert_select 'option[value="4"]'
1038 1046 assert_select 'option[value="15"]', 0
1039 1047 end
1040 1048 end
1041 1049 end
1042 1050
1043 1051 def test_update_form_should_allow_attachment_upload
1044 1052 @request.session[:user_id] = 2
1045 1053 get :show, :id => 1
1046 1054
1047 1055 assert_select 'form#issue-form[method=post][enctype="multipart/form-data"]' do
1048 1056 assert_select 'input[type=file][name=?]', 'attachments[dummy][file]'
1049 1057 end
1050 1058 end
1051 1059
1052 1060 def test_show_should_deny_anonymous_access_without_permission
1053 1061 Role.anonymous.remove_permission!(:view_issues)
1054 1062 get :show, :id => 1
1055 1063 assert_response :redirect
1056 1064 end
1057 1065
1058 1066 def test_show_should_deny_anonymous_access_to_private_issue
1059 1067 Issue.where(:id => 1).update_all(["is_private = ?", true])
1060 1068 get :show, :id => 1
1061 1069 assert_response :redirect
1062 1070 end
1063 1071
1064 1072 def test_show_should_deny_non_member_access_without_permission
1065 1073 Role.non_member.remove_permission!(:view_issues)
1066 1074 @request.session[:user_id] = 9
1067 1075 get :show, :id => 1
1068 1076 assert_response 403
1069 1077 end
1070 1078
1071 1079 def test_show_should_deny_non_member_access_to_private_issue
1072 1080 Issue.where(:id => 1).update_all(["is_private = ?", true])
1073 1081 @request.session[:user_id] = 9
1074 1082 get :show, :id => 1
1075 1083 assert_response 403
1076 1084 end
1077 1085
1078 1086 def test_show_should_deny_member_access_without_permission
1079 1087 Role.find(1).remove_permission!(:view_issues)
1080 1088 @request.session[:user_id] = 2
1081 1089 get :show, :id => 1
1082 1090 assert_response 403
1083 1091 end
1084 1092
1085 1093 def test_show_should_deny_member_access_to_private_issue_without_permission
1086 1094 Issue.where(:id => 1).update_all(["is_private = ?", true])
1087 1095 @request.session[:user_id] = 3
1088 1096 get :show, :id => 1
1089 1097 assert_response 403
1090 1098 end
1091 1099
1092 1100 def test_show_should_allow_author_access_to_private_issue
1093 1101 Issue.where(:id => 1).update_all(["is_private = ?, author_id = 3", true])
1094 1102 @request.session[:user_id] = 3
1095 1103 get :show, :id => 1
1096 1104 assert_response :success
1097 1105 end
1098 1106
1099 1107 def test_show_should_allow_assignee_access_to_private_issue
1100 1108 Issue.where(:id => 1).update_all(["is_private = ?, assigned_to_id = 3", true])
1101 1109 @request.session[:user_id] = 3
1102 1110 get :show, :id => 1
1103 1111 assert_response :success
1104 1112 end
1105 1113
1106 1114 def test_show_should_allow_member_access_to_private_issue_with_permission
1107 1115 Issue.where(:id => 1).update_all(["is_private = ?", true])
1108 1116 User.find(3).roles_for_project(Project.find(1)).first.update_attribute :issues_visibility, 'all'
1109 1117 @request.session[:user_id] = 3
1110 1118 get :show, :id => 1
1111 1119 assert_response :success
1112 1120 end
1113 1121
1114 1122 def test_show_should_not_disclose_relations_to_invisible_issues
1115 1123 Setting.cross_project_issue_relations = '1'
1116 1124 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(2), :relation_type => 'relates')
1117 1125 # Relation to a private project issue
1118 1126 IssueRelation.create!(:issue_from => Issue.find(1), :issue_to => Issue.find(4), :relation_type => 'relates')
1119 1127
1120 1128 get :show, :id => 1
1121 1129 assert_response :success
1122 1130
1123 1131 assert_select 'div#relations' do
1124 1132 assert_select 'a', :text => /#2$/
1125 1133 assert_select 'a', :text => /#4$/, :count => 0
1126 1134 end
1127 1135 end
1128 1136
1129 1137 def test_show_should_list_subtasks
1130 1138 Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')
1131 1139
1132 1140 get :show, :id => 1
1133 1141 assert_response :success
1134 1142
1135 1143 assert_select 'div#issue_tree' do
1136 1144 assert_select 'td.subject', :text => /Child Issue/
1137 1145 end
1138 1146 end
1139 1147
1140 1148 def test_show_should_list_parents
1141 1149 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :parent_issue_id => 1, :subject => 'Child Issue')
1142 1150
1143 1151 get :show, :id => issue.id
1144 1152 assert_response :success
1145 1153
1146 1154 assert_select 'div.subject' do
1147 1155 assert_select 'h3', 'Child Issue'
1148 1156 assert_select 'a[href="/issues/1"]'
1149 1157 end
1150 1158 end
1151 1159
1152 1160 def test_show_should_not_display_prev_next_links_without_query_in_session
1153 1161 get :show, :id => 1
1154 1162 assert_response :success
1155 1163 assert_nil assigns(:prev_issue_id)
1156 1164 assert_nil assigns(:next_issue_id)
1157 1165
1158 1166 assert_select 'div.next-prev-links', 0
1159 1167 end
1160 1168
1161 1169 def test_show_should_display_prev_next_links_with_query_in_session
1162 1170 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => nil}
1163 1171 @request.session['issues_index_sort'] = 'id'
1164 1172
1165 1173 with_settings :display_subprojects_issues => '0' do
1166 1174 get :show, :id => 3
1167 1175 end
1168 1176
1169 1177 assert_response :success
1170 1178 # Previous and next issues for all projects
1171 1179 assert_equal 2, assigns(:prev_issue_id)
1172 1180 assert_equal 5, assigns(:next_issue_id)
1173 1181
1174 1182 count = Issue.open.visible.count
1175 1183
1176 1184 assert_select 'div.next-prev-links' do
1177 1185 assert_select 'a[href="/issues/2"]', :text => /Previous/
1178 1186 assert_select 'a[href="/issues/5"]', :text => /Next/
1179 1187 assert_select 'span.position', :text => "3 of #{count}"
1180 1188 end
1181 1189 end
1182 1190
1183 1191 def test_show_should_display_prev_next_links_with_saved_query_in_session
1184 1192 query = IssueQuery.create!(:name => 'test', :visibility => IssueQuery::VISIBILITY_PUBLIC, :user_id => 1,
1185 1193 :filters => {'status_id' => {:values => ['5'], :operator => '='}},
1186 1194 :sort_criteria => [['id', 'asc']])
1187 1195 @request.session[:query] = {:id => query.id, :project_id => nil}
1188 1196
1189 1197 get :show, :id => 11
1190 1198
1191 1199 assert_response :success
1192 1200 assert_equal query, assigns(:query)
1193 1201 # Previous and next issues for all projects
1194 1202 assert_equal 8, assigns(:prev_issue_id)
1195 1203 assert_equal 12, assigns(:next_issue_id)
1196 1204
1197 1205 assert_select 'div.next-prev-links' do
1198 1206 assert_select 'a[href="/issues/8"]', :text => /Previous/
1199 1207 assert_select 'a[href="/issues/12"]', :text => /Next/
1200 1208 end
1201 1209 end
1202 1210
1203 1211 def test_show_should_display_prev_next_links_with_query_and_sort_on_association
1204 1212 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => nil}
1205 1213
1206 1214 %w(project tracker status priority author assigned_to category fixed_version).each do |assoc_sort|
1207 1215 @request.session['issues_index_sort'] = assoc_sort
1208 1216
1209 1217 get :show, :id => 3
1210 1218 assert_response :success, "Wrong response status for #{assoc_sort} sort"
1211 1219
1212 1220 assert_select 'div.next-prev-links' do
1213 1221 assert_select 'a', :text => /(Previous|Next)/
1214 1222 end
1215 1223 end
1216 1224 end
1217 1225
1218 1226 def test_show_should_display_prev_next_links_with_project_query_in_session
1219 1227 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => 1}
1220 1228 @request.session['issues_index_sort'] = 'id'
1221 1229
1222 1230 with_settings :display_subprojects_issues => '0' do
1223 1231 get :show, :id => 3
1224 1232 end
1225 1233
1226 1234 assert_response :success
1227 1235 # Previous and next issues inside project
1228 1236 assert_equal 2, assigns(:prev_issue_id)
1229 1237 assert_equal 7, assigns(:next_issue_id)
1230 1238
1231 1239 assert_select 'div.next-prev-links' do
1232 1240 assert_select 'a[href="/issues/2"]', :text => /Previous/
1233 1241 assert_select 'a[href="/issues/7"]', :text => /Next/
1234 1242 end
1235 1243 end
1236 1244
1237 1245 def test_show_should_not_display_prev_link_for_first_issue
1238 1246 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'o'}}, :project_id => 1}
1239 1247 @request.session['issues_index_sort'] = 'id'
1240 1248
1241 1249 with_settings :display_subprojects_issues => '0' do
1242 1250 get :show, :id => 1
1243 1251 end
1244 1252
1245 1253 assert_response :success
1246 1254 assert_nil assigns(:prev_issue_id)
1247 1255 assert_equal 2, assigns(:next_issue_id)
1248 1256
1249 1257 assert_select 'div.next-prev-links' do
1250 1258 assert_select 'a', :text => /Previous/, :count => 0
1251 1259 assert_select 'a[href="/issues/2"]', :text => /Next/
1252 1260 end
1253 1261 end
1254 1262
1255 1263 def test_show_should_not_display_prev_next_links_for_issue_not_in_query_results
1256 1264 @request.session[:query] = {:filters => {'status_id' => {:values => [''], :operator => 'c'}}, :project_id => 1}
1257 1265 @request.session['issues_index_sort'] = 'id'
1258 1266
1259 1267 get :show, :id => 1
1260 1268
1261 1269 assert_response :success
1262 1270 assert_nil assigns(:prev_issue_id)
1263 1271 assert_nil assigns(:next_issue_id)
1264 1272
1265 1273 assert_select 'a', :text => /Previous/, :count => 0
1266 1274 assert_select 'a', :text => /Next/, :count => 0
1267 1275 end
1268 1276
1269 1277 def test_show_show_should_display_prev_next_links_with_query_sort_by_user_custom_field
1270 1278 cf = IssueCustomField.create!(:name => 'User', :is_for_all => true, :tracker_ids => [1,2,3], :field_format => 'user')
1271 1279 CustomValue.create!(:custom_field => cf, :customized => Issue.find(1), :value => '2')
1272 1280 CustomValue.create!(:custom_field => cf, :customized => Issue.find(2), :value => '3')
1273 1281 CustomValue.create!(:custom_field => cf, :customized => Issue.find(3), :value => '3')
1274 1282 CustomValue.create!(:custom_field => cf, :customized => Issue.find(5), :value => '')
1275 1283
1276 1284 query = IssueQuery.create!(:name => 'test', :visibility => IssueQuery::VISIBILITY_PUBLIC, :user_id => 1, :filters => {},
1277 1285 :sort_criteria => [["cf_#{cf.id}", 'asc'], ['id', 'asc']])
1278 1286 @request.session[:query] = {:id => query.id, :project_id => nil}
1279 1287
1280 1288 get :show, :id => 3
1281 1289 assert_response :success
1282 1290
1283 1291 assert_equal 2, assigns(:prev_issue_id)
1284 1292 assert_equal 1, assigns(:next_issue_id)
1285 1293
1286 1294 assert_select 'div.next-prev-links' do
1287 1295 assert_select 'a[href="/issues/2"]', :text => /Previous/
1288 1296 assert_select 'a[href="/issues/1"]', :text => /Next/
1289 1297 end
1290 1298 end
1291 1299
1292 1300 def test_show_should_display_link_to_the_assignee
1293 1301 get :show, :id => 2
1294 1302 assert_response :success
1295 1303 assert_select '.assigned-to' do
1296 1304 assert_select 'a[href="/users/3"]'
1297 1305 end
1298 1306 end
1299 1307
1300 1308 def test_show_should_display_visible_changesets_from_other_projects
1301 1309 project = Project.find(2)
1302 1310 issue = project.issues.first
1303 1311 issue.changeset_ids = [102]
1304 1312 issue.save!
1305 1313 # changesets from other projects should be displayed even if repository
1306 1314 # is disabled on issue's project
1307 1315 project.disable_module! :repository
1308 1316
1309 1317 @request.session[:user_id] = 2
1310 1318 get :show, :id => issue.id
1311 1319
1312 1320 assert_select 'a[href=?]', '/projects/ecookbook/repository/revisions/3'
1313 1321 end
1314 1322
1315 1323 def test_show_should_display_watchers
1316 1324 @request.session[:user_id] = 2
1317 1325 Issue.find(1).add_watcher User.find(2)
1318 1326
1319 1327 get :show, :id => 1
1320 1328 assert_select 'div#watchers ul' do
1321 1329 assert_select 'li' do
1322 1330 assert_select 'a[href="/users/2"]'
1323 1331 assert_select 'a img[alt=Delete]'
1324 1332 end
1325 1333 end
1326 1334 end
1327 1335
1328 1336 def test_show_should_display_watchers_with_gravatars
1329 1337 @request.session[:user_id] = 2
1330 1338 Issue.find(1).add_watcher User.find(2)
1331 1339
1332 1340 with_settings :gravatar_enabled => '1' do
1333 1341 get :show, :id => 1
1334 1342 end
1335 1343
1336 1344 assert_select 'div#watchers ul' do
1337 1345 assert_select 'li' do
1338 1346 assert_select 'img.gravatar'
1339 1347 assert_select 'a[href="/users/2"]'
1340 1348 assert_select 'a img[alt=Delete]'
1341 1349 end
1342 1350 end
1343 1351 end
1344 1352
1345 1353 def test_show_with_thumbnails_enabled_should_display_thumbnails
1346 1354 @request.session[:user_id] = 2
1347 1355
1348 1356 with_settings :thumbnails_enabled => '1' do
1349 1357 get :show, :id => 14
1350 1358 assert_response :success
1351 1359 end
1352 1360
1353 1361 assert_select 'div.thumbnails' do
1354 1362 assert_select 'a[href="/attachments/16/testfile.png"]' do
1355 1363 assert_select 'img[src="/attachments/thumbnail/16"]'
1356 1364 end
1357 1365 end
1358 1366 end
1359 1367
1360 1368 def test_show_with_thumbnails_disabled_should_not_display_thumbnails
1361 1369 @request.session[:user_id] = 2
1362 1370
1363 1371 with_settings :thumbnails_enabled => '0' do
1364 1372 get :show, :id => 14
1365 1373 assert_response :success
1366 1374 end
1367 1375
1368 1376 assert_select 'div.thumbnails', 0
1369 1377 end
1370 1378
1371 1379 def test_show_with_multi_custom_field
1372 1380 field = CustomField.find(1)
1373 1381 field.update_attribute :multiple, true
1374 1382 issue = Issue.find(1)
1375 1383 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
1376 1384 issue.save!
1377 1385
1378 1386 get :show, :id => 1
1379 1387 assert_response :success
1380 1388
1381 1389 assert_select 'td', :text => 'MySQL, Oracle'
1382 1390 end
1383 1391
1384 1392 def test_show_with_multi_user_custom_field
1385 1393 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1386 1394 :tracker_ids => [1], :is_for_all => true)
1387 1395 issue = Issue.find(1)
1388 1396 issue.custom_field_values = {field.id => ['2', '3']}
1389 1397 issue.save!
1390 1398
1391 1399 get :show, :id => 1
1392 1400 assert_response :success
1393 1401
1394 1402 assert_select "td.cf_#{field.id}", :text => 'Dave Lopper, John Smith' do
1395 1403 assert_select 'a', :text => 'Dave Lopper'
1396 1404 assert_select 'a', :text => 'John Smith'
1397 1405 end
1398 1406 end
1399 1407
1400 1408 def test_show_should_display_private_notes_with_permission_only
1401 1409 journal = Journal.create!(:journalized => Issue.find(2), :notes => 'Privates notes', :private_notes => true, :user_id => 1)
1402 1410 @request.session[:user_id] = 2
1403 1411
1404 1412 get :show, :id => 2
1405 1413 assert_response :success
1406 1414 assert_include journal, assigns(:journals)
1407 1415
1408 1416 Role.find(1).remove_permission! :view_private_notes
1409 1417 get :show, :id => 2
1410 1418 assert_response :success
1411 1419 assert_not_include journal, assigns(:journals)
1412 1420 end
1413 1421
1414 1422 def test_show_atom
1415 1423 get :show, :id => 2, :format => 'atom'
1416 1424 assert_response :success
1417 1425 assert_template 'journals/index'
1418 1426 # Inline image
1419 1427 assert_select 'content', :text => Regexp.new(Regexp.quote('http://test.host/attachments/download/10'))
1420 1428 end
1421 1429
1422 1430 def test_show_export_to_pdf
1423 1431 issue = Issue.find(3)
1424 1432 assert issue.relations.select{|r| r.other_issue(issue).visible?}.present?
1425 1433 get :show, :id => 3, :format => 'pdf'
1426 1434 assert_response :success
1427 1435 assert_equal 'application/pdf', @response.content_type
1428 1436 assert @response.body.starts_with?('%PDF')
1429 1437 assert_not_nil assigns(:issue)
1430 1438 end
1431 1439
1432 1440 def test_export_to_pdf_with_utf8_u_fffd
1433 1441 # U+FFFD
1434 1442 s = "\xef\xbf\xbd"
1435 1443 s.force_encoding('UTF-8') if s.respond_to?(:force_encoding)
1436 1444 issue = Issue.generate!(:subject => s)
1437 1445 ["en", "zh", "zh-TW", "ja", "ko"].each do |lang|
1438 1446 with_settings :default_language => lang do
1439 1447 get :show, :id => issue.id, :format => 'pdf'
1440 1448 assert_response :success
1441 1449 assert_equal 'application/pdf', @response.content_type
1442 1450 assert @response.body.starts_with?('%PDF')
1443 1451 assert_not_nil assigns(:issue)
1444 1452 end
1445 1453 end
1446 1454 end
1447 1455
1448 1456 def test_show_export_to_pdf_with_ancestors
1449 1457 issue = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1450 1458
1451 1459 get :show, :id => issue.id, :format => 'pdf'
1452 1460 assert_response :success
1453 1461 assert_equal 'application/pdf', @response.content_type
1454 1462 assert @response.body.starts_with?('%PDF')
1455 1463 end
1456 1464
1457 1465 def test_show_export_to_pdf_with_descendants
1458 1466 c1 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1459 1467 c2 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => 1)
1460 1468 c3 = Issue.generate!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'child', :parent_issue_id => c1.id)
1461 1469
1462 1470 get :show, :id => 1, :format => 'pdf'
1463 1471 assert_response :success
1464 1472 assert_equal 'application/pdf', @response.content_type
1465 1473 assert @response.body.starts_with?('%PDF')
1466 1474 end
1467 1475
1468 1476 def test_show_export_to_pdf_with_journals
1469 1477 get :show, :id => 1, :format => 'pdf'
1470 1478 assert_response :success
1471 1479 assert_equal 'application/pdf', @response.content_type
1472 1480 assert @response.body.starts_with?('%PDF')
1473 1481 end
1474 1482
1475 1483 def test_show_export_to_pdf_with_changesets
1476 1484 [[100], [100, 101], [100, 101, 102]].each do |cs|
1477 1485 issue1 = Issue.find(3)
1478 1486 issue1.changesets = Changeset.find(cs)
1479 1487 issue1.save!
1480 1488 issue = Issue.find(3)
1481 1489 assert_equal issue.changesets.count, cs.size
1482 1490 get :show, :id => 3, :format => 'pdf'
1483 1491 assert_response :success
1484 1492 assert_equal 'application/pdf', @response.content_type
1485 1493 assert @response.body.starts_with?('%PDF')
1486 1494 end
1487 1495 end
1488 1496
1489 1497 def test_show_invalid_should_respond_with_404
1490 1498 get :show, :id => 999
1491 1499 assert_response 404
1492 1500 end
1493 1501
1494 1502 def test_get_new
1495 1503 @request.session[:user_id] = 2
1496 1504 get :new, :project_id => 1, :tracker_id => 1
1497 1505 assert_response :success
1498 1506 assert_template 'new'
1499 1507
1500 1508 assert_select 'form#issue-form[action=?]', '/projects/ecookbook/issues'
1501 1509 assert_select 'form#issue-form' do
1502 1510 assert_select 'input[name=?]', 'issue[is_private]'
1503 1511 assert_select 'select[name=?]', 'issue[project_id]', 0
1504 1512 assert_select 'select[name=?]', 'issue[tracker_id]'
1505 1513 assert_select 'input[name=?]', 'issue[subject]'
1506 1514 assert_select 'textarea[name=?]', 'issue[description]'
1507 1515 assert_select 'select[name=?]', 'issue[status_id]'
1508 1516 assert_select 'select[name=?]', 'issue[priority_id]'
1509 1517 assert_select 'select[name=?]', 'issue[assigned_to_id]'
1510 1518 assert_select 'select[name=?]', 'issue[category_id]'
1511 1519 assert_select 'select[name=?]', 'issue[fixed_version_id]'
1512 1520 assert_select 'input[name=?]', 'issue[parent_issue_id]'
1513 1521 assert_select 'input[name=?]', 'issue[start_date]'
1514 1522 assert_select 'input[name=?]', 'issue[due_date]'
1515 1523 assert_select 'select[name=?]', 'issue[done_ratio]'
1516 1524 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Default string'
1517 1525 assert_select 'input[name=?]', 'issue[watcher_user_ids][]'
1518 1526 end
1519 1527
1520 1528 # Be sure we don't display inactive IssuePriorities
1521 1529 assert ! IssuePriority.find(15).active?
1522 1530 assert_select 'select[name=?]', 'issue[priority_id]' do
1523 1531 assert_select 'option[value="15"]', 0
1524 1532 end
1525 1533 end
1526 1534
1527 1535 def test_get_new_with_minimal_permissions
1528 1536 Role.find(1).update_attribute :permissions, [:add_issues]
1529 1537 WorkflowTransition.delete_all :role_id => 1
1530 1538
1531 1539 @request.session[:user_id] = 2
1532 1540 get :new, :project_id => 1, :tracker_id => 1
1533 1541 assert_response :success
1534 1542 assert_template 'new'
1535 1543
1536 1544 assert_select 'form#issue-form' do
1537 1545 assert_select 'input[name=?]', 'issue[is_private]', 0
1538 1546 assert_select 'select[name=?]', 'issue[project_id]', 0
1539 1547 assert_select 'select[name=?]', 'issue[tracker_id]'
1540 1548 assert_select 'input[name=?]', 'issue[subject]'
1541 1549 assert_select 'textarea[name=?]', 'issue[description]'
1542 1550 assert_select 'select[name=?]', 'issue[status_id]'
1543 1551 assert_select 'select[name=?]', 'issue[priority_id]'
1544 1552 assert_select 'select[name=?]', 'issue[assigned_to_id]'
1545 1553 assert_select 'select[name=?]', 'issue[category_id]'
1546 1554 assert_select 'select[name=?]', 'issue[fixed_version_id]'
1547 1555 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
1548 1556 assert_select 'input[name=?]', 'issue[start_date]'
1549 1557 assert_select 'input[name=?]', 'issue[due_date]'
1550 1558 assert_select 'select[name=?]', 'issue[done_ratio]'
1551 1559 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Default string'
1552 1560 assert_select 'input[name=?]', 'issue[watcher_user_ids][]', 0
1553 1561 end
1554 1562 end
1555 1563
1556 1564 def test_new_without_project_id
1557 1565 @request.session[:user_id] = 2
1558 1566 get :new
1559 1567 assert_response :success
1560 1568 assert_template 'new'
1561 1569
1562 1570 assert_select 'form#issue-form[action=?]', '/issues'
1563 1571 assert_select 'form#issue-form' do
1564 1572 assert_select 'select[name=?]', 'issue[project_id]'
1565 1573 end
1566 1574
1567 1575 assert_nil assigns(:project)
1568 1576 assert_not_nil assigns(:issue)
1569 1577 end
1570 1578
1571 1579 def test_new_should_select_default_status
1572 1580 @request.session[:user_id] = 2
1573 1581
1574 1582 get :new, :project_id => 1
1575 1583 assert_response :success
1576 1584 assert_template 'new'
1577 1585 assert_select 'select[name=?]', 'issue[status_id]' do
1578 1586 assert_select 'option[value="1"][selected=selected]'
1579 1587 end
1580 1588 assert_select 'input[name=was_default_status][value="1"]'
1581 1589 end
1582 1590
1583 1591 def test_get_new_with_list_custom_field
1584 1592 @request.session[:user_id] = 2
1585 1593 get :new, :project_id => 1, :tracker_id => 1
1586 1594 assert_response :success
1587 1595 assert_template 'new'
1588 1596
1589 1597 assert_select 'select.list_cf[name=?]', 'issue[custom_field_values][1]' do
1590 1598 assert_select 'option', 4
1591 1599 assert_select 'option[value=MySQL]', :text => 'MySQL'
1592 1600 end
1593 1601 end
1594 1602
1595 1603 def test_get_new_with_multi_custom_field
1596 1604 field = IssueCustomField.find(1)
1597 1605 field.update_attribute :multiple, true
1598 1606
1599 1607 @request.session[:user_id] = 2
1600 1608 get :new, :project_id => 1, :tracker_id => 1
1601 1609 assert_response :success
1602 1610 assert_template 'new'
1603 1611
1604 1612 assert_select 'select[name=?][multiple=multiple]', 'issue[custom_field_values][1][]' do
1605 1613 assert_select 'option', 3
1606 1614 assert_select 'option[value=MySQL]', :text => 'MySQL'
1607 1615 end
1608 1616 assert_select 'input[name=?][type=hidden][value=?]', 'issue[custom_field_values][1][]', ''
1609 1617 end
1610 1618
1611 1619 def test_get_new_with_multi_user_custom_field
1612 1620 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1613 1621 :tracker_ids => [1], :is_for_all => true)
1614 1622
1615 1623 @request.session[:user_id] = 2
1616 1624 get :new, :project_id => 1, :tracker_id => 1
1617 1625 assert_response :success
1618 1626 assert_template 'new'
1619 1627
1620 1628 assert_select 'select[name=?][multiple=multiple]', "issue[custom_field_values][#{field.id}][]" do
1621 1629 assert_select 'option', Project.find(1).users.count
1622 1630 assert_select 'option[value="2"]', :text => 'John Smith'
1623 1631 end
1624 1632 assert_select 'input[name=?][type=hidden][value=?]', "issue[custom_field_values][#{field.id}][]", ''
1625 1633 end
1626 1634
1627 1635 def test_get_new_with_date_custom_field
1628 1636 field = IssueCustomField.create!(:name => 'Date', :field_format => 'date', :tracker_ids => [1], :is_for_all => true)
1629 1637
1630 1638 @request.session[:user_id] = 2
1631 1639 get :new, :project_id => 1, :tracker_id => 1
1632 1640 assert_response :success
1633 1641
1634 1642 assert_select 'input[name=?]', "issue[custom_field_values][#{field.id}]"
1635 1643 end
1636 1644
1637 1645 def test_get_new_with_text_custom_field
1638 1646 field = IssueCustomField.create!(:name => 'Text', :field_format => 'text', :tracker_ids => [1], :is_for_all => true)
1639 1647
1640 1648 @request.session[:user_id] = 2
1641 1649 get :new, :project_id => 1, :tracker_id => 1
1642 1650 assert_response :success
1643 1651
1644 1652 assert_select 'textarea[name=?]', "issue[custom_field_values][#{field.id}]"
1645 1653 end
1646 1654
1647 1655 def test_get_new_without_default_start_date_is_creation_date
1648 1656 with_settings :default_issue_start_date_to_creation_date => 0 do
1649 1657 @request.session[:user_id] = 2
1650 1658 get :new, :project_id => 1, :tracker_id => 1
1651 1659 assert_response :success
1652 1660 assert_template 'new'
1653 1661 assert_select 'input[name=?]', 'issue[start_date]'
1654 1662 assert_select 'input[name=?][value]', 'issue[start_date]', 0
1655 1663 end
1656 1664 end
1657 1665
1658 1666 def test_get_new_with_default_start_date_is_creation_date
1659 1667 with_settings :default_issue_start_date_to_creation_date => 1 do
1660 1668 @request.session[:user_id] = 2
1661 1669 get :new, :project_id => 1, :tracker_id => 1
1662 1670 assert_response :success
1663 1671 assert_template 'new'
1664 1672 assert_select 'input[name=?][value=?]', 'issue[start_date]',
1665 1673 Date.today.to_s
1666 1674 end
1667 1675 end
1668 1676
1669 1677 def test_get_new_form_should_allow_attachment_upload
1670 1678 @request.session[:user_id] = 2
1671 1679 get :new, :project_id => 1, :tracker_id => 1
1672 1680
1673 1681 assert_select 'form[id=issue-form][method=post][enctype="multipart/form-data"]' do
1674 1682 assert_select 'input[name=?][type=file]', 'attachments[dummy][file]'
1675 1683 end
1676 1684 end
1677 1685
1678 1686 def test_get_new_should_prefill_the_form_from_params
1679 1687 @request.session[:user_id] = 2
1680 1688 get :new, :project_id => 1,
1681 1689 :issue => {:tracker_id => 3, :description => 'Prefilled', :custom_field_values => {'2' => 'Custom field value'}}
1682 1690
1683 1691 issue = assigns(:issue)
1684 1692 assert_equal 3, issue.tracker_id
1685 1693 assert_equal 'Prefilled', issue.description
1686 1694 assert_equal 'Custom field value', issue.custom_field_value(2)
1687 1695
1688 1696 assert_select 'select[name=?]', 'issue[tracker_id]' do
1689 1697 assert_select 'option[value="3"][selected=selected]'
1690 1698 end
1691 1699 assert_select 'textarea[name=?]', 'issue[description]', :text => /Prefilled/
1692 1700 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Custom field value'
1693 1701 end
1694 1702
1695 1703 def test_get_new_should_mark_required_fields
1696 1704 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1697 1705 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1698 1706 WorkflowPermission.delete_all
1699 1707 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'required')
1700 1708 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'required')
1701 1709 @request.session[:user_id] = 2
1702 1710
1703 1711 get :new, :project_id => 1
1704 1712 assert_response :success
1705 1713 assert_template 'new'
1706 1714
1707 1715 assert_select 'label[for=issue_start_date]' do
1708 1716 assert_select 'span[class=required]', 0
1709 1717 end
1710 1718 assert_select 'label[for=issue_due_date]' do
1711 1719 assert_select 'span[class=required]'
1712 1720 end
1713 1721 assert_select 'label[for=?]', "issue_custom_field_values_#{cf1.id}" do
1714 1722 assert_select 'span[class=required]', 0
1715 1723 end
1716 1724 assert_select 'label[for=?]', "issue_custom_field_values_#{cf2.id}" do
1717 1725 assert_select 'span[class=required]'
1718 1726 end
1719 1727 end
1720 1728
1721 1729 def test_get_new_should_not_display_readonly_fields
1722 1730 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1723 1731 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
1724 1732 WorkflowPermission.delete_all
1725 1733 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
1726 1734 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
1727 1735 @request.session[:user_id] = 2
1728 1736
1729 1737 get :new, :project_id => 1
1730 1738 assert_response :success
1731 1739 assert_template 'new'
1732 1740
1733 1741 assert_select 'input[name=?]', 'issue[start_date]'
1734 1742 assert_select 'input[name=?]', 'issue[due_date]', 0
1735 1743 assert_select 'input[name=?]', "issue[custom_field_values][#{cf1.id}]"
1736 1744 assert_select 'input[name=?]', "issue[custom_field_values][#{cf2.id}]", 0
1737 1745 end
1738 1746
1739 1747 def test_get_new_without_tracker_id
1740 1748 @request.session[:user_id] = 2
1741 1749 get :new, :project_id => 1
1742 1750 assert_response :success
1743 1751 assert_template 'new'
1744 1752
1745 1753 issue = assigns(:issue)
1746 1754 assert_not_nil issue
1747 1755 assert_equal Project.find(1).trackers.first, issue.tracker
1748 1756 end
1749 1757
1750 1758 def test_get_new_with_no_default_status_should_display_an_error
1751 1759 @request.session[:user_id] = 2
1752 1760 IssueStatus.delete_all
1753 1761
1754 1762 get :new, :project_id => 1
1755 1763 assert_response 500
1756 1764 assert_select_error /No default issue/
1757 1765 end
1758 1766
1759 1767 def test_get_new_with_no_tracker_should_display_an_error
1760 1768 @request.session[:user_id] = 2
1761 1769 Tracker.delete_all
1762 1770
1763 1771 get :new, :project_id => 1
1764 1772 assert_response 500
1765 1773 assert_select_error /No tracker/
1766 1774 end
1767 1775
1768 1776 def test_new_with_invalid_project_id
1769 1777 @request.session[:user_id] = 1
1770 1778 get :new, :project_id => 'invalid'
1771 1779 assert_response 404
1772 1780 end
1773 1781
1774 1782 def test_update_form_for_new_issue
1775 1783 @request.session[:user_id] = 2
1776 1784 xhr :post, :new, :project_id => 1,
1777 1785 :issue => {:tracker_id => 2,
1778 1786 :subject => 'This is the test_new issue',
1779 1787 :description => 'This is the description',
1780 1788 :priority_id => 5}
1781 1789 assert_response :success
1782 1790 assert_template 'new'
1783 1791 assert_template :partial => '_form'
1784 1792 assert_equal 'text/javascript', response.content_type
1785 1793
1786 1794 issue = assigns(:issue)
1787 1795 assert_kind_of Issue, issue
1788 1796 assert_equal 1, issue.project_id
1789 1797 assert_equal 2, issue.tracker_id
1790 1798 assert_equal 'This is the test_new issue', issue.subject
1791 1799 end
1792 1800
1793 1801 def test_update_form_for_new_issue_should_propose_transitions_based_on_initial_status
1794 1802 @request.session[:user_id] = 2
1795 1803 WorkflowTransition.delete_all
1796 1804 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 2)
1797 1805 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5)
1798 1806 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 5, :new_status_id => 4)
1799 1807
1800 1808 xhr :post, :new, :project_id => 1,
1801 1809 :issue => {:tracker_id => 1,
1802 1810 :status_id => 5,
1803 1811 :subject => 'This is an issue'}
1804 1812
1805 1813 assert_equal 5, assigns(:issue).status_id
1806 1814 assert_equal [1,2,5], assigns(:allowed_statuses).map(&:id).sort
1807 1815 end
1808 1816
1809 1817 def test_update_form_with_default_status_should_ignore_submitted_status_id_if_equals
1810 1818 @request.session[:user_id] = 2
1811 1819 tracker = Tracker.find(2)
1812 1820 tracker.update! :default_status_id => 2
1813 1821 tracker.generate_transitions! 2, 1, :clear => true
1814 1822
1815 1823 xhr :post, :new, :project_id => 1,
1816 1824 :issue => {:tracker_id => 2,
1817 1825 :status_id => 1},
1818 1826 :was_default_status => 1
1819 1827
1820 1828 assert_equal 2, assigns(:issue).status_id
1821 1829 end
1822 1830
1823 1831 def test_post_create
1824 1832 @request.session[:user_id] = 2
1825 1833 assert_difference 'Issue.count' do
1826 1834 assert_no_difference 'Journal.count' do
1827 1835 post :create, :project_id => 1,
1828 1836 :issue => {:tracker_id => 3,
1829 1837 :status_id => 2,
1830 1838 :subject => 'This is the test_new issue',
1831 1839 :description => 'This is the description',
1832 1840 :priority_id => 5,
1833 1841 :start_date => '2010-11-07',
1834 1842 :estimated_hours => '',
1835 1843 :custom_field_values => {'2' => 'Value for field 2'}}
1836 1844 end
1837 1845 end
1838 1846 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1839 1847
1840 1848 issue = Issue.find_by_subject('This is the test_new issue')
1841 1849 assert_not_nil issue
1842 1850 assert_equal 2, issue.author_id
1843 1851 assert_equal 3, issue.tracker_id
1844 1852 assert_equal 2, issue.status_id
1845 1853 assert_equal Date.parse('2010-11-07'), issue.start_date
1846 1854 assert_nil issue.estimated_hours
1847 1855 v = issue.custom_values.where(:custom_field_id => 2).first
1848 1856 assert_not_nil v
1849 1857 assert_equal 'Value for field 2', v.value
1850 1858 end
1851 1859
1852 1860 def test_post_new_with_group_assignment
1853 1861 group = Group.find(11)
1854 1862 project = Project.find(1)
1855 1863 project.members << Member.new(:principal => group, :roles => [Role.givable.first])
1856 1864
1857 1865 with_settings :issue_group_assignment => '1' do
1858 1866 @request.session[:user_id] = 2
1859 1867 assert_difference 'Issue.count' do
1860 1868 post :create, :project_id => project.id,
1861 1869 :issue => {:tracker_id => 3,
1862 1870 :status_id => 1,
1863 1871 :subject => 'This is the test_new_with_group_assignment issue',
1864 1872 :assigned_to_id => group.id}
1865 1873 end
1866 1874 end
1867 1875 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1868 1876
1869 1877 issue = Issue.find_by_subject('This is the test_new_with_group_assignment issue')
1870 1878 assert_not_nil issue
1871 1879 assert_equal group, issue.assigned_to
1872 1880 end
1873 1881
1874 1882 def test_post_create_without_start_date_and_default_start_date_is_not_creation_date
1875 1883 with_settings :default_issue_start_date_to_creation_date => 0 do
1876 1884 @request.session[:user_id] = 2
1877 1885 assert_difference 'Issue.count' do
1878 1886 post :create, :project_id => 1,
1879 1887 :issue => {:tracker_id => 3,
1880 1888 :status_id => 2,
1881 1889 :subject => 'This is the test_new issue',
1882 1890 :description => 'This is the description',
1883 1891 :priority_id => 5,
1884 1892 :estimated_hours => '',
1885 1893 :custom_field_values => {'2' => 'Value for field 2'}}
1886 1894 end
1887 1895 assert_redirected_to :controller => 'issues', :action => 'show',
1888 1896 :id => Issue.last.id
1889 1897 issue = Issue.find_by_subject('This is the test_new issue')
1890 1898 assert_not_nil issue
1891 1899 assert_nil issue.start_date
1892 1900 end
1893 1901 end
1894 1902
1895 1903 def test_post_create_without_start_date_and_default_start_date_is_creation_date
1896 1904 with_settings :default_issue_start_date_to_creation_date => 1 do
1897 1905 @request.session[:user_id] = 2
1898 1906 assert_difference 'Issue.count' do
1899 1907 post :create, :project_id => 1,
1900 1908 :issue => {:tracker_id => 3,
1901 1909 :status_id => 2,
1902 1910 :subject => 'This is the test_new issue',
1903 1911 :description => 'This is the description',
1904 1912 :priority_id => 5,
1905 1913 :estimated_hours => '',
1906 1914 :custom_field_values => {'2' => 'Value for field 2'}}
1907 1915 end
1908 1916 assert_redirected_to :controller => 'issues', :action => 'show',
1909 1917 :id => Issue.last.id
1910 1918 issue = Issue.find_by_subject('This is the test_new issue')
1911 1919 assert_not_nil issue
1912 1920 assert_equal Date.today, issue.start_date
1913 1921 end
1914 1922 end
1915 1923
1916 1924 def test_post_create_and_continue
1917 1925 @request.session[:user_id] = 2
1918 1926 assert_difference 'Issue.count' do
1919 1927 post :create, :project_id => 1,
1920 1928 :issue => {:tracker_id => 3, :subject => 'This is first issue', :priority_id => 5},
1921 1929 :continue => ''
1922 1930 end
1923 1931
1924 1932 issue = Issue.order('id DESC').first
1925 1933 assert_redirected_to :controller => 'issues', :action => 'new', :project_id => 'ecookbook', :issue => {:tracker_id => 3}
1926 1934 assert_not_nil flash[:notice], "flash was not set"
1927 1935 assert_select_in flash[:notice],
1928 1936 'a[href=?][title=?]', "/issues/#{issue.id}", "This is first issue", :text => "##{issue.id}"
1929 1937 end
1930 1938
1931 1939 def test_post_create_without_custom_fields_param
1932 1940 @request.session[:user_id] = 2
1933 1941 assert_difference 'Issue.count' do
1934 1942 post :create, :project_id => 1,
1935 1943 :issue => {:tracker_id => 1,
1936 1944 :subject => 'This is the test_new issue',
1937 1945 :description => 'This is the description',
1938 1946 :priority_id => 5}
1939 1947 end
1940 1948 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
1941 1949 end
1942 1950
1943 1951 def test_post_create_with_multi_custom_field
1944 1952 field = IssueCustomField.find_by_name('Database')
1945 1953 field.update_attribute(:multiple, true)
1946 1954
1947 1955 @request.session[:user_id] = 2
1948 1956 assert_difference 'Issue.count' do
1949 1957 post :create, :project_id => 1,
1950 1958 :issue => {:tracker_id => 1,
1951 1959 :subject => 'This is the test_new issue',
1952 1960 :description => 'This is the description',
1953 1961 :priority_id => 5,
1954 1962 :custom_field_values => {'1' => ['', 'MySQL', 'Oracle']}}
1955 1963 end
1956 1964 assert_response 302
1957 1965 issue = Issue.order('id DESC').first
1958 1966 assert_equal ['MySQL', 'Oracle'], issue.custom_field_value(1).sort
1959 1967 end
1960 1968
1961 1969 def test_post_create_with_empty_multi_custom_field
1962 1970 field = IssueCustomField.find_by_name('Database')
1963 1971 field.update_attribute(:multiple, true)
1964 1972
1965 1973 @request.session[:user_id] = 2
1966 1974 assert_difference 'Issue.count' do
1967 1975 post :create, :project_id => 1,
1968 1976 :issue => {:tracker_id => 1,
1969 1977 :subject => 'This is the test_new issue',
1970 1978 :description => 'This is the description',
1971 1979 :priority_id => 5,
1972 1980 :custom_field_values => {'1' => ['']}}
1973 1981 end
1974 1982 assert_response 302
1975 1983 issue = Issue.order('id DESC').first
1976 1984 assert_equal [''], issue.custom_field_value(1).sort
1977 1985 end
1978 1986
1979 1987 def test_post_create_with_multi_user_custom_field
1980 1988 field = IssueCustomField.create!(:name => 'Multi user', :field_format => 'user', :multiple => true,
1981 1989 :tracker_ids => [1], :is_for_all => true)
1982 1990
1983 1991 @request.session[:user_id] = 2
1984 1992 assert_difference 'Issue.count' do
1985 1993 post :create, :project_id => 1,
1986 1994 :issue => {:tracker_id => 1,
1987 1995 :subject => 'This is the test_new issue',
1988 1996 :description => 'This is the description',
1989 1997 :priority_id => 5,
1990 1998 :custom_field_values => {field.id.to_s => ['', '2', '3']}}
1991 1999 end
1992 2000 assert_response 302
1993 2001 issue = Issue.order('id DESC').first
1994 2002 assert_equal ['2', '3'], issue.custom_field_value(field).sort
1995 2003 end
1996 2004
1997 2005 def test_post_create_with_required_custom_field_and_without_custom_fields_param
1998 2006 field = IssueCustomField.find_by_name('Database')
1999 2007 field.update_attribute(:is_required, true)
2000 2008
2001 2009 @request.session[:user_id] = 2
2002 2010 assert_no_difference 'Issue.count' do
2003 2011 post :create, :project_id => 1,
2004 2012 :issue => {:tracker_id => 1,
2005 2013 :subject => 'This is the test_new issue',
2006 2014 :description => 'This is the description',
2007 2015 :priority_id => 5}
2008 2016 end
2009 2017 assert_response :success
2010 2018 assert_template 'new'
2011 2019 issue = assigns(:issue)
2012 2020 assert_not_nil issue
2013 2021 assert_select_error /Database cannot be blank/
2014 2022 end
2015 2023
2016 2024 def test_create_should_validate_required_fields
2017 2025 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2018 2026 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2019 2027 WorkflowPermission.delete_all
2020 2028 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'due_date', :rule => 'required')
2021 2029 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'required')
2022 2030 @request.session[:user_id] = 2
2023 2031
2024 2032 assert_no_difference 'Issue.count' do
2025 2033 post :create, :project_id => 1, :issue => {
2026 2034 :tracker_id => 2,
2027 2035 :status_id => 1,
2028 2036 :subject => 'Test',
2029 2037 :start_date => '',
2030 2038 :due_date => '',
2031 2039 :custom_field_values => {cf1.id.to_s => '', cf2.id.to_s => ''}
2032 2040 }
2033 2041 assert_response :success
2034 2042 assert_template 'new'
2035 2043 end
2036 2044
2037 2045 assert_select_error /Due date cannot be blank/i
2038 2046 assert_select_error /Bar cannot be blank/i
2039 2047 end
2040 2048
2041 2049 def test_create_should_ignore_readonly_fields
2042 2050 cf1 = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2043 2051 cf2 = IssueCustomField.create!(:name => 'Bar', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
2044 2052 WorkflowPermission.delete_all
2045 2053 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
2046 2054 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
2047 2055 @request.session[:user_id] = 2
2048 2056
2049 2057 assert_difference 'Issue.count' do
2050 2058 post :create, :project_id => 1, :issue => {
2051 2059 :tracker_id => 2,
2052 2060 :status_id => 1,
2053 2061 :subject => 'Test',
2054 2062 :start_date => '2012-07-14',
2055 2063 :due_date => '2012-07-16',
2056 2064 :custom_field_values => {cf1.id.to_s => 'value1', cf2.id.to_s => 'value2'}
2057 2065 }
2058 2066 assert_response 302
2059 2067 end
2060 2068
2061 2069 issue = Issue.order('id DESC').first
2062 2070 assert_equal Date.parse('2012-07-14'), issue.start_date
2063 2071 assert_nil issue.due_date
2064 2072 assert_equal 'value1', issue.custom_field_value(cf1)
2065 2073 assert_nil issue.custom_field_value(cf2)
2066 2074 end
2067 2075
2068 2076 def test_post_create_with_watchers
2069 2077 @request.session[:user_id] = 2
2070 2078 ActionMailer::Base.deliveries.clear
2071 2079
2072 2080 with_settings :notified_events => %w(issue_added) do
2073 2081 assert_difference 'Watcher.count', 2 do
2074 2082 post :create, :project_id => 1,
2075 2083 :issue => {:tracker_id => 1,
2076 2084 :subject => 'This is a new issue with watchers',
2077 2085 :description => 'This is the description',
2078 2086 :priority_id => 5,
2079 2087 :watcher_user_ids => ['2', '3']}
2080 2088 end
2081 2089 end
2082 2090 issue = Issue.find_by_subject('This is a new issue with watchers')
2083 2091 assert_not_nil issue
2084 2092 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
2085 2093
2086 2094 # Watchers added
2087 2095 assert_equal [2, 3], issue.watcher_user_ids.sort
2088 2096 assert issue.watched_by?(User.find(3))
2089 2097 # Watchers notified
2090 2098 mail = ActionMailer::Base.deliveries.last
2091 2099 assert_not_nil mail
2092 2100 assert [mail.bcc, mail.cc].flatten.include?(User.find(3).mail)
2093 2101 end
2094 2102
2095 2103 def test_post_create_subissue
2096 2104 @request.session[:user_id] = 2
2097 2105
2098 2106 assert_difference 'Issue.count' do
2099 2107 post :create, :project_id => 1,
2100 2108 :issue => {:tracker_id => 1,
2101 2109 :subject => 'This is a child issue',
2102 2110 :parent_issue_id => '2'}
2103 2111 assert_response 302
2104 2112 end
2105 2113 issue = Issue.order('id DESC').first
2106 2114 assert_equal Issue.find(2), issue.parent
2107 2115 end
2108 2116
2109 2117 def test_post_create_subissue_with_sharp_parent_id
2110 2118 @request.session[:user_id] = 2
2111 2119
2112 2120 assert_difference 'Issue.count' do
2113 2121 post :create, :project_id => 1,
2114 2122 :issue => {:tracker_id => 1,
2115 2123 :subject => 'This is a child issue',
2116 2124 :parent_issue_id => '#2'}
2117 2125 assert_response 302
2118 2126 end
2119 2127 issue = Issue.order('id DESC').first
2120 2128 assert_equal Issue.find(2), issue.parent
2121 2129 end
2122 2130
2123 2131 def test_post_create_subissue_with_non_visible_parent_id_should_not_validate
2124 2132 @request.session[:user_id] = 2
2125 2133
2126 2134 assert_no_difference 'Issue.count' do
2127 2135 post :create, :project_id => 1,
2128 2136 :issue => {:tracker_id => 1,
2129 2137 :subject => 'This is a child issue',
2130 2138 :parent_issue_id => '4'}
2131 2139
2132 2140 assert_response :success
2133 2141 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', '4'
2134 2142 assert_select_error /Parent task is invalid/i
2135 2143 end
2136 2144 end
2137 2145
2138 2146 def test_post_create_subissue_with_non_numeric_parent_id_should_not_validate
2139 2147 @request.session[:user_id] = 2
2140 2148
2141 2149 assert_no_difference 'Issue.count' do
2142 2150 post :create, :project_id => 1,
2143 2151 :issue => {:tracker_id => 1,
2144 2152 :subject => 'This is a child issue',
2145 2153 :parent_issue_id => '01ABC'}
2146 2154
2147 2155 assert_response :success
2148 2156 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', '01ABC'
2149 2157 assert_select_error /Parent task is invalid/i
2150 2158 end
2151 2159 end
2152 2160
2153 2161 def test_post_create_private
2154 2162 @request.session[:user_id] = 2
2155 2163
2156 2164 assert_difference 'Issue.count' do
2157 2165 post :create, :project_id => 1,
2158 2166 :issue => {:tracker_id => 1,
2159 2167 :subject => 'This is a private issue',
2160 2168 :is_private => '1'}
2161 2169 end
2162 2170 issue = Issue.order('id DESC').first
2163 2171 assert issue.is_private?
2164 2172 end
2165 2173
2166 2174 def test_post_create_private_with_set_own_issues_private_permission
2167 2175 role = Role.find(1)
2168 2176 role.remove_permission! :set_issues_private
2169 2177 role.add_permission! :set_own_issues_private
2170 2178
2171 2179 @request.session[:user_id] = 2
2172 2180
2173 2181 assert_difference 'Issue.count' do
2174 2182 post :create, :project_id => 1,
2175 2183 :issue => {:tracker_id => 1,
2176 2184 :subject => 'This is a private issue',
2177 2185 :is_private => '1'}
2178 2186 end
2179 2187 issue = Issue.order('id DESC').first
2180 2188 assert issue.is_private?
2181 2189 end
2182 2190
2183 2191 def test_create_without_project_id
2184 2192 @request.session[:user_id] = 2
2185 2193
2186 2194 assert_difference 'Issue.count' do
2187 2195 post :create,
2188 2196 :issue => {:project_id => 3,
2189 2197 :tracker_id => 2,
2190 2198 :subject => 'Foo'}
2191 2199 assert_response 302
2192 2200 end
2193 2201 issue = Issue.order('id DESC').first
2194 2202 assert_equal 3, issue.project_id
2195 2203 assert_equal 2, issue.tracker_id
2196 2204 end
2197 2205
2198 2206 def test_create_without_project_id_and_continue_should_redirect_without_project_id
2199 2207 @request.session[:user_id] = 2
2200 2208
2201 2209 assert_difference 'Issue.count' do
2202 2210 post :create,
2203 2211 :issue => {:project_id => 3,
2204 2212 :tracker_id => 2,
2205 2213 :subject => 'Foo'},
2206 2214 :continue => '1'
2207 2215 assert_redirected_to '/issues/new?issue%5Bproject_id%5D=3&issue%5Btracker_id%5D=2'
2208 2216 end
2209 2217 end
2210 2218
2211 2219 def test_create_without_project_id_should_be_denied_without_permission
2212 2220 Role.non_member.remove_permission! :add_issues
2213 2221 Role.anonymous.remove_permission! :add_issues
2214 2222 @request.session[:user_id] = 2
2215 2223
2216 2224 assert_no_difference 'Issue.count' do
2217 2225 post :create,
2218 2226 :issue => {:project_id => 3,
2219 2227 :tracker_id => 2,
2220 2228 :subject => 'Foo'}
2221 2229 assert_response 422
2222 2230 end
2223 2231 end
2224 2232
2225 2233 def test_create_without_project_id_with_failure
2226 2234 @request.session[:user_id] = 2
2227 2235
2228 2236 post :create,
2229 2237 :issue => {:project_id => 3,
2230 2238 :tracker_id => 2,
2231 2239 :subject => ''}
2232 2240 assert_response :success
2233 2241 assert_nil assigns(:project)
2234 2242 end
2235 2243
2236 2244 def test_post_create_should_send_a_notification
2237 2245 ActionMailer::Base.deliveries.clear
2238 2246 @request.session[:user_id] = 2
2239 2247 with_settings :notified_events => %w(issue_added) do
2240 2248 assert_difference 'Issue.count' do
2241 2249 post :create, :project_id => 1,
2242 2250 :issue => {:tracker_id => 3,
2243 2251 :subject => 'This is the test_new issue',
2244 2252 :description => 'This is the description',
2245 2253 :priority_id => 5,
2246 2254 :estimated_hours => '',
2247 2255 :custom_field_values => {'2' => 'Value for field 2'}}
2248 2256 end
2249 2257 assert_redirected_to :controller => 'issues', :action => 'show', :id => Issue.last.id
2250 2258
2251 2259 assert_equal 1, ActionMailer::Base.deliveries.size
2252 2260 end
2253 2261 end
2254 2262
2255 2263 def test_post_create_should_preserve_fields_values_on_validation_failure
2256 2264 @request.session[:user_id] = 2
2257 2265 post :create, :project_id => 1,
2258 2266 :issue => {:tracker_id => 1,
2259 2267 # empty subject
2260 2268 :subject => '',
2261 2269 :description => 'This is a description',
2262 2270 :priority_id => 6,
2263 2271 :custom_field_values => {'1' => 'Oracle', '2' => 'Value for field 2'}}
2264 2272 assert_response :success
2265 2273 assert_template 'new'
2266 2274
2267 2275 assert_select 'textarea[name=?]', 'issue[description]', :text => 'This is a description'
2268 2276 assert_select 'select[name=?]', 'issue[priority_id]' do
2269 2277 assert_select 'option[value="6"][selected=selected]', :text => 'High'
2270 2278 end
2271 2279 # Custom fields
2272 2280 assert_select 'select[name=?]', 'issue[custom_field_values][1]' do
2273 2281 assert_select 'option[value=Oracle][selected=selected]', :text => 'Oracle'
2274 2282 end
2275 2283 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', 'Value for field 2'
2276 2284 end
2277 2285
2278 2286 def test_post_create_with_failure_should_preserve_watchers
2279 2287 assert !User.find(8).member_of?(Project.find(1))
2280 2288
2281 2289 @request.session[:user_id] = 2
2282 2290 post :create, :project_id => 1,
2283 2291 :issue => {:tracker_id => 1,
2284 2292 :watcher_user_ids => ['3', '8']}
2285 2293 assert_response :success
2286 2294 assert_template 'new'
2287 2295
2288 2296 assert_select 'input[name=?][value="2"]:not(checked)', 'issue[watcher_user_ids][]'
2289 2297 assert_select 'input[name=?][value="3"][checked=checked]', 'issue[watcher_user_ids][]'
2290 2298 assert_select 'input[name=?][value="8"][checked=checked]', 'issue[watcher_user_ids][]'
2291 2299 end
2292 2300
2293 2301 def test_post_create_should_ignore_non_safe_attributes
2294 2302 @request.session[:user_id] = 2
2295 2303 assert_nothing_raised do
2296 2304 post :create, :project_id => 1, :issue => { :tracker => "A param can not be a Tracker" }
2297 2305 end
2298 2306 end
2299 2307
2300 2308 def test_post_create_with_attachment
2301 2309 set_tmp_attachments_directory
2302 2310 @request.session[:user_id] = 2
2303 2311
2304 2312 assert_difference 'Issue.count' do
2305 2313 assert_difference 'Attachment.count' do
2306 2314 assert_no_difference 'Journal.count' do
2307 2315 post :create, :project_id => 1,
2308 2316 :issue => { :tracker_id => '1', :subject => 'With attachment' },
2309 2317 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2310 2318 end
2311 2319 end
2312 2320 end
2313 2321
2314 2322 issue = Issue.order('id DESC').first
2315 2323 attachment = Attachment.order('id DESC').first
2316 2324
2317 2325 assert_equal issue, attachment.container
2318 2326 assert_equal 2, attachment.author_id
2319 2327 assert_equal 'testfile.txt', attachment.filename
2320 2328 assert_equal 'text/plain', attachment.content_type
2321 2329 assert_equal 'test file', attachment.description
2322 2330 assert_equal 59, attachment.filesize
2323 2331 assert File.exists?(attachment.diskfile)
2324 2332 assert_equal 59, File.size(attachment.diskfile)
2325 2333 end
2326 2334
2327 2335 def test_post_create_with_attachment_should_notify_with_attachments
2328 2336 ActionMailer::Base.deliveries.clear
2329 2337 set_tmp_attachments_directory
2330 2338 @request.session[:user_id] = 2
2331 2339
2332 2340 with_settings :host_name => 'mydomain.foo', :protocol => 'http', :notified_events => %w(issue_added) do
2333 2341 assert_difference 'Issue.count' do
2334 2342 post :create, :project_id => 1,
2335 2343 :issue => { :tracker_id => '1', :subject => 'With attachment' },
2336 2344 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2337 2345 end
2338 2346 end
2339 2347
2340 2348 assert_not_nil ActionMailer::Base.deliveries.last
2341 2349 assert_select_email do
2342 2350 assert_select 'a[href^=?]', 'http://mydomain.foo/attachments/download', 'testfile.txt'
2343 2351 end
2344 2352 end
2345 2353
2346 2354 def test_post_create_with_failure_should_save_attachments
2347 2355 set_tmp_attachments_directory
2348 2356 @request.session[:user_id] = 2
2349 2357
2350 2358 assert_no_difference 'Issue.count' do
2351 2359 assert_difference 'Attachment.count' do
2352 2360 post :create, :project_id => 1,
2353 2361 :issue => { :tracker_id => '1', :subject => '' },
2354 2362 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
2355 2363 assert_response :success
2356 2364 assert_template 'new'
2357 2365 end
2358 2366 end
2359 2367
2360 2368 attachment = Attachment.order('id DESC').first
2361 2369 assert_equal 'testfile.txt', attachment.filename
2362 2370 assert File.exists?(attachment.diskfile)
2363 2371 assert_nil attachment.container
2364 2372
2365 2373 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
2366 2374 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
2367 2375 end
2368 2376
2369 2377 def test_post_create_with_failure_should_keep_saved_attachments
2370 2378 set_tmp_attachments_directory
2371 2379 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
2372 2380 @request.session[:user_id] = 2
2373 2381
2374 2382 assert_no_difference 'Issue.count' do
2375 2383 assert_no_difference 'Attachment.count' do
2376 2384 post :create, :project_id => 1,
2377 2385 :issue => { :tracker_id => '1', :subject => '' },
2378 2386 :attachments => {'p0' => {'token' => attachment.token}}
2379 2387 assert_response :success
2380 2388 assert_template 'new'
2381 2389 end
2382 2390 end
2383 2391
2384 2392 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
2385 2393 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
2386 2394 end
2387 2395
2388 2396 def test_post_create_should_attach_saved_attachments
2389 2397 set_tmp_attachments_directory
2390 2398 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
2391 2399 @request.session[:user_id] = 2
2392 2400
2393 2401 assert_difference 'Issue.count' do
2394 2402 assert_no_difference 'Attachment.count' do
2395 2403 post :create, :project_id => 1,
2396 2404 :issue => { :tracker_id => '1', :subject => 'Saved attachments' },
2397 2405 :attachments => {'p0' => {'token' => attachment.token}}
2398 2406 assert_response 302
2399 2407 end
2400 2408 end
2401 2409
2402 2410 issue = Issue.order('id DESC').first
2403 2411 assert_equal 1, issue.attachments.count
2404 2412
2405 2413 attachment.reload
2406 2414 assert_equal issue, attachment.container
2407 2415 end
2408 2416
2409 2417 def setup_without_workflow_privilege
2410 2418 WorkflowTransition.delete_all(["role_id = ?", Role.anonymous.id])
2411 2419 Role.anonymous.add_permission! :add_issues, :add_issue_notes
2412 2420 end
2413 2421 private :setup_without_workflow_privilege
2414 2422
2415 2423 test "without workflow privilege #new should propose default status only" do
2416 2424 setup_without_workflow_privilege
2417 2425 get :new, :project_id => 1
2418 2426 assert_response :success
2419 2427 assert_template 'new'
2420 2428
2421 2429 issue = assigns(:issue)
2422 2430 assert_not_nil issue.default_status
2423 2431
2424 2432 assert_select 'select[name=?]', 'issue[status_id]' do
2425 2433 assert_select 'option', 1
2426 2434 assert_select 'option[value=?]', issue.default_status.id.to_s
2427 2435 end
2428 2436 end
2429 2437
2430 2438 test "without workflow privilege #create should accept default status" do
2431 2439 setup_without_workflow_privilege
2432 2440 assert_difference 'Issue.count' do
2433 2441 post :create, :project_id => 1,
2434 2442 :issue => {:tracker_id => 1,
2435 2443 :subject => 'This is an issue',
2436 2444 :status_id => 1}
2437 2445 end
2438 2446 issue = Issue.order('id').last
2439 2447 assert_not_nil issue.default_status
2440 2448 assert_equal issue.default_status, issue.status
2441 2449 end
2442 2450
2443 2451 test "without workflow privilege #create should ignore unauthorized status" do
2444 2452 setup_without_workflow_privilege
2445 2453 assert_difference 'Issue.count' do
2446 2454 post :create, :project_id => 1,
2447 2455 :issue => {:tracker_id => 1,
2448 2456 :subject => 'This is an issue',
2449 2457 :status_id => 3}
2450 2458 end
2451 2459 issue = Issue.order('id').last
2452 2460 assert_not_nil issue.default_status
2453 2461 assert_equal issue.default_status, issue.status
2454 2462 end
2455 2463
2456 2464 test "without workflow privilege #update should ignore status change" do
2457 2465 setup_without_workflow_privilege
2458 2466 assert_difference 'Journal.count' do
2459 2467 put :update, :id => 1, :issue => {:status_id => 3, :notes => 'just trying'}
2460 2468 end
2461 2469 assert_equal 1, Issue.find(1).status_id
2462 2470 end
2463 2471
2464 2472 test "without workflow privilege #update ignore attributes changes" do
2465 2473 setup_without_workflow_privilege
2466 2474 assert_difference 'Journal.count' do
2467 2475 put :update, :id => 1,
2468 2476 :issue => {:subject => 'changed', :assigned_to_id => 2,
2469 2477 :notes => 'just trying'}
2470 2478 end
2471 2479 issue = Issue.find(1)
2472 2480 assert_equal "Cannot print recipes", issue.subject
2473 2481 assert_nil issue.assigned_to
2474 2482 end
2475 2483
2476 2484 def setup_with_workflow_privilege
2477 2485 WorkflowTransition.delete_all(["role_id = ?", Role.anonymous.id])
2478 2486 WorkflowTransition.create!(:role => Role.anonymous, :tracker_id => 1,
2479 2487 :old_status_id => 1, :new_status_id => 3)
2480 2488 WorkflowTransition.create!(:role => Role.anonymous, :tracker_id => 1,
2481 2489 :old_status_id => 1, :new_status_id => 4)
2482 2490 Role.anonymous.add_permission! :add_issues, :add_issue_notes
2483 2491 end
2484 2492 private :setup_with_workflow_privilege
2485 2493
2486 2494 def setup_with_workflow_privilege_and_edit_issues_permission
2487 2495 setup_with_workflow_privilege
2488 2496 Role.anonymous.add_permission! :add_issues, :edit_issues
2489 2497 end
2490 2498 private :setup_with_workflow_privilege_and_edit_issues_permission
2491 2499
2492 2500 test "with workflow privilege and :edit_issues permission should accept authorized status" do
2493 2501 setup_with_workflow_privilege_and_edit_issues_permission
2494 2502 assert_difference 'Journal.count' do
2495 2503 put :update, :id => 1, :issue => {:status_id => 3, :notes => 'just trying'}
2496 2504 end
2497 2505 assert_equal 3, Issue.find(1).status_id
2498 2506 end
2499 2507
2500 2508 test "with workflow privilege and :edit_issues permission should ignore unauthorized status" do
2501 2509 setup_with_workflow_privilege_and_edit_issues_permission
2502 2510 assert_difference 'Journal.count' do
2503 2511 put :update, :id => 1, :issue => {:status_id => 2, :notes => 'just trying'}
2504 2512 end
2505 2513 assert_equal 1, Issue.find(1).status_id
2506 2514 end
2507 2515
2508 2516 test "with workflow privilege and :edit_issues permission should accept authorized attributes changes" do
2509 2517 setup_with_workflow_privilege_and_edit_issues_permission
2510 2518 assert_difference 'Journal.count' do
2511 2519 put :update, :id => 1,
2512 2520 :issue => {:subject => 'changed', :assigned_to_id => 2,
2513 2521 :notes => 'just trying'}
2514 2522 end
2515 2523 issue = Issue.find(1)
2516 2524 assert_equal "changed", issue.subject
2517 2525 assert_equal 2, issue.assigned_to_id
2518 2526 end
2519 2527
2520 2528 def test_new_as_copy
2521 2529 @request.session[:user_id] = 2
2522 2530 get :new, :project_id => 1, :copy_from => 1
2523 2531
2524 2532 assert_response :success
2525 2533 assert_template 'new'
2526 2534
2527 2535 assert_not_nil assigns(:issue)
2528 2536 orig = Issue.find(1)
2529 2537 assert_equal 1, assigns(:issue).project_id
2530 2538 assert_equal orig.subject, assigns(:issue).subject
2531 2539 assert assigns(:issue).copy?
2532 2540
2533 2541 assert_select 'form[id=issue-form][action="/projects/ecookbook/issues"]' do
2534 2542 assert_select 'select[name=?]', 'issue[project_id]' do
2535 2543 assert_select 'option[value="1"][selected=selected]', :text => 'eCookbook'
2536 2544 assert_select 'option[value="2"]:not([selected])', :text => 'OnlineStore'
2537 2545 end
2538 2546 assert_select 'input[name=copy_from][value="1"]'
2539 2547 end
2540 2548
2541 2549 # "New issue" menu item should not link to copy
2542 2550 assert_select '#main-menu a.new-issue[href="/projects/ecookbook/issues/new"]'
2543 2551 end
2544 2552
2545 2553 def test_new_as_copy_without_add_issues_permission_should_not_propose_current_project_as_target
2546 2554 user = setup_user_with_copy_but_not_add_permission
2547 2555
2548 2556 @request.session[:user_id] = user.id
2549 2557 get :new, :project_id => 1, :copy_from => 1
2550 2558
2551 2559 assert_response :success
2552 2560 assert_template 'new'
2553 2561 assert_select 'select[name=?]', 'issue[project_id]' do
2554 2562 assert_select 'option[value="1"]', 0
2555 2563 assert_select 'option[value="2"]', :text => 'OnlineStore'
2556 2564 end
2557 2565 end
2558 2566
2559 2567 def test_new_as_copy_with_attachments_should_show_copy_attachments_checkbox
2560 2568 @request.session[:user_id] = 2
2561 2569 issue = Issue.find(3)
2562 2570 assert issue.attachments.count > 0
2563 2571 get :new, :project_id => 1, :copy_from => 3
2564 2572
2565 2573 assert_select 'input[name=copy_attachments][type=checkbox][checked=checked][value="1"]'
2566 2574 end
2567 2575
2568 2576 def test_new_as_copy_without_attachments_should_not_show_copy_attachments_checkbox
2569 2577 @request.session[:user_id] = 2
2570 2578 issue = Issue.find(3)
2571 2579 issue.attachments.delete_all
2572 2580 get :new, :project_id => 1, :copy_from => 3
2573 2581
2574 2582 assert_select 'input[name=copy_attachments]', 0
2575 2583 end
2576 2584
2577 2585 def test_new_as_copy_with_subtasks_should_show_copy_subtasks_checkbox
2578 2586 @request.session[:user_id] = 2
2579 2587 issue = Issue.generate_with_descendants!
2580 2588 get :new, :project_id => 1, :copy_from => issue.id
2581 2589
2582 2590 assert_select 'input[type=checkbox][name=copy_subtasks][checked=checked][value="1"]'
2583 2591 end
2584 2592
2585 2593 def test_new_as_copy_with_invalid_issue_should_respond_with_404
2586 2594 @request.session[:user_id] = 2
2587 2595 get :new, :project_id => 1, :copy_from => 99999
2588 2596 assert_response 404
2589 2597 end
2590 2598
2591 2599 def test_create_as_copy_on_different_project
2592 2600 @request.session[:user_id] = 2
2593 2601 assert_difference 'Issue.count' do
2594 2602 post :create, :project_id => 1, :copy_from => 1,
2595 2603 :issue => {:project_id => '2', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
2596 2604
2597 2605 assert_not_nil assigns(:issue)
2598 2606 assert assigns(:issue).copy?
2599 2607 end
2600 2608 issue = Issue.order('id DESC').first
2601 2609 assert_redirected_to "/issues/#{issue.id}"
2602 2610
2603 2611 assert_equal 2, issue.project_id
2604 2612 assert_equal 3, issue.tracker_id
2605 2613 assert_equal 'Copy', issue.subject
2606 2614 end
2607 2615
2608 2616 def test_create_as_copy_should_copy_attachments
2609 2617 @request.session[:user_id] = 2
2610 2618 issue = Issue.find(3)
2611 2619 count = issue.attachments.count
2612 2620 assert count > 0
2613 2621 assert_difference 'Issue.count' do
2614 2622 assert_difference 'Attachment.count', count do
2615 2623 post :create, :project_id => 1, :copy_from => 3,
2616 2624 :issue => {:project_id => '1', :tracker_id => '3',
2617 2625 :status_id => '1', :subject => 'Copy with attachments'},
2618 2626 :copy_attachments => '1'
2619 2627 end
2620 2628 end
2621 2629 copy = Issue.order('id DESC').first
2622 2630 assert_equal count, copy.attachments.count
2623 2631 assert_equal issue.attachments.map(&:filename).sort, copy.attachments.map(&:filename).sort
2624 2632 end
2625 2633
2626 2634 def test_create_as_copy_without_copy_attachments_option_should_not_copy_attachments
2627 2635 @request.session[:user_id] = 2
2628 2636 issue = Issue.find(3)
2629 2637 count = issue.attachments.count
2630 2638 assert count > 0
2631 2639 assert_difference 'Issue.count' do
2632 2640 assert_no_difference 'Attachment.count' do
2633 2641 post :create, :project_id => 1, :copy_from => 3,
2634 2642 :issue => {:project_id => '1', :tracker_id => '3',
2635 2643 :status_id => '1', :subject => 'Copy with attachments'}
2636 2644 end
2637 2645 end
2638 2646 copy = Issue.order('id DESC').first
2639 2647 assert_equal 0, copy.attachments.count
2640 2648 end
2641 2649
2642 2650 def test_create_as_copy_with_attachments_should_also_add_new_files
2643 2651 @request.session[:user_id] = 2
2644 2652 issue = Issue.find(3)
2645 2653 count = issue.attachments.count
2646 2654 assert count > 0
2647 2655 assert_difference 'Issue.count' do
2648 2656 assert_difference 'Attachment.count', count + 1 do
2649 2657 post :create, :project_id => 1, :copy_from => 3,
2650 2658 :issue => {:project_id => '1', :tracker_id => '3',
2651 2659 :status_id => '1', :subject => 'Copy with attachments'},
2652 2660 :copy_attachments => '1',
2653 2661 :attachments => {'1' =>
2654 2662 {'file' => uploaded_test_file('testfile.txt', 'text/plain'),
2655 2663 'description' => 'test file'}}
2656 2664 end
2657 2665 end
2658 2666 copy = Issue.order('id DESC').first
2659 2667 assert_equal count + 1, copy.attachments.count
2660 2668 end
2661 2669
2662 2670 def test_create_as_copy_should_add_relation_with_copied_issue
2663 2671 @request.session[:user_id] = 2
2664 2672 assert_difference 'Issue.count' do
2665 2673 assert_difference 'IssueRelation.count' do
2666 2674 post :create, :project_id => 1, :copy_from => 1, :link_copy => '1',
2667 2675 :issue => {:project_id => '1', :tracker_id => '3',
2668 2676 :status_id => '1', :subject => 'Copy'}
2669 2677 end
2670 2678 end
2671 2679 copy = Issue.order('id DESC').first
2672 2680 assert_equal 1, copy.relations.size
2673 2681 end
2674 2682
2675 2683 def test_create_as_copy_should_allow_not_to_add_relation_with_copied_issue
2676 2684 @request.session[:user_id] = 2
2677 2685 assert_difference 'Issue.count' do
2678 2686 assert_no_difference 'IssueRelation.count' do
2679 2687 post :create, :project_id => 1, :copy_from => 1,
2680 2688 :issue => {:subject => 'Copy'}
2681 2689 end
2682 2690 end
2683 2691 end
2684 2692
2685 2693 def test_create_as_copy_should_always_add_relation_with_copied_issue_by_setting
2686 2694 with_settings :link_copied_issue => 'yes' do
2687 2695 @request.session[:user_id] = 2
2688 2696 assert_difference 'Issue.count' do
2689 2697 assert_difference 'IssueRelation.count' do
2690 2698 post :create, :project_id => 1, :copy_from => 1,
2691 2699 :issue => {:subject => 'Copy'}
2692 2700 end
2693 2701 end
2694 2702 end
2695 2703 end
2696 2704
2697 2705 def test_create_as_copy_should_never_add_relation_with_copied_issue_by_setting
2698 2706 with_settings :link_copied_issue => 'no' do
2699 2707 @request.session[:user_id] = 2
2700 2708 assert_difference 'Issue.count' do
2701 2709 assert_no_difference 'IssueRelation.count' do
2702 2710 post :create, :project_id => 1, :copy_from => 1, :link_copy => '1',
2703 2711 :issue => {:subject => 'Copy'}
2704 2712 end
2705 2713 end
2706 2714 end
2707 2715 end
2708 2716
2709 2717 def test_create_as_copy_should_copy_subtasks
2710 2718 @request.session[:user_id] = 2
2711 2719 issue = Issue.generate_with_descendants!
2712 2720 count = issue.descendants.count
2713 2721 assert_difference 'Issue.count', count + 1 do
2714 2722 post :create, :project_id => 1, :copy_from => issue.id,
2715 2723 :issue => {:project_id => '1', :tracker_id => '3',
2716 2724 :status_id => '1', :subject => 'Copy with subtasks'},
2717 2725 :copy_subtasks => '1'
2718 2726 end
2719 2727 copy = Issue.where(:parent_id => nil).order('id DESC').first
2720 2728 assert_equal count, copy.descendants.count
2721 2729 assert_equal issue.descendants.map(&:subject).sort, copy.descendants.map(&:subject).sort
2722 2730 end
2723 2731
2724 2732 def test_create_as_copy_without_copy_subtasks_option_should_not_copy_subtasks
2725 2733 @request.session[:user_id] = 2
2726 2734 issue = Issue.generate_with_descendants!
2727 2735 assert_difference 'Issue.count', 1 do
2728 2736 post :create, :project_id => 1, :copy_from => 3,
2729 2737 :issue => {:project_id => '1', :tracker_id => '3',
2730 2738 :status_id => '1', :subject => 'Copy with subtasks'}
2731 2739 end
2732 2740 copy = Issue.where(:parent_id => nil).order('id DESC').first
2733 2741 assert_equal 0, copy.descendants.count
2734 2742 end
2735 2743
2736 2744 def test_create_as_copy_with_failure
2737 2745 @request.session[:user_id] = 2
2738 2746 post :create, :project_id => 1, :copy_from => 1,
2739 2747 :issue => {:project_id => '2', :tracker_id => '3', :status_id => '1', :subject => ''}
2740 2748
2741 2749 assert_response :success
2742 2750 assert_template 'new'
2743 2751
2744 2752 assert_not_nil assigns(:issue)
2745 2753 assert assigns(:issue).copy?
2746 2754
2747 2755 assert_select 'form#issue-form[action="/projects/ecookbook/issues"]' do
2748 2756 assert_select 'select[name=?]', 'issue[project_id]' do
2749 2757 assert_select 'option[value="1"]:not([selected])', :text => 'eCookbook'
2750 2758 assert_select 'option[value="2"][selected=selected]', :text => 'OnlineStore'
2751 2759 end
2752 2760 assert_select 'input[name=copy_from][value="1"]'
2753 2761 end
2754 2762 end
2755 2763
2756 2764 def test_create_as_copy_on_project_without_permission_should_ignore_target_project
2757 2765 @request.session[:user_id] = 2
2758 2766 assert !User.find(2).member_of?(Project.find(4))
2759 2767
2760 2768 assert_difference 'Issue.count' do
2761 2769 post :create, :project_id => 1, :copy_from => 1,
2762 2770 :issue => {:project_id => '4', :tracker_id => '3', :status_id => '1', :subject => 'Copy'}
2763 2771 end
2764 2772 issue = Issue.order('id DESC').first
2765 2773 assert_equal 1, issue.project_id
2766 2774 end
2767 2775
2768 2776 def test_get_edit
2769 2777 @request.session[:user_id] = 2
2770 2778 get :edit, :id => 1
2771 2779 assert_response :success
2772 2780 assert_template 'edit'
2773 2781 assert_not_nil assigns(:issue)
2774 2782 assert_equal Issue.find(1), assigns(:issue)
2775 2783
2776 2784 # Be sure we don't display inactive IssuePriorities
2777 2785 assert ! IssuePriority.find(15).active?
2778 2786 assert_select 'select[name=?]', 'issue[priority_id]' do
2779 2787 assert_select 'option[value="15"]', 0
2780 2788 end
2781 2789 end
2782 2790
2783 2791 def test_get_edit_should_display_the_time_entry_form_with_log_time_permission
2784 2792 @request.session[:user_id] = 2
2785 2793 Role.find_by_name('Manager').update_attribute :permissions, [:view_issues, :edit_issues, :log_time]
2786 2794
2787 2795 get :edit, :id => 1
2788 2796 assert_select 'input[name=?]', 'time_entry[hours]'
2789 2797 end
2790 2798
2791 2799 def test_get_edit_should_not_display_the_time_entry_form_without_log_time_permission
2792 2800 @request.session[:user_id] = 2
2793 2801 Role.find_by_name('Manager').remove_permission! :log_time
2794 2802
2795 2803 get :edit, :id => 1
2796 2804 assert_select 'input[name=?]', 'time_entry[hours]', 0
2797 2805 end
2798 2806
2799 2807 def test_get_edit_with_params
2800 2808 @request.session[:user_id] = 2
2801 2809 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 },
2802 2810 :time_entry => { :hours => '2.5', :comments => 'test_get_edit_with_params', :activity_id => 10 }
2803 2811 assert_response :success
2804 2812 assert_template 'edit'
2805 2813
2806 2814 issue = assigns(:issue)
2807 2815 assert_not_nil issue
2808 2816
2809 2817 assert_equal 5, issue.status_id
2810 2818 assert_select 'select[name=?]', 'issue[status_id]' do
2811 2819 assert_select 'option[value="5"][selected=selected]', :text => 'Closed'
2812 2820 end
2813 2821
2814 2822 assert_equal 7, issue.priority_id
2815 2823 assert_select 'select[name=?]', 'issue[priority_id]' do
2816 2824 assert_select 'option[value="7"][selected=selected]', :text => 'Urgent'
2817 2825 end
2818 2826
2819 2827 assert_select 'input[name=?][value="2.5"]', 'time_entry[hours]'
2820 2828 assert_select 'select[name=?]', 'time_entry[activity_id]' do
2821 2829 assert_select 'option[value="10"][selected=selected]', :text => 'Development'
2822 2830 end
2823 2831 assert_select 'input[name=?][value=test_get_edit_with_params]', 'time_entry[comments]'
2824 2832 end
2825 2833
2826 2834 def test_get_edit_with_multi_custom_field
2827 2835 field = CustomField.find(1)
2828 2836 field.update_attribute :multiple, true
2829 2837 issue = Issue.find(1)
2830 2838 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
2831 2839 issue.save!
2832 2840
2833 2841 @request.session[:user_id] = 2
2834 2842 get :edit, :id => 1
2835 2843 assert_response :success
2836 2844 assert_template 'edit'
2837 2845
2838 2846 assert_select 'select[name=?][multiple=multiple]', 'issue[custom_field_values][1][]' do
2839 2847 assert_select 'option', 3
2840 2848 assert_select 'option[value=MySQL][selected=selected]'
2841 2849 assert_select 'option[value=Oracle][selected=selected]'
2842 2850 assert_select 'option[value=PostgreSQL]:not([selected])'
2843 2851 end
2844 2852 end
2845 2853
2846 2854 def test_update_form_for_existing_issue
2847 2855 @request.session[:user_id] = 2
2848 2856 xhr :patch, :edit, :id => 1,
2849 2857 :issue => {:tracker_id => 2,
2850 2858 :subject => 'This is the test_new issue',
2851 2859 :description => 'This is the description',
2852 2860 :priority_id => 5}
2853 2861 assert_response :success
2854 2862 assert_equal 'text/javascript', response.content_type
2855 2863 assert_template 'edit'
2856 2864 assert_template :partial => '_form'
2857 2865
2858 2866 issue = assigns(:issue)
2859 2867 assert_kind_of Issue, issue
2860 2868 assert_equal 1, issue.id
2861 2869 assert_equal 1, issue.project_id
2862 2870 assert_equal 2, issue.tracker_id
2863 2871 assert_equal 'This is the test_new issue', issue.subject
2864 2872 end
2865 2873
2866 2874 def test_update_form_for_existing_issue_should_keep_issue_author
2867 2875 @request.session[:user_id] = 3
2868 2876 xhr :patch, :edit, :id => 1, :issue => {:subject => 'Changed'}
2869 2877 assert_response :success
2870 2878 assert_equal 'text/javascript', response.content_type
2871 2879
2872 2880 issue = assigns(:issue)
2873 2881 assert_equal User.find(2), issue.author
2874 2882 assert_equal 2, issue.author_id
2875 2883 assert_not_equal User.current, issue.author
2876 2884 end
2877 2885
2878 2886 def test_update_form_for_existing_issue_should_propose_transitions_based_on_initial_status
2879 2887 @request.session[:user_id] = 2
2880 2888 WorkflowTransition.delete_all
2881 2889 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 1)
2882 2890 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 5)
2883 2891 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 5, :new_status_id => 4)
2884 2892
2885 2893 xhr :patch, :edit, :id => 2,
2886 2894 :issue => {:tracker_id => 2,
2887 2895 :status_id => 5,
2888 2896 :subject => 'This is an issue'}
2889 2897
2890 2898 assert_equal 5, assigns(:issue).status_id
2891 2899 assert_equal [1,2,5], assigns(:allowed_statuses).map(&:id).sort
2892 2900 end
2893 2901
2894 2902 def test_update_form_for_existing_issue_with_project_change
2895 2903 @request.session[:user_id] = 2
2896 2904 xhr :patch, :edit, :id => 1,
2897 2905 :issue => {:project_id => 2,
2898 2906 :tracker_id => 2,
2899 2907 :subject => 'This is the test_new issue',
2900 2908 :description => 'This is the description',
2901 2909 :priority_id => 5}
2902 2910 assert_response :success
2903 2911 assert_template :partial => '_form'
2904 2912
2905 2913 issue = assigns(:issue)
2906 2914 assert_kind_of Issue, issue
2907 2915 assert_equal 1, issue.id
2908 2916 assert_equal 2, issue.project_id
2909 2917 assert_equal 2, issue.tracker_id
2910 2918 assert_equal 'This is the test_new issue', issue.subject
2911 2919 end
2912 2920
2913 2921 def test_update_form_should_propose_default_status_for_existing_issue
2914 2922 @request.session[:user_id] = 2
2915 2923 WorkflowTransition.delete_all
2916 2924 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2, :old_status_id => 2, :new_status_id => 3)
2917 2925
2918 2926 xhr :patch, :edit, :id => 2
2919 2927 assert_response :success
2920 2928 assert_equal [2,3], assigns(:allowed_statuses).map(&:id).sort
2921 2929 end
2922 2930
2923 2931 def test_put_update_without_custom_fields_param
2924 2932 @request.session[:user_id] = 2
2925 2933
2926 2934 issue = Issue.find(1)
2927 2935 assert_equal '125', issue.custom_value_for(2).value
2928 2936
2929 2937 assert_difference('Journal.count') do
2930 2938 assert_difference('JournalDetail.count') do
2931 2939 put :update, :id => 1, :issue => {:subject => 'New subject'}
2932 2940 end
2933 2941 end
2934 2942 assert_redirected_to :action => 'show', :id => '1'
2935 2943 issue.reload
2936 2944 assert_equal 'New subject', issue.subject
2937 2945 # Make sure custom fields were not cleared
2938 2946 assert_equal '125', issue.custom_value_for(2).value
2939 2947 end
2940 2948
2941 2949 def test_put_update_with_project_change
2942 2950 @request.session[:user_id] = 2
2943 2951 ActionMailer::Base.deliveries.clear
2944 2952
2945 2953 with_settings :notified_events => %w(issue_updated) do
2946 2954 assert_difference('Journal.count') do
2947 2955 assert_difference('JournalDetail.count', 3) do
2948 2956 put :update, :id => 1, :issue => {:project_id => '2',
2949 2957 :tracker_id => '1', # no change
2950 2958 :priority_id => '6',
2951 2959 :category_id => '3'
2952 2960 }
2953 2961 end
2954 2962 end
2955 2963 end
2956 2964 assert_redirected_to :action => 'show', :id => '1'
2957 2965 issue = Issue.find(1)
2958 2966 assert_equal 2, issue.project_id
2959 2967 assert_equal 1, issue.tracker_id
2960 2968 assert_equal 6, issue.priority_id
2961 2969 assert_equal 3, issue.category_id
2962 2970
2963 2971 mail = ActionMailer::Base.deliveries.last
2964 2972 assert_not_nil mail
2965 2973 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
2966 2974 assert_mail_body_match "Project changed from eCookbook to OnlineStore", mail
2967 2975 end
2968 2976
2969 2977 def test_put_update_with_tracker_change
2970 2978 @request.session[:user_id] = 2
2971 2979 ActionMailer::Base.deliveries.clear
2972 2980
2973 2981 with_settings :notified_events => %w(issue_updated) do
2974 2982 assert_difference('Journal.count') do
2975 2983 assert_difference('JournalDetail.count', 2) do
2976 2984 put :update, :id => 1, :issue => {:project_id => '1',
2977 2985 :tracker_id => '2',
2978 2986 :priority_id => '6'
2979 2987 }
2980 2988 end
2981 2989 end
2982 2990 end
2983 2991 assert_redirected_to :action => 'show', :id => '1'
2984 2992 issue = Issue.find(1)
2985 2993 assert_equal 1, issue.project_id
2986 2994 assert_equal 2, issue.tracker_id
2987 2995 assert_equal 6, issue.priority_id
2988 2996 assert_equal 1, issue.category_id
2989 2997
2990 2998 mail = ActionMailer::Base.deliveries.last
2991 2999 assert_not_nil mail
2992 3000 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
2993 3001 assert_mail_body_match "Tracker changed from Bug to Feature request", mail
2994 3002 end
2995 3003
2996 3004 def test_put_update_with_custom_field_change
2997 3005 @request.session[:user_id] = 2
2998 3006 issue = Issue.find(1)
2999 3007 assert_equal '125', issue.custom_value_for(2).value
3000 3008
3001 3009 with_settings :notified_events => %w(issue_updated) do
3002 3010 assert_difference('Journal.count') do
3003 3011 assert_difference('JournalDetail.count', 3) do
3004 3012 put :update, :id => 1, :issue => {:subject => 'Custom field change',
3005 3013 :priority_id => '6',
3006 3014 :category_id => '1', # no change
3007 3015 :custom_field_values => { '2' => 'New custom value' }
3008 3016 }
3009 3017 end
3010 3018 end
3011 3019 end
3012 3020 assert_redirected_to :action => 'show', :id => '1'
3013 3021 issue.reload
3014 3022 assert_equal 'New custom value', issue.custom_value_for(2).value
3015 3023
3016 3024 mail = ActionMailer::Base.deliveries.last
3017 3025 assert_not_nil mail
3018 3026 assert_mail_body_match "Searchable field changed from 125 to New custom value", mail
3019 3027 end
3020 3028
3021 3029 def test_put_update_with_multi_custom_field_change
3022 3030 field = CustomField.find(1)
3023 3031 field.update_attribute :multiple, true
3024 3032 issue = Issue.find(1)
3025 3033 issue.custom_field_values = {1 => ['MySQL', 'Oracle']}
3026 3034 issue.save!
3027 3035
3028 3036 @request.session[:user_id] = 2
3029 3037 assert_difference('Journal.count') do
3030 3038 assert_difference('JournalDetail.count', 3) do
3031 3039 put :update, :id => 1,
3032 3040 :issue => {
3033 3041 :subject => 'Custom field change',
3034 3042 :custom_field_values => { '1' => ['', 'Oracle', 'PostgreSQL'] }
3035 3043 }
3036 3044 end
3037 3045 end
3038 3046 assert_redirected_to :action => 'show', :id => '1'
3039 3047 assert_equal ['Oracle', 'PostgreSQL'], Issue.find(1).custom_field_value(1).sort
3040 3048 end
3041 3049
3042 3050 def test_put_update_with_status_and_assignee_change
3043 3051 issue = Issue.find(1)
3044 3052 assert_equal 1, issue.status_id
3045 3053 @request.session[:user_id] = 2
3046 3054
3047 3055 with_settings :notified_events => %w(issue_updated) do
3048 3056 assert_difference('TimeEntry.count', 0) do
3049 3057 put :update,
3050 3058 :id => 1,
3051 3059 :issue => { :status_id => 2, :assigned_to_id => 3, :notes => 'Assigned to dlopper' },
3052 3060 :time_entry => { :hours => '', :comments => '', :activity_id => TimeEntryActivity.first }
3053 3061 end
3054 3062 end
3055 3063 assert_redirected_to :action => 'show', :id => '1'
3056 3064 issue.reload
3057 3065 assert_equal 2, issue.status_id
3058 3066 j = Journal.order('id DESC').first
3059 3067 assert_equal 'Assigned to dlopper', j.notes
3060 3068 assert_equal 2, j.details.size
3061 3069
3062 3070 mail = ActionMailer::Base.deliveries.last
3063 3071 assert_mail_body_match "Status changed from New to Assigned", mail
3064 3072 # subject should contain the new status
3065 3073 assert mail.subject.include?("(#{ IssueStatus.find(2).name })")
3066 3074 end
3067 3075
3068 3076 def test_put_update_with_note_only
3069 3077 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
3070 3078
3071 3079 with_settings :notified_events => %w(issue_updated) do
3072 3080 # anonymous user
3073 3081 put :update,
3074 3082 :id => 1,
3075 3083 :issue => { :notes => notes }
3076 3084 end
3077 3085 assert_redirected_to :action => 'show', :id => '1'
3078 3086 j = Journal.order('id DESC').first
3079 3087 assert_equal notes, j.notes
3080 3088 assert_equal 0, j.details.size
3081 3089 assert_equal User.anonymous, j.user
3082 3090
3083 3091 mail = ActionMailer::Base.deliveries.last
3084 3092 assert_mail_body_match notes, mail
3085 3093 end
3086 3094
3087 3095 def test_put_update_with_private_note_only
3088 3096 notes = 'Private note'
3089 3097 @request.session[:user_id] = 2
3090 3098
3091 3099 assert_difference 'Journal.count' do
3092 3100 put :update, :id => 1, :issue => {:notes => notes, :private_notes => '1'}
3093 3101 assert_redirected_to :action => 'show', :id => '1'
3094 3102 end
3095 3103
3096 3104 j = Journal.order('id DESC').first
3097 3105 assert_equal notes, j.notes
3098 3106 assert_equal true, j.private_notes
3099 3107 end
3100 3108
3101 3109 def test_put_update_with_private_note_and_changes
3102 3110 notes = 'Private note'
3103 3111 @request.session[:user_id] = 2
3104 3112
3105 3113 assert_difference 'Journal.count', 2 do
3106 3114 put :update, :id => 1, :issue => {:subject => 'New subject', :notes => notes, :private_notes => '1'}
3107 3115 assert_redirected_to :action => 'show', :id => '1'
3108 3116 end
3109 3117
3110 3118 j = Journal.order('id DESC').first
3111 3119 assert_equal notes, j.notes
3112 3120 assert_equal true, j.private_notes
3113 3121 assert_equal 0, j.details.count
3114 3122
3115 3123 j = Journal.order('id DESC').offset(1).first
3116 3124 assert_nil j.notes
3117 3125 assert_equal false, j.private_notes
3118 3126 assert_equal 1, j.details.count
3119 3127 end
3120 3128
3121 3129 def test_put_update_with_note_and_spent_time
3122 3130 @request.session[:user_id] = 2
3123 3131 spent_hours_before = Issue.find(1).spent_hours
3124 3132 assert_difference('TimeEntry.count') do
3125 3133 put :update,
3126 3134 :id => 1,
3127 3135 :issue => { :notes => '2.5 hours added' },
3128 3136 :time_entry => { :hours => '2.5', :comments => 'test_put_update_with_note_and_spent_time', :activity_id => TimeEntryActivity.first.id }
3129 3137 end
3130 3138 assert_redirected_to :action => 'show', :id => '1'
3131 3139
3132 3140 issue = Issue.find(1)
3133 3141
3134 3142 j = Journal.order('id DESC').first
3135 3143 assert_equal '2.5 hours added', j.notes
3136 3144 assert_equal 0, j.details.size
3137 3145
3138 3146 t = issue.time_entries.find_by_comments('test_put_update_with_note_and_spent_time')
3139 3147 assert_not_nil t
3140 3148 assert_equal 2.5, t.hours
3141 3149 assert_equal spent_hours_before + 2.5, issue.spent_hours
3142 3150 end
3143 3151
3144 3152 def test_put_update_should_preserve_parent_issue_even_if_not_visible
3145 3153 parent = Issue.generate!(:project_id => 1, :is_private => true)
3146 3154 issue = Issue.generate!(:parent_issue_id => parent.id)
3147 3155 assert !parent.visible?(User.find(3))
3148 3156 @request.session[:user_id] = 3
3149 3157
3150 3158 get :edit, :id => issue.id
3151 3159 assert_select 'input[name=?][value=?]', 'issue[parent_issue_id]', parent.id.to_s
3152 3160
3153 3161 put :update, :id => issue.id, :issue => {:subject => 'New subject', :parent_issue_id => parent.id.to_s}
3154 3162 assert_response 302
3155 3163 assert_equal parent, issue.parent
3156 3164 end
3157 3165
3158 3166 def test_put_update_with_attachment_only
3159 3167 set_tmp_attachments_directory
3160 3168
3161 3169 # Delete all fixtured journals, a race condition can occur causing the wrong
3162 3170 # journal to get fetched in the next find.
3163 3171 Journal.delete_all
3164 3172
3165 3173 with_settings :notified_events => %w(issue_updated) do
3166 3174 # anonymous user
3167 3175 assert_difference 'Attachment.count' do
3168 3176 put :update, :id => 1,
3169 3177 :issue => {:notes => ''},
3170 3178 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
3171 3179 end
3172 3180 end
3173 3181
3174 3182 assert_redirected_to :action => 'show', :id => '1'
3175 3183 j = Issue.find(1).journals.reorder('id DESC').first
3176 3184 assert j.notes.blank?
3177 3185 assert_equal 1, j.details.size
3178 3186 assert_equal 'testfile.txt', j.details.first.value
3179 3187 assert_equal User.anonymous, j.user
3180 3188
3181 3189 attachment = Attachment.order('id DESC').first
3182 3190 assert_equal Issue.find(1), attachment.container
3183 3191 assert_equal User.anonymous, attachment.author
3184 3192 assert_equal 'testfile.txt', attachment.filename
3185 3193 assert_equal 'text/plain', attachment.content_type
3186 3194 assert_equal 'test file', attachment.description
3187 3195 assert_equal 59, attachment.filesize
3188 3196 assert File.exists?(attachment.diskfile)
3189 3197 assert_equal 59, File.size(attachment.diskfile)
3190 3198
3191 3199 mail = ActionMailer::Base.deliveries.last
3192 3200 assert_mail_body_match 'testfile.txt', mail
3193 3201 end
3194 3202
3195 3203 def test_put_update_with_failure_should_save_attachments
3196 3204 set_tmp_attachments_directory
3197 3205 @request.session[:user_id] = 2
3198 3206
3199 3207 assert_no_difference 'Journal.count' do
3200 3208 assert_difference 'Attachment.count' do
3201 3209 put :update, :id => 1,
3202 3210 :issue => { :subject => '' },
3203 3211 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
3204 3212 assert_response :success
3205 3213 assert_template 'edit'
3206 3214 end
3207 3215 end
3208 3216
3209 3217 attachment = Attachment.order('id DESC').first
3210 3218 assert_equal 'testfile.txt', attachment.filename
3211 3219 assert File.exists?(attachment.diskfile)
3212 3220 assert_nil attachment.container
3213 3221
3214 3222 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
3215 3223 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
3216 3224 end
3217 3225
3218 3226 def test_put_update_with_failure_should_keep_saved_attachments
3219 3227 set_tmp_attachments_directory
3220 3228 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
3221 3229 @request.session[:user_id] = 2
3222 3230
3223 3231 assert_no_difference 'Journal.count' do
3224 3232 assert_no_difference 'Attachment.count' do
3225 3233 put :update, :id => 1,
3226 3234 :issue => { :subject => '' },
3227 3235 :attachments => {'p0' => {'token' => attachment.token}}
3228 3236 assert_response :success
3229 3237 assert_template 'edit'
3230 3238 end
3231 3239 end
3232 3240
3233 3241 assert_select 'input[name=?][value=?]', 'attachments[p0][token]', attachment.token
3234 3242 assert_select 'input[name=?][value=?]', 'attachments[p0][filename]', 'testfile.txt'
3235 3243 end
3236 3244
3237 3245 def test_put_update_should_attach_saved_attachments
3238 3246 set_tmp_attachments_directory
3239 3247 attachment = Attachment.create!(:file => uploaded_test_file("testfile.txt", "text/plain"), :author_id => 2)
3240 3248 @request.session[:user_id] = 2
3241 3249
3242 3250 assert_difference 'Journal.count' do
3243 3251 assert_difference 'JournalDetail.count' do
3244 3252 assert_no_difference 'Attachment.count' do
3245 3253 put :update, :id => 1,
3246 3254 :issue => {:notes => 'Attachment added'},
3247 3255 :attachments => {'p0' => {'token' => attachment.token}}
3248 3256 assert_redirected_to '/issues/1'
3249 3257 end
3250 3258 end
3251 3259 end
3252 3260
3253 3261 attachment.reload
3254 3262 assert_equal Issue.find(1), attachment.container
3255 3263
3256 3264 journal = Journal.order('id DESC').first
3257 3265 assert_equal 1, journal.details.size
3258 3266 assert_equal 'testfile.txt', journal.details.first.value
3259 3267 end
3260 3268
3261 3269 def test_put_update_with_attachment_that_fails_to_save
3262 3270 set_tmp_attachments_directory
3263 3271
3264 3272 # anonymous user
3265 3273 with_settings :attachment_max_size => 0 do
3266 3274 put :update,
3267 3275 :id => 1,
3268 3276 :issue => {:notes => ''},
3269 3277 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
3270 3278 assert_redirected_to :action => 'show', :id => '1'
3271 3279 assert_equal '1 file(s) could not be saved.', flash[:warning]
3272 3280 end
3273 3281 end
3274 3282
3275 3283 def test_put_update_with_no_change
3276 3284 issue = Issue.find(1)
3277 3285 issue.journals.clear
3278 3286 ActionMailer::Base.deliveries.clear
3279 3287
3280 3288 put :update,
3281 3289 :id => 1,
3282 3290 :issue => {:notes => ''}
3283 3291 assert_redirected_to :action => 'show', :id => '1'
3284 3292
3285 3293 issue.reload
3286 3294 assert issue.journals.empty?
3287 3295 # No email should be sent
3288 3296 assert ActionMailer::Base.deliveries.empty?
3289 3297 end
3290 3298
3291 3299 def test_put_update_should_send_a_notification
3292 3300 @request.session[:user_id] = 2
3293 3301 ActionMailer::Base.deliveries.clear
3294 3302 issue = Issue.find(1)
3295 3303 old_subject = issue.subject
3296 3304 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
3297 3305
3298 3306 with_settings :notified_events => %w(issue_updated) do
3299 3307 put :update, :id => 1, :issue => {:subject => new_subject,
3300 3308 :priority_id => '6',
3301 3309 :category_id => '1' # no change
3302 3310 }
3303 3311 assert_equal 1, ActionMailer::Base.deliveries.size
3304 3312 end
3305 3313 end
3306 3314
3307 3315 def test_put_update_with_invalid_spent_time_hours_only
3308 3316 @request.session[:user_id] = 2
3309 3317 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
3310 3318
3311 3319 assert_no_difference('Journal.count') do
3312 3320 put :update,
3313 3321 :id => 1,
3314 3322 :issue => {:notes => notes},
3315 3323 :time_entry => {"comments"=>"", "activity_id"=>"", "hours"=>"2z"}
3316 3324 end
3317 3325 assert_response :success
3318 3326 assert_template 'edit'
3319 3327
3320 3328 assert_select_error /Activity cannot be blank/
3321 3329 assert_select 'textarea[name=?]', 'issue[notes]', :text => notes
3322 3330 assert_select 'input[name=?][value=?]', 'time_entry[hours]', '2z'
3323 3331 end
3324 3332
3325 3333 def test_put_update_with_invalid_spent_time_comments_only
3326 3334 @request.session[:user_id] = 2
3327 3335 notes = 'Note added by IssuesControllerTest#test_post_edit_with_invalid_spent_time'
3328 3336
3329 3337 assert_no_difference('Journal.count') do
3330 3338 put :update,
3331 3339 :id => 1,
3332 3340 :issue => {:notes => notes},
3333 3341 :time_entry => {"comments"=>"this is my comment", "activity_id"=>"", "hours"=>""}
3334 3342 end
3335 3343 assert_response :success
3336 3344 assert_template 'edit'
3337 3345
3338 3346 assert_select_error /Activity cannot be blank/
3339 3347 assert_select_error /Hours cannot be blank/
3340 3348 assert_select 'textarea[name=?]', 'issue[notes]', :text => notes
3341 3349 assert_select 'input[name=?][value=?]', 'time_entry[comments]', 'this is my comment'
3342 3350 end
3343 3351
3344 3352 def test_put_update_should_allow_fixed_version_to_be_set_to_a_subproject
3345 3353 issue = Issue.find(2)
3346 3354 @request.session[:user_id] = 2
3347 3355
3348 3356 put :update,
3349 3357 :id => issue.id,
3350 3358 :issue => {
3351 3359 :fixed_version_id => 4
3352 3360 }
3353 3361
3354 3362 assert_response :redirect
3355 3363 issue.reload
3356 3364 assert_equal 4, issue.fixed_version_id
3357 3365 assert_not_equal issue.project_id, issue.fixed_version.project_id
3358 3366 end
3359 3367
3360 3368 def test_put_update_should_redirect_back_using_the_back_url_parameter
3361 3369 issue = Issue.find(2)
3362 3370 @request.session[:user_id] = 2
3363 3371
3364 3372 put :update,
3365 3373 :id => issue.id,
3366 3374 :issue => {
3367 3375 :fixed_version_id => 4
3368 3376 },
3369 3377 :back_url => '/issues'
3370 3378
3371 3379 assert_response :redirect
3372 3380 assert_redirected_to '/issues'
3373 3381 end
3374 3382
3375 3383 def test_put_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
3376 3384 issue = Issue.find(2)
3377 3385 @request.session[:user_id] = 2
3378 3386
3379 3387 put :update,
3380 3388 :id => issue.id,
3381 3389 :issue => {
3382 3390 :fixed_version_id => 4
3383 3391 },
3384 3392 :back_url => 'http://google.com'
3385 3393
3386 3394 assert_response :redirect
3387 3395 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue.id
3388 3396 end
3389 3397
3390 3398 def test_get_bulk_edit
3391 3399 @request.session[:user_id] = 2
3392 3400 get :bulk_edit, :ids => [1, 3]
3393 3401 assert_response :success
3394 3402 assert_template 'bulk_edit'
3395 3403
3396 3404 assert_select 'ul#bulk-selection' do
3397 3405 assert_select 'li', 2
3398 3406 assert_select 'li a', :text => 'Bug #1'
3399 3407 end
3400 3408
3401 3409 assert_select 'form#bulk_edit_form[action=?]', '/issues/bulk_update' do
3402 3410 assert_select 'input[name=?]', 'ids[]', 2
3403 3411 assert_select 'input[name=?][value="1"][type=hidden]', 'ids[]'
3404 3412
3405 3413 assert_select 'select[name=?]', 'issue[project_id]'
3406 3414 assert_select 'input[name=?]', 'issue[parent_issue_id]'
3407 3415
3408 3416 # Project specific custom field, date type
3409 3417 field = CustomField.find(9)
3410 3418 assert !field.is_for_all?
3411 3419 assert_equal 'date', field.field_format
3412 3420 assert_select 'input[name=?]', 'issue[custom_field_values][9]'
3413 3421
3414 3422 # System wide custom field
3415 3423 assert CustomField.find(1).is_for_all?
3416 3424 assert_select 'select[name=?]', 'issue[custom_field_values][1]'
3417 3425
3418 3426 # Be sure we don't display inactive IssuePriorities
3419 3427 assert ! IssuePriority.find(15).active?
3420 3428 assert_select 'select[name=?]', 'issue[priority_id]' do
3421 3429 assert_select 'option[value="15"]', 0
3422 3430 end
3423 3431 end
3424 3432 end
3425 3433
3426 3434 def test_get_bulk_edit_on_different_projects
3427 3435 @request.session[:user_id] = 2
3428 3436 get :bulk_edit, :ids => [1, 2, 6]
3429 3437 assert_response :success
3430 3438 assert_template 'bulk_edit'
3431 3439
3432 3440 # Can not set issues from different projects as children of an issue
3433 3441 assert_select 'input[name=?]', 'issue[parent_issue_id]', 0
3434 3442
3435 3443 # Project specific custom field, date type
3436 3444 field = CustomField.find(9)
3437 3445 assert !field.is_for_all?
3438 3446 assert !field.project_ids.include?(Issue.find(6).project_id)
3439 3447 assert_select 'input[name=?]', 'issue[custom_field_values][9]', 0
3440 3448 end
3441 3449
3442 3450 def test_get_bulk_edit_with_user_custom_field
3443 3451 field = IssueCustomField.create!(:name => 'Tester', :field_format => 'user', :is_for_all => true, :tracker_ids => [1,2,3])
3444 3452
3445 3453 @request.session[:user_id] = 2
3446 3454 get :bulk_edit, :ids => [1, 2]
3447 3455 assert_response :success
3448 3456 assert_template 'bulk_edit'
3449 3457
3450 3458 assert_select 'select.user_cf[name=?]', "issue[custom_field_values][#{field.id}]" do
3451 3459 assert_select 'option', Project.find(1).users.count + 2 # "no change" + "none" options
3452 3460 end
3453 3461 end
3454 3462
3455 3463 def test_get_bulk_edit_with_version_custom_field
3456 3464 field = IssueCustomField.create!(:name => 'Affected version', :field_format => 'version', :is_for_all => true, :tracker_ids => [1,2,3])
3457 3465
3458 3466 @request.session[:user_id] = 2
3459 3467 get :bulk_edit, :ids => [1, 2]
3460 3468 assert_response :success
3461 3469 assert_template 'bulk_edit'
3462 3470
3463 3471 assert_select 'select.version_cf[name=?]', "issue[custom_field_values][#{field.id}]" do
3464 3472 assert_select 'option', Project.find(1).shared_versions.count + 2 # "no change" + "none" options
3465 3473 end
3466 3474 end
3467 3475
3468 3476 def test_get_bulk_edit_with_multi_custom_field
3469 3477 field = CustomField.find(1)
3470 3478 field.update_attribute :multiple, true
3471 3479
3472 3480 @request.session[:user_id] = 2
3473 3481 get :bulk_edit, :ids => [1, 3]
3474 3482 assert_response :success
3475 3483 assert_template 'bulk_edit'
3476 3484
3477 3485 assert_select 'select[name=?]', 'issue[custom_field_values][1][]' do
3478 3486 assert_select 'option', field.possible_values.size + 1 # "none" options
3479 3487 end
3480 3488 end
3481 3489
3482 3490 def test_bulk_edit_should_propose_to_clear_text_custom_fields
3483 3491 @request.session[:user_id] = 2
3484 3492 get :bulk_edit, :ids => [1, 3]
3485 3493 assert_select 'input[name=?][value=?]', 'issue[custom_field_values][2]', '__none__'
3486 3494 end
3487 3495
3488 3496 def test_bulk_edit_should_only_propose_statuses_allowed_for_all_issues
3489 3497 WorkflowTransition.delete_all
3490 3498 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
3491 3499 :old_status_id => 1, :new_status_id => 1)
3492 3500 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
3493 3501 :old_status_id => 1, :new_status_id => 3)
3494 3502 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1,
3495 3503 :old_status_id => 1, :new_status_id => 4)
3496 3504 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2,
3497 3505 :old_status_id => 2, :new_status_id => 1)
3498 3506 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2,
3499 3507 :old_status_id => 2, :new_status_id => 3)
3500 3508 WorkflowTransition.create!(:role_id => 1, :tracker_id => 2,
3501 3509 :old_status_id => 2, :new_status_id => 5)
3502 3510 @request.session[:user_id] = 2
3503 3511 get :bulk_edit, :ids => [1, 2]
3504 3512
3505 3513 assert_response :success
3506 3514 statuses = assigns(:available_statuses)
3507 3515 assert_not_nil statuses
3508 3516 assert_equal [1, 3], statuses.map(&:id).sort
3509 3517
3510 3518 assert_select 'select[name=?]', 'issue[status_id]' do
3511 3519 assert_select 'option', 3 # 2 statuses + "no change" option
3512 3520 end
3513 3521 end
3514 3522
3515 3523 def test_bulk_edit_should_propose_target_project_open_shared_versions
3516 3524 @request.session[:user_id] = 2
3517 3525 post :bulk_edit, :ids => [1, 2, 6], :issue => {:project_id => 1}
3518 3526 assert_response :success
3519 3527 assert_template 'bulk_edit'
3520 3528 assert_equal Project.find(1).shared_versions.open.to_a.sort, assigns(:versions).sort
3521 3529
3522 3530 assert_select 'select[name=?]', 'issue[fixed_version_id]' do
3523 3531 assert_select 'option', :text => '2.0'
3524 3532 end
3525 3533 end
3526 3534
3527 3535 def test_bulk_edit_should_propose_target_project_categories
3528 3536 @request.session[:user_id] = 2
3529 3537 post :bulk_edit, :ids => [1, 2, 6], :issue => {:project_id => 1}
3530 3538 assert_response :success
3531 3539 assert_template 'bulk_edit'
3532 3540 assert_equal Project.find(1).issue_categories.sort, assigns(:categories).sort
3533 3541
3534 3542 assert_select 'select[name=?]', 'issue[category_id]' do
3535 3543 assert_select 'option', :text => 'Recipes'
3536 3544 end
3537 3545 end
3538 3546
3539 3547 def test_bulk_edit_should_only_propose_issues_trackers_custom_fields
3540 3548 IssueCustomField.delete_all
3541 3549 field = IssueCustomField.generate!(:tracker_ids => [1], :is_for_all => true)
3542 3550 IssueCustomField.generate!(:tracker_ids => [2], :is_for_all => true)
3543 3551 @request.session[:user_id] = 2
3544 3552
3545 3553 issue_ids = Issue.where(:project_id => 1, :tracker_id => 1).limit(2).ids
3546 3554 get :bulk_edit, :ids => issue_ids
3547 3555 assert_equal [field], assigns(:custom_fields)
3548 3556 end
3549 3557
3550 3558 def test_bulk_update
3551 3559 @request.session[:user_id] = 2
3552 3560 # update issues priority
3553 3561 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
3554 3562 :issue => {:priority_id => 7,
3555 3563 :assigned_to_id => '',
3556 3564 :custom_field_values => {'2' => ''}}
3557 3565
3558 3566 assert_response 302
3559 3567 # check that the issues were updated
3560 3568 assert_equal [7, 7], Issue.where(:id =>[1, 2]).collect {|i| i.priority.id}
3561 3569
3562 3570 issue = Issue.find(1)
3563 3571 journal = issue.journals.reorder('created_on DESC').first
3564 3572 assert_equal '125', issue.custom_value_for(2).value
3565 3573 assert_equal 'Bulk editing', journal.notes
3566 3574 assert_equal 1, journal.details.size
3567 3575 end
3568 3576
3569 3577 def test_bulk_update_with_group_assignee
3570 3578 group = Group.find(11)
3571 3579 project = Project.find(1)
3572 3580 project.members << Member.new(:principal => group, :roles => [Role.givable.first])
3573 3581
3574 3582 @request.session[:user_id] = 2
3575 3583 # update issues assignee
3576 3584 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing',
3577 3585 :issue => {:priority_id => '',
3578 3586 :assigned_to_id => group.id,
3579 3587 :custom_field_values => {'2' => ''}}
3580 3588
3581 3589 assert_response 302
3582 3590 assert_equal [group, group], Issue.where(:id => [1, 2]).collect {|i| i.assigned_to}
3583 3591 end
3584 3592
3585 3593 def test_bulk_update_on_different_projects
3586 3594 @request.session[:user_id] = 2
3587 3595 # update issues priority
3588 3596 post :bulk_update, :ids => [1, 2, 6], :notes => 'Bulk editing',
3589 3597 :issue => {:priority_id => 7,
3590 3598 :assigned_to_id => '',
3591 3599 :custom_field_values => {'2' => ''}}
3592 3600
3593 3601 assert_response 302
3594 3602 # check that the issues were updated
3595 3603 assert_equal [7, 7, 7], Issue.find([1,2,6]).map(&:priority_id)
3596 3604
3597 3605 issue = Issue.find(1)
3598 3606 journal = issue.journals.reorder('created_on DESC').first
3599 3607 assert_equal '125', issue.custom_value_for(2).value
3600 3608 assert_equal 'Bulk editing', journal.notes
3601 3609 assert_equal 1, journal.details.size
3602 3610 end
3603 3611
3604 3612 def test_bulk_update_on_different_projects_without_rights
3605 3613 @request.session[:user_id] = 3
3606 3614 user = User.find(3)
3607 3615 action = { :controller => "issues", :action => "bulk_update" }
3608 3616 assert user.allowed_to?(action, Issue.find(1).project)
3609 3617 assert ! user.allowed_to?(action, Issue.find(6).project)
3610 3618 post :bulk_update, :ids => [1, 6], :notes => 'Bulk should fail',
3611 3619 :issue => {:priority_id => 7,
3612 3620 :assigned_to_id => '',
3613 3621 :custom_field_values => {'2' => ''}}
3614 3622 assert_response 403
3615 3623 assert_not_equal "Bulk should fail", Journal.last.notes
3616 3624 end
3617 3625
3618 3626 def test_bullk_update_should_send_a_notification
3619 3627 @request.session[:user_id] = 2
3620 3628 ActionMailer::Base.deliveries.clear
3621 3629 with_settings :notified_events => %w(issue_updated) do
3622 3630 post(:bulk_update,
3623 3631 {
3624 3632 :ids => [1, 2],
3625 3633 :notes => 'Bulk editing',
3626 3634 :issue => {
3627 3635 :priority_id => 7,
3628 3636 :assigned_to_id => '',
3629 3637 :custom_field_values => {'2' => ''}
3630 3638 }
3631 3639 })
3632 3640 assert_response 302
3633 3641 assert_equal 2, ActionMailer::Base.deliveries.size
3634 3642 end
3635 3643 end
3636 3644
3637 3645 def test_bulk_update_project
3638 3646 @request.session[:user_id] = 2
3639 3647 post :bulk_update, :ids => [1, 2], :issue => {:project_id => '2'}
3640 3648 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3641 3649 # Issues moved to project 2
3642 3650 assert_equal 2, Issue.find(1).project_id
3643 3651 assert_equal 2, Issue.find(2).project_id
3644 3652 # No tracker change
3645 3653 assert_equal 1, Issue.find(1).tracker_id
3646 3654 assert_equal 2, Issue.find(2).tracker_id
3647 3655 end
3648 3656
3649 3657 def test_bulk_update_project_on_single_issue_should_follow_when_needed
3650 3658 @request.session[:user_id] = 2
3651 3659 post :bulk_update, :id => 1, :issue => {:project_id => '2'}, :follow => '1'
3652 3660 assert_redirected_to '/issues/1'
3653 3661 end
3654 3662
3655 3663 def test_bulk_update_project_on_multiple_issues_should_follow_when_needed
3656 3664 @request.session[:user_id] = 2
3657 3665 post :bulk_update, :id => [1, 2], :issue => {:project_id => '2'}, :follow => '1'
3658 3666 assert_redirected_to '/projects/onlinestore/issues'
3659 3667 end
3660 3668
3661 3669 def test_bulk_update_tracker
3662 3670 @request.session[:user_id] = 2
3663 3671 post :bulk_update, :ids => [1, 2], :issue => {:tracker_id => '2'}
3664 3672 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3665 3673 assert_equal 2, Issue.find(1).tracker_id
3666 3674 assert_equal 2, Issue.find(2).tracker_id
3667 3675 end
3668 3676
3669 3677 def test_bulk_update_status
3670 3678 @request.session[:user_id] = 2
3671 3679 # update issues priority
3672 3680 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing status',
3673 3681 :issue => {:priority_id => '',
3674 3682 :assigned_to_id => '',
3675 3683 :status_id => '5'}
3676 3684
3677 3685 assert_response 302
3678 3686 issue = Issue.find(1)
3679 3687 assert issue.closed?
3680 3688 end
3681 3689
3682 3690 def test_bulk_update_priority
3683 3691 @request.session[:user_id] = 2
3684 3692 post :bulk_update, :ids => [1, 2], :issue => {:priority_id => 6}
3685 3693
3686 3694 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3687 3695 assert_equal 6, Issue.find(1).priority_id
3688 3696 assert_equal 6, Issue.find(2).priority_id
3689 3697 end
3690 3698
3691 3699 def test_bulk_update_with_notes
3692 3700 @request.session[:user_id] = 2
3693 3701 post :bulk_update, :ids => [1, 2], :notes => 'Moving two issues'
3694 3702
3695 3703 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => 'ecookbook'
3696 3704 assert_equal 'Moving two issues', Issue.find(1).journals.sort_by(&:id).last.notes
3697 3705 assert_equal 'Moving two issues', Issue.find(2).journals.sort_by(&:id).last.notes
3698 3706 end
3699 3707
3700 3708 def test_bulk_update_parent_id
3701 3709 IssueRelation.delete_all
3702 3710 @request.session[:user_id] = 2
3703 3711 post :bulk_update, :ids => [1, 3],
3704 3712 :notes => 'Bulk editing parent',
3705 3713 :issue => {:priority_id => '', :assigned_to_id => '',
3706 3714 :status_id => '', :parent_issue_id => '2'}
3707 3715 assert_response 302
3708 3716 parent = Issue.find(2)
3709 3717 assert_equal parent.id, Issue.find(1).parent_id
3710 3718 assert_equal parent.id, Issue.find(3).parent_id
3711 3719 assert_equal [1, 3], parent.children.collect(&:id).sort
3712 3720 end
3713 3721
3714 3722 def test_bulk_update_custom_field
3715 3723 @request.session[:user_id] = 2
3716 3724 # update issues priority
3717 3725 post :bulk_update, :ids => [1, 2], :notes => 'Bulk editing custom field',
3718 3726 :issue => {:priority_id => '',
3719 3727 :assigned_to_id => '',
3720 3728 :custom_field_values => {'2' => '777'}}
3721 3729
3722 3730 assert_response 302
3723 3731
3724 3732 issue = Issue.find(1)
3725 3733 journal = issue.journals.reorder('created_on DESC').first
3726 3734 assert_equal '777', issue.custom_value_for(2).value
3727 3735 assert_equal 1, journal.details.size
3728 3736 assert_equal '125', journal.details.first.old_value
3729 3737 assert_equal '777', journal.details.first.value
3730 3738 end
3731 3739
3732 3740 def test_bulk_update_custom_field_to_blank
3733 3741 @request.session[:user_id] = 2
3734 3742 post :bulk_update, :ids => [1, 3], :notes => 'Bulk editing custom field',
3735 3743 :issue => {:priority_id => '',
3736 3744 :assigned_to_id => '',
3737 3745 :custom_field_values => {'1' => '__none__'}}
3738 3746 assert_response 302
3739 3747 assert_equal '', Issue.find(1).custom_field_value(1)
3740 3748 assert_equal '', Issue.find(3).custom_field_value(1)
3741 3749 end
3742 3750
3743 3751 def test_bulk_update_multi_custom_field
3744 3752 field = CustomField.find(1)
3745 3753 field.update_attribute :multiple, true
3746 3754
3747 3755 @request.session[:user_id] = 2
3748 3756 post :bulk_update, :ids => [1, 2, 3], :notes => 'Bulk editing multi custom field',
3749 3757 :issue => {:priority_id => '',
3750 3758 :assigned_to_id => '',
3751 3759 :custom_field_values => {'1' => ['MySQL', 'Oracle']}}
3752 3760
3753 3761 assert_response 302
3754 3762
3755 3763 assert_equal ['MySQL', 'Oracle'], Issue.find(1).custom_field_value(1).sort
3756 3764 assert_equal ['MySQL', 'Oracle'], Issue.find(3).custom_field_value(1).sort
3757 3765 # the custom field is not associated with the issue tracker
3758 3766 assert_nil Issue.find(2).custom_field_value(1)
3759 3767 end
3760 3768
3761 3769 def test_bulk_update_multi_custom_field_to_blank
3762 3770 field = CustomField.find(1)
3763 3771 field.update_attribute :multiple, true
3764 3772
3765 3773 @request.session[:user_id] = 2
3766 3774 post :bulk_update, :ids => [1, 3], :notes => 'Bulk editing multi custom field',
3767 3775 :issue => {:priority_id => '',
3768 3776 :assigned_to_id => '',
3769 3777 :custom_field_values => {'1' => ['__none__']}}
3770 3778 assert_response 302
3771 3779 assert_equal [''], Issue.find(1).custom_field_value(1)
3772 3780 assert_equal [''], Issue.find(3).custom_field_value(1)
3773 3781 end
3774 3782
3775 3783 def test_bulk_update_unassign
3776 3784 assert_not_nil Issue.find(2).assigned_to
3777 3785 @request.session[:user_id] = 2
3778 3786 # unassign issues
3779 3787 post :bulk_update, :ids => [1, 2], :notes => 'Bulk unassigning', :issue => {:assigned_to_id => 'none'}
3780 3788 assert_response 302
3781 3789 # check that the issues were updated
3782 3790 assert_nil Issue.find(2).assigned_to
3783 3791 end
3784 3792
3785 3793 def test_post_bulk_update_should_allow_fixed_version_to_be_set_to_a_subproject
3786 3794 @request.session[:user_id] = 2
3787 3795
3788 3796 post :bulk_update, :ids => [1,2], :issue => {:fixed_version_id => 4}
3789 3797
3790 3798 assert_response :redirect
3791 3799 issues = Issue.find([1,2])
3792 3800 issues.each do |issue|
3793 3801 assert_equal 4, issue.fixed_version_id
3794 3802 assert_not_equal issue.project_id, issue.fixed_version.project_id
3795 3803 end
3796 3804 end
3797 3805
3798 3806 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
3799 3807 @request.session[:user_id] = 2
3800 3808 post :bulk_update, :ids => [1,2], :back_url => '/issues'
3801 3809
3802 3810 assert_response :redirect
3803 3811 assert_redirected_to '/issues'
3804 3812 end
3805 3813
3806 3814 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
3807 3815 @request.session[:user_id] = 2
3808 3816 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
3809 3817
3810 3818 assert_response :redirect
3811 3819 assert_redirected_to :controller => 'issues', :action => 'index', :project_id => Project.find(1).identifier
3812 3820 end
3813 3821
3814 3822 def test_bulk_update_with_all_failures_should_show_errors
3815 3823 @request.session[:user_id] = 2
3816 3824 post :bulk_update, :ids => [1, 2], :issue => {:start_date => 'foo'}
3817 3825
3818 3826 assert_response :success
3819 3827 assert_template 'bulk_edit'
3820 3828 assert_select '#errorExplanation span', :text => 'Failed to save 2 issue(s) on 2 selected: #1, #2.'
3821 3829 assert_select '#errorExplanation ul li', :text => 'Start date is not a valid date: #1, #2'
3822 3830
3823 3831 assert_equal [1, 2], assigns[:issues].map(&:id)
3824 3832 end
3825 3833
3826 3834 def test_bulk_update_with_some_failures_should_show_errors
3827 3835 issue1 = Issue.generate!(:start_date => '2013-05-12')
3828 3836 issue2 = Issue.generate!(:start_date => '2013-05-15')
3829 3837 issue3 = Issue.generate!
3830 3838 @request.session[:user_id] = 2
3831 3839 post :bulk_update, :ids => [issue1.id, issue2.id, issue3.id],
3832 3840 :issue => {:due_date => '2013-05-01'}
3833 3841 assert_response :success
3834 3842 assert_template 'bulk_edit'
3835 3843 assert_select '#errorExplanation span',
3836 3844 :text => "Failed to save 2 issue(s) on 3 selected: ##{issue1.id}, ##{issue2.id}."
3837 3845 assert_select '#errorExplanation ul li',
3838 3846 :text => "Due date must be greater than start date: ##{issue1.id}, ##{issue2.id}"
3839 3847 assert_equal [issue1.id, issue2.id], assigns[:issues].map(&:id)
3840 3848 end
3841 3849
3842 3850 def test_bulk_update_with_failure_should_preserved_form_values
3843 3851 @request.session[:user_id] = 2
3844 3852 post :bulk_update, :ids => [1, 2], :issue => {:tracker_id => '2', :start_date => 'foo'}
3845 3853
3846 3854 assert_response :success
3847 3855 assert_template 'bulk_edit'
3848 3856 assert_select 'select[name=?]', 'issue[tracker_id]' do
3849 3857 assert_select 'option[value="2"][selected=selected]'
3850 3858 end
3851 3859 assert_select 'input[name=?][value=?]', 'issue[start_date]', 'foo'
3852 3860 end
3853 3861
3854 3862 def test_get_bulk_copy
3855 3863 @request.session[:user_id] = 2
3856 3864 get :bulk_edit, :ids => [1, 2, 3], :copy => '1'
3857 3865 assert_response :success
3858 3866 assert_template 'bulk_edit'
3859 3867
3860 3868 issues = assigns(:issues)
3861 3869 assert_not_nil issues
3862 3870 assert_equal [1, 2, 3], issues.map(&:id).sort
3863 3871
3864 3872 assert_select 'select[name=?]', 'issue[project_id]' do
3865 3873 assert_select 'option[value=""]'
3866 3874 end
3867 3875 assert_select 'input[name=copy_attachments]'
3868 3876 end
3869 3877
3870 3878 def test_get_bulk_copy_without_add_issues_permission_should_not_propose_current_project_as_target
3871 3879 user = setup_user_with_copy_but_not_add_permission
3872 3880 @request.session[:user_id] = user.id
3873 3881
3874 3882 get :bulk_edit, :ids => [1, 2, 3], :copy => '1'
3875 3883 assert_response :success
3876 3884 assert_template 'bulk_edit'
3877 3885
3878 3886 assert_select 'select[name=?]', 'issue[project_id]' do
3879 3887 assert_select 'option[value=""]', 0
3880 3888 assert_select 'option[value="2"]'
3881 3889 end
3882 3890 end
3883 3891
3884 3892 def test_bulk_copy_to_another_project
3885 3893 @request.session[:user_id] = 2
3886 3894 assert_difference 'Issue.count', 2 do
3887 3895 assert_no_difference 'Project.find(1).issues.count' do
3888 3896 post :bulk_update, :ids => [1, 2], :issue => {:project_id => '2'}, :copy => '1'
3889 3897 end
3890 3898 end
3891 3899 assert_redirected_to '/projects/ecookbook/issues'
3892 3900
3893 3901 copies = Issue.order('id DESC').limit(issues.size)
3894 3902 copies.each do |copy|
3895 3903 assert_equal 2, copy.project_id
3896 3904 end
3897 3905 end
3898 3906
3899 3907 def test_bulk_copy_without_add_issues_permission_should_be_allowed_on_project_with_permission
3900 3908 user = setup_user_with_copy_but_not_add_permission
3901 3909 @request.session[:user_id] = user.id
3902 3910
3903 3911 assert_difference 'Issue.count', 3 do
3904 3912 post :bulk_update, :ids => [1, 2, 3], :issue => {:project_id => '2'}, :copy => '1'
3905 3913 assert_response 302
3906 3914 end
3907 3915 end
3908 3916
3909 3917 def test_bulk_copy_on_same_project_without_add_issues_permission_should_be_denied
3910 3918 user = setup_user_with_copy_but_not_add_permission
3911 3919 @request.session[:user_id] = user.id
3912 3920
3913 3921 post :bulk_update, :ids => [1, 2, 3], :issue => {:project_id => ''}, :copy => '1'
3914 3922 assert_response 403
3915 3923 end
3916 3924
3917 3925 def test_bulk_copy_on_different_project_without_add_issues_permission_should_be_denied
3918 3926 user = setup_user_with_copy_but_not_add_permission
3919 3927 @request.session[:user_id] = user.id
3920 3928
3921 3929 post :bulk_update, :ids => [1, 2, 3], :issue => {:project_id => '1'}, :copy => '1'
3922 3930 assert_response 403
3923 3931 end
3924 3932
3925 3933 def test_bulk_copy_should_allow_not_changing_the_issue_attributes
3926 3934 @request.session[:user_id] = 2
3927 3935 issues = [
3928 3936 Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 1,
3929 3937 :priority_id => 2, :subject => 'issue 1', :author_id => 1,
3930 3938 :assigned_to_id => nil),
3931 3939 Issue.create!(:project_id => 2, :tracker_id => 3, :status_id => 2,
3932 3940 :priority_id => 1, :subject => 'issue 2', :author_id => 2,
3933 3941 :assigned_to_id => 3)
3934 3942 ]
3935 3943 assert_difference 'Issue.count', issues.size do
3936 3944 post :bulk_update, :ids => issues.map(&:id), :copy => '1',
3937 3945 :issue => {
3938 3946 :project_id => '', :tracker_id => '', :assigned_to_id => '',
3939 3947 :status_id => '', :start_date => '', :due_date => ''
3940 3948 }
3941 3949 end
3942 3950
3943 3951 copies = Issue.order('id DESC').limit(issues.size)
3944 3952 issues.each do |orig|
3945 3953 copy = copies.detect {|c| c.subject == orig.subject}
3946 3954 assert_not_nil copy
3947 3955 assert_equal orig.project_id, copy.project_id
3948 3956 assert_equal orig.tracker_id, copy.tracker_id
3949 3957 assert_equal orig.status_id, copy.status_id
3950 3958 assert_equal orig.assigned_to_id, copy.assigned_to_id
3951 3959 assert_equal orig.priority_id, copy.priority_id
3952 3960 end
3953 3961 end
3954 3962
3955 3963 def test_bulk_copy_should_allow_changing_the_issue_attributes
3956 3964 # Fixes random test failure with Mysql
3957 3965 # where Issue.where(:project_id => 2).limit(2).order('id desc')
3958 3966 # doesn't return the expected results
3959 3967 Issue.delete_all("project_id=2")
3960 3968
3961 3969 @request.session[:user_id] = 2
3962 3970 assert_difference 'Issue.count', 2 do
3963 3971 assert_no_difference 'Project.find(1).issues.count' do
3964 3972 post :bulk_update, :ids => [1, 2], :copy => '1',
3965 3973 :issue => {
3966 3974 :project_id => '2', :tracker_id => '', :assigned_to_id => '4',
3967 3975 :status_id => '1', :start_date => '2009-12-01', :due_date => '2009-12-31'
3968 3976 }
3969 3977 end
3970 3978 end
3971 3979
3972 3980 copied_issues = Issue.where(:project_id => 2).limit(2).order('id desc').to_a
3973 3981 assert_equal 2, copied_issues.size
3974 3982 copied_issues.each do |issue|
3975 3983 assert_equal 2, issue.project_id, "Project is incorrect"
3976 3984 assert_equal 4, issue.assigned_to_id, "Assigned to is incorrect"
3977 3985 assert_equal 1, issue.status_id, "Status is incorrect"
3978 3986 assert_equal '2009-12-01', issue.start_date.to_s, "Start date is incorrect"
3979 3987 assert_equal '2009-12-31', issue.due_date.to_s, "Due date is incorrect"
3980 3988 end
3981 3989 end
3982 3990
3983 3991 def test_bulk_copy_should_allow_adding_a_note
3984 3992 @request.session[:user_id] = 2
3985 3993 assert_difference 'Issue.count', 1 do
3986 3994 post :bulk_update, :ids => [1], :copy => '1',
3987 3995 :notes => 'Copying one issue',
3988 3996 :issue => {
3989 3997 :project_id => '', :tracker_id => '', :assigned_to_id => '4',
3990 3998 :status_id => '3', :start_date => '2009-12-01', :due_date => '2009-12-31'
3991 3999 }
3992 4000 end
3993 4001 issue = Issue.order('id DESC').first
3994 4002 assert_equal 1, issue.journals.size
3995 4003 journal = issue.journals.first
3996 4004 assert_equal 'Copying one issue', journal.notes
3997 4005 end
3998 4006
3999 4007 def test_bulk_copy_should_allow_not_copying_the_attachments
4000 4008 attachment_count = Issue.find(3).attachments.size
4001 4009 assert attachment_count > 0
4002 4010 @request.session[:user_id] = 2
4003 4011
4004 4012 assert_difference 'Issue.count', 1 do
4005 4013 assert_no_difference 'Attachment.count' do
4006 4014 post :bulk_update, :ids => [3], :copy => '1',
4007 4015 :issue => {
4008 4016 :project_id => ''
4009 4017 }
4010 4018 end
4011 4019 end
4012 4020 end
4013 4021
4014 4022 def test_bulk_copy_should_allow_copying_the_attachments
4015 4023 attachment_count = Issue.find(3).attachments.size
4016 4024 assert attachment_count > 0
4017 4025 @request.session[:user_id] = 2
4018 4026
4019 4027 assert_difference 'Issue.count', 1 do
4020 4028 assert_difference 'Attachment.count', attachment_count do
4021 4029 post :bulk_update, :ids => [3], :copy => '1', :copy_attachments => '1',
4022 4030 :issue => {
4023 4031 :project_id => ''
4024 4032 }
4025 4033 end
4026 4034 end
4027 4035 end
4028 4036
4029 4037 def test_bulk_copy_should_add_relations_with_copied_issues
4030 4038 @request.session[:user_id] = 2
4031 4039
4032 4040 assert_difference 'Issue.count', 2 do
4033 4041 assert_difference 'IssueRelation.count', 2 do
4034 4042 post :bulk_update, :ids => [1, 3], :copy => '1', :link_copy => '1',
4035 4043 :issue => {
4036 4044 :project_id => '1'
4037 4045 }
4038 4046 end
4039 4047 end
4040 4048 end
4041 4049
4042 4050 def test_bulk_copy_should_allow_not_copying_the_subtasks
4043 4051 issue = Issue.generate_with_descendants!
4044 4052 @request.session[:user_id] = 2
4045 4053
4046 4054 assert_difference 'Issue.count', 1 do
4047 4055 post :bulk_update, :ids => [issue.id], :copy => '1',
4048 4056 :issue => {
4049 4057 :project_id => ''
4050 4058 }
4051 4059 end
4052 4060 end
4053 4061
4054 4062 def test_bulk_copy_should_allow_copying_the_subtasks
4055 4063 issue = Issue.generate_with_descendants!
4056 4064 count = issue.descendants.count
4057 4065 @request.session[:user_id] = 2
4058 4066
4059 4067 assert_difference 'Issue.count', count+1 do
4060 4068 post :bulk_update, :ids => [issue.id], :copy => '1', :copy_subtasks => '1',
4061 4069 :issue => {
4062 4070 :project_id => ''
4063 4071 }
4064 4072 end
4065 4073 copy = Issue.where(:parent_id => nil).order("id DESC").first
4066 4074 assert_equal count, copy.descendants.count
4067 4075 end
4068 4076
4069 4077 def test_bulk_copy_should_not_copy_selected_subtasks_twice
4070 4078 issue = Issue.generate_with_descendants!
4071 4079 count = issue.descendants.count
4072 4080 @request.session[:user_id] = 2
4073 4081
4074 4082 assert_difference 'Issue.count', count+1 do
4075 4083 post :bulk_update, :ids => issue.self_and_descendants.map(&:id), :copy => '1', :copy_subtasks => '1',
4076 4084 :issue => {
4077 4085 :project_id => ''
4078 4086 }
4079 4087 end
4080 4088 copy = Issue.where(:parent_id => nil).order("id DESC").first
4081 4089 assert_equal count, copy.descendants.count
4082 4090 end
4083 4091
4084 4092 def test_bulk_copy_to_another_project_should_follow_when_needed
4085 4093 @request.session[:user_id] = 2
4086 4094 post :bulk_update, :ids => [1], :copy => '1', :issue => {:project_id => 2}, :follow => '1'
4087 4095 issue = Issue.order('id DESC').first
4088 4096 assert_redirected_to :controller => 'issues', :action => 'show', :id => issue
4089 4097 end
4090 4098
4091 4099 def test_bulk_copy_with_all_failures_should_display_errors
4092 4100 @request.session[:user_id] = 2
4093 4101 post :bulk_update, :ids => [1, 2], :copy => '1', :issue => {:start_date => 'foo'}
4094 4102
4095 4103 assert_response :success
4096 4104 end
4097 4105
4098 4106 def test_destroy_issue_with_no_time_entries
4099 4107 assert_nil TimeEntry.find_by_issue_id(2)
4100 4108 @request.session[:user_id] = 2
4101 4109
4102 4110 assert_difference 'Issue.count', -1 do
4103 4111 delete :destroy, :id => 2
4104 4112 end
4105 4113 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4106 4114 assert_nil Issue.find_by_id(2)
4107 4115 end
4108 4116
4109 4117 def test_destroy_issues_with_time_entries
4110 4118 @request.session[:user_id] = 2
4111 4119
4112 4120 assert_no_difference 'Issue.count' do
4113 4121 delete :destroy, :ids => [1, 3]
4114 4122 end
4115 4123 assert_response :success
4116 4124 assert_template 'destroy'
4117 4125 assert_not_nil assigns(:hours)
4118 4126 assert Issue.find_by_id(1) && Issue.find_by_id(3)
4119 4127
4120 4128 assert_select 'form' do
4121 4129 assert_select 'input[name=_method][value=delete]'
4122 4130 end
4123 4131 end
4124 4132
4125 4133 def test_destroy_issues_and_destroy_time_entries
4126 4134 @request.session[:user_id] = 2
4127 4135
4128 4136 assert_difference 'Issue.count', -2 do
4129 4137 assert_difference 'TimeEntry.count', -3 do
4130 4138 delete :destroy, :ids => [1, 3], :todo => 'destroy'
4131 4139 end
4132 4140 end
4133 4141 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4134 4142 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
4135 4143 assert_nil TimeEntry.find_by_id([1, 2])
4136 4144 end
4137 4145
4138 4146 def test_destroy_issues_and_assign_time_entries_to_project
4139 4147 @request.session[:user_id] = 2
4140 4148
4141 4149 assert_difference 'Issue.count', -2 do
4142 4150 assert_no_difference 'TimeEntry.count' do
4143 4151 delete :destroy, :ids => [1, 3], :todo => 'nullify'
4144 4152 end
4145 4153 end
4146 4154 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4147 4155 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
4148 4156 assert_nil TimeEntry.find(1).issue_id
4149 4157 assert_nil TimeEntry.find(2).issue_id
4150 4158 end
4151 4159
4152 4160 def test_destroy_issues_and_reassign_time_entries_to_another_issue
4153 4161 @request.session[:user_id] = 2
4154 4162
4155 4163 assert_difference 'Issue.count', -2 do
4156 4164 assert_no_difference 'TimeEntry.count' do
4157 4165 delete :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
4158 4166 end
4159 4167 end
4160 4168 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
4161 4169 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
4162 4170 assert_equal 2, TimeEntry.find(1).issue_id
4163 4171 assert_equal 2, TimeEntry.find(2).issue_id
4164 4172 end
4165 4173
4166 4174 def test_destroy_issues_and_reassign_time_entries_to_an_invalid_issue_should_fail
4167 4175 @request.session[:user_id] = 2
4168 4176
4169 4177 assert_no_difference 'Issue.count' do
4170 4178 assert_no_difference 'TimeEntry.count' do
4171 4179 # try to reassign time to an issue of another project
4172 4180 delete :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 4
4173 4181 end
4174 4182 end
4175 4183 assert_response :success
4176 4184 assert_template 'destroy'
4177 4185 end
4178 4186
4179 4187 def test_destroy_issues_from_different_projects
4180 4188 @request.session[:user_id] = 2
4181 4189
4182 4190 assert_difference 'Issue.count', -3 do
4183 4191 delete :destroy, :ids => [1, 2, 6], :todo => 'destroy'
4184 4192 end
4185 4193 assert_redirected_to :controller => 'issues', :action => 'index'
4186 4194 assert !(Issue.find_by_id(1) || Issue.find_by_id(2) || Issue.find_by_id(6))
4187 4195 end
4188 4196
4189 4197 def test_destroy_parent_and_child_issues
4190 4198 parent = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'Parent Issue')
4191 4199 child = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'Child Issue', :parent_issue_id => parent.id)
4192 4200 assert child.is_descendant_of?(parent.reload)
4193 4201
4194 4202 @request.session[:user_id] = 2
4195 4203 assert_difference 'Issue.count', -2 do
4196 4204 delete :destroy, :ids => [parent.id, child.id], :todo => 'destroy'
4197 4205 end
4198 4206 assert_response 302
4199 4207 end
4200 4208
4201 4209 def test_destroy_invalid_should_respond_with_404
4202 4210 @request.session[:user_id] = 2
4203 4211 assert_no_difference 'Issue.count' do
4204 4212 delete :destroy, :id => 999
4205 4213 end
4206 4214 assert_response 404
4207 4215 end
4208 4216
4209 4217 def test_default_search_scope
4210 4218 get :index
4211 4219
4212 4220 assert_select 'div#quick-search form' do
4213 4221 assert_select 'input[name=issues][value="1"][type=hidden]'
4214 4222 end
4215 4223 end
4216 4224
4217 4225 def setup_user_with_copy_but_not_add_permission
4218 4226 Role.all.each {|r| r.remove_permission! :add_issues}
4219 4227 Role.find_by_name('Manager').add_permission! :add_issues
4220 4228 user = User.generate!
4221 4229 User.add_to_project(user, Project.find(1), Role.find_by_name('Developer'))
4222 4230 User.add_to_project(user, Project.find(2), Role.find_by_name('Manager'))
4223 4231 user
4224 4232 end
4225 4233 end
@@ -1,109 +1,109
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../../test_helper', __FILE__)
19 19
20 20 class SortHelperTest < ActionView::TestCase
21 21 include SortHelper
22 22 include Redmine::I18n
23 23 include ERB::Util
24 24
25 25 def setup
26 26 @session = nil
27 27 @sort_param = nil
28 28 end
29 29
30 30 def test_default_sort_clause_with_array
31 31 sort_init 'attr1', 'desc'
32 32 sort_update(['attr1', 'attr2'])
33 33
34 34 assert_equal ['attr1 DESC'], sort_clause
35 35 end
36 36
37 37 def test_default_sort_clause_with_hash
38 38 sort_init 'attr1', 'desc'
39 39 sort_update({'attr1' => 'table1.attr1', 'attr2' => 'table2.attr2'})
40 40
41 41 assert_equal ['table1.attr1 DESC'], sort_clause
42 42 end
43 43
44 44 def test_default_sort_clause_with_multiple_columns
45 45 sort_init 'attr1', 'desc'
46 46 sort_update({'attr1' => ['table1.attr1', 'table1.attr2'], 'attr2' => 'table2.attr2'})
47 47
48 48 assert_equal ['table1.attr1 DESC', 'table1.attr2 DESC'], sort_clause
49 49 end
50 50
51 51 def test_params_sort
52 52 @sort_param = 'attr1,attr2:desc'
53 53
54 54 sort_init 'attr1', 'desc'
55 55 sort_update({'attr1' => 'table1.attr1', 'attr2' => 'table2.attr2'})
56 56
57 assert_equal ['table1.attr1', 'table2.attr2 DESC'], sort_clause
57 assert_equal ['table1.attr1 ASC', 'table2.attr2 DESC'], sort_clause
58 58 assert_equal 'attr1,attr2:desc', @session['foo_bar_sort']
59 59 end
60 60
61 61 def test_invalid_params_sort
62 62 @sort_param = 'invalid_key'
63 63
64 64 sort_init 'attr1', 'desc'
65 65 sort_update({'attr1' => 'table1.attr1', 'attr2' => 'table2.attr2'})
66 66
67 67 assert_equal ['table1.attr1 DESC'], sort_clause
68 68 assert_equal 'attr1:desc', @session['foo_bar_sort']
69 69 end
70 70
71 71 def test_invalid_order_params_sort
72 72 @sort_param = 'attr1:foo:bar,attr2'
73 73
74 74 sort_init 'attr1', 'desc'
75 75 sort_update({'attr1' => 'table1.attr1', 'attr2' => 'table2.attr2'})
76 76
77 assert_equal ['table1.attr1', 'table2.attr2'], sort_clause
77 assert_equal ['table1.attr1 ASC', 'table2.attr2 ASC'], sort_clause
78 78 assert_equal 'attr1,attr2', @session['foo_bar_sort']
79 79 end
80 80
81 81 def test_sort_css_without_params_should_use_default_sort
82 82 sort_init 'attr1', 'desc'
83 83 sort_update(['attr1', 'attr2'])
84 84
85 85 assert_equal 'sort-by-attr1 sort-desc', sort_css_classes
86 86 end
87 87
88 88 def test_sort_css_should_use_params
89 89 @sort_param = 'attr2,attr1'
90 90 sort_init 'attr1', 'desc'
91 91 sort_update(['attr1', 'attr2'])
92 92
93 93 assert_equal 'sort-by-attr2 sort-asc', sort_css_classes
94 94 end
95 95
96 96 def test_sort_css_should_dasherize_sort_name
97 97 sort_init 'foo_bar'
98 98 sort_update(['foo_bar'])
99 99
100 100 assert_equal 'sort-by-foo-bar sort-asc', sort_css_classes
101 101 end
102 102
103 103 private
104 104
105 105 def controller_name; 'foo'; end
106 106 def action_name; 'bar'; end
107 107 def params; {:sort => @sort_param}; end
108 108 def session; @session ||= {}; end
109 109 end
General Comments 0
You need to be logged in to leave comments. Login now