##// END OF EJS Templates
code layout clean up of test/unit/message_test.rb...
Toshi MARUYAMA -
r7433:d817b063ece4
parent child
Show More
@@ -1,146 +1,150
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', :content => 'Test message content', :author => @user)
32 message = Message.new(:board => @board, :subject => 'Test message',
33 :content => 'Test message content',
34 :author => @user)
33 assert message.save
35 assert message.save
34 @board.reload
36 @board.reload
35 # topics count incremented
37 # topics count incremented
36 assert_equal topics_count+1, @board[:topics_count]
38 assert_equal topics_count+1, @board[:topics_count]
37 # messages count incremented
39 # messages count incremented
38 assert_equal messages_count+1, @board[:messages_count]
40 assert_equal messages_count+1, @board[:messages_count]
39 assert_equal message, @board.last_message
41 assert_equal message, @board.last_message
40 # author should be watching the message
42 # author should be watching the message
41 assert message.watched_by?(@user)
43 assert message.watched_by?(@user)
42 end
44 end
43
45
44 def test_reply
46 def test_reply
45 topics_count = @board.topics_count
47 topics_count = @board.topics_count
46 messages_count = @board.messages_count
48 messages_count = @board.messages_count
47 @message = Message.find(1)
49 @message = Message.find(1)
48 replies_count = @message.replies_count
50 replies_count = @message.replies_count
49
51
50 reply_author = User.find(2)
52 reply_author = User.find(2)
51 reply = Message.new(:board => @board, :subject => 'Test reply', :content => 'Test reply content', :parent => @message, :author => reply_author)
53 reply = Message.new(:board => @board, :subject => 'Test reply',
54 :content => 'Test reply content',
55 :parent => @message, :author => reply_author)
52 assert reply.save
56 assert reply.save
53 @board.reload
57 @board.reload
54 # same topics count
58 # same topics count
55 assert_equal topics_count, @board[:topics_count]
59 assert_equal topics_count, @board[:topics_count]
56 # messages count incremented
60 # messages count incremented
57 assert_equal messages_count+1, @board[:messages_count]
61 assert_equal messages_count+1, @board[:messages_count]
58 assert_equal reply, @board.last_message
62 assert_equal reply, @board.last_message
59 @message.reload
63 @message.reload
60 # replies count incremented
64 # replies count incremented
61 assert_equal replies_count+1, @message[:replies_count]
65 assert_equal replies_count+1, @message[:replies_count]
62 assert_equal reply, @message.last_reply
66 assert_equal reply, @message.last_reply
63 # author should be watching the message
67 # author should be watching the message
64 assert @message.watched_by?(reply_author)
68 assert @message.watched_by?(reply_author)
65 end
69 end
66
70
67 def test_moving_message_should_update_counters
71 def test_moving_message_should_update_counters
68 @message = Message.find(1)
72 @message = Message.find(1)
69 assert_no_difference 'Message.count' do
73 assert_no_difference 'Message.count' do
70 # Previous board
74 # Previous board
71 assert_difference 'Board.find(1).topics_count', -1 do
75 assert_difference 'Board.find(1).topics_count', -1 do
72 assert_difference 'Board.find(1).messages_count', -(1 + @message.replies_count) do
76 assert_difference 'Board.find(1).messages_count', -(1 + @message.replies_count) do
73 # New board
77 # New board
74 assert_difference 'Board.find(2).topics_count' do
78 assert_difference 'Board.find(2).topics_count' do
75 assert_difference 'Board.find(2).messages_count', (1 + @message.replies_count) do
79 assert_difference 'Board.find(2).messages_count', (1 + @message.replies_count) do
76 @message.update_attributes(:board_id => 2)
80 @message.update_attributes(:board_id => 2)
77 end
81 end
78 end
82 end
79 end
83 end
80 end
84 end
81 end
85 end
82 end
86 end
83
87
84 def test_destroy_topic
88 def test_destroy_topic
85 message = Message.find(1)
89 message = Message.find(1)
86 board = message.board
90 board = message.board
87 topics_count, messages_count = board.topics_count, board.messages_count
91 topics_count, messages_count = board.topics_count, board.messages_count
88
92
89 assert_difference('Watcher.count', -1) do
93 assert_difference('Watcher.count', -1) do
90 assert message.destroy
94 assert message.destroy
91 end
95 end
92 board.reload
96 board.reload
93
97
94 # Replies deleted
98 # Replies deleted
95 assert Message.find_all_by_parent_id(1).empty?
99 assert Message.find_all_by_parent_id(1).empty?
96 # Checks counters
100 # Checks counters
97 assert_equal topics_count - 1, board.topics_count
101 assert_equal topics_count - 1, board.topics_count
98 assert_equal messages_count - 3, board.messages_count
102 assert_equal messages_count - 3, board.messages_count
99 # Watchers removed
103 # Watchers removed
100 end
104 end
101
105
102 def test_destroy_reply
106 def test_destroy_reply
103 message = Message.find(5)
107 message = Message.find(5)
104 board = message.board
108 board = message.board
105 topics_count, messages_count = board.topics_count, board.messages_count
109 topics_count, messages_count = board.topics_count, board.messages_count
106 assert message.destroy
110 assert message.destroy
107 board.reload
111 board.reload
108
112
109 # Checks counters
113 # Checks counters
110 assert_equal topics_count, board.topics_count
114 assert_equal topics_count, board.topics_count
111 assert_equal messages_count - 1, board.messages_count
115 assert_equal messages_count - 1, board.messages_count
112 end
116 end
113
117
114 def test_editable_by
118 def test_editable_by
115 message = Message.find(6)
119 message = Message.find(6)
116 author = message.author
120 author = message.author
117 assert message.editable_by?(author)
121 assert message.editable_by?(author)
118
122
119 author.roles_for_project(message.project).first.remove_permission!(:edit_own_messages)
123 author.roles_for_project(message.project).first.remove_permission!(:edit_own_messages)
120 assert !message.reload.editable_by?(author.reload)
124 assert !message.reload.editable_by?(author.reload)
121 end
125 end
122
126
123 def test_destroyable_by
127 def test_destroyable_by
124 message = Message.find(6)
128 message = Message.find(6)
125 author = message.author
129 author = message.author
126 assert message.destroyable_by?(author)
130 assert message.destroyable_by?(author)
127
131
128 author.roles_for_project(message.project).first.remove_permission!(:delete_own_messages)
132 author.roles_for_project(message.project).first.remove_permission!(:delete_own_messages)
129 assert !message.reload.destroyable_by?(author.reload)
133 assert !message.reload.destroyable_by?(author.reload)
130 end
134 end
131
135
132 def test_set_sticky
136 def test_set_sticky
133 message = Message.new
137 message = Message.new
134 assert_equal 0, message.sticky
138 assert_equal 0, message.sticky
135 message.sticky = nil
139 message.sticky = nil
136 assert_equal 0, message.sticky
140 assert_equal 0, message.sticky
137 message.sticky = false
141 message.sticky = false
138 assert_equal 0, message.sticky
142 assert_equal 0, message.sticky
139 message.sticky = true
143 message.sticky = true
140 assert_equal 1, message.sticky
144 assert_equal 1, message.sticky
141 message.sticky = '0'
145 message.sticky = '0'
142 assert_equal 0, message.sticky
146 assert_equal 0, message.sticky
143 message.sticky = '1'
147 message.sticky = '1'
144 assert_equal 1, message.sticky
148 assert_equal 1, message.sticky
145 end
149 end
146 end
150 end
General Comments 0
You need to be logged in to leave comments. Login now