To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 82 / 822b75d0bb3697ed3dee9071dcf61cf8a7528ac2.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (5.48 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2011  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, :users, :watchers
22

    
23
  def setup
24
    @board = Board.find(1)
25
    @user = User.find(1)
26
  end
27

    
28
  def test_create
29
    topics_count = @board.topics_count
30
    messages_count = @board.messages_count
31

    
32
    message = Message.new(:board => @board, :subject => 'Test message',
33
                          :content => 'Test message content',
34
                          :author => @user)
35
    assert message.save
36
    @board.reload
37
    # topics count incremented
38
    assert_equal topics_count+1, @board[:topics_count]
39
    # messages count incremented
40
    assert_equal messages_count+1, @board[:messages_count]
41
    assert_equal message, @board.last_message
42
    # author should be watching the message
43
    assert message.watched_by?(@user)
44
  end
45

    
46
  def test_reply
47
    topics_count = @board.topics_count
48
    messages_count = @board.messages_count
49
    @message = Message.find(1)
50
    replies_count = @message.replies_count
51

    
52
    reply_author = User.find(2)
53
    reply = Message.new(:board => @board, :subject => 'Test reply',
54
                        :content => 'Test reply content',
55
                        :parent => @message, :author => reply_author)
56
    assert reply.save
57
    @board.reload
58
    # same topics count
59
    assert_equal topics_count, @board[:topics_count]
60
    # messages count incremented
61
    assert_equal messages_count+1, @board[:messages_count]
62
    assert_equal reply, @board.last_message
63
    @message.reload
64
    # replies count incremented
65
    assert_equal replies_count+1, @message[:replies_count]
66
    assert_equal reply, @message.last_reply
67
    # author should be watching the message
68
    assert @message.watched_by?(reply_author)
69
  end
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

    
89
  def test_moving_message_should_update_counters
90
    @message = Message.find(1)
91
    assert_no_difference 'Message.count' do
92
      # Previous board
93
      assert_difference 'Board.find(1).topics_count', -1 do
94
        assert_difference 'Board.find(1).messages_count', -(1 + @message.replies_count) do
95
          # New board
96
          assert_difference 'Board.find(2).topics_count' do
97
            assert_difference 'Board.find(2).messages_count', (1 + @message.replies_count) do
98
              @message.update_attributes(:board_id => 2)
99
            end
100
          end
101
        end
102
      end
103
    end
104
  end
105

    
106
  def test_destroy_topic
107
    message = Message.find(1)
108
    board = message.board
109
    topics_count, messages_count = board.topics_count, board.messages_count
110

    
111
    assert_difference('Watcher.count', -1) do
112
      assert message.destroy
113
    end
114
    board.reload
115

    
116
    # Replies deleted
117
    assert Message.find_all_by_parent_id(1).empty?
118
    # Checks counters
119
    assert_equal topics_count - 1, board.topics_count
120
    assert_equal messages_count - 3, board.messages_count
121
    # Watchers removed
122
  end
123

    
124
  def test_destroy_reply
125
    message = Message.find(5)
126
    board = message.board
127
    topics_count, messages_count = board.topics_count, board.messages_count
128
    assert message.destroy
129
    board.reload
130

    
131
    # Checks counters
132
    assert_equal topics_count, board.topics_count
133
    assert_equal messages_count - 1, board.messages_count
134
  end
135

    
136
  def test_editable_by
137
    message = Message.find(6)
138
    author = message.author
139
    assert message.editable_by?(author)
140

    
141
    author.roles_for_project(message.project).first.remove_permission!(:edit_own_messages)
142
    assert !message.reload.editable_by?(author.reload)
143
  end
144

    
145
  def test_destroyable_by
146
    message = Message.find(6)
147
    author = message.author
148
    assert message.destroyable_by?(author)
149

    
150
    author.roles_for_project(message.project).first.remove_permission!(:delete_own_messages)
151
    assert !message.reload.destroyable_by?(author.reload)
152
  end
153

    
154
  def test_set_sticky
155
    message = Message.new
156
    assert_equal 0, message.sticky
157
    message.sticky = nil
158
    assert_equal 0, message.sticky
159
    message.sticky = false
160
    assert_equal 0, message.sticky
161
    message.sticky = true
162
    assert_equal 1, message.sticky
163
    message.sticky = '0'
164
    assert_equal 0, message.sticky
165
    message.sticky = '1'
166
    assert_equal 1, message.sticky
167
  end
168
end