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