comparison test/functional/timelog_controller_test.rb @ 1526:404aa68d4227

Merge from live branch
author Chris Cannam
date Thu, 11 Sep 2014 12:46:20 +0100
parents dffacf8a6908
children
comparison
equal deleted inserted replaced
1493:a5f2bdf3b486 1526:404aa68d4227
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2 # Redmine - project management software 2 # Redmine - project management software
3 # Copyright (C) 2006-2012 Jean-Philippe Lang 3 # Copyright (C) 2006-2014 Jean-Philippe Lang
4 # 4 #
5 # This program is free software; you can redistribute it and/or 5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License 6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2 7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version. 8 # of the License, or (at your option) any later version.
15 # You should have received a copy of the GNU General Public License 15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software 16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 18
19 require File.expand_path('../../test_helper', __FILE__) 19 require File.expand_path('../../test_helper', __FILE__)
20 require 'timelog_controller'
21
22 # Re-raise errors caught by the controller.
23 class TimelogController; def rescue_action(e) raise e end; end
24 20
25 class TimelogControllerTest < ActionController::TestCase 21 class TimelogControllerTest < ActionController::TestCase
26 fixtures :projects, :enabled_modules, :roles, :members, 22 fixtures :projects, :enabled_modules, :roles, :members,
27 :member_roles, :issues, :time_entries, :users, 23 :member_roles, :issues, :time_entries, :users,
28 :trackers, :enumerations, :issue_statuses, 24 :trackers, :enumerations, :issue_statuses,
29 :custom_fields, :custom_values 25 :custom_fields, :custom_values,
26 :projects_trackers, :custom_fields_trackers,
27 :custom_fields_projects
30 28
31 include Redmine::I18n 29 include Redmine::I18n
32
33 def setup
34 @controller = TimelogController.new
35 @request = ActionController::TestRequest.new
36 @response = ActionController::TestResponse.new
37 end
38 30
39 def test_new_with_project_id 31 def test_new_with_project_id
40 @request.session[:user_id] = 3 32 @request.session[:user_id] = 3
41 get :new, :project_id => 1 33 get :new, :project_id => 1
42 assert_response :success 34 assert_response :success
230 :spent_on => '2008-03-14', 222 :spent_on => '2008-03-14',
231 :hours => '7.3'} 223 :hours => '7.3'}
232 end 224 end
233 225
234 assert_redirected_to '/projects/ecookbook/time_entries' 226 assert_redirected_to '/projects/ecookbook/time_entries'
235 time_entry = TimeEntry.first(:order => 'id DESC') 227 time_entry = TimeEntry.order('id DESC').first
236 assert_equal 1, time_entry.project_id 228 assert_equal 1, time_entry.project_id
237 end 229 end
238 230
239 def test_create_without_project_should_fail_with_issue_not_inside_project 231 def test_create_without_project_should_fail_with_issue_not_inside_project
240 @request.session[:user_id] = 2 232 @request.session[:user_id] = 2
295 assert_equal 8, entry.hours 287 assert_equal 8, entry.hours
296 assert_equal 2, entry.issue_id 288 assert_equal 2, entry.issue_id
297 assert_equal 2, entry.user_id 289 assert_equal 2, entry.user_id
298 end 290 end
299 291
292 def test_update_should_allow_to_change_issue_to_another_project
293 entry = TimeEntry.generate!(:issue_id => 1)
294
295 @request.session[:user_id] = 1
296 put :update, :id => entry.id, :time_entry => {:issue_id => '5'}
297 assert_response 302
298 entry.reload
299
300 assert_equal 5, entry.issue_id
301 assert_equal 3, entry.project_id
302 end
303
304 def test_update_should_not_allow_to_change_issue_to_an_invalid_project
305 entry = TimeEntry.generate!(:issue_id => 1)
306 Project.find(3).disable_module!(:time_tracking)
307
308 @request.session[:user_id] = 1
309 put :update, :id => entry.id, :time_entry => {:issue_id => '5'}
310 assert_response 200
311 assert_include "Issue is invalid", assigns(:time_entry).errors.full_messages
312 end
313
300 def test_get_bulk_edit 314 def test_get_bulk_edit
301 @request.session[:user_id] = 2 315 @request.session[:user_id] = 2
302 get :bulk_edit, :ids => [1, 2] 316 get :bulk_edit, :ids => [1, 2]
303 assert_response :success 317 assert_response :success
304 assert_template 'bulk_edit' 318 assert_template 'bulk_edit'
305 319
306 # System wide custom field 320 assert_select 'ul#bulk-selection' do
307 assert_tag :select, :attributes => {:name => 'time_entry[custom_field_values][10]'} 321 assert_select 'li', 2
308 322 assert_select 'li a', :text => '03/23/2007 - eCookbook: 4.25 hours'
309 # Activities 323 end
310 assert_select 'select[name=?]', 'time_entry[activity_id]' do 324
311 assert_select 'option[value=]', :text => '(No change)' 325 assert_select 'form#bulk_edit_form[action=?]', '/time_entries/bulk_update' do
312 assert_select 'option[value=9]', :text => 'Design' 326 # System wide custom field
327 assert_select 'select[name=?]', 'time_entry[custom_field_values][10]'
328
329 # Activities
330 assert_select 'select[name=?]', 'time_entry[activity_id]' do
331 assert_select 'option[value=]', :text => '(No change)'
332 assert_select 'option[value=9]', :text => 'Design'
333 end
313 end 334 end
314 end 335 end
315 336
316 def test_get_bulk_edit_on_different_projects 337 def test_get_bulk_edit_on_different_projects
317 @request.session[:user_id] = 2 338 @request.session[:user_id] = 2
325 # update time entry activity 346 # update time entry activity
326 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9} 347 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9}
327 348
328 assert_response 302 349 assert_response 302
329 # check that the issues were updated 350 # check that the issues were updated
330 assert_equal [9, 9], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.activity_id} 351 assert_equal [9, 9], TimeEntry.where(:id => [1, 2]).collect {|i| i.activity_id}
331 end 352 end
332 353
333 def test_bulk_update_with_failure 354 def test_bulk_update_with_failure
334 @request.session[:user_id] = 2 355 @request.session[:user_id] = 2
335 post :bulk_update, :ids => [1, 2], :time_entry => { :hours => 'A'} 356 post :bulk_update, :ids => [1, 2], :time_entry => { :hours => 'A'}
346 # update time entry activity 367 # update time entry activity
347 post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 } 368 post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 }
348 369
349 assert_response 302 370 assert_response 302
350 # check that the issues were updated 371 # check that the issues were updated
351 assert_equal [9, 9, 9], TimeEntry.find_all_by_id([1, 2, 4]).collect {|i| i.activity_id} 372 assert_equal [9, 9, 9], TimeEntry.where(:id => [1, 2, 4]).collect {|i| i.activity_id}
352 end 373 end
353 374
354 def test_bulk_update_on_different_projects_without_rights 375 def test_bulk_update_on_different_projects_without_rights
355 @request.session[:user_id] = 3 376 @request.session[:user_id] = 3
356 user = User.find(3) 377 user = User.find(3)
364 def test_bulk_update_custom_field 385 def test_bulk_update_custom_field
365 @request.session[:user_id] = 2 386 @request.session[:user_id] = 2
366 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} } 387 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }
367 388
368 assert_response 302 389 assert_response 302
369 assert_equal ["0", "0"], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.custom_value_for(10).value} 390 assert_equal ["0", "0"], TimeEntry.where(:id => [1, 2]).collect {|i| i.custom_value_for(10).value}
370 end 391 end
371 392
372 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter 393 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
373 @request.session[:user_id] = 2 394 @request.session[:user_id] = 2
374 post :bulk_update, :ids => [1,2], :back_url => '/time_entries' 395 post :bulk_update, :ids => [1,2], :back_url => '/time_entries'
426 @request.session[:user_id] = 2 447 @request.session[:user_id] = 2
427 get :index 448 get :index
428 assert_response :success 449 assert_response :success
429 assert_template 'index' 450 assert_template 'index'
430 assert_tag 'a', :attributes => {:href => '/time_entries/new'}, :content => /Log time/ 451 assert_tag 'a', :attributes => {:href => '/time_entries/new'}, :content => /Log time/
452 end
453
454 def test_index_my_spent_time
455 @request.session[:user_id] = 2
456 get :index, :user_id => 'me'
457 assert_response :success
458 assert_template 'index'
459 assert assigns(:entries).all? {|entry| entry.user_id == 2}
431 end 460 end
432 461
433 def test_index_at_project_level 462 def test_index_at_project_level
434 get :index, :project_id => 'ecookbook' 463 get :index, :project_id => 'ecookbook'
435 assert_response :success 464 assert_response :success
438 assert_equal 4, assigns(:entries).size 467 assert_equal 4, assigns(:entries).size
439 # project and subproject 468 # project and subproject
440 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort 469 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
441 assert_not_nil assigns(:total_hours) 470 assert_not_nil assigns(:total_hours)
442 assert_equal "162.90", "%.2f" % assigns(:total_hours) 471 assert_equal "162.90", "%.2f" % assigns(:total_hours)
443 # display all time by default
444 assert_nil assigns(:from)
445 assert_nil assigns(:to)
446 assert_tag :form, 472 assert_tag :form,
447 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} 473 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
448 end 474 end
449 475
476 def test_index_with_display_subprojects_issues_to_false_should_not_include_subproject_entries
477 entry = TimeEntry.generate!(:project => Project.find(3))
478
479 with_settings :display_subprojects_issues => '0' do
480 get :index, :project_id => 'ecookbook'
481 assert_response :success
482 assert_template 'index'
483 assert_not_include entry, assigns(:entries)
484 end
485 end
486
487 def test_index_with_display_subprojects_issues_to_false_and_subproject_filter_should_include_subproject_entries
488 entry = TimeEntry.generate!(:project => Project.find(3))
489
490 with_settings :display_subprojects_issues => '0' do
491 get :index, :project_id => 'ecookbook', :subproject_id => 3
492 assert_response :success
493 assert_template 'index'
494 assert_include entry, assigns(:entries)
495 end
496 end
497
450 def test_index_at_project_level_with_date_range 498 def test_index_at_project_level_with_date_range
451 get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30' 499 get :index, :project_id => 'ecookbook',
500 :f => ['spent_on'],
501 :op => {'spent_on' => '><'},
502 :v => {'spent_on' => ['2007-03-20', '2007-04-30']}
452 assert_response :success 503 assert_response :success
453 assert_template 'index' 504 assert_template 'index'
454 assert_not_nil assigns(:entries) 505 assert_not_nil assigns(:entries)
455 assert_equal 3, assigns(:entries).size 506 assert_equal 3, assigns(:entries).size
456 assert_not_nil assigns(:total_hours) 507 assert_not_nil assigns(:total_hours)
457 assert_equal "12.90", "%.2f" % assigns(:total_hours) 508 assert_equal "12.90", "%.2f" % assigns(:total_hours)
458 assert_equal '2007-03-20'.to_date, assigns(:from)
459 assert_equal '2007-04-30'.to_date, assigns(:to)
460 assert_tag :form, 509 assert_tag :form,
461 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} 510 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
462 end 511 end
463 512
513 def test_index_at_project_level_with_date_range_using_from_and_to_params
514 get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30'
515 assert_response :success
516 assert_template 'index'
517 assert_not_nil assigns(:entries)
518 assert_equal 3, assigns(:entries).size
519 assert_not_nil assigns(:total_hours)
520 assert_equal "12.90", "%.2f" % assigns(:total_hours)
521 assert_tag :form,
522 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
523 end
524
464 def test_index_at_project_level_with_period 525 def test_index_at_project_level_with_period
465 get :index, :project_id => 'ecookbook', :period => '7_days' 526 get :index, :project_id => 'ecookbook',
527 :f => ['spent_on'],
528 :op => {'spent_on' => '>t-'},
529 :v => {'spent_on' => ['7']}
466 assert_response :success 530 assert_response :success
467 assert_template 'index' 531 assert_template 'index'
468 assert_not_nil assigns(:entries) 532 assert_not_nil assigns(:entries)
469 assert_not_nil assigns(:total_hours) 533 assert_not_nil assigns(:total_hours)
470 assert_equal Date.today - 7, assigns(:from)
471 assert_equal Date.today, assigns(:to)
472 assert_tag :form, 534 assert_tag :form,
473 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} 535 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
474 end
475
476 def test_index_one_day
477 get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => "2007-03-23"
478 assert_response :success
479 assert_template 'index'
480 assert_not_nil assigns(:total_hours)
481 assert_equal "4.25", "%.2f" % assigns(:total_hours)
482 assert_tag :form,
483 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
484 end
485
486 def test_index_from_a_date
487 get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => ""
488 assert_equal '2007-03-23'.to_date, assigns(:from)
489 assert_nil assigns(:to)
490 end
491
492 def test_index_to_a_date
493 get :index, :project_id => 'ecookbook', :from => "", :to => "2007-03-23"
494 assert_nil assigns(:from)
495 assert_equal '2007-03-23'.to_date, assigns(:to)
496 end
497
498 def test_index_today
499 Date.stubs(:today).returns('2011-12-15'.to_date)
500 get :index, :period => 'today'
501 assert_equal '2011-12-15'.to_date, assigns(:from)
502 assert_equal '2011-12-15'.to_date, assigns(:to)
503 end
504
505 def test_index_yesterday
506 Date.stubs(:today).returns('2011-12-15'.to_date)
507 get :index, :period => 'yesterday'
508 assert_equal '2011-12-14'.to_date, assigns(:from)
509 assert_equal '2011-12-14'.to_date, assigns(:to)
510 end
511
512 def test_index_current_week
513 Date.stubs(:today).returns('2011-12-15'.to_date)
514 get :index, :period => 'current_week'
515 assert_equal '2011-12-12'.to_date, assigns(:from)
516 assert_equal '2011-12-18'.to_date, assigns(:to)
517 end
518
519 def test_index_last_week
520 Date.stubs(:today).returns('2011-12-15'.to_date)
521 get :index, :period => 'last_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_2_week
527 Date.stubs(:today).returns('2011-12-15'.to_date)
528 get :index, :period => 'last_2_weeks'
529 assert_equal '2011-11-28'.to_date, assigns(:from)
530 assert_equal '2011-12-11'.to_date, assigns(:to)
531 end
532
533 def test_index_7_days
534 Date.stubs(:today).returns('2011-12-15'.to_date)
535 get :index, :period => '7_days'
536 assert_equal '2011-12-08'.to_date, assigns(:from)
537 assert_equal '2011-12-15'.to_date, assigns(:to)
538 end
539
540 def test_index_current_month
541 Date.stubs(:today).returns('2011-12-15'.to_date)
542 get :index, :period => 'current_month'
543 assert_equal '2011-12-01'.to_date, assigns(:from)
544 assert_equal '2011-12-31'.to_date, assigns(:to)
545 end
546
547 def test_index_last_month
548 Date.stubs(:today).returns('2011-12-15'.to_date)
549 get :index, :period => 'last_month'
550 assert_equal '2011-11-01'.to_date, assigns(:from)
551 assert_equal '2011-11-30'.to_date, assigns(:to)
552 end
553
554 def test_index_30_days
555 Date.stubs(:today).returns('2011-12-15'.to_date)
556 get :index, :period => '30_days'
557 assert_equal '2011-11-15'.to_date, assigns(:from)
558 assert_equal '2011-12-15'.to_date, assigns(:to)
559 end
560
561 def test_index_current_year
562 Date.stubs(:today).returns('2011-12-15'.to_date)
563 get :index, :period => 'current_year'
564 assert_equal '2011-01-01'.to_date, assigns(:from)
565 assert_equal '2011-12-31'.to_date, assigns(:to)
566 end 536 end
567 537
568 def test_index_at_issue_level 538 def test_index_at_issue_level
569 get :index, :issue_id => 1 539 get :index, :issue_id => 1
570 assert_response :success 540 assert_response :success
585 def test_index_should_sort_by_spent_on_and_created_on 555 def test_index_should_sort_by_spent_on_and_created_on
586 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) 556 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 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) 557 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 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) 558 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 559
590 get :index, :project_id => 1, :from => '2012-06-15', :to => '2012-06-16' 560 get :index, :project_id => 1,
561 :f => ['spent_on'],
562 :op => {'spent_on' => '><'},
563 :v => {'spent_on' => ['2012-06-15', '2012-06-16']}
591 assert_response :success 564 assert_response :success
592 assert_equal [t2, t1, t3], assigns(:entries) 565 assert_equal [t2, t1, t3], assigns(:entries)
593 566
594 get :index, :project_id => 1, :from => '2012-06-15', :to => '2012-06-16', :sort => 'spent_on' 567 get :index, :project_id => 1,
568 :f => ['spent_on'],
569 :op => {'spent_on' => '><'},
570 :v => {'spent_on' => ['2012-06-15', '2012-06-16']},
571 :sort => 'spent_on'
595 assert_response :success 572 assert_response :success
596 assert_equal [t3, t1, t2], assigns(:entries) 573 assert_equal [t3, t1, t2], assigns(:entries)
574 end
575
576 def test_index_with_filter_on_issue_custom_field
577 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
578 entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
579
580 get :index, :f => ['issue.cf_2'], :op => {'issue.cf_2' => '='}, :v => {'issue.cf_2' => ['filter_on_issue_custom_field']}
581 assert_response :success
582 assert_equal [entry], assigns(:entries)
583 end
584
585 def test_index_with_issue_custom_field_column
586 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
587 entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
588
589 get :index, :c => %w(project spent_on issue comments hours issue.cf_2)
590 assert_response :success
591 assert_include :'issue.cf_2', assigns(:query).column_names
592 assert_select 'td.issue_cf_2', :text => 'filter_on_issue_custom_field'
593 end
594
595 def test_index_with_time_entry_custom_field_column
596 field = TimeEntryCustomField.generate!(:field_format => 'string')
597 entry = TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value'})
598 field_name = "cf_#{field.id}"
599
600 get :index, :c => ["hours", field_name]
601 assert_response :success
602 assert_include field_name.to_sym, assigns(:query).column_names
603 assert_select "td.#{field_name}", :text => 'CF Value'
604 end
605
606 def test_index_with_time_entry_custom_field_sorting
607 field = TimeEntryCustomField.generate!(:field_format => 'string', :name => 'String Field')
608 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 1'})
609 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 3'})
610 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 2'})
611 field_name = "cf_#{field.id}"
612
613 get :index, :c => ["hours", field_name], :sort => field_name
614 assert_response :success
615 assert_include field_name.to_sym, assigns(:query).column_names
616 assert_select "th a.sort", :text => 'String Field'
617
618 # Make sure that values are properly sorted
619 values = assigns(:entries).map {|e| e.custom_field_value(field)}.compact
620 assert_equal 3, values.size
621 assert_equal values.sort, values
597 end 622 end
598 623
599 def test_index_atom_feed 624 def test_index_atom_feed
600 get :index, :project_id => 1, :format => 'atom' 625 get :index, :project_id => 1, :format => 'atom'
601 assert_response :success 626 assert_response :success
602 assert_equal 'application/atom+xml', @response.content_type 627 assert_equal 'application/atom+xml', @response.content_type
603 assert_not_nil assigns(:items) 628 assert_not_nil assigns(:items)
604 assert assigns(:items).first.is_a?(TimeEntry) 629 assert assigns(:items).first.is_a?(TimeEntry)
605 end 630 end
606 631
607 def test_index_all_projects_csv_export 632 def test_index_at_project_level_should_include_csv_export_dialog
633 get :index, :project_id => 'ecookbook',
634 :f => ['spent_on'],
635 :op => {'spent_on' => '>='},
636 :v => {'spent_on' => ['2007-04-01']},
637 :c => ['spent_on', 'user']
638 assert_response :success
639
640 assert_select '#csv-export-options' do
641 assert_select 'form[action=?][method=get]', '/projects/ecookbook/time_entries.csv' do
642 # filter
643 assert_select 'input[name=?][value=?]', 'f[]', 'spent_on'
644 assert_select 'input[name=?][value=?]', 'op[spent_on]', '&gt;='
645 assert_select 'input[name=?][value=?]', 'v[spent_on][]', '2007-04-01'
646 # columns
647 assert_select 'input[name=?][value=?]', 'c[]', 'spent_on'
648 assert_select 'input[name=?][value=?]', 'c[]', 'user'
649 assert_select 'input[name=?]', 'c[]', 2
650 end
651 end
652 end
653
654 def test_index_cross_project_should_include_csv_export_dialog
655 get :index
656 assert_response :success
657
658 assert_select '#csv-export-options' do
659 assert_select 'form[action=?][method=get]', '/time_entries.csv'
660 end
661 end
662
663 def test_index_at_issue_level_should_include_csv_export_dialog
664 get :index, :project_id => 'ecookbook', :issue_id => 3
665 assert_response :success
666
667 assert_select '#csv-export-options' do
668 assert_select 'form[action=?][method=get]', '/projects/ecookbook/issues/3/time_entries.csv'
669 end
670 end
671
672 def test_index_csv_all_projects
608 Setting.date_format = '%m/%d/%Y' 673 Setting.date_format = '%m/%d/%Y'
609 get :index, :format => 'csv' 674 get :index, :format => 'csv'
610 assert_response :success 675 assert_response :success
611 assert_equal 'text/csv; header=present', @response.content_type 676 assert_equal 'text/csv; header=present', response.content_type
612 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n") 677 end
613 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n") 678
614 end 679 def test_index_csv
615
616 def test_index_csv_export
617 Setting.date_format = '%m/%d/%Y' 680 Setting.date_format = '%m/%d/%Y'
618 get :index, :project_id => 1, :format => 'csv' 681 get :index, :project_id => 1, :format => 'csv'
619 assert_response :success 682 assert_response :success
620 assert_equal 'text/csv; header=present', @response.content_type 683 assert_equal 'text/csv; header=present', response.content_type
621 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
622 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
623 end
624
625 def test_index_csv_export_with_multi_custom_field
626 field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'list',
627 :multiple => true, :possible_values => ['value1', 'value2'])
628 entry = TimeEntry.find(1)
629 entry.custom_field_values = {field.id => ['value1', 'value2']}
630 entry.save!
631
632 get :index, :project_id => 1, :format => 'csv'
633 assert_response :success
634 assert_include '"value1, value2"', @response.body
635 end
636
637 def test_csv_big_5
638 user = User.find_by_id(3)
639 user.language = "zh-TW"
640 assert user.save
641 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
642 str_big5 = "\xa4@\xa4\xeb"
643 if str_utf8.respond_to?(:force_encoding)
644 str_utf8.force_encoding('UTF-8')
645 str_big5.force_encoding('Big5')
646 end
647 @request.session[:user_id] = 3
648 post :create, :project_id => 1,
649 :time_entry => {:comments => str_utf8,
650 # Not the default activity
651 :activity_id => '11',
652 :issue_id => '',
653 :spent_on => '2011-11-10',
654 :hours => '7.3'}
655 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
656
657 t = TimeEntry.find_by_comments(str_utf8)
658 assert_not_nil t
659 assert_equal 11, t.activity_id
660 assert_equal 7.3, t.hours
661 assert_equal 3, t.user_id
662
663 get :index, :project_id => 1, :format => 'csv',
664 :from => '2011-11-10', :to => '2011-11-10'
665 assert_response :success
666 assert_equal 'text/csv; header=present', @response.content_type
667 ar = @response.body.chomp.split("\n")
668 s1 = "\xa4\xe9\xb4\xc1"
669 if str_utf8.respond_to?(:force_encoding)
670 s1.force_encoding('Big5')
671 end
672 assert ar[0].include?(s1)
673 assert ar[1].include?(str_big5)
674 end
675
676 def test_csv_cannot_convert_should_be_replaced_big_5
677 user = User.find_by_id(3)
678 user.language = "zh-TW"
679 assert user.save
680 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
681 if str_utf8.respond_to?(:force_encoding)
682 str_utf8.force_encoding('UTF-8')
683 end
684 @request.session[:user_id] = 3
685 post :create, :project_id => 1,
686 :time_entry => {:comments => str_utf8,
687 # Not the default activity
688 :activity_id => '11',
689 :issue_id => '',
690 :spent_on => '2011-11-10',
691 :hours => '7.3'}
692 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
693
694 t = TimeEntry.find_by_comments(str_utf8)
695 assert_not_nil t
696 assert_equal 11, t.activity_id
697 assert_equal 7.3, t.hours
698 assert_equal 3, t.user_id
699
700 get :index, :project_id => 1, :format => 'csv',
701 :from => '2011-11-10', :to => '2011-11-10'
702 assert_response :success
703 assert_equal 'text/csv; header=present', @response.content_type
704 ar = @response.body.chomp.split("\n")
705 s1 = "\xa4\xe9\xb4\xc1"
706 if str_utf8.respond_to?(:force_encoding)
707 s1.force_encoding('Big5')
708 end
709 assert ar[0].include?(s1)
710 s2 = ar[1].split(",")[8]
711 if s2.respond_to?(:force_encoding)
712 s3 = "\xa5H?"
713 s3.force_encoding('Big5')
714 assert_equal s3, s2
715 elsif RUBY_PLATFORM == 'java'
716 assert_equal "??", s2
717 else
718 assert_equal "\xa5H???", s2
719 end
720 end
721
722 def test_csv_tw
723 with_settings :default_language => "zh-TW" do
724 str1 = "test_csv_tw"
725 user = User.find_by_id(3)
726 te1 = TimeEntry.create(:spent_on => '2011-11-10',
727 :hours => 999.9,
728 :project => Project.find(1),
729 :user => user,
730 :activity => TimeEntryActivity.find_by_name('Design'),
731 :comments => str1)
732 te2 = TimeEntry.find_by_comments(str1)
733 assert_not_nil te2
734 assert_equal 999.9, te2.hours
735 assert_equal 3, te2.user_id
736
737 get :index, :project_id => 1, :format => 'csv',
738 :from => '2011-11-10', :to => '2011-11-10'
739 assert_response :success
740 assert_equal 'text/csv; header=present', @response.content_type
741
742 ar = @response.body.chomp.split("\n")
743 s2 = ar[1].split(",")[7]
744 assert_equal '999.9', s2
745
746 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
747 if str_tw.respond_to?(:force_encoding)
748 str_tw.force_encoding('UTF-8')
749 end
750 assert_equal str_tw, l(:general_lang_name)
751 assert_equal ',', l(:general_csv_separator)
752 assert_equal '.', l(:general_csv_decimal_separator)
753 end
754 end
755
756 def test_csv_fr
757 with_settings :default_language => "fr" do
758 str1 = "test_csv_fr"
759 user = User.find_by_id(3)
760 te1 = TimeEntry.create(:spent_on => '2011-11-10',
761 :hours => 999.9,
762 :project => Project.find(1),
763 :user => user,
764 :activity => TimeEntryActivity.find_by_name('Design'),
765 :comments => str1)
766 te2 = TimeEntry.find_by_comments(str1)
767 assert_not_nil te2
768 assert_equal 999.9, te2.hours
769 assert_equal 3, te2.user_id
770
771 get :index, :project_id => 1, :format => 'csv',
772 :from => '2011-11-10', :to => '2011-11-10'
773 assert_response :success
774 assert_equal 'text/csv; header=present', @response.content_type
775
776 ar = @response.body.chomp.split("\n")
777 s2 = ar[1].split(";")[7]
778 assert_equal '999,9', s2
779
780 str_fr = "Fran\xc3\xa7ais"
781 if str_fr.respond_to?(:force_encoding)
782 str_fr.force_encoding('UTF-8')
783 end
784 assert_equal str_fr, l(:general_lang_name)
785 assert_equal ';', l(:general_csv_separator)
786 assert_equal ',', l(:general_csv_decimal_separator)
787 end
788 end 684 end
789 end 685 end