1 require 'abstract_unit'
\r
3 class User < ActiveRecord::Base
\r
4 cattr_accessor :current_user
\r
7 class Entry < ActiveRecord::Base
\r
8 belongs_to :created_by, :class_name => "User", :foreign_key => "created_by"
\r
9 belongs_to :updated_by, :class_name => "User", :foreign_key => "updated_by"
\r
12 class Post < ActiveRecord::Base
\r
13 record_userstamps = false
\r
16 class Customer < ActiveRecord::Base
\r
17 cattr_accessor :current_user
\r
20 class UserstampTest < Test::Unit::TestCase
\r
21 fixtures :users, :entries, :posts
\r
24 User.current_user = @first_user
\r
28 User.current_user = nil
\r
29 Entry.user_model_name = :user
\r
32 def test_ar_userstamp_loaded
\r
33 assert ActiveRecord::Base.include?(ActiveRecord::Userstamp)
\r
34 assert_equal @first_user, User.current_user
\r
35 assert_equal User, @first_entry.user_model
\r
36 assert_equal User, @second_entry.user_model
\r
39 def test_column_write_method
\r
40 assert_equal @first_user, User.current_user
\r
41 assert Entry.new.respond_to?("created_by=")
\r
42 assert Entry.new.respond_to?("updated_by=")
\r
44 assert Post.new.respond_to?("created_by=")
\r
45 assert Post.new.respond_to?("updated_by=")
\r
48 def test_column_read_method
\r
49 assert_equal @first_user, User.current_user
\r
50 assert Entry.new.respond_to?("created_by")
\r
51 assert Entry.new.respond_to?("updated_by")
\r
52 assert Post.new.respond_to?("created_by")
\r
53 assert Post.new.respond_to?("updated_by")
\r
57 assert_equal @first_user, User.current_user
\r
58 assert_equal @first_user, @first_entry.created_by
\r
59 assert_equal @second_user, @second_entry.created_by
\r
61 assert_nil @first_post.created_by
\r
62 assert_nil @second_post.created_by
\r
66 assert_equal @first_user, User.current_user
\r
67 assert_equal @first_user, @first_entry.updated_by
\r
68 assert_equal @second_user, @second_entry.updated_by
\r
70 assert_nil @first_post.updated_by
\r
71 assert_nil @second_post.updated_by
\r
75 assert_equal @first_user, User.current_user,
\r
76 third_user = User.create("name" => "Tester Three")
\r
77 User.current_user = third_user
\r
78 assert_equal third_user, User.current_user
\r
80 third_entry = Entry.create("name" => "Third Entry")
\r
82 assert_equal User, third_entry.user_model
\r
83 assert_equal third_user, third_entry.created_by
\r
84 assert_equal third_user, third_entry.updated_by
\r
87 def test_update_entry
\r
88 assert_equal @first_user, User.current_user
\r
89 @second_entry.update_attribute("name", "Updated by First User")
\r
91 User.current_user = @second_user
\r
92 assert_equal @second_user, User.current_user
\r
93 @first_entry.update_attribute("name", "Updated by Second User")
\r
95 assert_equal @first_user, @second_entry.updated_by
\r
96 assert_equal @second_user, @first_entry.updated_by
\r
99 def test_create_and_update_entry
\r
100 assert_equal @first_user, User.current_user
\r
102 third_entry = Entry.create("name" => "Third Entry")
\r
103 User.current_user = @second_user
\r
104 assert_equal @second_user, User.current_user
\r
106 third_entry.update_attribute("name", "Updated by Second User")
\r
107 assert_equal @first_user, third_entry.created_by
\r
108 assert_equal @second_user, third_entry.updated_by
\r
111 def test_update_with_nil_current_user
\r
112 assert_equal @first_user, User.current_user
\r
114 @first_entry.update_attribute("name", "Updated by Nil")
\r
116 assert_equal @first_user, @first_entry.created_by
\r
117 assert_equal @first_user, @first_entry.updated_by
\r
120 def test_create_with_different_user_model
\r
121 assert_equal @first_user, User.current_user
\r
122 Customer.current_user = @first_customer
\r
123 assert_equal @first_customer, Customer.current_user
\r
125 Entry.user_model_name = :customer
\r
126 third_entry = Entry.create("name" => "Customer Created Entry")
\r
129 assert_equal third_entry.user_model, Customer
\r
130 assert_equal third_entry.created_by, @first_customer
\r
131 assert_equal third_entry.updated_by, @first_customer
\r