##// END OF EJS Templates
code layout clean up test_read_only_attribute_names_for_multiple_roles_should_intersect_rules of unit issue test...
Toshi MARUYAMA -
r10438:eedad03141f8
parent child
Show More
@@ -1,1782 +1,1788
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 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'due_date', :rule => 'readonly')
710 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 1, :field_name => 'start_date', :rule => 'readonly')
709 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
710 :role_id => 1, :field_name => 'due_date',
711 :rule => 'readonly')
712 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
713 :role_id => 1, :field_name => 'start_date',
714 :rule => 'readonly')
711 715 user = User.find(2)
712 716 member = Member.find(1)
713 717 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1)
714 718
715 719 assert_equal %w(due_date start_date), issue.read_only_attribute_names(user).sort
716 720
717 721 member.role_ids = [1, 2]
718 722 member.save!
719 723 assert_equal [], issue.read_only_attribute_names(user.reload)
720 724
721 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1, :role_id => 2, :field_name => 'due_date', :rule => 'readonly')
725 WorkflowPermission.create!(:old_status_id => 1, :tracker_id => 1,
726 :role_id => 2, :field_name => 'due_date',
727 :rule => 'readonly')
722 728 assert_equal %w(due_date), issue.read_only_attribute_names(user)
723 729 end
724 730
725 731 def test_copy
726 732 issue = Issue.new.copy_from(1)
727 733 assert issue.copy?
728 734 assert issue.save
729 735 issue.reload
730 736 orig = Issue.find(1)
731 737 assert_equal orig.subject, issue.subject
732 738 assert_equal orig.tracker, issue.tracker
733 739 assert_equal "125", issue.custom_value_for(2).value
734 740 end
735 741
736 742 def test_copy_should_copy_status
737 743 orig = Issue.find(8)
738 744 assert orig.status != IssueStatus.default
739 745
740 746 issue = Issue.new.copy_from(orig)
741 747 assert issue.save
742 748 issue.reload
743 749 assert_equal orig.status, issue.status
744 750 end
745 751
746 752 def test_copy_should_add_relation_with_copied_issue
747 753 copied = Issue.find(1)
748 754 issue = Issue.new.copy_from(copied)
749 755 assert issue.save
750 756 issue.reload
751 757
752 758 assert_equal 1, issue.relations.size
753 759 relation = issue.relations.first
754 760 assert_equal 'copied_to', relation.relation_type
755 761 assert_equal copied, relation.issue_from
756 762 assert_equal issue, relation.issue_to
757 763 end
758 764
759 765 def test_copy_should_copy_subtasks
760 766 issue = Issue.generate_with_descendants!
761 767
762 768 copy = issue.reload.copy
763 769 copy.author = User.find(7)
764 770 assert_difference 'Issue.count', 1+issue.descendants.count do
765 771 assert copy.save
766 772 end
767 773 copy.reload
768 774 assert_equal %w(Child1 Child2), copy.children.map(&:subject).sort
769 775 child_copy = copy.children.detect {|c| c.subject == 'Child1'}
770 776 assert_equal %w(Child11), child_copy.children.map(&:subject).sort
771 777 assert_equal copy.author, child_copy.author
772 778 end
773 779
774 780 def test_copy_should_copy_subtasks_to_target_project
775 781 issue = Issue.generate_with_descendants!
776 782
777 783 copy = issue.copy(:project_id => 3)
778 784 assert_difference 'Issue.count', 1+issue.descendants.count do
779 785 assert copy.save
780 786 end
781 787 assert_equal [3], copy.reload.descendants.map(&:project_id).uniq
782 788 end
783 789
784 790 def test_copy_should_not_copy_subtasks_twice_when_saving_twice
785 791 issue = Issue.generate_with_descendants!
786 792
787 793 copy = issue.reload.copy
788 794 assert_difference 'Issue.count', 1+issue.descendants.count do
789 795 assert copy.save
790 796 assert copy.save
791 797 end
792 798 end
793 799
794 800 def test_should_not_call_after_project_change_on_creation
795 801 issue = Issue.new(:project_id => 1, :tracker_id => 1, :status_id => 1, :subject => 'Test', :author_id => 1)
796 802 issue.expects(:after_project_change).never
797 803 issue.save!
798 804 end
799 805
800 806 def test_should_not_call_after_project_change_on_update
801 807 issue = Issue.find(1)
802 808 issue.project = Project.find(1)
803 809 issue.subject = 'No project change'
804 810 issue.expects(:after_project_change).never
805 811 issue.save!
806 812 end
807 813
808 814 def test_should_call_after_project_change_on_project_change
809 815 issue = Issue.find(1)
810 816 issue.project = Project.find(2)
811 817 issue.expects(:after_project_change).once
812 818 issue.save!
813 819 end
814 820
815 821 def test_adding_journal_should_update_timestamp
816 822 issue = Issue.find(1)
817 823 updated_on_was = issue.updated_on
818 824
819 825 issue.init_journal(User.first, "Adding notes")
820 826 assert_difference 'Journal.count' do
821 827 assert issue.save
822 828 end
823 829 issue.reload
824 830
825 831 assert_not_equal updated_on_was, issue.updated_on
826 832 end
827 833
828 834 def test_should_close_duplicates
829 835 # Create 3 issues
830 836 issue1 = Issue.generate!
831 837 issue2 = Issue.generate!
832 838 issue3 = Issue.generate!
833 839
834 840 # 2 is a dupe of 1
835 841 IssueRelation.create!(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
836 842 # And 3 is a dupe of 2
837 843 IssueRelation.create!(:issue_from => issue3, :issue_to => issue2, :relation_type => IssueRelation::TYPE_DUPLICATES)
838 844 # And 3 is a dupe of 1 (circular duplicates)
839 845 IssueRelation.create!(:issue_from => issue3, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
840 846
841 847 assert issue1.reload.duplicates.include?(issue2)
842 848
843 849 # Closing issue 1
844 850 issue1.init_journal(User.find(:first), "Closing issue1")
845 851 issue1.status = IssueStatus.find :first, :conditions => {:is_closed => true}
846 852 assert issue1.save
847 853 # 2 and 3 should be also closed
848 854 assert issue2.reload.closed?
849 855 assert issue3.reload.closed?
850 856 end
851 857
852 858 def test_should_not_close_duplicated_issue
853 859 issue1 = Issue.generate!
854 860 issue2 = Issue.generate!
855 861
856 862 # 2 is a dupe of 1
857 863 IssueRelation.create(:issue_from => issue2, :issue_to => issue1, :relation_type => IssueRelation::TYPE_DUPLICATES)
858 864 # 2 is a dup of 1 but 1 is not a duplicate of 2
859 865 assert !issue2.reload.duplicates.include?(issue1)
860 866
861 867 # Closing issue 2
862 868 issue2.init_journal(User.find(:first), "Closing issue2")
863 869 issue2.status = IssueStatus.find :first, :conditions => {:is_closed => true}
864 870 assert issue2.save
865 871 # 1 should not be also closed
866 872 assert !issue1.reload.closed?
867 873 end
868 874
869 875 def test_assignable_versions
870 876 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 1, :subject => 'New issue')
871 877 assert_equal ['open'], issue.assignable_versions.collect(&:status).uniq
872 878 end
873 879
874 880 def test_should_not_be_able_to_assign_a_new_issue_to_a_closed_version
875 881 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 1, :subject => 'New issue')
876 882 assert !issue.save
877 883 assert_not_nil issue.errors[:fixed_version_id]
878 884 end
879 885
880 886 def test_should_not_be_able_to_assign_a_new_issue_to_a_locked_version
881 887 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :fixed_version_id => 2, :subject => 'New issue')
882 888 assert !issue.save
883 889 assert_not_nil issue.errors[:fixed_version_id]
884 890 end
885 891
886 892 def test_should_be_able_to_assign_a_new_issue_to_an_open_version
887 893 issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1,
888 894 :status_id => 1, :fixed_version_id => 3,
889 895 :subject => 'New issue')
890 896 assert issue.save
891 897 end
892 898
893 899 def test_should_be_able_to_update_an_issue_assigned_to_a_closed_version
894 900 issue = Issue.find(11)
895 901 assert_equal 'closed', issue.fixed_version.status
896 902 issue.subject = 'Subject changed'
897 903 assert issue.save
898 904 end
899 905
900 906 def test_should_not_be_able_to_reopen_an_issue_assigned_to_a_closed_version
901 907 issue = Issue.find(11)
902 908 issue.status_id = 1
903 909 assert !issue.save
904 910 assert_not_nil issue.errors[:base]
905 911 end
906 912
907 913 def test_should_be_able_to_reopen_and_reassign_an_issue_assigned_to_a_closed_version
908 914 issue = Issue.find(11)
909 915 issue.status_id = 1
910 916 issue.fixed_version_id = 3
911 917 assert issue.save
912 918 end
913 919
914 920 def test_should_be_able_to_reopen_an_issue_assigned_to_a_locked_version
915 921 issue = Issue.find(12)
916 922 assert_equal 'locked', issue.fixed_version.status
917 923 issue.status_id = 1
918 924 assert issue.save
919 925 end
920 926
921 927 def test_should_not_be_able_to_keep_unshared_version_when_changing_project
922 928 issue = Issue.find(2)
923 929 assert_equal 2, issue.fixed_version_id
924 930 issue.project_id = 3
925 931 assert_nil issue.fixed_version_id
926 932 issue.fixed_version_id = 2
927 933 assert !issue.save
928 934 assert_include 'Target version is not included in the list', issue.errors.full_messages
929 935 end
930 936
931 937 def test_should_keep_shared_version_when_changing_project
932 938 Version.find(2).update_attribute :sharing, 'tree'
933 939
934 940 issue = Issue.find(2)
935 941 assert_equal 2, issue.fixed_version_id
936 942 issue.project_id = 3
937 943 assert_equal 2, issue.fixed_version_id
938 944 assert issue.save
939 945 end
940 946
941 947 def test_allowed_target_projects_on_move_should_include_projects_with_issue_tracking_enabled
942 948 assert_include Project.find(2), Issue.allowed_target_projects_on_move(User.find(2))
943 949 end
944 950
945 951 def test_allowed_target_projects_on_move_should_not_include_projects_with_issue_tracking_disabled
946 952 Project.find(2).disable_module! :issue_tracking
947 953 assert_not_include Project.find(2), Issue.allowed_target_projects_on_move(User.find(2))
948 954 end
949 955
950 956 def test_move_to_another_project_with_same_category
951 957 issue = Issue.find(1)
952 958 issue.project = Project.find(2)
953 959 assert issue.save
954 960 issue.reload
955 961 assert_equal 2, issue.project_id
956 962 # Category changes
957 963 assert_equal 4, issue.category_id
958 964 # Make sure time entries were move to the target project
959 965 assert_equal 2, issue.time_entries.first.project_id
960 966 end
961 967
962 968 def test_move_to_another_project_without_same_category
963 969 issue = Issue.find(2)
964 970 issue.project = Project.find(2)
965 971 assert issue.save
966 972 issue.reload
967 973 assert_equal 2, issue.project_id
968 974 # Category cleared
969 975 assert_nil issue.category_id
970 976 end
971 977
972 978 def test_move_to_another_project_should_clear_fixed_version_when_not_shared
973 979 issue = Issue.find(1)
974 980 issue.update_attribute(:fixed_version_id, 1)
975 981 issue.project = Project.find(2)
976 982 assert issue.save
977 983 issue.reload
978 984 assert_equal 2, issue.project_id
979 985 # Cleared fixed_version
980 986 assert_equal nil, issue.fixed_version
981 987 end
982 988
983 989 def test_move_to_another_project_should_keep_fixed_version_when_shared_with_the_target_project
984 990 issue = Issue.find(1)
985 991 issue.update_attribute(:fixed_version_id, 4)
986 992 issue.project = Project.find(5)
987 993 assert issue.save
988 994 issue.reload
989 995 assert_equal 5, issue.project_id
990 996 # Keep fixed_version
991 997 assert_equal 4, issue.fixed_version_id
992 998 end
993 999
994 1000 def test_move_to_another_project_should_clear_fixed_version_when_not_shared_with_the_target_project
995 1001 issue = Issue.find(1)
996 1002 issue.update_attribute(:fixed_version_id, 1)
997 1003 issue.project = Project.find(5)
998 1004 assert issue.save
999 1005 issue.reload
1000 1006 assert_equal 5, issue.project_id
1001 1007 # Cleared fixed_version
1002 1008 assert_equal nil, issue.fixed_version
1003 1009 end
1004 1010
1005 1011 def test_move_to_another_project_should_keep_fixed_version_when_shared_systemwide
1006 1012 issue = Issue.find(1)
1007 1013 issue.update_attribute(:fixed_version_id, 7)
1008 1014 issue.project = Project.find(2)
1009 1015 assert issue.save
1010 1016 issue.reload
1011 1017 assert_equal 2, issue.project_id
1012 1018 # Keep fixed_version
1013 1019 assert_equal 7, issue.fixed_version_id
1014 1020 end
1015 1021
1016 1022 def test_move_to_another_project_should_keep_parent_if_valid
1017 1023 issue = Issue.find(1)
1018 1024 issue.update_attribute(:parent_issue_id, 2)
1019 1025 issue.project = Project.find(3)
1020 1026 assert issue.save
1021 1027 issue.reload
1022 1028 assert_equal 2, issue.parent_id
1023 1029 end
1024 1030
1025 1031 def test_move_to_another_project_should_clear_parent_if_not_valid
1026 1032 issue = Issue.find(1)
1027 1033 issue.update_attribute(:parent_issue_id, 2)
1028 1034 issue.project = Project.find(2)
1029 1035 assert issue.save
1030 1036 issue.reload
1031 1037 assert_nil issue.parent_id
1032 1038 end
1033 1039
1034 1040 def test_move_to_another_project_with_disabled_tracker
1035 1041 issue = Issue.find(1)
1036 1042 target = Project.find(2)
1037 1043 target.tracker_ids = [3]
1038 1044 target.save
1039 1045 issue.project = target
1040 1046 assert issue.save
1041 1047 issue.reload
1042 1048 assert_equal 2, issue.project_id
1043 1049 assert_equal 3, issue.tracker_id
1044 1050 end
1045 1051
1046 1052 def test_copy_to_the_same_project
1047 1053 issue = Issue.find(1)
1048 1054 copy = issue.copy
1049 1055 assert_difference 'Issue.count' do
1050 1056 copy.save!
1051 1057 end
1052 1058 assert_kind_of Issue, copy
1053 1059 assert_equal issue.project, copy.project
1054 1060 assert_equal "125", copy.custom_value_for(2).value
1055 1061 end
1056 1062
1057 1063 def test_copy_to_another_project_and_tracker
1058 1064 issue = Issue.find(1)
1059 1065 copy = issue.copy(:project_id => 3, :tracker_id => 2)
1060 1066 assert_difference 'Issue.count' do
1061 1067 copy.save!
1062 1068 end
1063 1069 copy.reload
1064 1070 assert_kind_of Issue, copy
1065 1071 assert_equal Project.find(3), copy.project
1066 1072 assert_equal Tracker.find(2), copy.tracker
1067 1073 # Custom field #2 is not associated with target tracker
1068 1074 assert_nil copy.custom_value_for(2)
1069 1075 end
1070 1076
1071 1077 context "#copy" do
1072 1078 setup do
1073 1079 @issue = Issue.find(1)
1074 1080 end
1075 1081
1076 1082 should "not create a journal" do
1077 1083 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3)
1078 1084 copy.save!
1079 1085 assert_equal 0, copy.reload.journals.size
1080 1086 end
1081 1087
1082 1088 should "allow assigned_to changes" do
1083 1089 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :assigned_to_id => 3)
1084 1090 assert_equal 3, copy.assigned_to_id
1085 1091 end
1086 1092
1087 1093 should "allow status changes" do
1088 1094 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :status_id => 2)
1089 1095 assert_equal 2, copy.status_id
1090 1096 end
1091 1097
1092 1098 should "allow start date changes" do
1093 1099 date = Date.today
1094 1100 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :start_date => date)
1095 1101 assert_equal date, copy.start_date
1096 1102 end
1097 1103
1098 1104 should "allow due date changes" do
1099 1105 date = Date.today
1100 1106 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :due_date => date)
1101 1107 assert_equal date, copy.due_date
1102 1108 end
1103 1109
1104 1110 should "set current user as author" do
1105 1111 User.current = User.find(9)
1106 1112 copy = @issue.copy(:project_id => 3, :tracker_id => 2)
1107 1113 assert_equal User.current, copy.author
1108 1114 end
1109 1115
1110 1116 should "create a journal with notes" do
1111 1117 date = Date.today
1112 1118 notes = "Notes added when copying"
1113 1119 copy = @issue.copy(:project_id => 3, :tracker_id => 2, :start_date => date)
1114 1120 copy.init_journal(User.current, notes)
1115 1121 copy.save!
1116 1122
1117 1123 assert_equal 1, copy.journals.size
1118 1124 journal = copy.journals.first
1119 1125 assert_equal 0, journal.details.size
1120 1126 assert_equal notes, journal.notes
1121 1127 end
1122 1128 end
1123 1129
1124 1130 def test_valid_parent_project
1125 1131 issue = Issue.find(1)
1126 1132 issue_in_same_project = Issue.find(2)
1127 1133 issue_in_child_project = Issue.find(5)
1128 1134 issue_in_grandchild_project = Issue.generate!(:project_id => 6, :tracker_id => 1)
1129 1135 issue_in_other_child_project = Issue.find(6)
1130 1136 issue_in_different_tree = Issue.find(4)
1131 1137
1132 1138 with_settings :cross_project_subtasks => '' do
1133 1139 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1134 1140 assert_equal false, issue.valid_parent_project?(issue_in_child_project)
1135 1141 assert_equal false, issue.valid_parent_project?(issue_in_grandchild_project)
1136 1142 assert_equal false, issue.valid_parent_project?(issue_in_different_tree)
1137 1143 end
1138 1144
1139 1145 with_settings :cross_project_subtasks => 'system' do
1140 1146 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1141 1147 assert_equal true, issue.valid_parent_project?(issue_in_child_project)
1142 1148 assert_equal true, issue.valid_parent_project?(issue_in_different_tree)
1143 1149 end
1144 1150
1145 1151 with_settings :cross_project_subtasks => 'tree' do
1146 1152 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1147 1153 assert_equal true, issue.valid_parent_project?(issue_in_child_project)
1148 1154 assert_equal true, issue.valid_parent_project?(issue_in_grandchild_project)
1149 1155 assert_equal false, issue.valid_parent_project?(issue_in_different_tree)
1150 1156
1151 1157 assert_equal true, issue_in_child_project.valid_parent_project?(issue_in_same_project)
1152 1158 assert_equal true, issue_in_child_project.valid_parent_project?(issue_in_other_child_project)
1153 1159 end
1154 1160
1155 1161 with_settings :cross_project_subtasks => 'descendants' do
1156 1162 assert_equal true, issue.valid_parent_project?(issue_in_same_project)
1157 1163 assert_equal false, issue.valid_parent_project?(issue_in_child_project)
1158 1164 assert_equal false, issue.valid_parent_project?(issue_in_grandchild_project)
1159 1165 assert_equal false, issue.valid_parent_project?(issue_in_different_tree)
1160 1166
1161 1167 assert_equal true, issue_in_child_project.valid_parent_project?(issue)
1162 1168 assert_equal false, issue_in_child_project.valid_parent_project?(issue_in_other_child_project)
1163 1169 end
1164 1170 end
1165 1171
1166 1172 def test_recipients_should_include_previous_assignee
1167 1173 user = User.find(3)
1168 1174 user.members.update_all ["mail_notification = ?", false]
1169 1175 user.update_attribute :mail_notification, 'only_assigned'
1170 1176
1171 1177 issue = Issue.find(2)
1172 1178 issue.assigned_to = nil
1173 1179 assert_include user.mail, issue.recipients
1174 1180 issue.save!
1175 1181 assert !issue.recipients.include?(user.mail)
1176 1182 end
1177 1183
1178 1184 def test_recipients_should_not_include_users_that_cannot_view_the_issue
1179 1185 issue = Issue.find(12)
1180 1186 assert issue.recipients.include?(issue.author.mail)
1181 1187 # copy the issue to a private project
1182 1188 copy = issue.copy(:project_id => 5, :tracker_id => 2)
1183 1189 # author is not a member of project anymore
1184 1190 assert !copy.recipients.include?(copy.author.mail)
1185 1191 end
1186 1192
1187 1193 def test_recipients_should_include_the_assigned_group_members
1188 1194 group_member = User.generate!
1189 1195 group = Group.generate!
1190 1196 group.users << group_member
1191 1197
1192 1198 issue = Issue.find(12)
1193 1199 issue.assigned_to = group
1194 1200 assert issue.recipients.include?(group_member.mail)
1195 1201 end
1196 1202
1197 1203 def test_watcher_recipients_should_not_include_users_that_cannot_view_the_issue
1198 1204 user = User.find(3)
1199 1205 issue = Issue.find(9)
1200 1206 Watcher.create!(:user => user, :watchable => issue)
1201 1207 assert issue.watched_by?(user)
1202 1208 assert !issue.watcher_recipients.include?(user.mail)
1203 1209 end
1204 1210
1205 1211 def test_issue_destroy
1206 1212 Issue.find(1).destroy
1207 1213 assert_nil Issue.find_by_id(1)
1208 1214 assert_nil TimeEntry.find_by_issue_id(1)
1209 1215 end
1210 1216
1211 1217 def test_destroying_a_deleted_issue_should_not_raise_an_error
1212 1218 issue = Issue.find(1)
1213 1219 Issue.find(1).destroy
1214 1220
1215 1221 assert_nothing_raised do
1216 1222 assert_no_difference 'Issue.count' do
1217 1223 issue.destroy
1218 1224 end
1219 1225 assert issue.destroyed?
1220 1226 end
1221 1227 end
1222 1228
1223 1229 def test_destroying_a_stale_issue_should_not_raise_an_error
1224 1230 issue = Issue.find(1)
1225 1231 Issue.find(1).update_attribute :subject, "Updated"
1226 1232
1227 1233 assert_nothing_raised do
1228 1234 assert_difference 'Issue.count', -1 do
1229 1235 issue.destroy
1230 1236 end
1231 1237 assert issue.destroyed?
1232 1238 end
1233 1239 end
1234 1240
1235 1241 def test_blocked
1236 1242 blocked_issue = Issue.find(9)
1237 1243 blocking_issue = Issue.find(10)
1238 1244
1239 1245 assert blocked_issue.blocked?
1240 1246 assert !blocking_issue.blocked?
1241 1247 end
1242 1248
1243 1249 def test_blocked_issues_dont_allow_closed_statuses
1244 1250 blocked_issue = Issue.find(9)
1245 1251
1246 1252 allowed_statuses = blocked_issue.new_statuses_allowed_to(users(:users_002))
1247 1253 assert !allowed_statuses.empty?
1248 1254 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
1249 1255 assert closed_statuses.empty?
1250 1256 end
1251 1257
1252 1258 def test_unblocked_issues_allow_closed_statuses
1253 1259 blocking_issue = Issue.find(10)
1254 1260
1255 1261 allowed_statuses = blocking_issue.new_statuses_allowed_to(users(:users_002))
1256 1262 assert !allowed_statuses.empty?
1257 1263 closed_statuses = allowed_statuses.select {|st| st.is_closed?}
1258 1264 assert !closed_statuses.empty?
1259 1265 end
1260 1266
1261 1267 def test_rescheduling_an_issue_should_reschedule_following_issue
1262 1268 issue1 = Issue.create!(:project_id => 1, :tracker_id => 1,
1263 1269 :author_id => 1, :status_id => 1,
1264 1270 :subject => '-',
1265 1271 :start_date => Date.today, :due_date => Date.today + 2)
1266 1272 issue2 = Issue.create!(:project_id => 1, :tracker_id => 1,
1267 1273 :author_id => 1, :status_id => 1,
1268 1274 :subject => '-',
1269 1275 :start_date => Date.today, :due_date => Date.today + 2)
1270 1276 IssueRelation.create!(:issue_from => issue1, :issue_to => issue2,
1271 1277 :relation_type => IssueRelation::TYPE_PRECEDES)
1272 1278 assert_equal issue1.due_date + 1, issue2.reload.start_date
1273 1279
1274 1280 issue1.due_date = Date.today + 5
1275 1281 issue1.save!
1276 1282 assert_equal issue1.due_date + 1, issue2.reload.start_date
1277 1283 end
1278 1284
1279 1285 def test_rescheduling_a_stale_issue_should_not_raise_an_error
1280 1286 stale = Issue.find(1)
1281 1287 issue = Issue.find(1)
1282 1288 issue.subject = "Updated"
1283 1289 issue.save!
1284 1290
1285 1291 date = 10.days.from_now.to_date
1286 1292 assert_nothing_raised do
1287 1293 stale.reschedule_after(date)
1288 1294 end
1289 1295 assert_equal date, stale.reload.start_date
1290 1296 end
1291 1297
1292 1298 def test_overdue
1293 1299 assert Issue.new(:due_date => 1.day.ago.to_date).overdue?
1294 1300 assert !Issue.new(:due_date => Date.today).overdue?
1295 1301 assert !Issue.new(:due_date => 1.day.from_now.to_date).overdue?
1296 1302 assert !Issue.new(:due_date => nil).overdue?
1297 1303 assert !Issue.new(:due_date => 1.day.ago.to_date,
1298 1304 :status => IssueStatus.find(:first,
1299 1305 :conditions => {:is_closed => true})
1300 1306 ).overdue?
1301 1307 end
1302 1308
1303 1309 context "#behind_schedule?" do
1304 1310 should "be false if the issue has no start_date" do
1305 1311 assert !Issue.new(:start_date => nil,
1306 1312 :due_date => 1.day.from_now.to_date,
1307 1313 :done_ratio => 0).behind_schedule?
1308 1314 end
1309 1315
1310 1316 should "be false if the issue has no end_date" do
1311 1317 assert !Issue.new(:start_date => 1.day.from_now.to_date,
1312 1318 :due_date => nil,
1313 1319 :done_ratio => 0).behind_schedule?
1314 1320 end
1315 1321
1316 1322 should "be false if the issue has more done than it's calendar time" do
1317 1323 assert !Issue.new(:start_date => 50.days.ago.to_date,
1318 1324 :due_date => 50.days.from_now.to_date,
1319 1325 :done_ratio => 90).behind_schedule?
1320 1326 end
1321 1327
1322 1328 should "be true if the issue hasn't been started at all" do
1323 1329 assert Issue.new(:start_date => 1.day.ago.to_date,
1324 1330 :due_date => 1.day.from_now.to_date,
1325 1331 :done_ratio => 0).behind_schedule?
1326 1332 end
1327 1333
1328 1334 should "be true if the issue has used more calendar time than it's done ratio" do
1329 1335 assert Issue.new(:start_date => 100.days.ago.to_date,
1330 1336 :due_date => Date.today,
1331 1337 :done_ratio => 90).behind_schedule?
1332 1338 end
1333 1339 end
1334 1340
1335 1341 context "#assignable_users" do
1336 1342 should "be Users" do
1337 1343 assert_kind_of User, Issue.find(1).assignable_users.first
1338 1344 end
1339 1345
1340 1346 should "include the issue author" do
1341 1347 non_project_member = User.generate!
1342 1348 issue = Issue.generate!(:author => non_project_member)
1343 1349
1344 1350 assert issue.assignable_users.include?(non_project_member)
1345 1351 end
1346 1352
1347 1353 should "include the current assignee" do
1348 1354 user = User.generate!
1349 1355 issue = Issue.generate!(:assigned_to => user)
1350 1356 user.lock!
1351 1357
1352 1358 assert Issue.find(issue.id).assignable_users.include?(user)
1353 1359 end
1354 1360
1355 1361 should "not show the issue author twice" do
1356 1362 assignable_user_ids = Issue.find(1).assignable_users.collect(&:id)
1357 1363 assert_equal 2, assignable_user_ids.length
1358 1364
1359 1365 assignable_user_ids.each do |user_id|
1360 1366 assert_equal 1, assignable_user_ids.select {|i| i == user_id}.length,
1361 1367 "User #{user_id} appears more or less than once"
1362 1368 end
1363 1369 end
1364 1370
1365 1371 context "with issue_group_assignment" do
1366 1372 should "include groups" do
1367 1373 issue = Issue.new(:project => Project.find(2))
1368 1374
1369 1375 with_settings :issue_group_assignment => '1' do
1370 1376 assert_equal %w(Group User), issue.assignable_users.map {|a| a.class.name}.uniq.sort
1371 1377 assert issue.assignable_users.include?(Group.find(11))
1372 1378 end
1373 1379 end
1374 1380 end
1375 1381
1376 1382 context "without issue_group_assignment" do
1377 1383 should "not include groups" do
1378 1384 issue = Issue.new(:project => Project.find(2))
1379 1385
1380 1386 with_settings :issue_group_assignment => '0' do
1381 1387 assert_equal %w(User), issue.assignable_users.map {|a| a.class.name}.uniq.sort
1382 1388 assert !issue.assignable_users.include?(Group.find(11))
1383 1389 end
1384 1390 end
1385 1391 end
1386 1392 end
1387 1393
1388 1394 def test_create_should_send_email_notification
1389 1395 ActionMailer::Base.deliveries.clear
1390 1396 issue = Issue.new(:project_id => 1, :tracker_id => 1,
1391 1397 :author_id => 3, :status_id => 1,
1392 1398 :priority => IssuePriority.all.first,
1393 1399 :subject => 'test_create', :estimated_hours => '1:30')
1394 1400
1395 1401 assert issue.save
1396 1402 assert_equal 1, ActionMailer::Base.deliveries.size
1397 1403 end
1398 1404
1399 1405 def test_stale_issue_should_not_send_email_notification
1400 1406 ActionMailer::Base.deliveries.clear
1401 1407 issue = Issue.find(1)
1402 1408 stale = Issue.find(1)
1403 1409
1404 1410 issue.init_journal(User.find(1))
1405 1411 issue.subject = 'Subjet update'
1406 1412 assert issue.save
1407 1413 assert_equal 1, ActionMailer::Base.deliveries.size
1408 1414 ActionMailer::Base.deliveries.clear
1409 1415
1410 1416 stale.init_journal(User.find(1))
1411 1417 stale.subject = 'Another subjet update'
1412 1418 assert_raise ActiveRecord::StaleObjectError do
1413 1419 stale.save
1414 1420 end
1415 1421 assert ActionMailer::Base.deliveries.empty?
1416 1422 end
1417 1423
1418 1424 def test_journalized_description
1419 1425 IssueCustomField.delete_all
1420 1426
1421 1427 i = Issue.first
1422 1428 old_description = i.description
1423 1429 new_description = "This is the new description"
1424 1430
1425 1431 i.init_journal(User.find(2))
1426 1432 i.description = new_description
1427 1433 assert_difference 'Journal.count', 1 do
1428 1434 assert_difference 'JournalDetail.count', 1 do
1429 1435 i.save!
1430 1436 end
1431 1437 end
1432 1438
1433 1439 detail = JournalDetail.first(:order => 'id DESC')
1434 1440 assert_equal i, detail.journal.journalized
1435 1441 assert_equal 'attr', detail.property
1436 1442 assert_equal 'description', detail.prop_key
1437 1443 assert_equal old_description, detail.old_value
1438 1444 assert_equal new_description, detail.value
1439 1445 end
1440 1446
1441 1447 def test_blank_descriptions_should_not_be_journalized
1442 1448 IssueCustomField.delete_all
1443 1449 Issue.update_all("description = NULL", "id=1")
1444 1450
1445 1451 i = Issue.find(1)
1446 1452 i.init_journal(User.find(2))
1447 1453 i.subject = "blank description"
1448 1454 i.description = "\r\n"
1449 1455
1450 1456 assert_difference 'Journal.count', 1 do
1451 1457 assert_difference 'JournalDetail.count', 1 do
1452 1458 i.save!
1453 1459 end
1454 1460 end
1455 1461 end
1456 1462
1457 1463 def test_journalized_multi_custom_field
1458 1464 field = IssueCustomField.create!(:name => 'filter', :field_format => 'list',
1459 1465 :is_filter => true, :is_for_all => true,
1460 1466 :tracker_ids => [1],
1461 1467 :possible_values => ['value1', 'value2', 'value3'],
1462 1468 :multiple => true)
1463 1469
1464 1470 issue = Issue.create!(:project_id => 1, :tracker_id => 1,
1465 1471 :subject => 'Test', :author_id => 1)
1466 1472
1467 1473 assert_difference 'Journal.count' do
1468 1474 assert_difference 'JournalDetail.count' do
1469 1475 issue.init_journal(User.first)
1470 1476 issue.custom_field_values = {field.id => ['value1']}
1471 1477 issue.save!
1472 1478 end
1473 1479 assert_difference 'JournalDetail.count' do
1474 1480 issue.init_journal(User.first)
1475 1481 issue.custom_field_values = {field.id => ['value1', 'value2']}
1476 1482 issue.save!
1477 1483 end
1478 1484 assert_difference 'JournalDetail.count', 2 do
1479 1485 issue.init_journal(User.first)
1480 1486 issue.custom_field_values = {field.id => ['value3', 'value2']}
1481 1487 issue.save!
1482 1488 end
1483 1489 assert_difference 'JournalDetail.count', 2 do
1484 1490 issue.init_journal(User.first)
1485 1491 issue.custom_field_values = {field.id => nil}
1486 1492 issue.save!
1487 1493 end
1488 1494 end
1489 1495 end
1490 1496
1491 1497 def test_description_eol_should_be_normalized
1492 1498 i = Issue.new(:description => "CR \r LF \n CRLF \r\n")
1493 1499 assert_equal "CR \r\n LF \r\n CRLF \r\n", i.description
1494 1500 end
1495 1501
1496 1502 def test_saving_twice_should_not_duplicate_journal_details
1497 1503 i = Issue.find(:first)
1498 1504 i.init_journal(User.find(2), 'Some notes')
1499 1505 # initial changes
1500 1506 i.subject = 'New subject'
1501 1507 i.done_ratio = i.done_ratio + 10
1502 1508 assert_difference 'Journal.count' do
1503 1509 assert i.save
1504 1510 end
1505 1511 # 1 more change
1506 1512 i.priority = IssuePriority.find(:first, :conditions => ["id <> ?", i.priority_id])
1507 1513 assert_no_difference 'Journal.count' do
1508 1514 assert_difference 'JournalDetail.count', 1 do
1509 1515 i.save
1510 1516 end
1511 1517 end
1512 1518 # no more change
1513 1519 assert_no_difference 'Journal.count' do
1514 1520 assert_no_difference 'JournalDetail.count' do
1515 1521 i.save
1516 1522 end
1517 1523 end
1518 1524 end
1519 1525
1520 1526 def test_all_dependent_issues
1521 1527 IssueRelation.delete_all
1522 1528 assert IssueRelation.create!(:issue_from => Issue.find(1),
1523 1529 :issue_to => Issue.find(2),
1524 1530 :relation_type => IssueRelation::TYPE_PRECEDES)
1525 1531 assert IssueRelation.create!(:issue_from => Issue.find(2),
1526 1532 :issue_to => Issue.find(3),
1527 1533 :relation_type => IssueRelation::TYPE_PRECEDES)
1528 1534 assert IssueRelation.create!(:issue_from => Issue.find(3),
1529 1535 :issue_to => Issue.find(8),
1530 1536 :relation_type => IssueRelation::TYPE_PRECEDES)
1531 1537
1532 1538 assert_equal [2, 3, 8], Issue.find(1).all_dependent_issues.collect(&:id).sort
1533 1539 end
1534 1540
1535 1541 def test_all_dependent_issues_with_persistent_circular_dependency
1536 1542 IssueRelation.delete_all
1537 1543 assert IssueRelation.create!(:issue_from => Issue.find(1),
1538 1544 :issue_to => Issue.find(2),
1539 1545 :relation_type => IssueRelation::TYPE_PRECEDES)
1540 1546 assert IssueRelation.create!(:issue_from => Issue.find(2),
1541 1547 :issue_to => Issue.find(3),
1542 1548 :relation_type => IssueRelation::TYPE_PRECEDES)
1543 1549
1544 1550 r = IssueRelation.create!(:issue_from => Issue.find(3),
1545 1551 :issue_to => Issue.find(7),
1546 1552 :relation_type => IssueRelation::TYPE_PRECEDES)
1547 1553 IssueRelation.update_all("issue_to_id = 1", ["id = ?", r.id])
1548 1554
1549 1555 assert_equal [2, 3], Issue.find(1).all_dependent_issues.collect(&:id).sort
1550 1556 end
1551 1557
1552 1558 def test_all_dependent_issues_with_persistent_multiple_circular_dependencies
1553 1559 IssueRelation.delete_all
1554 1560 assert IssueRelation.create!(:issue_from => Issue.find(1),
1555 1561 :issue_to => Issue.find(2),
1556 1562 :relation_type => IssueRelation::TYPE_RELATES)
1557 1563 assert IssueRelation.create!(:issue_from => Issue.find(2),
1558 1564 :issue_to => Issue.find(3),
1559 1565 :relation_type => IssueRelation::TYPE_RELATES)
1560 1566 assert IssueRelation.create!(:issue_from => Issue.find(3),
1561 1567 :issue_to => Issue.find(8),
1562 1568 :relation_type => IssueRelation::TYPE_RELATES)
1563 1569
1564 1570 r = IssueRelation.create!(:issue_from => Issue.find(8),
1565 1571 :issue_to => Issue.find(7),
1566 1572 :relation_type => IssueRelation::TYPE_RELATES)
1567 1573 IssueRelation.update_all("issue_to_id = 2", ["id = ?", r.id])
1568 1574
1569 1575 r = IssueRelation.create!(:issue_from => Issue.find(3),
1570 1576 :issue_to => Issue.find(7),
1571 1577 :relation_type => IssueRelation::TYPE_RELATES)
1572 1578 IssueRelation.update_all("issue_to_id = 1", ["id = ?", r.id])
1573 1579
1574 1580 assert_equal [2, 3, 8], Issue.find(1).all_dependent_issues.collect(&:id).sort
1575 1581 end
1576 1582
1577 1583 context "#done_ratio" do
1578 1584 setup do
1579 1585 @issue = Issue.find(1)
1580 1586 @issue_status = IssueStatus.find(1)
1581 1587 @issue_status.update_attribute(:default_done_ratio, 50)
1582 1588 @issue2 = Issue.find(2)
1583 1589 @issue_status2 = IssueStatus.find(2)
1584 1590 @issue_status2.update_attribute(:default_done_ratio, 0)
1585 1591 end
1586 1592
1587 1593 teardown do
1588 1594 Setting.issue_done_ratio = 'issue_field'
1589 1595 end
1590 1596
1591 1597 context "with Setting.issue_done_ratio using the issue_field" do
1592 1598 setup do
1593 1599 Setting.issue_done_ratio = 'issue_field'
1594 1600 end
1595 1601
1596 1602 should "read the issue's field" do
1597 1603 assert_equal 0, @issue.done_ratio
1598 1604 assert_equal 30, @issue2.done_ratio
1599 1605 end
1600 1606 end
1601 1607
1602 1608 context "with Setting.issue_done_ratio using the issue_status" do
1603 1609 setup do
1604 1610 Setting.issue_done_ratio = 'issue_status'
1605 1611 end
1606 1612
1607 1613 should "read the Issue Status's default done ratio" do
1608 1614 assert_equal 50, @issue.done_ratio
1609 1615 assert_equal 0, @issue2.done_ratio
1610 1616 end
1611 1617 end
1612 1618 end
1613 1619
1614 1620 context "#update_done_ratio_from_issue_status" do
1615 1621 setup do
1616 1622 @issue = Issue.find(1)
1617 1623 @issue_status = IssueStatus.find(1)
1618 1624 @issue_status.update_attribute(:default_done_ratio, 50)
1619 1625 @issue2 = Issue.find(2)
1620 1626 @issue_status2 = IssueStatus.find(2)
1621 1627 @issue_status2.update_attribute(:default_done_ratio, 0)
1622 1628 end
1623 1629
1624 1630 context "with Setting.issue_done_ratio using the issue_field" do
1625 1631 setup do
1626 1632 Setting.issue_done_ratio = 'issue_field'
1627 1633 end
1628 1634
1629 1635 should "not change the issue" do
1630 1636 @issue.update_done_ratio_from_issue_status
1631 1637 @issue2.update_done_ratio_from_issue_status
1632 1638
1633 1639 assert_equal 0, @issue.read_attribute(:done_ratio)
1634 1640 assert_equal 30, @issue2.read_attribute(:done_ratio)
1635 1641 end
1636 1642 end
1637 1643
1638 1644 context "with Setting.issue_done_ratio using the issue_status" do
1639 1645 setup do
1640 1646 Setting.issue_done_ratio = 'issue_status'
1641 1647 end
1642 1648
1643 1649 should "change the issue's done ratio" do
1644 1650 @issue.update_done_ratio_from_issue_status
1645 1651 @issue2.update_done_ratio_from_issue_status
1646 1652
1647 1653 assert_equal 50, @issue.read_attribute(:done_ratio)
1648 1654 assert_equal 0, @issue2.read_attribute(:done_ratio)
1649 1655 end
1650 1656 end
1651 1657 end
1652 1658
1653 1659 test "#by_tracker" do
1654 1660 User.current = User.anonymous
1655 1661 groups = Issue.by_tracker(Project.find(1))
1656 1662 assert_equal 3, groups.size
1657 1663 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1658 1664 end
1659 1665
1660 1666 test "#by_version" do
1661 1667 User.current = User.anonymous
1662 1668 groups = Issue.by_version(Project.find(1))
1663 1669 assert_equal 3, groups.size
1664 1670 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1665 1671 end
1666 1672
1667 1673 test "#by_priority" do
1668 1674 User.current = User.anonymous
1669 1675 groups = Issue.by_priority(Project.find(1))
1670 1676 assert_equal 4, groups.size
1671 1677 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1672 1678 end
1673 1679
1674 1680 test "#by_category" do
1675 1681 User.current = User.anonymous
1676 1682 groups = Issue.by_category(Project.find(1))
1677 1683 assert_equal 2, groups.size
1678 1684 assert_equal 3, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1679 1685 end
1680 1686
1681 1687 test "#by_assigned_to" do
1682 1688 User.current = User.anonymous
1683 1689 groups = Issue.by_assigned_to(Project.find(1))
1684 1690 assert_equal 2, groups.size
1685 1691 assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1686 1692 end
1687 1693
1688 1694 test "#by_author" do
1689 1695 User.current = User.anonymous
1690 1696 groups = Issue.by_author(Project.find(1))
1691 1697 assert_equal 4, groups.size
1692 1698 assert_equal 7, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1693 1699 end
1694 1700
1695 1701 test "#by_subproject" do
1696 1702 User.current = User.anonymous
1697 1703 groups = Issue.by_subproject(Project.find(1))
1698 1704 # Private descendant not visible
1699 1705 assert_equal 1, groups.size
1700 1706 assert_equal 2, groups.inject(0) {|sum, group| sum + group['total'].to_i}
1701 1707 end
1702 1708
1703 1709 def test_recently_updated_scope
1704 1710 #should return the last updated issue
1705 1711 assert_equal Issue.reorder("updated_on DESC").first, Issue.recently_updated.limit(1).first
1706 1712 end
1707 1713
1708 1714 def test_on_active_projects_scope
1709 1715 assert Project.find(2).archive
1710 1716
1711 1717 before = Issue.on_active_project.length
1712 1718 # test inclusion to results
1713 1719 issue = Issue.generate!(:tracker => Project.find(2).trackers.first)
1714 1720 assert_equal before + 1, Issue.on_active_project.length
1715 1721
1716 1722 # Move to an archived project
1717 1723 issue.project = Project.find(2)
1718 1724 assert issue.save
1719 1725 assert_equal before, Issue.on_active_project.length
1720 1726 end
1721 1727
1722 1728 context "Issue#recipients" do
1723 1729 setup do
1724 1730 @project = Project.find(1)
1725 1731 @author = User.generate!
1726 1732 @assignee = User.generate!
1727 1733 @issue = Issue.generate!(:project => @project, :assigned_to => @assignee, :author => @author)
1728 1734 end
1729 1735
1730 1736 should "include project recipients" do
1731 1737 assert @project.recipients.present?
1732 1738 @project.recipients.each do |project_recipient|
1733 1739 assert @issue.recipients.include?(project_recipient)
1734 1740 end
1735 1741 end
1736 1742
1737 1743 should "include the author if the author is active" do
1738 1744 assert @issue.author, "No author set for Issue"
1739 1745 assert @issue.recipients.include?(@issue.author.mail)
1740 1746 end
1741 1747
1742 1748 should "include the assigned to user if the assigned to user is active" do
1743 1749 assert @issue.assigned_to, "No assigned_to set for Issue"
1744 1750 assert @issue.recipients.include?(@issue.assigned_to.mail)
1745 1751 end
1746 1752
1747 1753 should "not include users who opt out of all email" do
1748 1754 @author.update_attribute(:mail_notification, :none)
1749 1755
1750 1756 assert !@issue.recipients.include?(@issue.author.mail)
1751 1757 end
1752 1758
1753 1759 should "not include the issue author if they are only notified of assigned issues" do
1754 1760 @author.update_attribute(:mail_notification, :only_assigned)
1755 1761
1756 1762 assert !@issue.recipients.include?(@issue.author.mail)
1757 1763 end
1758 1764
1759 1765 should "not include the assigned user if they are only notified of owned issues" do
1760 1766 @assignee.update_attribute(:mail_notification, :only_owner)
1761 1767
1762 1768 assert !@issue.recipients.include?(@issue.assigned_to.mail)
1763 1769 end
1764 1770 end
1765 1771
1766 1772 def test_last_journal_id_with_journals_should_return_the_journal_id
1767 1773 assert_equal 2, Issue.find(1).last_journal_id
1768 1774 end
1769 1775
1770 1776 def test_last_journal_id_without_journals_should_return_nil
1771 1777 assert_nil Issue.find(3).last_journal_id
1772 1778 end
1773 1779
1774 1780 def test_journals_after_should_return_journals_with_greater_id
1775 1781 assert_equal [Journal.find(2)], Issue.find(1).journals_after('1')
1776 1782 assert_equal [], Issue.find(1).journals_after('2')
1777 1783 end
1778 1784
1779 1785 def test_journals_after_with_blank_arg_should_return_all_journals
1780 1786 assert_equal [Journal.find(1), Journal.find(2)], Issue.find(1).journals_after('')
1781 1787 end
1782 1788 end
General Comments 0
You need to be logged in to leave comments. Login now