Chris@909: # -*- coding: utf-8 -*- Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: require File.expand_path('../../test_helper', __FILE__) Chris@909: require 'timelog_controller' Chris@909: Chris@909: # Re-raise errors caught by the controller. Chris@909: class TimelogController; def rescue_action(e) raise e end; end Chris@909: Chris@909: class TimelogControllerTest < ActionController::TestCase Chris@909: fixtures :projects, :enabled_modules, :roles, :members, Chris@909: :member_roles, :issues, :time_entries, :users, Chris@909: :trackers, :enumerations, :issue_statuses, Chris@909: :custom_fields, :custom_values Chris@909: Chris@909: include Redmine::I18n Chris@909: Chris@909: def setup Chris@909: @controller = TimelogController.new Chris@909: @request = ActionController::TestRequest.new Chris@909: @response = ActionController::TestResponse.new Chris@909: end Chris@909: Chris@909: def test_get_new Chris@909: @request.session[:user_id] = 3 Chris@909: get :new, :project_id => 1 Chris@909: assert_response :success Chris@909: assert_template 'edit' Chris@909: # Default activity selected Chris@909: assert_tag :tag => 'option', :attributes => { :selected => 'selected' }, Chris@909: :content => 'Development' Chris@909: end Chris@909: Chris@909: def test_get_new_should_only_show_active_time_entry_activities Chris@909: @request.session[:user_id] = 3 Chris@909: get :new, :project_id => 1 Chris@909: assert_response :success Chris@909: assert_template 'edit' Chris@909: assert_no_tag :tag => 'option', :content => 'Inactive Activity' Chris@909: end Chris@909: Chris@909: def test_get_edit_existing_time Chris@909: @request.session[:user_id] = 2 Chris@909: get :edit, :id => 2, :project_id => nil Chris@909: assert_response :success Chris@909: assert_template 'edit' Chris@909: # Default activity selected Chris@909: assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/time_entries/2' } Chris@909: end Chris@909: Chris@909: def test_get_edit_with_an_existing_time_entry_with_inactive_activity Chris@909: te = TimeEntry.find(1) Chris@909: te.activity = TimeEntryActivity.find_by_name("Inactive Activity") Chris@909: te.save! Chris@909: Chris@909: @request.session[:user_id] = 1 Chris@909: get :edit, :project_id => 1, :id => 1 Chris@909: assert_response :success Chris@909: assert_template 'edit' Chris@909: # Blank option since nothing is pre-selected Chris@909: assert_tag :tag => 'option', :content => '--- Please select ---' Chris@909: end Chris@909: Chris@909: def test_post_create Chris@909: # TODO: should POST to issues’ time log instead of project. change form Chris@909: # and routing Chris@909: @request.session[:user_id] = 3 Chris@909: post :create, :project_id => 1, Chris@909: :time_entry => {:comments => 'Some work on TimelogControllerTest', Chris@909: # Not the default activity Chris@909: :activity_id => '11', Chris@909: :spent_on => '2008-03-14', Chris@909: :issue_id => '1', Chris@909: :hours => '7.3'} Chris@909: assert_redirected_to :action => 'index', :project_id => 'ecookbook' Chris@909: Chris@909: i = Issue.find(1) Chris@909: t = TimeEntry.find_by_comments('Some work on TimelogControllerTest') Chris@909: assert_not_nil t Chris@909: assert_equal 11, t.activity_id Chris@909: assert_equal 7.3, t.hours Chris@909: assert_equal 3, t.user_id Chris@909: assert_equal i, t.issue Chris@909: assert_equal i.project, t.project Chris@909: end Chris@909: Chris@909: def test_post_create_with_blank_issue Chris@909: # TODO: should POST to issues’ time log instead of project. change form Chris@909: # and routing Chris@909: @request.session[:user_id] = 3 Chris@909: post :create, :project_id => 1, Chris@909: :time_entry => {:comments => 'Some work on TimelogControllerTest', Chris@909: # Not the default activity Chris@909: :activity_id => '11', Chris@909: :issue_id => '', Chris@909: :spent_on => '2008-03-14', Chris@909: :hours => '7.3'} Chris@909: assert_redirected_to :action => 'index', :project_id => 'ecookbook' Chris@909: Chris@909: t = TimeEntry.find_by_comments('Some work on TimelogControllerTest') Chris@909: assert_not_nil t Chris@909: assert_equal 11, t.activity_id Chris@909: assert_equal 7.3, t.hours Chris@909: assert_equal 3, t.user_id Chris@909: end Chris@909: Chris@909: def test_create_without_log_time_permission_should_be_denied Chris@909: @request.session[:user_id] = 2 Chris@909: Role.find_by_name('Manager').remove_permission! :log_time Chris@909: post :create, :project_id => 1, Chris@909: :time_entry => {:activity_id => '11', Chris@909: :issue_id => '', Chris@909: :spent_on => '2008-03-14', Chris@909: :hours => '7.3'} Chris@909: Chris@909: assert_response 403 Chris@909: end Chris@909: Chris@909: def test_update Chris@909: entry = TimeEntry.find(1) Chris@909: assert_equal 1, entry.issue_id Chris@909: assert_equal 2, entry.user_id Chris@909: Chris@909: @request.session[:user_id] = 1 Chris@909: put :update, :id => 1, Chris@909: :time_entry => {:issue_id => '2', Chris@909: :hours => '8'} Chris@909: assert_redirected_to :action => 'index', :project_id => 'ecookbook' Chris@909: entry.reload Chris@909: Chris@909: assert_equal 8, entry.hours Chris@909: assert_equal 2, entry.issue_id Chris@909: assert_equal 2, entry.user_id Chris@909: end Chris@909: Chris@909: def test_get_bulk_edit Chris@909: @request.session[:user_id] = 2 Chris@909: get :bulk_edit, :ids => [1, 2] Chris@909: assert_response :success Chris@909: assert_template 'bulk_edit' Chris@909: Chris@909: # System wide custom field Chris@909: assert_tag :select, :attributes => {:name => 'time_entry[custom_field_values][10]'} Chris@909: end Chris@909: Chris@909: def test_get_bulk_edit_on_different_projects Chris@909: @request.session[:user_id] = 2 Chris@909: get :bulk_edit, :ids => [1, 2, 6] Chris@909: assert_response :success Chris@909: assert_template 'bulk_edit' Chris@909: end Chris@909: Chris@909: def test_bulk_update Chris@909: @request.session[:user_id] = 2 Chris@909: # update time entry activity Chris@909: post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9} Chris@909: Chris@909: assert_response 302 Chris@909: # check that the issues were updated Chris@909: assert_equal [9, 9], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.activity_id} Chris@909: end Chris@909: Chris@909: def test_bulk_update_on_different_projects Chris@909: @request.session[:user_id] = 2 Chris@909: # makes user a manager on the other project Chris@909: Member.create!(:user_id => 2, :project_id => 3, :role_ids => [1]) Chris@909: Chris@909: # update time entry activity Chris@909: post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 } Chris@909: Chris@909: assert_response 302 Chris@909: # check that the issues were updated Chris@909: assert_equal [9, 9, 9], TimeEntry.find_all_by_id([1, 2, 4]).collect {|i| i.activity_id} Chris@909: end Chris@909: Chris@909: def test_bulk_update_on_different_projects_without_rights Chris@909: @request.session[:user_id] = 3 Chris@909: user = User.find(3) Chris@909: action = { :controller => "timelog", :action => "bulk_update" } Chris@909: assert user.allowed_to?(action, TimeEntry.find(1).project) Chris@909: assert ! user.allowed_to?(action, TimeEntry.find(5).project) Chris@909: post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 } Chris@909: assert_response 403 Chris@909: end Chris@909: Chris@909: def test_bulk_update_custom_field Chris@909: @request.session[:user_id] = 2 Chris@909: post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} } Chris@909: Chris@909: assert_response 302 Chris@909: assert_equal ["0", "0"], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.custom_value_for(10).value} Chris@909: end Chris@909: Chris@909: def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter Chris@909: @request.session[:user_id] = 2 Chris@909: post :bulk_update, :ids => [1,2], :back_url => '/time_entries' Chris@909: Chris@909: assert_response :redirect Chris@909: assert_redirected_to '/time_entries' Chris@909: end Chris@909: Chris@909: def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host Chris@909: @request.session[:user_id] = 2 Chris@909: post :bulk_update, :ids => [1,2], :back_url => 'http://google.com' Chris@909: Chris@909: assert_response :redirect Chris@909: assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier Chris@909: end Chris@909: Chris@909: def test_post_bulk_update_without_edit_permission_should_be_denied Chris@909: @request.session[:user_id] = 2 Chris@909: Role.find_by_name('Manager').remove_permission! :edit_time_entries Chris@909: post :bulk_update, :ids => [1,2] Chris@909: Chris@909: assert_response 403 Chris@909: end Chris@909: Chris@909: def test_destroy Chris@909: @request.session[:user_id] = 2 Chris@909: delete :destroy, :id => 1 Chris@909: assert_redirected_to :action => 'index', :project_id => 'ecookbook' Chris@909: assert_equal I18n.t(:notice_successful_delete), flash[:notice] Chris@909: assert_nil TimeEntry.find_by_id(1) Chris@909: end Chris@909: Chris@909: def test_destroy_should_fail Chris@909: # simulate that this fails (e.g. due to a plugin), see #5700 Chris@909: TimeEntry.any_instance.expects(:destroy).returns(false) Chris@909: Chris@909: @request.session[:user_id] = 2 Chris@909: delete :destroy, :id => 1 Chris@909: assert_redirected_to :action => 'index', :project_id => 'ecookbook' Chris@909: assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error] Chris@909: assert_not_nil TimeEntry.find_by_id(1) Chris@909: end Chris@909: Chris@909: def test_index_all_projects Chris@909: get :index Chris@909: assert_response :success Chris@909: assert_template 'index' Chris@909: assert_not_nil assigns(:total_hours) Chris@909: assert_equal "162.90", "%.2f" % assigns(:total_hours) Chris@909: assert_tag :form, Chris@909: :attributes => {:action => "/time_entries", :id => 'query_form'} Chris@909: end Chris@909: Chris@909: def test_index_at_project_level Chris@909: get :index, :project_id => 'ecookbook' Chris@909: assert_response :success Chris@909: assert_template 'index' Chris@909: assert_not_nil assigns(:entries) Chris@909: assert_equal 4, assigns(:entries).size Chris@909: # project and subproject Chris@909: assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort Chris@909: assert_not_nil assigns(:total_hours) Chris@909: assert_equal "162.90", "%.2f" % assigns(:total_hours) Chris@909: # display all time by default Chris@909: assert_equal '2007-03-12'.to_date, assigns(:from) Chris@909: assert_equal '2007-04-22'.to_date, assigns(:to) Chris@909: assert_tag :form, Chris@909: :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} Chris@909: end Chris@909: Chris@909: def test_index_at_project_level_with_date_range Chris@909: get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30' Chris@909: assert_response :success Chris@909: assert_template 'index' Chris@909: assert_not_nil assigns(:entries) Chris@909: assert_equal 3, assigns(:entries).size Chris@909: assert_not_nil assigns(:total_hours) Chris@909: assert_equal "12.90", "%.2f" % assigns(:total_hours) Chris@909: assert_equal '2007-03-20'.to_date, assigns(:from) Chris@909: assert_equal '2007-04-30'.to_date, assigns(:to) Chris@909: assert_tag :form, Chris@909: :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} Chris@909: end Chris@909: Chris@909: def test_index_at_project_level_with_period Chris@909: get :index, :project_id => 'ecookbook', :period => '7_days' Chris@909: assert_response :success Chris@909: assert_template 'index' Chris@909: assert_not_nil assigns(:entries) Chris@909: assert_not_nil assigns(:total_hours) Chris@909: assert_equal Date.today - 7, assigns(:from) Chris@909: assert_equal Date.today, assigns(:to) Chris@909: assert_tag :form, Chris@909: :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} Chris@909: end Chris@909: Chris@909: def test_index_one_day Chris@909: get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => "2007-03-23" Chris@909: assert_response :success Chris@909: assert_template 'index' Chris@909: assert_not_nil assigns(:total_hours) Chris@909: assert_equal "4.25", "%.2f" % assigns(:total_hours) Chris@909: assert_tag :form, Chris@909: :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} Chris@909: end Chris@909: Chris@909: def test_index_at_issue_level Chris@909: get :index, :issue_id => 1 Chris@909: assert_response :success Chris@909: assert_template 'index' Chris@909: assert_not_nil assigns(:entries) Chris@909: assert_equal 2, assigns(:entries).size Chris@909: assert_not_nil assigns(:total_hours) Chris@909: assert_equal 154.25, assigns(:total_hours) Chris@909: # display all time based on what's been logged Chris@909: assert_equal '2007-03-12'.to_date, assigns(:from) Chris@909: assert_equal '2007-04-22'.to_date, assigns(:to) Chris@909: # TODO: remove /projects/:project_id/issues/:issue_id/time_entries routes Chris@909: # to use /issues/:issue_id/time_entries Chris@909: assert_tag :form, Chris@909: :attributes => {:action => "/projects/ecookbook/issues/1/time_entries", :id => 'query_form'} Chris@909: end Chris@909: Chris@909: def test_index_atom_feed Chris@909: get :index, :project_id => 1, :format => 'atom' Chris@909: assert_response :success Chris@909: assert_equal 'application/atom+xml', @response.content_type Chris@909: assert_not_nil assigns(:items) Chris@909: assert assigns(:items).first.is_a?(TimeEntry) Chris@909: end Chris@909: Chris@909: def test_index_all_projects_csv_export Chris@909: Setting.date_format = '%m/%d/%Y' Chris@909: get :index, :format => 'csv' Chris@909: assert_response :success Chris@909: assert_equal 'text/csv', @response.content_type Chris@909: assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n") Chris@909: assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n") Chris@909: end Chris@909: Chris@909: def test_index_csv_export Chris@909: Setting.date_format = '%m/%d/%Y' Chris@909: get :index, :project_id => 1, :format => 'csv' Chris@909: assert_response :success Chris@909: assert_equal 'text/csv', @response.content_type Chris@909: assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n") Chris@909: assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n") Chris@909: end Chris@909: Chris@909: def test_csv_big_5 Chris@909: user = User.find_by_id(3) Chris@909: user.language = "zh-TW" Chris@909: assert user.save Chris@909: str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88" Chris@909: str_big5 = "\xa4@\xa4\xeb" Chris@909: if str_utf8.respond_to?(:force_encoding) Chris@909: str_utf8.force_encoding('UTF-8') Chris@909: str_big5.force_encoding('Big5') Chris@909: end Chris@909: @request.session[:user_id] = 3 Chris@909: post :create, :project_id => 1, Chris@909: :time_entry => {:comments => str_utf8, Chris@909: # Not the default activity Chris@909: :activity_id => '11', Chris@909: :issue_id => '', Chris@909: :spent_on => '2011-11-10', Chris@909: :hours => '7.3'} Chris@909: assert_redirected_to :action => 'index', :project_id => 'ecookbook' Chris@909: Chris@909: t = TimeEntry.find_by_comments(str_utf8) Chris@909: assert_not_nil t Chris@909: assert_equal 11, t.activity_id Chris@909: assert_equal 7.3, t.hours Chris@909: assert_equal 3, t.user_id Chris@909: Chris@909: get :index, :project_id => 1, :format => 'csv', Chris@909: :from => '2011-11-10', :to => '2011-11-10' Chris@909: assert_response :success Chris@909: assert_equal 'text/csv', @response.content_type Chris@909: ar = @response.body.chomp.split("\n") Chris@909: s1 = "\xa4\xe9\xb4\xc1" Chris@909: if str_utf8.respond_to?(:force_encoding) Chris@909: s1.force_encoding('Big5') Chris@909: end Chris@909: assert ar[0].include?(s1) Chris@909: assert ar[1].include?(str_big5) Chris@909: end Chris@909: Chris@909: def test_csv_cannot_convert_should_be_replaced_big_5 Chris@909: user = User.find_by_id(3) Chris@909: user.language = "zh-TW" Chris@909: assert user.save Chris@909: str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85" Chris@909: if str_utf8.respond_to?(:force_encoding) Chris@909: str_utf8.force_encoding('UTF-8') Chris@909: end Chris@909: @request.session[:user_id] = 3 Chris@909: post :create, :project_id => 1, Chris@909: :time_entry => {:comments => str_utf8, Chris@909: # Not the default activity Chris@909: :activity_id => '11', Chris@909: :issue_id => '', Chris@909: :spent_on => '2011-11-10', Chris@909: :hours => '7.3'} Chris@909: assert_redirected_to :action => 'index', :project_id => 'ecookbook' Chris@909: Chris@909: t = TimeEntry.find_by_comments(str_utf8) Chris@909: assert_not_nil t Chris@909: assert_equal 11, t.activity_id Chris@909: assert_equal 7.3, t.hours Chris@909: assert_equal 3, t.user_id Chris@909: Chris@909: get :index, :project_id => 1, :format => 'csv', Chris@909: :from => '2011-11-10', :to => '2011-11-10' Chris@909: assert_response :success Chris@909: assert_equal 'text/csv', @response.content_type Chris@909: ar = @response.body.chomp.split("\n") Chris@909: s1 = "\xa4\xe9\xb4\xc1" Chris@909: if str_utf8.respond_to?(:force_encoding) Chris@909: s1.force_encoding('Big5') Chris@909: end Chris@909: assert ar[0].include?(s1) Chris@909: s2 = ar[1].split(",")[8] Chris@909: if s2.respond_to?(:force_encoding) Chris@909: s3 = "\xa5H?" Chris@909: s3.force_encoding('Big5') Chris@909: assert_equal s3, s2 Chris@909: elsif RUBY_PLATFORM == 'java' Chris@909: assert_equal "??", s2 Chris@909: else Chris@909: assert_equal "\xa5H???", s2 Chris@909: end Chris@909: end Chris@909: Chris@909: def test_csv_tw Chris@909: with_settings :default_language => "zh-TW" do Chris@909: str1 = "test_csv_tw" Chris@909: user = User.find_by_id(3) Chris@909: te1 = TimeEntry.create(:spent_on => '2011-11-10', Chris@909: :hours => 999.9, Chris@909: :project => Project.find(1), Chris@909: :user => user, Chris@909: :activity => TimeEntryActivity.find_by_name('Design'), Chris@909: :comments => str1) Chris@909: te2 = TimeEntry.find_by_comments(str1) Chris@909: assert_not_nil te2 Chris@909: assert_equal 999.9, te2.hours Chris@909: assert_equal 3, te2.user_id Chris@909: Chris@909: get :index, :project_id => 1, :format => 'csv', Chris@909: :from => '2011-11-10', :to => '2011-11-10' Chris@909: assert_response :success Chris@909: assert_equal 'text/csv', @response.content_type Chris@909: Chris@909: ar = @response.body.chomp.split("\n") Chris@909: s2 = ar[1].split(",")[7] Chris@909: assert_equal '999.9', s2 Chris@909: Chris@909: str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)" Chris@909: if str_tw.respond_to?(:force_encoding) Chris@909: str_tw.force_encoding('UTF-8') Chris@909: end Chris@909: assert_equal str_tw, l(:general_lang_name) Chris@909: assert_equal ',', l(:general_csv_separator) Chris@909: assert_equal '.', l(:general_csv_decimal_separator) Chris@909: end Chris@909: end Chris@909: Chris@909: def test_csv_fr Chris@909: with_settings :default_language => "fr" do Chris@909: str1 = "test_csv_fr" Chris@909: user = User.find_by_id(3) Chris@909: te1 = TimeEntry.create(:spent_on => '2011-11-10', Chris@909: :hours => 999.9, Chris@909: :project => Project.find(1), Chris@909: :user => user, Chris@909: :activity => TimeEntryActivity.find_by_name('Design'), Chris@909: :comments => str1) Chris@909: te2 = TimeEntry.find_by_comments(str1) Chris@909: assert_not_nil te2 Chris@909: assert_equal 999.9, te2.hours Chris@909: assert_equal 3, te2.user_id Chris@909: Chris@909: get :index, :project_id => 1, :format => 'csv', Chris@909: :from => '2011-11-10', :to => '2011-11-10' Chris@909: assert_response :success Chris@909: assert_equal 'text/csv', @response.content_type Chris@909: Chris@909: ar = @response.body.chomp.split("\n") Chris@909: s2 = ar[1].split(";")[7] Chris@909: assert_equal '999,9', s2 Chris@909: Chris@909: str_fr = "Fran\xc3\xa7ais" Chris@909: if str_fr.respond_to?(:force_encoding) Chris@909: str_fr.force_encoding('UTF-8') Chris@909: end Chris@909: assert_equal str_fr, l(:general_lang_name) Chris@909: assert_equal ';', l(:general_csv_separator) Chris@909: assert_equal ',', l(:general_csv_decimal_separator) Chris@909: end Chris@909: end Chris@909: end