##// END OF EJS Templates
Fixed: error raised when trying to add an empty comment to a news (#3615)....
Jean-Philippe Lang -
r2715:d41bd93acbea
parent child
Show More
@@ -1,107 +1,108
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 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 NewsController < ApplicationController
18 class NewsController < ApplicationController
19 before_filter :find_news, :except => [:new, :index, :preview]
19 before_filter :find_news, :except => [:new, :index, :preview]
20 before_filter :find_project, :only => [:new, :preview]
20 before_filter :find_project, :only => [:new, :preview]
21 before_filter :authorize, :except => [:index, :preview]
21 before_filter :authorize, :except => [:index, :preview]
22 before_filter :find_optional_project, :only => :index
22 before_filter :find_optional_project, :only => :index
23 accept_key_auth :index
23 accept_key_auth :index
24
24
25 def index
25 def index
26 @news_pages, @newss = paginate :news,
26 @news_pages, @newss = paginate :news,
27 :per_page => 10,
27 :per_page => 10,
28 :conditions => (@project ? {:project_id => @project.id} : Project.visible_by(User.current)),
28 :conditions => (@project ? {:project_id => @project.id} : Project.visible_by(User.current)),
29 :include => [:author, :project],
29 :include => [:author, :project],
30 :order => "#{News.table_name}.created_on DESC"
30 :order => "#{News.table_name}.created_on DESC"
31 respond_to do |format|
31 respond_to do |format|
32 format.html { render :layout => false if request.xhr? }
32 format.html { render :layout => false if request.xhr? }
33 format.atom { render_feed(@newss, :title => (@project ? @project.name : Setting.app_title) + ": #{l(:label_news_plural)}") }
33 format.atom { render_feed(@newss, :title => (@project ? @project.name : Setting.app_title) + ": #{l(:label_news_plural)}") }
34 end
34 end
35 end
35 end
36
36
37 def show
37 def show
38 @comments = @news.comments
38 @comments = @news.comments
39 @comments.reverse! if User.current.wants_comments_in_reverse_order?
39 @comments.reverse! if User.current.wants_comments_in_reverse_order?
40 end
40 end
41
41
42 def new
42 def new
43 @news = News.new(:project => @project, :author => User.current)
43 @news = News.new(:project => @project, :author => User.current)
44 if request.post?
44 if request.post?
45 @news.attributes = params[:news]
45 @news.attributes = params[:news]
46 if @news.save
46 if @news.save
47 flash[:notice] = l(:notice_successful_create)
47 flash[:notice] = l(:notice_successful_create)
48 redirect_to :controller => 'news', :action => 'index', :project_id => @project
48 redirect_to :controller => 'news', :action => 'index', :project_id => @project
49 end
49 end
50 end
50 end
51 end
51 end
52
52
53 def edit
53 def edit
54 if request.post? and @news.update_attributes(params[:news])
54 if request.post? and @news.update_attributes(params[:news])
55 flash[:notice] = l(:notice_successful_update)
55 flash[:notice] = l(:notice_successful_update)
56 redirect_to :action => 'show', :id => @news
56 redirect_to :action => 'show', :id => @news
57 end
57 end
58 end
58 end
59
59
60 def add_comment
60 def add_comment
61 @comment = Comment.new(params[:comment])
61 @comment = Comment.new(params[:comment])
62 @comment.author = User.current
62 @comment.author = User.current
63 if @news.comments << @comment
63 if @news.comments << @comment
64 flash[:notice] = l(:label_comment_added)
64 flash[:notice] = l(:label_comment_added)
65 redirect_to :action => 'show', :id => @news
65 redirect_to :action => 'show', :id => @news
66 else
66 else
67 show
67 render :action => 'show'
68 render :action => 'show'
68 end
69 end
69 end
70 end
70
71
71 def destroy_comment
72 def destroy_comment
72 @news.comments.find(params[:comment_id]).destroy
73 @news.comments.find(params[:comment_id]).destroy
73 redirect_to :action => 'show', :id => @news
74 redirect_to :action => 'show', :id => @news
74 end
75 end
75
76
76 def destroy
77 def destroy
77 @news.destroy
78 @news.destroy
78 redirect_to :action => 'index', :project_id => @project
79 redirect_to :action => 'index', :project_id => @project
79 end
80 end
80
81
81 def preview
82 def preview
82 @text = (params[:news] ? params[:news][:description] : nil)
83 @text = (params[:news] ? params[:news][:description] : nil)
83 render :partial => 'common/preview'
84 render :partial => 'common/preview'
84 end
85 end
85
86
86 private
87 private
87 def find_news
88 def find_news
88 @news = News.find(params[:id])
89 @news = News.find(params[:id])
89 @project = @news.project
90 @project = @news.project
90 rescue ActiveRecord::RecordNotFound
91 rescue ActiveRecord::RecordNotFound
91 render_404
92 render_404
92 end
93 end
93
94
94 def find_project
95 def find_project
95 @project = Project.find(params[:project_id])
96 @project = Project.find(params[:project_id])
96 rescue ActiveRecord::RecordNotFound
97 rescue ActiveRecord::RecordNotFound
97 render_404
98 render_404
98 end
99 end
99
100
100 def find_optional_project
101 def find_optional_project
101 return true unless params[:project_id]
102 return true unless params[:project_id]
102 @project = Project.find(params[:project_id])
103 @project = Project.find(params[:project_id])
103 authorize
104 authorize
104 rescue ActiveRecord::RecordNotFound
105 rescue ActiveRecord::RecordNotFound
105 render_404
106 render_404
106 end
107 end
107 end
108 end
@@ -1,215 +1,224
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 require File.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19 require 'news_controller'
19 require 'news_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class NewsController; def rescue_action(e) raise e end; end
22 class NewsController; def rescue_action(e) raise e end; end
23
23
24 class NewsControllerTest < Test::Unit::TestCase
24 class NewsControllerTest < Test::Unit::TestCase
25 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, :news, :comments
25 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, :news, :comments
26
26
27 def setup
27 def setup
28 @controller = NewsController.new
28 @controller = NewsController.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_index_routing
34 def test_index_routing
35 assert_routing(
35 assert_routing(
36 {:method => :get, :path => '/news'},
36 {:method => :get, :path => '/news'},
37 :controller => 'news', :action => 'index'
37 :controller => 'news', :action => 'index'
38 )
38 )
39 end
39 end
40
40
41 def test_index_routing_formatted
41 def test_index_routing_formatted
42 assert_routing(
42 assert_routing(
43 {:method => :get, :path => '/news.atom'},
43 {:method => :get, :path => '/news.atom'},
44 :controller => 'news', :action => 'index', :format => 'atom'
44 :controller => 'news', :action => 'index', :format => 'atom'
45 )
45 )
46 end
46 end
47
47
48 def test_index
48 def test_index
49 get :index
49 get :index
50 assert_response :success
50 assert_response :success
51 assert_template 'index'
51 assert_template 'index'
52 assert_not_nil assigns(:newss)
52 assert_not_nil assigns(:newss)
53 assert_nil assigns(:project)
53 assert_nil assigns(:project)
54 end
54 end
55
55
56 def test_index_with_project_routing
56 def test_index_with_project_routing
57 assert_routing(
57 assert_routing(
58 {:method => :get, :path => '/projects/567/news'},
58 {:method => :get, :path => '/projects/567/news'},
59 :controller => 'news', :action => 'index', :project_id => '567'
59 :controller => 'news', :action => 'index', :project_id => '567'
60 )
60 )
61 end
61 end
62
62
63 def test_index_with_project_routing_formatted
63 def test_index_with_project_routing_formatted
64 assert_routing(
64 assert_routing(
65 {:method => :get, :path => '/projects/567/news.atom'},
65 {:method => :get, :path => '/projects/567/news.atom'},
66 :controller => 'news', :action => 'index', :project_id => '567', :format => 'atom'
66 :controller => 'news', :action => 'index', :project_id => '567', :format => 'atom'
67 )
67 )
68 end
68 end
69
69
70 def test_index_with_project
70 def test_index_with_project
71 get :index, :project_id => 1
71 get :index, :project_id => 1
72 assert_response :success
72 assert_response :success
73 assert_template 'index'
73 assert_template 'index'
74 assert_not_nil assigns(:newss)
74 assert_not_nil assigns(:newss)
75 end
75 end
76
76
77 def test_show_routing
77 def test_show_routing
78 assert_routing(
78 assert_routing(
79 {:method => :get, :path => '/news/2'},
79 {:method => :get, :path => '/news/2'},
80 :controller => 'news', :action => 'show', :id => '2'
80 :controller => 'news', :action => 'show', :id => '2'
81 )
81 )
82 end
82 end
83
83
84 def test_show
84 def test_show
85 get :show, :id => 1
85 get :show, :id => 1
86 assert_response :success
86 assert_response :success
87 assert_template 'show'
87 assert_template 'show'
88 assert_tag :tag => 'h2', :content => /eCookbook first release/
88 assert_tag :tag => 'h2', :content => /eCookbook first release/
89 end
89 end
90
90
91 def test_show_not_found
91 def test_show_not_found
92 get :show, :id => 999
92 get :show, :id => 999
93 assert_response 404
93 assert_response 404
94 end
94 end
95
95
96 def test_new_routing
96 def test_new_routing
97 assert_routing(
97 assert_routing(
98 {:method => :get, :path => '/projects/567/news/new'},
98 {:method => :get, :path => '/projects/567/news/new'},
99 :controller => 'news', :action => 'new', :project_id => '567'
99 :controller => 'news', :action => 'new', :project_id => '567'
100 )
100 )
101 assert_recognizes(
101 assert_recognizes(
102 {:controller => 'news', :action => 'new', :project_id => '567'},
102 {:controller => 'news', :action => 'new', :project_id => '567'},
103 {:method => :post, :path => '/projects/567/news'}
103 {:method => :post, :path => '/projects/567/news'}
104 )
104 )
105 end
105 end
106
106
107 def test_get_new
107 def test_get_new
108 @request.session[:user_id] = 2
108 @request.session[:user_id] = 2
109 get :new, :project_id => 1
109 get :new, :project_id => 1
110 assert_response :success
110 assert_response :success
111 assert_template 'new'
111 assert_template 'new'
112 end
112 end
113
113
114 def test_post_new
114 def test_post_new
115 ActionMailer::Base.deliveries.clear
115 ActionMailer::Base.deliveries.clear
116 Setting.notified_events << 'news_added'
116 Setting.notified_events << 'news_added'
117
117
118 @request.session[:user_id] = 2
118 @request.session[:user_id] = 2
119 post :new, :project_id => 1, :news => { :title => 'NewsControllerTest',
119 post :new, :project_id => 1, :news => { :title => 'NewsControllerTest',
120 :description => 'This is the description',
120 :description => 'This is the description',
121 :summary => '' }
121 :summary => '' }
122 assert_redirected_to 'projects/ecookbook/news'
122 assert_redirected_to 'projects/ecookbook/news'
123
123
124 news = News.find_by_title('NewsControllerTest')
124 news = News.find_by_title('NewsControllerTest')
125 assert_not_nil news
125 assert_not_nil news
126 assert_equal 'This is the description', news.description
126 assert_equal 'This is the description', news.description
127 assert_equal User.find(2), news.author
127 assert_equal User.find(2), news.author
128 assert_equal Project.find(1), news.project
128 assert_equal Project.find(1), news.project
129 assert_equal 1, ActionMailer::Base.deliveries.size
129 assert_equal 1, ActionMailer::Base.deliveries.size
130 end
130 end
131
131
132 def test_edit_routing
132 def test_edit_routing
133 assert_routing(
133 assert_routing(
134 {:method => :get, :path => '/news/234'},
134 {:method => :get, :path => '/news/234'},
135 :controller => 'news', :action => 'show', :id => '234'
135 :controller => 'news', :action => 'show', :id => '234'
136 )
136 )
137 assert_recognizes(#TODO: PUT to news URI instead, need to modify form
137 assert_recognizes(#TODO: PUT to news URI instead, need to modify form
138 {:controller => 'news', :action => 'edit', :id => '567'},
138 {:controller => 'news', :action => 'edit', :id => '567'},
139 {:method => :post, :path => '/news/567/edit'}
139 {:method => :post, :path => '/news/567/edit'}
140 )
140 )
141 end
141 end
142
142
143 def test_get_edit
143 def test_get_edit
144 @request.session[:user_id] = 2
144 @request.session[:user_id] = 2
145 get :edit, :id => 1
145 get :edit, :id => 1
146 assert_response :success
146 assert_response :success
147 assert_template 'edit'
147 assert_template 'edit'
148 end
148 end
149
149
150 def test_post_edit
150 def test_post_edit
151 @request.session[:user_id] = 2
151 @request.session[:user_id] = 2
152 post :edit, :id => 1, :news => { :description => 'Description changed by test_post_edit' }
152 post :edit, :id => 1, :news => { :description => 'Description changed by test_post_edit' }
153 assert_redirected_to 'news/1'
153 assert_redirected_to 'news/1'
154 news = News.find(1)
154 news = News.find(1)
155 assert_equal 'Description changed by test_post_edit', news.description
155 assert_equal 'Description changed by test_post_edit', news.description
156 end
156 end
157
157
158 def test_post_new_with_validation_failure
158 def test_post_new_with_validation_failure
159 @request.session[:user_id] = 2
159 @request.session[:user_id] = 2
160 post :new, :project_id => 1, :news => { :title => '',
160 post :new, :project_id => 1, :news => { :title => '',
161 :description => 'This is the description',
161 :description => 'This is the description',
162 :summary => '' }
162 :summary => '' }
163 assert_response :success
163 assert_response :success
164 assert_template 'new'
164 assert_template 'new'
165 assert_not_nil assigns(:news)
165 assert_not_nil assigns(:news)
166 assert assigns(:news).new_record?
166 assert assigns(:news).new_record?
167 assert_tag :tag => 'div', :attributes => { :id => 'errorExplanation' },
167 assert_tag :tag => 'div', :attributes => { :id => 'errorExplanation' },
168 :content => /1 error/
168 :content => /1 error/
169 end
169 end
170
170
171 def test_add_comment
171 def test_add_comment
172 @request.session[:user_id] = 2
172 @request.session[:user_id] = 2
173 post :add_comment, :id => 1, :comment => { :comments => 'This is a NewsControllerTest comment' }
173 post :add_comment, :id => 1, :comment => { :comments => 'This is a NewsControllerTest comment' }
174 assert_redirected_to 'news/1'
174 assert_redirected_to 'news/1'
175
175
176 comment = News.find(1).comments.find(:first, :order => 'created_on DESC')
176 comment = News.find(1).comments.find(:first, :order => 'created_on DESC')
177 assert_not_nil comment
177 assert_not_nil comment
178 assert_equal 'This is a NewsControllerTest comment', comment.comments
178 assert_equal 'This is a NewsControllerTest comment', comment.comments
179 assert_equal User.find(2), comment.author
179 assert_equal User.find(2), comment.author
180 end
180 end
181
181
182 def test_empty_comment_should_not_be_added
183 @request.session[:user_id] = 2
184 assert_no_difference 'Comment.count' do
185 post :add_comment, :id => 1, :comment => { :comments => '' }
186 assert_response :success
187 assert_template 'show'
188 end
189 end
190
182 def test_destroy_comment
191 def test_destroy_comment
183 comments_count = News.find(1).comments.size
192 comments_count = News.find(1).comments.size
184 @request.session[:user_id] = 2
193 @request.session[:user_id] = 2
185 post :destroy_comment, :id => 1, :comment_id => 2
194 post :destroy_comment, :id => 1, :comment_id => 2
186 assert_redirected_to 'news/1'
195 assert_redirected_to 'news/1'
187 assert_nil Comment.find_by_id(2)
196 assert_nil Comment.find_by_id(2)
188 assert_equal comments_count - 1, News.find(1).comments.size
197 assert_equal comments_count - 1, News.find(1).comments.size
189 end
198 end
190
199
191 def test_destroy_routing
200 def test_destroy_routing
192 assert_recognizes(#TODO: should use DELETE to news URI, need to change form
201 assert_recognizes(#TODO: should use DELETE to news URI, need to change form
193 {:controller => 'news', :action => 'destroy', :id => '567'},
202 {:controller => 'news', :action => 'destroy', :id => '567'},
194 {:method => :post, :path => '/news/567/destroy'}
203 {:method => :post, :path => '/news/567/destroy'}
195 )
204 )
196 end
205 end
197
206
198 def test_destroy
207 def test_destroy
199 @request.session[:user_id] = 2
208 @request.session[:user_id] = 2
200 post :destroy, :id => 1
209 post :destroy, :id => 1
201 assert_redirected_to 'projects/ecookbook/news'
210 assert_redirected_to 'projects/ecookbook/news'
202 assert_nil News.find_by_id(1)
211 assert_nil News.find_by_id(1)
203 end
212 end
204
213
205 def test_preview
214 def test_preview
206 get :preview, :project_id => 1,
215 get :preview, :project_id => 1,
207 :news => {:title => '',
216 :news => {:title => '',
208 :description => 'News description',
217 :description => 'News description',
209 :summary => ''}
218 :summary => ''}
210 assert_response :success
219 assert_response :success
211 assert_template 'common/_preview'
220 assert_template 'common/_preview'
212 assert_tag :tag => 'fieldset', :attributes => { :class => 'preview' },
221 assert_tag :tag => 'fieldset', :attributes => { :class => 'preview' },
213 :content => /News description/
222 :content => /News description/
214 end
223 end
215 end
224 end
General Comments 0
You need to be logged in to leave comments. Login now