##// END OF EJS Templates
Merged r11446 from trunk (#13232)....
Jean-Philippe Lang -
r11366:c2eb28b9015c
parent child
Show More
@@ -1,139 +1,140
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class MessagesController < ApplicationController
19 19 menu_item :boards
20 20 default_search_scope :messages
21 21 before_filter :find_board, :only => [:new, :preview]
22 22 before_filter :find_message, :except => [:new, :preview]
23 23 before_filter :authorize, :except => [:preview, :edit, :destroy]
24 24
25 25 helper :boards
26 26 helper :watchers
27 27 helper :attachments
28 28 include AttachmentsHelper
29 29
30 30 REPLIES_PER_PAGE = 25 unless const_defined?(:REPLIES_PER_PAGE)
31 31
32 32 # Show a topic and its replies
33 33 def show
34 34 page = params[:page]
35 35 # Find the page of the requested reply
36 36 if params[:r] && page.nil?
37 37 offset = @topic.children.count(:conditions => ["#{Message.table_name}.id < ?", params[:r].to_i])
38 38 page = 1 + offset / REPLIES_PER_PAGE
39 39 end
40 40
41 41 @reply_count = @topic.children.count
42 42 @reply_pages = Paginator.new self, @reply_count, REPLIES_PER_PAGE, page
43 43 @replies = @topic.children.find(:all, :include => [:author, :attachments, {:board => :project}],
44 44 :order => "#{Message.table_name}.created_on ASC",
45 45 :limit => @reply_pages.items_per_page,
46 46 :offset => @reply_pages.current.offset)
47 47
48 48 @reply = Message.new(:subject => "RE: #{@message.subject}")
49 49 render :action => "show", :layout => false if request.xhr?
50 50 end
51 51
52 52 # Create a new topic
53 53 def new
54 54 @message = Message.new
55 55 @message.author = User.current
56 56 @message.board = @board
57 57 @message.safe_attributes = params[:message]
58 58 if request.post?
59 59 @message.save_attachments(params[:attachments])
60 60 if @message.save
61 61 call_hook(:controller_messages_new_after_save, { :params => params, :message => @message})
62 62 render_attachment_warning_if_needed(@message)
63 63 redirect_to board_message_path(@board, @message)
64 64 end
65 65 end
66 66 end
67 67
68 68 # Reply to a topic
69 69 def reply
70 70 @reply = Message.new
71 71 @reply.author = User.current
72 72 @reply.board = @board
73 73 @reply.safe_attributes = params[:reply]
74 74 @topic.children << @reply
75 75 if !@reply.new_record?
76 76 call_hook(:controller_messages_reply_after_save, { :params => params, :message => @reply})
77 77 attachments = Attachment.attach_files(@reply, params[:attachments])
78 78 render_attachment_warning_if_needed(@reply)
79 79 end
80 80 redirect_to board_message_path(@board, @topic, :r => @reply)
81 81 end
82 82
83 83 # Edit a message
84 84 def edit
85 85 (render_403; return false) unless @message.editable_by?(User.current)
86 86 @message.safe_attributes = params[:message]
87 87 if request.post? && @message.save
88 88 attachments = Attachment.attach_files(@message, params[:attachments])
89 89 render_attachment_warning_if_needed(@message)
90 90 flash[:notice] = l(:notice_successful_update)
91 91 @message.reload
92 92 redirect_to board_message_path(@message.board, @message.root, :r => (@message.parent_id && @message.id))
93 93 end
94 94 end
95 95
96 96 # Delete a messages
97 97 def destroy
98 98 (render_403; return false) unless @message.destroyable_by?(User.current)
99 99 r = @message.to_param
100 100 @message.destroy
101 101 if @message.parent
102 102 redirect_to board_message_path(@board, @message.parent, :r => r)
103 103 else
104 104 redirect_to project_board_path(@project, @board)
105 105 end
106 106 end
107 107
108 108 def quote
109 109 @subject = @message.subject
110 110 @subject = "RE: #{@subject}" unless @subject.starts_with?('RE:')
111 111
112 112 @content = "#{ll(Setting.default_language, :text_user_wrote, @message.author)}\n> "
113 113 @content << @message.content.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub(/(\r?\n|\r\n?)/, "\n> ") + "\n\n"
114 114 end
115 115
116 116 def preview
117 117 message = @board.messages.find_by_id(params[:id])
118 118 @attachements = message.attachments if message
119 119 @text = (params[:message] || params[:reply])[:content]
120 120 @previewed = message
121 121 render :partial => 'common/preview'
122 122 end
123 123
124 124 private
125 125 def find_message
126 find_board
126 return unless find_board
127 127 @message = @board.messages.find(params[:id], :include => :parent)
128 128 @topic = @message.root
129 129 rescue ActiveRecord::RecordNotFound
130 130 render_404
131 131 end
132 132
133 133 def find_board
134 134 @board = Board.find(params[:board_id], :include => :project)
135 135 @project = @board.project
136 136 rescue ActiveRecord::RecordNotFound
137 137 render_404
138 nil
138 139 end
139 140 end
@@ -1,213 +1,218
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19 require 'messages_controller'
20 20
21 21 # Re-raise errors caught by the controller.
22 22 class MessagesController; def rescue_action(e) raise e end; end
23 23
24 24 class MessagesControllerTest < ActionController::TestCase
25 25 fixtures :projects, :users, :members, :member_roles, :roles, :boards, :messages, :enabled_modules
26 26
27 27 def setup
28 28 @controller = MessagesController.new
29 29 @request = ActionController::TestRequest.new
30 30 @response = ActionController::TestResponse.new
31 31 User.current = nil
32 32 end
33 33
34 34 def test_show
35 35 get :show, :board_id => 1, :id => 1
36 36 assert_response :success
37 37 assert_template 'show'
38 38 assert_not_nil assigns(:board)
39 39 assert_not_nil assigns(:project)
40 40 assert_not_nil assigns(:topic)
41 41 end
42 42
43 43 def test_show_should_contain_reply_field_tags_for_quoting
44 44 @request.session[:user_id] = 2
45 45 get :show, :board_id => 1, :id => 1
46 46 assert_response :success
47 47
48 48 # tags required by MessagesController#quote
49 49 assert_tag 'input', :attributes => {:id => 'message_subject'}
50 50 assert_tag 'textarea', :attributes => {:id => 'message_content'}
51 51 assert_tag 'div', :attributes => {:id => 'reply'}
52 52 end
53 53
54 54 def test_show_with_pagination
55 55 message = Message.find(1)
56 56 assert_difference 'Message.count', 30 do
57 57 30.times do
58 58 message.children << Message.new(:subject => 'Reply', :content => 'Reply body', :author_id => 2, :board_id => 1)
59 59 end
60 60 end
61 61 get :show, :board_id => 1, :id => 1, :r => message.children.last(:order => 'id').id
62 62 assert_response :success
63 63 assert_template 'show'
64 64 replies = assigns(:replies)
65 65 assert_not_nil replies
66 66 assert !replies.include?(message.children.first(:order => 'id'))
67 67 assert replies.include?(message.children.last(:order => 'id'))
68 68 end
69 69
70 70 def test_show_with_reply_permission
71 71 @request.session[:user_id] = 2
72 72 get :show, :board_id => 1, :id => 1
73 73 assert_response :success
74 74 assert_template 'show'
75 75 assert_tag :div, :attributes => { :id => 'reply' },
76 76 :descendant => { :tag => 'textarea', :attributes => { :id => 'message_content' } }
77 77 end
78 78
79 79 def test_show_message_not_found
80 80 get :show, :board_id => 1, :id => 99999
81 81 assert_response 404
82 82 end
83 83
84 def test_show_message_from_invalid_board_should_respond_with_404
85 get :show, :board_id => 999, :id => 1
86 assert_response 404
87 end
88
84 89 def test_get_new
85 90 @request.session[:user_id] = 2
86 91 get :new, :board_id => 1
87 92 assert_response :success
88 93 assert_template 'new'
89 94 end
90 95
91 96 def test_post_new
92 97 @request.session[:user_id] = 2
93 98 ActionMailer::Base.deliveries.clear
94 99
95 100 with_settings :notified_events => %w(message_posted) do
96 101 post :new, :board_id => 1,
97 102 :message => { :subject => 'Test created message',
98 103 :content => 'Message body'}
99 104 end
100 105 message = Message.find_by_subject('Test created message')
101 106 assert_not_nil message
102 107 assert_redirected_to "/boards/1/topics/#{message.to_param}"
103 108 assert_equal 'Message body', message.content
104 109 assert_equal 2, message.author_id
105 110 assert_equal 1, message.board_id
106 111
107 112 mail = ActionMailer::Base.deliveries.last
108 113 assert_not_nil mail
109 114 assert_equal "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] Test created message", mail.subject
110 115 assert_mail_body_match 'Message body', mail
111 116 # author
112 117 assert mail.bcc.include?('jsmith@somenet.foo')
113 118 # project member
114 119 assert mail.bcc.include?('dlopper@somenet.foo')
115 120 end
116 121
117 122 def test_get_edit
118 123 @request.session[:user_id] = 2
119 124 get :edit, :board_id => 1, :id => 1
120 125 assert_response :success
121 126 assert_template 'edit'
122 127 end
123 128
124 129 def test_post_edit
125 130 @request.session[:user_id] = 2
126 131 post :edit, :board_id => 1, :id => 1,
127 132 :message => { :subject => 'New subject',
128 133 :content => 'New body'}
129 134 assert_redirected_to '/boards/1/topics/1'
130 135 message = Message.find(1)
131 136 assert_equal 'New subject', message.subject
132 137 assert_equal 'New body', message.content
133 138 end
134 139
135 140 def test_post_edit_sticky_and_locked
136 141 @request.session[:user_id] = 2
137 142 post :edit, :board_id => 1, :id => 1,
138 143 :message => { :subject => 'New subject',
139 144 :content => 'New body',
140 145 :locked => '1',
141 146 :sticky => '1'}
142 147 assert_redirected_to '/boards/1/topics/1'
143 148 message = Message.find(1)
144 149 assert_equal true, message.sticky?
145 150 assert_equal true, message.locked?
146 151 end
147 152
148 153 def test_post_edit_should_allow_to_change_board
149 154 @request.session[:user_id] = 2
150 155 post :edit, :board_id => 1, :id => 1,
151 156 :message => { :subject => 'New subject',
152 157 :content => 'New body',
153 158 :board_id => 2}
154 159 assert_redirected_to '/boards/2/topics/1'
155 160 message = Message.find(1)
156 161 assert_equal Board.find(2), message.board
157 162 end
158 163
159 164 def test_reply
160 165 @request.session[:user_id] = 2
161 166 post :reply, :board_id => 1, :id => 1, :reply => { :content => 'This is a test reply', :subject => 'Test reply' }
162 167 reply = Message.find(:first, :order => 'id DESC')
163 168 assert_redirected_to "/boards/1/topics/1?r=#{reply.id}"
164 169 assert Message.find_by_subject('Test reply')
165 170 end
166 171
167 172 def test_destroy_topic
168 173 @request.session[:user_id] = 2
169 174 assert_difference 'Message.count', -3 do
170 175 post :destroy, :board_id => 1, :id => 1
171 176 end
172 177 assert_redirected_to '/projects/ecookbook/boards/1'
173 178 assert_nil Message.find_by_id(1)
174 179 end
175 180
176 181 def test_destroy_reply
177 182 @request.session[:user_id] = 2
178 183 assert_difference 'Message.count', -1 do
179 184 post :destroy, :board_id => 1, :id => 2
180 185 end
181 186 assert_redirected_to '/boards/1/topics/1?r=2'
182 187 assert_nil Message.find_by_id(2)
183 188 end
184 189
185 190 def test_quote
186 191 @request.session[:user_id] = 2
187 192 xhr :get, :quote, :board_id => 1, :id => 3
188 193 assert_response :success
189 194 assert_equal 'text/javascript', response.content_type
190 195 assert_template 'quote'
191 196 assert_include 'RE: First post', response.body
192 197 assert_include '> An other reply', response.body
193 198 end
194 199
195 200 def test_preview_new
196 201 @request.session[:user_id] = 2
197 202 post :preview,
198 203 :board_id => 1,
199 204 :message => {:subject => "", :content => "Previewed text"}
200 205 assert_response :success
201 206 assert_template 'common/_preview'
202 207 end
203 208
204 209 def test_preview_edit
205 210 @request.session[:user_id] = 2
206 211 post :preview,
207 212 :id => 4,
208 213 :board_id => 1,
209 214 :message => {:subject => "", :content => "Previewed text"}
210 215 assert_response :success
211 216 assert_template 'common/_preview'
212 217 end
213 218 end
General Comments 0
You need to be logged in to leave comments. Login now