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