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] Handle progress value derivation when % complete is 100%

Christophe Bliard4330f5375890
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rb
index 8a20b9251ff..51626dab2c6 100644
--- a/app/contracts/work_packages/base_contract.rb
+++ b/app/contracts/work_packages/base_contract.rb
@@ -80,6 +80,7 @@ module WorkPackages
attribute :remaining_hours do
validate_remaining_work_is_lower_than_work
if OpenProject::FeatureDecisions.percent_complete_edition_active?
+ validate_remaining_work_is_zero_or_unset_when_percent_complete_is_100p
validate_remaining_work_is_set_when_work_and_percent_complete_are_set
else
# to be removed in 15.0 with :percent_complete_edition feature flag removal
@@ -362,12 +363,24 @@ module WorkPackages
end
def validate_work_is_set_when_remaining_work_and_percent_complete_are_set
- if remaining_work_set_and_valid? && percent_complete_set_and_valid? && work_unset?
+ if remaining_work_set_and_valid? && percent_complete_set_and_valid? && work_unset? && percent_complete != 100
errors.add(:estimated_hours, :must_be_set_when_remaining_work_and_percent_complete_are_set)
end
end
+ def validate_remaining_work_is_zero_or_unset_when_percent_complete_is_100p
+ return unless percent_complete == 100
+
+ if work_set_and_valid? && remaining_work != 0
+ errors.add(:remaining_hours, :must_be_set_to_zero_hours_when_work_is_set_and_percent_complete_is_100p)
+ elsif work_unset? && remaining_work_set?
+ errors.add(:remaining_hours, :must_be_unset_when_work_is_unset_and_percent_complete_is_100p)
+ end
+ end
+
def validate_remaining_work_is_set_when_work_and_percent_complete_are_set
+ return if percent_complete == 100
+
if work_set_and_valid? && percent_complete_set_and_valid? && remaining_work_unset?
errors.add(:remaining_hours, :must_be_set_when_work_and_percent_complete_are_set)
end
@@ -379,20 +392,15 @@ module WorkPackages
end
end
- # rubocop:disable Metrics/AbcSize
def validate_percent_complete_matches_work_and_remaining_work
- return if WorkPackage.status_based_mode? || percent_complete_unset? || work == 0
+ return if WorkPackage.status_based_mode? || percent_complete_unset? || work == 0 || percent_complete == 100
return if invalid_work_or_remaining_work_values? # avoid too many error messages at the same time
return unless work_set? && remaining_work_set?
- work_done = work - remaining_work
- expected_percent_complete = (100 * work_done.to_f / work).round
-
- if percent_complete != expected_percent_complete
+ if percent_complete != percent_complete_derived_from_work_and_remaining_work
errors.add(:done_ratio, :does_not_match_work_and_remaining_work)
end
end
- # rubocop:enable Metrics/AbcSize
def validate_percent_complete_is_unset_when_work_is_zero
return if WorkPackage.status_based_mode?
@@ -441,6 +449,10 @@ module WorkPackages
end
def remaining_work_exceeds_work?
+ # if % complete is 100%, then remaining work should be 0h or unset, so no
+ # need to display an error for remaining work exceeding work
+ return false if percent_complete == 100
+
work_set_and_valid? && remaining_work_set_and_valid? && remaining_work > work
end
@@ -460,6 +472,11 @@ module WorkPackages
percent_complete.nil?
end
+ def percent_complete_derived_from_work_and_remaining_work
+ work_done = work - remaining_work
+ (100 * work_done.to_f / work).round
+ 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/app/services/work_packages/set_attributes_service/derive_progress_values_work_based.rb b/app/services/work_packages/set_attributes_service/derive_progress_values_work_based.rb
index 155e0796c98..568d8ffc24a 100644
--- a/app/services/work_packages/set_attributes_service/derive_progress_values_work_based.rb
+++ b/app/services/work_packages/set_attributes_service/derive_progress_values_work_based.rb
@@ -68,6 +68,7 @@ class WorkPackages::SetAttributesService
def update_work
return if work_set_and_no_user_inputs_provided_for_both_remaining_work_and_percent_complete?
return if remaining_work_unset? && percent_complete_unset?
+ return if percent_complete == 100 # would be Infinity if computed when % complete is 100%
self.work = work_from_percent_complete_and_remaining_work
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 519ea15d3d7..4d015ed09ee 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1154,6 +1154,10 @@ en:
cant_exceed_work: "Cannot be higher than Work."
must_be_set_when_work_is_set: "Required when Work is set."
must_be_set_when_work_and_percent_complete_are_set: "Required when Work and % Complete are set."
+ must_be_set_to_zero_hours_when_work_is_set_and_percent_complete_is_100p: >-
+ must be set to 0h when Work is set and % Complete is 100%.
+ must_be_unset_when_work_is_unset_and_percent_complete_is_100p: >-
+ must be unset when Work is unset and % Complete is 100%.
format: "%{message}"
readonly_status: "The work package is in a readonly status so its attributes cannot be changed."
type:
diff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rb
index e765b1fec51..b23d2004dd8 100644
--- a/spec/contracts/work_packages/base_contract_spec.rb
+++ b/spec/contracts/work_packages/base_contract_spec.rb
@@ -552,13 +552,22 @@ RSpec.describe WorkPackages::BaseContract,
context "when remaining work exceeds work" do
let(:estimated_hours) { 5.0 }
let(:remaining_hours) { 6.0 }
- let(:done_ratio) { 100 }
+ let(:done_ratio) { 50 }
# no errors should be reported for % complete in this case to not overload
# the progress modal with error messages.
include_examples "contract is invalid", estimated_hours: :cant_be_inferior_to_remaining_work,
remaining_hours: :cant_exceed_work,
done_ratio: nil
+
+ context "and % complete is 100%" do
+ let(:done_ratio) { 100 }
+
+ include_examples "contract is invalid",
+ estimated_hours: nil,
+ remaining_hours: :must_be_set_to_zero_hours_when_work_is_set_and_percent_complete_is_100p,
+ done_ratio: nil
+ end
end
context "when all three unset" do
@@ -595,6 +604,12 @@ RSpec.describe WorkPackages::BaseContract,
include_examples "contract is invalid", done_ratio: :cannot_be_set_when_work_is_zero
end
+ context "when % complete is set to 100%" do
+ let(:done_ratio) { 100 }
+
+ include_examples "contract is invalid", done_ratio: :cannot_be_set_when_work_is_zero
+ end
+
context "when % complete is unset" do
let(:done_ratio) { nil }
@@ -602,6 +617,74 @@ RSpec.describe WorkPackages::BaseContract,
end
end
+ context "when % complete is 100%" do
+ let(:done_ratio) { 100 }
+
+ context "and work and remaining are both set to 0h" do
+ let(:estimated_hours) { 0 }
+ let(:remaining_hours) { 0 }
+
+ include_examples "contract is invalid",
+ estimated_hours: nil,
+ remaining_hours: nil,
+ done_ratio: :cannot_be_set_when_work_is_zero
+ end
+
+ context "and work is set" do
+ let(:estimated_hours) { 10 }
+
+ context "and remaining work is unset" do
+ let(:remaining_hours) { nil }
+
+ include_examples "contract is invalid",
+ estimated_hours: nil,
+ remaining_hours: :must_be_set_to_zero_hours_when_work_is_set_and_percent_complete_is_100p,
+ done_ratio: nil
+ end
+
+ context "and remaining is set to 0h" do
+ let(:remaining_hours) { 0 }
+
+ include_examples "contract is valid"
+ end
+
+ context "and remaining is set (but not 0h)" do
+ let(:remaining_hours) { 5 }
+
+ include_examples "contract is invalid",
+ estimated_hours: nil,
+ remaining_hours: :must_be_set_to_zero_hours_when_work_is_set_and_percent_complete_is_100p,
+ done_ratio: nil
+ end
+ end
+
+ context "and work is unset" do
+ let(:estimated_hours) { nil }
+
+ context "and remaining work is unset" do
+ let(:remaining_hours) { nil }
+
+ include_examples "contract is valid"
+ end
+
+ context "and remaining is set to 0h" do
+ let(:remaining_hours) { 0 }
+
+ include_examples "contract is invalid", estimated_hours: nil,
+ remaining_hours: :must_be_unset_when_work_is_unset_and_percent_complete_is_100p,
+ done_ratio: nil
+ end
+
+ context "and remaining is set (but not 0h)" do
+ let(:remaining_hours) { 5 }
+
+ include_examples "contract is invalid", estimated_hours: nil,
+ remaining_hours: :must_be_unset_when_work_is_unset_and_percent_complete_is_100p,
+ done_ratio: nil
+ end
+ end
+ end
+
context "when work and remaining work are both unset" do
let(:estimated_hours) { nil }
let(:remaining_hours) { nil }
diff --git a/spec/services/work_packages/set_attributes_service/derive_progress_values_work_based_spec.rb b/spec/services/work_packages/set_attributes_service/derive_progress_values_work_based_spec.rb
index 88eebf1b8ee..40153066f5c 100644
--- a/spec/services/work_packages/set_attributes_service/derive_progress_values_work_based_spec.rb
+++ b/spec/services/work_packages/set_attributes_service/derive_progress_values_work_based_spec.rb
@@ -191,6 +191,14 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
include_examples "update progress values", description: "work is updated accordingly"
end
+ context "when remaining work is changed and % complete is changed to 100%" do
+ let(:set_attributes) { { remaining_hours: 12.0, done_ratio: 100 } }
+ let(:expected_kept_attributes) { %w[estimated_hours] }
+
+ include_examples "update progress values",
+ description: "is an error state (to be detected by contract), and work is kept"
+ end
+
context "when work and remaining work are both changed to negative values" do
let(:set_attributes) { { estimated_hours: -10, remaining_hours: -5 } }
let(:expected_kept_attributes) { %w[done_ratio] }
@@ -587,6 +595,51 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
end
end
+ context "given a work package with work and remaining work unset, and % complete being set to 100%" do
+ before do
+ work_package.estimated_hours = nil
+ work_package.remaining_hours = nil
+ work_package.done_ratio = 100
+ work_package.clear_changes_information
+ end
+
+ context "when work is set" do
+ let(:set_attributes) { { estimated_hours: 10.0 } }
+ let(:expected_derived_attributes) { { remaining_hours: 0.0 } }
+ let(:expected_kept_attributes) { %w[done_ratio] }
+
+ include_examples "update progress values", description: "% complete is kept and remaining work is set to 0h"
+ end
+
+ context "when work is set to a string" do
+ let(:set_attributes) { { estimated_hours: "I am a string" } }
+ let(:expected_derived_attributes) { { estimated_hours: 0.0, remaining_hours: 0.0 } }
+
+ it "keeps the original string value in the _before_type_cast method " \
+ "so that validation can detect it is invalid" do
+ work_package.attributes = set_attributes
+ instance.call
+
+ expect(work_package.estimated_hours_before_type_cast).to eq("I am a string")
+ end
+ end
+
+ context "when work and remaining work are set" do
+ let(:set_attributes) { { estimated_hours: 10.0, remaining_hours: 5.0 } }
+ let(:expected_derived_attributes) { { done_ratio: 50 } }
+
+ include_examples "update progress values", description: "% complete is updated accordingly"
+ end
+
+ context "when remaining work is set to 0h" do
+ let(:set_attributes) { { remaining_hours: 0.0 } }
+ let(:expected_kept_attributes) { %w[estimated_hours done_ratio] }
+
+ include_examples "update progress values",
+ description: "is an error state (to be detected by contract), and % complete is kept"
+ end
+ end
+
context "given a work package with work, remaining work, and % complete being unset" do
before do
work_package.estimated_hours = nil