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