##// END OF EJS Templates
not use unnecessary instance variable at unit message test...
Toshi MARUYAMA -
r10969:10c96dba8b79
parent child
Show More
@@ -1,184 +1,184
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class MessageTest < ActiveSupport::TestCase
20 class MessageTest < ActiveSupport::TestCase
21 fixtures :projects, :roles, :members, :member_roles, :boards, :messages,
21 fixtures :projects, :roles, :members, :member_roles, :boards, :messages,
22 :users, :watchers, :enabled_modules
22 :users, :watchers, :enabled_modules
23
23
24 def setup
24 def setup
25 @board = Board.find(1)
25 @board = Board.find(1)
26 @user = User.find(1)
26 @user = User.find(1)
27 end
27 end
28
28
29 def test_create
29 def test_create
30 topics_count = @board.topics_count
30 topics_count = @board.topics_count
31 messages_count = @board.messages_count
31 messages_count = @board.messages_count
32
32
33 message = Message.new(:board => @board, :subject => 'Test message',
33 message = Message.new(:board => @board, :subject => 'Test message',
34 :content => 'Test message content',
34 :content => 'Test message content',
35 :author => @user)
35 :author => @user)
36 assert message.save
36 assert message.save
37 @board.reload
37 @board.reload
38 # topics count incremented
38 # topics count incremented
39 assert_equal topics_count + 1, @board[:topics_count]
39 assert_equal topics_count + 1, @board[:topics_count]
40 # messages count incremented
40 # messages count incremented
41 assert_equal messages_count + 1, @board[:messages_count]
41 assert_equal messages_count + 1, @board[:messages_count]
42 assert_equal message, @board.last_message
42 assert_equal message, @board.last_message
43 # author should be watching the message
43 # author should be watching the message
44 assert message.watched_by?(@user)
44 assert message.watched_by?(@user)
45 end
45 end
46
46
47 def test_reply
47 def test_reply
48 topics_count = @board.topics_count
48 topics_count = @board.topics_count
49 messages_count = @board.messages_count
49 messages_count = @board.messages_count
50 @message = Message.find(1)
50 message = Message.find(1)
51 replies_count = @message.replies_count
51 replies_count = message.replies_count
52
52
53 reply_author = User.find(2)
53 reply_author = User.find(2)
54 reply = Message.new(:board => @board, :subject => 'Test reply',
54 reply = Message.new(:board => @board, :subject => 'Test reply',
55 :content => 'Test reply content',
55 :content => 'Test reply content',
56 :parent => @message, :author => reply_author)
56 :parent => message, :author => reply_author)
57 assert reply.save
57 assert reply.save
58 @board.reload
58 @board.reload
59 # same topics count
59 # same topics count
60 assert_equal topics_count, @board[:topics_count]
60 assert_equal topics_count, @board[:topics_count]
61 # messages count incremented
61 # messages count incremented
62 assert_equal messages_count+1, @board[:messages_count]
62 assert_equal messages_count+1, @board[:messages_count]
63 assert_equal reply, @board.last_message
63 assert_equal reply, @board.last_message
64 @message.reload
64 message.reload
65 # replies count incremented
65 # replies count incremented
66 assert_equal replies_count+1, @message[:replies_count]
66 assert_equal replies_count+1, message[:replies_count]
67 assert_equal reply, @message.last_reply
67 assert_equal reply, message.last_reply
68 # author should be watching the message
68 # author should be watching the message
69 assert @message.watched_by?(reply_author)
69 assert message.watched_by?(reply_author)
70 end
70 end
71
71
72 def test_cannot_reply_to_locked_topic
72 def test_cannot_reply_to_locked_topic
73 topics_count = @board.topics_count
73 topics_count = @board.topics_count
74 messages_count = @board.messages_count
74 messages_count = @board.messages_count
75 @message = Message.find(1)
75 message = Message.find(1)
76 replies_count = @message.replies_count
76 replies_count = message.replies_count
77 assert_equal false, @message.locked
77 assert_equal false, message.locked
78 @message.locked = true
78 message.locked = true
79 assert @message.save
79 assert message.save
80 assert_equal true, @message.locked
80 assert_equal true, message.locked
81
81
82 reply_author = User.find(2)
82 reply_author = User.find(2)
83 reply = Message.new(:board => @board, :subject => 'Test reply',
83 reply = Message.new(:board => @board, :subject => 'Test reply',
84 :content => 'Test reply content',
84 :content => 'Test reply content',
85 :parent => @message, :author => reply_author)
85 :parent => message, :author => reply_author)
86 reply.save
86 reply.save
87 assert_equal 1, reply.errors.count
87 assert_equal 1, reply.errors.count
88 end
88 end
89
89
90 def test_moving_message_should_update_counters
90 def test_moving_message_should_update_counters
91 @message = Message.find(1)
91 message = Message.find(1)
92 assert_no_difference 'Message.count' do
92 assert_no_difference 'Message.count' do
93 # Previous board
93 # Previous board
94 assert_difference 'Board.find(1).topics_count', -1 do
94 assert_difference 'Board.find(1).topics_count', -1 do
95 assert_difference 'Board.find(1).messages_count', -(1 + @message.replies_count) do
95 assert_difference 'Board.find(1).messages_count', -(1 + message.replies_count) do
96 # New board
96 # New board
97 assert_difference 'Board.find(2).topics_count' do
97 assert_difference 'Board.find(2).topics_count' do
98 assert_difference 'Board.find(2).messages_count', (1 + @message.replies_count) do
98 assert_difference 'Board.find(2).messages_count', (1 + message.replies_count) do
99 @message.update_attributes(:board_id => 2)
99 message.update_attributes(:board_id => 2)
100 end
100 end
101 end
101 end
102 end
102 end
103 end
103 end
104 end
104 end
105 end
105 end
106
106
107 def test_destroy_topic
107 def test_destroy_topic
108 message = Message.find(1)
108 message = Message.find(1)
109 board = message.board
109 board = message.board
110 topics_count, messages_count = board.topics_count, board.messages_count
110 topics_count, messages_count = board.topics_count, board.messages_count
111
111
112 assert_difference('Watcher.count', -1) do
112 assert_difference('Watcher.count', -1) do
113 assert message.destroy
113 assert message.destroy
114 end
114 end
115 board.reload
115 board.reload
116
116
117 # Replies deleted
117 # Replies deleted
118 assert Message.find_all_by_parent_id(1).empty?
118 assert Message.find_all_by_parent_id(1).empty?
119 # Checks counters
119 # Checks counters
120 assert_equal topics_count - 1, board.topics_count
120 assert_equal topics_count - 1, board.topics_count
121 assert_equal messages_count - 3, board.messages_count
121 assert_equal messages_count - 3, board.messages_count
122 # Watchers removed
122 # Watchers removed
123 end
123 end
124
124
125 def test_destroy_reply
125 def test_destroy_reply
126 message = Message.find(5)
126 message = Message.find(5)
127 board = message.board
127 board = message.board
128 topics_count, messages_count = board.topics_count, board.messages_count
128 topics_count, messages_count = board.topics_count, board.messages_count
129 assert message.destroy
129 assert message.destroy
130 board.reload
130 board.reload
131
131
132 # Checks counters
132 # Checks counters
133 assert_equal topics_count, board.topics_count
133 assert_equal topics_count, board.topics_count
134 assert_equal messages_count - 1, board.messages_count
134 assert_equal messages_count - 1, board.messages_count
135 end
135 end
136
136
137 def test_destroying_last_reply_should_update_topic_last_reply_id
137 def test_destroying_last_reply_should_update_topic_last_reply_id
138 topic = Message.find(4)
138 topic = Message.find(4)
139 assert_equal 6, topic.last_reply_id
139 assert_equal 6, topic.last_reply_id
140
140
141 assert_difference 'Message.count', -1 do
141 assert_difference 'Message.count', -1 do
142 Message.find(6).destroy
142 Message.find(6).destroy
143 end
143 end
144 assert_equal 5, topic.reload.last_reply_id
144 assert_equal 5, topic.reload.last_reply_id
145
145
146 assert_difference 'Message.count', -1 do
146 assert_difference 'Message.count', -1 do
147 Message.find(5).destroy
147 Message.find(5).destroy
148 end
148 end
149 assert_nil topic.reload.last_reply_id
149 assert_nil topic.reload.last_reply_id
150 end
150 end
151
151
152 def test_editable_by
152 def test_editable_by
153 message = Message.find(6)
153 message = Message.find(6)
154 author = message.author
154 author = message.author
155 assert message.editable_by?(author)
155 assert message.editable_by?(author)
156
156
157 author.roles_for_project(message.project).first.remove_permission!(:edit_own_messages)
157 author.roles_for_project(message.project).first.remove_permission!(:edit_own_messages)
158 assert !message.reload.editable_by?(author.reload)
158 assert !message.reload.editable_by?(author.reload)
159 end
159 end
160
160
161 def test_destroyable_by
161 def test_destroyable_by
162 message = Message.find(6)
162 message = Message.find(6)
163 author = message.author
163 author = message.author
164 assert message.destroyable_by?(author)
164 assert message.destroyable_by?(author)
165
165
166 author.roles_for_project(message.project).first.remove_permission!(:delete_own_messages)
166 author.roles_for_project(message.project).first.remove_permission!(:delete_own_messages)
167 assert !message.reload.destroyable_by?(author.reload)
167 assert !message.reload.destroyable_by?(author.reload)
168 end
168 end
169
169
170 def test_set_sticky
170 def test_set_sticky
171 message = Message.new
171 message = Message.new
172 assert_equal 0, message.sticky
172 assert_equal 0, message.sticky
173 message.sticky = nil
173 message.sticky = nil
174 assert_equal 0, message.sticky
174 assert_equal 0, message.sticky
175 message.sticky = false
175 message.sticky = false
176 assert_equal 0, message.sticky
176 assert_equal 0, message.sticky
177 message.sticky = true
177 message.sticky = true
178 assert_equal 1, message.sticky
178 assert_equal 1, message.sticky
179 message.sticky = '0'
179 message.sticky = '0'
180 assert_equal 0, message.sticky
180 assert_equal 0, message.sticky
181 message.sticky = '1'
181 message.sticky = '1'
182 assert_equal 1, message.sticky
182 assert_equal 1, message.sticky
183 end
183 end
184 end
184 end
General Comments 0
You need to be logged in to leave comments. Login now