Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Only use user-supplied values when updating progress

Christophe Bliard7a588db68a97
diff --git a/app/controllers/work_packages/progress_controller.rb b/app/controllers/work_packages/progress_controller.rb
index 35a51045aa9..5f5b82b93bd 100644
--- a/app/controllers/work_packages/progress_controller.rb
+++ b/app/controllers/work_packages/progress_controller.rb
@@ -118,9 +118,12 @@ class WorkPackages::ProgressController < ApplicationController
end
def touched_field_map
- params.require(:work_package).permit("estimated_hours_touched",
- "remaining_hours_touched",
- "status_id_touched").to_h
+ params.require(:work_package)
+ .slice("estimated_hours_touched",
+ "remaining_hours_touched",
+ "status_id_touched")
+ .transform_values { _1 == "true" }
+ .permit!
end
def extract_persisted_progress_attributes
@@ -131,7 +134,12 @@ class WorkPackages::ProgressController < ApplicationController
def work_package_params
params.require(:work_package)
- .permit(allowed_params)
+ .slice(*allowed_touched_params)
+ .permit!
+ end
+
+ def allowed_touched_params
+ allowed_params.filter { touched?(_1) }
end
def allowed_params
@@ -142,24 +150,8 @@ class WorkPackages::ProgressController < ApplicationController
end
end
- def filtered_work_package_params
- {}.tap do |filtered_params|
- filtered_params[:estimated_hours] = work_package_params["estimated_hours"] if estimated_hours_touched?
- filtered_params[:remaining_hours] = work_package_params["remaining_hours"] if remaining_hours_touched?
- filtered_params[:status_id] = work_package_params["status_id"] if status_id_touched?
- end
- end
-
- def estimated_hours_touched?
- params.require(:work_package)[:estimated_hours_touched] == "true"
- end
-
- def remaining_hours_touched?
- params.require(:work_package)[:remaining_hours_touched] == "true"
- end
-
- def status_id_touched?
- params.require(:work_package)[:status_id_touched] == "true"
+ def touched?(field)
+ touched_field_map[:"#{field}_touched"]
end
def build_up_work_package
@@ -167,7 +159,7 @@ class WorkPackages::ProgressController < ApplicationController
.new(user: current_user,
model: @work_package,
contract_class: WorkPackages::CreateContract)
- .call(filtered_work_package_params)
+ .call(work_package_params)
end
def build_up_brand_new_work_package
diff --git a/spec/controllers/work_packages/progress_controller_spec.rb b/spec/controllers/work_packages/progress_controller_spec.rb
new file mode 100644
index 00000000000..10c16fd8686
--- /dev/null
+++ b/spec/controllers/work_packages/progress_controller_spec.rb
@@ -0,0 +1,84 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+
+RSpec.describe WorkPackages::ProgressController do
+ shared_let(:user) { create(:admin) }
+ shared_let(:work_package) { create(:work_package) }
+
+ current_user { user }
+
+ describe "POST /work_packages/:d/progress" do
+ let(:params) do
+ {
+ "work_package_id" => work_package.id,
+ "work_package" => {
+ "estimated_hours" => "42",
+ "remaining_hours" => "4h",
+ "done_ratio" => "90",
+ "estimated_hours_touched" => "false",
+ "remaining_hours_touched" => "false"
+ }
+ }
+ end
+
+ it "updates the work package progress values with touched values (none touched)" do
+ patch("update", params:, as: :turbo_stream)
+ work_package.reload
+
+ expect(work_package.estimated_hours).to be_nil
+ expect(work_package.remaining_hours).to be_nil
+ end
+
+ it "updates the work package progress values with touched values (only work touched)" do
+ params["work_package"]["estimated_hours_touched"] = "true"
+
+ patch("update", params:, as: :turbo_stream)
+ work_package.reload
+
+ expect(work_package.estimated_hours).to eq(42)
+ # when not supplied by the user, the remaining work is set to the same
+ # value as work
+ expect(work_package.remaining_hours).to eq(42)
+ end
+
+ it "updates the work package progress values (all values touched)" do
+ params["work_package"]["estimated_hours_touched"] = "true"
+ params["work_package"]["remaining_hours_touched"] = "true"
+
+ patch("update", params:, as: :turbo_stream)
+ work_package.reload
+
+ expect(work_package.estimated_hours).to eq(42)
+ expect(work_package.remaining_hours).to eq(4)
+ end
+ end
+end