Chris@0: # -*- coding: utf-8 -*- Chris@909: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@909: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@909: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@119: require File.expand_path('../../test_helper', __FILE__) Chris@0: Chris@0: 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@1464: :custom_fields, :custom_values, Chris@1464: :projects_trackers, :custom_fields_trackers, Chris@1464: :custom_fields_projects Chris@909: Chris@909: include Redmine::I18n Chris@0: Chris@1115: def test_new_with_project_id Chris@0: @request.session[:user_id] = 3 chris@37: get :new, :project_id => 1 Chris@0: assert_response :success Chris@1115: assert_template 'new' Chris@1115: assert_select 'select[name=?]', 'time_entry[project_id]', 0 Chris@1115: assert_select 'input[name=?][value=1][type=hidden]', 'time_entry[project_id]' Chris@0: end Chris@909: Chris@1115: def test_new_with_issue_id Chris@1115: @request.session[:user_id] = 3 Chris@1115: get :new, :issue_id => 2 Chris@1115: assert_response :success Chris@1115: assert_template 'new' Chris@1115: assert_select 'select[name=?]', 'time_entry[project_id]', 0 Chris@1115: assert_select 'input[name=?][value=1][type=hidden]', 'time_entry[project_id]' Chris@1115: end Chris@1115: Chris@1115: def test_new_without_project Chris@1115: @request.session[:user_id] = 3 Chris@1115: get :new Chris@1115: assert_response :success Chris@1115: assert_template 'new' Chris@1115: assert_select 'select[name=?]', 'time_entry[project_id]' Chris@1115: assert_select 'input[name=?]', 'time_entry[project_id]', 0 Chris@1115: end Chris@1115: Chris@1115: def test_new_without_project_should_prefill_the_form Chris@1115: @request.session[:user_id] = 3 Chris@1115: get :new, :time_entry => {:project_id => '1'} Chris@1115: assert_response :success Chris@1115: assert_template 'new' Chris@1115: assert_select 'select[name=?]', 'time_entry[project_id]' do Chris@1115: assert_select 'option[value=1][selected=selected]' Chris@1115: end Chris@1115: assert_select 'input[name=?]', 'time_entry[project_id]', 0 Chris@1115: end Chris@1115: Chris@1115: def test_new_without_project_should_deny_without_permission Chris@1115: Role.all.each {|role| role.remove_permission! :log_time} Chris@1115: @request.session[:user_id] = 3 Chris@1115: Chris@1115: get :new Chris@1115: assert_response 403 Chris@1115: end Chris@1115: Chris@1115: def test_new_should_select_default_activity chris@37: @request.session[:user_id] = 3 chris@37: get :new, :project_id => 1 chris@37: assert_response :success Chris@1115: assert_select 'select[name=?]', 'time_entry[activity_id]' do Chris@1115: assert_select 'option[selected=selected]', :text => 'Development' Chris@1115: end Chris@1115: end Chris@1115: Chris@1115: def test_new_should_only_show_active_time_entry_activities Chris@1115: @request.session[:user_id] = 3 Chris@1115: get :new, :project_id => 1 Chris@1115: assert_response :success Chris@1115: assert_no_tag 'option', :content => 'Inactive Activity' chris@37: end chris@37: Chris@0: def test_get_edit_existing_time Chris@0: @request.session[:user_id] = 2 Chris@0: get :edit, :id => 2, :project_id => nil Chris@0: assert_response :success Chris@0: assert_template 'edit' Chris@0: # Default activity selected chris@37: assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/time_entries/2' } Chris@0: end Chris@909: Chris@0: def test_get_edit_with_an_existing_time_entry_with_inactive_activity Chris@0: te = TimeEntry.find(1) Chris@0: te.activity = TimeEntryActivity.find_by_name("Inactive Activity") Chris@0: te.save! Chris@0: Chris@0: @request.session[:user_id] = 1 Chris@0: get :edit, :project_id => 1, :id => 1 Chris@0: assert_response :success Chris@0: assert_template 'edit' Chris@0: # Blank option since nothing is pre-selected Chris@0: assert_tag :tag => 'option', :content => '--- Please select ---' Chris@0: end Chris@909: chris@37: def test_post_create Chris@0: # TODO: should POST to issues’ time log instead of project. change form Chris@0: # and routing Chris@0: @request.session[:user_id] = 3 chris@37: post :create, :project_id => 1, Chris@0: :time_entry => {:comments => 'Some work on TimelogControllerTest', Chris@0: # Not the default activity Chris@0: :activity_id => '11', Chris@0: :spent_on => '2008-03-14', Chris@0: :issue_id => '1', Chris@0: :hours => '7.3'} chris@37: assert_redirected_to :action => 'index', :project_id => 'ecookbook' Chris@909: Chris@0: i = Issue.find(1) Chris@0: t = TimeEntry.find_by_comments('Some work on TimelogControllerTest') Chris@0: assert_not_nil t Chris@0: assert_equal 11, t.activity_id Chris@0: assert_equal 7.3, t.hours Chris@0: assert_equal 3, t.user_id Chris@0: assert_equal i, t.issue Chris@0: assert_equal i.project, t.project Chris@0: end Chris@119: Chris@119: def test_post_create_with_blank_issue Chris@119: # TODO: should POST to issues’ time log instead of project. change form Chris@119: # and routing Chris@119: @request.session[:user_id] = 3 Chris@119: post :create, :project_id => 1, Chris@119: :time_entry => {:comments => 'Some work on TimelogControllerTest', Chris@119: # Not the default activity Chris@119: :activity_id => '11', Chris@119: :issue_id => '', Chris@119: :spent_on => '2008-03-14', Chris@119: :hours => '7.3'} Chris@119: assert_redirected_to :action => 'index', :project_id => 'ecookbook' Chris@909: Chris@119: t = TimeEntry.find_by_comments('Some work on TimelogControllerTest') Chris@119: assert_not_nil t Chris@119: assert_equal 11, t.activity_id Chris@119: assert_equal 7.3, t.hours Chris@119: assert_equal 3, t.user_id Chris@119: end Chris@909: Chris@1115: def test_create_and_continue Chris@1115: @request.session[:user_id] = 2 Chris@1115: post :create, :project_id => 1, Chris@1115: :time_entry => {:activity_id => '11', Chris@1115: :issue_id => '', Chris@1115: :spent_on => '2008-03-14', Chris@1115: :hours => '7.3'}, Chris@1115: :continue => '1' Chris@1115: assert_redirected_to '/projects/ecookbook/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=' Chris@1115: end Chris@1115: Chris@1115: def test_create_and_continue_with_issue_id Chris@1115: @request.session[:user_id] = 2 Chris@1115: post :create, :project_id => 1, Chris@1115: :time_entry => {:activity_id => '11', Chris@1115: :issue_id => '1', Chris@1115: :spent_on => '2008-03-14', Chris@1115: :hours => '7.3'}, Chris@1115: :continue => '1' Chris@1115: assert_redirected_to '/projects/ecookbook/issues/1/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=1' Chris@1115: end Chris@1115: Chris@1115: def test_create_and_continue_without_project Chris@1115: @request.session[:user_id] = 2 Chris@1115: post :create, :time_entry => {:project_id => '1', Chris@1115: :activity_id => '11', Chris@1115: :issue_id => '', Chris@1115: :spent_on => '2008-03-14', Chris@1115: :hours => '7.3'}, Chris@1115: :continue => '1' Chris@1115: Chris@1115: assert_redirected_to '/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=&time_entry%5Bproject_id%5D=1' Chris@1115: end Chris@1115: 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@1115: def test_create_with_failure Chris@1115: @request.session[:user_id] = 2 Chris@1115: post :create, :project_id => 1, Chris@1115: :time_entry => {:activity_id => '', Chris@1115: :issue_id => '', Chris@1115: :spent_on => '2008-03-14', Chris@1115: :hours => '7.3'} Chris@1115: Chris@1115: assert_response :success Chris@1115: assert_template 'new' Chris@1115: end Chris@1115: Chris@1115: def test_create_without_project Chris@1115: @request.session[:user_id] = 2 Chris@1115: assert_difference 'TimeEntry.count' do Chris@1115: post :create, :time_entry => {:project_id => '1', Chris@1115: :activity_id => '11', Chris@1115: :issue_id => '', Chris@1115: :spent_on => '2008-03-14', Chris@1115: :hours => '7.3'} Chris@1115: end Chris@1115: Chris@1115: assert_redirected_to '/projects/ecookbook/time_entries' Chris@1517: time_entry = TimeEntry.order('id DESC').first Chris@1115: assert_equal 1, time_entry.project_id Chris@1115: end Chris@1115: Chris@1115: def test_create_without_project_should_fail_with_issue_not_inside_project Chris@1115: @request.session[:user_id] = 2 Chris@1115: assert_no_difference 'TimeEntry.count' do Chris@1115: post :create, :time_entry => {:project_id => '1', Chris@1115: :activity_id => '11', Chris@1115: :issue_id => '5', Chris@1115: :spent_on => '2008-03-14', Chris@1115: :hours => '7.3'} Chris@1115: end Chris@1115: Chris@1115: assert_response :success Chris@1115: assert assigns(:time_entry).errors[:issue_id].present? Chris@1115: end Chris@1115: Chris@1115: def test_create_without_project_should_deny_without_permission Chris@1115: @request.session[:user_id] = 2 Chris@1115: Project.find(3).disable_module!(:time_tracking) Chris@1115: Chris@1115: assert_no_difference 'TimeEntry.count' do Chris@1115: post :create, :time_entry => {:project_id => '3', Chris@1115: :activity_id => '11', Chris@1115: :issue_id => '', Chris@1115: :spent_on => '2008-03-14', Chris@1115: :hours => '7.3'} Chris@1115: end Chris@1115: Chris@1115: assert_response 403 Chris@1115: end Chris@1115: Chris@1115: def test_create_without_project_with_failure Chris@1115: @request.session[:user_id] = 2 Chris@1115: assert_no_difference 'TimeEntry.count' do Chris@1115: post :create, :time_entry => {:project_id => '1', Chris@1115: :activity_id => '11', Chris@1115: :issue_id => '', Chris@1115: :spent_on => '2008-03-14', Chris@1115: :hours => ''} Chris@1115: end Chris@1115: Chris@1115: assert_response :success Chris@1115: assert_tag 'select', :attributes => {:name => 'time_entry[project_id]'}, Chris@1115: :child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}} Chris@1115: end Chris@1115: Chris@0: def test_update Chris@0: entry = TimeEntry.find(1) Chris@0: assert_equal 1, entry.issue_id Chris@0: assert_equal 2, entry.user_id Chris@909: Chris@0: @request.session[:user_id] = 1 chris@37: put :update, :id => 1, Chris@0: :time_entry => {:issue_id => '2', Chris@0: :hours => '8'} chris@37: assert_redirected_to :action => 'index', :project_id => 'ecookbook' Chris@0: entry.reload Chris@909: Chris@0: assert_equal 8, entry.hours Chris@0: assert_equal 2, entry.issue_id Chris@0: assert_equal 2, entry.user_id Chris@0: end Chris@441: Chris@1517: def test_update_should_allow_to_change_issue_to_another_project Chris@1517: entry = TimeEntry.generate!(:issue_id => 1) Chris@1517: Chris@1517: @request.session[:user_id] = 1 Chris@1517: put :update, :id => entry.id, :time_entry => {:issue_id => '5'} Chris@1517: assert_response 302 Chris@1517: entry.reload Chris@1517: Chris@1517: assert_equal 5, entry.issue_id Chris@1517: assert_equal 3, entry.project_id Chris@1517: end Chris@1517: Chris@1517: def test_update_should_not_allow_to_change_issue_to_an_invalid_project Chris@1517: entry = TimeEntry.generate!(:issue_id => 1) Chris@1517: Project.find(3).disable_module!(:time_tracking) Chris@1517: Chris@1517: @request.session[:user_id] = 1 Chris@1517: put :update, :id => entry.id, :time_entry => {:issue_id => '5'} Chris@1517: assert_response 200 Chris@1517: assert_include "Issue is invalid", assigns(:time_entry).errors.full_messages Chris@1517: end Chris@1517: Chris@441: def test_get_bulk_edit Chris@441: @request.session[:user_id] = 2 Chris@441: get :bulk_edit, :ids => [1, 2] Chris@441: assert_response :success Chris@441: assert_template 'bulk_edit' Chris@909: Chris@1464: assert_select 'ul#bulk-selection' do Chris@1464: assert_select 'li', 2 Chris@1464: assert_select 'li a', :text => '03/23/2007 - eCookbook: 4.25 hours' Chris@1464: end Chris@1115: Chris@1464: assert_select 'form#bulk_edit_form[action=?]', '/time_entries/bulk_update' do Chris@1464: # System wide custom field Chris@1464: assert_select 'select[name=?]', 'time_entry[custom_field_values][10]' Chris@1464: Chris@1464: # Activities Chris@1464: assert_select 'select[name=?]', 'time_entry[activity_id]' do Chris@1464: assert_select 'option[value=]', :text => '(No change)' Chris@1464: assert_select 'option[value=9]', :text => 'Design' Chris@1464: end Chris@1115: end Chris@441: end Chris@441: Chris@441: def test_get_bulk_edit_on_different_projects Chris@441: @request.session[:user_id] = 2 Chris@441: get :bulk_edit, :ids => [1, 2, 6] Chris@441: assert_response :success Chris@441: assert_template 'bulk_edit' Chris@441: end Chris@441: Chris@441: def test_bulk_update Chris@441: @request.session[:user_id] = 2 Chris@441: # update time entry activity Chris@441: post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9} Chris@909: Chris@441: assert_response 302 Chris@441: # check that the issues were updated Chris@1517: assert_equal [9, 9], TimeEntry.where(:id => [1, 2]).collect {|i| i.activity_id} Chris@441: end Chris@441: Chris@1115: def test_bulk_update_with_failure Chris@1115: @request.session[:user_id] = 2 Chris@1115: post :bulk_update, :ids => [1, 2], :time_entry => { :hours => 'A'} Chris@1115: Chris@1115: assert_response 302 Chris@1115: assert_match /Failed to save 2 time entrie/, flash[:error] Chris@1115: end Chris@1115: Chris@441: def test_bulk_update_on_different_projects Chris@441: @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@441: # update time entry activity Chris@441: post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 } Chris@909: Chris@441: assert_response 302 Chris@441: # check that the issues were updated Chris@1517: assert_equal [9, 9, 9], TimeEntry.where(:id => [1, 2, 4]).collect {|i| i.activity_id} Chris@441: end Chris@441: Chris@441: def test_bulk_update_on_different_projects_without_rights Chris@441: @request.session[:user_id] = 3 Chris@441: user = User.find(3) Chris@441: action = { :controller => "timelog", :action => "bulk_update" } Chris@441: assert user.allowed_to?(action, TimeEntry.find(1).project) Chris@441: assert ! user.allowed_to?(action, TimeEntry.find(5).project) Chris@441: post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 } Chris@441: assert_response 403 Chris@441: end Chris@441: Chris@441: def test_bulk_update_custom_field Chris@441: @request.session[:user_id] = 2 Chris@441: post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} } Chris@909: Chris@441: assert_response 302 Chris@1517: assert_equal ["0", "0"], TimeEntry.where(:id => [1, 2]).collect {|i| i.custom_value_for(10).value} Chris@441: end Chris@441: Chris@441: def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter Chris@441: @request.session[:user_id] = 2 Chris@441: post :bulk_update, :ids => [1,2], :back_url => '/time_entries' Chris@441: Chris@441: assert_response :redirect Chris@441: assert_redirected_to '/time_entries' Chris@441: end Chris@441: Chris@441: def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host Chris@441: @request.session[:user_id] = 2 Chris@441: post :bulk_update, :ids => [1,2], :back_url => 'http://google.com' Chris@441: Chris@441: assert_response :redirect Chris@441: assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier Chris@441: 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@0: def test_destroy Chris@0: @request.session[:user_id] = 2 chris@37: delete :destroy, :id => 1 chris@37: assert_redirected_to :action => 'index', :project_id => 'ecookbook' Chris@0: assert_equal I18n.t(:notice_successful_delete), flash[:notice] Chris@0: assert_nil TimeEntry.find_by_id(1) Chris@0: end Chris@909: Chris@0: def test_destroy_should_fail Chris@0: # simulate that this fails (e.g. due to a plugin), see #5700 Chris@441: TimeEntry.any_instance.expects(:destroy).returns(false) Chris@0: Chris@0: @request.session[:user_id] = 2 chris@37: delete :destroy, :id => 1 chris@37: assert_redirected_to :action => 'index', :project_id => 'ecookbook' Chris@0: assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error] Chris@0: assert_not_nil TimeEntry.find_by_id(1) Chris@0: end Chris@909: chris@37: def test_index_all_projects chris@37: get :index Chris@0: assert_response :success chris@37: assert_template 'index' Chris@0: assert_not_nil assigns(:total_hours) Chris@0: assert_equal "162.90", "%.2f" % assigns(:total_hours) Chris@441: assert_tag :form, Chris@441: :attributes => {:action => "/time_entries", :id => 'query_form'} Chris@0: end Chris@909: Chris@1115: def test_index_all_projects_should_show_log_time_link Chris@1115: @request.session[:user_id] = 2 Chris@1115: get :index Chris@1115: assert_response :success Chris@1115: assert_template 'index' Chris@1115: assert_tag 'a', :attributes => {:href => '/time_entries/new'}, :content => /Log time/ Chris@1115: end Chris@1115: Chris@1464: def test_index_my_spent_time Chris@1464: @request.session[:user_id] = 2 Chris@1464: get :index, :user_id => 'me' Chris@1464: assert_response :success Chris@1464: assert_template 'index' Chris@1464: assert assigns(:entries).all? {|entry| entry.user_id == 2} Chris@1464: end Chris@1464: chris@37: def test_index_at_project_level Chris@441: get :index, :project_id => 'ecookbook' Chris@0: assert_response :success chris@37: assert_template 'index' Chris@0: assert_not_nil assigns(:entries) Chris@0: assert_equal 4, assigns(:entries).size Chris@0: # project and subproject Chris@0: assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort Chris@0: assert_not_nil assigns(:total_hours) Chris@0: assert_equal "162.90", "%.2f" % assigns(:total_hours) Chris@441: assert_tag :form, Chris@441: :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} Chris@0: end Chris@909: Chris@1464: def test_index_with_display_subprojects_issues_to_false_should_not_include_subproject_entries Chris@1464: entry = TimeEntry.generate!(:project => Project.find(3)) Chris@1464: Chris@1464: with_settings :display_subprojects_issues => '0' do Chris@1464: get :index, :project_id => 'ecookbook' Chris@1464: assert_response :success Chris@1464: assert_template 'index' Chris@1464: assert_not_include entry, assigns(:entries) Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: def test_index_with_display_subprojects_issues_to_false_and_subproject_filter_should_include_subproject_entries Chris@1464: entry = TimeEntry.generate!(:project => Project.find(3)) Chris@1464: Chris@1464: with_settings :display_subprojects_issues => '0' do Chris@1464: get :index, :project_id => 'ecookbook', :subproject_id => 3 Chris@1464: assert_response :success Chris@1464: assert_template 'index' Chris@1464: assert_include entry, assigns(:entries) Chris@1464: end Chris@1464: end Chris@1464: chris@37: def test_index_at_project_level_with_date_range Chris@1464: get :index, :project_id => 'ecookbook', Chris@1464: :f => ['spent_on'], Chris@1464: :op => {'spent_on' => '><'}, Chris@1464: :v => {'spent_on' => ['2007-03-20', '2007-04-30']} Chris@1464: assert_response :success Chris@1464: assert_template 'index' Chris@1464: assert_not_nil assigns(:entries) Chris@1464: assert_equal 3, assigns(:entries).size Chris@1464: assert_not_nil assigns(:total_hours) Chris@1464: assert_equal "12.90", "%.2f" % assigns(:total_hours) Chris@1464: assert_tag :form, Chris@1464: :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} Chris@1464: end Chris@1464: Chris@1464: def test_index_at_project_level_with_date_range_using_from_and_to_params Chris@441: get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30' Chris@0: assert_response :success chris@37: assert_template 'index' Chris@0: assert_not_nil assigns(:entries) Chris@0: assert_equal 3, assigns(:entries).size Chris@0: assert_not_nil assigns(:total_hours) Chris@0: assert_equal "12.90", "%.2f" % assigns(:total_hours) Chris@441: assert_tag :form, Chris@441: :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} Chris@0: end Chris@0: chris@37: def test_index_at_project_level_with_period Chris@1464: get :index, :project_id => 'ecookbook', Chris@1464: :f => ['spent_on'], Chris@1464: :op => {'spent_on' => '>t-'}, Chris@1464: :v => {'spent_on' => ['7']} Chris@0: assert_response :success chris@37: assert_template 'index' Chris@0: assert_not_nil assigns(:entries) Chris@0: assert_not_nil assigns(:total_hours) Chris@441: assert_tag :form, Chris@441: :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} Chris@0: end Chris@0: chris@37: def test_index_at_issue_level chris@37: get :index, :issue_id => 1 Chris@0: assert_response :success chris@37: assert_template 'index' Chris@0: assert_not_nil assigns(:entries) Chris@0: assert_equal 2, assigns(:entries).size Chris@0: assert_not_nil assigns(:total_hours) Chris@0: assert_equal 154.25, assigns(:total_hours) Chris@1115: # display all time Chris@1115: assert_nil assigns(:from) Chris@1115: assert_nil assigns(:to) Chris@441: # TODO: remove /projects/:project_id/issues/:issue_id/time_entries routes Chris@441: # to use /issues/:issue_id/time_entries Chris@441: assert_tag :form, Chris@441: :attributes => {:action => "/projects/ecookbook/issues/1/time_entries", :id => 'query_form'} Chris@0: end Chris@909: Chris@1115: def test_index_should_sort_by_spent_on_and_created_on Chris@1115: 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) Chris@1115: 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) Chris@1115: 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) Chris@1115: Chris@1464: get :index, :project_id => 1, Chris@1464: :f => ['spent_on'], Chris@1464: :op => {'spent_on' => '><'}, Chris@1464: :v => {'spent_on' => ['2012-06-15', '2012-06-16']} Chris@1115: assert_response :success Chris@1115: assert_equal [t2, t1, t3], assigns(:entries) Chris@1115: Chris@1464: get :index, :project_id => 1, Chris@1464: :f => ['spent_on'], Chris@1464: :op => {'spent_on' => '><'}, Chris@1464: :v => {'spent_on' => ['2012-06-15', '2012-06-16']}, Chris@1464: :sort => 'spent_on' Chris@1115: assert_response :success Chris@1115: assert_equal [t3, t1, t2], assigns(:entries) Chris@1115: end Chris@1115: Chris@1464: def test_index_with_filter_on_issue_custom_field Chris@1464: issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'}) Chris@1464: entry = TimeEntry.generate!(:issue => issue, :hours => 2.5) Chris@1464: Chris@1464: get :index, :f => ['issue.cf_2'], :op => {'issue.cf_2' => '='}, :v => {'issue.cf_2' => ['filter_on_issue_custom_field']} Chris@1464: assert_response :success Chris@1464: assert_equal [entry], assigns(:entries) Chris@1464: end Chris@1464: Chris@1464: def test_index_with_issue_custom_field_column Chris@1464: issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'}) Chris@1464: entry = TimeEntry.generate!(:issue => issue, :hours => 2.5) Chris@1464: Chris@1464: get :index, :c => %w(project spent_on issue comments hours issue.cf_2) Chris@1464: assert_response :success Chris@1464: assert_include :'issue.cf_2', assigns(:query).column_names Chris@1464: assert_select 'td.issue_cf_2', :text => 'filter_on_issue_custom_field' Chris@1464: end Chris@1464: Chris@1464: def test_index_with_time_entry_custom_field_column Chris@1464: field = TimeEntryCustomField.generate!(:field_format => 'string') Chris@1464: entry = TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value'}) Chris@1464: field_name = "cf_#{field.id}" Chris@1464: Chris@1464: get :index, :c => ["hours", field_name] Chris@1464: assert_response :success Chris@1464: assert_include field_name.to_sym, assigns(:query).column_names Chris@1464: assert_select "td.#{field_name}", :text => 'CF Value' Chris@1464: end Chris@1464: Chris@1464: def test_index_with_time_entry_custom_field_sorting Chris@1464: field = TimeEntryCustomField.generate!(:field_format => 'string', :name => 'String Field') Chris@1464: TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 1'}) Chris@1464: TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 3'}) Chris@1464: TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 2'}) Chris@1464: field_name = "cf_#{field.id}" Chris@1464: Chris@1464: get :index, :c => ["hours", field_name], :sort => field_name Chris@1464: assert_response :success Chris@1464: assert_include field_name.to_sym, assigns(:query).column_names Chris@1464: assert_select "th a.sort", :text => 'String Field' Chris@1464: Chris@1464: # Make sure that values are properly sorted Chris@1464: values = assigns(:entries).map {|e| e.custom_field_value(field)}.compact Chris@1464: assert_equal 3, values.size Chris@1464: assert_equal values.sort, values Chris@1464: end Chris@1464: chris@37: def test_index_atom_feed chris@37: get :index, :project_id => 1, :format => 'atom' Chris@0: assert_response :success Chris@0: assert_equal 'application/atom+xml', @response.content_type Chris@0: assert_not_nil assigns(:items) Chris@0: assert assigns(:items).first.is_a?(TimeEntry) Chris@0: end Chris@909: Chris@1464: def test_index_at_project_level_should_include_csv_export_dialog Chris@1464: get :index, :project_id => 'ecookbook', Chris@1464: :f => ['spent_on'], Chris@1464: :op => {'spent_on' => '>='}, Chris@1464: :v => {'spent_on' => ['2007-04-01']}, Chris@1464: :c => ['spent_on', 'user'] Chris@1464: assert_response :success Chris@1464: Chris@1464: assert_select '#csv-export-options' do Chris@1464: assert_select 'form[action=?][method=get]', '/projects/ecookbook/time_entries.csv' do Chris@1464: # filter Chris@1464: assert_select 'input[name=?][value=?]', 'f[]', 'spent_on' Chris@1464: assert_select 'input[name=?][value=?]', 'op[spent_on]', '>=' Chris@1464: assert_select 'input[name=?][value=?]', 'v[spent_on][]', '2007-04-01' Chris@1464: # columns Chris@1464: assert_select 'input[name=?][value=?]', 'c[]', 'spent_on' Chris@1464: assert_select 'input[name=?][value=?]', 'c[]', 'user' Chris@1464: assert_select 'input[name=?]', 'c[]', 2 Chris@1464: end Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: def test_index_cross_project_should_include_csv_export_dialog Chris@1464: get :index Chris@1464: assert_response :success Chris@1464: Chris@1464: assert_select '#csv-export-options' do Chris@1464: assert_select 'form[action=?][method=get]', '/time_entries.csv' Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: def test_index_at_issue_level_should_include_csv_export_dialog Chris@1464: get :index, :project_id => 'ecookbook', :issue_id => 3 Chris@1464: assert_response :success Chris@1464: Chris@1464: assert_select '#csv-export-options' do Chris@1464: assert_select 'form[action=?][method=get]', '/projects/ecookbook/issues/3/time_entries.csv' Chris@1464: end Chris@1464: end Chris@1464: Chris@1464: def test_index_csv_all_projects Chris@0: Setting.date_format = '%m/%d/%Y' chris@37: get :index, :format => 'csv' Chris@0: assert_response :success Chris@1464: assert_equal 'text/csv; header=present', response.content_type Chris@0: end Chris@909: Chris@1464: def test_index_csv Chris@0: Setting.date_format = '%m/%d/%Y' chris@37: get :index, :project_id => 1, :format => 'csv' Chris@0: assert_response :success Chris@1464: assert_equal 'text/csv; header=present', response.content_type Chris@909: end Chris@0: end