##// END OF EJS Templates
code layout clean up test/functional/timelog_controller_test.rb...
Toshi MARUYAMA -
r7642:41bf0a74a53a
parent child
Show More
@@ -1,329 +1,332
1 1 # -*- coding: utf-8 -*-
2 2 # Redmine - project management software
3 3 # Copyright (C) 2006-2011 Jean-Philippe Lang
4 4 #
5 5 # This program is free software; you can redistribute it and/or
6 6 # modify it under the terms of the GNU General Public License
7 7 # as published by the Free Software Foundation; either version 2
8 8 # of the License, or (at your option) any later version.
9 9 #
10 10 # This program is distributed in the hope that it will be useful,
11 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 # GNU General Public License for more details.
14 14 #
15 15 # You should have received a copy of the GNU General Public License
16 16 # along with this program; if not, write to the Free Software
17 17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 18
19 19 require File.expand_path('../../test_helper', __FILE__)
20 20 require 'timelog_controller'
21 21
22 22 # Re-raise errors caught by the controller.
23 23 class TimelogController; def rescue_action(e) raise e end; end
24 24
25 25 class TimelogControllerTest < ActionController::TestCase
26 fixtures :projects, :enabled_modules, :roles, :members, :member_roles, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses, :custom_fields, :custom_values
26 fixtures :projects, :enabled_modules, :roles, :members,
27 :member_roles, :issues, :time_entries, :users,
28 :trackers, :enumerations, :issue_statuses,
29 :custom_fields, :custom_values
27 30
28 31 def setup
29 32 @controller = TimelogController.new
30 33 @request = ActionController::TestRequest.new
31 34 @response = ActionController::TestResponse.new
32 35 end
33 36
34 37 def test_get_new
35 38 @request.session[:user_id] = 3
36 39 get :new, :project_id => 1
37 40 assert_response :success
38 41 assert_template 'edit'
39 42 # Default activity selected
40 43 assert_tag :tag => 'option', :attributes => { :selected => 'selected' },
41 44 :content => 'Development'
42 45 end
43 46
44 47 def test_get_new_should_only_show_active_time_entry_activities
45 48 @request.session[:user_id] = 3
46 49 get :new, :project_id => 1
47 50 assert_response :success
48 51 assert_template 'edit'
49 52 assert_no_tag :tag => 'option', :content => 'Inactive Activity'
50 53 end
51 54
52 55 def test_get_edit_existing_time
53 56 @request.session[:user_id] = 2
54 57 get :edit, :id => 2, :project_id => nil
55 58 assert_response :success
56 59 assert_template 'edit'
57 60 # Default activity selected
58 61 assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/time_entries/2' }
59 62 end
60 63
61 64 def test_get_edit_with_an_existing_time_entry_with_inactive_activity
62 65 te = TimeEntry.find(1)
63 66 te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
64 67 te.save!
65 68
66 69 @request.session[:user_id] = 1
67 70 get :edit, :project_id => 1, :id => 1
68 71 assert_response :success
69 72 assert_template 'edit'
70 73 # Blank option since nothing is pre-selected
71 74 assert_tag :tag => 'option', :content => '--- Please select ---'
72 75 end
73 76
74 77 def test_post_create
75 78 # TODO: should POST to issues’ time log instead of project. change form
76 79 # and routing
77 80 @request.session[:user_id] = 3
78 81 post :create, :project_id => 1,
79 82 :time_entry => {:comments => 'Some work on TimelogControllerTest',
80 83 # Not the default activity
81 84 :activity_id => '11',
82 85 :spent_on => '2008-03-14',
83 86 :issue_id => '1',
84 87 :hours => '7.3'}
85 88 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
86 89
87 90 i = Issue.find(1)
88 91 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
89 92 assert_not_nil t
90 93 assert_equal 11, t.activity_id
91 94 assert_equal 7.3, t.hours
92 95 assert_equal 3, t.user_id
93 96 assert_equal i, t.issue
94 97 assert_equal i.project, t.project
95 98 end
96 99
97 100 def test_post_create_with_blank_issue
98 101 # TODO: should POST to issues’ time log instead of project. change form
99 102 # and routing
100 103 @request.session[:user_id] = 3
101 104 post :create, :project_id => 1,
102 105 :time_entry => {:comments => 'Some work on TimelogControllerTest',
103 106 # Not the default activity
104 107 :activity_id => '11',
105 108 :issue_id => '',
106 109 :spent_on => '2008-03-14',
107 110 :hours => '7.3'}
108 111 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
109 112
110 113 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
111 114 assert_not_nil t
112 115 assert_equal 11, t.activity_id
113 116 assert_equal 7.3, t.hours
114 117 assert_equal 3, t.user_id
115 118 end
116 119
117 120 def test_update
118 121 entry = TimeEntry.find(1)
119 122 assert_equal 1, entry.issue_id
120 123 assert_equal 2, entry.user_id
121 124
122 125 @request.session[:user_id] = 1
123 126 put :update, :id => 1,
124 127 :time_entry => {:issue_id => '2',
125 128 :hours => '8'}
126 129 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
127 130 entry.reload
128 131
129 132 assert_equal 8, entry.hours
130 133 assert_equal 2, entry.issue_id
131 134 assert_equal 2, entry.user_id
132 135 end
133 136
134 137 def test_get_bulk_edit
135 138 @request.session[:user_id] = 2
136 139 get :bulk_edit, :ids => [1, 2]
137 140 assert_response :success
138 141 assert_template 'bulk_edit'
139 142
140 143 # System wide custom field
141 144 assert_tag :select, :attributes => {:name => 'time_entry[custom_field_values][10]'}
142 145 end
143 146
144 147 def test_get_bulk_edit_on_different_projects
145 148 @request.session[:user_id] = 2
146 149 get :bulk_edit, :ids => [1, 2, 6]
147 150 assert_response :success
148 151 assert_template 'bulk_edit'
149 152 end
150 153
151 154 def test_bulk_update
152 155 @request.session[:user_id] = 2
153 156 # update time entry activity
154 157 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9}
155 158
156 159 assert_response 302
157 160 # check that the issues were updated
158 161 assert_equal [9, 9], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.activity_id}
159 162 end
160 163
161 164 def test_bulk_update_on_different_projects
162 165 @request.session[:user_id] = 2
163 166 # update time entry activity
164 167 post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 }
165 168
166 169 assert_response 302
167 170 # check that the issues were updated
168 171 assert_equal [9, 9, 9], TimeEntry.find_all_by_id([1, 2, 4]).collect {|i| i.activity_id}
169 172 end
170 173
171 174 def test_bulk_update_on_different_projects_without_rights
172 175 @request.session[:user_id] = 3
173 176 user = User.find(3)
174 177 action = { :controller => "timelog", :action => "bulk_update" }
175 178 assert user.allowed_to?(action, TimeEntry.find(1).project)
176 179 assert ! user.allowed_to?(action, TimeEntry.find(5).project)
177 180 post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 }
178 181 assert_response 403
179 182 end
180 183
181 184 def test_bulk_update_custom_field
182 185 @request.session[:user_id] = 2
183 186 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }
184 187
185 188 assert_response 302
186 189 assert_equal ["0", "0"], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.custom_value_for(10).value}
187 190 end
188 191
189 192 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
190 193 @request.session[:user_id] = 2
191 194 post :bulk_update, :ids => [1,2], :back_url => '/time_entries'
192 195
193 196 assert_response :redirect
194 197 assert_redirected_to '/time_entries'
195 198 end
196 199
197 200 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
198 201 @request.session[:user_id] = 2
199 202 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
200 203
201 204 assert_response :redirect
202 205 assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
203 206 end
204 207
205 208 def test_destroy
206 209 @request.session[:user_id] = 2
207 210 delete :destroy, :id => 1
208 211 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
209 212 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
210 213 assert_nil TimeEntry.find_by_id(1)
211 214 end
212 215
213 216 def test_destroy_should_fail
214 217 # simulate that this fails (e.g. due to a plugin), see #5700
215 218 TimeEntry.any_instance.expects(:destroy).returns(false)
216 219
217 220 @request.session[:user_id] = 2
218 221 delete :destroy, :id => 1
219 222 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
220 223 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
221 224 assert_not_nil TimeEntry.find_by_id(1)
222 225 end
223 226
224 227 def test_index_all_projects
225 228 get :index
226 229 assert_response :success
227 230 assert_template 'index'
228 231 assert_not_nil assigns(:total_hours)
229 232 assert_equal "162.90", "%.2f" % assigns(:total_hours)
230 233 assert_tag :form,
231 234 :attributes => {:action => "/time_entries", :id => 'query_form'}
232 235 end
233 236
234 237 def test_index_at_project_level
235 238 get :index, :project_id => 'ecookbook'
236 239 assert_response :success
237 240 assert_template 'index'
238 241 assert_not_nil assigns(:entries)
239 242 assert_equal 4, assigns(:entries).size
240 243 # project and subproject
241 244 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
242 245 assert_not_nil assigns(:total_hours)
243 246 assert_equal "162.90", "%.2f" % assigns(:total_hours)
244 247 # display all time by default
245 248 assert_equal '2007-03-12'.to_date, assigns(:from)
246 249 assert_equal '2007-04-22'.to_date, assigns(:to)
247 250 assert_tag :form,
248 251 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
249 252 end
250 253
251 254 def test_index_at_project_level_with_date_range
252 255 get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30'
253 256 assert_response :success
254 257 assert_template 'index'
255 258 assert_not_nil assigns(:entries)
256 259 assert_equal 3, assigns(:entries).size
257 260 assert_not_nil assigns(:total_hours)
258 261 assert_equal "12.90", "%.2f" % assigns(:total_hours)
259 262 assert_equal '2007-03-20'.to_date, assigns(:from)
260 263 assert_equal '2007-04-30'.to_date, assigns(:to)
261 264 assert_tag :form,
262 265 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
263 266 end
264 267
265 268 def test_index_at_project_level_with_period
266 269 get :index, :project_id => 'ecookbook', :period => '7_days'
267 270 assert_response :success
268 271 assert_template 'index'
269 272 assert_not_nil assigns(:entries)
270 273 assert_not_nil assigns(:total_hours)
271 274 assert_equal Date.today - 7, assigns(:from)
272 275 assert_equal Date.today, assigns(:to)
273 276 assert_tag :form,
274 277 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
275 278 end
276 279
277 280 def test_index_one_day
278 281 get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => "2007-03-23"
279 282 assert_response :success
280 283 assert_template 'index'
281 284 assert_not_nil assigns(:total_hours)
282 285 assert_equal "4.25", "%.2f" % assigns(:total_hours)
283 286 assert_tag :form,
284 287 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
285 288 end
286 289
287 290 def test_index_at_issue_level
288 291 get :index, :issue_id => 1
289 292 assert_response :success
290 293 assert_template 'index'
291 294 assert_not_nil assigns(:entries)
292 295 assert_equal 2, assigns(:entries).size
293 296 assert_not_nil assigns(:total_hours)
294 297 assert_equal 154.25, assigns(:total_hours)
295 298 # display all time based on what's been logged
296 299 assert_equal '2007-03-12'.to_date, assigns(:from)
297 300 assert_equal '2007-04-22'.to_date, assigns(:to)
298 301 # TODO: remove /projects/:project_id/issues/:issue_id/time_entries routes
299 302 # to use /issues/:issue_id/time_entries
300 303 assert_tag :form,
301 304 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries", :id => 'query_form'}
302 305 end
303 306
304 307 def test_index_atom_feed
305 308 get :index, :project_id => 1, :format => 'atom'
306 309 assert_response :success
307 310 assert_equal 'application/atom+xml', @response.content_type
308 311 assert_not_nil assigns(:items)
309 312 assert assigns(:items).first.is_a?(TimeEntry)
310 313 end
311 314
312 315 def test_index_all_projects_csv_export
313 316 Setting.date_format = '%m/%d/%Y'
314 317 get :index, :format => 'csv'
315 318 assert_response :success
316 319 assert_equal 'text/csv', @response.content_type
317 320 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
318 321 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
319 322 end
320 323
321 324 def test_index_csv_export
322 325 Setting.date_format = '%m/%d/%Y'
323 326 get :index, :project_id => 1, :format => 'csv'
324 327 assert_response :success
325 328 assert_equal 'text/csv', @response.content_type
326 329 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
327 330 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
328 331 end
329 332 end
General Comments 0
You need to be logged in to leave comments. Login now