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

[52233] Validate done_ratio is consistent with work and remaining work

Christophe Bliard22af62066c45
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rb
index 05aaad833bc..bfa58dc0359 100644
--- a/app/contracts/work_packages/base_contract.rb
+++ b/app/contracts/work_packages/base_contract.rb
@@ -53,7 +53,10 @@ module WorkPackages
attribute :project_id
attribute :done_ratio,
- writable: ->(*) { WorkPackage.work_based_mode? }
+ writable: ->(*) { WorkPackage.work_based_mode? } do
+ validate_done_ratio_matches_work_and_remaining_work
+ validate_done_ratio_is_unset_when_work_is_zero
+ end
attribute :derived_done_ratio,
writable: false
@@ -347,10 +350,40 @@ module WorkPackages
end
end
+ # rubocop:disable Metrics/AbcSize
+ def validate_done_ratio_matches_work_and_remaining_work
+ return if WorkPackage.status_based_mode? || percent_complete_unset? || work == 0
+ return unless work_set? && remaining_work_set?
+
+ work_done = work - remaining_work
+ expected_done_ratio = (100 * work_done / work).round
+
+ if done_ratio != expected_done_ratio
+ errors.add(:done_ratio, :does_not_match_work_and_remaining_work)
+ end
+ end
+ # rubocop:enable Metrics/AbcSize
+
+ def validate_done_ratio_is_unset_when_work_is_zero
+ return if WorkPackage.status_based_mode?
+
+ if work == 0 && done_ratio.present?
+ errors.add(:done_ratio, :cannot_be_set_when_work_is_zero)
+ end
+ end
+
+ def work
+ model.estimated_hours
+ end
+
def work_set?
model.estimated_hours.present?
end
+ def remaining_work
+ model.remaining_hours
+ end
+
def remaining_work_set?
model.remaining_hours.present?
end
@@ -359,6 +392,10 @@ module WorkPackages
model.remaining_hours > model.estimated_hours
end
+ def percent_complete_unset?
+ model.done_ratio.nil?
+ end
+
def validate_no_reopen_on_closed_version
if model.version_id && model.reopened? && model.version.closed?
errors.add :base, I18n.t(:error_can_not_reopen_work_package_on_closed_version)
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 9607c686136..114ee02d588 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1104,6 +1104,9 @@ en:
attributes:
assigned_to:
format: "%{message}"
+ done_ratio:
+ does_not_match_work_and_remaining_work: "does not match work and wemaining work"
+ cannot_be_set_when_work_is_zero: "cannot be set when work is zero"
due_date:
not_start_date: "is not on start date, although this is required for milestones."
cannot_be_null: "can not be set to null as start date and duration are known."
diff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rb
index cb53b417f75..99f3be74389 100644
--- a/spec/contracts/work_packages/base_contract_spec.rb
+++ b/spec/contracts/work_packages/base_contract_spec.rb
@@ -293,11 +293,13 @@ RSpec.describe WorkPackages::BaseContract do
describe "work (estimated hours)" do
let(:estimated_hours) { 1 }
let(:remaining_hours) { 0 }
+ let(:done_ratio) { 100 }
before do
- work_package.estimated_hours = estimated_hours
work_package.remaining_hours = remaining_hours
- work_package.clear_remaining_hours_change
+ work_package.done_ratio = done_ratio
+ work_package.clear_changes_information
+ work_package.estimated_hours = estimated_hours
end
context "when > 0" do
@@ -308,6 +310,7 @@ RSpec.describe WorkPackages::BaseContract do
context "when 0" do
let(:estimated_hours) { 0 }
+ let(:done_ratio) { nil }
include_examples "contract is valid"
end
@@ -369,30 +372,35 @@ RSpec.describe WorkPackages::BaseContract do
describe "remaining work (remaining hours)" do
before do
work_package.estimated_hours = 5.0
+ work_package.done_ratio = done_ratio
work_package.clear_remaining_hours_change
work_package.remaining_hours = remaining_hours
end
context "when it's the same as work" do
let(:remaining_hours) { 5.0 }
+ let(:done_ratio) { 0 }
include_examples "contract is valid"
end
context "when it's less than work" do
let(:remaining_hours) { 4.0 }
+ let(:done_ratio) { 20 }
include_examples "contract is valid"
end
context "when it's greater than work" do
let(:remaining_hours) { 6.0 }
+ let(:done_ratio) { 0 }
include_examples "contract is invalid", remaining_hours: :cant_exceed_work
end
context "when unset" do
let(:remaining_hours) { nil }
+ let(:done_ratio) { nil }
include_examples "contract is invalid", remaining_hours: :must_be_set_when_work_is_set
end
@@ -410,6 +418,51 @@ RSpec.describe WorkPackages::BaseContract do
end
end
+ describe "work, remaining work, and % complete" do
+ context "when changing all three with consistent values" do
+ before do
+ work_package.estimated_hours = 10.0
+ work_package.remaining_hours = 6.0
+ work_package.done_ratio = 40
+ end
+
+ include_examples "contract is valid"
+ end
+
+ context "when changing all three with inconsistent values" do
+ before do
+ work_package.estimated_hours = 10.0
+ work_package.remaining_hours = 0
+ work_package.done_ratio = 50
+ end
+
+ include_examples "contract is invalid", done_ratio: :does_not_match_work_and_remaining_work
+ end
+
+ context "when work and remaining work are both 0h" do
+ before do
+ work_package.estimated_hours = 0
+ work_package.remaining_hours = 0
+ end
+
+ context "when % complete is set to any value" do
+ before do
+ work_package.done_ratio = 50
+ end
+
+ include_examples "contract is invalid", done_ratio: :cannot_be_set_when_work_is_zero
+ end
+
+ context "when % complete is unset" do
+ before do
+ work_package.done_ratio = nil
+ end
+
+ include_examples "contract is valid"
+ end
+ end
+ end
+
shared_examples_for "a date attribute" do |attribute|
context "for a date" do
before do
@@ -726,9 +779,11 @@ RSpec.describe WorkPackages::BaseContract do
it_behaves_like "invalid if changed", :done_ratio
end
- context "when in work-based progress calculation mode",
+ context "when changed in work-based progress calculation mode",
with_settings: { work_package_done_ratio: "field" } do
before do
+ work_package.estimated_hours = 50
+ work_package.remaining_hours = 29
work_package.done_ratio = 42
end