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