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