diff --git a/app/models/issue.rb b/app/models/issue.rb index 5c1911eb8d..4faa063762 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -1679,8 +1679,28 @@ class Issue < ActiveRecord::Base end def after_project_change - # Update project_id on related time entries - TimeEntry.where({:issue_id => id}).update_all(["project_id = ?", project_id]) + # Update project_id and activity_id on related time entries + activities = project.activities + time_entries.each do |time_entry| + curr_activity = time_entry.activity + curr_activity_id = (curr_activity.parent_id || curr_activity.id) + new_activity = + activities.detect do |activity| + activity.id == curr_activity_id || activity.parent_id == curr_activity_id + end + + time_entry.project = project + time_entry.activity = new_activity unless curr_activity == new_activity + unless time_entry.save + errors.add( + :base, + l(:error_move_of_time_entries_not_possible, + :id => "##{time_entry.id}", + :errors => time_entry.errors.full_messages.join(", ")) + ) + raise ActiveRecord::Rollback + end + end # Delete issue relations unless Setting.cross_project_issue_relations? diff --git a/config/locales/en.yml b/config/locales/en.yml index 2eb4af0e13..8693538ac1 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -229,6 +229,7 @@ en: error_no_tracker_allowed_for_new_issue_in_project: "The project doesn't have any trackers for which you can create an issue" error_no_projects_with_tracker_allowed_for_new_issue: "There are no projects with trackers for which you can create an issue" error_move_of_child_not_possible: "Subtask %{child} could not be moved to the new project: %{errors}" + error_move_of_time_entries_not_possible: "Spent time %{id} could not be moved to the new project: %{errors}" error_cannot_reassign_time_entries_to_an_issue_about_to_be_deleted: "Spent time cannot be reassigned to an issue that is about to be deleted" warning_fields_cleared_on_bulk_edit: "Changes will result in the automatic deletion of values from one or more fields on the selected objects" error_exceeds_maximum_hours_per_day: "Cannot log more than %{max_hours} hours on the same day (%{logged_hours} hours have already been logged)" diff --git a/test/unit/issue_test.rb b/test/unit/issue_test.rb index cc53598622..255476754f 100644 --- a/test/unit/issue_test.rb +++ b/test/unit/issue_test.rb @@ -1550,6 +1550,35 @@ class IssueTest < ActiveSupport::TestCase issue.save! end + def test_after_project_change_should_update_project_and_activity_of_time_entries + time_entry = TimeEntry.find(3) + assert_equal [1, 9], [time_entry.project_id, time_entry.activity_id] + + activity = TimeEntryActivity.create!(:name => 'Design', :parent_id => 9, :project_id => 3) + issue = Issue.find(3) + issue.project = Project.find(3) + + assert issue.save + + time_entry.reload + assert_equal [3, activity.id], [time_entry.project_id, time_entry.activity_id] + end + + def test_after_project_change_should_not_update_project_and_activity_of_time_entries_when_activity_is_inactive + time_entry = TimeEntry.find(3) + assert_equal [1, 9], [time_entry.project_id, time_entry.activity_id] + + TimeEntryActivity.create!(:name => 'Design', :active => false, :parent_id => 9, :project_id => 3) + issue = Issue.find(3) + issue.project = Project.find(3) + + assert !issue.save + assert_match /\ASpent time #3 could not be moved to the new project:/, issue.errors.full_messages.first + + time_entry.reload + assert_equal [1, 9], [time_entry.project_id, time_entry.activity_id] + end + def test_adding_journal_should_update_timestamp issue = Issue.find(1) updated_on_was = issue.updated_on