##// END OF EJS Templates
Changed assertions to make them work with Rails2/3 ruby1.8/1.9 different behaviours....
Jean-Philippe Lang -
r8987:97ca65d1b217
parent child
Show More
@@ -1,887 +1,884
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 UserTest < ActiveSupport::TestCase
21 21 fixtures :users, :members, :projects, :roles, :member_roles, :auth_sources,
22 22 :trackers, :issue_statuses,
23 23 :projects_trackers,
24 24 :watchers,
25 25 :issue_categories, :enumerations, :issues,
26 26 :journals, :journal_details,
27 27 :groups_users,
28 28 :enabled_modules,
29 29 :workflows
30 30
31 31 def setup
32 32 @admin = User.find(1)
33 33 @jsmith = User.find(2)
34 34 @dlopper = User.find(3)
35 35 end
36 36
37 37 test 'object_daddy creation' do
38 38 User.generate_with_protected!(:firstname => 'Testing connection')
39 39 User.generate_with_protected!(:firstname => 'Testing connection')
40 40 assert_equal 2, User.count(:all, :conditions => {:firstname => 'Testing connection'})
41 41 end
42 42
43 43 def test_truth
44 44 assert_kind_of User, @jsmith
45 45 end
46 46
47 47 def test_mail_should_be_stripped
48 48 u = User.new
49 49 u.mail = " foo@bar.com "
50 50 assert_equal "foo@bar.com", u.mail
51 51 end
52 52
53 53 def test_mail_validation
54 54 u = User.new
55 55 u.mail = ''
56 56 assert !u.valid?
57 assert_equal I18n.translate('activerecord.errors.messages.blank'),
58 u.errors[:mail].to_s
57 assert_include I18n.translate('activerecord.errors.messages.blank'), u.errors[:mail]
59 58 end
60 59
61 60 def test_login_length_validation
62 61 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
63 62 user.login = "x" * (User::LOGIN_LENGTH_LIMIT+1)
64 63 assert !user.valid?
65 64
66 65 user.login = "x" * (User::LOGIN_LENGTH_LIMIT)
67 66 assert user.valid?
68 67 assert user.save
69 68 end
70 69
71 70 def test_create
72 71 user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
73 72
74 73 user.login = "jsmith"
75 74 user.password, user.password_confirmation = "password", "password"
76 75 # login uniqueness
77 76 assert !user.save
78 77 assert_equal 1, user.errors.count
79 78
80 79 user.login = "newuser"
81 80 user.password, user.password_confirmation = "passwd", "password"
82 81 # password confirmation
83 82 assert !user.save
84 83 assert_equal 1, user.errors.count
85 84
86 85 user.password, user.password_confirmation = "password", "password"
87 86 assert user.save
88 87 end
89 88
90 89 context "User#before_create" do
91 90 should "set the mail_notification to the default Setting" do
92 91 @user1 = User.generate_with_protected!
93 92 assert_equal 'only_my_events', @user1.mail_notification
94 93
95 94 with_settings :default_notification_option => 'all' do
96 95 @user2 = User.generate_with_protected!
97 96 assert_equal 'all', @user2.mail_notification
98 97 end
99 98 end
100 99 end
101 100
102 101 context "User.login" do
103 102 should "be case-insensitive." do
104 103 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
105 104 u.login = 'newuser'
106 105 u.password, u.password_confirmation = "password", "password"
107 106 assert u.save
108 107
109 108 u = User.new(:firstname => "Similar", :lastname => "User", :mail => "similaruser@somenet.foo")
110 109 u.login = 'NewUser'
111 110 u.password, u.password_confirmation = "password", "password"
112 111 assert !u.save
113 assert_equal I18n.translate('activerecord.errors.messages.taken'),
114 u.errors[:login].to_s
112 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:login]
115 113 end
116 114 end
117 115
118 116 def test_mail_uniqueness_should_not_be_case_sensitive
119 117 u = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
120 118 u.login = 'newuser1'
121 119 u.password, u.password_confirmation = "password", "password"
122 120 assert u.save
123 121
124 122 u = User.new(:firstname => "new", :lastname => "user", :mail => "newUser@Somenet.foo")
125 123 u.login = 'newuser2'
126 124 u.password, u.password_confirmation = "password", "password"
127 125 assert !u.save
128 assert_equal I18n.translate('activerecord.errors.messages.taken'),
129 u.errors[:mail].to_s
126 assert_include I18n.translate('activerecord.errors.messages.taken'), u.errors[:mail]
130 127 end
131 128
132 129 def test_update
133 130 assert_equal "admin", @admin.login
134 131 @admin.login = "john"
135 132 assert @admin.save, @admin.errors.full_messages.join("; ")
136 133 @admin.reload
137 134 assert_equal "john", @admin.login
138 135 end
139 136
140 137 def test_destroy_should_delete_members_and_roles
141 138 members = Member.find_all_by_user_id(2)
142 139 ms = members.size
143 140 rs = members.collect(&:roles).flatten.size
144 141
145 142 assert_difference 'Member.count', - ms do
146 143 assert_difference 'MemberRole.count', - rs do
147 144 User.find(2).destroy
148 145 end
149 146 end
150 147
151 148 assert_nil User.find_by_id(2)
152 149 assert Member.find_all_by_user_id(2).empty?
153 150 end
154 151
155 152 def test_destroy_should_update_attachments
156 153 attachment = Attachment.create!(:container => Project.find(1),
157 154 :file => uploaded_test_file("testfile.txt", "text/plain"),
158 155 :author_id => 2)
159 156
160 157 User.find(2).destroy
161 158 assert_nil User.find_by_id(2)
162 159 assert_equal User.anonymous, attachment.reload.author
163 160 end
164 161
165 162 def test_destroy_should_update_comments
166 163 comment = Comment.create!(
167 164 :commented => News.create!(:project_id => 1, :author_id => 1, :title => 'foo', :description => 'foo'),
168 165 :author => User.find(2),
169 166 :comments => 'foo'
170 167 )
171 168
172 169 User.find(2).destroy
173 170 assert_nil User.find_by_id(2)
174 171 assert_equal User.anonymous, comment.reload.author
175 172 end
176 173
177 174 def test_destroy_should_update_issues
178 175 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
179 176
180 177 User.find(2).destroy
181 178 assert_nil User.find_by_id(2)
182 179 assert_equal User.anonymous, issue.reload.author
183 180 end
184 181
185 182 def test_destroy_should_unassign_issues
186 183 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
187 184
188 185 User.find(2).destroy
189 186 assert_nil User.find_by_id(2)
190 187 assert_nil issue.reload.assigned_to
191 188 end
192 189
193 190 def test_destroy_should_update_journals
194 191 issue = Issue.create!(:project_id => 1, :author_id => 2, :tracker_id => 1, :subject => 'foo')
195 192 issue.init_journal(User.find(2), "update")
196 193 issue.save!
197 194
198 195 User.find(2).destroy
199 196 assert_nil User.find_by_id(2)
200 197 assert_equal User.anonymous, issue.journals.first.reload.user
201 198 end
202 199
203 200 def test_destroy_should_update_journal_details_old_value
204 201 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo', :assigned_to_id => 2)
205 202 issue.init_journal(User.find(1), "update")
206 203 issue.assigned_to_id = nil
207 204 assert_difference 'JournalDetail.count' do
208 205 issue.save!
209 206 end
210 207 journal_detail = JournalDetail.first(:order => 'id DESC')
211 208 assert_equal '2', journal_detail.old_value
212 209
213 210 User.find(2).destroy
214 211 assert_nil User.find_by_id(2)
215 212 assert_equal User.anonymous.id.to_s, journal_detail.reload.old_value
216 213 end
217 214
218 215 def test_destroy_should_update_journal_details_value
219 216 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
220 217 issue.init_journal(User.find(1), "update")
221 218 issue.assigned_to_id = 2
222 219 assert_difference 'JournalDetail.count' do
223 220 issue.save!
224 221 end
225 222 journal_detail = JournalDetail.first(:order => 'id DESC')
226 223 assert_equal '2', journal_detail.value
227 224
228 225 User.find(2).destroy
229 226 assert_nil User.find_by_id(2)
230 227 assert_equal User.anonymous.id.to_s, journal_detail.reload.value
231 228 end
232 229
233 230 def test_destroy_should_update_messages
234 231 board = Board.create!(:project_id => 1, :name => 'Board', :description => 'Board')
235 232 message = Message.create!(:board_id => board.id, :author_id => 2, :subject => 'foo', :content => 'foo')
236 233
237 234 User.find(2).destroy
238 235 assert_nil User.find_by_id(2)
239 236 assert_equal User.anonymous, message.reload.author
240 237 end
241 238
242 239 def test_destroy_should_update_news
243 240 news = News.create!(:project_id => 1, :author_id => 2, :title => 'foo', :description => 'foo')
244 241
245 242 User.find(2).destroy
246 243 assert_nil User.find_by_id(2)
247 244 assert_equal User.anonymous, news.reload.author
248 245 end
249 246
250 247 def test_destroy_should_delete_private_queries
251 248 query = Query.new(:name => 'foo', :is_public => false)
252 249 query.project_id = 1
253 250 query.user_id = 2
254 251 query.save!
255 252
256 253 User.find(2).destroy
257 254 assert_nil User.find_by_id(2)
258 255 assert_nil Query.find_by_id(query.id)
259 256 end
260 257
261 258 def test_destroy_should_update_public_queries
262 259 query = Query.new(:name => 'foo', :is_public => true)
263 260 query.project_id = 1
264 261 query.user_id = 2
265 262 query.save!
266 263
267 264 User.find(2).destroy
268 265 assert_nil User.find_by_id(2)
269 266 assert_equal User.anonymous, query.reload.user
270 267 end
271 268
272 269 def test_destroy_should_update_time_entries
273 270 entry = TimeEntry.new(:hours => '2', :spent_on => Date.today, :activity => TimeEntryActivity.create!(:name => 'foo'))
274 271 entry.project_id = 1
275 272 entry.user_id = 2
276 273 entry.save!
277 274
278 275 User.find(2).destroy
279 276 assert_nil User.find_by_id(2)
280 277 assert_equal User.anonymous, entry.reload.user
281 278 end
282 279
283 280 def test_destroy_should_delete_tokens
284 281 token = Token.create!(:user_id => 2, :value => 'foo')
285 282
286 283 User.find(2).destroy
287 284 assert_nil User.find_by_id(2)
288 285 assert_nil Token.find_by_id(token.id)
289 286 end
290 287
291 288 def test_destroy_should_delete_watchers
292 289 issue = Issue.create!(:project_id => 1, :author_id => 1, :tracker_id => 1, :subject => 'foo')
293 290 watcher = Watcher.create!(:user_id => 2, :watchable => issue)
294 291
295 292 User.find(2).destroy
296 293 assert_nil User.find_by_id(2)
297 294 assert_nil Watcher.find_by_id(watcher.id)
298 295 end
299 296
300 297 def test_destroy_should_update_wiki_contents
301 298 wiki_content = WikiContent.create!(
302 299 :text => 'foo',
303 300 :author_id => 2,
304 301 :page => WikiPage.create!(:title => 'Foo', :wiki => Wiki.create!(:project_id => 1, :start_page => 'Start'))
305 302 )
306 303 wiki_content.text = 'bar'
307 304 assert_difference 'WikiContent::Version.count' do
308 305 wiki_content.save!
309 306 end
310 307
311 308 User.find(2).destroy
312 309 assert_nil User.find_by_id(2)
313 310 assert_equal User.anonymous, wiki_content.reload.author
314 311 wiki_content.versions.each do |version|
315 312 assert_equal User.anonymous, version.reload.author
316 313 end
317 314 end
318 315
319 316 def test_destroy_should_nullify_issue_categories
320 317 category = IssueCategory.create!(:project_id => 1, :assigned_to_id => 2, :name => 'foo')
321 318
322 319 User.find(2).destroy
323 320 assert_nil User.find_by_id(2)
324 321 assert_nil category.reload.assigned_to_id
325 322 end
326 323
327 324 def test_destroy_should_nullify_changesets
328 325 changeset = Changeset.create!(
329 326 :repository => Repository::Subversion.generate!(
330 327 :project_id => 1
331 328 ),
332 329 :revision => '12',
333 330 :committed_on => Time.now,
334 331 :committer => 'jsmith'
335 332 )
336 333 assert_equal 2, changeset.user_id
337 334
338 335 User.find(2).destroy
339 336 assert_nil User.find_by_id(2)
340 337 assert_nil changeset.reload.user_id
341 338 end
342 339
343 340 def test_anonymous_user_should_not_be_destroyable
344 341 assert_no_difference 'User.count' do
345 342 assert_equal false, User.anonymous.destroy
346 343 end
347 344 end
348 345
349 346 def test_validate_login_presence
350 347 @admin.login = ""
351 348 assert !@admin.save
352 349 assert_equal 1, @admin.errors.count
353 350 end
354 351
355 352 def test_validate_mail_notification_inclusion
356 353 u = User.new
357 354 u.mail_notification = 'foo'
358 355 u.save
359 356 assert_not_nil u.errors[:mail_notification]
360 357 end
361 358
362 359 context "User#try_to_login" do
363 360 should "fall-back to case-insensitive if user login is not found as-typed." do
364 361 user = User.try_to_login("AdMin", "admin")
365 362 assert_kind_of User, user
366 363 assert_equal "admin", user.login
367 364 end
368 365
369 366 should "select the exact matching user first" do
370 367 case_sensitive_user = User.generate_with_protected!(
371 368 :login => 'changed', :password => 'admin',
372 369 :password_confirmation => 'admin')
373 370 # bypass validations to make it appear like existing data
374 371 case_sensitive_user.update_attribute(:login, 'ADMIN')
375 372
376 373 user = User.try_to_login("ADMIN", "admin")
377 374 assert_kind_of User, user
378 375 assert_equal "ADMIN", user.login
379 376
380 377 end
381 378 end
382 379
383 380 def test_password
384 381 user = User.try_to_login("admin", "admin")
385 382 assert_kind_of User, user
386 383 assert_equal "admin", user.login
387 384 user.password = "hello"
388 385 assert user.save
389 386
390 387 user = User.try_to_login("admin", "hello")
391 388 assert_kind_of User, user
392 389 assert_equal "admin", user.login
393 390 end
394 391
395 392 def test_validate_password_length
396 393 with_settings :password_min_length => '100' do
397 394 user = User.new(:firstname => "new100", :lastname => "user100", :mail => "newuser100@somenet.foo")
398 395 user.login = "newuser100"
399 396 user.password, user.password_confirmation = "password100", "password100"
400 397 assert !user.save
401 398 assert_equal 1, user.errors.count
402 399 end
403 400 end
404 401
405 402 def test_name_format
406 403 assert_equal 'Smith, John', @jsmith.name(:lastname_coma_firstname)
407 404 with_settings :user_format => :firstname_lastname do
408 405 assert_equal 'John Smith', @jsmith.reload.name
409 406 end
410 407 with_settings :user_format => :username do
411 408 assert_equal 'jsmith', @jsmith.reload.name
412 409 end
413 410 end
414 411
415 412 def test_fields_for_order_statement_should_return_fields_according_user_format_setting
416 413 with_settings :user_format => 'lastname_coma_firstname' do
417 414 assert_equal ['users.lastname', 'users.firstname', 'users.id'], User.fields_for_order_statement
418 415 end
419 416 end
420 417
421 418 def test_fields_for_order_statement_width_table_name_should_prepend_table_name
422 419 with_settings :user_format => 'lastname_firstname' do
423 420 assert_equal ['authors.lastname', 'authors.firstname', 'authors.id'], User.fields_for_order_statement('authors')
424 421 end
425 422 end
426 423
427 424 def test_fields_for_order_statement_with_blank_format_should_return_default
428 425 with_settings :user_format => '' do
429 426 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
430 427 end
431 428 end
432 429
433 430 def test_fields_for_order_statement_with_invalid_format_should_return_default
434 431 with_settings :user_format => 'foo' do
435 432 assert_equal ['users.firstname', 'users.lastname', 'users.id'], User.fields_for_order_statement
436 433 end
437 434 end
438 435
439 436 def test_lock
440 437 user = User.try_to_login("jsmith", "jsmith")
441 438 assert_equal @jsmith, user
442 439
443 440 @jsmith.status = User::STATUS_LOCKED
444 441 assert @jsmith.save
445 442
446 443 user = User.try_to_login("jsmith", "jsmith")
447 444 assert_equal nil, user
448 445 end
449 446
450 447 context ".try_to_login" do
451 448 context "with good credentials" do
452 449 should "return the user" do
453 450 user = User.try_to_login("admin", "admin")
454 451 assert_kind_of User, user
455 452 assert_equal "admin", user.login
456 453 end
457 454 end
458 455
459 456 context "with wrong credentials" do
460 457 should "return nil" do
461 458 assert_nil User.try_to_login("admin", "foo")
462 459 end
463 460 end
464 461 end
465 462
466 463 if ldap_configured?
467 464 context "#try_to_login using LDAP" do
468 465 context "with failed connection to the LDAP server" do
469 466 should "return nil" do
470 467 @auth_source = AuthSourceLdap.find(1)
471 468 AuthSource.any_instance.stubs(:initialize_ldap_con).raises(Net::LDAP::LdapError, 'Cannot connect')
472 469
473 470 assert_equal nil, User.try_to_login('edavis', 'wrong')
474 471 end
475 472 end
476 473
477 474 context "with an unsuccessful authentication" do
478 475 should "return nil" do
479 476 assert_equal nil, User.try_to_login('edavis', 'wrong')
480 477 end
481 478 end
482 479
483 480 context "on the fly registration" do
484 481 setup do
485 482 @auth_source = AuthSourceLdap.find(1)
486 483 @auth_source.update_attribute :onthefly_register, true
487 484 end
488 485
489 486 context "with a successful authentication" do
490 487 should "create a new user account if it doesn't exist" do
491 488 assert_difference('User.count') do
492 489 user = User.try_to_login('edavis', '123456')
493 490 assert !user.admin?
494 491 end
495 492 end
496 493
497 494 should "retrieve existing user" do
498 495 user = User.try_to_login('edavis', '123456')
499 496 user.admin = true
500 497 user.save!
501 498
502 499 assert_no_difference('User.count') do
503 500 user = User.try_to_login('edavis', '123456')
504 501 assert user.admin?
505 502 end
506 503 end
507 504 end
508 505 end
509 506 end
510 507
511 508 else
512 509 puts "Skipping LDAP tests."
513 510 end
514 511
515 512 def test_create_anonymous
516 513 AnonymousUser.delete_all
517 514 anon = User.anonymous
518 515 assert !anon.new_record?
519 516 assert_kind_of AnonymousUser, anon
520 517 end
521 518
522 519 def test_ensure_single_anonymous_user
523 520 AnonymousUser.delete_all
524 521 anon1 = User.anonymous
525 522 assert !anon1.new_record?
526 523 assert_kind_of AnonymousUser, anon1
527 524 anon2 = AnonymousUser.create(
528 525 :lastname => 'Anonymous', :firstname => '',
529 526 :mail => '', :login => '', :status => 0)
530 527 assert_equal 1, anon2.errors.count
531 528 end
532 529
533 530 def test_rss_key
534 531 assert_nil @jsmith.rss_token
535 532 key = @jsmith.rss_key
536 533 assert_equal 40, key.length
537 534
538 535 @jsmith.reload
539 536 assert_equal key, @jsmith.rss_key
540 537 end
541 538
542 539 context "User#api_key" do
543 540 should "generate a new one if the user doesn't have one" do
544 541 user = User.generate_with_protected!(:api_token => nil)
545 542 assert_nil user.api_token
546 543
547 544 key = user.api_key
548 545 assert_equal 40, key.length
549 546 user.reload
550 547 assert_equal key, user.api_key
551 548 end
552 549
553 550 should "return the existing api token value" do
554 551 user = User.generate_with_protected!
555 552 token = Token.generate!(:action => 'api')
556 553 user.api_token = token
557 554 assert user.save
558 555
559 556 assert_equal token.value, user.api_key
560 557 end
561 558 end
562 559
563 560 context "User#find_by_api_key" do
564 561 should "return nil if no matching key is found" do
565 562 assert_nil User.find_by_api_key('zzzzzzzzz')
566 563 end
567 564
568 565 should "return nil if the key is found for an inactive user" do
569 566 user = User.generate_with_protected!(:status => User::STATUS_LOCKED)
570 567 token = Token.generate!(:action => 'api')
571 568 user.api_token = token
572 569 user.save
573 570
574 571 assert_nil User.find_by_api_key(token.value)
575 572 end
576 573
577 574 should "return the user if the key is found for an active user" do
578 575 user = User.generate_with_protected!(:status => User::STATUS_ACTIVE)
579 576 token = Token.generate!(:action => 'api')
580 577 user.api_token = token
581 578 user.save
582 579
583 580 assert_equal user, User.find_by_api_key(token.value)
584 581 end
585 582 end
586 583
587 584 def test_roles_for_project
588 585 # user with a role
589 586 roles = @jsmith.roles_for_project(Project.find(1))
590 587 assert_kind_of Role, roles.first
591 588 assert_equal "Manager", roles.first.name
592 589
593 590 # user with no role
594 591 assert_nil @dlopper.roles_for_project(Project.find(2)).detect {|role| role.member?}
595 592 end
596 593
597 594 def test_projects_by_role_for_user_with_role
598 595 user = User.find(2)
599 596 assert_kind_of Hash, user.projects_by_role
600 597 assert_equal 2, user.projects_by_role.size
601 598 assert_equal [1,5], user.projects_by_role[Role.find(1)].collect(&:id).sort
602 599 assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort
603 600 end
604 601
605 602 def test_projects_by_role_for_user_with_no_role
606 603 user = User.generate!
607 604 assert_equal({}, user.projects_by_role)
608 605 end
609 606
610 607 def test_projects_by_role_for_anonymous
611 608 assert_equal({}, User.anonymous.projects_by_role)
612 609 end
613 610
614 611 def test_valid_notification_options
615 612 # without memberships
616 613 assert_equal 5, User.find(7).valid_notification_options.size
617 614 # with memberships
618 615 assert_equal 6, User.find(2).valid_notification_options.size
619 616 end
620 617
621 618 def test_valid_notification_options_class_method
622 619 assert_equal 5, User.valid_notification_options.size
623 620 assert_equal 5, User.valid_notification_options(User.find(7)).size
624 621 assert_equal 6, User.valid_notification_options(User.find(2)).size
625 622 end
626 623
627 624 def test_mail_notification_all
628 625 @jsmith.mail_notification = 'all'
629 626 @jsmith.notified_project_ids = []
630 627 @jsmith.save
631 628 @jsmith.reload
632 629 assert @jsmith.projects.first.recipients.include?(@jsmith.mail)
633 630 end
634 631
635 632 def test_mail_notification_selected
636 633 @jsmith.mail_notification = 'selected'
637 634 @jsmith.notified_project_ids = [1]
638 635 @jsmith.save
639 636 @jsmith.reload
640 637 assert Project.find(1).recipients.include?(@jsmith.mail)
641 638 end
642 639
643 640 def test_mail_notification_only_my_events
644 641 @jsmith.mail_notification = 'only_my_events'
645 642 @jsmith.notified_project_ids = []
646 643 @jsmith.save
647 644 @jsmith.reload
648 645 assert !@jsmith.projects.first.recipients.include?(@jsmith.mail)
649 646 end
650 647
651 648 def test_comments_sorting_preference
652 649 assert !@jsmith.wants_comments_in_reverse_order?
653 650 @jsmith.pref.comments_sorting = 'asc'
654 651 assert !@jsmith.wants_comments_in_reverse_order?
655 652 @jsmith.pref.comments_sorting = 'desc'
656 653 assert @jsmith.wants_comments_in_reverse_order?
657 654 end
658 655
659 656 def test_find_by_mail_should_be_case_insensitive
660 657 u = User.find_by_mail('JSmith@somenet.foo')
661 658 assert_not_nil u
662 659 assert_equal 'jsmith@somenet.foo', u.mail
663 660 end
664 661
665 662 def test_random_password
666 663 u = User.new
667 664 u.random_password
668 665 assert !u.password.blank?
669 666 assert !u.password_confirmation.blank?
670 667 end
671 668
672 669 context "#change_password_allowed?" do
673 670 should "be allowed if no auth source is set" do
674 671 user = User.generate_with_protected!
675 672 assert user.change_password_allowed?
676 673 end
677 674
678 675 should "delegate to the auth source" do
679 676 user = User.generate_with_protected!
680 677
681 678 allowed_auth_source = AuthSource.generate!
682 679 def allowed_auth_source.allow_password_changes?; true; end
683 680
684 681 denied_auth_source = AuthSource.generate!
685 682 def denied_auth_source.allow_password_changes?; false; end
686 683
687 684 assert user.change_password_allowed?
688 685
689 686 user.auth_source = allowed_auth_source
690 687 assert user.change_password_allowed?, "User not allowed to change password, though auth source does"
691 688
692 689 user.auth_source = denied_auth_source
693 690 assert !user.change_password_allowed?, "User allowed to change password, though auth source does not"
694 691 end
695 692
696 693 end
697 694
698 695 context "#allowed_to?" do
699 696 context "with a unique project" do
700 697 should "return false if project is archived" do
701 698 project = Project.find(1)
702 699 Project.any_instance.stubs(:status).returns(Project::STATUS_ARCHIVED)
703 700 assert ! @admin.allowed_to?(:view_issues, Project.find(1))
704 701 end
705 702
706 703 should "return false if related module is disabled" do
707 704 project = Project.find(1)
708 705 project.enabled_module_names = ["issue_tracking"]
709 706 assert @admin.allowed_to?(:add_issues, project)
710 707 assert ! @admin.allowed_to?(:view_wiki_pages, project)
711 708 end
712 709
713 710 should "authorize nearly everything for admin users" do
714 711 project = Project.find(1)
715 712 assert ! @admin.member_of?(project)
716 713 %w(edit_issues delete_issues manage_news manage_documents manage_wiki).each do |p|
717 714 assert @admin.allowed_to?(p.to_sym, project)
718 715 end
719 716 end
720 717
721 718 should "authorize normal users depending on their roles" do
722 719 project = Project.find(1)
723 720 assert @jsmith.allowed_to?(:delete_messages, project) #Manager
724 721 assert ! @dlopper.allowed_to?(:delete_messages, project) #Developper
725 722 end
726 723 end
727 724
728 725 context "with multiple projects" do
729 726 should "return false if array is empty" do
730 727 assert ! @admin.allowed_to?(:view_project, [])
731 728 end
732 729
733 730 should "return true only if user has permission on all these projects" do
734 731 assert @admin.allowed_to?(:view_project, Project.all)
735 732 assert ! @dlopper.allowed_to?(:view_project, Project.all) #cannot see Project(2)
736 733 assert @jsmith.allowed_to?(:edit_issues, @jsmith.projects) #Manager or Developer everywhere
737 734 assert ! @jsmith.allowed_to?(:delete_issue_watchers, @jsmith.projects) #Dev cannot delete_issue_watchers
738 735 end
739 736
740 737 should "behave correctly with arrays of 1 project" do
741 738 assert ! User.anonymous.allowed_to?(:delete_issues, [Project.first])
742 739 end
743 740 end
744 741
745 742 context "with options[:global]" do
746 743 should "authorize if user has at least one role that has this permission" do
747 744 @dlopper2 = User.find(5) #only Developper on a project, not Manager anywhere
748 745 @anonymous = User.find(6)
749 746 assert @jsmith.allowed_to?(:delete_issue_watchers, nil, :global => true)
750 747 assert ! @dlopper2.allowed_to?(:delete_issue_watchers, nil, :global => true)
751 748 assert @dlopper2.allowed_to?(:add_issues, nil, :global => true)
752 749 assert ! @anonymous.allowed_to?(:add_issues, nil, :global => true)
753 750 assert @anonymous.allowed_to?(:view_issues, nil, :global => true)
754 751 end
755 752 end
756 753 end
757 754
758 755 context "User#notify_about?" do
759 756 context "Issues" do
760 757 setup do
761 758 @project = Project.find(1)
762 759 @author = User.generate_with_protected!
763 760 @assignee = User.generate_with_protected!
764 761 @issue = Issue.generate_for_project!(@project, :assigned_to => @assignee, :author => @author)
765 762 end
766 763
767 764 should "be true for a user with :all" do
768 765 @author.update_attribute(:mail_notification, 'all')
769 766 assert @author.notify_about?(@issue)
770 767 end
771 768
772 769 should "be false for a user with :none" do
773 770 @author.update_attribute(:mail_notification, 'none')
774 771 assert ! @author.notify_about?(@issue)
775 772 end
776 773
777 774 should "be false for a user with :only_my_events and isn't an author, creator, or assignee" do
778 775 @user = User.generate_with_protected!(:mail_notification => 'only_my_events')
779 776 Member.create!(:user => @user, :project => @project, :role_ids => [1])
780 777 assert ! @user.notify_about?(@issue)
781 778 end
782 779
783 780 should "be true for a user with :only_my_events and is the author" do
784 781 @author.update_attribute(:mail_notification, 'only_my_events')
785 782 assert @author.notify_about?(@issue)
786 783 end
787 784
788 785 should "be true for a user with :only_my_events and is the assignee" do
789 786 @assignee.update_attribute(:mail_notification, 'only_my_events')
790 787 assert @assignee.notify_about?(@issue)
791 788 end
792 789
793 790 should "be true for a user with :only_assigned and is the assignee" do
794 791 @assignee.update_attribute(:mail_notification, 'only_assigned')
795 792 assert @assignee.notify_about?(@issue)
796 793 end
797 794
798 795 should "be false for a user with :only_assigned and is not the assignee" do
799 796 @author.update_attribute(:mail_notification, 'only_assigned')
800 797 assert ! @author.notify_about?(@issue)
801 798 end
802 799
803 800 should "be true for a user with :only_owner and is the author" do
804 801 @author.update_attribute(:mail_notification, 'only_owner')
805 802 assert @author.notify_about?(@issue)
806 803 end
807 804
808 805 should "be false for a user with :only_owner and is not the author" do
809 806 @assignee.update_attribute(:mail_notification, 'only_owner')
810 807 assert ! @assignee.notify_about?(@issue)
811 808 end
812 809
813 810 should "be true for a user with :selected and is the author" do
814 811 @author.update_attribute(:mail_notification, 'selected')
815 812 assert @author.notify_about?(@issue)
816 813 end
817 814
818 815 should "be true for a user with :selected and is the assignee" do
819 816 @assignee.update_attribute(:mail_notification, 'selected')
820 817 assert @assignee.notify_about?(@issue)
821 818 end
822 819
823 820 should "be false for a user with :selected and is not the author or assignee" do
824 821 @user = User.generate_with_protected!(:mail_notification => 'selected')
825 822 Member.create!(:user => @user, :project => @project, :role_ids => [1])
826 823 assert ! @user.notify_about?(@issue)
827 824 end
828 825 end
829 826
830 827 context "other events" do
831 828 should 'be added and tested'
832 829 end
833 830 end
834 831
835 832 def test_salt_unsalted_passwords
836 833 # Restore a user with an unsalted password
837 834 user = User.find(1)
838 835 user.salt = nil
839 836 user.hashed_password = User.hash_password("unsalted")
840 837 user.save!
841 838
842 839 User.salt_unsalted_passwords!
843 840
844 841 user.reload
845 842 # Salt added
846 843 assert !user.salt.blank?
847 844 # Password still valid
848 845 assert user.check_password?("unsalted")
849 846 assert_equal user, User.try_to_login(user.login, "unsalted")
850 847 end
851 848
852 849 if Object.const_defined?(:OpenID)
853 850
854 851 def test_setting_identity_url
855 852 normalized_open_id_url = 'http://example.com/'
856 853 u = User.new( :identity_url => 'http://example.com/' )
857 854 assert_equal normalized_open_id_url, u.identity_url
858 855 end
859 856
860 857 def test_setting_identity_url_without_trailing_slash
861 858 normalized_open_id_url = 'http://example.com/'
862 859 u = User.new( :identity_url => 'http://example.com' )
863 860 assert_equal normalized_open_id_url, u.identity_url
864 861 end
865 862
866 863 def test_setting_identity_url_without_protocol
867 864 normalized_open_id_url = 'http://example.com/'
868 865 u = User.new( :identity_url => 'example.com' )
869 866 assert_equal normalized_open_id_url, u.identity_url
870 867 end
871 868
872 869 def test_setting_blank_identity_url
873 870 u = User.new( :identity_url => 'example.com' )
874 871 u.identity_url = ''
875 872 assert u.identity_url.blank?
876 873 end
877 874
878 875 def test_setting_invalid_identity_url
879 876 u = User.new( :identity_url => 'this is not an openid url' )
880 877 assert u.identity_url.blank?
881 878 end
882 879
883 880 else
884 881 puts "Skipping openid tests."
885 882 end
886 883
887 884 end
@@ -1,229 +1,229
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 VersionTest < ActiveSupport::TestCase
21 21 fixtures :projects, :users, :issues, :issue_statuses, :trackers, :enumerations, :versions
22 22
23 23 def setup
24 24 end
25 25
26 26 def test_create
27 27 v = Version.new(:project => Project.find(1), :name => '1.1', :effective_date => '2011-03-25')
28 28 assert v.save
29 29 assert_equal 'open', v.status
30 30 assert_equal 'none', v.sharing
31 31 end
32 32
33 33 def test_invalid_effective_date_validation
34 34 v = Version.new(:project => Project.find(1), :name => '1.1', :effective_date => '99999-01-01')
35 35 assert !v.save
36 assert_equal I18n.translate('activerecord.errors.messages.not_a_date'),
37 v.errors[:effective_date].to_s
36 assert_include I18n.translate('activerecord.errors.messages.not_a_date'),
37 v.errors[:effective_date]
38 38 end
39 39
40 40 def test_progress_should_be_0_with_no_assigned_issues
41 41 project = Project.find(1)
42 42 v = Version.create!(:project => project, :name => 'Progress')
43 43 assert_equal 0, v.completed_pourcent
44 44 assert_equal 0, v.closed_pourcent
45 45 end
46 46
47 47 def test_progress_should_be_0_with_unbegun_assigned_issues
48 48 project = Project.find(1)
49 49 v = Version.create!(:project => project, :name => 'Progress')
50 50 add_issue(v)
51 51 add_issue(v, :done_ratio => 0)
52 52 assert_progress_equal 0, v.completed_pourcent
53 53 assert_progress_equal 0, v.closed_pourcent
54 54 end
55 55
56 56 def test_progress_should_be_100_with_closed_assigned_issues
57 57 project = Project.find(1)
58 58 status = IssueStatus.find(:first, :conditions => {:is_closed => true})
59 59 v = Version.create!(:project => project, :name => 'Progress')
60 60 add_issue(v, :status => status)
61 61 add_issue(v, :status => status, :done_ratio => 20)
62 62 add_issue(v, :status => status, :done_ratio => 70, :estimated_hours => 25)
63 63 add_issue(v, :status => status, :estimated_hours => 15)
64 64 assert_progress_equal 100.0, v.completed_pourcent
65 65 assert_progress_equal 100.0, v.closed_pourcent
66 66 end
67 67
68 68 def test_progress_should_consider_done_ratio_of_open_assigned_issues
69 69 project = Project.find(1)
70 70 v = Version.create!(:project => project, :name => 'Progress')
71 71 add_issue(v)
72 72 add_issue(v, :done_ratio => 20)
73 73 add_issue(v, :done_ratio => 70)
74 74 assert_progress_equal (0.0 + 20.0 + 70.0)/3, v.completed_pourcent
75 75 assert_progress_equal 0, v.closed_pourcent
76 76 end
77 77
78 78 def test_progress_should_consider_closed_issues_as_completed
79 79 project = Project.find(1)
80 80 v = Version.create!(:project => project, :name => 'Progress')
81 81 add_issue(v)
82 82 add_issue(v, :done_ratio => 20)
83 83 add_issue(v, :status => IssueStatus.find(:first, :conditions => {:is_closed => true}))
84 84 assert_progress_equal (0.0 + 20.0 + 100.0)/3, v.completed_pourcent
85 85 assert_progress_equal (100.0)/3, v.closed_pourcent
86 86 end
87 87
88 88 def test_progress_should_consider_estimated_hours_to_weigth_issues
89 89 project = Project.find(1)
90 90 v = Version.create!(:project => project, :name => 'Progress')
91 91 add_issue(v, :estimated_hours => 10)
92 92 add_issue(v, :estimated_hours => 20, :done_ratio => 30)
93 93 add_issue(v, :estimated_hours => 40, :done_ratio => 10)
94 94 add_issue(v, :estimated_hours => 25, :status => IssueStatus.find(:first, :conditions => {:is_closed => true}))
95 95 assert_progress_equal (10.0*0 + 20.0*0.3 + 40*0.1 + 25.0*1)/95.0*100, v.completed_pourcent
96 96 assert_progress_equal 25.0/95.0*100, v.closed_pourcent
97 97 end
98 98
99 99 def test_progress_should_consider_average_estimated_hours_to_weigth_unestimated_issues
100 100 project = Project.find(1)
101 101 v = Version.create!(:project => project, :name => 'Progress')
102 102 add_issue(v, :done_ratio => 20)
103 103 add_issue(v, :status => IssueStatus.find(:first, :conditions => {:is_closed => true}))
104 104 add_issue(v, :estimated_hours => 10, :done_ratio => 30)
105 105 add_issue(v, :estimated_hours => 40, :done_ratio => 10)
106 106 assert_progress_equal (25.0*0.2 + 25.0*1 + 10.0*0.3 + 40.0*0.1)/100.0*100, v.completed_pourcent
107 107 assert_progress_equal 25.0/100.0*100, v.closed_pourcent
108 108 end
109 109
110 110 context "#behind_schedule?" do
111 111 setup do
112 112 ProjectCustomField.destroy_all # Custom values are a mess to isolate in tests
113 113 @project = Project.create!(:name => 'test0', :identifier => 'test0')
114 114 @project.trackers << Tracker.create!(:name => 'track')
115 115
116 116 @version = Version.create!(:project => @project, :effective_date => nil, :name => 'version')
117 117 end
118 118
119 119 should "be false if there are no issues assigned" do
120 120 @version.update_attribute(:effective_date, Date.yesterday)
121 121 assert_equal false, @version.behind_schedule?
122 122 end
123 123
124 124 should "be false if there is no effective_date" do
125 125 assert_equal false, @version.behind_schedule?
126 126 end
127 127
128 128 should "be false if all of the issues are ahead of schedule" do
129 129 @version.update_attribute(:effective_date, 7.days.from_now.to_date)
130 130 add_issue(@version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
131 131 add_issue(@version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
132 132 assert_equal 60, @version.completed_pourcent
133 133 assert_equal false, @version.behind_schedule?
134 134 end
135 135
136 136 should "be true if any of the issues are behind schedule" do
137 137 @version.update_attribute(:effective_date, 7.days.from_now.to_date)
138 138 add_issue(@version, :start_date => 7.days.ago, :done_ratio => 60) # 14 day span, 60% done, 50% time left
139 139 add_issue(@version, :start_date => 7.days.ago, :done_ratio => 20) # 14 day span, 20% done, 50% time left
140 140 assert_equal 40, @version.completed_pourcent
141 141 assert_equal true, @version.behind_schedule?
142 142 end
143 143
144 144 should "be false if all of the issues are complete" do
145 145 @version.update_attribute(:effective_date, 7.days.from_now.to_date)
146 146 add_issue(@version, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span
147 147 add_issue(@version, :start_date => 14.days.ago, :done_ratio => 100, :status => IssueStatus.find(5)) # 7 day span
148 148 assert_equal 100, @version.completed_pourcent
149 149 assert_equal false, @version.behind_schedule?
150 150 end
151 151 end
152 152
153 153 context "#estimated_hours" do
154 154 setup do
155 155 @version = Version.create!(:project_id => 1, :name => '#estimated_hours')
156 156 end
157 157
158 158 should "return 0 with no assigned issues" do
159 159 assert_equal 0, @version.estimated_hours
160 160 end
161 161
162 162 should "return 0 with no estimated hours" do
163 163 add_issue(@version)
164 164 assert_equal 0, @version.estimated_hours
165 165 end
166 166
167 167 should "return the sum of estimated hours" do
168 168 add_issue(@version, :estimated_hours => 2.5)
169 169 add_issue(@version, :estimated_hours => 5)
170 170 assert_equal 7.5, @version.estimated_hours
171 171 end
172 172
173 173 should "return the sum of leaves estimated hours" do
174 174 parent = add_issue(@version)
175 175 add_issue(@version, :estimated_hours => 2.5, :parent_issue_id => parent.id)
176 176 add_issue(@version, :estimated_hours => 5, :parent_issue_id => parent.id)
177 177 assert_equal 7.5, @version.estimated_hours
178 178 end
179 179 end
180 180
181 181 test "should update all issue's fixed_version associations in case the hierarchy changed XXX" do
182 182 User.current = User.find(1) # Need the admin's permissions
183 183
184 184 @version = Version.find(7)
185 185 # Separate hierarchy
186 186 project_1_issue = Issue.find(1)
187 187 project_1_issue.fixed_version = @version
188 188 assert project_1_issue.save, project_1_issue.errors.full_messages.to_s
189 189
190 190 project_5_issue = Issue.find(6)
191 191 project_5_issue.fixed_version = @version
192 192 assert project_5_issue.save
193 193
194 194 # Project
195 195 project_2_issue = Issue.find(4)
196 196 project_2_issue.fixed_version = @version
197 197 assert project_2_issue.save
198 198
199 199 # Update the sharing
200 200 @version.sharing = 'none'
201 201 assert @version.save
202 202
203 203 # Project 1 now out of the shared scope
204 204 project_1_issue.reload
205 205 assert_equal nil, project_1_issue.fixed_version, "Fixed version is still set after changing the Version's sharing"
206 206
207 207 # Project 5 now out of the shared scope
208 208 project_5_issue.reload
209 209 assert_equal nil, project_5_issue.fixed_version, "Fixed version is still set after changing the Version's sharing"
210 210
211 211 # Project 2 issue remains
212 212 project_2_issue.reload
213 213 assert_equal @version, project_2_issue.fixed_version
214 214 end
215 215
216 216 private
217 217
218 218 def add_issue(version, attributes={})
219 219 Issue.create!({:project => version.project,
220 220 :fixed_version => version,
221 221 :subject => 'Test',
222 222 :author => User.find(:first),
223 223 :tracker => version.project.trackers.find(:first)}.merge(attributes))
224 224 end
225 225
226 226 def assert_progress_equal(expected_float, actual_float, message="")
227 227 assert_in_delta(expected_float, actual_float, 0.000001, message="")
228 228 end
229 229 end
@@ -1,134 +1,134
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 WikiPageTest < ActiveSupport::TestCase
21 21 fixtures :projects, :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions
22 22
23 23 def setup
24 24 @wiki = Wiki.find(1)
25 25 @page = @wiki.pages.first
26 26 end
27 27
28 28 def test_create
29 29 page = WikiPage.new(:wiki => @wiki)
30 30 assert !page.save
31 31 assert_equal 1, page.errors.count
32 32
33 33 page.title = "Page"
34 34 assert page.save
35 35 page.reload
36 36 assert !page.protected?
37 37
38 38 @wiki.reload
39 39 assert @wiki.pages.include?(page)
40 40 end
41 41
42 42 def test_sidebar_should_be_protected_by_default
43 43 page = @wiki.find_or_new_page('sidebar')
44 44 assert page.new_record?
45 45 assert page.protected?
46 46 end
47 47
48 48 def test_find_or_new_page
49 49 page = @wiki.find_or_new_page("CookBook documentation")
50 50 assert_kind_of WikiPage, page
51 51 assert !page.new_record?
52 52
53 53 page = @wiki.find_or_new_page("Non existing page")
54 54 assert_kind_of WikiPage, page
55 55 assert page.new_record?
56 56 end
57 57
58 58 def test_parent_title
59 59 page = WikiPage.find_by_title('Another_page')
60 60 assert_nil page.parent_title
61 61
62 62 page = WikiPage.find_by_title('Page_with_an_inline_image')
63 63 assert_equal 'CookBook documentation', page.parent_title
64 64 end
65 65
66 66 def test_assign_parent
67 67 page = WikiPage.find_by_title('Another_page')
68 68 page.parent_title = 'CookBook documentation'
69 69 assert page.save
70 70 page.reload
71 71 assert_equal WikiPage.find_by_title('CookBook_documentation'), page.parent
72 72 end
73 73
74 74 def test_unassign_parent
75 75 page = WikiPage.find_by_title('Page_with_an_inline_image')
76 76 page.parent_title = ''
77 77 assert page.save
78 78 page.reload
79 79 assert_nil page.parent
80 80 end
81 81
82 82 def test_parent_validation
83 83 page = WikiPage.find_by_title('CookBook_documentation')
84 84
85 85 # A page that doesn't exist
86 86 page.parent_title = 'Unknown title'
87 87 assert !page.save
88 assert_equal I18n.translate('activerecord.errors.messages.invalid'),
89 page.errors[:parent_title].to_s
88 assert_include I18n.translate('activerecord.errors.messages.invalid'),
89 page.errors[:parent_title]
90 90 # A child page
91 91 page.parent_title = 'Page_with_an_inline_image'
92 92 assert !page.save
93 assert_equal I18n.translate('activerecord.errors.messages.circular_dependency'),
94 page.errors[:parent_title].to_s
93 assert_include I18n.translate('activerecord.errors.messages.circular_dependency'),
94 page.errors[:parent_title]
95 95 # The page itself
96 96 page.parent_title = 'CookBook_documentation'
97 97 assert !page.save
98 assert_equal I18n.translate('activerecord.errors.messages.circular_dependency'),
99 page.errors[:parent_title].to_s
98 assert_include I18n.translate('activerecord.errors.messages.circular_dependency'),
99 page.errors[:parent_title]
100 100 page.parent_title = 'Another_page'
101 101 assert page.save
102 102 end
103 103
104 104 def test_destroy
105 105 page = WikiPage.find(1)
106 106 page.destroy
107 107 assert_nil WikiPage.find_by_id(1)
108 108 # make sure that page content and its history are deleted
109 109 assert WikiContent.find_all_by_page_id(1).empty?
110 110 assert WikiContent.versioned_class.find_all_by_page_id(1).empty?
111 111 end
112 112
113 113 def test_destroy_should_not_nullify_children
114 114 page = WikiPage.find(2)
115 115 child_ids = page.child_ids
116 116 assert child_ids.any?
117 117 page.destroy
118 118 assert_nil WikiPage.find_by_id(2)
119 119
120 120 children = WikiPage.find_all_by_id(child_ids)
121 121 assert_equal child_ids.size, children.size
122 122 children.each do |child|
123 123 assert_nil child.parent_id
124 124 end
125 125 end
126 126
127 127 def test_updated_on_eager_load
128 128 page = WikiPage.with_updated_on.first(:order => 'id')
129 129 assert page.is_a?(WikiPage)
130 130 assert_not_nil page.read_attribute(:updated_on)
131 131 assert_equal Time.gm(2007, 3, 6, 23, 10, 51), page.content.updated_on
132 132 assert_equal page.content.updated_on, page.updated_on
133 133 end
134 134 end
General Comments 0
You need to be logged in to leave comments. Login now