adding the userstamp plugin to the vendor plugins directory
[kwestie.git] / vendor / plugins / userstamp / test / userstamp_test.rb
blob666d256b45a28eecef588eabe89ac2338b0c9a26
1 require 'abstract_unit'\r
2 \r
3 class User < ActiveRecord::Base\r
4   cattr_accessor :current_user\r
5 end\r
6 \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
10 end\r
12 class Post < ActiveRecord::Base\r
13   record_userstamps = false\r
14 end\r
16 class Customer < ActiveRecord::Base\r
17   cattr_accessor :current_user\r
18 end\r
20 class UserstampTest < Test::Unit::TestCase\r
21   fixtures :users, :entries, :posts\r
23   def setup\r
24     User.current_user = @first_user\r
25   end\r
27   def teardown\r
28     User.current_user = nil\r
29     Entry.user_model_name = :user\r
30   end\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
37   end\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
46   end\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
54   end\r
56   def test_created_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
63   end\r
65   def test_updated_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
72   end\r
74   def test_create_new\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
85   end\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
97   end\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
105     \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
109   end\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
118   end\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
124     \r
125     Entry.user_model_name = :customer\r
126     third_entry = Entry.create("name" => "Customer Created Entry")\r
127     third_entry.save\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
132   end\r
134 end\r