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