##// END OF EJS Templates
Fixed: Unable to change locked, sticky flags and board when editing a message (#10564)....
Jean-Philippe Lang -
r9216:a7bacf70fb18
parent child
Show More
@@ -1,111 +1,110
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 class Message < ActiveRecord::Base
18 class Message < ActiveRecord::Base
19 include Redmine::SafeAttributes
19 include Redmine::SafeAttributes
20 belongs_to :board
20 belongs_to :board
21 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
21 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
22 acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC"
22 acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC"
23 acts_as_attachable
23 acts_as_attachable
24 belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id'
24 belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id'
25
25
26 acts_as_searchable :columns => ['subject', 'content'],
26 acts_as_searchable :columns => ['subject', 'content'],
27 :include => {:board => :project},
27 :include => {:board => :project},
28 :project_key => "#{Board.table_name}.project_id",
28 :project_key => "#{Board.table_name}.project_id",
29 :date_column => "#{table_name}.created_on"
29 :date_column => "#{table_name}.created_on"
30 acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"},
30 acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"},
31 :description => :content,
31 :description => :content,
32 :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
32 :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
33 :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} :
33 :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} :
34 {:id => o.parent_id, :r => o.id, :anchor => "message-#{o.id}"})}
34 {:id => o.parent_id, :r => o.id, :anchor => "message-#{o.id}"})}
35
35
36 acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]},
36 acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]},
37 :author_key => :author_id
37 :author_key => :author_id
38 acts_as_watchable
38 acts_as_watchable
39
39
40 attr_protected :locked, :sticky
41 validates_presence_of :board, :subject, :content
40 validates_presence_of :board, :subject, :content
42 validates_length_of :subject, :maximum => 255
41 validates_length_of :subject, :maximum => 255
43 validate :cannot_reply_to_locked_topic, :on => :create
42 validate :cannot_reply_to_locked_topic, :on => :create
44
43
45 after_create :add_author_as_watcher, :update_parent_last_reply
44 after_create :add_author_as_watcher, :update_parent_last_reply
46 after_update :update_messages_board
45 after_update :update_messages_board
47 after_destroy :reset_board_counters
46 after_destroy :reset_board_counters
48
47
49 named_scope :visible, lambda {|*args| { :include => {:board => :project},
48 named_scope :visible, lambda {|*args| { :include => {:board => :project},
50 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
49 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
51
50
52 safe_attributes 'subject', 'content'
51 safe_attributes 'subject', 'content'
53 safe_attributes 'locked', 'sticky',
52 safe_attributes 'locked', 'sticky', 'board_id',
54 :if => lambda {|message, user|
53 :if => lambda {|message, user|
55 user.allowed_to?(:edit_messages, message.project)
54 user.allowed_to?(:edit_messages, message.project)
56 }
55 }
57
56
58 def visible?(user=User.current)
57 def visible?(user=User.current)
59 !user.nil? && user.allowed_to?(:view_messages, project)
58 !user.nil? && user.allowed_to?(:view_messages, project)
60 end
59 end
61
60
62 def cannot_reply_to_locked_topic
61 def cannot_reply_to_locked_topic
63 # Can not reply to a locked topic
62 # Can not reply to a locked topic
64 errors.add :base, 'Topic is locked' if root.locked? && self != root
63 errors.add :base, 'Topic is locked' if root.locked? && self != root
65 end
64 end
66
65
67 def update_parent_last_reply
66 def update_parent_last_reply
68 if parent
67 if parent
69 parent.reload.update_attribute(:last_reply_id, self.id)
68 parent.reload.update_attribute(:last_reply_id, self.id)
70 end
69 end
71 board.reset_counters!
70 board.reset_counters!
72 end
71 end
73
72
74 def update_messages_board
73 def update_messages_board
75 if board_id_changed?
74 if board_id_changed?
76 Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id])
75 Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id])
77 Board.reset_counters!(board_id_was)
76 Board.reset_counters!(board_id_was)
78 Board.reset_counters!(board_id)
77 Board.reset_counters!(board_id)
79 end
78 end
80 end
79 end
81
80
82 def reset_board_counters
81 def reset_board_counters
83 board.reset_counters!
82 board.reset_counters!
84 end
83 end
85
84
86 def sticky=(arg)
85 def sticky=(arg)
87 write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0)
86 write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0)
88 end
87 end
89
88
90 def sticky?
89 def sticky?
91 sticky == 1
90 sticky == 1
92 end
91 end
93
92
94 def project
93 def project
95 board.project
94 board.project
96 end
95 end
97
96
98 def editable_by?(usr)
97 def editable_by?(usr)
99 usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)))
98 usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)))
100 end
99 end
101
100
102 def destroyable_by?(usr)
101 def destroyable_by?(usr)
103 usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project)))
102 usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project)))
104 end
103 end
105
104
106 private
105 private
107
106
108 def add_author_as_watcher
107 def add_author_as_watcher
109 Watcher.create(:watchable => self.root, :user => author)
108 Watcher.create(:watchable => self.root, :user => author)
110 end
109 end
111 end
110 end
@@ -1,185 +1,209
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 require 'messages_controller'
19 require 'messages_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class MessagesController; def rescue_action(e) raise e end; end
22 class MessagesController; def rescue_action(e) raise e end; end
23
23
24 class MessagesControllerTest < ActionController::TestCase
24 class MessagesControllerTest < ActionController::TestCase
25 fixtures :projects, :users, :members, :member_roles, :roles, :boards, :messages, :enabled_modules
25 fixtures :projects, :users, :members, :member_roles, :roles, :boards, :messages, :enabled_modules
26
26
27 def setup
27 def setup
28 @controller = MessagesController.new
28 @controller = MessagesController.new
29 @request = ActionController::TestRequest.new
29 @request = ActionController::TestRequest.new
30 @response = ActionController::TestResponse.new
30 @response = ActionController::TestResponse.new
31 User.current = nil
31 User.current = nil
32 end
32 end
33
33
34 def test_show
34 def test_show
35 get :show, :board_id => 1, :id => 1
35 get :show, :board_id => 1, :id => 1
36 assert_response :success
36 assert_response :success
37 assert_template 'show'
37 assert_template 'show'
38 assert_not_nil assigns(:board)
38 assert_not_nil assigns(:board)
39 assert_not_nil assigns(:project)
39 assert_not_nil assigns(:project)
40 assert_not_nil assigns(:topic)
40 assert_not_nil assigns(:topic)
41 end
41 end
42
42
43 def test_show_should_contain_reply_field_tags_for_quoting
43 def test_show_should_contain_reply_field_tags_for_quoting
44 @request.session[:user_id] = 2
44 @request.session[:user_id] = 2
45 get :show, :board_id => 1, :id => 1
45 get :show, :board_id => 1, :id => 1
46 assert_response :success
46 assert_response :success
47
47
48 # tags required by MessagesController#quote
48 # tags required by MessagesController#quote
49 assert_tag 'input', :attributes => {:id => 'message_subject'}
49 assert_tag 'input', :attributes => {:id => 'message_subject'}
50 assert_tag 'textarea', :attributes => {:id => 'message_content'}
50 assert_tag 'textarea', :attributes => {:id => 'message_content'}
51 assert_tag 'div', :attributes => {:id => 'reply'}
51 assert_tag 'div', :attributes => {:id => 'reply'}
52 end
52 end
53
53
54 def test_show_with_pagination
54 def test_show_with_pagination
55 message = Message.find(1)
55 message = Message.find(1)
56 assert_difference 'Message.count', 30 do
56 assert_difference 'Message.count', 30 do
57 30.times do
57 30.times do
58 message.children << Message.new(:subject => 'Reply', :content => 'Reply body', :author_id => 2, :board_id => 1)
58 message.children << Message.new(:subject => 'Reply', :content => 'Reply body', :author_id => 2, :board_id => 1)
59 end
59 end
60 end
60 end
61 get :show, :board_id => 1, :id => 1, :r => message.children.last(:order => 'id').id
61 get :show, :board_id => 1, :id => 1, :r => message.children.last(:order => 'id').id
62 assert_response :success
62 assert_response :success
63 assert_template 'show'
63 assert_template 'show'
64 replies = assigns(:replies)
64 replies = assigns(:replies)
65 assert_not_nil replies
65 assert_not_nil replies
66 assert !replies.include?(message.children.first(:order => 'id'))
66 assert !replies.include?(message.children.first(:order => 'id'))
67 assert replies.include?(message.children.last(:order => 'id'))
67 assert replies.include?(message.children.last(:order => 'id'))
68 end
68 end
69
69
70 def test_show_with_reply_permission
70 def test_show_with_reply_permission
71 @request.session[:user_id] = 2
71 @request.session[:user_id] = 2
72 get :show, :board_id => 1, :id => 1
72 get :show, :board_id => 1, :id => 1
73 assert_response :success
73 assert_response :success
74 assert_template 'show'
74 assert_template 'show'
75 assert_tag :div, :attributes => { :id => 'reply' },
75 assert_tag :div, :attributes => { :id => 'reply' },
76 :descendant => { :tag => 'textarea', :attributes => { :id => 'message_content' } }
76 :descendant => { :tag => 'textarea', :attributes => { :id => 'message_content' } }
77 end
77 end
78
78
79 def test_show_message_not_found
79 def test_show_message_not_found
80 get :show, :board_id => 1, :id => 99999
80 get :show, :board_id => 1, :id => 99999
81 assert_response 404
81 assert_response 404
82 end
82 end
83
83
84 def test_get_new
84 def test_get_new
85 @request.session[:user_id] = 2
85 @request.session[:user_id] = 2
86 get :new, :board_id => 1
86 get :new, :board_id => 1
87 assert_response :success
87 assert_response :success
88 assert_template 'new'
88 assert_template 'new'
89 end
89 end
90
90
91 def test_post_new
91 def test_post_new
92 @request.session[:user_id] = 2
92 @request.session[:user_id] = 2
93 ActionMailer::Base.deliveries.clear
93 ActionMailer::Base.deliveries.clear
94 Setting.notified_events = ['message_posted']
94 Setting.notified_events = ['message_posted']
95
95
96 post :new, :board_id => 1,
96 post :new, :board_id => 1,
97 :message => { :subject => 'Test created message',
97 :message => { :subject => 'Test created message',
98 :content => 'Message body'}
98 :content => 'Message body'}
99 message = Message.find_by_subject('Test created message')
99 message = Message.find_by_subject('Test created message')
100 assert_not_nil message
100 assert_not_nil message
101 assert_redirected_to "/boards/1/topics/#{message.to_param}"
101 assert_redirected_to "/boards/1/topics/#{message.to_param}"
102 assert_equal 'Message body', message.content
102 assert_equal 'Message body', message.content
103 assert_equal 2, message.author_id
103 assert_equal 2, message.author_id
104 assert_equal 1, message.board_id
104 assert_equal 1, message.board_id
105
105
106 mail = ActionMailer::Base.deliveries.last
106 mail = ActionMailer::Base.deliveries.last
107 assert_not_nil mail
107 assert_not_nil mail
108 assert_equal "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] Test created message", mail.subject
108 assert_equal "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] Test created message", mail.subject
109 assert_mail_body_match 'Message body', mail
109 assert_mail_body_match 'Message body', mail
110 # author
110 # author
111 assert mail.bcc.include?('jsmith@somenet.foo')
111 assert mail.bcc.include?('jsmith@somenet.foo')
112 # project member
112 # project member
113 assert mail.bcc.include?('dlopper@somenet.foo')
113 assert mail.bcc.include?('dlopper@somenet.foo')
114 end
114 end
115
115
116 def test_get_edit
116 def test_get_edit
117 @request.session[:user_id] = 2
117 @request.session[:user_id] = 2
118 get :edit, :board_id => 1, :id => 1
118 get :edit, :board_id => 1, :id => 1
119 assert_response :success
119 assert_response :success
120 assert_template 'edit'
120 assert_template 'edit'
121 end
121 end
122
122
123 def test_post_edit
123 def test_post_edit
124 @request.session[:user_id] = 2
124 @request.session[:user_id] = 2
125 post :edit, :board_id => 1, :id => 1,
125 post :edit, :board_id => 1, :id => 1,
126 :message => { :subject => 'New subject',
126 :message => { :subject => 'New subject',
127 :content => 'New body'}
127 :content => 'New body'}
128 assert_redirected_to '/boards/1/topics/1'
128 assert_redirected_to '/boards/1/topics/1'
129 message = Message.find(1)
129 message = Message.find(1)
130 assert_equal 'New subject', message.subject
130 assert_equal 'New subject', message.subject
131 assert_equal 'New body', message.content
131 assert_equal 'New body', message.content
132 end
132 end
133
133
134 def test_post_edit_sticky_and_locked
135 @request.session[:user_id] = 2
136 post :edit, :board_id => 1, :id => 1,
137 :message => { :subject => 'New subject',
138 :content => 'New body',
139 :locked => '1',
140 :sticky => '1'}
141 assert_redirected_to '/boards/1/topics/1'
142 message = Message.find(1)
143 assert_equal true, message.sticky?
144 assert_equal true, message.locked?
145 end
146
147 def test_post_edit_should_allow_to_change_board
148 @request.session[:user_id] = 2
149 post :edit, :board_id => 1, :id => 1,
150 :message => { :subject => 'New subject',
151 :content => 'New body',
152 :board_id => 2}
153 assert_redirected_to '/boards/2/topics/1'
154 message = Message.find(1)
155 assert_equal Board.find(2), message.board
156 end
157
134 def test_reply
158 def test_reply
135 @request.session[:user_id] = 2
159 @request.session[:user_id] = 2
136 post :reply, :board_id => 1, :id => 1, :reply => { :content => 'This is a test reply', :subject => 'Test reply' }
160 post :reply, :board_id => 1, :id => 1, :reply => { :content => 'This is a test reply', :subject => 'Test reply' }
137 reply = Message.find(:first, :order => 'id DESC')
161 reply = Message.find(:first, :order => 'id DESC')
138 assert_redirected_to "/boards/1/topics/1?r=#{reply.id}"
162 assert_redirected_to "/boards/1/topics/1?r=#{reply.id}"
139 assert Message.find_by_subject('Test reply')
163 assert Message.find_by_subject('Test reply')
140 end
164 end
141
165
142 def test_destroy_topic
166 def test_destroy_topic
143 @request.session[:user_id] = 2
167 @request.session[:user_id] = 2
144 assert_difference 'Message.count', -3 do
168 assert_difference 'Message.count', -3 do
145 post :destroy, :board_id => 1, :id => 1
169 post :destroy, :board_id => 1, :id => 1
146 end
170 end
147 assert_redirected_to '/projects/ecookbook/boards/1'
171 assert_redirected_to '/projects/ecookbook/boards/1'
148 assert_nil Message.find_by_id(1)
172 assert_nil Message.find_by_id(1)
149 end
173 end
150
174
151 def test_destroy_reply
175 def test_destroy_reply
152 @request.session[:user_id] = 2
176 @request.session[:user_id] = 2
153 assert_difference 'Message.count', -1 do
177 assert_difference 'Message.count', -1 do
154 post :destroy, :board_id => 1, :id => 2
178 post :destroy, :board_id => 1, :id => 2
155 end
179 end
156 assert_redirected_to '/boards/1/topics/1?r=2'
180 assert_redirected_to '/boards/1/topics/1?r=2'
157 assert_nil Message.find_by_id(2)
181 assert_nil Message.find_by_id(2)
158 end
182 end
159
183
160 def test_quote
184 def test_quote
161 @request.session[:user_id] = 2
185 @request.session[:user_id] = 2
162 xhr :get, :quote, :board_id => 1, :id => 3
186 xhr :get, :quote, :board_id => 1, :id => 3
163 assert_response :success
187 assert_response :success
164 assert_select_rjs :show, 'reply'
188 assert_select_rjs :show, 'reply'
165 end
189 end
166
190
167 def test_preview_new
191 def test_preview_new
168 @request.session[:user_id] = 2
192 @request.session[:user_id] = 2
169 post :preview,
193 post :preview,
170 :board_id => 1,
194 :board_id => 1,
171 :message => {:subject => "", :content => "Previewed text"}
195 :message => {:subject => "", :content => "Previewed text"}
172 assert_response :success
196 assert_response :success
173 assert_template 'common/_preview'
197 assert_template 'common/_preview'
174 end
198 end
175
199
176 def test_preview_edit
200 def test_preview_edit
177 @request.session[:user_id] = 2
201 @request.session[:user_id] = 2
178 post :preview,
202 post :preview,
179 :id => 4,
203 :id => 4,
180 :board_id => 1,
204 :board_id => 1,
181 :message => {:subject => "", :content => "Previewed text"}
205 :message => {:subject => "", :content => "Previewed text"}
182 assert_response :success
206 assert_response :success
183 assert_template 'common/_preview'
207 assert_template 'common/_preview'
184 end
208 end
185 end
209 end
General Comments 0
You need to be logged in to leave comments. Login now