##// END OF EJS Templates
Fixed: editing a message may cause sticky attribute to be NULL (#3356)....
Jean-Philippe Lang -
r2687:7642b5a9ab18
parent child
Show More
@@ -0,0 +1,9
1 class FixMessagesStickyNull < ActiveRecord::Migration
2 def self.up
3 Message.update_all('sticky = 0', 'sticky IS NULL')
4 end
5
6 def self.down
7 # nothing to do
8 end
9 end
@@ -1,90 +1,94
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 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 class Message < ActiveRecord::Base
18 class Message < ActiveRecord::Base
19 belongs_to :board
19 belongs_to :board
20 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
20 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
21 acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC"
21 acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC"
22 acts_as_attachable
22 acts_as_attachable
23 belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id'
23 belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id'
24
24
25 acts_as_searchable :columns => ['subject', 'content'],
25 acts_as_searchable :columns => ['subject', 'content'],
26 :include => {:board => :project},
26 :include => {:board => :project},
27 :project_key => 'project_id',
27 :project_key => 'project_id',
28 :date_column => "#{table_name}.created_on"
28 :date_column => "#{table_name}.created_on"
29 acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"},
29 acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"},
30 :description => :content,
30 :description => :content,
31 :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
31 :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
32 :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} :
32 :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} :
33 {:id => o.parent_id, :anchor => "message-#{o.id}"})}
33 {:id => o.parent_id, :anchor => "message-#{o.id}"})}
34
34
35 acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]},
35 acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]},
36 :author_key => :author_id
36 :author_key => :author_id
37 acts_as_watchable
37 acts_as_watchable
38
38
39 attr_protected :locked, :sticky
39 attr_protected :locked, :sticky
40 validates_presence_of :board, :subject, :content
40 validates_presence_of :board, :subject, :content
41 validates_length_of :subject, :maximum => 255
41 validates_length_of :subject, :maximum => 255
42
42
43 after_create :add_author_as_watcher
43 after_create :add_author_as_watcher
44
44
45 def validate_on_create
45 def validate_on_create
46 # Can not reply to a locked topic
46 # Can not reply to a locked topic
47 errors.add_to_base 'Topic is locked' if root.locked? && self != root
47 errors.add_to_base 'Topic is locked' if root.locked? && self != root
48 end
48 end
49
49
50 def after_create
50 def after_create
51 if parent
51 if parent
52 parent.reload.update_attribute(:last_reply_id, self.id)
52 parent.reload.update_attribute(:last_reply_id, self.id)
53 end
53 end
54 board.reset_counters!
54 board.reset_counters!
55 end
55 end
56
56
57 def after_update
57 def after_update
58 if board_id_changed?
58 if board_id_changed?
59 Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id])
59 Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id])
60 Board.reset_counters!(board_id_was)
60 Board.reset_counters!(board_id_was)
61 Board.reset_counters!(board_id)
61 Board.reset_counters!(board_id)
62 end
62 end
63 end
63 end
64
64
65 def after_destroy
65 def after_destroy
66 board.reset_counters!
66 board.reset_counters!
67 end
67 end
68
68
69 def sticky=(arg)
70 write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0)
71 end
72
69 def sticky?
73 def sticky?
70 sticky == 1
74 sticky == 1
71 end
75 end
72
76
73 def project
77 def project
74 board.project
78 board.project
75 end
79 end
76
80
77 def editable_by?(usr)
81 def editable_by?(usr)
78 usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)))
82 usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)))
79 end
83 end
80
84
81 def destroyable_by?(usr)
85 def destroyable_by?(usr)
82 usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project)))
86 usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project)))
83 end
87 end
84
88
85 private
89 private
86
90
87 def add_author_as_watcher
91 def add_author_as_watcher
88 Watcher.create(:watchable => self.root, :user => author)
92 Watcher.create(:watchable => self.root, :user => author)
89 end
93 end
90 end
94 end
@@ -1,131 +1,146
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2009 Jean-Philippe Lang
2 # Copyright (C) 2006-2009 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.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19
19
20 class MessageTest < Test::Unit::TestCase
20 class MessageTest < Test::Unit::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', :content => 'Test message content', :author => @user)
33 assert message.save
33 assert message.save
34 @board.reload
34 @board.reload
35 # topics count incremented
35 # topics count incremented
36 assert_equal topics_count+1, @board[:topics_count]
36 assert_equal topics_count+1, @board[:topics_count]
37 # messages count incremented
37 # messages count incremented
38 assert_equal messages_count+1, @board[:messages_count]
38 assert_equal messages_count+1, @board[:messages_count]
39 assert_equal message, @board.last_message
39 assert_equal message, @board.last_message
40 # author should be watching the message
40 # author should be watching the message
41 assert message.watched_by?(@user)
41 assert message.watched_by?(@user)
42 end
42 end
43
43
44 def test_reply
44 def test_reply
45 topics_count = @board.topics_count
45 topics_count = @board.topics_count
46 messages_count = @board.messages_count
46 messages_count = @board.messages_count
47 @message = Message.find(1)
47 @message = Message.find(1)
48 replies_count = @message.replies_count
48 replies_count = @message.replies_count
49
49
50 reply_author = User.find(2)
50 reply_author = User.find(2)
51 reply = Message.new(:board => @board, :subject => 'Test reply', :content => 'Test reply content', :parent => @message, :author => reply_author)
51 reply = Message.new(:board => @board, :subject => 'Test reply', :content => 'Test reply content', :parent => @message, :author => reply_author)
52 assert reply.save
52 assert reply.save
53 @board.reload
53 @board.reload
54 # same topics count
54 # same topics count
55 assert_equal topics_count, @board[:topics_count]
55 assert_equal topics_count, @board[:topics_count]
56 # messages count incremented
56 # messages count incremented
57 assert_equal messages_count+1, @board[:messages_count]
57 assert_equal messages_count+1, @board[:messages_count]
58 assert_equal reply, @board.last_message
58 assert_equal reply, @board.last_message
59 @message.reload
59 @message.reload
60 # replies count incremented
60 # replies count incremented
61 assert_equal replies_count+1, @message[:replies_count]
61 assert_equal replies_count+1, @message[:replies_count]
62 assert_equal reply, @message.last_reply
62 assert_equal reply, @message.last_reply
63 # author should be watching the message
63 # author should be watching the message
64 assert @message.watched_by?(reply_author)
64 assert @message.watched_by?(reply_author)
65 end
65 end
66
66
67 def test_moving_message_should_update_counters
67 def test_moving_message_should_update_counters
68 @message = Message.find(1)
68 @message = Message.find(1)
69 assert_no_difference 'Message.count' do
69 assert_no_difference 'Message.count' do
70 # Previous board
70 # Previous board
71 assert_difference 'Board.find(1).topics_count', -1 do
71 assert_difference 'Board.find(1).topics_count', -1 do
72 assert_difference 'Board.find(1).messages_count', -(1 + @message.replies_count) do
72 assert_difference 'Board.find(1).messages_count', -(1 + @message.replies_count) do
73 # New board
73 # New board
74 assert_difference 'Board.find(2).topics_count' do
74 assert_difference 'Board.find(2).topics_count' do
75 assert_difference 'Board.find(2).messages_count', (1 + @message.replies_count) do
75 assert_difference 'Board.find(2).messages_count', (1 + @message.replies_count) do
76 @message.update_attributes(:board_id => 2)
76 @message.update_attributes(:board_id => 2)
77 end
77 end
78 end
78 end
79 end
79 end
80 end
80 end
81 end
81 end
82 end
82 end
83
83
84 def test_destroy_topic
84 def test_destroy_topic
85 message = Message.find(1)
85 message = Message.find(1)
86 board = message.board
86 board = message.board
87 topics_count, messages_count = board.topics_count, board.messages_count
87 topics_count, messages_count = board.topics_count, board.messages_count
88
88
89 assert_difference('Watcher.count', -1) do
89 assert_difference('Watcher.count', -1) do
90 assert message.destroy
90 assert message.destroy
91 end
91 end
92 board.reload
92 board.reload
93
93
94 # Replies deleted
94 # Replies deleted
95 assert Message.find_all_by_parent_id(1).empty?
95 assert Message.find_all_by_parent_id(1).empty?
96 # Checks counters
96 # Checks counters
97 assert_equal topics_count - 1, board.topics_count
97 assert_equal topics_count - 1, board.topics_count
98 assert_equal messages_count - 3, board.messages_count
98 assert_equal messages_count - 3, board.messages_count
99 # Watchers removed
99 # Watchers removed
100 end
100 end
101
101
102 def test_destroy_reply
102 def test_destroy_reply
103 message = Message.find(5)
103 message = Message.find(5)
104 board = message.board
104 board = message.board
105 topics_count, messages_count = board.topics_count, board.messages_count
105 topics_count, messages_count = board.topics_count, board.messages_count
106 assert message.destroy
106 assert message.destroy
107 board.reload
107 board.reload
108
108
109 # Checks counters
109 # Checks counters
110 assert_equal topics_count, board.topics_count
110 assert_equal topics_count, board.topics_count
111 assert_equal messages_count - 1, board.messages_count
111 assert_equal messages_count - 1, board.messages_count
112 end
112 end
113
113
114 def test_editable_by
114 def test_editable_by
115 message = Message.find(6)
115 message = Message.find(6)
116 author = message.author
116 author = message.author
117 assert message.editable_by?(author)
117 assert message.editable_by?(author)
118
118
119 author.roles_for_project(message.project).first.remove_permission!(:edit_own_messages)
119 author.roles_for_project(message.project).first.remove_permission!(:edit_own_messages)
120 assert !message.reload.editable_by?(author.reload)
120 assert !message.reload.editable_by?(author.reload)
121 end
121 end
122
122
123 def test_destroyable_by
123 def test_destroyable_by
124 message = Message.find(6)
124 message = Message.find(6)
125 author = message.author
125 author = message.author
126 assert message.destroyable_by?(author)
126 assert message.destroyable_by?(author)
127
127
128 author.roles_for_project(message.project).first.remove_permission!(:delete_own_messages)
128 author.roles_for_project(message.project).first.remove_permission!(:delete_own_messages)
129 assert !message.reload.destroyable_by?(author.reload)
129 assert !message.reload.destroyable_by?(author.reload)
130 end
130 end
131
132 def test_set_sticky
133 message = Message.new
134 assert_equal 0, message.sticky
135 message.sticky = nil
136 assert_equal 0, message.sticky
137 message.sticky = false
138 assert_equal 0, message.sticky
139 message.sticky = true
140 assert_equal 1, message.sticky
141 message.sticky = '0'
142 assert_equal 0, message.sticky
143 message.sticky = '1'
144 assert_equal 1, message.sticky
145 end
131 end
146 end
General Comments 0
You need to be logged in to leave comments. Login now