@@ -1,789 +1,782 | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | # Redmine - project management software |
|
3 | 3 | # Copyright (C) 2006-2012 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, |
|
27 | 27 | :member_roles, :issues, :time_entries, :users, |
|
28 | 28 | :trackers, :enumerations, :issue_statuses, |
|
29 | 29 | :custom_fields, :custom_values |
|
30 | 30 | |
|
31 | 31 | include Redmine::I18n |
|
32 | 32 | |
|
33 | 33 | def setup |
|
34 | 34 | @controller = TimelogController.new |
|
35 | 35 | @request = ActionController::TestRequest.new |
|
36 | 36 | @response = ActionController::TestResponse.new |
|
37 | 37 | end |
|
38 | 38 | |
|
39 | 39 | def test_new_with_project_id |
|
40 | 40 | @request.session[:user_id] = 3 |
|
41 | 41 | get :new, :project_id => 1 |
|
42 | 42 | assert_response :success |
|
43 | 43 | assert_template 'new' |
|
44 | 44 | assert_select 'select[name=?]', 'time_entry[project_id]', 0 |
|
45 | 45 | assert_select 'input[name=?][value=1][type=hidden]', 'time_entry[project_id]' |
|
46 | 46 | end |
|
47 | 47 | |
|
48 | 48 | def test_new_with_issue_id |
|
49 | 49 | @request.session[:user_id] = 3 |
|
50 | 50 | get :new, :issue_id => 2 |
|
51 | 51 | assert_response :success |
|
52 | 52 | assert_template 'new' |
|
53 | 53 | assert_select 'select[name=?]', 'time_entry[project_id]', 0 |
|
54 | 54 | assert_select 'input[name=?][value=1][type=hidden]', 'time_entry[project_id]' |
|
55 | 55 | end |
|
56 | 56 | |
|
57 | 57 | def test_new_without_project |
|
58 | 58 | @request.session[:user_id] = 3 |
|
59 | 59 | get :new |
|
60 | 60 | assert_response :success |
|
61 | 61 | assert_template 'new' |
|
62 | 62 | assert_select 'select[name=?]', 'time_entry[project_id]' |
|
63 | 63 | assert_select 'input[name=?]', 'time_entry[project_id]', 0 |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | def test_new_without_project_should_prefill_the_form |
|
67 | 67 | @request.session[:user_id] = 3 |
|
68 | 68 | get :new, :time_entry => {:project_id => '1'} |
|
69 | 69 | assert_response :success |
|
70 | 70 | assert_template 'new' |
|
71 | 71 | assert_select 'select[name=?]', 'time_entry[project_id]' do |
|
72 | 72 | assert_select 'option[value=1][selected=selected]' |
|
73 | 73 | end |
|
74 | 74 | assert_select 'input[name=?]', 'time_entry[project_id]', 0 |
|
75 | 75 | end |
|
76 | 76 | |
|
77 | 77 | def test_new_without_project_should_deny_without_permission |
|
78 | 78 | Role.all.each {|role| role.remove_permission! :log_time} |
|
79 | 79 | @request.session[:user_id] = 3 |
|
80 | 80 | |
|
81 | 81 | get :new |
|
82 | 82 | assert_response 403 |
|
83 | 83 | end |
|
84 | 84 | |
|
85 | 85 | def test_new_should_select_default_activity |
|
86 | 86 | @request.session[:user_id] = 3 |
|
87 | 87 | get :new, :project_id => 1 |
|
88 | 88 | assert_response :success |
|
89 | 89 | assert_select 'select[name=?]', 'time_entry[activity_id]' do |
|
90 | 90 | assert_select 'option[selected=selected]', :text => 'Development' |
|
91 | 91 | end |
|
92 | 92 | end |
|
93 | 93 | |
|
94 | 94 | def test_new_should_only_show_active_time_entry_activities |
|
95 | 95 | @request.session[:user_id] = 3 |
|
96 | 96 | get :new, :project_id => 1 |
|
97 | 97 | assert_response :success |
|
98 | 98 | assert_no_tag 'option', :content => 'Inactive Activity' |
|
99 | 99 | end |
|
100 | 100 | |
|
101 | 101 | def test_get_edit_existing_time |
|
102 | 102 | @request.session[:user_id] = 2 |
|
103 | 103 | get :edit, :id => 2, :project_id => nil |
|
104 | 104 | assert_response :success |
|
105 | 105 | assert_template 'edit' |
|
106 | 106 | # Default activity selected |
|
107 | 107 | assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/time_entries/2' } |
|
108 | 108 | end |
|
109 | 109 | |
|
110 | 110 | def test_get_edit_with_an_existing_time_entry_with_inactive_activity |
|
111 | 111 | te = TimeEntry.find(1) |
|
112 | 112 | te.activity = TimeEntryActivity.find_by_name("Inactive Activity") |
|
113 | 113 | te.save! |
|
114 | 114 | |
|
115 | 115 | @request.session[:user_id] = 1 |
|
116 | 116 | get :edit, :project_id => 1, :id => 1 |
|
117 | 117 | assert_response :success |
|
118 | 118 | assert_template 'edit' |
|
119 | 119 | # Blank option since nothing is pre-selected |
|
120 | 120 | assert_tag :tag => 'option', :content => '--- Please select ---' |
|
121 | 121 | end |
|
122 | 122 | |
|
123 | 123 | def test_post_create |
|
124 | 124 | # TODO: should POST to issuesβ time log instead of project. change form |
|
125 | 125 | # and routing |
|
126 | 126 | @request.session[:user_id] = 3 |
|
127 | 127 | post :create, :project_id => 1, |
|
128 | 128 | :time_entry => {:comments => 'Some work on TimelogControllerTest', |
|
129 | 129 | # Not the default activity |
|
130 | 130 | :activity_id => '11', |
|
131 | 131 | :spent_on => '2008-03-14', |
|
132 | 132 | :issue_id => '1', |
|
133 | 133 | :hours => '7.3'} |
|
134 | 134 | assert_redirected_to :action => 'index', :project_id => 'ecookbook' |
|
135 | 135 | |
|
136 | 136 | i = Issue.find(1) |
|
137 | 137 | t = TimeEntry.find_by_comments('Some work on TimelogControllerTest') |
|
138 | 138 | assert_not_nil t |
|
139 | 139 | assert_equal 11, t.activity_id |
|
140 | 140 | assert_equal 7.3, t.hours |
|
141 | 141 | assert_equal 3, t.user_id |
|
142 | 142 | assert_equal i, t.issue |
|
143 | 143 | assert_equal i.project, t.project |
|
144 | 144 | end |
|
145 | 145 | |
|
146 | 146 | def test_post_create_with_blank_issue |
|
147 | 147 | # TODO: should POST to issuesβ time log instead of project. change form |
|
148 | 148 | # and routing |
|
149 | 149 | @request.session[:user_id] = 3 |
|
150 | 150 | post :create, :project_id => 1, |
|
151 | 151 | :time_entry => {:comments => 'Some work on TimelogControllerTest', |
|
152 | 152 | # Not the default activity |
|
153 | 153 | :activity_id => '11', |
|
154 | 154 | :issue_id => '', |
|
155 | 155 | :spent_on => '2008-03-14', |
|
156 | 156 | :hours => '7.3'} |
|
157 | 157 | assert_redirected_to :action => 'index', :project_id => 'ecookbook' |
|
158 | 158 | |
|
159 | 159 | t = TimeEntry.find_by_comments('Some work on TimelogControllerTest') |
|
160 | 160 | assert_not_nil t |
|
161 | 161 | assert_equal 11, t.activity_id |
|
162 | 162 | assert_equal 7.3, t.hours |
|
163 | 163 | assert_equal 3, t.user_id |
|
164 | 164 | end |
|
165 | 165 | |
|
166 | 166 | def test_create_and_continue |
|
167 | 167 | @request.session[:user_id] = 2 |
|
168 | 168 | post :create, :project_id => 1, |
|
169 | 169 | :time_entry => {:activity_id => '11', |
|
170 | 170 | :issue_id => '', |
|
171 | 171 | :spent_on => '2008-03-14', |
|
172 | 172 | :hours => '7.3'}, |
|
173 | 173 | :continue => '1' |
|
174 | 174 | assert_redirected_to '/projects/ecookbook/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=' |
|
175 | 175 | end |
|
176 | 176 | |
|
177 | 177 | def test_create_and_continue_with_issue_id |
|
178 | 178 | @request.session[:user_id] = 2 |
|
179 | 179 | post :create, :project_id => 1, |
|
180 | 180 | :time_entry => {:activity_id => '11', |
|
181 | 181 | :issue_id => '1', |
|
182 | 182 | :spent_on => '2008-03-14', |
|
183 | 183 | :hours => '7.3'}, |
|
184 | 184 | :continue => '1' |
|
185 | 185 | assert_redirected_to '/projects/ecookbook/issues/1/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=1' |
|
186 | 186 | end |
|
187 | 187 | |
|
188 | 188 | def test_create_and_continue_without_project |
|
189 | 189 | @request.session[:user_id] = 2 |
|
190 | 190 | post :create, :time_entry => {:project_id => '1', |
|
191 | 191 | :activity_id => '11', |
|
192 | 192 | :issue_id => '', |
|
193 | 193 | :spent_on => '2008-03-14', |
|
194 | 194 | :hours => '7.3'}, |
|
195 | 195 | :continue => '1' |
|
196 | 196 | |
|
197 | 197 | assert_redirected_to '/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=&time_entry%5Bproject_id%5D=1' |
|
198 | 198 | end |
|
199 | 199 | |
|
200 | 200 | def test_create_without_log_time_permission_should_be_denied |
|
201 | 201 | @request.session[:user_id] = 2 |
|
202 | 202 | Role.find_by_name('Manager').remove_permission! :log_time |
|
203 | 203 | post :create, :project_id => 1, |
|
204 | 204 | :time_entry => {:activity_id => '11', |
|
205 | 205 | :issue_id => '', |
|
206 | 206 | :spent_on => '2008-03-14', |
|
207 | 207 | :hours => '7.3'} |
|
208 | 208 | |
|
209 | 209 | assert_response 403 |
|
210 | 210 | end |
|
211 | 211 | |
|
212 | 212 | def test_create_with_failure |
|
213 | 213 | @request.session[:user_id] = 2 |
|
214 | 214 | post :create, :project_id => 1, |
|
215 | 215 | :time_entry => {:activity_id => '', |
|
216 | 216 | :issue_id => '', |
|
217 | 217 | :spent_on => '2008-03-14', |
|
218 | 218 | :hours => '7.3'} |
|
219 | 219 | |
|
220 | 220 | assert_response :success |
|
221 | 221 | assert_template 'new' |
|
222 | 222 | end |
|
223 | 223 | |
|
224 | 224 | def test_create_without_project |
|
225 | 225 | @request.session[:user_id] = 2 |
|
226 | 226 | assert_difference 'TimeEntry.count' do |
|
227 | 227 | post :create, :time_entry => {:project_id => '1', |
|
228 | 228 | :activity_id => '11', |
|
229 | 229 | :issue_id => '', |
|
230 | 230 | :spent_on => '2008-03-14', |
|
231 | 231 | :hours => '7.3'} |
|
232 | 232 | end |
|
233 | 233 | |
|
234 | 234 | assert_redirected_to '/projects/ecookbook/time_entries' |
|
235 | 235 | time_entry = TimeEntry.first(:order => 'id DESC') |
|
236 | 236 | assert_equal 1, time_entry.project_id |
|
237 | 237 | end |
|
238 | 238 | |
|
239 | 239 | def test_create_without_project_should_fail_with_issue_not_inside_project |
|
240 | 240 | @request.session[:user_id] = 2 |
|
241 | 241 | assert_no_difference 'TimeEntry.count' do |
|
242 | 242 | post :create, :time_entry => {:project_id => '1', |
|
243 | 243 | :activity_id => '11', |
|
244 | 244 | :issue_id => '5', |
|
245 | 245 | :spent_on => '2008-03-14', |
|
246 | 246 | :hours => '7.3'} |
|
247 | 247 | end |
|
248 | 248 | |
|
249 | 249 | assert_response :success |
|
250 | 250 | assert assigns(:time_entry).errors[:issue_id].present? |
|
251 | 251 | end |
|
252 | 252 | |
|
253 | 253 | def test_create_without_project_should_deny_without_permission |
|
254 | 254 | @request.session[:user_id] = 2 |
|
255 | 255 | Project.find(3).disable_module!(:time_tracking) |
|
256 | 256 | |
|
257 | 257 | assert_no_difference 'TimeEntry.count' do |
|
258 | 258 | post :create, :time_entry => {:project_id => '3', |
|
259 | 259 | :activity_id => '11', |
|
260 | 260 | :issue_id => '', |
|
261 | 261 | :spent_on => '2008-03-14', |
|
262 | 262 | :hours => '7.3'} |
|
263 | 263 | end |
|
264 | 264 | |
|
265 | 265 | assert_response 403 |
|
266 | 266 | end |
|
267 | 267 | |
|
268 | 268 | def test_create_without_project_with_failure |
|
269 | 269 | @request.session[:user_id] = 2 |
|
270 | 270 | assert_no_difference 'TimeEntry.count' do |
|
271 | 271 | post :create, :time_entry => {:project_id => '1', |
|
272 | 272 | :activity_id => '11', |
|
273 | 273 | :issue_id => '', |
|
274 | 274 | :spent_on => '2008-03-14', |
|
275 | 275 | :hours => ''} |
|
276 | 276 | end |
|
277 | 277 | |
|
278 | 278 | assert_response :success |
|
279 | 279 | assert_tag 'select', :attributes => {:name => 'time_entry[project_id]'}, |
|
280 | 280 | :child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}} |
|
281 | 281 | end |
|
282 | 282 | |
|
283 | 283 | def test_update |
|
284 | 284 | entry = TimeEntry.find(1) |
|
285 | 285 | assert_equal 1, entry.issue_id |
|
286 | 286 | assert_equal 2, entry.user_id |
|
287 | 287 | |
|
288 | 288 | @request.session[:user_id] = 1 |
|
289 | 289 | put :update, :id => 1, |
|
290 | 290 | :time_entry => {:issue_id => '2', |
|
291 | 291 | :hours => '8'} |
|
292 | 292 | assert_redirected_to :action => 'index', :project_id => 'ecookbook' |
|
293 | 293 | entry.reload |
|
294 | 294 | |
|
295 | 295 | assert_equal 8, entry.hours |
|
296 | 296 | assert_equal 2, entry.issue_id |
|
297 | 297 | assert_equal 2, entry.user_id |
|
298 | 298 | end |
|
299 | 299 | |
|
300 | 300 | def test_get_bulk_edit |
|
301 | 301 | @request.session[:user_id] = 2 |
|
302 | 302 | get :bulk_edit, :ids => [1, 2] |
|
303 | 303 | assert_response :success |
|
304 | 304 | assert_template 'bulk_edit' |
|
305 | 305 | |
|
306 | 306 | # System wide custom field |
|
307 | 307 | assert_tag :select, :attributes => {:name => 'time_entry[custom_field_values][10]'} |
|
308 | 308 | |
|
309 | 309 | # Activities |
|
310 | 310 | assert_select 'select[name=?]', 'time_entry[activity_id]' do |
|
311 | 311 | assert_select 'option[value=]', :text => '(No change)' |
|
312 | 312 | assert_select 'option[value=9]', :text => 'Design' |
|
313 | 313 | end |
|
314 | 314 | end |
|
315 | 315 | |
|
316 | 316 | def test_get_bulk_edit_on_different_projects |
|
317 | 317 | @request.session[:user_id] = 2 |
|
318 | 318 | get :bulk_edit, :ids => [1, 2, 6] |
|
319 | 319 | assert_response :success |
|
320 | 320 | assert_template 'bulk_edit' |
|
321 | 321 | end |
|
322 | 322 | |
|
323 | 323 | def test_bulk_update |
|
324 | 324 | @request.session[:user_id] = 2 |
|
325 | 325 | # update time entry activity |
|
326 | 326 | post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9} |
|
327 | 327 | |
|
328 | 328 | assert_response 302 |
|
329 | 329 | # check that the issues were updated |
|
330 | 330 | assert_equal [9, 9], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.activity_id} |
|
331 | 331 | end |
|
332 | 332 | |
|
333 | 333 | def test_bulk_update_with_failure |
|
334 | 334 | @request.session[:user_id] = 2 |
|
335 | 335 | post :bulk_update, :ids => [1, 2], :time_entry => { :hours => 'A'} |
|
336 | 336 | |
|
337 | 337 | assert_response 302 |
|
338 | 338 | assert_match /Failed to save 2 time entrie/, flash[:error] |
|
339 | 339 | end |
|
340 | 340 | |
|
341 | 341 | def test_bulk_update_on_different_projects |
|
342 | 342 | @request.session[:user_id] = 2 |
|
343 | 343 | # makes user a manager on the other project |
|
344 | 344 | Member.create!(:user_id => 2, :project_id => 3, :role_ids => [1]) |
|
345 | 345 | |
|
346 | 346 | # update time entry activity |
|
347 | 347 | post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 } |
|
348 | 348 | |
|
349 | 349 | assert_response 302 |
|
350 | 350 | # check that the issues were updated |
|
351 | 351 | assert_equal [9, 9, 9], TimeEntry.find_all_by_id([1, 2, 4]).collect {|i| i.activity_id} |
|
352 | 352 | end |
|
353 | 353 | |
|
354 | 354 | def test_bulk_update_on_different_projects_without_rights |
|
355 | 355 | @request.session[:user_id] = 3 |
|
356 | 356 | user = User.find(3) |
|
357 | 357 | action = { :controller => "timelog", :action => "bulk_update" } |
|
358 | 358 | assert user.allowed_to?(action, TimeEntry.find(1).project) |
|
359 | 359 | assert ! user.allowed_to?(action, TimeEntry.find(5).project) |
|
360 | 360 | post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 } |
|
361 | 361 | assert_response 403 |
|
362 | 362 | end |
|
363 | 363 | |
|
364 | 364 | def test_bulk_update_custom_field |
|
365 | 365 | @request.session[:user_id] = 2 |
|
366 | 366 | post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} } |
|
367 | 367 | |
|
368 | 368 | assert_response 302 |
|
369 | 369 | assert_equal ["0", "0"], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.custom_value_for(10).value} |
|
370 | 370 | end |
|
371 | 371 | |
|
372 | 372 | def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter |
|
373 | 373 | @request.session[:user_id] = 2 |
|
374 | 374 | post :bulk_update, :ids => [1,2], :back_url => '/time_entries' |
|
375 | 375 | |
|
376 | 376 | assert_response :redirect |
|
377 | 377 | assert_redirected_to '/time_entries' |
|
378 | 378 | end |
|
379 | 379 | |
|
380 | 380 | def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host |
|
381 | 381 | @request.session[:user_id] = 2 |
|
382 | 382 | post :bulk_update, :ids => [1,2], :back_url => 'http://google.com' |
|
383 | 383 | |
|
384 | 384 | assert_response :redirect |
|
385 | 385 | assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier |
|
386 | 386 | end |
|
387 | 387 | |
|
388 | 388 | def test_post_bulk_update_without_edit_permission_should_be_denied |
|
389 | 389 | @request.session[:user_id] = 2 |
|
390 | 390 | Role.find_by_name('Manager').remove_permission! :edit_time_entries |
|
391 | 391 | post :bulk_update, :ids => [1,2] |
|
392 | 392 | |
|
393 | 393 | assert_response 403 |
|
394 | 394 | end |
|
395 | 395 | |
|
396 | 396 | def test_destroy |
|
397 | 397 | @request.session[:user_id] = 2 |
|
398 | 398 | delete :destroy, :id => 1 |
|
399 | 399 | assert_redirected_to :action => 'index', :project_id => 'ecookbook' |
|
400 | 400 | assert_equal I18n.t(:notice_successful_delete), flash[:notice] |
|
401 | 401 | assert_nil TimeEntry.find_by_id(1) |
|
402 | 402 | end |
|
403 | 403 | |
|
404 | 404 | def test_destroy_should_fail |
|
405 | 405 | # simulate that this fails (e.g. due to a plugin), see #5700 |
|
406 | 406 | TimeEntry.any_instance.expects(:destroy).returns(false) |
|
407 | 407 | |
|
408 | 408 | @request.session[:user_id] = 2 |
|
409 | 409 | delete :destroy, :id => 1 |
|
410 | 410 | assert_redirected_to :action => 'index', :project_id => 'ecookbook' |
|
411 | 411 | assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error] |
|
412 | 412 | assert_not_nil TimeEntry.find_by_id(1) |
|
413 | 413 | end |
|
414 | 414 | |
|
415 | 415 | def test_index_all_projects |
|
416 | 416 | get :index |
|
417 | 417 | assert_response :success |
|
418 | 418 | assert_template 'index' |
|
419 | 419 | assert_not_nil assigns(:total_hours) |
|
420 | 420 | assert_equal "162.90", "%.2f" % assigns(:total_hours) |
|
421 | 421 | assert_tag :form, |
|
422 | 422 | :attributes => {:action => "/time_entries", :id => 'query_form'} |
|
423 | 423 | end |
|
424 | 424 | |
|
425 | 425 | def test_index_all_projects_should_show_log_time_link |
|
426 | 426 | @request.session[:user_id] = 2 |
|
427 | 427 | get :index |
|
428 | 428 | assert_response :success |
|
429 | 429 | assert_template 'index' |
|
430 | 430 | assert_tag 'a', :attributes => {:href => '/time_entries/new'}, :content => /Log time/ |
|
431 | 431 | end |
|
432 | 432 | |
|
433 | 433 | def test_index_at_project_level |
|
434 | 434 | get :index, :project_id => 'ecookbook' |
|
435 | 435 | assert_response :success |
|
436 | 436 | assert_template 'index' |
|
437 | 437 | assert_not_nil assigns(:entries) |
|
438 | 438 | assert_equal 4, assigns(:entries).size |
|
439 | 439 | # project and subproject |
|
440 | 440 | assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort |
|
441 | 441 | assert_not_nil assigns(:total_hours) |
|
442 | 442 | assert_equal "162.90", "%.2f" % assigns(:total_hours) |
|
443 | 443 | # display all time by default |
|
444 | 444 | assert_nil assigns(:from) |
|
445 | 445 | assert_nil assigns(:to) |
|
446 | 446 | assert_tag :form, |
|
447 | 447 | :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} |
|
448 | 448 | end |
|
449 | 449 | |
|
450 | 450 | def test_index_at_project_level_with_date_range |
|
451 | 451 | get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30' |
|
452 | 452 | assert_response :success |
|
453 | 453 | assert_template 'index' |
|
454 | 454 | assert_not_nil assigns(:entries) |
|
455 | 455 | assert_equal 3, assigns(:entries).size |
|
456 | 456 | assert_not_nil assigns(:total_hours) |
|
457 | 457 | assert_equal "12.90", "%.2f" % assigns(:total_hours) |
|
458 | 458 | assert_equal '2007-03-20'.to_date, assigns(:from) |
|
459 | 459 | assert_equal '2007-04-30'.to_date, assigns(:to) |
|
460 | 460 | assert_tag :form, |
|
461 | 461 | :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} |
|
462 | 462 | end |
|
463 | 463 | |
|
464 | 464 | def test_index_at_project_level_with_period |
|
465 | 465 | get :index, :project_id => 'ecookbook', :period => '7_days' |
|
466 | 466 | assert_response :success |
|
467 | 467 | assert_template 'index' |
|
468 | 468 | assert_not_nil assigns(:entries) |
|
469 | 469 | assert_not_nil assigns(:total_hours) |
|
470 | 470 | assert_equal Date.today - 7, assigns(:from) |
|
471 | 471 | assert_equal Date.today, assigns(:to) |
|
472 | 472 | assert_tag :form, |
|
473 | 473 | :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} |
|
474 | 474 | end |
|
475 | 475 | |
|
476 | 476 | def test_index_one_day |
|
477 | 477 | get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => "2007-03-23" |
|
478 | 478 | assert_response :success |
|
479 | 479 | assert_template 'index' |
|
480 | 480 | assert_not_nil assigns(:total_hours) |
|
481 | 481 | assert_equal "4.25", "%.2f" % assigns(:total_hours) |
|
482 | 482 | assert_tag :form, |
|
483 | 483 | :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} |
|
484 | 484 | end |
|
485 | 485 | |
|
486 | 486 | def test_index_from_a_date |
|
487 | 487 | get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => "" |
|
488 | 488 | assert_equal '2007-03-23'.to_date, assigns(:from) |
|
489 | 489 | assert_nil assigns(:to) |
|
490 | 490 | end |
|
491 | 491 | |
|
492 | 492 | def test_index_to_a_date |
|
493 | 493 | get :index, :project_id => 'ecookbook', :from => "", :to => "2007-03-23" |
|
494 | 494 | assert_nil assigns(:from) |
|
495 | 495 | assert_equal '2007-03-23'.to_date, assigns(:to) |
|
496 | 496 | end |
|
497 | 497 | |
|
498 | 498 | def test_index_today |
|
499 | 499 | Date.stubs(:today).returns('2011-12-15'.to_date) |
|
500 | 500 | get :index, :period => 'today' |
|
501 | 501 | assert_equal '2011-12-15'.to_date, assigns(:from) |
|
502 | 502 | assert_equal '2011-12-15'.to_date, assigns(:to) |
|
503 | 503 | end |
|
504 | 504 | |
|
505 | 505 | def test_index_yesterday |
|
506 | 506 | Date.stubs(:today).returns('2011-12-15'.to_date) |
|
507 | 507 | get :index, :period => 'yesterday' |
|
508 | 508 | assert_equal '2011-12-14'.to_date, assigns(:from) |
|
509 | 509 | assert_equal '2011-12-14'.to_date, assigns(:to) |
|
510 | 510 | end |
|
511 | 511 | |
|
512 | 512 | def test_index_current_week |
|
513 | 513 | Date.stubs(:today).returns('2011-12-15'.to_date) |
|
514 | 514 | get :index, :period => 'current_week' |
|
515 | 515 | assert_equal '2011-12-12'.to_date, assigns(:from) |
|
516 | 516 | assert_equal '2011-12-18'.to_date, assigns(:to) |
|
517 | 517 | end |
|
518 | 518 | |
|
519 | 519 | def test_index_last_week |
|
520 | 520 | Date.stubs(:today).returns('2011-12-15'.to_date) |
|
521 | get :index, :period => 'current_week' | |
|
522 | assert_equal '2011-12-05'.to_date, assigns(:from) | |
|
523 | assert_equal '2011-12-11'.to_date, assigns(:to) | |
|
524 | end | |
|
525 | ||
|
526 | def test_index_last_week | |
|
527 | Date.stubs(:today).returns('2011-12-15'.to_date) | |
|
528 | 521 | get :index, :period => 'last_week' |
|
529 | 522 | assert_equal '2011-12-05'.to_date, assigns(:from) |
|
530 | 523 | assert_equal '2011-12-11'.to_date, assigns(:to) |
|
531 | 524 | end |
|
532 | 525 | |
|
533 | 526 | def test_index_7_days |
|
534 | 527 | Date.stubs(:today).returns('2011-12-15'.to_date) |
|
535 | 528 | get :index, :period => '7_days' |
|
536 | 529 | assert_equal '2011-12-08'.to_date, assigns(:from) |
|
537 | 530 | assert_equal '2011-12-15'.to_date, assigns(:to) |
|
538 | 531 | end |
|
539 | 532 | |
|
540 | 533 | def test_index_current_month |
|
541 | 534 | Date.stubs(:today).returns('2011-12-15'.to_date) |
|
542 | 535 | get :index, :period => 'current_month' |
|
543 | 536 | assert_equal '2011-12-01'.to_date, assigns(:from) |
|
544 | 537 | assert_equal '2011-12-31'.to_date, assigns(:to) |
|
545 | 538 | end |
|
546 | 539 | |
|
547 | 540 | def test_index_last_month |
|
548 | 541 | Date.stubs(:today).returns('2011-12-15'.to_date) |
|
549 | 542 | get :index, :period => 'last_month' |
|
550 | 543 | assert_equal '2011-11-01'.to_date, assigns(:from) |
|
551 | 544 | assert_equal '2011-11-30'.to_date, assigns(:to) |
|
552 | 545 | end |
|
553 | 546 | |
|
554 | 547 | def test_index_30_days |
|
555 | 548 | Date.stubs(:today).returns('2011-12-15'.to_date) |
|
556 | 549 | get :index, :period => '30_days' |
|
557 | 550 | assert_equal '2011-11-15'.to_date, assigns(:from) |
|
558 | 551 | assert_equal '2011-12-15'.to_date, assigns(:to) |
|
559 | 552 | end |
|
560 | 553 | |
|
561 | 554 | def test_index_current_year |
|
562 | 555 | Date.stubs(:today).returns('2011-12-15'.to_date) |
|
563 | 556 | get :index, :period => 'current_year' |
|
564 | 557 | assert_equal '2011-01-01'.to_date, assigns(:from) |
|
565 | 558 | assert_equal '2011-12-31'.to_date, assigns(:to) |
|
566 | 559 | end |
|
567 | 560 | |
|
568 | 561 | def test_index_at_issue_level |
|
569 | 562 | get :index, :issue_id => 1 |
|
570 | 563 | assert_response :success |
|
571 | 564 | assert_template 'index' |
|
572 | 565 | assert_not_nil assigns(:entries) |
|
573 | 566 | assert_equal 2, assigns(:entries).size |
|
574 | 567 | assert_not_nil assigns(:total_hours) |
|
575 | 568 | assert_equal 154.25, assigns(:total_hours) |
|
576 | 569 | # display all time |
|
577 | 570 | assert_nil assigns(:from) |
|
578 | 571 | assert_nil assigns(:to) |
|
579 | 572 | # TODO: remove /projects/:project_id/issues/:issue_id/time_entries routes |
|
580 | 573 | # to use /issues/:issue_id/time_entries |
|
581 | 574 | assert_tag :form, |
|
582 | 575 | :attributes => {:action => "/projects/ecookbook/issues/1/time_entries", :id => 'query_form'} |
|
583 | 576 | end |
|
584 | 577 | |
|
585 | 578 | def test_index_should_sort_by_spent_on_and_created_on |
|
586 | 579 | t1 = TimeEntry.create!(:user => User.find(1), :project => Project.find(1), :hours => 1, :spent_on => '2012-06-16', :created_on => '2012-06-16 20:00:00', :activity_id => 10) |
|
587 | 580 | t2 = TimeEntry.create!(:user => User.find(1), :project => Project.find(1), :hours => 1, :spent_on => '2012-06-16', :created_on => '2012-06-16 20:05:00', :activity_id => 10) |
|
588 | 581 | t3 = TimeEntry.create!(:user => User.find(1), :project => Project.find(1), :hours => 1, :spent_on => '2012-06-15', :created_on => '2012-06-16 20:10:00', :activity_id => 10) |
|
589 | 582 | |
|
590 | 583 | get :index, :project_id => 1, :from => '2012-06-15', :to => '2012-06-16' |
|
591 | 584 | assert_response :success |
|
592 | 585 | assert_equal [t2, t1, t3], assigns(:entries) |
|
593 | 586 | |
|
594 | 587 | get :index, :project_id => 1, :from => '2012-06-15', :to => '2012-06-16', :sort => 'spent_on' |
|
595 | 588 | assert_response :success |
|
596 | 589 | assert_equal [t3, t1, t2], assigns(:entries) |
|
597 | 590 | end |
|
598 | 591 | |
|
599 | 592 | def test_index_atom_feed |
|
600 | 593 | get :index, :project_id => 1, :format => 'atom' |
|
601 | 594 | assert_response :success |
|
602 | 595 | assert_equal 'application/atom+xml', @response.content_type |
|
603 | 596 | assert_not_nil assigns(:items) |
|
604 | 597 | assert assigns(:items).first.is_a?(TimeEntry) |
|
605 | 598 | end |
|
606 | 599 | |
|
607 | 600 | def test_index_all_projects_csv_export |
|
608 | 601 | Setting.date_format = '%m/%d/%Y' |
|
609 | 602 | get :index, :format => 'csv' |
|
610 | 603 | assert_response :success |
|
611 | 604 | assert_equal 'text/csv; header=present', @response.content_type |
|
612 | 605 | assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n") |
|
613 | 606 | assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n") |
|
614 | 607 | end |
|
615 | 608 | |
|
616 | 609 | def test_index_csv_export |
|
617 | 610 | Setting.date_format = '%m/%d/%Y' |
|
618 | 611 | get :index, :project_id => 1, :format => 'csv' |
|
619 | 612 | assert_response :success |
|
620 | 613 | assert_equal 'text/csv; header=present', @response.content_type |
|
621 | 614 | assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n") |
|
622 | 615 | assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n") |
|
623 | 616 | end |
|
624 | 617 | |
|
625 | 618 | def test_index_csv_export_with_multi_custom_field |
|
626 | 619 | field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'list', |
|
627 | 620 | :multiple => true, :possible_values => ['value1', 'value2']) |
|
628 | 621 | entry = TimeEntry.find(1) |
|
629 | 622 | entry.custom_field_values = {field.id => ['value1', 'value2']} |
|
630 | 623 | entry.save! |
|
631 | 624 | |
|
632 | 625 | get :index, :project_id => 1, :format => 'csv' |
|
633 | 626 | assert_response :success |
|
634 | 627 | assert_include '"value1, value2"', @response.body |
|
635 | 628 | end |
|
636 | 629 | |
|
637 | 630 | def test_csv_big_5 |
|
638 | 631 | user = User.find_by_id(3) |
|
639 | 632 | user.language = "zh-TW" |
|
640 | 633 | assert user.save |
|
641 | 634 | str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88" |
|
642 | 635 | str_big5 = "\xa4@\xa4\xeb" |
|
643 | 636 | if str_utf8.respond_to?(:force_encoding) |
|
644 | 637 | str_utf8.force_encoding('UTF-8') |
|
645 | 638 | str_big5.force_encoding('Big5') |
|
646 | 639 | end |
|
647 | 640 | @request.session[:user_id] = 3 |
|
648 | 641 | post :create, :project_id => 1, |
|
649 | 642 | :time_entry => {:comments => str_utf8, |
|
650 | 643 | # Not the default activity |
|
651 | 644 | :activity_id => '11', |
|
652 | 645 | :issue_id => '', |
|
653 | 646 | :spent_on => '2011-11-10', |
|
654 | 647 | :hours => '7.3'} |
|
655 | 648 | assert_redirected_to :action => 'index', :project_id => 'ecookbook' |
|
656 | 649 | |
|
657 | 650 | t = TimeEntry.find_by_comments(str_utf8) |
|
658 | 651 | assert_not_nil t |
|
659 | 652 | assert_equal 11, t.activity_id |
|
660 | 653 | assert_equal 7.3, t.hours |
|
661 | 654 | assert_equal 3, t.user_id |
|
662 | 655 | |
|
663 | 656 | get :index, :project_id => 1, :format => 'csv', |
|
664 | 657 | :from => '2011-11-10', :to => '2011-11-10' |
|
665 | 658 | assert_response :success |
|
666 | 659 | assert_equal 'text/csv; header=present', @response.content_type |
|
667 | 660 | ar = @response.body.chomp.split("\n") |
|
668 | 661 | s1 = "\xa4\xe9\xb4\xc1" |
|
669 | 662 | if str_utf8.respond_to?(:force_encoding) |
|
670 | 663 | s1.force_encoding('Big5') |
|
671 | 664 | end |
|
672 | 665 | assert ar[0].include?(s1) |
|
673 | 666 | assert ar[1].include?(str_big5) |
|
674 | 667 | end |
|
675 | 668 | |
|
676 | 669 | def test_csv_cannot_convert_should_be_replaced_big_5 |
|
677 | 670 | user = User.find_by_id(3) |
|
678 | 671 | user.language = "zh-TW" |
|
679 | 672 | assert user.save |
|
680 | 673 | str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85" |
|
681 | 674 | if str_utf8.respond_to?(:force_encoding) |
|
682 | 675 | str_utf8.force_encoding('UTF-8') |
|
683 | 676 | end |
|
684 | 677 | @request.session[:user_id] = 3 |
|
685 | 678 | post :create, :project_id => 1, |
|
686 | 679 | :time_entry => {:comments => str_utf8, |
|
687 | 680 | # Not the default activity |
|
688 | 681 | :activity_id => '11', |
|
689 | 682 | :issue_id => '', |
|
690 | 683 | :spent_on => '2011-11-10', |
|
691 | 684 | :hours => '7.3'} |
|
692 | 685 | assert_redirected_to :action => 'index', :project_id => 'ecookbook' |
|
693 | 686 | |
|
694 | 687 | t = TimeEntry.find_by_comments(str_utf8) |
|
695 | 688 | assert_not_nil t |
|
696 | 689 | assert_equal 11, t.activity_id |
|
697 | 690 | assert_equal 7.3, t.hours |
|
698 | 691 | assert_equal 3, t.user_id |
|
699 | 692 | |
|
700 | 693 | get :index, :project_id => 1, :format => 'csv', |
|
701 | 694 | :from => '2011-11-10', :to => '2011-11-10' |
|
702 | 695 | assert_response :success |
|
703 | 696 | assert_equal 'text/csv; header=present', @response.content_type |
|
704 | 697 | ar = @response.body.chomp.split("\n") |
|
705 | 698 | s1 = "\xa4\xe9\xb4\xc1" |
|
706 | 699 | if str_utf8.respond_to?(:force_encoding) |
|
707 | 700 | s1.force_encoding('Big5') |
|
708 | 701 | end |
|
709 | 702 | assert ar[0].include?(s1) |
|
710 | 703 | s2 = ar[1].split(",")[8] |
|
711 | 704 | if s2.respond_to?(:force_encoding) |
|
712 | 705 | s3 = "\xa5H?" |
|
713 | 706 | s3.force_encoding('Big5') |
|
714 | 707 | assert_equal s3, s2 |
|
715 | 708 | elsif RUBY_PLATFORM == 'java' |
|
716 | 709 | assert_equal "??", s2 |
|
717 | 710 | else |
|
718 | 711 | assert_equal "\xa5H???", s2 |
|
719 | 712 | end |
|
720 | 713 | end |
|
721 | 714 | |
|
722 | 715 | def test_csv_tw |
|
723 | 716 | with_settings :default_language => "zh-TW" do |
|
724 | 717 | str1 = "test_csv_tw" |
|
725 | 718 | user = User.find_by_id(3) |
|
726 | 719 | te1 = TimeEntry.create(:spent_on => '2011-11-10', |
|
727 | 720 | :hours => 999.9, |
|
728 | 721 | :project => Project.find(1), |
|
729 | 722 | :user => user, |
|
730 | 723 | :activity => TimeEntryActivity.find_by_name('Design'), |
|
731 | 724 | :comments => str1) |
|
732 | 725 | te2 = TimeEntry.find_by_comments(str1) |
|
733 | 726 | assert_not_nil te2 |
|
734 | 727 | assert_equal 999.9, te2.hours |
|
735 | 728 | assert_equal 3, te2.user_id |
|
736 | 729 | |
|
737 | 730 | get :index, :project_id => 1, :format => 'csv', |
|
738 | 731 | :from => '2011-11-10', :to => '2011-11-10' |
|
739 | 732 | assert_response :success |
|
740 | 733 | assert_equal 'text/csv; header=present', @response.content_type |
|
741 | 734 | |
|
742 | 735 | ar = @response.body.chomp.split("\n") |
|
743 | 736 | s2 = ar[1].split(",")[7] |
|
744 | 737 | assert_equal '999.9', s2 |
|
745 | 738 | |
|
746 | 739 | str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)" |
|
747 | 740 | if str_tw.respond_to?(:force_encoding) |
|
748 | 741 | str_tw.force_encoding('UTF-8') |
|
749 | 742 | end |
|
750 | 743 | assert_equal str_tw, l(:general_lang_name) |
|
751 | 744 | assert_equal ',', l(:general_csv_separator) |
|
752 | 745 | assert_equal '.', l(:general_csv_decimal_separator) |
|
753 | 746 | end |
|
754 | 747 | end |
|
755 | 748 | |
|
756 | 749 | def test_csv_fr |
|
757 | 750 | with_settings :default_language => "fr" do |
|
758 | 751 | str1 = "test_csv_fr" |
|
759 | 752 | user = User.find_by_id(3) |
|
760 | 753 | te1 = TimeEntry.create(:spent_on => '2011-11-10', |
|
761 | 754 | :hours => 999.9, |
|
762 | 755 | :project => Project.find(1), |
|
763 | 756 | :user => user, |
|
764 | 757 | :activity => TimeEntryActivity.find_by_name('Design'), |
|
765 | 758 | :comments => str1) |
|
766 | 759 | te2 = TimeEntry.find_by_comments(str1) |
|
767 | 760 | assert_not_nil te2 |
|
768 | 761 | assert_equal 999.9, te2.hours |
|
769 | 762 | assert_equal 3, te2.user_id |
|
770 | 763 | |
|
771 | 764 | get :index, :project_id => 1, :format => 'csv', |
|
772 | 765 | :from => '2011-11-10', :to => '2011-11-10' |
|
773 | 766 | assert_response :success |
|
774 | 767 | assert_equal 'text/csv; header=present', @response.content_type |
|
775 | 768 | |
|
776 | 769 | ar = @response.body.chomp.split("\n") |
|
777 | 770 | s2 = ar[1].split(";")[7] |
|
778 | 771 | assert_equal '999,9', s2 |
|
779 | 772 | |
|
780 | 773 | str_fr = "Fran\xc3\xa7ais" |
|
781 | 774 | if str_fr.respond_to?(:force_encoding) |
|
782 | 775 | str_fr.force_encoding('UTF-8') |
|
783 | 776 | end |
|
784 | 777 | assert_equal str_fr, l(:general_lang_name) |
|
785 | 778 | assert_equal ';', l(:general_csv_separator) |
|
786 | 779 | assert_equal ',', l(:general_csv_decimal_separator) |
|
787 | 780 | end |
|
788 | 781 | end |
|
789 | 782 | end |
General Comments 0
You need to be logged in to leave comments.
Login now