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

Harmonize errors when progress values are invalid

Christophe Bliard4230e479ab34
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rb
index 0c8754f870d..4a824ffb468 100644
--- a/app/contracts/work_packages/base_contract.rb
+++ b/app/contracts/work_packages/base_contract.rb
@@ -141,7 +141,6 @@ module WorkPackages
validate :validate_priority_exists
validate :validate_category
- validate :validate_estimated_hours
validate :validate_assigned_to_exists
@@ -222,12 +221,6 @@ module WorkPackages
attr_reader :can
- def validate_estimated_hours
- if !model.estimated_hours.nil? && model.estimated_hours < 0
- errors.add :estimated_hours, :only_values_greater_or_equal_zeroes_allowed
- end
- end
-
def validate_after_soonest_start(date_attribute)
if !model.schedule_manually? && before_soonest_start?(date_attribute)
message = I18n.t("activerecord.errors.models.work_package.attributes.start_date.violates_relationships",
@@ -340,19 +333,19 @@ module WorkPackages
end
def validate_work_is_set_when_remaining_work_and_percent_complete_are_set
- if remaining_work_set? && percent_complete_set? && work_unset?
+ if remaining_work_set_and_valid? && percent_complete_set_and_valid? && work_unset?
errors.add(:estimated_hours, :must_be_set_when_remaining_work_and_percent_complete_are_set)
end
end
def validate_remaining_work_is_set_when_work_and_percent_complete_are_set
- if work_set? && percent_complete_set? && remaining_work_unset?
+ 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
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?
+ if work_set_and_valid? && remaining_work_set_and_valid? && work != 0 && percent_complete_unset?
errors.add(:done_ratio, :must_be_set_when_work_and_remaining_work_are_set)
end
end
@@ -388,6 +381,10 @@ module WorkPackages
model.estimated_hours.present?
end
+ def work_set_and_valid?
+ work_set? && work >= 0
+ end
+
def work_unset?
model.estimated_hours.nil?
end
@@ -400,6 +397,10 @@ module WorkPackages
model.remaining_hours.present?
end
+ def remaining_work_set_and_valid?
+ remaining_work_set? && remaining_work >= 0
+ end
+
def remaining_work_unset?
model.remaining_hours.nil?
end
@@ -416,6 +417,10 @@ module WorkPackages
model.done_ratio.present?
end
+ def percent_complete_set_and_valid?
+ percent_complete_set? && percent_complete.between?(0, 100)
+ end
+
def percent_complete_unset?
model.done_ratio.nil?
end
diff --git a/app/models/work_package/validations.rb b/app/models/work_package/validations.rb
index 94f1c37475e..064a7fcda36 100644
--- a/app/models/work_package/validations.rb
+++ b/app/models/work_package/validations.rb
@@ -34,7 +34,7 @@ module WorkPackage::Validations
validates :subject, length: { maximum: 255 }
validates :done_ratio, inclusion: { in: 0..100 }, allow_nil: true
- validates :estimated_hours, numericality: { allow_nil: true }
+ validates :estimated_hours, numericality: { allow_nil: true, greater_than_or_equal_to: 0 }
validates :remaining_hours, numericality: { allow_nil: true, greater_than_or_equal_to: 0 }
validates :derived_remaining_hours, numericality: { allow_nil: true, greater_than_or_equal_to: 0 }
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 0bc1ece7c5b..26c20bcb363 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,9 +45,14 @@ class WorkPackages::SetAttributesService
def invalid_progress_values?
work&.negative? \
|| remaining_work&.negative? \
+ || percent_complete_out_of_range? \
|| remaining_work_set_greater_than_work?
end
+ def percent_complete_out_of_range?
+ percent_complete && !percent_complete.between?(0, 100)
+ end
+
def derive_work?
(remaining_work_changed? || percent_complete_changed?) && !work_came_from_user?
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index ebf3ae44008..cd212c22d0d 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1108,6 +1108,7 @@ en:
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."
+ inclusion: "must be between 0 and 100."
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."
@@ -1138,7 +1139,6 @@ en:
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_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."
diff --git a/spec/contracts/shared/model_contract_shared_context.rb b/spec/contracts/shared/model_contract_shared_context.rb
index 6f8b7490240..b3570fb7304 100644
--- a/spec/contracts/shared/model_contract_shared_context.rb
+++ b/spec/contracts/shared/model_contract_shared_context.rb
@@ -27,7 +27,7 @@ RSpec.shared_context "ModelContract shared context" do # rubocop:disable RSpec/C
example_title = "contract is invalid"
example_title << " with #{error_symbols.inspect}" if error_symbols.any?
- it example_title do
+ it example_title, :aggregate_failures do
expect_contract_invalid error_symbols
end
end
diff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rb
index 7896be16847..1b616e9e78d 100644
--- a/spec/contracts/work_packages/base_contract_spec.rb
+++ b/spec/contracts/work_packages/base_contract_spec.rb
@@ -313,12 +313,32 @@ RSpec.describe WorkPackages::BaseContract do
include_examples "contract is invalid", estimated_hours: :must_be_set_when_remaining_work_and_percent_complete_are_set
end
+ context "when unset while remaining work is set and % complete is set to an invalid value" do
+ let(:estimated_hours) { nil }
+ let(:remaining_hours) { 2.0 }
+ let(:done_ratio) { 200 }
+
+ include_examples "contract is invalid", estimated_hours: nil,
+ remaining_hours: nil,
+ done_ratio: :inclusion
+ end
+
+ context "when unset while % complete is set and remaining work is set to an invalid value" do
+ let(:estimated_hours) { nil }
+ let(:remaining_hours) { -2.0 }
+ let(:done_ratio) { 50 }
+
+ include_examples "contract is invalid", estimated_hours: nil,
+ remaining_hours: :greater_than_or_equal_to,
+ done_ratio: nil
+ end
+
context "when < 0" do
let(:estimated_hours) { -1 }
let(:remaining_hours) { nil }
let(:done_ratio) { nil }
- include_examples "contract is invalid", estimated_hours: :only_values_greater_or_equal_zeroes_allowed
+ include_examples "contract is invalid", estimated_hours: :greater_than_or_equal_to
end
context "when inferior to remaining work" do
@@ -401,6 +421,26 @@ RSpec.describe WorkPackages::BaseContract do
include_examples "contract is invalid", remaining_hours: :must_be_set_when_work_and_percent_complete_are_set
end
+
+ context "when unset while work is set and % complete is set to an invalid value" do
+ let(:estimated_hours) { 3 }
+ let(:remaining_hours) { nil }
+ let(:done_ratio) { -1 }
+
+ include_examples "contract is invalid", estimated_hours: nil,
+ remaining_hours: nil,
+ done_ratio: :inclusion
+ end
+
+ context "when unset while % complete is set and work is set to a negative value" do
+ let(:estimated_hours) { -3 }
+ let(:remaining_hours) { nil }
+ let(:done_ratio) { 50 }
+
+ include_examples "contract is invalid", estimated_hours: :greater_than_or_equal_to,
+ remaining_hours: nil,
+ done_ratio: nil
+ end
end
describe "percent complete" do
@@ -450,6 +490,26 @@ RSpec.describe WorkPackages::BaseContract do
it_behaves_like "contract is invalid", done_ratio: :must_be_set_when_work_and_remaining_work_are_set
end
+
+ context "when unset while work is set and remaining work is set to a negative value" do
+ let(:estimated_hours) { 4 }
+ let(:remaining_hours) { -3 }
+ let(:done_ratio) { nil }
+
+ it_behaves_like "contract is invalid", estimated_hours: nil,
+ remaining_hours: :greater_than_or_equal_to,
+ done_ratio: nil
+ end
+
+ context "when unset while remaining work is set and work is set to an invalid value" do
+ let(:estimated_hours) { -4 }
+ let(:remaining_hours) { 3 }
+ let(:done_ratio) { nil }
+
+ it_behaves_like "contract is invalid", estimated_hours: %i[cant_be_inferior_to_remaining_work greater_than_or_equal_to],
+ remaining_hours: nil,
+ done_ratio: nil
+ end
end
end
diff --git a/spec/models/work_package_spec.rb b/spec/models/work_package_spec.rb
index 939a49b6168..c28d3c6b792 100644
--- a/spec/models/work_package_spec.rb
+++ b/spec/models/work_package_spec.rb
@@ -775,6 +775,10 @@ RSpec.describe WorkPackage do
it "disallows negative values" do
work_package.remaining_hours = "-1"
expect(work_package).not_to be_valid
+
+ work_package.remaining_hours = "-1h"
+ expect(work_package.remaining_hours).to eq(-1)
+ expect(work_package).not_to be_valid
end
it "disallows string values, that are not numbers" do
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 e783e3392fa..9959910b3ef 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
@@ -283,6 +283,22 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
include_examples "update progress values", description: "work is kept, and remaining work is updated accordingly"
end
+ context "when % complete is changed to a negative value" do
+ let(:set_attributes) { { done_ratio: -1.0 } }
+ let(:expected_kept_attributes) { %w[estimated_hours remaining_hours] }
+
+ include_examples "update progress values",
+ description: "is an error state (to be detected by contract), and work and remaining work are kept"
+ end
+
+ context "when % complete is more than 100%" do
+ let(:set_attributes) { { done_ratio: 101 } }
+ let(:expected_kept_attributes) { %w[estimated_hours remaining_hours] }
+
+ include_examples "update progress values",
+ description: "is an error state (to be detected by contract), and work and remaining work are kept"
+ end
+
context "when work, remaining work, and % complete are all changed to consistent values" do
let(:set_attributes) { { estimated_hours: 20, remaining_hours: 12.0, done_ratio: 40 } }
let(:expected_kept_attributes) { %w[estimated_hours remaining_hours done_ratio] }