##// END OF EJS Templates
Fixed: private subprojects are listed on the issues view (#1217)....
Jean-Philippe Lang -
r1417:9e225cc63ff8
parent child
Show More
@@ -1,372 +1,369
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class QueryColumn
19 19 attr_accessor :name, :sortable, :default_order
20 20 include GLoc
21 21
22 22 def initialize(name, options={})
23 23 self.name = name
24 24 self.sortable = options[:sortable]
25 25 self.default_order = options[:default_order]
26 26 end
27 27
28 28 def caption
29 29 set_language_if_valid(User.current.language)
30 30 l("field_#{name}")
31 31 end
32 32 end
33 33
34 34 class QueryCustomFieldColumn < QueryColumn
35 35
36 36 def initialize(custom_field)
37 37 self.name = "cf_#{custom_field.id}".to_sym
38 38 self.sortable = false
39 39 @cf = custom_field
40 40 end
41 41
42 42 def caption
43 43 @cf.name
44 44 end
45 45
46 46 def custom_field
47 47 @cf
48 48 end
49 49 end
50 50
51 51 class Query < ActiveRecord::Base
52 52 belongs_to :project
53 53 belongs_to :user
54 54 serialize :filters
55 55 serialize :column_names
56 56
57 57 attr_protected :project_id, :user_id
58 58
59 59 validates_presence_of :name, :on => :save
60 60 validates_length_of :name, :maximum => 255
61 61
62 62 @@operators = { "=" => :label_equals,
63 63 "!" => :label_not_equals,
64 64 "o" => :label_open_issues,
65 65 "c" => :label_closed_issues,
66 66 "!*" => :label_none,
67 67 "*" => :label_all,
68 68 ">=" => '>=',
69 69 "<=" => '<=',
70 70 "<t+" => :label_in_less_than,
71 71 ">t+" => :label_in_more_than,
72 72 "t+" => :label_in,
73 73 "t" => :label_today,
74 74 "w" => :label_this_week,
75 75 ">t-" => :label_less_than_ago,
76 76 "<t-" => :label_more_than_ago,
77 77 "t-" => :label_ago,
78 78 "~" => :label_contains,
79 79 "!~" => :label_not_contains }
80 80
81 81 cattr_reader :operators
82 82
83 83 @@operators_by_filter_type = { :list => [ "=", "!" ],
84 84 :list_status => [ "o", "=", "!", "c", "*" ],
85 85 :list_optional => [ "=", "!", "!*", "*" ],
86 86 :list_subprojects => [ "*", "!*", "=" ],
87 87 :date => [ "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-" ],
88 88 :date_past => [ ">t-", "<t-", "t-", "t", "w" ],
89 89 :string => [ "=", "~", "!", "!~" ],
90 90 :text => [ "~", "!~" ],
91 91 :integer => [ "=", ">=", "<=" ] }
92 92
93 93 cattr_reader :operators_by_filter_type
94 94
95 95 @@available_columns = [
96 96 QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position"),
97 97 QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position"),
98 98 QueryColumn.new(:priority, :sortable => "#{Enumeration.table_name}.position", :default_order => 'desc'),
99 99 QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"),
100 100 QueryColumn.new(:author),
101 101 QueryColumn.new(:assigned_to, :sortable => "#{User.table_name}.lastname"),
102 102 QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'),
103 103 QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name"),
104 104 QueryColumn.new(:fixed_version, :sortable => "#{Version.table_name}.effective_date", :default_order => 'desc'),
105 105 QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"),
106 106 QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"),
107 107 QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"),
108 108 QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio"),
109 109 QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'),
110 110 ]
111 111 cattr_reader :available_columns
112 112
113 113 def initialize(attributes = nil)
114 114 super attributes
115 115 self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} }
116 116 set_language_if_valid(User.current.language)
117 117 end
118 118
119 119 def after_initialize
120 120 # Store the fact that project is nil (used in #editable_by?)
121 121 @is_for_all = project.nil?
122 122 end
123 123
124 124 def validate
125 125 filters.each_key do |field|
126 126 errors.add label_for(field), :activerecord_error_blank unless
127 127 # filter requires one or more values
128 128 (values_for(field) and !values_for(field).first.blank?) or
129 129 # filter doesn't require any value
130 130 ["o", "c", "!*", "*", "t", "w"].include? operator_for(field)
131 131 end if filters
132 132 end
133 133
134 134 def editable_by?(user)
135 135 return false unless user
136 136 # Admin can edit them all and regular users can edit their private queries
137 137 return true if user.admin? || (!is_public && self.user_id == user.id)
138 138 # Members can not edit public queries that are for all project (only admin is allowed to)
139 139 is_public && !@is_for_all && user.allowed_to?(:manage_public_queries, project)
140 140 end
141 141
142 142 def available_filters
143 143 return @available_filters if @available_filters
144 144
145 145 trackers = project.nil? ? Tracker.find(:all, :order => 'position') : project.rolled_up_trackers
146 146
147 147 @available_filters = { "status_id" => { :type => :list_status, :order => 1, :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } },
148 148 "tracker_id" => { :type => :list, :order => 2, :values => trackers.collect{|s| [s.name, s.id.to_s] } },
149 149 "priority_id" => { :type => :list, :order => 3, :values => Enumeration.find(:all, :conditions => ['opt=?','IPRI'], :order => 'position').collect{|s| [s.name, s.id.to_s] } },
150 150 "subject" => { :type => :text, :order => 8 },
151 151 "created_on" => { :type => :date_past, :order => 9 },
152 152 "updated_on" => { :type => :date_past, :order => 10 },
153 153 "start_date" => { :type => :date, :order => 11 },
154 154 "due_date" => { :type => :date, :order => 12 },
155 155 "done_ratio" => { :type => :integer, :order => 13 }}
156 156
157 157 user_values = []
158 158 user_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
159 159 if project
160 160 user_values += project.users.sort.collect{|s| [s.name, s.id.to_s] }
161 161 else
162 162 # members of the user's projects
163 163 user_values += User.current.projects.collect(&:users).flatten.uniq.sort.collect{|s| [s.name, s.id.to_s] }
164 164 end
165 165 @available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => user_values } unless user_values.empty?
166 166 @available_filters["author_id"] = { :type => :list, :order => 5, :values => user_values } unless user_values.empty?
167 167
168 168 if project
169 169 # project specific filters
170 170 @available_filters["category_id"] = { :type => :list_optional, :order => 6, :values => @project.issue_categories.collect{|s| [s.name, s.id.to_s] } }
171 171 @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => @project.versions.sort.collect{|s| [s.name, s.id.to_s] } }
172 172 unless @project.active_children.empty?
173 173 @available_filters["subproject_id"] = { :type => :list_subprojects, :order => 13, :values => @project.active_children.collect{|s| [s.name, s.id.to_s] } }
174 174 end
175 175 @project.all_custom_fields.select(&:is_filter?).each do |field|
176 176 case field.field_format
177 177 when "text"
178 178 options = { :type => :text, :order => 20 }
179 179 when "list"
180 180 options = { :type => :list_optional, :values => field.possible_values, :order => 20}
181 181 when "date"
182 182 options = { :type => :date, :order => 20 }
183 183 when "bool"
184 184 options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 }
185 185 else
186 186 options = { :type => :string, :order => 20 }
187 187 end
188 188 @available_filters["cf_#{field.id}"] = options.merge({ :name => field.name })
189 189 end
190 190 # remove category filter if no category defined
191 191 @available_filters.delete "category_id" if @available_filters["category_id"][:values].empty?
192 192 end
193 193 @available_filters
194 194 end
195 195
196 196 def add_filter(field, operator, values)
197 197 # values must be an array
198 198 return unless values and values.is_a? Array # and !values.first.empty?
199 199 # check if field is defined as an available filter
200 200 if available_filters.has_key? field
201 201 filter_options = available_filters[field]
202 202 # check if operator is allowed for that filter
203 203 #if @@operators_by_filter_type[filter_options[:type]].include? operator
204 204 # allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]})
205 205 # filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator
206 206 #end
207 207 filters[field] = {:operator => operator, :values => values }
208 208 end
209 209 end
210 210
211 211 def add_short_filter(field, expression)
212 212 return unless expression
213 213 parms = expression.scan(/^(o|c|\!|\*)?(.*)$/).first
214 214 add_filter field, (parms[0] || "="), [parms[1] || ""]
215 215 end
216 216
217 217 def has_filter?(field)
218 218 filters and filters[field]
219 219 end
220 220
221 221 def operator_for(field)
222 222 has_filter?(field) ? filters[field][:operator] : nil
223 223 end
224 224
225 225 def values_for(field)
226 226 has_filter?(field) ? filters[field][:values] : nil
227 227 end
228 228
229 229 def label_for(field)
230 230 label = @available_filters[field][:name] if @available_filters.has_key?(field)
231 231 label ||= field.gsub(/\_id$/, "")
232 232 end
233 233
234 234 def available_columns
235 235 return @available_columns if @available_columns
236 236 @available_columns = Query.available_columns
237 237 @available_columns += (project ?
238 238 project.all_custom_fields :
239 239 IssueCustomField.find(:all, :conditions => {:is_for_all => true})
240 240 ).collect {|cf| QueryCustomFieldColumn.new(cf) }
241 241 end
242 242
243 243 def columns
244 244 if has_default_columns?
245 245 available_columns.select {|c| Setting.issue_list_default_columns.include?(c.name.to_s) }
246 246 else
247 247 # preserve the column_names order
248 248 column_names.collect {|name| available_columns.find {|col| col.name == name}}.compact
249 249 end
250 250 end
251 251
252 252 def column_names=(names)
253 253 names = names.select {|n| n.is_a?(Symbol) || !n.blank? } if names
254 254 names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym } if names
255 255 write_attribute(:column_names, names)
256 256 end
257 257
258 258 def has_column?(column)
259 259 column_names && column_names.include?(column.name)
260 260 end
261 261
262 262 def has_default_columns?
263 263 column_names.nil? || column_names.empty?
264 264 end
265 265
266 266 def statement
267 267 # project/subprojects clause
268 clause = ''
268 project_clauses = []
269 269 if project && !@project.active_children.empty?
270 270 ids = [project.id]
271 271 if has_filter?("subproject_id")
272 272 case operator_for("subproject_id")
273 273 when '='
274 274 # include the selected subprojects
275 275 ids += values_for("subproject_id").each(&:to_i)
276 276 when '!*'
277 277 # main project only
278 278 else
279 279 # all subprojects
280 ids += project.active_children.collect{|p| p.id}
280 ids += project.child_ids
281 281 end
282 282 elsif Setting.display_subprojects_issues?
283 ids += project.active_children.collect{|p| p.id}
283 ids += project.child_ids
284 284 end
285 clause << "#{Issue.table_name}.project_id IN (%s)" % ids.join(',')
285 project_clauses << "#{Issue.table_name}.project_id IN (%s)" % ids.join(',')
286 286 elsif project
287 clause << "#{Issue.table_name}.project_id = %d" % project.id
288 else
289 clause << Project.visible_by(User.current)
287 project_clauses << "#{Issue.table_name}.project_id = %d" % project.id
290 288 end
289 project_clauses << Project.visible_by(User.current)
291 290
292 291 # filters clauses
293 292 filters_clauses = []
294 293 filters.each_key do |field|
295 294 next if field == "subproject_id"
296 295 v = values_for(field).clone
297 296 next unless v and !v.empty?
298 297
299 298 sql = ''
300 299 is_custom_filter = false
301 300 if field =~ /^cf_(\d+)$/
302 301 # custom field
303 302 db_table = CustomValue.table_name
304 303 db_field = 'value'
305 304 is_custom_filter = true
306 305 sql << "#{Issue.table_name}.id IN (SELECT #{Issue.table_name}.id FROM #{Issue.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{$1} WHERE "
307 306 else
308 307 # regular field
309 308 db_table = Issue.table_name
310 309 db_field = field
311 310 sql << '('
312 311 end
313 312
314 313 # "me" value subsitution
315 314 if %w(assigned_to_id author_id).include?(field)
316 315 v.push(User.current.logged? ? User.current.id.to_s : "0") if v.delete("me")
317 316 end
318 317
319 318 case operator_for field
320 319 when "="
321 320 sql = sql + "#{db_table}.#{db_field} IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")"
322 321 when "!"
323 322 sql = sql + "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + v.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))"
324 323 when "!*"
325 324 sql = sql + "#{db_table}.#{db_field} IS NULL"
326 325 sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter
327 326 when "*"
328 327 sql = sql + "#{db_table}.#{db_field} IS NOT NULL"
329 328 sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter
330 329 when ">="
331 330 sql = sql + "#{db_table}.#{db_field} >= #{v.first.to_i}"
332 331 when "<="
333 332 sql = sql + "#{db_table}.#{db_field} <= #{v.first.to_i}"
334 333 when "o"
335 334 sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_false}" if field == "status_id"
336 335 when "c"
337 336 sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_true}" if field == "status_id"
338 337 when ">t-"
339 338 sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today - v.first.to_i).to_time), connection.quoted_date((Date.today + 1).to_time)]
340 339 when "<t-"
341 340 sql = sql + "#{db_table}.#{db_field} <= '%s'" % connection.quoted_date((Date.today - v.first.to_i).to_time)
342 341 when "t-"
343 342 sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today - v.first.to_i).to_time), connection.quoted_date((Date.today - v.first.to_i + 1).to_time)]
344 343 when ">t+"
345 344 sql = sql + "#{db_table}.#{db_field} >= '%s'" % connection.quoted_date((Date.today + v.first.to_i).to_time)
346 345 when "<t+"
347 346 sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Date.today.to_time), connection.quoted_date((Date.today + v.first.to_i + 1).to_time)]
348 347 when "t+"
349 348 sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today + v.first.to_i).to_time), connection.quoted_date((Date.today + v.first.to_i + 1).to_time)]
350 349 when "t"
351 350 sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Date.today.to_time), connection.quoted_date((Date.today+1).to_time)]
352 351 when "w"
353 352 from = l(:general_first_day_of_week) == '7' ?
354 353 # week starts on sunday
355 354 ((Date.today.cwday == 7) ? Time.now.at_beginning_of_day : Time.now.at_beginning_of_week - 1.day) :
356 355 # week starts on monday (Rails default)
357 356 Time.now.at_beginning_of_week
358 357 sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(from), connection.quoted_date(from + 7.days)]
359 358 when "~"
360 359 sql = sql + "#{db_table}.#{db_field} LIKE '%#{connection.quote_string(v.first)}%'"
361 360 when "!~"
362 361 sql = sql + "#{db_table}.#{db_field} NOT LIKE '%#{connection.quote_string(v.first)}%'"
363 362 end
364 363 sql << ')'
365 364 filters_clauses << sql
366 365 end if filters and valid?
367 366
368 clause << ' AND ' unless clause.empty?
369 clause << filters_clauses.join(' AND ') unless filters_clauses.empty?
370 clause
367 (project_clauses + filters_clauses).join(' AND ')
371 368 end
372 369 end
@@ -1,514 +1,545
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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.dirname(__FILE__) + '/../test_helper'
19 19 require 'issues_controller'
20 20
21 21 # Re-raise errors caught by the controller.
22 22 class IssuesController; def rescue_action(e) raise e end; end
23 23
24 24 class IssuesControllerTest < Test::Unit::TestCase
25 25 fixtures :projects,
26 26 :users,
27 27 :roles,
28 28 :members,
29 29 :issues,
30 30 :issue_statuses,
31 31 :trackers,
32 32 :projects_trackers,
33 33 :issue_categories,
34 34 :enabled_modules,
35 35 :enumerations,
36 36 :attachments,
37 37 :workflows,
38 38 :custom_fields,
39 39 :custom_values,
40 40 :custom_fields_trackers,
41 41 :time_entries
42 42
43 43 def setup
44 44 @controller = IssuesController.new
45 45 @request = ActionController::TestRequest.new
46 46 @response = ActionController::TestResponse.new
47 47 User.current = nil
48 48 end
49 49
50 50 def test_index
51 51 get :index
52 52 assert_response :success
53 53 assert_template 'index.rhtml'
54 54 assert_not_nil assigns(:issues)
55 55 assert_nil assigns(:project)
56 assert_tag :tag => 'a', :content => /Can't print recipes/
57 assert_tag :tag => 'a', :content => /Subproject issue/
58 # private projects hidden
59 assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
60 assert_no_tag :tag => 'a', :content => /Issue on project 2/
56 61 end
57 62
58 63 def test_index_with_project
64 Setting.display_subprojects_issues = 0
59 65 get :index, :project_id => 1
60 66 assert_response :success
61 67 assert_template 'index.rhtml'
62 68 assert_not_nil assigns(:issues)
69 assert_tag :tag => 'a', :content => /Can't print recipes/
70 assert_no_tag :tag => 'a', :content => /Subproject issue/
71 end
72
73 def test_index_with_project_and_subprojects
74 Setting.display_subprojects_issues = 1
75 get :index, :project_id => 1
76 assert_response :success
77 assert_template 'index.rhtml'
78 assert_not_nil assigns(:issues)
79 assert_tag :tag => 'a', :content => /Can't print recipes/
80 assert_tag :tag => 'a', :content => /Subproject issue/
81 assert_no_tag :tag => 'a', :content => /Issue of a private subproject/
82 end
83
84 def test_index_with_project_and_subprojects_should_show_private_subprojects
85 @request.session[:user_id] = 2
86 Setting.display_subprojects_issues = 1
87 get :index, :project_id => 1
88 assert_response :success
89 assert_template 'index.rhtml'
90 assert_not_nil assigns(:issues)
91 assert_tag :tag => 'a', :content => /Can't print recipes/
92 assert_tag :tag => 'a', :content => /Subproject issue/
93 assert_tag :tag => 'a', :content => /Issue of a private subproject/
63 94 end
64 95
65 96 def test_index_with_project_and_filter
66 97 get :index, :project_id => 1, :set_filter => 1
67 98 assert_response :success
68 99 assert_template 'index.rhtml'
69 100 assert_not_nil assigns(:issues)
70 101 end
71 102
72 103 def test_index_csv_with_project
73 104 get :index, :format => 'csv'
74 105 assert_response :success
75 106 assert_not_nil assigns(:issues)
76 107 assert_equal 'text/csv', @response.content_type
77 108
78 109 get :index, :project_id => 1, :format => 'csv'
79 110 assert_response :success
80 111 assert_not_nil assigns(:issues)
81 112 assert_equal 'text/csv', @response.content_type
82 113 end
83 114
84 115 def test_index_pdf
85 116 get :index, :format => 'pdf'
86 117 assert_response :success
87 118 assert_not_nil assigns(:issues)
88 119 assert_equal 'application/pdf', @response.content_type
89 120
90 121 get :index, :project_id => 1, :format => 'pdf'
91 122 assert_response :success
92 123 assert_not_nil assigns(:issues)
93 124 assert_equal 'application/pdf', @response.content_type
94 125 end
95 126
96 127 def test_changes
97 128 get :changes, :project_id => 1
98 129 assert_response :success
99 130 assert_not_nil assigns(:journals)
100 131 assert_equal 'application/atom+xml', @response.content_type
101 132 end
102 133
103 134 def test_show_by_anonymous
104 135 get :show, :id => 1
105 136 assert_response :success
106 137 assert_template 'show.rhtml'
107 138 assert_not_nil assigns(:issue)
108 139 assert_equal Issue.find(1), assigns(:issue)
109 140
110 141 # anonymous role is allowed to add a note
111 142 assert_tag :tag => 'form',
112 143 :descendant => { :tag => 'fieldset',
113 144 :child => { :tag => 'legend',
114 145 :content => /Notes/ } }
115 146 end
116 147
117 148 def test_show_by_manager
118 149 @request.session[:user_id] = 2
119 150 get :show, :id => 1
120 151 assert_response :success
121 152
122 153 assert_tag :tag => 'form',
123 154 :descendant => { :tag => 'fieldset',
124 155 :child => { :tag => 'legend',
125 156 :content => /Change properties/ } },
126 157 :descendant => { :tag => 'fieldset',
127 158 :child => { :tag => 'legend',
128 159 :content => /Log time/ } },
129 160 :descendant => { :tag => 'fieldset',
130 161 :child => { :tag => 'legend',
131 162 :content => /Notes/ } }
132 163 end
133 164
134 165 def test_get_new
135 166 @request.session[:user_id] = 2
136 167 get :new, :project_id => 1, :tracker_id => 1
137 168 assert_response :success
138 169 assert_template 'new'
139 170
140 171 assert_tag :tag => 'input', :attributes => { :name => 'custom_fields[2]',
141 172 :value => 'Default string' }
142 173 end
143 174
144 175 def test_get_new_without_tracker_id
145 176 @request.session[:user_id] = 2
146 177 get :new, :project_id => 1
147 178 assert_response :success
148 179 assert_template 'new'
149 180
150 181 issue = assigns(:issue)
151 182 assert_not_nil issue
152 183 assert_equal Project.find(1).trackers.first, issue.tracker
153 184 end
154 185
155 186 def test_update_new_form
156 187 @request.session[:user_id] = 2
157 188 xhr :post, :new, :project_id => 1,
158 189 :issue => {:tracker_id => 2,
159 190 :subject => 'This is the test_new issue',
160 191 :description => 'This is the description',
161 192 :priority_id => 5}
162 193 assert_response :success
163 194 assert_template 'new'
164 195 end
165 196
166 197 def test_post_new
167 198 @request.session[:user_id] = 2
168 199 post :new, :project_id => 1,
169 200 :issue => {:tracker_id => 1,
170 201 :subject => 'This is the test_new issue',
171 202 :description => 'This is the description',
172 203 :priority_id => 5,
173 204 :estimated_hours => ''},
174 205 :custom_fields => {'2' => 'Value for field 2'}
175 206 assert_redirected_to 'issues/show'
176 207
177 208 issue = Issue.find_by_subject('This is the test_new issue')
178 209 assert_not_nil issue
179 210 assert_equal 2, issue.author_id
180 211 assert_nil issue.estimated_hours
181 212 v = issue.custom_values.find_by_custom_field_id(2)
182 213 assert_not_nil v
183 214 assert_equal 'Value for field 2', v.value
184 215 end
185 216
186 217 def test_post_new_without_custom_fields_param
187 218 @request.session[:user_id] = 2
188 219 post :new, :project_id => 1,
189 220 :issue => {:tracker_id => 1,
190 221 :subject => 'This is the test_new issue',
191 222 :description => 'This is the description',
192 223 :priority_id => 5}
193 224 assert_redirected_to 'issues/show'
194 225 end
195 226
196 227 def test_copy_issue
197 228 @request.session[:user_id] = 2
198 229 get :new, :project_id => 1, :copy_from => 1
199 230 assert_template 'new'
200 231 assert_not_nil assigns(:issue)
201 232 orig = Issue.find(1)
202 233 assert_equal orig.subject, assigns(:issue).subject
203 234 end
204 235
205 236 def test_get_edit
206 237 @request.session[:user_id] = 2
207 238 get :edit, :id => 1
208 239 assert_response :success
209 240 assert_template 'edit'
210 241 assert_not_nil assigns(:issue)
211 242 assert_equal Issue.find(1), assigns(:issue)
212 243 end
213 244
214 245 def test_get_edit_with_params
215 246 @request.session[:user_id] = 2
216 247 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 }
217 248 assert_response :success
218 249 assert_template 'edit'
219 250
220 251 issue = assigns(:issue)
221 252 assert_not_nil issue
222 253
223 254 assert_equal 5, issue.status_id
224 255 assert_tag :select, :attributes => { :name => 'issue[status_id]' },
225 256 :child => { :tag => 'option',
226 257 :content => 'Closed',
227 258 :attributes => { :selected => 'selected' } }
228 259
229 260 assert_equal 7, issue.priority_id
230 261 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
231 262 :child => { :tag => 'option',
232 263 :content => 'Urgent',
233 264 :attributes => { :selected => 'selected' } }
234 265 end
235 266
236 267 def test_post_edit
237 268 @request.session[:user_id] = 2
238 269 ActionMailer::Base.deliveries.clear
239 270
240 271 issue = Issue.find(1)
241 272 old_subject = issue.subject
242 273 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
243 274
244 275 post :edit, :id => 1, :issue => {:subject => new_subject}
245 276 assert_redirected_to 'issues/show/1'
246 277 issue.reload
247 278 assert_equal new_subject, issue.subject
248 279
249 280 mail = ActionMailer::Base.deliveries.last
250 281 assert_kind_of TMail::Mail, mail
251 282 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
252 283 assert mail.body.include?("Subject changed from #{old_subject} to #{new_subject}")
253 284 end
254 285
255 286 def test_post_edit_with_status_and_assignee_change
256 287 issue = Issue.find(1)
257 288 assert_equal 1, issue.status_id
258 289 @request.session[:user_id] = 2
259 290 assert_difference('TimeEntry.count', 0) do
260 291 post :edit,
261 292 :id => 1,
262 293 :issue => { :status_id => 2, :assigned_to_id => 3 },
263 294 :notes => 'Assigned to dlopper',
264 295 :time_entry => { :hours => '', :comments => '', :activity_id => Enumeration.get_values('ACTI').first }
265 296 end
266 297 assert_redirected_to 'issues/show/1'
267 298 issue.reload
268 299 assert_equal 2, issue.status_id
269 300 j = issue.journals.find(:first, :order => 'id DESC')
270 301 assert_equal 'Assigned to dlopper', j.notes
271 302 assert_equal 2, j.details.size
272 303
273 304 mail = ActionMailer::Base.deliveries.last
274 305 assert mail.body.include?("Status changed from New to Assigned")
275 306 end
276 307
277 308 def test_post_edit_with_note_only
278 309 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
279 310 # anonymous user
280 311 post :edit,
281 312 :id => 1,
282 313 :notes => notes
283 314 assert_redirected_to 'issues/show/1'
284 315 j = Issue.find(1).journals.find(:first, :order => 'id DESC')
285 316 assert_equal notes, j.notes
286 317 assert_equal 0, j.details.size
287 318 assert_equal User.anonymous, j.user
288 319
289 320 mail = ActionMailer::Base.deliveries.last
290 321 assert mail.body.include?(notes)
291 322 end
292 323
293 324 def test_post_edit_with_note_and_spent_time
294 325 @request.session[:user_id] = 2
295 326 spent_hours_before = Issue.find(1).spent_hours
296 327 assert_difference('TimeEntry.count') do
297 328 post :edit,
298 329 :id => 1,
299 330 :notes => '2.5 hours added',
300 331 :time_entry => { :hours => '2.5', :comments => '', :activity_id => Enumeration.get_values('ACTI').first }
301 332 end
302 333 assert_redirected_to 'issues/show/1'
303 334
304 335 issue = Issue.find(1)
305 336
306 337 j = issue.journals.find(:first, :order => 'id DESC')
307 338 assert_equal '2.5 hours added', j.notes
308 339 assert_equal 0, j.details.size
309 340
310 341 t = issue.time_entries.find(:first, :order => 'id DESC')
311 342 assert_not_nil t
312 343 assert_equal 2.5, t.hours
313 344 assert_equal spent_hours_before + 2.5, issue.spent_hours
314 345 end
315 346
316 347 def test_post_edit_with_attachment_only
317 348 # anonymous user
318 349 post :edit,
319 350 :id => 1,
320 351 :notes => '',
321 352 :attachments => {'1' => {'file' => test_uploaded_file('testfile.txt', 'text/plain')}}
322 353 assert_redirected_to 'issues/show/1'
323 354 j = Issue.find(1).journals.find(:first, :order => 'id DESC')
324 355 assert j.notes.blank?
325 356 assert_equal 1, j.details.size
326 357 assert_equal 'testfile.txt', j.details.first.value
327 358 assert_equal User.anonymous, j.user
328 359
329 360 mail = ActionMailer::Base.deliveries.last
330 361 assert mail.body.include?('testfile.txt')
331 362 end
332 363
333 364 def test_post_edit_with_no_change
334 365 issue = Issue.find(1)
335 366 issue.journals.clear
336 367 ActionMailer::Base.deliveries.clear
337 368
338 369 post :edit,
339 370 :id => 1,
340 371 :notes => ''
341 372 assert_redirected_to 'issues/show/1'
342 373
343 374 issue.reload
344 375 assert issue.journals.empty?
345 376 # No email should be sent
346 377 assert ActionMailer::Base.deliveries.empty?
347 378 end
348 379
349 380 def test_bulk_edit
350 381 @request.session[:user_id] = 2
351 382 # update issues priority
352 383 post :bulk_edit, :ids => [1, 2], :priority_id => 7, :notes => 'Bulk editing', :assigned_to_id => ''
353 384 assert_response 302
354 385 # check that the issues were updated
355 386 assert_equal [7, 7], Issue.find_all_by_id([1, 2]).collect {|i| i.priority.id}
356 387 assert_equal 'Bulk editing', Issue.find(1).journals.find(:first, :order => 'created_on DESC').notes
357 388 end
358 389
359 390 def test_bulk_unassign
360 391 assert_not_nil Issue.find(2).assigned_to
361 392 @request.session[:user_id] = 2
362 393 # unassign issues
363 394 post :bulk_edit, :ids => [1, 2], :notes => 'Bulk unassigning', :assigned_to_id => 'none'
364 395 assert_response 302
365 396 # check that the issues were updated
366 397 assert_nil Issue.find(2).assigned_to
367 398 end
368 399
369 400 def test_move_one_issue_to_another_project
370 401 @request.session[:user_id] = 1
371 402 post :move, :id => 1, :new_project_id => 2
372 403 assert_redirected_to 'projects/ecookbook/issues'
373 404 assert_equal 2, Issue.find(1).project_id
374 405 end
375 406
376 407 def test_bulk_move_to_another_project
377 408 @request.session[:user_id] = 1
378 409 post :move, :ids => [1, 2], :new_project_id => 2
379 410 assert_redirected_to 'projects/ecookbook/issues'
380 411 # Issues moved to project 2
381 412 assert_equal 2, Issue.find(1).project_id
382 413 assert_equal 2, Issue.find(2).project_id
383 414 # No tracker change
384 415 assert_equal 1, Issue.find(1).tracker_id
385 416 assert_equal 2, Issue.find(2).tracker_id
386 417 end
387 418
388 419 def test_bulk_move_to_another_tracker
389 420 @request.session[:user_id] = 1
390 421 post :move, :ids => [1, 2], :new_tracker_id => 2
391 422 assert_redirected_to 'projects/ecookbook/issues'
392 423 assert_equal 2, Issue.find(1).tracker_id
393 424 assert_equal 2, Issue.find(2).tracker_id
394 425 end
395 426
396 427 def test_context_menu_one_issue
397 428 @request.session[:user_id] = 2
398 429 get :context_menu, :ids => [1]
399 430 assert_response :success
400 431 assert_template 'context_menu'
401 432 assert_tag :tag => 'a', :content => 'Edit',
402 433 :attributes => { :href => '/issues/edit/1',
403 434 :class => 'icon-edit' }
404 435 assert_tag :tag => 'a', :content => 'Closed',
405 436 :attributes => { :href => '/issues/edit/1?issue%5Bstatus_id%5D=5',
406 437 :class => '' }
407 438 assert_tag :tag => 'a', :content => 'Immediate',
408 439 :attributes => { :href => '/issues/edit/1?issue%5Bpriority_id%5D=8',
409 440 :class => '' }
410 441 assert_tag :tag => 'a', :content => 'Dave Lopper',
411 442 :attributes => { :href => '/issues/edit/1?issue%5Bassigned_to_id%5D=3',
412 443 :class => '' }
413 444 assert_tag :tag => 'a', :content => 'Copy',
414 445 :attributes => { :href => '/projects/ecookbook/issues/new?copy_from=1',
415 446 :class => 'icon-copy' }
416 447 assert_tag :tag => 'a', :content => 'Move',
417 448 :attributes => { :href => '/issues/move?ids%5B%5D=1',
418 449 :class => 'icon-move' }
419 450 assert_tag :tag => 'a', :content => 'Delete',
420 451 :attributes => { :href => '/issues/destroy?ids%5B%5D=1',
421 452 :class => 'icon-del' }
422 453 end
423 454
424 455 def test_context_menu_one_issue_by_anonymous
425 456 get :context_menu, :ids => [1]
426 457 assert_response :success
427 458 assert_template 'context_menu'
428 459 assert_tag :tag => 'a', :content => 'Delete',
429 460 :attributes => { :href => '#',
430 461 :class => 'icon-del disabled' }
431 462 end
432 463
433 464 def test_context_menu_multiple_issues_of_same_project
434 465 @request.session[:user_id] = 2
435 466 get :context_menu, :ids => [1, 2]
436 467 assert_response :success
437 468 assert_template 'context_menu'
438 469 assert_tag :tag => 'a', :content => 'Edit',
439 470 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;ids%5B%5D=2',
440 471 :class => 'icon-edit' }
441 472 assert_tag :tag => 'a', :content => 'Move',
442 473 :attributes => { :href => '/issues/move?ids%5B%5D=1&amp;ids%5B%5D=2',
443 474 :class => 'icon-move' }
444 475 assert_tag :tag => 'a', :content => 'Delete',
445 476 :attributes => { :href => '/issues/destroy?ids%5B%5D=1&amp;ids%5B%5D=2',
446 477 :class => 'icon-del' }
447 478 end
448 479
449 480 def test_context_menu_multiple_issues_of_different_project
450 481 @request.session[:user_id] = 2
451 482 get :context_menu, :ids => [1, 2, 4]
452 483 assert_response :success
453 484 assert_template 'context_menu'
454 485 assert_tag :tag => 'a', :content => 'Delete',
455 486 :attributes => { :href => '#',
456 487 :class => 'icon-del disabled' }
457 488 end
458 489
459 490 def test_destroy_issue_with_no_time_entries
460 491 assert_nil TimeEntry.find_by_issue_id(2)
461 492 @request.session[:user_id] = 2
462 493 post :destroy, :id => 2
463 494 assert_redirected_to 'projects/ecookbook/issues'
464 495 assert_nil Issue.find_by_id(2)
465 496 end
466 497
467 498 def test_destroy_issues_with_time_entries
468 499 @request.session[:user_id] = 2
469 500 post :destroy, :ids => [1, 3]
470 501 assert_response :success
471 502 assert_template 'destroy'
472 503 assert_not_nil assigns(:hours)
473 504 assert Issue.find_by_id(1) && Issue.find_by_id(3)
474 505 end
475 506
476 507 def test_destroy_issues_and_destroy_time_entries
477 508 @request.session[:user_id] = 2
478 509 post :destroy, :ids => [1, 3], :todo => 'destroy'
479 510 assert_redirected_to 'projects/ecookbook/issues'
480 511 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
481 512 assert_nil TimeEntry.find_by_id([1, 2])
482 513 end
483 514
484 515 def test_destroy_issues_and_assign_time_entries_to_project
485 516 @request.session[:user_id] = 2
486 517 post :destroy, :ids => [1, 3], :todo => 'nullify'
487 518 assert_redirected_to 'projects/ecookbook/issues'
488 519 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
489 520 assert_nil TimeEntry.find(1).issue_id
490 521 assert_nil TimeEntry.find(2).issue_id
491 522 end
492 523
493 524 def test_destroy_issues_and_reassign_time_entries_to_another_issue
494 525 @request.session[:user_id] = 2
495 526 post :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
496 527 assert_redirected_to 'projects/ecookbook/issues'
497 528 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
498 529 assert_equal 2, TimeEntry.find(1).issue_id
499 530 assert_equal 2, TimeEntry.find(2).issue_id
500 531 end
501 532
502 533 def test_destroy_attachment
503 534 issue = Issue.find(3)
504 535 a = issue.attachments.size
505 536 @request.session[:user_id] = 2
506 537 post :destroy_attachment, :id => 3, :attachment_id => 1
507 538 assert_redirected_to 'issues/show/3'
508 539 assert_nil Attachment.find_by_id(1)
509 540 issue.reload
510 541 assert_equal((a-1), issue.attachments.size)
511 542 j = issue.journals.find(:first, :order => 'created_on DESC')
512 543 assert_equal 'attachment', j.details.first.property
513 544 end
514 545 end
General Comments 0
You need to be logged in to leave comments. Login now