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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[52233] Implement new business rules
Christophe Bliard8b91097b2423
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rbindex bfa58dc0359..0c8754f870d 100644--- a/app/contracts/work_packages/base_contract.rb+++ b/app/contracts/work_packages/base_contract.rb@@ -54,21 +54,22 @@ module WorkPackagesattribute :done_ratio,writable: ->(*) { WorkPackage.work_based_mode? } do- validate_done_ratio_matches_work_and_remaining_work- validate_done_ratio_is_unset_when_work_is_zero+ validate_percent_complete_matches_work_and_remaining_work+ validate_percent_complete_is_unset_when_work_is_zero+ validate_percent_complete_is_set_when_work_and_remaining_work_are_not_setendattribute :derived_done_ratio,writable: falseattribute :estimated_hours do- validate_work_is_set_when_remaining_work_is_set+ validate_work_is_set_when_remaining_work_and_percent_complete_are_setendattribute :derived_estimated_hours,writable: falseattribute :remaining_hours dovalidate_remaining_work_is_lower_than_work- validate_remaining_work_is_set_when_work_is_set+ validate_remaining_work_is_set_when_work_and_percent_complete_are_setendattribute :derived_remaining_hours,writable: false@@ -327,7 +328,7 @@ module WorkPackagesenddef validate_remaining_work_is_lower_than_work- if work_set? && remaining_work_set? && remaining_work_exceeds_work?+ if remaining_work_exceeds_work?if model.changed.include?("estimated_hours")errors.add(:estimated_hours, :cant_be_inferior_to_remaining_work)end@@ -338,36 +339,43 @@ module WorkPackagesendend- def validate_remaining_work_is_set_when_work_is_set- if work_set? && !remaining_work_set?- errors.add(:remaining_hours, :must_be_set_when_work_is_set)+ def validate_work_is_set_when_remaining_work_and_percent_complete_are_set+ if remaining_work_set? && percent_complete_set? && work_unset?+ errors.add(:estimated_hours, :must_be_set_when_remaining_work_and_percent_complete_are_set)endend- def validate_work_is_set_when_remaining_work_is_set- if remaining_work_set? && !work_set?- errors.add(:estimated_hours, :must_be_set_when_remaining_work_is_set)+ def validate_remaining_work_is_set_when_work_and_percent_complete_are_set+ if work_set? && percent_complete_set? && remaining_work_unset?+ errors.add(:remaining_hours, :must_be_set_when_work_and_percent_complete_are_set)+ end+ end++ def validate_percent_complete_is_set_when_work_and_remaining_work_are_not_set+ if work_set? && remaining_work_set? && work != 0 && percent_complete_unset?+ errors.add(:done_ratio, :must_be_set_when_work_and_remaining_work_are_set)endend# rubocop:disable Metrics/AbcSize- def validate_done_ratio_matches_work_and_remaining_work+ def validate_percent_complete_matches_work_and_remaining_workreturn if WorkPackage.status_based_mode? || percent_complete_unset? || work == 0+ return if remaining_work_exceeds_work? # avoid too many error messages at the same timereturn unless work_set? && remaining_work_set?work_done = work - remaining_work- expected_done_ratio = (100 * work_done / work).round+ expected_percent_complete = (100 * work_done.to_f / work).round- if done_ratio != expected_done_ratio+ if percent_complete != expected_percent_completeerrors.add(:done_ratio, :does_not_match_work_and_remaining_work)endend# rubocop:enable Metrics/AbcSize- def validate_done_ratio_is_unset_when_work_is_zero+ def validate_percent_complete_is_unset_when_work_is_zeroreturn if WorkPackage.status_based_mode?- if work == 0 && done_ratio.present?+ if work == 0 && percent_complete_set?errors.add(:done_ratio, :cannot_be_set_when_work_is_zero)endend@@ -380,6 +388,10 @@ module WorkPackagesmodel.estimated_hours.present?end+ def work_unset?+ model.estimated_hours.nil?+ end+def remaining_workmodel.remaining_hoursend@@ -388,8 +400,20 @@ module WorkPackagesmodel.remaining_hours.present?end+ def remaining_work_unset?+ model.remaining_hours.nil?+ end+def remaining_work_exceeds_work?- model.remaining_hours > model.estimated_hours+ work_set? && remaining_work_set? && model.remaining_hours > model.estimated_hours+ end++ def percent_complete+ model.done_ratio+ end++ def percent_complete_set?+ model.done_ratio.present?enddef percent_complete_unset?diff --git a/app/services/work_packages/set_attributes_service/derive_progress_values_base.rb b/app/services/work_packages/set_attributes_service/derive_progress_values_base.rbindex f8f74f4b453..4917a3a16c6 100644--- a/app/services/work_packages/set_attributes_service/derive_progress_values_base.rb+++ b/app/services/work_packages/set_attributes_service/derive_progress_values_base.rb@@ -109,6 +109,10 @@ class WorkPackages::SetAttributesServicework_package.done_ratio = valueend+ def percent_complete_set?+ percent_complete.present?+ end+def percent_complete_unset?percent_complete.nil?enddiff --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.rbindex 6a6f1d35146..9cad3c650d3 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@@ -45,8 +45,7 @@ class WorkPackages::SetAttributesServicedef invalid_progress_values?work&.negative? \|| remaining_work&.negative? \- || remaining_work_set_greater_than_work? \- || work_and_remaining_exclusively_unset?+ || remaining_work_set_greater_than_work?enddef derive_work?@@ -62,8 +61,8 @@ class WorkPackages::SetAttributesServiceenddef update_work- return if work.present? && !(remaining_work_changed? && percent_complete_changed?)- return if remaining_work_unset?+ return if work_set? && !(remaining_work_came_from_user? && percent_complete_came_from_user?)+ return if remaining_work_unset? && percent_complete_unset?self.work = work_from_percent_complete_and_remaining_workend@@ -102,6 +101,8 @@ class WorkPackages::SetAttributesServiceenddef work_from_percent_complete_and_remaining_work+ return if remaining_work_unset?+remaining_percent_complete = 1.0 - ((percent_complete || 0) / 100.0)remaining_work / remaining_percent_completeend@@ -110,12 +111,6 @@ class WorkPackages::SetAttributesServiceattributes_from_user == %i[remaining_work] && work && remaining_work && remaining_work > workend- # Returns true if work is set and remaining_work is unset, or if work is- # unset and remaining_work is set.- def work_and_remaining_exclusively_unset?- remaining_work_came_from_user? && work_came_from_user? && (work_unset? || remaining_work_unset?)- end-def attributes_from_user@attributes_from_user ||= PROGRESS_ATTRIBUTES.filter { |attr| public_send(:"#{attr}_came_from_user?") }enddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex db2f379f373..ebf3ae44008 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -1107,6 +1107,7 @@ en:done_ratio:does_not_match_work_and_remaining_work: "does not match work and remaining work"cannot_be_set_when_work_is_zero: "cannot be set when work is zero"+ must_be_set_when_work_and_remaining_work_are_set: "Required when Work and Remaining work are set."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."@@ -1136,13 +1137,13 @@ en:estimated_hours:not_a_number: "is not a valid duration."cant_be_inferior_to_remaining_work: "Cannot be lower than Remaining work."- must_be_set_when_remaining_work_is_set: "Required when Remaining work is set."+ must_be_set_when_remaining_work_and_percent_complete_are_set: "Required when Remaining work and % Complete are set."only_values_greater_or_equal_zeroes_allowed: "Must be >= 0."format: "%{message}"remaining_hours:not_a_number: "is not a valid duration."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."format: "%{message}"readonly_status: "The work package is in a readonly status so its attributes cannot be changed."type:diff --git a/spec/contracts/shared/model_contract_shared_context.rb b/spec/contracts/shared/model_contract_shared_context.rbindex 6fa875de44f..6f8b7490240 100644--- a/spec/contracts/shared/model_contract_shared_context.rb+++ b/spec/contracts/shared/model_contract_shared_context.rb@@ -9,8 +9,10 @@ RSpec.shared_context "ModelContract shared context" do # rubocop:disable RSpec/Cexpect(contract.validate).to be(false)errors.each do |key, error_symbols|- expect(contract.errors.attribute_names)- .to include(key), "expected errors attributes #{contract.errors.attribute_names.inspect} to include #{key.inspect}"+ if error_symbols.present?+ expect(contract.errors.attribute_names)+ .to include(key), "expected errors attributes #{contract.errors.attribute_names.inspect} to include #{key.inspect}"+ endexpect(contract.errors.symbols_for(key)).to match_array Array(error_symbols)endenddiff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rbindex 99f3be74389..7896be16847 100644--- a/spec/contracts/work_packages/base_contract_spec.rb+++ b/spec/contracts/work_packages/base_contract_spec.rb@@ -291,47 +291,32 @@ RSpec.describe WorkPackages::BaseContract doenddescribe "work (estimated hours)" do- let(:estimated_hours) { 1 }- let(:remaining_hours) { 0 }- let(:done_ratio) { 100 }-before do+ work_package.estimated_hours = estimated_hourswork_package.remaining_hours = remaining_hourswork_package.done_ratio = done_ratio- work_package.clear_changes_information- work_package.estimated_hours = estimated_hours- end-- context "when > 0" do- let(:estimated_hours) { 1 }-- include_examples "contract is valid"end- context "when 0" do- let(:estimated_hours) { 0 }- let(:done_ratio) { nil }-- include_examples "contract is valid"- end-- context "when nil while remaining work is nil" do- let(:estimated_hours) { nil }+ context "when only one being set" do+ let(:estimated_hours) { 3.0 }let(:remaining_hours) { nil }+ let(:done_ratio) { nil }include_examples "contract is valid"end- context "when nil while remaining work is set" do+ context "when unset while remaining work and % complete are set" dolet(:estimated_hours) { nil }let(:remaining_hours) { 2.0 }+ let(:done_ratio) { 50 }- include_examples "contract is invalid", estimated_hours: :must_be_set_when_remaining_work_is_set+ include_examples "contract is invalid", estimated_hours: :must_be_set_when_remaining_work_and_percent_complete_are_setendcontext "when < 0" dolet(:estimated_hours) { -1 }- let(:remaining_hours) { -2 }+ let(:remaining_hours) { nil }+ let(:done_ratio) { nil }include_examples "contract is invalid", estimated_hours: :only_values_greater_or_equal_zeroes_allowedend@@ -339,6 +324,7 @@ RSpec.describe WorkPackages::BaseContract docontext "when inferior to remaining work" dolet(:estimated_hours) { 5.0 }let(:remaining_hours) { 7.0 }+ let(:done_ratio) { 50 }include_examples "contract is invalid", estimated_hours: :cant_be_inferior_to_remaining_workend@@ -371,13 +357,13 @@ RSpec.describe WorkPackages::BaseContract dodescribe "remaining work (remaining hours)" dobefore do- work_package.estimated_hours = 5.0- work_package.done_ratio = done_ratio- work_package.clear_remaining_hours_change+ work_package.estimated_hours = estimated_hourswork_package.remaining_hours = remaining_hours+ work_package.done_ratio = done_ratioendcontext "when it's the same as work" do+ let(:estimated_hours) { 5.0 }let(:remaining_hours) { 5.0 }let(:done_ratio) { 0 }@@ -385,6 +371,7 @@ RSpec.describe WorkPackages::BaseContract doendcontext "when it's less than work" do+ let(:estimated_hours) { 5.0 }let(:remaining_hours) { 4.0 }let(:done_ratio) { 20 }@@ -392,71 +379,152 @@ RSpec.describe WorkPackages::BaseContract doendcontext "when it's greater than work" do+ let(:estimated_hours) { 5.0 }let(:remaining_hours) { 6.0 }let(:done_ratio) { 0 }include_examples "contract is invalid", remaining_hours: :cant_exceed_workend- context "when unset" do- let(:remaining_hours) { nil }+ context "when only one being set" do+ let(:estimated_hours) { nil }+ let(:remaining_hours) { 1.5 }let(:done_ratio) { nil }- include_examples "contract is invalid", remaining_hours: :must_be_set_when_work_is_set+ include_examples "contract is valid"+ end++ context "when unset while work and % complete are set" do+ let(:estimated_hours) { 3 }+ let(:remaining_hours) { nil }+ let(:done_ratio) { 50 }++ include_examples "contract is invalid", remaining_hours: :must_be_set_when_work_and_percent_complete_are_setendend- describe "work and remaining work" do- context "when changing both and remaining work exceeds work" do+ describe "percent complete" do+ context "when inferred by status",+ with_settings: { work_package_done_ratio: "status" } do+ it_behaves_like "invalid if changed", :done_ratio+ end++ context "when in work-based progress calculation mode",+ with_settings: { work_package_done_ratio: "field" } dobefore do- work_package.estimated_hours = 5.0- work_package.remaining_hours = 6.0+ work_package.estimated_hours = estimated_hours+ work_package.remaining_hours = remaining_hours+ work_package.done_ratio = done_ratioend- include_examples "contract is invalid", estimated_hours: :cant_be_inferior_to_remaining_work,- remaining_hours: :cant_exceed_work+ # TODO: get better error message+ context "when less than 0" do+ let(:estimated_hours) { nil }+ let(:remaining_hours) { nil }+ let(:done_ratio) { -1 }++ it_behaves_like "contract is invalid", done_ratio: :inclusion+ end++ # TODO: get better error message+ context "when less more than 100" do+ let(:estimated_hours) { nil }+ let(:remaining_hours) { nil }+ let(:done_ratio) { 101 }++ it_behaves_like "contract is invalid", done_ratio: :inclusion+ end++ context "when only one being set" do+ let(:estimated_hours) { nil }+ let(:remaining_hours) { nil }+ let(:done_ratio) { 42 }++ it_behaves_like "contract is valid"+ end++ context "when unset while work and remaining work are set" do+ let(:estimated_hours) { 4 }+ let(:remaining_hours) { 3 }+ let(:done_ratio) { nil }++ it_behaves_like "contract is invalid", done_ratio: :must_be_set_when_work_and_remaining_work_are_set+ endendenddescribe "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+ before do+ work_package.estimated_hours = estimated_hours+ work_package.remaining_hours = remaining_hours+ work_package.done_ratio = done_ratio+ end++ context "when remaining work exceeds work" do+ let(:estimated_hours) { 5.0 }+ let(:remaining_hours) { 6.0 }+ let(:done_ratio) { 100 }++ # 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+ end++ context "when all three unset" do+ let(:estimated_hours) { nil }+ let(:remaining_hours) { nil }+ let(:done_ratio) { nil }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+ context "when all three set with consistent values" do+ let(:estimated_hours) { 10 }+ let(:remaining_hours) { 6.0 }+ let(:done_ratio) { 40 }++ include_examples "contract is valid"+ end++ context "when all three set with inconsistent values" do+ let(:estimated_hours) { 10 }+ let(:remaining_hours) { 0 }+ let(:done_ratio) { 50 }include_examples "contract is invalid", done_ratio: :does_not_match_work_and_remaining_workendcontext "when work and remaining work are both 0h" do- before do- work_package.estimated_hours = 0- work_package.remaining_hours = 0- end+ let(:estimated_hours) { 0 }+ let(:remaining_hours) { 0 }context "when % complete is set to any value" do- before do- work_package.done_ratio = 50- end+ let(:done_ratio) { 50 }include_examples "contract is invalid", done_ratio: :cannot_be_set_when_work_is_zeroendcontext "when % complete is unset" do- before do- work_package.done_ratio = nil- end+ let(:done_ratio) { nil }++ include_examples "contract is valid"+ end+ end++ context "when work and remaining work are both unset" do+ let(:estimated_hours) { nil }+ let(:remaining_hours) { nil }++ context "when % complete is set to any value" do+ let(:done_ratio) { 50 }++ include_examples "contract is valid"+ end++ context "when % complete is unset" do+ let(:done_ratio) { nil }include_examples "contract is valid"end@@ -773,24 +841,6 @@ RSpec.describe WorkPackages::BaseContract doendend- describe "percentage done" do- context "when % Complete inferred by status",- with_settings: { work_package_done_ratio: "status" } do- it_behaves_like "invalid if changed", :done_ratio- end-- 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-- it_behaves_like "contract is valid"- end- end-describe "version" dosubject(:contract) { described_class.new(work_package, current_user) }diff --git a/spec/features/work_packages/progress_modal_spec.rb b/spec/features/work_packages/progress_modal_spec.rbindex 568a2c17841..26b5af80f7d 100644--- a/spec/features/work_packages/progress_modal_spec.rb+++ b/spec/features/work_packages/progress_modal_spec.rb@@ -477,7 +477,7 @@ RSpec.describe "Progress modal", :js, :with_cuprite dospecify "Case 2: when work is set to 12h, " \"remaining work is automatically set to 6h " \"and subsequently work is set to 14h, " \- "remaining work updates to 1d" do+ "remaining work updates to 8h" dowork_package_table.visit_query(progress_query)work_package_table.expect_work_package_listed(work_package)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.rbindex a2b37d583e1..1ab715b758f 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@@ -232,11 +232,30 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedcontext "when work is changed and remaining work is unset" dolet(:set_attributes) { { estimated_hours: 8.0, remaining_hours: nil } }- let(:expected_kept_attributes) { %w[done_ratio] }+ let(:expected_derived_attributes) { { done_ratio: nil } }- include_examples "update progress values",- description: "% complete is kept and remaining work is kept unset and not recomputed" \- "(error state to be detected by contract)"+ include_examples "update progress values", description: "% complete is unset"+ end++ context "when percent complete is changed and work is unset" do+ let(:set_attributes) { { done_ratio: 40, estimated_hours: nil } }+ let(:expected_derived_attributes) { { remaining_hours: nil } }++ include_examples "update progress values", description: "remaining work is unset"+ end++ context "when percent complete is changed and remaining work is unset" do+ let(:set_attributes) { { done_ratio: 40, remaining_hours: nil } }+ let(:expected_derived_attributes) { { estimated_hours: nil } }++ include_examples "update progress values", description: "work is unset"+ end++ context "when % complete is changed and remaining work is set to same value" do+ let(:set_attributes) { { done_ratio: 90, remaining_hours: 3 } }+ let(:expected_derived_attributes) { { estimated_hours: 30 } }++ include_examples "update progress values", description: "work is updated accordingly"endcontext "when work is set to the same value and remaining work is changed" do@@ -513,11 +532,9 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBasedcontext "when work is set and remaining work is unset" dolet(:set_attributes) { { estimated_hours: 10.0, remaining_hours: nil } }- let(:expected_kept_attributes) { %w[done_ratio] }+ let(:expected_derived_attributes) { { done_ratio: nil } }- include_examples "update progress values",- description: "% complete is kept and remaining work is kept unset and not recomputed" \- "(error state to be detected by contract)"+ include_examples "update progress values", description: "% complete is unset"endcontext "when work and remaining work are both set to negative values" do