##// END OF EJS Templates
add unit message test of cannot reply to a locked topic...
Toshi MARUYAMA -
r7434:8f362558b6ca
parent child
Show More
@@ -1,150 +1,168
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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, :users, :watchers
21 fixtures :projects, :roles, :members, :member_roles, :boards, :messages, :users, :watchers
22
22
23 def setup
23 def setup
24 @board = Board.find(1)
24 @board = Board.find(1)
25 @user = User.find(1)
25 @user = User.find(1)
26 end
26 end
27
27
28 def test_create
28 def test_create
29 topics_count = @board.topics_count
29 topics_count = @board.topics_count
30 messages_count = @board.messages_count
30 messages_count = @board.messages_count
31
31
32 message = Message.new(:board => @board, :subject => 'Test message',
32 message = Message.new(:board => @board, :subject => 'Test message',
33 :content => 'Test message content',
33 :content => 'Test message content',
34 :author => @user)
34 :author => @user)
35 assert message.save
35 assert message.save
36 @board.reload
36 @board.reload
37 # topics count incremented
37 # topics count incremented
38 assert_equal topics_count+1, @board[:topics_count]
38 assert_equal topics_count+1, @board[:topics_count]
39 # messages count incremented
39 # messages count incremented
40 assert_equal messages_count+1, @board[:messages_count]
40 assert_equal messages_count+1, @board[:messages_count]
41 assert_equal message, @board.last_message
41 assert_equal message, @board.last_message
42 # author should be watching the message
42 # author should be watching the message
43 assert message.watched_by?(@user)
43 assert message.watched_by?(@user)
44 end
44 end
45
45
46 def test_reply
46 def test_reply
47 topics_count = @board.topics_count
47 topics_count = @board.topics_count
48 messages_count = @board.messages_count
48 messages_count = @board.messages_count
49 @message = Message.find(1)
49 @message = Message.find(1)
50 replies_count = @message.replies_count
50 replies_count = @message.replies_count
51
51
52 reply_author = User.find(2)
52 reply_author = User.find(2)
53 reply = Message.new(:board => @board, :subject => 'Test reply',
53 reply = Message.new(:board => @board, :subject => 'Test reply',
54 :content => 'Test reply content',
54 :content => 'Test reply content',
55 :parent => @message, :author => reply_author)
55 :parent => @message, :author => reply_author)
56 assert reply.save
56 assert reply.save
57 @board.reload
57 @board.reload
58 # same topics count
58 # same topics count
59 assert_equal topics_count, @board[:topics_count]
59 assert_equal topics_count, @board[:topics_count]
60 # messages count incremented
60 # messages count incremented
61 assert_equal messages_count+1, @board[:messages_count]
61 assert_equal messages_count+1, @board[:messages_count]
62 assert_equal reply, @board.last_message
62 assert_equal reply, @board.last_message
63 @message.reload
63 @message.reload
64 # replies count incremented
64 # replies count incremented
65 assert_equal replies_count+1, @message[:replies_count]
65 assert_equal replies_count+1, @message[:replies_count]
66 assert_equal reply, @message.last_reply
66 assert_equal reply, @message.last_reply
67 # author should be watching the message
67 # author should be watching the message
68 assert @message.watched_by?(reply_author)
68 assert @message.watched_by?(reply_author)
69 end
69 end
70
70
71 def test_cannot_reply_to_locked_topic
72 topics_count = @board.topics_count
73 messages_count = @board.messages_count
74 @message = Message.find(1)
75 replies_count = @message.replies_count
76 assert_equal false, @message.locked
77 @message.locked = true
78 assert @message.save
79 assert_equal true, @message.locked
80
81 reply_author = User.find(2)
82 reply = Message.new(:board => @board, :subject => 'Test reply',
83 :content => 'Test reply content',
84 :parent => @message, :author => reply_author)
85 reply.save
86 assert_equal 1, reply.errors.count
87 end
88
71 def test_moving_message_should_update_counters
89 def test_moving_message_should_update_counters
72 @message = Message.find(1)
90 @message = Message.find(1)
73 assert_no_difference 'Message.count' do
91 assert_no_difference 'Message.count' do
74 # Previous board
92 # Previous board
75 assert_difference 'Board.find(1).topics_count', -1 do
93 assert_difference 'Board.find(1).topics_count', -1 do
76 assert_difference 'Board.find(1).messages_count', -(1 + @message.replies_count) do
94 assert_difference 'Board.find(1).messages_count', -(1 + @message.replies_count) do
77 # New board
95 # New board
78 assert_difference 'Board.find(2).topics_count' do
96 assert_difference 'Board.find(2).topics_count' do
79 assert_difference 'Board.find(2).messages_count', (1 + @message.replies_count) do
97 assert_difference 'Board.find(2).messages_count', (1 + @message.replies_count) do
80 @message.update_attributes(:board_id => 2)
98 @message.update_attributes(:board_id => 2)
81 end
99 end
82 end
100 end
83 end
101 end
84 end
102 end
85 end
103 end
86 end
104 end
87
105
88 def test_destroy_topic
106 def test_destroy_topic
89 message = Message.find(1)
107 message = Message.find(1)
90 board = message.board
108 board = message.board
91 topics_count, messages_count = board.topics_count, board.messages_count
109 topics_count, messages_count = board.topics_count, board.messages_count
92
110
93 assert_difference('Watcher.count', -1) do
111 assert_difference('Watcher.count', -1) do
94 assert message.destroy
112 assert message.destroy
95 end
113 end
96 board.reload
114 board.reload
97
115
98 # Replies deleted
116 # Replies deleted
99 assert Message.find_all_by_parent_id(1).empty?
117 assert Message.find_all_by_parent_id(1).empty?
100 # Checks counters
118 # Checks counters
101 assert_equal topics_count - 1, board.topics_count
119 assert_equal topics_count - 1, board.topics_count
102 assert_equal messages_count - 3, board.messages_count
120 assert_equal messages_count - 3, board.messages_count
103 # Watchers removed
121 # Watchers removed
104 end
122 end
105
123
106 def test_destroy_reply
124 def test_destroy_reply
107 message = Message.find(5)
125 message = Message.find(5)
108 board = message.board
126 board = message.board
109 topics_count, messages_count = board.topics_count, board.messages_count
127 topics_count, messages_count = board.topics_count, board.messages_count
110 assert message.destroy
128 assert message.destroy
111 board.reload
129 board.reload
112
130
113 # Checks counters
131 # Checks counters
114 assert_equal topics_count, board.topics_count
132 assert_equal topics_count, board.topics_count
115 assert_equal messages_count - 1, board.messages_count
133 assert_equal messages_count - 1, board.messages_count
116 end
134 end
117
135
118 def test_editable_by
136 def test_editable_by
119 message = Message.find(6)
137 message = Message.find(6)
120 author = message.author
138 author = message.author
121 assert message.editable_by?(author)
139 assert message.editable_by?(author)
122
140
123 author.roles_for_project(message.project).first.remove_permission!(:edit_own_messages)
141 author.roles_for_project(message.project).first.remove_permission!(:edit_own_messages)
124 assert !message.reload.editable_by?(author.reload)
142 assert !message.reload.editable_by?(author.reload)
125 end
143 end
126
144
127 def test_destroyable_by
145 def test_destroyable_by
128 message = Message.find(6)
146 message = Message.find(6)
129 author = message.author
147 author = message.author
130 assert message.destroyable_by?(author)
148 assert message.destroyable_by?(author)
131
149
132 author.roles_for_project(message.project).first.remove_permission!(:delete_own_messages)
150 author.roles_for_project(message.project).first.remove_permission!(:delete_own_messages)
133 assert !message.reload.destroyable_by?(author.reload)
151 assert !message.reload.destroyable_by?(author.reload)
134 end
152 end
135
153
136 def test_set_sticky
154 def test_set_sticky
137 message = Message.new
155 message = Message.new
138 assert_equal 0, message.sticky
156 assert_equal 0, message.sticky
139 message.sticky = nil
157 message.sticky = nil
140 assert_equal 0, message.sticky
158 assert_equal 0, message.sticky
141 message.sticky = false
159 message.sticky = false
142 assert_equal 0, message.sticky
160 assert_equal 0, message.sticky
143 message.sticky = true
161 message.sticky = true
144 assert_equal 1, message.sticky
162 assert_equal 1, message.sticky
145 message.sticky = '0'
163 message.sticky = '0'
146 assert_equal 0, message.sticky
164 assert_equal 0, message.sticky
147 message.sticky = '1'
165 message.sticky = '1'
148 assert_equal 1, message.sticky
166 assert_equal 1, message.sticky
149 end
167 end
150 end
168 end
General Comments 0
You need to be logged in to leave comments. Login now