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

Improve error messages with invalid progress values

Christophe Bliard711846d6f8cc
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rb
index 2ffdc13dd8b..8a20b9251ff 100644
--- a/app/contracts/work_packages/base_contract.rb
+++ b/app/contracts/work_packages/base_contract.rb
@@ -382,7 +382,7 @@ module WorkPackages
# 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 remaining_work_exceeds_work? # avoid too many error messages at the same time
+ 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
@@ -434,8 +434,14 @@ module WorkPackages
remaining_work.nil?
end
+ def invalid_work_or_remaining_work_values?
+ (work_set? && work.negative?) ||
+ (remaining_work_set? && remaining_work.negative?) ||
+ remaining_work_exceeds_work?
+ end
+
def remaining_work_exceeds_work?
- work_set? && remaining_work_set? && remaining_work > work
+ work_set_and_valid? && remaining_work_set_and_valid? && remaining_work > work
end
def percent_complete
diff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rb
index b87fdb7c7e2..e765b1fec51 100644
--- a/spec/contracts/work_packages/base_contract_spec.rb
+++ b/spec/contracts/work_packages/base_contract_spec.rb
@@ -500,15 +500,45 @@ RSpec.describe WorkPackages::BaseContract,
done_ratio: nil
end
- context "when unset while remaining work is set and work is set to an invalid value" do
+ context "when set 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) { 50 }
+
+ include_examples "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 a negative 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],
+ it_behaves_like "contract is invalid", estimated_hours: :greater_than_or_equal_to,
remaining_hours: nil,
done_ratio: nil
end
+
+ context "when set while remaining work is set and work is set to a negative value" do
+ let(:estimated_hours) { -4 }
+ let(:remaining_hours) { 3 }
+ let(:done_ratio) { 50 }
+
+ it_behaves_like "contract is invalid", estimated_hours: :greater_than_or_equal_to,
+ remaining_hours: nil,
+ done_ratio: nil
+ end
+
+ context "when set while work and remaining work are both set to a negative values (and work > remaining work)" do
+ let(:estimated_hours) { -4 }
+ let(:remaining_hours) { -12 }
+ let(:done_ratio) { 50 }
+
+ it_behaves_like "contract is invalid", estimated_hours: :greater_than_or_equal_to,
+ remaining_hours: :greater_than_or_equal_to,
+ done_ratio: nil
+ end
end
end