From 76ade8d054b7929d71e6f0847497b27c50d3d11b Mon Sep 17 00:00:00 2001
From: ishikawa999
<%= f.select :default_time_entry_activity_id, options_from_collection_for_select(TimeEntryActivity.active.shared, :id, :name, @role.default_time_entry_activity_id), :include_blank => l(:label_none) %>
+ <% end %> + <% if @role.new_record? && @roles.any? %><%= select_tag(:copy_workflow_from, content_tag("option") + options_from_collection_for_select(@roles, :id, :name, params[:copy_workflow_from] || @copy_from.try(:id))) %>
diff --git a/app/views/timelog/new.js.erb b/app/views/timelog/new.js.erb index 4cba8cfe6c..70b731e267 100644 --- a/app/views/timelog/new.js.erb +++ b/app/views/timelog/new.js.erb @@ -1,2 +1,2 @@ -$('#time_entry_activity_id').html('<%= escape_javascript options_for_select(activity_collection_for_select_options(@time_entry), @time_entry.activity_id) %>'); +$('#time_entry_activity_id').html('<%= escape_javascript options_for_select(activity_collection_for_select_options(@time_entry), default_activity(@time_entry)) %>'); $('#time_entry_issue').html('<%= escape_javascript link_to_issue(@time_entry.issue) if @time_entry.issue.try(:visible?) %>'); diff --git a/db/migrate/20180806215628_add_default_time_entry_activity_to_roles.rb b/db/migrate/20180806215628_add_default_time_entry_activity_to_roles.rb new file mode 100644 index 0000000000..1afc016c4c --- /dev/null +++ b/db/migrate/20180806215628_add_default_time_entry_activity_to_roles.rb @@ -0,0 +1,5 @@ +class AddDefaultTimeEntryActivityToRoles < ActiveRecord::Migration[5.2] + def change + add_column :roles, :default_time_entry_activity_id, :int + end +end diff --git a/test/functional/timelog_controller_test.rb b/test/functional/timelog_controller_test.rb index 321ed4d217..deb75a5bb1 100644 --- a/test/functional/timelog_controller_test.rb +++ b/test/functional/timelog_controller_test.rb @@ -89,7 +89,20 @@ class TimelogControllerTest < Redmine::ControllerTest assert_response 403 end - def test_new_should_select_default_activity + def test_new_should_select_default_role_activity + developer = Role.find(2) + developer.default_time_entry_activity_id = 9 + developer.save! + + @request.session[:user_id] = 3 + get :new, :params => {:project_id => 1} + assert_response :success + assert_select 'select[name=?]', 'time_entry[activity_id]' do + assert_select 'option[selected=selected]', :text => 'Design' + end + end + + def test_new_should_select_default_global_activity_for_user_roles_without_default_activities @request.session[:user_id] = 3 get :new, :params => {:project_id => 1} assert_response :success diff --git a/test/unit/time_entry_activity_test.rb b/test/unit/time_entry_activity_test.rb index dc2e44c13a..49c689a65d 100644 --- a/test/unit/time_entry_activity_test.rb +++ b/test/unit/time_entry_activity_test.rb @@ -231,4 +231,86 @@ class TimeEntryActivityTest < ActiveSupport::TestCase assert_not_equal TimeEntryActivity.default(project).id, 10 assert_equal TimeEntryActivity.default(project).id, project_specific_default_activity.id end + + def test_default_activity_id_without_user_and_project_should_return_global_default_activity + assert_equal 10, TimeEntryActivity.default_activity_id + end + + def test_default_activity_id_with_user_and_project_should_return_role_default_activity + # set a default activity for Manager role + manager = Role.find(1) + manager.default_time_entry_activity_id = 9 + manager.save + + assert_equal 9, TimeEntryActivity.default_activity_id(User.find(2), Project.find(1)) + end + + def test_default_activity_id_with_user_and_project_should_consider_role_position + project = Project.find(1) + user = User.find(2) + + # set a default activity for Manager role + manager = Role.find(1) + manager.default_time_entry_activity_id = 9 + manager.save! + + # set a default activity for Developer role + # and set the role position first + developer = Role.find(2) + developer.default_time_entry_activity_id = 11 + developer.position = 1 + developer.save! + + member = Member.find_or_initialize_by(:project_id => project.id, :user_id => user.id) + member.role_ids = [1, 2] + member.save! + + assert_equal 11, TimeEntryActivity.default_activity_id(user, project) + end + + def test_default_activity_id_should_include_only_available_activities + # set a default activity for Manager role + manager = Role.find(1) + manager.default_time_entry_activity_id = 9 + manager.save! + + project = Project.find(1) + + # disable role default activity + disable_activity = TimeEntryActivity.new({:name => "QA", :project => project, :parent => TimeEntryActivity.find(9), :active => false}) + disable_activity.save! + + assert_equal 10, TimeEntryActivity.default_activity_id(User.find(2), project) + end + + def test_default_activity_id_should_selected_from_highest_priority_of_multiple_default_activity_candidates + project = Project.find(1) + + manager = Role.find(1) + manager.default_time_entry_activity_id = 9 + manager.save + + # Returns the role_default_activity with the highest priority + assert_equal 9, TimeEntryActivity.default_activity_id(User.find(2), project) + + # Returns the child activity of role_default_activity if there is an activity that has the id of role_default_activity as parent_id + design_project_activity = TimeEntryActivity.create!(name: 'Design', parent_id: 9, project_id: project.id, is_default: false) + development_project_activity = TimeEntryActivity.create!(name: 'Development', parent_id: 10, project_id: project.id, is_default: true) + qa_project_activity = TimeEntryActivity.create!(name: 'QA', parent_id: 11, project_id: project.id, is_default: false) + assert_equal design_project_activity.id, TimeEntryActivity.default_activity_id(User.find(2), project) + + # Returns default project activity if role_default_activity is not present + manager.default_time_entry_activity_id = nil + manager.save + assert_equal development_project_activity.id, TimeEntryActivity.default_activity_id(User.find(2), project) + + # Returns global default activity if role_default_activity and project activities are not present + [design_project_activity, development_project_activity, qa_project_activity].each {|activity| activity.destroy} + TimeEntryActivity.find(11).update(is_default: true) + assert_equal 11, TimeEntryActivity.default_activity_id(User.find(2), project) + + # If there is only one activity available, it returns that activity. + [TimeEntryActivity.find(10), TimeEntryActivity.find(11)].each {|a| a.update(active: false)} + assert_equal 9, TimeEntryActivity.default_activity_id(User.find(2), project) + end end -- 2.30.2