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