comparison .svn/pristine/24/24ebd90070f1307a8f6d2f2f35671e06e60bb2b7.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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