##// END OF EJS Templates
code layout clean up test_should_not_update_custom_fields_on_changing_tracker_with_different_custom_fields of unit issue test...
Toshi MARUYAMA -
r10427:a66a50eda249
parent child
Show More
@@ -1,1711 +1,1713
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 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 IssueTest < ActiveSupport::TestCase
21 21 fixtures :projects, :users, :members, :member_roles, :roles,
22 22 :groups_users,
23 23 :trackers, :projects_trackers,
24 24 :enabled_modules,
25 25 :versions,
26 26 :issue_statuses, :issue_categories, :issue_relations, :workflows,
27 27 :enumerations,
28 28 :issues, :journals, :journal_details,
29 29 :custom_fields, :custom_fields_projects, :custom_fields_trackers, :custom_values,
30 30 :time_entries
31 31
32 32 include Redmine::I18n
33 33
34 34 def teardown
35 35 User.current = nil
36 36 end
37 37
38 38 def test_create
39 39 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
40 40 :status_id => 1, :priority => IssuePriority.all.first,
41 41 :subject => 'test_create',
42 42 :description => 'IssueTest#test_create', :estimated_hours => '1:30')
43 43 assert issue.save
44 44 issue.reload
45 45 assert_equal 1.5, issue.estimated_hours
46 46 end
47 47
48 48 def test_create_minimal
49 49 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3,
50 50 :status_id => 1, :priority => IssuePriority.all.first,
51 51 :subject => 'test_create')
52 52 assert issue.save
53 53 assert issue.description.nil?
54 54 assert_nil issue.estimated_hours
55 55 end
56 56
57 57 def test_create_with_required_custom_field
58 58 set_language_if_valid 'en'
59 59 field = IssueCustomField.find_by_name('Database')
60 60 field.update_attribute(:is_required, true)
61 61
62 62 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
63 63 :status_id => 1, :subject => 'test_create',
64 64 :description => 'IssueTest#test_create_with_required_custom_field')
65 65 assert issue.available_custom_fields.include?(field)
66 66 # No value for the custom field
67 67 assert !issue.save
68 68 assert_equal ["Database can't be blank"], issue.errors.full_messages
69 69 # Blank value
70 70 issue.custom_field_values = { field.id => '' }
71 71 assert !issue.save
72 72 assert_equal ["Database can't be blank"], issue.errors.full_messages
73 73 # Invalid value
74 74 issue.custom_field_values = { field.id => 'SQLServer' }
75 75 assert !issue.save
76 76 assert_equal ["Database is not included in the list"], issue.errors.full_messages
77 77 # Valid value
78 78 issue.custom_field_values = { field.id => 'PostgreSQL' }
79 79 assert issue.save
80 80 issue.reload
81 81 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
82 82 end
83 83
84 84 def test_create_with_group_assignment
85 85 with_settings :issue_group_assignment => '1' do
86 86 assert Issue.new(:project_id => 2, :tracker_id => 1, :author_id => 1,
87 87 :subject => 'Group assignment',
88 88 :assigned_to_id => 11).save
89 89 issue = Issue.first(:order => 'id DESC')
90 90 assert_kind_of Group, issue.assigned_to
91 91 assert_equal Group.find(11), issue.assigned_to
92 92 end
93 93 end
94 94
95 95 def test_create_with_parent_issue_id
96 96 issue = Issue.new(:project_id => 1, :tracker_id => 1,
97 97 :author_id => 1, :subject => 'Group assignment',
98 98 :parent_issue_id => 1)
99 99 assert_save issue
100 100 assert_equal 1, issue.parent_issue_id
101 101 assert_equal Issue.find(1), issue.parent
102 102 end
103 103
104 104 def test_create_with_sharp_parent_issue_id
105 105 issue = Issue.new(:project_id => 1, :tracker_id => 1,
106 106 :author_id => 1, :subject => 'Group assignment',
107 107 :parent_issue_id => "#1")
108 108 assert_save issue
109 109 assert_equal 1, issue.parent_issue_id
110 110 assert_equal Issue.find(1), issue.parent
111 111 end
112 112
113 113 def test_create_with_invalid_parent_issue_id
114 114 set_language_if_valid 'en'
115 115 issue = Issue.new(:project_id => 1, :tracker_id => 1,
116 116 :author_id => 1, :subject => 'Group assignment',
117 117 :parent_issue_id => '01ABC')
118 118 assert !issue.save
119 119 assert_equal '01ABC', issue.parent_issue_id
120 120 assert_include 'Parent task is invalid', issue.errors.full_messages
121 121 end
122 122
123 123 def test_create_with_invalid_sharp_parent_issue_id
124 124 set_language_if_valid 'en'
125 125 issue = Issue.new(:project_id => 1, :tracker_id => 1,
126 126 :author_id => 1, :subject => 'Group assignment',
127 127 :parent_issue_id => '#01ABC')
128 128 assert !issue.save
129 129 assert_equal '#01ABC', issue.parent_issue_id
130 130 assert_include 'Parent task is invalid', issue.errors.full_messages
131 131 end
132 132
133 133 def assert_visibility_match(user, issues)
134 134 assert_equal issues.collect(&:id).sort, Issue.all.select {|issue| issue.visible?(user)}.collect(&:id).sort
135 135 end
136 136
137 137 def test_visible_scope_for_anonymous
138 138 # Anonymous user should see issues of public projects only
139 139 issues = Issue.visible(User.anonymous).all
140 140 assert issues.any?
141 141 assert_nil issues.detect {|issue| !issue.project.is_public?}
142 142 assert_nil issues.detect {|issue| issue.is_private?}
143 143 assert_visibility_match User.anonymous, issues
144 144 end
145 145
146 146 def test_visible_scope_for_anonymous_without_view_issues_permissions
147 147 # Anonymous user should not see issues without permission
148 148 Role.anonymous.remove_permission!(:view_issues)
149 149 issues = Issue.visible(User.anonymous).all
150 150 assert issues.empty?
151 151 assert_visibility_match User.anonymous, issues
152 152 end
153 153
154 154 def test_anonymous_should_not_see_private_issues_with_issues_visibility_set_to_default
155 155 assert Role.anonymous.update_attribute(:issues_visibility, 'default')
156 156 issue = Issue.generate!(:author => User.anonymous, :assigned_to => User.anonymous, :is_private => true)
157 157 assert_nil Issue.where(:id => issue.id).visible(User.anonymous).first
158 158 assert !issue.visible?(User.anonymous)
159 159 end
160 160
161 161 def test_anonymous_should_not_see_private_issues_with_issues_visibility_set_to_own
162 162 assert Role.anonymous.update_attribute(:issues_visibility, 'own')
163 163 issue = Issue.generate!(:author => User.anonymous, :assigned_to => User.anonymous, :is_private => true)
164 164 assert_nil Issue.where(:id => issue.id).visible(User.anonymous).first
165 165 assert !issue.visible?(User.anonymous)
166 166 end
167 167
168 168 def test_visible_scope_for_non_member
169 169 user = User.find(9)
170 170 assert user.projects.empty?
171 171 # Non member user should see issues of public projects only
172 172 issues = Issue.visible(user).all
173 173 assert issues.any?
174 174 assert_nil issues.detect {|issue| !issue.project.is_public?}
175 175 assert_nil issues.detect {|issue| issue.is_private?}
176 176 assert_visibility_match user, issues
177 177 end
178 178
179 179 def test_visible_scope_for_non_member_with_own_issues_visibility
180 180 Role.non_member.update_attribute :issues_visibility, 'own'
181 181 Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 9, :subject => 'Issue by non member')
182 182 user = User.find(9)
183 183
184 184 issues = Issue.visible(user).all
185 185 assert issues.any?
186 186 assert_nil issues.detect {|issue| issue.author != user}
187 187 assert_visibility_match user, issues
188 188 end
189 189
190 190 def test_visible_scope_for_non_member_without_view_issues_permissions
191 191 # Non member user should not see issues without permission
192 192 Role.non_member.remove_permission!(:view_issues)
193 193 user = User.find(9)
194 194 assert user.projects.empty?
195 195 issues = Issue.visible(user).all
196 196 assert issues.empty?
197 197 assert_visibility_match user, issues
198 198 end
199 199
200 200 def test_visible_scope_for_member
201 201 user = User.find(9)
202 202 # User should see issues of projects for which he has view_issues permissions only
203 203 Role.non_member.remove_permission!(:view_issues)
204 204 Member.create!(:principal => user, :project_id => 3, :role_ids => [2])
205 205 issues = Issue.visible(user).all
206 206 assert issues.any?
207 207 assert_nil issues.detect {|issue| issue.project_id != 3}
208 208 assert_nil issues.detect {|issue| issue.is_private?}
209 209 assert_visibility_match user, issues
210 210 end
211 211
212 212 def test_visible_scope_for_member_with_groups_should_return_assigned_issues
213 213 user = User.find(8)
214 214 assert user.groups.any?
215 215 Member.create!(:principal => user.groups.first, :project_id => 1, :role_ids => [2])
216 216 Role.non_member.remove_permission!(:view_issues)
217 217
218 218 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3,
219 219 :status_id => 1, :priority => IssuePriority.all.first,
220 220 :subject => 'Assignment test',
221 221 :assigned_to => user.groups.first,
222 222 :is_private => true)
223 223
224 224 Role.find(2).update_attribute :issues_visibility, 'default'
225 225 issues = Issue.visible(User.find(8)).all
226 226 assert issues.any?
227 227 assert issues.include?(issue)
228 228
229 229 Role.find(2).update_attribute :issues_visibility, 'own'
230 230 issues = Issue.visible(User.find(8)).all
231 231 assert issues.any?
232 232 assert issues.include?(issue)
233 233 end
234 234
235 235 def test_visible_scope_for_admin
236 236 user = User.find(1)
237 237 user.members.each(&:destroy)
238 238 assert user.projects.empty?
239 239 issues = Issue.visible(user).all
240 240 assert issues.any?
241 241 # Admin should see issues on private projects that he does not belong to
242 242 assert issues.detect {|issue| !issue.project.is_public?}
243 243 # Admin should see private issues of other users
244 244 assert issues.detect {|issue| issue.is_private? && issue.author != user}
245 245 assert_visibility_match user, issues
246 246 end
247 247
248 248 def test_visible_scope_with_project
249 249 project = Project.find(1)
250 250 issues = Issue.visible(User.find(2), :project => project).all
251 251 projects = issues.collect(&:project).uniq
252 252 assert_equal 1, projects.size
253 253 assert_equal project, projects.first
254 254 end
255 255
256 256 def test_visible_scope_with_project_and_subprojects
257 257 project = Project.find(1)
258 258 issues = Issue.visible(User.find(2), :project => project, :with_subprojects => true).all
259 259 projects = issues.collect(&:project).uniq
260 260 assert projects.size > 1
261 261 assert_equal [], projects.select {|p| !p.is_or_is_descendant_of?(project)}
262 262 end
263 263
264 264 def test_visible_and_nested_set_scopes
265 265 assert_equal 0, Issue.find(1).descendants.visible.all.size
266 266 end
267 267
268 268 def test_open_scope
269 269 issues = Issue.open.all
270 270 assert_nil issues.detect(&:closed?)
271 271 end
272 272
273 273 def test_open_scope_with_arg
274 274 issues = Issue.open(false).all
275 275 assert_equal issues, issues.select(&:closed?)
276 276 end
277 277
278 278 def test_errors_full_messages_should_include_custom_fields_errors
279 279 field = IssueCustomField.find_by_name('Database')
280 280
281 281 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
282 282 :status_id => 1, :subject => 'test_create',
283 283 :description => 'IssueTest#test_create_with_required_custom_field')
284 284 assert issue.available_custom_fields.include?(field)
285 285 # Invalid value
286 286 issue.custom_field_values = { field.id => 'SQLServer' }
287 287
288 288 assert !issue.valid?
289 289 assert_equal 1, issue.errors.full_messages.size
290 290 assert_equal "Database #{I18n.translate('activerecord.errors.messages.inclusion')}",
291 291 issue.errors.full_messages.first
292 292 end
293 293
294 294 def test_update_issue_with_required_custom_field
295 295 field = IssueCustomField.find_by_name('Database')
296 296 field.update_attribute(:is_required, true)
297 297
298 298 issue = Issue.find(1)
299 299 assert_nil issue.custom_value_for(field)
300 300 assert issue.available_custom_fields.include?(field)
301 301 # No change to custom values, issue can be saved
302 302 assert issue.save
303 303 # Blank value
304 304 issue.custom_field_values = { field.id => '' }
305 305 assert !issue.save
306 306 # Valid value
307 307 issue.custom_field_values = { field.id => 'PostgreSQL' }
308 308 assert issue.save
309 309 issue.reload
310 310 assert_equal 'PostgreSQL', issue.custom_value_for(field).value
311 311 end
312 312
313 313 def test_should_not_update_attributes_if_custom_fields_validation_fails
314 314 issue = Issue.find(1)
315 315 field = IssueCustomField.find_by_name('Database')
316 316 assert issue.available_custom_fields.include?(field)
317 317
318 318 issue.custom_field_values = { field.id => 'Invalid' }
319 319 issue.subject = 'Should be not be saved'
320 320 assert !issue.save
321 321
322 322 issue.reload
323 323 assert_equal "Can't print recipes", issue.subject
324 324 end
325 325
326 326 def test_should_not_recreate_custom_values_objects_on_update
327 327 field = IssueCustomField.find_by_name('Database')
328 328
329 329 issue = Issue.find(1)
330 330 issue.custom_field_values = { field.id => 'PostgreSQL' }
331 331 assert issue.save
332 332 custom_value = issue.custom_value_for(field)
333 333 issue.reload
334 334 issue.custom_field_values = { field.id => 'MySQL' }
335 335 assert issue.save
336 336 issue.reload
337 337 assert_equal custom_value.id, issue.custom_value_for(field).id
338 338 end
339 339
340 340 def test_should_not_update_custom_fields_on_changing_tracker_with_different_custom_fields
341 issue = Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :subject => 'Test', :custom_field_values => {'2' => 'Test'})
341 issue = Issue.create!(:project_id => 1, :tracker_id => 1, :author_id => 1,
342 :status_id => 1, :subject => 'Test',
343 :custom_field_values => {'2' => 'Test'})
342 344 assert !Tracker.find(2).custom_field_ids.include?(2)
343 345
344 346 issue = Issue.find(issue.id)
345 347 issue.attributes = {:tracker_id => 2, :custom_field_values => {'1' => ''}}
346 348
347 349 issue = Issue.find(issue.id)
348 350 custom_value = issue.custom_value_for(2)
349 351 assert_not_nil custom_value
350 352 assert_equal 'Test', custom_value.value
351 353 end
352 354
353 355 def test_assigning_tracker_id_should_reload_custom_fields_values
354 356 issue = Issue.new(:project => Project.find(1))
355 357 assert issue.custom_field_values.empty?
356 358 issue.tracker_id = 1
357 359 assert issue.custom_field_values.any?
358 360 end
359 361
360 362 def test_assigning_attributes_should_assign_project_and_tracker_first
361 363 seq = sequence('seq')
362 364 issue = Issue.new
363 365 issue.expects(:project_id=).in_sequence(seq)
364 366 issue.expects(:tracker_id=).in_sequence(seq)
365 367 issue.expects(:subject=).in_sequence(seq)
366 368 issue.attributes = {:tracker_id => 2, :project_id => 1, :subject => 'Test'}
367 369 end
368 370
369 371 def test_assigning_tracker_and_custom_fields_should_assign_custom_fields
370 372 attributes = ActiveSupport::OrderedHash.new
371 373 attributes['custom_field_values'] = { '1' => 'MySQL' }
372 374 attributes['tracker_id'] = '1'
373 375 issue = Issue.new(:project => Project.find(1))
374 376 issue.attributes = attributes
375 377 assert_equal 'MySQL', issue.custom_field_value(1)
376 378 end
377 379
378 380 def test_should_update_issue_with_disabled_tracker
379 381 p = Project.find(1)
380 382 issue = Issue.find(1)
381 383
382 384 p.trackers.delete(issue.tracker)
383 385 assert !p.trackers.include?(issue.tracker)
384 386
385 387 issue.reload
386 388 issue.subject = 'New subject'
387 389 assert issue.save
388 390 end
389 391
390 392 def test_should_not_set_a_disabled_tracker
391 393 p = Project.find(1)
392 394 p.trackers.delete(Tracker.find(2))
393 395
394 396 issue = Issue.find(1)
395 397 issue.tracker_id = 2
396 398 issue.subject = 'New subject'
397 399 assert !issue.save
398 400 assert_not_nil issue.errors[:tracker_id]
399 401 end
400 402
401 403 def test_category_based_assignment
402 404 issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3,
403 405 :status_id => 1, :priority => IssuePriority.all.first,
404 406 :subject => 'Assignment test',
405 407 :description => 'Assignment test', :category_id => 1)
406 408 assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to
407 409 end
408 410
409 411 def test_new_statuses_allowed_to
410 412 WorkflowTransition.delete_all
411 413
412 414 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 2, :author => false, :assignee => false)
413 415 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 3, :author => true, :assignee => false)
414 416 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 4, :author => false, :assignee => true)
415 417 WorkflowTransition.create!(:role_id => 1, :tracker_id => 1, :old_status_id => 1, :new_status_id => 5, :author => true, :assignee => true)
416 418 status = IssueStatus.find(1)
417 419 role = Role.find(1)
418 420 tracker = Tracker.find(1)
419 421 user = User.find(2)
420 422
421 423 issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author_id => 1)
422 424 assert_equal [1, 2], issue.new_statuses_allowed_to(user).map(&:id)
423 425
424 426 issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author => user)
425 427 assert_equal [1, 2, 3, 5], issue.new_statuses_allowed_to(user).map(&:id)
426 428
427 429 issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author_id => 1, :assigned_to => user)
428 430 assert_equal [1, 2, 4, 5], issue.new_statuses_allowed_to(user).map(&:id)
429 431
430 432 issue = Issue.generate!(:tracker => tracker, :status => status, :project_id => 1, :author => user, :assigned_to => user)
431 433 assert_equal [1, 2, 3, 4, 5], issue.new_statuses_allowed_to(user).map(&:id)
432 434 end
433 435
434 436 def test_new_statuses_allowed_to_should_return_all_transitions_for_admin
435 437 admin = User.find(1)
436 438 issue = Issue.find(1)
437 439 assert !admin.member_of?(issue.project)
438 440 expected_statuses = [issue.status] + WorkflowTransition.find_all_by_old_status_id(issue.status_id).map(&:new_status).uniq.sort
439 441
440 442 assert_equal expected_statuses, issue.new_statuses_allowed_to(admin)
441 443 end
442 444
443 445 def test_new_statuses_allowed_to_should_return_default_and_current_status_when_copying
444 446 issue = Issue.find(1).copy
445 447 assert_equal [1], issue.new_statuses_allowed_to(User.find(2)).map(&:id)
446 448
447 449 issue = Issue.find(2).copy
448 450 assert_equal [1, 2], issue.new_statuses_allowed_to(User.find(2)).map(&:id)
449 451 end
450 452
451 453 def test_safe_attributes_names_should_not_include_disabled_field
452 454 tracker = Tracker.new(:core_fields => %w(assigned_to_id fixed_version_id))
453 455
454 456 issue = Issue.new(:tracker => tracker)
455 457 assert_include 'tracker_id', issue.safe_attribute_names
456 458 assert_include 'status_id', issue.safe_attribute_names
457 459 assert_include 'subject', issue.safe_attribute_names
458 460 assert_include 'description', issue.safe_attribute_names
459 461 assert_include 'custom_field_values', issue.safe_attribute_names
460 462 assert_include 'custom_fields', issue.safe_attribute_names
461 463 assert_include 'lock_version', issue.safe_attribute_names
462 464
463 465 tracker.core_fields.each do |field|
464 466 assert_include field, issue.safe_attribute_names
465 467 end
466 468
467 469 tracker.disabled_core_fields.each do |field|
468 470 assert_not_include field, issue.safe_attribute_names
469 471 end
470 472 end
471 473
472 474 def test_safe_attributes_should_ignore_disabled_fields
473 475 tracker = Tracker.find(1)
474 476 tracker.core_fields = %w(assigned_to_id due_date)
475 477 tracker.save!
476 478
477 479 issue = Issue.new(:tracker => tracker)
478 480 issue.safe_attributes = {'start_date' => '2012-07-14', 'due_date' => '2012-07-14'}
479 481 assert_nil issue.start_date
480 482 assert_equal Date.parse('2012-07-14'), issue.due_date
481 483 end
482 484
483 485 def test_safe_attributes_should_accept_target_tracker_enabled_fields
484 486 source = Tracker.find(1)
485 487 source.core_fields = []
486 488 source.save!
487 489 target = Tracker.find(2)
488 490 target.core_fields = %w(assigned_to_id due_date)
489 491 target.save!
490 492
491 493 issue = Issue.new(:tracker => source)
492 494 issue.safe_attributes = {'tracker_id' => 2, 'due_date' => '2012-07-14'}
493 495 assert_equal target, issue.tracker
494 496 assert_equal Date.parse('2012-07-14'), issue.due_date
495 497 end
496 498
497 499 def test_safe_attributes_should_not_include_readonly_fields
498 500 WorkflowPermission.delete_all
499 501 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
500 502 user = User.find(2)
501 503
502 504 issue = Issue.new(:project_id => 1, :tracker_id => 1)
503 505 assert_equal %w(due_date), issue.read_only_attribute_names(user)
504 506 assert_not_include 'due_date', issue.safe_attribute_names(user)
505 507
506 508 issue.send :safe_attributes=, {'start_date' => '2012-07-14', 'due_date' => '2012-07-14'}, user
507 509 assert_equal Date.parse('2012-07-14'), issue.start_date
508 510 assert_nil issue.due_date
509 511 end
510 512
511 513 def test_safe_attributes_should_not_include_readonly_custom_fields
512 514 cf1 = IssueCustomField.create!(:name => 'Writable field', :field_format => 'string', :is_for_all => true, :tracker_ids => [1])
513 515 cf2 = IssueCustomField.create!(:name => 'Readonly field', :field_format => 'string', :is_for_all => true, :tracker_ids => [1])
514 516
515 517 WorkflowPermission.delete_all
516 518 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
517 519 user = User.find(2)
518 520
519 521 issue = Issue.new(:project_id => 1, :tracker_id => 1)
520 522 assert_equal [cf2.id.to_s], issue.read_only_attribute_names(user)
521 523 assert_not_include cf2.id.to_s, issue.safe_attribute_names(user)
522 524
523 525 issue.send :safe_attributes=, {'custom_field_values' => {cf1.id.to_s => 'value1', cf2.id.to_s => 'value2'}}, user
524 526 assert_equal 'value1', issue.custom_field_value(cf1)
525 527 assert_nil issue.custom_field_value(cf2)
526 528
527 529 issue.send :safe_attributes=, {'custom_fields' => [{'id' => cf1.id.to_s, 'value' => 'valuea'}, {'id' => cf2.id.to_s, 'value' => 'valueb'}]}, user
528 530 assert_equal 'valuea', issue.custom_field_value(cf1)
529 531 assert_nil issue.custom_field_value(cf2)
530 532 end
531 533
532 534 def test_editable_custom_field_values_should_return_non_readonly_custom_values
533 535 cf1 = IssueCustomField.create!(:name => 'Writable field', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
534 536 cf2 = IssueCustomField.create!(:name => 'Readonly field', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
535 537
536 538 WorkflowPermission.delete_all
537 539 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf2.id.to_s, :rule => 'readonly')
538 540 user = User.find(2)
539 541
540 542 issue = Issue.new(:project_id => 1, :tracker_id => 1)
541 543 values = issue.editable_custom_field_values(user)
542 544 assert values.detect {|value| value.custom_field == cf1}
543 545 assert_nil values.detect {|value| value.custom_field == cf2}
544 546
545 547 issue.tracker_id = 2
546 548 values = issue.editable_custom_field_values(user)
547 549 assert values.detect {|value| value.custom_field == cf1}
548 550 assert values.detect {|value| value.custom_field == cf2}
549 551 end
550 552
551 553 def test_safe_attributes_should_accept_target_tracker_writable_fields
552 554 WorkflowPermission.delete_all
553 555 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
554 556 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'start_date', :rule => 'readonly')
555 557 user = User.find(2)
556 558
557 559 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1)
558 560
559 561 issue.send :safe_attributes=, {'start_date' => '2012-07-12', 'due_date' => '2012-07-14'}, user
560 562 assert_equal Date.parse('2012-07-12'), issue.start_date
561 563 assert_nil issue.due_date
562 564
563 565 issue.send :safe_attributes=, {'start_date' => '2012-07-15', 'due_date' => '2012-07-16', 'tracker_id' => 2}, user
564 566 assert_equal Date.parse('2012-07-12'), issue.start_date
565 567 assert_equal Date.parse('2012-07-16'), issue.due_date
566 568 end
567 569
568 570 def test_safe_attributes_should_accept_target_status_writable_fields
569 571 WorkflowPermission.delete_all
570 572 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
571 573 WorkflowPermission.create!(:old_status_id => 2, :tracker_id => 1, :role_id => 1, :field_name => 'start_date', :rule => 'readonly')
572 574 user = User.find(2)
573 575
574 576 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1)
575 577
576 578 issue.send :safe_attributes=, {'start_date' => '2012-07-12', 'due_date' => '2012-07-14'}, user
577 579 assert_equal Date.parse('2012-07-12'), issue.start_date
578 580 assert_nil issue.due_date
579 581
580 582 issue.send :safe_attributes=, {'start_date' => '2012-07-15', 'due_date' => '2012-07-16', 'status_id' => 2}, user
581 583 assert_equal Date.parse('2012-07-12'), issue.start_date
582 584 assert_equal Date.parse('2012-07-16'), issue.due_date
583 585 end
584 586
585 587 def test_required_attributes_should_be_validated
586 588 cf = IssueCustomField.create!(:name => 'Foo', :field_format => 'string', :is_for_all => true, :tracker_ids => [1, 2])
587 589
588 590 WorkflowPermission.delete_all
589 591 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'required')
590 592 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'category_id', :rule => 'required')
591 593 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => cf.id.to_s, :rule => 'required')
592 594
593 595 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => 'start_date', :rule => 'required')
594 596 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 2, :role_id => 1, :field_name => cf.id.to_s, :rule => 'required')
595 597 user = User.find(2)
596 598
597 599 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1, :subject => 'Required fields', :author => user)
598 600 assert_equal [cf.id.to_s, "category_id", "due_date"], issue.required_attribute_names(user).sort
599 601 assert !issue.save, "Issue was saved"
600 602 assert_equal ["Category can't be blank", "Due date can't be blank", "Foo can't be blank"], issue.errors.full_messages.sort
601 603
602 604 issue.tracker_id = 2
603 605 assert_equal [cf.id.to_s, "start_date"], issue.required_attribute_names(user).sort
604 606 assert !issue.save, "Issue was saved"
605 607 assert_equal ["Foo can't be blank", "Start date can't be blank"], issue.errors.full_messages.sort
606 608
607 609 issue.start_date = Date.today
608 610 issue.custom_field_values = {cf.id.to_s => 'bar'}
609 611 assert issue.save
610 612 end
611 613
612 614 def test_required_attribute_names_for_multiple_roles_should_intersect_rules
613 615 WorkflowPermission.delete_all
614 616 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'required')
615 617 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'start_date', :rule => 'required')
616 618 user = User.find(2)
617 619 member = Member.find(1)
618 620 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1)
619 621
620 622 assert_equal %w(due_date start_date), issue.required_attribute_names(user).sort
621 623
622 624 member.role_ids = [1, 2]
623 625 member.save!
624 626 assert_equal [], issue.required_attribute_names(user.reload)
625 627
626 628 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 2, :field_name => 'due_date', :rule => 'required')
627 629 assert_equal %w(due_date), issue.required_attribute_names(user)
628 630
629 631 member.role_ids = [1, 2, 3]
630 632 member.save!
631 633 assert_equal [], issue.required_attribute_names(user.reload)
632 634
633 635 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 2, :field_name => 'due_date', :rule => 'readonly')
634 636 # required + readonly => required
635 637 assert_equal %w(due_date), issue.required_attribute_names(user)
636 638 end
637 639
638 640 def test_read_only_attribute_names_for_multiple_roles_should_intersect_rules
639 641 WorkflowPermission.delete_all
640 642 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
641 643 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'start_date', :rule => 'readonly')
642 644 user = User.find(2)
643 645 member = Member.find(1)
644 646 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1)
645 647
646 648 assert_equal %w(due_date start_date), issue.read_only_attribute_names(user).sort
647 649
648 650 member.role_ids = [1, 2]
649 651 member.save!
650 652 assert_equal [], issue.read_only_attribute_names(user.reload)
651 653
652 654 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 2, :field_name => 'due_date', :rule => 'readonly')
653 655 assert_equal %w(due_date), issue.read_only_attribute_names(user)
654 656 end
655 657
656 658 def test_copy
657 659 issue = Issue.new.copy_from(1)
658 660 assert issue.copy?
659 661 assert issue.save
660 662 issue.reload
661 663 orig = Issue.find(1)
662 664 assert_equal orig.subject, issue.subject
663 665 assert_equal orig.tracker, issue.tracker
664 666 assert_equal "125", issue.custom_value_for(2).value
665 667 end
666 668
667 669 def test_copy_should_copy_status
668 670 orig = Issue.find(8)
669 671 assert orig.status != IssueStatus.default
670 672
671 673 issue = Issue.new.copy_from(orig)
672 674 assert issue.save
673 675 issue.reload
674 676 assert_equal orig.status, issue.status
675 677 end
676 678
677 679 def test_copy_should_add_relation_with_copied_issue
678 680 copied = Issue.find(1)
679 681 issue = Issue.new.copy_from(copied)
680 682 assert issue.save
681 683 issue.reload
682 684
683 685 assert_equal 1, issue.relations.size
684 686 relation = issue.relations.first
685 687 assert_equal 'copied_to', relation.relation_type
686 688 assert_equal copied, relation.issue_from
687 689 assert_equal issue, relation.issue_to
688 690 end
689 691
690 692 def test_copy_should_copy_subtasks
691 693 issue = Issue.generate_with_descendants!
692 694
693 695 copy = issue.reload.copy
694 696 copy.author = User.find(7)
695 697 assert_difference 'Issue.count', 1+issue.descendants.count do
696 698 assert copy.save
697 699 end
698 700 copy.reload
699 701 assert_equal %w(Child1 Child2), copy.children.map(&:subject).sort
700 702 child_copy = copy.children.detect {|c| c.subject == 'Child1'}
701 703 assert_equal %w(Child11), child_copy.children.map(&:subject).sort
702 704 assert_equal copy.author, child_copy.author
703 705 end
704 706
705 707 def test_copy_should_copy_subtasks_to_target_project
706 708 issue = Issue.generate_with_descendants!
707 709
708 710 copy = issue.copy(:project_id => 3)
709 711 assert_difference 'Issue.count', 1+issue.descendants.count do
710 712 assert copy.save
711 713 end
712 714 assert_equal [3], copy.reload.descendants.map(&:project_id).uniq
713 715 end
714 716
715 717 def test_copy_should_not_copy_subtasks_twice_when_saving_twice
716 718 issue = Issue.generate_with_descendants!
717 719
718 720 copy = issue.reload.copy
719 721 assert_difference 'Issue.count', 1+issue.descendants.count do
720 722 assert copy.save
721 723 assert copy.save
722 724 end
723 725 end
724 726
725 727 def test_should_not_call_after_project_change_on_creation
726 728 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1, :subject => 'Test', :author_id => 1)
727 729 issue.expects(:after_project_change).never
728 730 issue.save!
729 731 end
730 732
731 733 def test_should_not_call_after_project_change_on_update
732 734 issue = Issue.find(1)
733 735 issue.project = Project.find(1)
734 736 issue.subject = 'No project change'
735 737 issue.expects(:after_project_change).never
736 738 issue.save!
737 739 end
738 740
739 741 def test_should_call_after_project_change_on_project_change
740 742 issue = Issue.find(1)
741 743 issue.project = Project.find(2)
742 744 issue.expects(:after_project_change).once
743 745 issue.save!
744 746 end
745 747
746 748 def test_adding_journal_should_update_timestamp
747 749 issue = Issue.find(1)
748 750 updated_on_was = issue.updated_on
749 751
750 752 issue.init_journal(User.first, "Adding notes")
751 753 assert_difference 'Journal.count' do
752 754 assert issue.save
753 755 end
754 756 issue.reload
755 757
756 758 assert_not_equal updated_on_was, issue.updated_on
757 759 end
758 760
759 761 def test_should_close_duplicates
760 762 # Create 3 issues
761 763 issue1 = Issue.generate!
762 764 issue2 = Issue.generate!
763 765 issue3 = Issue.generate!
764 766
765 767 # 2 is a dupe of 1
766 768 IssueRelation.create!(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
767 769 # And 3 is a dupe of 2
768 770 IssueRelation.create!(:issue_from => issue3, :issue_to => issue2, :relation_type => IssueRelation::TYPE_DUPLICATES)
769 771 # And 3 is a dupe of 1 (circular duplicates)
770 772 IssueRelation.create!(:issue_from => issue3, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
771 773
772 774 assert issue1.reload.duplicates.include?(issue2)
773 775
774 776 # Closing issue 1
775 777 issue1.init_journal(User.find(:first), "Closing issue1")
776 778 issue1.status = IssueStatus.find :first, :conditions => {:is_closed => true}
777 779 assert issue1.save
778 780 # 2 and 3 should be also closed
779 781 assert issue2.reload.closed?
780 782 assert issue3.reload.closed?
781 783 end
782 784
783 785 def test_should_not_close_duplicated_issue
784 786 issue1 = Issue.generate!
785 787 issue2 = Issue.generate!
786 788
787 789 # 2 is a dupe of 1
788 790 IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
789 791 # 2 is a dup of 1 but 1 is not a duplicate of 2
790 792 assert !issue2.reload.duplicates.include?(issue1)
791 793
792 794 # Closing issue 2
793 795 issue2.init_journal(User.find(:first), "Closing issue2")
794 796 issue2.status = IssueStatus.find :first, :conditions => {:is_closed => true}
795 797 assert issue2.save
796 798 # 1 should not be also closed
797 799 assert !issue1.reload.closed?
798 800 end
799 801
800 802 def test_assignable_versions
801 803 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 1, :subject => 'New issue')
802 804 assert_equal ['open'], issue.assignable_versions.collect(&:status).uniq
803 805 end
804 806
805 807 def test_should_not_be_able_to_assign_a_new_issue_to_a_closed_version
806 808 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 1, :subject => 'New issue')
807 809 assert !issue.save
808 810 assert_not_nil issue.errors[:fixed_version_id]
809 811 end
810 812
811 813 def test_should_not_be_able_to_assign_a_new_issue_to_a_locked_version
812 814 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 2, :subject => 'New issue')
813 815 assert !issue.save
814 816 assert_not_nil issue.errors[:fixed_version_id]
815 817 end
816 818
817 819 def test_should_be_able_to_assign_a_new_issue_to_an_open_version
818 820 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 3, :subject => 'New issue')
819 821 assert issue.save
820 822 end
821 823
822 824 def test_should_be_able_to_update_an_issue_assigned_to_a_closed_version
823 825 issue = Issue.find(11)
824 826 assert_equal 'closed', issue.fixed_version.status
825 827 issue.subject = 'Subject changed'
826 828 assert issue.save
827 829 end
828 830
829 831 def test_should_not_be_able_to_reopen_an_issue_assigned_to_a_closed_version
830 832 issue = Issue.find(11)
831 833 issue.status_id = 1
832 834 assert !issue.save
833 835 assert_not_nil issue.errors[:base]
834 836 end
835 837
836 838 def test_should_be_able_to_reopen_and_reassign_an_issue_assigned_to_a_closed_version
837 839 issue = Issue.find(11)
838 840 issue.status_id = 1
839 841 issue.fixed_version_id = 3
840 842 assert issue.save
841 843 end
842 844
843 845 def test_should_be_able_to_reopen_an_issue_assigned_to_a_locked_version
844 846 issue = Issue.find(12)
845 847 assert_equal 'locked', issue.fixed_version.status
846 848 issue.status_id = 1
847 849 assert issue.save
848 850 end
849 851
850 852 def test_should_not_be_able_to_keep_unshared_version_when_changing_project
851 853 issue = Issue.find(2)
852 854 assert_equal 2, issue.fixed_version_id
853 855 issue.project_id = 3
854 856 assert_nil issue.fixed_version_id
855 857 issue.fixed_version_id = 2
856 858 assert !issue.save
857 859 assert_include 'Target version is not included in the list', issue.errors.full_messages
858 860 end
859 861
860 862 def test_should_keep_shared_version_when_changing_project
861 863 Version.find(2).update_attribute :sharing, 'tree'
862 864
863 865 issue = Issue.find(2)
864 866 assert_equal 2, issue.fixed_version_id
865 867 issue.project_id = 3
866 868 assert_equal 2, issue.fixed_version_id
867 869 assert issue.save
868 870 end
869 871
870 872 def test_allowed_target_projects_on_move_should_include_projects_with_issue_tracking_enabled
871 873 assert_include Project.find(2), Issue.allowed_target_projects_on_move(User.find(2))
872 874 end
873 875
874 876 def test_allowed_target_projects_on_move_should_not_include_projects_with_issue_tracking_disabled
875 877 Project.find(2).disable_module! :issue_tracking
876 878 assert_not_include Project.find(2), Issue.allowed_target_projects_on_move(User.find(2))
877 879 end
878 880
879 881 def test_move_to_another_project_with_same_category
880 882 issue = Issue.find(1)
881 883 issue.project = Project.find(2)
882 884 assert issue.save
883 885 issue.reload
884 886 assert_equal 2, issue.project_id
885 887 # Category changes
886 888 assert_equal 4, issue.category_id
887 889 # Make sure time entries were move to the target project
888 890 assert_equal 2, issue.time_entries.first.project_id
889 891 end
890 892
891 893 def test_move_to_another_project_without_same_category
892 894 issue = Issue.find(2)
893 895 issue.project = Project.find(2)
894 896 assert issue.save
895 897 issue.reload
896 898 assert_equal 2, issue.project_id
897 899 # Category cleared
898 900 assert_nil issue.category_id
899 901 end
900 902
901 903 def test_move_to_another_project_should_clear_fixed_version_when_not_shared
902 904 issue = Issue.find(1)
903 905 issue.update_attribute(:fixed_version_id, 1)
904 906 issue.project = Project.find(2)
905 907 assert issue.save
906 908 issue.reload
907 909 assert_equal 2, issue.project_id
908 910 # Cleared fixed_version
909 911 assert_equal nil, issue.fixed_version
910 912 end
911 913
912 914 def test_move_to_another_project_should_keep_fixed_version_when_shared_with_the_target_project
913 915 issue = Issue.find(1)
914 916 issue.update_attribute(:fixed_version_id, 4)
915 917 issue.project = Project.find(5)
916 918 assert issue.save
917 919 issue.reload
918 920 assert_equal 5, issue.project_id
919 921 # Keep fixed_version
920 922 assert_equal 4, issue.fixed_version_id
921 923 end
922 924
923 925 def test_move_to_another_project_should_clear_fixed_version_when_not_shared_with_the_target_project
924 926 issue = Issue.find(1)
925 927 issue.update_attribute(:fixed_version_id, 1)
926 928 issue.project = Project.find(5)
927 929 assert issue.save
928 930 issue.reload
929 931 assert_equal 5, issue.project_id
930 932 # Cleared fixed_version
931 933 assert_equal nil, issue.fixed_version
932 934 end
933 935
934 936 def test_move_to_another_project_should_keep_fixed_version_when_shared_systemwide
935 937 issue = Issue.find(1)
936 938 issue.update_attribute(:fixed_version_id, 7)
937 939 issue.project = Project.find(2)
938 940 assert issue.save
939 941 issue.reload
940 942 assert_equal 2, issue.project_id
941 943 # Keep fixed_version
942 944 assert_equal 7, issue.fixed_version_id
943 945 end
944 946
945 947 def test_move_to_another_project_should_keep_parent_if_valid
946 948 issue = Issue.find(1)
947 949 issue.update_attribute(:parent_issue_id, 2)
948 950 issue.project = Project.find(3)
949 951 assert issue.save
950 952 issue.reload
951 953 assert_equal 2, issue.parent_id
952 954 end
953 955
954 956 def test_move_to_another_project_should_clear_parent_if_not_valid
955 957 issue = Issue.find(1)
956 958 issue.update_attribute(:parent_issue_id, 2)
957 959 issue.project = Project.find(2)
958 960 assert issue.save
959 961 issue.reload
960 962 assert_nil issue.parent_id
961 963 end
962 964
963 965 def test_move_to_another_project_with_disabled_tracker
964 966 issue = Issue.find(1)
965 967 target = Project.find(2)
966 968 target.tracker_ids = [3]
967 969 target.save
968 970 issue.project = target
969 971 assert issue.save
970 972 issue.reload
971 973 assert_equal 2, issue.project_id
972 974 assert_equal 3, issue.tracker_id
973 975 end
974 976
975 977 def test_copy_to_the_same_project
976 978 issue = Issue.find(1)
977 979 copy = issue.copy
978 980 assert_difference 'Issue.count' do
979 981 copy.save!
980 982 end
981 983 assert_kind_of Issue, copy
982 984 assert_equal issue.project, copy.project
983 985 assert_equal "125", copy.custom_value_for(2).value
984 986 end
985 987
986 988 def test_copy_to_another_project_and_tracker
987 989 issue = Issue.find(1)
988 990 copy = issue.copy(:project_id => 3, :tracker_id => 2)
989 991 assert_difference 'Issue.count' do
990 992 copy.save!
991 993 end
992 994 copy.reload
993 995 assert_kind_of Issue, copy
994 996 assert_equal Project.find(3), copy.project
995 997 assert_equal Tracker.find(2), copy.tracker
996 998 # Custom field #2 is not associated with target tracker
997 999 assert_nil copy.custom_value_for(2)
998 1000 end
999 1001
1000 1002 context "#copy" do
1001 1003 setup do
1002 1004 @issue = Issue.find(1)
1003 1005 end
1004 1006
1005 1007 should "not create a journal" do
1006 1008 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3)
1007 1009 copy.save!
1008 1010 assert_equal 0, copy.reload.journals.size
1009 1011 end
1010 1012
1011 1013 should "allow assigned_to changes" do
1012 1014 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3)
1013 1015 assert_equal 3, copy.assigned_to_id
1014 1016 end
1015 1017
1016 1018 should "allow status changes" do
1017 1019 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :status_id => 2)
1018 1020 assert_equal 2, copy.status_id
1019 1021 end
1020 1022
1021 1023 should "allow start date changes" do
1022 1024 date = Date.today
1023 1025 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :start_date => date)
1024 1026 assert_equal date, copy.start_date
1025 1027 end
1026 1028
1027 1029 should "allow due date changes" do
1028 1030 date = Date.today
1029 1031 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :due_date => date)
1030 1032 assert_equal date, copy.due_date
1031 1033 end
1032 1034
1033 1035 should "set current user as author" do
1034 1036 User.current = User.find(9)
1035 1037 copy = @issue.copy(:project_id => 3, :tracker_id => 2)
1036 1038 assert_equal User.current, copy.author
1037 1039 end
1038 1040
1039 1041 should "create a journal with notes" do
1040 1042 date = Date.today
1041 1043 notes = "Notes added when copying"
1042 1044 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :start_date => date)
1043 1045 copy.init_journal(User.current, notes)
1044 1046 copy.save!
1045 1047
1046 1048 assert_equal 1, copy.journals.size
1047 1049 journal = copy.journals.first
1048 1050 assert_equal 0, journal.details.size
1049 1051 assert_equal notes, journal.notes
1050 1052 end
1051 1053 end
1052 1054
1053 1055 def test_valid_parent_project
1054 1056 issue = Issue.find(1)
1055 1057 issue_in_same_project = Issue.find(2)
1056 1058 issue_in_child_project = Issue.find(5)
1057 1059 issue_in_grandchild_project = Issue.generate!(:project_id => 6, :tracker_id => 1)
1058 1060 issue_in_other_child_project = Issue.find(6)
1059 1061 issue_in_different_tree = Issue.find(4)
1060 1062
1061 1063 with_settings :cross_project_subtasks => '' do
1062 1064 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1063 1065 assert_equal false, issue.valid_parent_project?(issue_in_child_project)
1064 1066 assert_equal false, issue.valid_parent_project?(issue_in_grandchild_project)
1065 1067 assert_equal false, issue.valid_parent_project?(issue_in_different_tree)
1066 1068 end
1067 1069
1068 1070 with_settings :cross_project_subtasks => 'system' do
1069 1071 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1070 1072 assert_equal true, issue.valid_parent_project?(issue_in_child_project)
1071 1073 assert_equal true, issue.valid_parent_project?(issue_in_different_tree)
1072 1074 end
1073 1075
1074 1076 with_settings :cross_project_subtasks => 'tree' do
1075 1077 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1076 1078 assert_equal true, issue.valid_parent_project?(issue_in_child_project)
1077 1079 assert_equal true, issue.valid_parent_project?(issue_in_grandchild_project)
1078 1080 assert_equal false, issue.valid_parent_project?(issue_in_different_tree)
1079 1081
1080 1082 assert_equal true, issue_in_child_project.valid_parent_project?(issue_in_same_project)
1081 1083 assert_equal true, issue_in_child_project.valid_parent_project?(issue_in_other_child_project)
1082 1084 end
1083 1085
1084 1086 with_settings :cross_project_subtasks => 'descendants' do
1085 1087 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1086 1088 assert_equal false, issue.valid_parent_project?(issue_in_child_project)
1087 1089 assert_equal false, issue.valid_parent_project?(issue_in_grandchild_project)
1088 1090 assert_equal false, issue.valid_parent_project?(issue_in_different_tree)
1089 1091
1090 1092 assert_equal true, issue_in_child_project.valid_parent_project?(issue)
1091 1093 assert_equal false, issue_in_child_project.valid_parent_project?(issue_in_other_child_project)
1092 1094 end
1093 1095 end
1094 1096
1095 1097 def test_recipients_should_include_previous_assignee
1096 1098 user = User.find(3)
1097 1099 user.members.update_all ["mail_notification = ?", false]
1098 1100 user.update_attribute :mail_notification, 'only_assigned'
1099 1101
1100 1102 issue = Issue.find(2)
1101 1103 issue.assigned_to = nil
1102 1104 assert_include user.mail, issue.recipients
1103 1105 issue.save!
1104 1106 assert !issue.recipients.include?(user.mail)
1105 1107 end
1106 1108
1107 1109 def test_recipients_should_not_include_users_that_cannot_view_the_issue
1108 1110 issue = Issue.find(12)
1109 1111 assert issue.recipients.include?(issue.author.mail)
1110 1112 # copy the issue to a private project
1111 1113 copy = issue.copy(:project_id => 5, :tracker_id => 2)
1112 1114 # author is not a member of project anymore
1113 1115 assert !copy.recipients.include?(copy.author.mail)
1114 1116 end
1115 1117
1116 1118 def test_recipients_should_include_the_assigned_group_members
1117 1119 group_member = User.generate!
1118 1120 group = Group.generate!
1119 1121 group.users << group_member
1120 1122
1121 1123 issue = Issue.find(12)
1122 1124 issue.assigned_to = group
1123 1125 assert issue.recipients.include?(group_member.mail)
1124 1126 end
1125 1127
1126 1128 def test_watcher_recipients_should_not_include_users_that_cannot_view_the_issue
1127 1129 user = User.find(3)
1128 1130 issue = Issue.find(9)
1129 1131 Watcher.create!(:user => user, :watchable => issue)
1130 1132 assert issue.watched_by?(user)
1131 1133 assert !issue.watcher_recipients.include?(user.mail)
1132 1134 end
1133 1135
1134 1136 def test_issue_destroy
1135 1137 Issue.find(1).destroy
1136 1138 assert_nil Issue.find_by_id(1)
1137 1139 assert_nil TimeEntry.find_by_issue_id(1)
1138 1140 end
1139 1141
1140 1142 def test_destroying_a_deleted_issue_should_not_raise_an_error
1141 1143 issue = Issue.find(1)
1142 1144 Issue.find(1).destroy
1143 1145
1144 1146 assert_nothing_raised do
1145 1147 assert_no_difference 'Issue.count' do
1146 1148 issue.destroy
1147 1149 end
1148 1150 assert issue.destroyed?
1149 1151 end
1150 1152 end
1151 1153
1152 1154 def test_destroying_a_stale_issue_should_not_raise_an_error
1153 1155 issue = Issue.find(1)
1154 1156 Issue.find(1).update_attribute :subject, "Updated"
1155 1157
1156 1158 assert_nothing_raised do
1157 1159 assert_difference 'Issue.count', -1 do
1158 1160 issue.destroy
1159 1161 end
1160 1162 assert issue.destroyed?
1161 1163 end
1162 1164 end
1163 1165
1164 1166 def test_blocked
1165 1167 blocked_issue = Issue.find(9)
1166 1168 blocking_issue = Issue.find(10)
1167 1169
1168 1170 assert blocked_issue.blocked?
1169 1171 assert !blocking_issue.blocked?
1170 1172 end
1171 1173
1172 1174 def test_blocked_issues_dont_allow_closed_statuses
1173 1175 blocked_issue = Issue.find(9)
1174 1176
1175 1177 allowed_statuses = blocked_issue.new_statuses_allowed_to(users(:users_002))
1176 1178 assert !allowed_statuses.empty?
1177 1179 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
1178 1180 assert closed_statuses.empty?
1179 1181 end
1180 1182
1181 1183 def test_unblocked_issues_allow_closed_statuses
1182 1184 blocking_issue = Issue.find(10)
1183 1185
1184 1186 allowed_statuses = blocking_issue.new_statuses_allowed_to(users(:users_002))
1185 1187 assert !allowed_statuses.empty?
1186 1188 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
1187 1189 assert !closed_statuses.empty?
1188 1190 end
1189 1191
1190 1192 def test_rescheduling_an_issue_should_reschedule_following_issue
1191 1193 issue1 = Issue.create!(:project_id => 1, :tracker_id => 1,
1192 1194 :author_id => 1, :status_id => 1,
1193 1195 :subject => '-',
1194 1196 :start_date => Date.today, :due_date => Date.today + 2)
1195 1197 issue2 = Issue.create!(:project_id => 1, :tracker_id => 1,
1196 1198 :author_id => 1, :status_id => 1,
1197 1199 :subject => '-',
1198 1200 :start_date => Date.today, :due_date => Date.today + 2)
1199 1201 IssueRelation.create!(:issue_from => issue1, :issue_to => issue2,
1200 1202 :relation_type => IssueRelation::TYPE_PRECEDES)
1201 1203 assert_equal issue1.due_date + 1, issue2.reload.start_date
1202 1204
1203 1205 issue1.due_date = Date.today + 5
1204 1206 issue1.save!
1205 1207 assert_equal issue1.due_date + 1, issue2.reload.start_date
1206 1208 end
1207 1209
1208 1210 def test_rescheduling_a_stale_issue_should_not_raise_an_error
1209 1211 stale = Issue.find(1)
1210 1212 issue = Issue.find(1)
1211 1213 issue.subject = "Updated"
1212 1214 issue.save!
1213 1215
1214 1216 date = 10.days.from_now.to_date
1215 1217 assert_nothing_raised do
1216 1218 stale.reschedule_after(date)
1217 1219 end
1218 1220 assert_equal date, stale.reload.start_date
1219 1221 end
1220 1222
1221 1223 def test_overdue
1222 1224 assert Issue.new(:due_date => 1.day.ago.to_date).overdue?
1223 1225 assert !Issue.new(:due_date => Date.today).overdue?
1224 1226 assert !Issue.new(:due_date => 1.day.from_now.to_date).overdue?
1225 1227 assert !Issue.new(:due_date => nil).overdue?
1226 1228 assert !Issue.new(:due_date => 1.day.ago.to_date,
1227 1229 :status => IssueStatus.find(:first,
1228 1230 :conditions => {:is_closed => true})
1229 1231 ).overdue?
1230 1232 end
1231 1233
1232 1234 context "#behind_schedule?" do
1233 1235 should "be false if the issue has no start_date" do
1234 1236 assert !Issue.new(:start_date => nil,
1235 1237 :due_date => 1.day.from_now.to_date,
1236 1238 :done_ratio => 0).behind_schedule?
1237 1239 end
1238 1240
1239 1241 should "be false if the issue has no end_date" do
1240 1242 assert !Issue.new(:start_date => 1.day.from_now.to_date,
1241 1243 :due_date => nil,
1242 1244 :done_ratio => 0).behind_schedule?
1243 1245 end
1244 1246
1245 1247 should "be false if the issue has more done than it's calendar time" do
1246 1248 assert !Issue.new(:start_date => 50.days.ago.to_date,
1247 1249 :due_date => 50.days.from_now.to_date,
1248 1250 :done_ratio => 90).behind_schedule?
1249 1251 end
1250 1252
1251 1253 should "be true if the issue hasn't been started at all" do
1252 1254 assert Issue.new(:start_date => 1.day.ago.to_date,
1253 1255 :due_date => 1.day.from_now.to_date,
1254 1256 :done_ratio => 0).behind_schedule?
1255 1257 end
1256 1258
1257 1259 should "be true if the issue has used more calendar time than it's done ratio" do
1258 1260 assert Issue.new(:start_date => 100.days.ago.to_date,
1259 1261 :due_date => Date.today,
1260 1262 :done_ratio => 90).behind_schedule?
1261 1263 end
1262 1264 end
1263 1265
1264 1266 context "#assignable_users" do
1265 1267 should "be Users" do
1266 1268 assert_kind_of User, Issue.find(1).assignable_users.first
1267 1269 end
1268 1270
1269 1271 should "include the issue author" do
1270 1272 non_project_member = User.generate!
1271 1273 issue = Issue.generate!(:author => non_project_member)
1272 1274
1273 1275 assert issue.assignable_users.include?(non_project_member)
1274 1276 end
1275 1277
1276 1278 should "include the current assignee" do
1277 1279 user = User.generate!
1278 1280 issue = Issue.generate!(:assigned_to => user)
1279 1281 user.lock!
1280 1282
1281 1283 assert Issue.find(issue.id).assignable_users.include?(user)
1282 1284 end
1283 1285
1284 1286 should "not show the issue author twice" do
1285 1287 assignable_user_ids = Issue.find(1).assignable_users.collect(&:id)
1286 1288 assert_equal 2, assignable_user_ids.length
1287 1289
1288 1290 assignable_user_ids.each do |user_id|
1289 1291 assert_equal 1, assignable_user_ids.select {|i| i == user_id}.length,
1290 1292 "User #{user_id} appears more or less than once"
1291 1293 end
1292 1294 end
1293 1295
1294 1296 context "with issue_group_assignment" do
1295 1297 should "include groups" do
1296 1298 issue = Issue.new(:project => Project.find(2))
1297 1299
1298 1300 with_settings :issue_group_assignment => '1' do
1299 1301 assert_equal %w(Group User), issue.assignable_users.map {|a| a.class.name}.uniq.sort
1300 1302 assert issue.assignable_users.include?(Group.find(11))
1301 1303 end
1302 1304 end
1303 1305 end
1304 1306
1305 1307 context "without issue_group_assignment" do
1306 1308 should "not include groups" do
1307 1309 issue = Issue.new(:project => Project.find(2))
1308 1310
1309 1311 with_settings :issue_group_assignment => '0' do
1310 1312 assert_equal %w(User), issue.assignable_users.map {|a| a.class.name}.uniq.sort
1311 1313 assert !issue.assignable_users.include?(Group.find(11))
1312 1314 end
1313 1315 end
1314 1316 end
1315 1317 end
1316 1318
1317 1319 def test_create_should_send_email_notification
1318 1320 ActionMailer::Base.deliveries.clear
1319 1321 issue = Issue.new(:project_id => 1, :tracker_id => 1,
1320 1322 :author_id => 3, :status_id => 1,
1321 1323 :priority => IssuePriority.all.first,
1322 1324 :subject => 'test_create', :estimated_hours => '1:30')
1323 1325
1324 1326 assert issue.save
1325 1327 assert_equal 1, ActionMailer::Base.deliveries.size
1326 1328 end
1327 1329
1328 1330 def test_stale_issue_should_not_send_email_notification
1329 1331 ActionMailer::Base.deliveries.clear
1330 1332 issue = Issue.find(1)
1331 1333 stale = Issue.find(1)
1332 1334
1333 1335 issue.init_journal(User.find(1))
1334 1336 issue.subject = 'Subjet update'
1335 1337 assert issue.save
1336 1338 assert_equal 1, ActionMailer::Base.deliveries.size
1337 1339 ActionMailer::Base.deliveries.clear
1338 1340
1339 1341 stale.init_journal(User.find(1))
1340 1342 stale.subject = 'Another subjet update'
1341 1343 assert_raise ActiveRecord::StaleObjectError do
1342 1344 stale.save
1343 1345 end
1344 1346 assert ActionMailer::Base.deliveries.empty?
1345 1347 end
1346 1348
1347 1349 def test_journalized_description
1348 1350 IssueCustomField.delete_all
1349 1351
1350 1352 i = Issue.first
1351 1353 old_description = i.description
1352 1354 new_description = "This is the new description"
1353 1355
1354 1356 i.init_journal(User.find(2))
1355 1357 i.description = new_description
1356 1358 assert_difference 'Journal.count', 1 do
1357 1359 assert_difference 'JournalDetail.count', 1 do
1358 1360 i.save!
1359 1361 end
1360 1362 end
1361 1363
1362 1364 detail = JournalDetail.first(:order => 'id DESC')
1363 1365 assert_equal i, detail.journal.journalized
1364 1366 assert_equal 'attr', detail.property
1365 1367 assert_equal 'description', detail.prop_key
1366 1368 assert_equal old_description, detail.old_value
1367 1369 assert_equal new_description, detail.value
1368 1370 end
1369 1371
1370 1372 def test_blank_descriptions_should_not_be_journalized
1371 1373 IssueCustomField.delete_all
1372 1374 Issue.update_all("description = NULL", "id=1")
1373 1375
1374 1376 i = Issue.find(1)
1375 1377 i.init_journal(User.find(2))
1376 1378 i.subject = "blank description"
1377 1379 i.description = "\r\n"
1378 1380
1379 1381 assert_difference 'Journal.count', 1 do
1380 1382 assert_difference 'JournalDetail.count', 1 do
1381 1383 i.save!
1382 1384 end
1383 1385 end
1384 1386 end
1385 1387
1386 1388 def test_journalized_multi_custom_field
1387 1389 field = IssueCustomField.create!(:name => 'filter', :field_format => 'list',
1388 1390 :is_filter => true, :is_for_all => true,
1389 1391 :tracker_ids => [1],
1390 1392 :possible_values => ['value1', 'value2', 'value3'],
1391 1393 :multiple => true)
1392 1394
1393 1395 issue = Issue.create!(:project_id => 1, :tracker_id => 1,
1394 1396 :subject => 'Test', :author_id => 1)
1395 1397
1396 1398 assert_difference 'Journal.count' do
1397 1399 assert_difference 'JournalDetail.count' do
1398 1400 issue.init_journal(User.first)
1399 1401 issue.custom_field_values = {field.id => ['value1']}
1400 1402 issue.save!
1401 1403 end
1402 1404 assert_difference 'JournalDetail.count' do
1403 1405 issue.init_journal(User.first)
1404 1406 issue.custom_field_values = {field.id => ['value1', 'value2']}
1405 1407 issue.save!
1406 1408 end
1407 1409 assert_difference 'JournalDetail.count', 2 do
1408 1410 issue.init_journal(User.first)
1409 1411 issue.custom_field_values = {field.id => ['value3', 'value2']}
1410 1412 issue.save!
1411 1413 end
1412 1414 assert_difference 'JournalDetail.count', 2 do
1413 1415 issue.init_journal(User.first)
1414 1416 issue.custom_field_values = {field.id => nil}
1415 1417 issue.save!
1416 1418 end
1417 1419 end
1418 1420 end
1419 1421
1420 1422 def test_description_eol_should_be_normalized
1421 1423 i = Issue.new(:description => "CR \r LF \n CRLF \r\n")
1422 1424 assert_equal "CR \r\n LF \r\n CRLF \r\n", i.description
1423 1425 end
1424 1426
1425 1427 def test_saving_twice_should_not_duplicate_journal_details
1426 1428 i = Issue.find(:first)
1427 1429 i.init_journal(User.find(2), 'Some notes')
1428 1430 # initial changes
1429 1431 i.subject = 'New subject'
1430 1432 i.done_ratio = i.done_ratio + 10
1431 1433 assert_difference 'Journal.count' do
1432 1434 assert i.save
1433 1435 end
1434 1436 # 1 more change
1435 1437 i.priority = IssuePriority.find(:first, :conditions => ["id <> ?", i.priority_id])
1436 1438 assert_no_difference 'Journal.count' do
1437 1439 assert_difference 'JournalDetail.count', 1 do
1438 1440 i.save
1439 1441 end
1440 1442 end
1441 1443 # no more change
1442 1444 assert_no_difference 'Journal.count' do
1443 1445 assert_no_difference 'JournalDetail.count' do
1444 1446 i.save
1445 1447 end
1446 1448 end
1447 1449 end
1448 1450
1449 1451 def test_all_dependent_issues
1450 1452 IssueRelation.delete_all
1451 1453 assert IssueRelation.create!(:issue_from => Issue.find(1),
1452 1454 :issue_to => Issue.find(2),
1453 1455 :relation_type => IssueRelation::TYPE_PRECEDES)
1454 1456 assert IssueRelation.create!(:issue_from => Issue.find(2),
1455 1457 :issue_to => Issue.find(3),
1456 1458 :relation_type => IssueRelation::TYPE_PRECEDES)
1457 1459 assert IssueRelation.create!(:issue_from => Issue.find(3),
1458 1460 :issue_to => Issue.find(8),
1459 1461 :relation_type => IssueRelation::TYPE_PRECEDES)
1460 1462
1461 1463 assert_equal [2, 3, 8], Issue.find(1).all_dependent_issues.collect(&:id).sort
1462 1464 end
1463 1465
1464 1466 def test_all_dependent_issues_with_persistent_circular_dependency
1465 1467 IssueRelation.delete_all
1466 1468 assert IssueRelation.create!(:issue_from => Issue.find(1),
1467 1469 :issue_to => Issue.find(2),
1468 1470 :relation_type => IssueRelation::TYPE_PRECEDES)
1469 1471 assert IssueRelation.create!(:issue_from => Issue.find(2),
1470 1472 :issue_to => Issue.find(3),
1471 1473 :relation_type => IssueRelation::TYPE_PRECEDES)
1472 1474
1473 1475 r = IssueRelation.create!(:issue_from => Issue.find(3),
1474 1476 :issue_to => Issue.find(7),
1475 1477 :relation_type => IssueRelation::TYPE_PRECEDES)
1476 1478 IssueRelation.update_all("issue_to_id = 1", ["id = ?", r.id])
1477 1479
1478 1480 assert_equal [2, 3], Issue.find(1).all_dependent_issues.collect(&:id).sort
1479 1481 end
1480 1482
1481 1483 def test_all_dependent_issues_with_persistent_multiple_circular_dependencies
1482 1484 IssueRelation.delete_all
1483 1485 assert IssueRelation.create!(:issue_from => Issue.find(1),
1484 1486 :issue_to => Issue.find(2),
1485 1487 :relation_type => IssueRelation::TYPE_RELATES)
1486 1488 assert IssueRelation.create!(:issue_from => Issue.find(2),
1487 1489 :issue_to => Issue.find(3),
1488 1490 :relation_type => IssueRelation::TYPE_RELATES)
1489 1491 assert IssueRelation.create!(:issue_from => Issue.find(3),
1490 1492 :issue_to => Issue.find(8),
1491 1493 :relation_type => IssueRelation::TYPE_RELATES)
1492 1494
1493 1495 r = IssueRelation.create!(:issue_from => Issue.find(8),
1494 1496 :issue_to => Issue.find(7),
1495 1497 :relation_type => IssueRelation::TYPE_RELATES)
1496 1498 IssueRelation.update_all("issue_to_id = 2", ["id = ?", r.id])
1497 1499
1498 1500 r = IssueRelation.create!(:issue_from => Issue.find(3),
1499 1501 :issue_to => Issue.find(7),
1500 1502 :relation_type => IssueRelation::TYPE_RELATES)
1501 1503 IssueRelation.update_all("issue_to_id = 1", ["id = ?", r.id])
1502 1504
1503 1505 assert_equal [2, 3, 8], Issue.find(1).all_dependent_issues.collect(&:id).sort
1504 1506 end
1505 1507
1506 1508 context "#done_ratio" do
1507 1509 setup do
1508 1510 @issue = Issue.find(1)
1509 1511 @issue_status = IssueStatus.find(1)
1510 1512 @issue_status.update_attribute(:default_done_ratio, 50)
1511 1513 @issue2 = Issue.find(2)
1512 1514 @issue_status2 = IssueStatus.find(2)
1513 1515 @issue_status2.update_attribute(:default_done_ratio, 0)
1514 1516 end
1515 1517
1516 1518 teardown do
1517 1519 Setting.issue_done_ratio = 'issue_field'
1518 1520 end
1519 1521
1520 1522 context "with Setting.issue_done_ratio using the issue_field" do
1521 1523 setup do
1522 1524 Setting.issue_done_ratio = 'issue_field'
1523 1525 end
1524 1526
1525 1527 should "read the issue's field" do
1526 1528 assert_equal 0, @issue.done_ratio
1527 1529 assert_equal 30, @issue2.done_ratio
1528 1530 end
1529 1531 end
1530 1532
1531 1533 context "with Setting.issue_done_ratio using the issue_status" do
1532 1534 setup do
1533 1535 Setting.issue_done_ratio = 'issue_status'
1534 1536 end
1535 1537
1536 1538 should "read the Issue Status's default done ratio" do
1537 1539 assert_equal 50, @issue.done_ratio
1538 1540 assert_equal 0, @issue2.done_ratio
1539 1541 end
1540 1542 end
1541 1543 end
1542 1544
1543 1545 context "#update_done_ratio_from_issue_status" do
1544 1546 setup do
1545 1547 @issue = Issue.find(1)
1546 1548 @issue_status = IssueStatus.find(1)
1547 1549 @issue_status.update_attribute(:default_done_ratio, 50)
1548 1550 @issue2 = Issue.find(2)
1549 1551 @issue_status2 = IssueStatus.find(2)
1550 1552 @issue_status2.update_attribute(:default_done_ratio, 0)
1551 1553 end
1552 1554
1553 1555 context "with Setting.issue_done_ratio using the issue_field" do
1554 1556 setup do
1555 1557 Setting.issue_done_ratio = 'issue_field'
1556 1558 end
1557 1559
1558 1560 should "not change the issue" do
1559 1561 @issue.update_done_ratio_from_issue_status
1560 1562 @issue2.update_done_ratio_from_issue_status
1561 1563
1562 1564 assert_equal 0, @issue.read_attribute(:done_ratio)
1563 1565 assert_equal 30, @issue2.read_attribute(:done_ratio)
1564 1566 end
1565 1567 end
1566 1568
1567 1569 context "with Setting.issue_done_ratio using the issue_status" do
1568 1570 setup do
1569 1571 Setting.issue_done_ratio = 'issue_status'
1570 1572 end
1571 1573
1572 1574 should "change the issue's done ratio" do
1573 1575 @issue.update_done_ratio_from_issue_status
1574 1576 @issue2.update_done_ratio_from_issue_status
1575 1577
1576 1578 assert_equal 50, @issue.read_attribute(:done_ratio)
1577 1579 assert_equal 0, @issue2.read_attribute(:done_ratio)
1578 1580 end
1579 1581 end
1580 1582 end
1581 1583
1582 1584 test "#by_tracker" do
1583 1585 User.current = User.anonymous
1584 1586 groups = Issue.by_tracker(Project.find(1))
1585 1587 assert_equal 3, groups.size
1586 1588 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1587 1589 end
1588 1590
1589 1591 test "#by_version" do
1590 1592 User.current = User.anonymous
1591 1593 groups = Issue.by_version(Project.find(1))
1592 1594 assert_equal 3, groups.size
1593 1595 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1594 1596 end
1595 1597
1596 1598 test "#by_priority" do
1597 1599 User.current = User.anonymous
1598 1600 groups = Issue.by_priority(Project.find(1))
1599 1601 assert_equal 4, groups.size
1600 1602 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1601 1603 end
1602 1604
1603 1605 test "#by_category" do
1604 1606 User.current = User.anonymous
1605 1607 groups = Issue.by_category(Project.find(1))
1606 1608 assert_equal 2, groups.size
1607 1609 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1608 1610 end
1609 1611
1610 1612 test "#by_assigned_to" do
1611 1613 User.current = User.anonymous
1612 1614 groups = Issue.by_assigned_to(Project.find(1))
1613 1615 assert_equal 2, groups.size
1614 1616 assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1615 1617 end
1616 1618
1617 1619 test "#by_author" do
1618 1620 User.current = User.anonymous
1619 1621 groups = Issue.by_author(Project.find(1))
1620 1622 assert_equal 4, groups.size
1621 1623 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1622 1624 end
1623 1625
1624 1626 test "#by_subproject" do
1625 1627 User.current = User.anonymous
1626 1628 groups = Issue.by_subproject(Project.find(1))
1627 1629 # Private descendant not visible
1628 1630 assert_equal 1, groups.size
1629 1631 assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1630 1632 end
1631 1633
1632 1634 def test_recently_updated_scope
1633 1635 #should return the last updated issue
1634 1636 assert_equal Issue.reorder("updated_on DESC").first, Issue.recently_updated.limit(1).first
1635 1637 end
1636 1638
1637 1639 def test_on_active_projects_scope
1638 1640 assert Project.find(2).archive
1639 1641
1640 1642 before = Issue.on_active_project.length
1641 1643 # test inclusion to results
1642 1644 issue = Issue.generate!(:tracker => Project.find(2).trackers.first)
1643 1645 assert_equal before + 1, Issue.on_active_project.length
1644 1646
1645 1647 # Move to an archived project
1646 1648 issue.project = Project.find(2)
1647 1649 assert issue.save
1648 1650 assert_equal before, Issue.on_active_project.length
1649 1651 end
1650 1652
1651 1653 context "Issue#recipients" do
1652 1654 setup do
1653 1655 @project = Project.find(1)
1654 1656 @author = User.generate!
1655 1657 @assignee = User.generate!
1656 1658 @issue = Issue.generate!(:project => @project, :assigned_to => @assignee, :author => @author)
1657 1659 end
1658 1660
1659 1661 should "include project recipients" do
1660 1662 assert @project.recipients.present?
1661 1663 @project.recipients.each do |project_recipient|
1662 1664 assert @issue.recipients.include?(project_recipient)
1663 1665 end
1664 1666 end
1665 1667
1666 1668 should "include the author if the author is active" do
1667 1669 assert @issue.author, "No author set for Issue"
1668 1670 assert @issue.recipients.include?(@issue.author.mail)
1669 1671 end
1670 1672
1671 1673 should "include the assigned to user if the assigned to user is active" do
1672 1674 assert @issue.assigned_to, "No assigned_to set for Issue"
1673 1675 assert @issue.recipients.include?(@issue.assigned_to.mail)
1674 1676 end
1675 1677
1676 1678 should "not include users who opt out of all email" do
1677 1679 @author.update_attribute(:mail_notification, :none)
1678 1680
1679 1681 assert !@issue.recipients.include?(@issue.author.mail)
1680 1682 end
1681 1683
1682 1684 should "not include the issue author if they are only notified of assigned issues" do
1683 1685 @author.update_attribute(:mail_notification, :only_assigned)
1684 1686
1685 1687 assert !@issue.recipients.include?(@issue.author.mail)
1686 1688 end
1687 1689
1688 1690 should "not include the assigned user if they are only notified of owned issues" do
1689 1691 @assignee.update_attribute(:mail_notification, :only_owner)
1690 1692
1691 1693 assert !@issue.recipients.include?(@issue.assigned_to.mail)
1692 1694 end
1693 1695 end
1694 1696
1695 1697 def test_last_journal_id_with_journals_should_return_the_journal_id
1696 1698 assert_equal 2, Issue.find(1).last_journal_id
1697 1699 end
1698 1700
1699 1701 def test_last_journal_id_without_journals_should_return_nil
1700 1702 assert_nil Issue.find(3).last_journal_id
1701 1703 end
1702 1704
1703 1705 def test_journals_after_should_return_journals_with_greater_id
1704 1706 assert_equal [Journal.find(2)], Issue.find(1).journals_after('1')
1705 1707 assert_equal [], Issue.find(1).journals_after('2')
1706 1708 end
1707 1709
1708 1710 def test_journals_after_with_blank_arg_should_return_all_journals
1709 1711 assert_equal [Journal.find(1), Journal.find(2)], Issue.find(1).journals_after('')
1710 1712 end
1711 1713 end
General Comments 0
You need to be logged in to leave comments. Login now