##// END OF EJS Templates
Random test failure....
Jean-Philippe Lang -
r15527:06d66df16e7c
parent child
Show More
@@ -1,1018 +1,1018
1 1 # -*- coding: utf-8 -*-
2 2 # Redmine - project management software
3 3 # Copyright (C) 2006-2016 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
21 21 class TimelogControllerTest < Redmine::ControllerTest
22 22 fixtures :projects, :enabled_modules, :roles, :members,
23 23 :member_roles, :issues, :time_entries, :users,
24 24 :trackers, :enumerations, :issue_statuses,
25 25 :custom_fields, :custom_values,
26 26 :projects_trackers, :custom_fields_trackers,
27 27 :custom_fields_projects
28 28
29 29 include Redmine::I18n
30 30
31 31 def test_new
32 32 @request.session[:user_id] = 3
33 33 get :new
34 34 assert_response :success
35 35
36 36 assert_select 'input[name=?][type=hidden]', 'project_id', 0
37 37 assert_select 'input[name=?][type=hidden]', 'issue_id', 0
38 38 assert_select 'select[name=?]', 'time_entry[project_id]' do
39 39 # blank option for project
40 40 assert_select 'option[value=""]'
41 41 end
42 42 end
43 43
44 44 def test_new_with_project_id
45 45 @request.session[:user_id] = 3
46 46 get :new, :params => {:project_id => 1}
47 47 assert_response :success
48 48
49 49 assert_select 'input[name=?][type=hidden]', 'project_id'
50 50 assert_select 'input[name=?][type=hidden]', 'issue_id', 0
51 51 assert_select 'select[name=?]', 'time_entry[project_id]', 0
52 52 end
53 53
54 54 def test_new_with_issue_id
55 55 @request.session[:user_id] = 3
56 56 get :new, :params => {:issue_id => 2}
57 57 assert_response :success
58 58
59 59 assert_select 'input[name=?][type=hidden]', 'project_id', 0
60 60 assert_select 'input[name=?][type=hidden]', 'issue_id'
61 61 assert_select 'select[name=?]', 'time_entry[project_id]', 0
62 62 end
63 63
64 64 def test_new_without_project_should_prefill_the_form
65 65 @request.session[:user_id] = 3
66 66 get :new, :params => {:time_entry => {:project_id => '1'}}
67 67 assert_response :success
68 68
69 69 assert_select 'select[name=?]', 'time_entry[project_id]' do
70 70 assert_select 'option[value="1"][selected=selected]'
71 71 end
72 72 end
73 73
74 74 def test_new_without_project_should_deny_without_permission
75 75 Role.all.each {|role| role.remove_permission! :log_time}
76 76 @request.session[:user_id] = 3
77 77
78 78 get :new
79 79 assert_response 403
80 80 end
81 81
82 82 def test_new_should_select_default_activity
83 83 @request.session[:user_id] = 3
84 84 get :new, :params => {:project_id => 1}
85 85 assert_response :success
86 86 assert_select 'select[name=?]', 'time_entry[activity_id]' do
87 87 assert_select 'option[selected=selected]', :text => 'Development'
88 88 end
89 89 end
90 90
91 91 def test_new_should_only_show_active_time_entry_activities
92 92 @request.session[:user_id] = 3
93 93 get :new, :params => {:project_id => 1}
94 94 assert_response :success
95 95 assert_select 'option', :text => 'Inactive Activity', :count => 0
96 96 end
97 97
98 98 def test_post_new_as_js_should_update_activity_options
99 99 @request.session[:user_id] = 3
100 100 post :new, :params => {:time_entry => {:project_id => 1}, :format => 'js'}
101 101 assert_response :success
102 102 assert_include '#time_entry_activity_id', response.body
103 103 end
104 104
105 105 def test_get_edit_existing_time
106 106 @request.session[:user_id] = 2
107 107 get :edit, :params => {:id => 2, :project_id => nil}
108 108 assert_response :success
109 109
110 110 assert_select 'form[action=?]', '/time_entries/2'
111 111 end
112 112
113 113 def test_get_edit_with_an_existing_time_entry_with_inactive_activity
114 114 te = TimeEntry.find(1)
115 115 te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
116 116 te.save!(:validate => false)
117 117
118 118 @request.session[:user_id] = 1
119 119 get :edit, :params => {:project_id => 1, :id => 1}
120 120 assert_response :success
121 121
122 122 # Blank option since nothing is pre-selected
123 123 assert_select 'option', :text => '--- Please select ---'
124 124 end
125 125
126 126 def test_post_create
127 127 @request.session[:user_id] = 3
128 128 assert_difference 'TimeEntry.count' do
129 129 post :create, :params => {
130 130 :project_id => 1,
131 131 :time_entry => {:comments => 'Some work on TimelogControllerTest',
132 132 # Not the default activity
133 133 :activity_id => '11',
134 134 :spent_on => '2008-03-14',
135 135 :issue_id => '1',
136 136 :hours => '7.3'
137 137 }
138 138 }
139 139 assert_redirected_to '/projects/ecookbook/time_entries'
140 140 end
141 141
142 142 t = TimeEntry.order('id DESC').first
143 143 assert_not_nil t
144 144 assert_equal 'Some work on TimelogControllerTest', t.comments
145 145 assert_equal 1, t.project_id
146 146 assert_equal 1, t.issue_id
147 147 assert_equal 11, t.activity_id
148 148 assert_equal 7.3, t.hours
149 149 assert_equal 3, t.user_id
150 150 end
151 151
152 152 def test_post_create_with_blank_issue
153 153 @request.session[:user_id] = 3
154 154 assert_difference 'TimeEntry.count' do
155 155 post :create, :params => {
156 156 :project_id => 1,
157 157 :time_entry => {
158 158 :comments => 'Some work on TimelogControllerTest',
159 159 # Not the default activity
160 160 :activity_id => '11',
161 161 :issue_id => '',
162 162 :spent_on => '2008-03-14',
163 163 :hours => '7.3'
164 164 }
165 165 }
166 166 assert_redirected_to '/projects/ecookbook/time_entries'
167 167 end
168 168
169 169 t = TimeEntry.order('id DESC').first
170 170 assert_not_nil t
171 171 assert_equal 'Some work on TimelogControllerTest', t.comments
172 172 assert_equal 1, t.project_id
173 173 assert_nil t.issue_id
174 174 assert_equal 11, t.activity_id
175 175 assert_equal 7.3, t.hours
176 176 assert_equal 3, t.user_id
177 177 end
178 178
179 179 def test_create_on_project_with_time_tracking_disabled_should_fail
180 180 Project.find(1).disable_module! :time_tracking
181 181
182 182 @request.session[:user_id] = 2
183 183 assert_no_difference 'TimeEntry.count' do
184 184 post :create, :params => {
185 185 :time_entry => {
186 186 :project_id => '1', :issue_id => '',
187 187 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
188 188 }
189 189 }
190 190 end
191 191 end
192 192
193 193 def test_create_on_project_without_permission_should_fail
194 194 Role.find(1).remove_permission! :log_time
195 195
196 196 @request.session[:user_id] = 2
197 197 assert_no_difference 'TimeEntry.count' do
198 198 post :create, :params => {
199 199 :time_entry => {
200 200 :project_id => '1', :issue_id => '',
201 201 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
202 202 }
203 203 }
204 204 end
205 205 end
206 206
207 207 def test_create_on_issue_in_project_with_time_tracking_disabled_should_fail
208 208 Project.find(1).disable_module! :time_tracking
209 209
210 210 @request.session[:user_id] = 2
211 211 assert_no_difference 'TimeEntry.count' do
212 212 post :create, :params => {
213 213 :time_entry => {
214 214 :project_id => '', :issue_id => '1',
215 215 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
216 216 }
217 217 }
218 218 assert_select_error /Issue is invalid/
219 219 end
220 220 end
221 221
222 222 def test_create_on_issue_in_project_without_permission_should_fail
223 223 Role.find(1).remove_permission! :log_time
224 224
225 225 @request.session[:user_id] = 2
226 226 assert_no_difference 'TimeEntry.count' do
227 227 post :create, :params => {
228 228 :time_entry => {
229 229 :project_id => '', :issue_id => '1',
230 230 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
231 231 }
232 232 }
233 233 assert_select_error /Issue is invalid/
234 234 end
235 235 end
236 236
237 237 def test_create_on_issue_that_is_not_visible_should_not_disclose_subject
238 238 issue = Issue.generate!(:subject => "issue_that_is_not_visible", :is_private => true)
239 239 assert !issue.visible?(User.find(3))
240 240
241 241 @request.session[:user_id] = 3
242 242 assert_no_difference 'TimeEntry.count' do
243 243 post :create, :params => {
244 244 :time_entry => {
245 245 :project_id => '', :issue_id => issue.id.to_s,
246 246 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
247 247 }
248 248 }
249 249 end
250 250 assert_select_error /Issue is invalid/
251 251 assert_select "input[name=?][value=?]", "time_entry[issue_id]", issue.id.to_s
252 252 assert_select "#time_entry_issue", 0
253 253 assert !response.body.include?('issue_that_is_not_visible')
254 254 end
255 255
256 256 def test_create_and_continue_at_project_level
257 257 @request.session[:user_id] = 2
258 258 assert_difference 'TimeEntry.count' do
259 259 post :create, :params => {
260 260 :time_entry => {
261 261 :project_id => '1',
262 262 :activity_id => '11',
263 263 :issue_id => '',
264 264 :spent_on => '2008-03-14',
265 265 :hours => '7.3'
266 266 },
267 267 :continue => '1'
268 268 }
269 269 assert_redirected_to '/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=&time_entry%5Bproject_id%5D=1'
270 270 end
271 271 end
272 272
273 273 def test_create_and_continue_at_issue_level
274 274 @request.session[:user_id] = 2
275 275 assert_difference 'TimeEntry.count' do
276 276 post :create, :params => {
277 277 :time_entry => {
278 278 :project_id => '',
279 279 :activity_id => '11',
280 280 :issue_id => '1',
281 281 :spent_on => '2008-03-14',
282 282 :hours => '7.3'
283 283 },
284 284 :continue => '1'
285 285 }
286 286 assert_redirected_to '/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=1&time_entry%5Bproject_id%5D='
287 287 end
288 288 end
289 289
290 290 def test_create_and_continue_with_project_id
291 291 @request.session[:user_id] = 2
292 292 assert_difference 'TimeEntry.count' do
293 293 post :create, :params => {
294 294 :project_id => 1,
295 295 :time_entry => {
296 296 :activity_id => '11',
297 297 :issue_id => '',
298 298 :spent_on => '2008-03-14',
299 299 :hours => '7.3'
300 300 },
301 301 :continue => '1'
302 302 }
303 303 assert_redirected_to '/projects/ecookbook/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=&time_entry%5Bproject_id%5D='
304 304 end
305 305 end
306 306
307 307 def test_create_and_continue_with_issue_id
308 308 @request.session[:user_id] = 2
309 309 assert_difference 'TimeEntry.count' do
310 310 post :create, :params => {
311 311 :issue_id => 1,
312 312 :time_entry => {
313 313 :activity_id => '11',
314 314 :issue_id => '1',
315 315 :spent_on => '2008-03-14',
316 316 :hours => '7.3'
317 317 },
318 318 :continue => '1'
319 319 }
320 320 assert_redirected_to '/issues/1/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=1&time_entry%5Bproject_id%5D='
321 321 end
322 322 end
323 323
324 324 def test_create_without_log_time_permission_should_be_denied
325 325 @request.session[:user_id] = 2
326 326 Role.find_by_name('Manager').remove_permission! :log_time
327 327 post :create, :params => {
328 328 :project_id => 1,
329 329 :time_entry => {
330 330 :activity_id => '11',
331 331 :issue_id => '',
332 332 :spent_on => '2008-03-14',
333 333 :hours => '7.3'
334 334 }
335 335 }
336 336 assert_response 403
337 337 end
338 338
339 339 def test_create_without_project_and_issue_should_fail
340 340 @request.session[:user_id] = 2
341 341 post :create, :params => {:time_entry => {:issue_id => ''}}
342 342
343 343 assert_response :success
344 344 assert_select_error /Project cannot be blank/
345 345 end
346 346
347 347 def test_create_with_failure
348 348 @request.session[:user_id] = 2
349 349 post :create, :params => {
350 350 :project_id => 1,
351 351 :time_entry => {
352 352 :activity_id => '',
353 353 :issue_id => '',
354 354 :spent_on => '2008-03-14',
355 355 :hours => '7.3'
356 356 }
357 357 }
358 358 assert_response :success
359 359 end
360 360
361 361 def test_create_without_project
362 362 @request.session[:user_id] = 2
363 363 assert_difference 'TimeEntry.count' do
364 364 post :create, :params => {
365 365 :time_entry => {
366 366 :project_id => '1',
367 367 :activity_id => '11',
368 368 :issue_id => '',
369 369 :spent_on => '2008-03-14',
370 370 :hours => '7.3'
371 371 }
372 372 }
373 373 end
374 374
375 375 assert_redirected_to '/projects/ecookbook/time_entries'
376 376 time_entry = TimeEntry.order('id DESC').first
377 377 assert_equal 1, time_entry.project_id
378 378 end
379 379
380 380 def test_create_without_project_should_fail_with_issue_not_inside_project
381 381 @request.session[:user_id] = 2
382 382 assert_no_difference 'TimeEntry.count' do
383 383 post :create, :params => {
384 384 :time_entry => {
385 385 :project_id => '1',
386 386 :activity_id => '11',
387 387 :issue_id => '5',
388 388 :spent_on => '2008-03-14',
389 389 :hours => '7.3'
390 390 }
391 391 }
392 392 end
393 393
394 394 assert_response :success
395 395 assert_select_error /Issue is invalid/
396 396 end
397 397
398 398 def test_create_without_project_should_deny_without_permission
399 399 @request.session[:user_id] = 2
400 400 Project.find(3).disable_module!(:time_tracking)
401 401
402 402 assert_no_difference 'TimeEntry.count' do
403 403 post :create, :params => {
404 404 :time_entry => {
405 405 :project_id => '3',
406 406 :activity_id => '11',
407 407 :issue_id => '',
408 408 :spent_on => '2008-03-14',
409 409 :hours => '7.3'
410 410 }
411 411 }
412 412 end
413 413
414 414 assert_response 403
415 415 end
416 416
417 417 def test_create_without_project_with_failure
418 418 @request.session[:user_id] = 2
419 419 assert_no_difference 'TimeEntry.count' do
420 420 post :create, :params => {
421 421 :time_entry => {
422 422 :project_id => '1',
423 423 :activity_id => '11',
424 424 :issue_id => '',
425 425 :spent_on => '2008-03-14',
426 426 :hours => ''
427 427 }
428 428 }
429 429 end
430 430
431 431 assert_response :success
432 432 assert_select 'select[name=?]', 'time_entry[project_id]' do
433 433 assert_select 'option[value="1"][selected=selected]'
434 434 end
435 435 end
436 436
437 437 def test_update
438 438 entry = TimeEntry.find(1)
439 439 assert_equal 1, entry.issue_id
440 440 assert_equal 2, entry.user_id
441 441
442 442 @request.session[:user_id] = 1
443 443 put :update, :params => {
444 444 :id => 1,
445 445 :time_entry => {
446 446 :issue_id => '2',
447 447 :hours => '8'
448 448 }
449 449 }
450 450 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
451 451 entry.reload
452 452
453 453 assert_equal 8, entry.hours
454 454 assert_equal 2, entry.issue_id
455 455 assert_equal 2, entry.user_id
456 456 end
457 457
458 458 def test_update_should_allow_to_change_issue_to_another_project
459 459 entry = TimeEntry.generate!(:issue_id => 1)
460 460
461 461 @request.session[:user_id] = 1
462 462 put :update, :params => {
463 463 :id => entry.id,
464 464 :time_entry => {
465 465 :issue_id => '5'
466 466 }
467 467 }
468 468 assert_response 302
469 469 entry.reload
470 470
471 471 assert_equal 5, entry.issue_id
472 472 assert_equal 3, entry.project_id
473 473 end
474 474
475 475 def test_update_should_not_allow_to_change_issue_to_an_invalid_project
476 476 entry = TimeEntry.generate!(:issue_id => 1)
477 477 Project.find(3).disable_module!(:time_tracking)
478 478
479 479 @request.session[:user_id] = 1
480 480 put :update, :params => {
481 481 :id => entry.id,
482 482 :time_entry => {
483 483 :issue_id => '5'
484 484 }
485 485 }
486 486 assert_response :success
487 487 assert_select_error /Issue is invalid/
488 488 end
489 489
490 490 def test_get_bulk_edit
491 491 @request.session[:user_id] = 2
492 492
493 493 get :bulk_edit, :params => {:ids => [1, 2]}
494 494 assert_response :success
495 495
496 496 assert_select 'ul#bulk-selection' do
497 497 assert_select 'li', 2
498 498 assert_select 'li a', :text => '03/23/2007 - eCookbook: 4.25 hours'
499 499 end
500 500
501 501 assert_select 'form#bulk_edit_form[action=?]', '/time_entries/bulk_update' do
502 502 # System wide custom field
503 503 assert_select 'select[name=?]', 'time_entry[custom_field_values][10]'
504 504
505 505 # Activities
506 506 assert_select 'select[name=?]', 'time_entry[activity_id]' do
507 507 assert_select 'option[value=""]', :text => '(No change)'
508 508 assert_select 'option[value="9"]', :text => 'Design'
509 509 end
510 510 end
511 511 end
512 512
513 513 def test_get_bulk_edit_on_different_projects
514 514 @request.session[:user_id] = 2
515 515
516 516 get :bulk_edit, :params => {:ids => [1, 2, 6]}
517 517 assert_response :success
518 518 end
519 519
520 520 def test_bulk_edit_with_edit_own_time_entries_permission
521 521 @request.session[:user_id] = 2
522 522 Role.find_by_name('Manager').remove_permission! :edit_time_entries
523 523 Role.find_by_name('Manager').add_permission! :edit_own_time_entries
524 524 ids = (0..1).map {TimeEntry.generate!(:user => User.find(2)).id}
525 525
526 526 get :bulk_edit, :params => {:ids => ids}
527 527 assert_response :success
528 528 end
529 529
530 530 def test_bulk_update
531 531 @request.session[:user_id] = 2
532 532 # update time entry activity
533 533 post :bulk_update, :params => {:ids => [1, 2], :time_entry => { :activity_id => 9}}
534 534
535 535 assert_response 302
536 536 # check that the issues were updated
537 537 assert_equal [9, 9], TimeEntry.where(:id => [1, 2]).collect {|i| i.activity_id}
538 538 end
539 539
540 540 def test_bulk_update_with_failure
541 541 @request.session[:user_id] = 2
542 542 post :bulk_update, :params => {:ids => [1, 2], :time_entry => { :hours => 'A'}}
543 543
544 544 assert_response 302
545 545 assert_match /Failed to save 2 time entrie/, flash[:error]
546 546 end
547 547
548 548 def test_bulk_update_on_different_projects
549 549 @request.session[:user_id] = 2
550 550 # makes user a manager on the other project
551 551 Member.create!(:user_id => 2, :project_id => 3, :role_ids => [1])
552 552
553 553 # update time entry activity
554 554 post :bulk_update, :params => {:ids => [1, 2, 4], :time_entry => { :activity_id => 9 }}
555 555
556 556 assert_response 302
557 557 # check that the issues were updated
558 558 assert_equal [9, 9, 9], TimeEntry.where(:id => [1, 2, 4]).collect {|i| i.activity_id}
559 559 end
560 560
561 561 def test_bulk_update_on_different_projects_without_rights
562 562 @request.session[:user_id] = 3
563 563 user = User.find(3)
564 564 action = { :controller => "timelog", :action => "bulk_update" }
565 565 assert user.allowed_to?(action, TimeEntry.find(1).project)
566 566 assert ! user.allowed_to?(action, TimeEntry.find(5).project)
567 567
568 568 post :bulk_update, :params => {:ids => [1, 5], :time_entry => { :activity_id => 9 }}
569 569 assert_response 403
570 570 end
571 571
572 572 def test_bulk_update_with_edit_own_time_entries_permission
573 573 @request.session[:user_id] = 2
574 574 Role.find_by_name('Manager').remove_permission! :edit_time_entries
575 575 Role.find_by_name('Manager').add_permission! :edit_own_time_entries
576 576 ids = (0..1).map {TimeEntry.generate!(:user => User.find(2)).id}
577 577
578 578 post :bulk_update, :params => {:ids => ids, :time_entry => { :activity_id => 9 }}
579 579 assert_response 302
580 580 end
581 581
582 582 def test_bulk_update_with_edit_own_time_entries_permissions_should_be_denied_for_time_entries_of_other_user
583 583 @request.session[:user_id] = 2
584 584 Role.find_by_name('Manager').remove_permission! :edit_time_entries
585 585 Role.find_by_name('Manager').add_permission! :edit_own_time_entries
586 586
587 587 post :bulk_update, :params => {:ids => [1, 2], :time_entry => { :activity_id => 9 }}
588 588 assert_response 403
589 589 end
590 590
591 591 def test_bulk_update_custom_field
592 592 @request.session[:user_id] = 2
593 593 post :bulk_update, :params => {:ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }}
594 594
595 595 assert_response 302
596 596 assert_equal ["0", "0"], TimeEntry.where(:id => [1, 2]).collect {|i| i.custom_value_for(10).value}
597 597 end
598 598
599 599 def test_bulk_update_clear_custom_field
600 600 field = TimeEntryCustomField.generate!(:field_format => 'string')
601 601 @request.session[:user_id] = 2
602 602 post :bulk_update, :params => {:ids => [1, 2], :time_entry => { :custom_field_values => {field.id.to_s => '__none__'} }}
603 603
604 604 assert_response 302
605 605 assert_equal ["", ""], TimeEntry.where(:id => [1, 2]).collect {|i| i.custom_value_for(field).value}
606 606 end
607 607
608 608 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
609 609 @request.session[:user_id] = 2
610 610 post :bulk_update, :params => {:ids => [1,2], :back_url => '/time_entries'}
611 611
612 612 assert_response :redirect
613 613 assert_redirected_to '/time_entries'
614 614 end
615 615
616 616 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
617 617 @request.session[:user_id] = 2
618 618 post :bulk_update, :params => {:ids => [1,2], :back_url => 'http://google.com'}
619 619
620 620 assert_response :redirect
621 621 assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
622 622 end
623 623
624 624 def test_post_bulk_update_without_edit_permission_should_be_denied
625 625 @request.session[:user_id] = 2
626 626 Role.find_by_name('Manager').remove_permission! :edit_time_entries
627 627
628 628 post :bulk_update, :params => {:ids => [1,2]}
629 629 assert_response 403
630 630 end
631 631
632 632 def test_destroy
633 633 @request.session[:user_id] = 2
634 634
635 635 delete :destroy, :params => {:id => 1}
636 636 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
637 637 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
638 638 assert_nil TimeEntry.find_by_id(1)
639 639 end
640 640
641 641 def test_destroy_should_fail
642 642 # simulate that this fails (e.g. due to a plugin), see #5700
643 643 TimeEntry.any_instance.expects(:destroy).returns(false)
644 644 @request.session[:user_id] = 2
645 645
646 646 delete :destroy, :params => {:id => 1}
647 647 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
648 648 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
649 649 assert_not_nil TimeEntry.find_by_id(1)
650 650 end
651 651
652 652 def test_index_all_projects
653 653 get :index
654 654 assert_response :success
655 655
656 656 assert_select '.total-for-hours', :text => 'Hours: 162.90'
657 657 assert_select 'form#query_form[action=?]', '/time_entries'
658 658 end
659 659
660 660 def test_index_all_projects_should_show_log_time_link
661 661 @request.session[:user_id] = 2
662 662 get :index
663 663 assert_response :success
664 664
665 665 assert_select 'a[href=?]', '/time_entries/new', :text => /Log time/
666 666 end
667 667
668 668 def test_index_my_spent_time
669 669 @request.session[:user_id] = 2
670 670 get :index, :params => {:user_id => 'me', :c => ['user']}
671 671 assert_response :success
672 672
673 673 users = css_select('table.time-entries tbody td.user').map(&:text).uniq
674 674 assert_equal ["John Smith"], users
675 675 end
676 676
677 677 def test_index_at_project_level
678 678 get :index, :params => {:project_id => 'ecookbook', :c => ['project']}
679 679 assert_response :success
680 680
681 681 assert_select 'tr.time-entry', 4
682 682
683 683 # project and subproject
684 684 projects = css_select('table.time-entries tbody td.project').map(&:text).uniq.sort
685 685 assert_equal ["eCookbook", "eCookbook Subproject 1"], projects
686 686
687 687 assert_select '.total-for-hours', :text => 'Hours: 162.90'
688 688 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
689 689 end
690 690
691 691 def test_index_with_display_subprojects_issues_to_false_should_not_include_subproject_entries
692 692 entry = TimeEntry.generate!(:project => Project.find(3))
693 693
694 694 with_settings :display_subprojects_issues => '0' do
695 695 get :index, :params => {:project_id => 'ecookbook', :c => ['project']}
696 696 assert_response :success
697 697
698 698 projects = css_select('table.time-entries tbody td.project').map(&:text).uniq.sort
699 699 assert_equal ["eCookbook"], projects
700 700 end
701 701 end
702 702
703 703 def test_index_with_display_subprojects_issues_to_false_and_subproject_filter_should_include_subproject_entries
704 704 entry = TimeEntry.generate!(:project => Project.find(3))
705 705
706 706 with_settings :display_subprojects_issues => '0' do
707 707 get :index, :params => {:project_id => 'ecookbook', :c => ['project'], :subproject_id => 3}
708 708 assert_response :success
709 709
710 710 projects = css_select('table.time-entries tbody td.project').map(&:text).uniq.sort
711 711 assert_equal ["eCookbook", "eCookbook Subproject 1"], projects
712 712 end
713 713 end
714 714
715 715 def test_index_at_project_level_with_issue_id_short_filter
716 716 issue = Issue.generate!(:project_id => 1)
717 717 TimeEntry.generate!(:issue => issue, :hours => 4)
718 718 TimeEntry.generate!(:issue => issue, :hours => 3)
719 719 @request.session[:user_id] = 2
720 720
721 721 get :index, :params => {:project_id => 'ecookbook', :issue_id => issue.id.to_s, :set_filter => 1}
722 722 assert_select '.total-for-hours', :text => 'Hours: 7.00'
723 723 end
724 724
725 725 def test_index_at_project_level_with_issue_fixed_version_id_short_filter
726 726 version = Version.generate!(:project_id => 1)
727 727 issue = Issue.generate!(:project_id => 1, :fixed_version => version)
728 728 TimeEntry.generate!(:issue => issue, :hours => 2)
729 729 TimeEntry.generate!(:issue => issue, :hours => 3)
730 730 @request.session[:user_id] = 2
731 731
732 732 get :index, :params => {:project_id => 'ecookbook', :"issue.fixed_version_id" => version.id.to_s, :set_filter => 1}
733 733 assert_select '.total-for-hours', :text => 'Hours: 5.00'
734 734 end
735 735
736 736 def test_index_at_project_level_with_date_range
737 737 get :index, :params => {
738 738 :project_id => 'ecookbook',
739 739 :f => ['spent_on'],
740 740 :op => {'spent_on' => '><'},
741 741 :v => {'spent_on' => ['2007-03-20', '2007-04-30']}
742 742 }
743 743 assert_response :success
744 744
745 745 assert_select 'tr.time-entry', 3
746 746 assert_select '.total-for-hours', :text => 'Hours: 12.90'
747 747 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
748 748 end
749 749
750 750 def test_index_at_project_level_with_date_range_using_from_and_to_params
751 751 get :index, :params => {
752 752 :project_id => 'ecookbook',
753 753 :from => '2007-03-20',
754 754 :to => '2007-04-30'
755 755 }
756 756 assert_response :success
757 757
758 758 assert_select 'tr.time-entry', 3
759 759 assert_select '.total-for-hours', :text => 'Hours: 12.90'
760 760 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
761 761 end
762 762
763 763 def test_index_at_project_level_with_period
764 764 get :index, :params => {
765 765 :project_id => 'ecookbook',
766 766 :f => ['spent_on'],
767 767 :op => {'spent_on' => '>t-'},
768 768 :v => {'spent_on' => ['7']}
769 769 }
770 770 assert_response :success
771 771
772 772 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
773 773 end
774 774
775 775 def test_index_should_sort_by_spent_on_and_created_on
776 776 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)
777 777 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)
778 778 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)
779 779
780 780 get :index, :params => {
781 781 :project_id => 1,
782 782 :f => ['spent_on'],
783 783 :op => {'spent_on' => '><'},
784 784 :v => {'spent_on' => ['2012-06-15', '2012-06-16']}
785 785 }
786 786 assert_response :success
787 787 assert_equal [t2, t1, t3].map(&:id).map(&:to_s), css_select('input[name="ids[]"]').map {|e| e.attr('value')}
788 788
789 789 get :index, :params => {
790 790 :project_id => 1,
791 791 :f => ['spent_on'],
792 792 :op => {'spent_on' => '><'},
793 793 :v => {'spent_on' => ['2012-06-15', '2012-06-16']},
794 794 :sort => 'spent_on'
795 795 }
796 796 assert_response :success
797 797 assert_equal [t3, t1, t2].map(&:id).map(&:to_s), css_select('input[name="ids[]"]').map {|e| e.attr('value')}
798 798 end
799 799
800 800 def test_index_with_issue_status_filter
801 801 Issue.where(:status_id => 4).update_all(:status_id => 2)
802 802 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :status_id => 4)
803 803 entry = TimeEntry.generate!(:issue => issue, :hours => 4.5)
804 804
805 805 get :index, :params => {
806 806 :f => ['issue.status_id'],
807 807 :op => {'issue.status_id' => '='},
808 808 :v => {'issue.status_id' => ['4']}
809 809 }
810 810 assert_response :success
811 811 assert_equal [entry].map(&:id).map(&:to_s), css_select('input[name="ids[]"]').map {|e| e.attr('value')}
812 812 end
813 813
814 814 def test_index_with_issue_status_column
815 815 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :status_id => 4)
816 816 entry = TimeEntry.generate!(:issue => issue)
817 817
818 818 get :index, :params => {
819 819 :c => %w(project spent_on issue comments hours issue.status)
820 820 }
821 821 assert_response :success
822 822 assert_select 'td.issue-status', :text => issue.status.name
823 823 end
824 824
825 825 def test_index_with_issue_status_sort
826 826 TimeEntry.delete_all
827 TimeEntry.generate!(:issue => Issue.generate!(:status_id => 1))
828 TimeEntry.generate!(:issue => Issue.generate!(:status_id => 5))
829 TimeEntry.generate!(:issue => Issue.generate!(:status_id => 3))
827 TimeEntry.generate!(:issue => Issue.generate!(:project_id => 1, :tracker_id => 1, :status_id => 1))
828 TimeEntry.generate!(:issue => Issue.generate!(:project_id => 1, :tracker_id => 1, :status_id => 5))
829 TimeEntry.generate!(:issue => Issue.generate!(:project_id => 1, :tracker_id => 1, :status_id => 3))
830 830 TimeEntry.generate!(:project_id => 1)
831 831
832 832 get :index, :params => {
833 833 :c => ["hours", 'issue.status'],
834 834 :sort => 'issue.status'
835 835 }
836 836 assert_response :success
837 837
838 838 # Make sure that values are properly sorted
839 839 values = css_select("td.issue-status").map(&:text).reject(&:blank?)
840 840 assert_equal IssueStatus.where(:id => [1, 5, 3]).sorted.pluck(:name), values
841 841 end
842 842
843 843 def test_index_with_issue_tracker_filter
844 844 Issue.where(:tracker_id => 2).update_all(:tracker_id => 1)
845 845 issue = Issue.generate!(:project_id => 1, :tracker_id => 2)
846 846 entry = TimeEntry.generate!(:issue => issue, :hours => 4.5)
847 847
848 848 get :index, :params => {
849 849 :f => ['issue.tracker_id'],
850 850 :op => {'issue.tracker_id' => '='},
851 851 :v => {'issue.tracker_id' => ['2']}
852 852 }
853 853 assert_response :success
854 854 assert_equal [entry].map(&:id).map(&:to_s), css_select('input[name="ids[]"]').map {|e| e.attr('value')}
855 855 end
856 856
857 857 def test_index_with_issue_tracker_column
858 858 issue = Issue.generate!(:project_id => 1, :tracker_id => 2)
859 859 entry = TimeEntry.generate!(:issue => issue)
860 860
861 861 get :index, :params => {
862 862 :c => %w(project spent_on issue comments hours issue.tracker)
863 863 }
864 864 assert_response :success
865 865 assert_select 'td.issue-tracker', :text => issue.tracker.name
866 866 end
867 867
868 868 def test_index_with_issue_tracker_sort
869 869 TimeEntry.delete_all
870 870 TimeEntry.generate!(:issue => Issue.generate!(:tracker_id => 1))
871 871 TimeEntry.generate!(:issue => Issue.generate!(:tracker_id => 3))
872 872 TimeEntry.generate!(:issue => Issue.generate!(:tracker_id => 2))
873 873 TimeEntry.generate!(:project_id => 1)
874 874
875 875 get :index, :params => {
876 876 :c => ["hours", 'issue.tracker'],
877 877 :sort => 'issue.tracker'
878 878 }
879 879 assert_response :success
880 880
881 881 # Make sure that values are properly sorted
882 882 values = css_select("td.issue-tracker").map(&:text).reject(&:blank?)
883 883 assert_equal Tracker.where(:id => [1, 2, 3]).sorted.pluck(:name), values
884 884 end
885 885
886 886 def test_index_with_filter_on_issue_custom_field
887 887 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
888 888 entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
889 889
890 890 get :index, :params => {
891 891 :f => ['issue.cf_2'],
892 892 :op => {'issue.cf_2' => '='},
893 893 :v => {'issue.cf_2' => ['filter_on_issue_custom_field']}
894 894 }
895 895 assert_response :success
896 896 assert_equal [entry].map(&:id).map(&:to_s), css_select('input[name="ids[]"]').map {|e| e.attr('value')}
897 897 end
898 898
899 899 def test_index_with_issue_custom_field_column
900 900 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
901 901 entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
902 902
903 903 get :index, :params => {
904 904 :c => %w(project spent_on issue comments hours issue.cf_2)
905 905 }
906 906 assert_response :success
907 907 assert_select 'td.issue_cf_2', :text => 'filter_on_issue_custom_field'
908 908 end
909 909
910 910 def test_index_with_time_entry_custom_field_column
911 911 field = TimeEntryCustomField.generate!(:field_format => 'string')
912 912 entry = TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value'})
913 913 field_name = "cf_#{field.id}"
914 914
915 915 get :index, :params => {
916 916 :c => ["hours", field_name]
917 917 }
918 918 assert_response :success
919 919 assert_select "td.#{field_name}", :text => 'CF Value'
920 920 end
921 921
922 922 def test_index_with_time_entry_custom_field_sorting
923 923 field = TimeEntryCustomField.generate!(:field_format => 'string', :name => 'String Field')
924 924 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 1'})
925 925 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 3'})
926 926 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 2'})
927 927 field_name = "cf_#{field.id}"
928 928
929 929 get :index, :params => {
930 930 :c => ["hours", field_name],
931 931 :sort => field_name
932 932 }
933 933 assert_response :success
934 934 assert_select "th a.sort", :text => 'String Field'
935 935
936 936 # Make sure that values are properly sorted
937 937 values = css_select("td.#{field_name}").map(&:text).reject(&:blank?)
938 938 assert_equal values.sort, values
939 939 assert_equal 3, values.size
940 940 end
941 941
942 942 def test_index_with_query
943 943 query = TimeEntryQuery.new(:project_id => 1, :name => 'Time Entry Query', :visibility => 2)
944 944 query.save!
945 945 @request.session[:user_id] = 2
946 946
947 947 get :index, :params => {:project_id => 'ecookbook', :query_id => query.id}
948 948 assert_response :success
949 949 assert_select 'h2', :text => query.name
950 950 assert_select '#sidebar a.selected', :text => query.name
951 951 end
952 952
953 953 def test_index_atom_feed
954 954 get :index, :params => {:project_id => 1, :format => 'atom'}
955 955 assert_response :success
956 956 assert_equal 'application/atom+xml', @response.content_type
957 957 assert_select 'entry > title', :text => /7\.65 hours/
958 958 end
959 959
960 960 def test_index_at_project_level_should_include_csv_export_dialog
961 961 get :index, :params => {
962 962 :project_id => 'ecookbook',
963 963 :f => ['spent_on'],
964 964 :op => {'spent_on' => '>='},
965 965 :v => {'spent_on' => ['2007-04-01']},
966 966 :c => ['spent_on', 'user']
967 967 }
968 968 assert_response :success
969 969
970 970 assert_select '#csv-export-options' do
971 971 assert_select 'form[action=?][method=get]', '/projects/ecookbook/time_entries.csv' do
972 972 # filter
973 973 assert_select 'input[name=?][value=?]', 'f[]', 'spent_on'
974 974 assert_select 'input[name=?][value=?]', 'op[spent_on]', '>='
975 975 assert_select 'input[name=?][value=?]', 'v[spent_on][]', '2007-04-01'
976 976 # columns
977 977 assert_select 'input[name=?][value=?]', 'c[]', 'spent_on'
978 978 assert_select 'input[name=?][value=?]', 'c[]', 'user'
979 979 assert_select 'input[name=?]', 'c[]', 2
980 980 end
981 981 end
982 982 end
983 983
984 984 def test_index_cross_project_should_include_csv_export_dialog
985 985 get :index
986 986 assert_response :success
987 987
988 988 assert_select '#csv-export-options' do
989 989 assert_select 'form[action=?][method=get]', '/time_entries.csv'
990 990 end
991 991 end
992 992
993 993 def test_index_csv_all_projects
994 994 with_settings :date_format => '%m/%d/%Y' do
995 995 get :index, :params => {:format => 'csv'}
996 996 assert_response :success
997 997 assert_equal 'text/csv; header=present', response.content_type
998 998 end
999 999 end
1000 1000
1001 1001 def test_index_csv
1002 1002 with_settings :date_format => '%m/%d/%Y' do
1003 1003 get :index, :params => {:project_id => 1, :format => 'csv'}
1004 1004 assert_response :success
1005 1005 assert_equal 'text/csv; header=present', response.content_type
1006 1006 end
1007 1007 end
1008 1008
1009 1009 def test_index_csv_should_fill_issue_column_with_tracker_id_and_subject
1010 1010 issue = Issue.find(1)
1011 1011 entry = TimeEntry.generate!(:issue => issue, :comments => "Issue column content test")
1012 1012
1013 1013 get :index, :params => {:format => 'csv'}
1014 1014 line = response.body.split("\n").detect {|l| l.include?(entry.comments)}
1015 1015 assert_not_nil line
1016 1016 assert_include "#{issue.tracker} #1: #{issue.subject}", line
1017 1017 end
1018 1018 end
General Comments 0
You need to be logged in to leave comments. Login now