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

Handle invalid work and remaining work values

Christophe Bliard6851972eb207
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rb
index 87c77f50031..239586b1efe 100644
--- a/app/contracts/work_packages/base_contract.rb
+++ b/app/contracts/work_packages/base_contract.rb
@@ -405,7 +405,7 @@ module WorkPackages
def validate_percent_complete_is_empty_when_work_is_zero
return if WorkPackage.status_based_mode?
- if work == 0 && percent_complete_set?
+ if work_set_and_valid? && work == 0 && percent_complete_set?
errors.add(:done_ratio, :cannot_be_set_when_work_is_zero)
end
end
@@ -419,7 +419,7 @@ module WorkPackages
end
def work_set_and_valid?
- work_set? && work >= 0
+ work_set? && work >= 0 && !model.errors.has_key?(:estimated_hours)
end
def work_empty?
@@ -435,7 +435,7 @@ module WorkPackages
end
def remaining_work_set_and_valid?
- remaining_work_set? && remaining_work >= 0
+ remaining_work_set? && remaining_work >= 0 && !model.errors.has_key?(:remaining_hours)
end
def remaining_work_empty?
@@ -445,7 +445,8 @@ module WorkPackages
def invalid_work_or_remaining_work_values?
(work_set? && work.negative?) ||
(remaining_work_set? && remaining_work.negative?) ||
- remaining_work_exceeds_work?
+ (model.errors.has_key?(:estimated_hours) || model.errors.has_key?(:remaining_hours)) ||
+ remaining_work_exceeds_work?
end
def remaining_work_exceeds_work?
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index de48fc2e390..859a093eb4d 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -565,7 +565,7 @@ class WorkPackage < ApplicationRecord
def convert_duration_to_hours(value)
if value.is_a?(String)
begin
- value = value.blank? ? nil : DurationConverter.parse(value)
+ value = DurationConverter.parse(value)
rescue ChronicDuration::DurationParseError
# keep invalid value, error shall be caught by numericality validator
end
diff --git a/app/services/duration_converter.rb b/app/services/duration_converter.rb
index f267b5b05c2..1df40065e91 100644
--- a/app/services/duration_converter.rb
+++ b/app/services/duration_converter.rb
@@ -76,6 +76,8 @@ class DurationConverter
class << self
def parse(duration_string)
+ return nil if duration_string.blank?
+
# Assume the next logical unit to allow users to write
# durations such as "2h 1" assuming "1" is "1 minute"
last_unit_in_string = duration_string.scan(/[a-zA-Z]+/)
@@ -95,6 +97,21 @@ class DurationConverter
**duration_length_options) / 3600.to_f
end
+ def valid?(duration)
+ case duration
+ when String
+ duration.blank? || parseable?(duration)
+ when Numeric
+ duration >= 0
+ when nil
+ true
+ else
+ false
+ end
+ rescue ChronicDuration::DurationParseError
+ false
+ end
+
def output(duration_in_hours)
return duration_in_hours if duration_in_hours.nil?
@@ -108,6 +125,39 @@ class DurationConverter
private
+ def parseable?(duration_string)
+ if number = Integer(duration_string, 10, exception: false) || Float(duration_string, exception: false)
+ number >= 0
+ else
+ begin
+ internal_parse(duration_string)
+ true
+ rescue ChronicDuration::DurationParseError
+ false
+ end
+ end
+ end
+
+ def internal_parse(duration_string)
+ # Assume the next logical unit to allow users to write
+ # durations such as "2h 1" assuming "1" is "1 minute"
+ last_unit_in_string = duration_string.scan(/[a-zA-Z]+/)
+ .last
+ default_unit = if last_unit_in_string
+ last_unit_in_string
+ .then { |last_unit| UNIT_ABBREVIATION_MAP[last_unit.downcase] }
+ .then { |last_unit| NEXT_UNIT_MAP[last_unit] }
+ else
+ "hours"
+ end
+
+ ChronicDuration.parse(duration_string,
+ keep_zero: true,
+ default_unit:,
+ raise_exceptions: true,
+ **duration_length_options) / 3600.to_f
+ end
+
def format
Setting.duration_format == "days_and_hours" ? :days_and_hours : :hours_only
end
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 9921ee1cb24..87d96d6db8d 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,8 @@ class WorkPackages::SetAttributesService
end
def invalid_progress_values?
- work&.negative? \
- || remaining_work&.negative? \
+ work_invalid? \
+ || remaining_work_invalid? \
|| percent_complete_out_of_range? \
|| percent_complete_unparsable? \
|| remaining_work_set_greater_than_work?
@@ -152,6 +152,14 @@ class WorkPackages::SetAttributesService
remaining_work / remaining_percent_complete
end
+ def work_invalid?
+ !DurationConverter.valid?(work_package.estimated_hours_before_type_cast)
+ end
+
+ def remaining_work_invalid?
+ !DurationConverter.valid?(work_package.remaining_hours_before_type_cast)
+ end
+
def percent_complete_unparsable?
!PercentageConverter.valid?(work_package.done_ratio_before_type_cast)
end
diff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rb
index c89622a048d..971317d151a 100644
--- a/spec/contracts/work_packages/base_contract_spec.rb
+++ b/spec/contracts/work_packages/base_contract_spec.rb
@@ -590,6 +590,30 @@ RSpec.describe WorkPackages::BaseContract,
done_ratio: nil
end
+ context "when work is not a valid duration (an invalid string for instance)" do
+ let(:estimated_hours) { "invalid" }
+ let(:remaining_hours) { 6.0 }
+ 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: :not_a_number,
+ remaining_hours: nil,
+ done_ratio: nil
+ end
+
+ context "when remaining work is not a valid duration (an invalid string for instance)" do
+ let(:estimated_hours) { 10.0 }
+ let(:remaining_hours) { "invalid" }
+ 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: nil,
+ remaining_hours: :not_a_number,
+ done_ratio: nil
+ end
+
context "when all three empty" do
let(:estimated_hours) { nil }
let(:remaining_hours) { nil }
diff --git a/spec/services/duration_converter_spec.rb b/spec/services/duration_converter_spec.rb
index 622ed4327c0..95c082fd15b 100644
--- a/spec/services/duration_converter_spec.rb
+++ b/spec/services/duration_converter_spec.rb
@@ -32,6 +32,13 @@ require "spec_helper"
RSpec.describe DurationConverter do
describe ".parse" do
+ it "returns nil when given blank strings or nil" do
+ expect(described_class.parse("")).to be_nil
+ expect(described_class.parse(" ")).to be_nil
+ expect(described_class.parse(" \t ")).to be_nil
+ expect(described_class.parse(nil)).to be_nil
+ end
+
it "returns 0 when given 0 duration" do
expect(described_class.parse("0 hrs")).to eq(0)
end
@@ -53,6 +60,64 @@ RSpec.describe DurationConverter do
end
end
+ describe ".valid?", :aggregate_failures do
+ it "returns true for positive numbers" do
+ expect(described_class.valid?(0)).to be(true)
+ expect(described_class.valid?(0.0)).to be(true)
+ expect(described_class.valid?(100)).to be(true)
+ expect(described_class.valid?(100.0)).to be(true)
+ expect(described_class.valid?(789)).to be(true)
+ expect(described_class.valid?(789.123)).to be(true)
+ end
+
+ it "returns false for negative numbers" do
+ expect(described_class.valid?(-0.01)).to be(false)
+ expect(described_class.valid?(-1)).to be(false)
+ expect(described_class.valid?(-1.5)).to be(false)
+ expect(described_class.valid?(-456)).to be(false)
+ expect(described_class.valid?(-456.789)).to be(false)
+ end
+
+ it "returns true for blank values" do
+ expect(described_class.valid?(nil)).to be(true)
+ expect(described_class.valid?("")).to be(true)
+ expect(described_class.valid?(" ")).to be(true)
+ expect(described_class.valid?(" \t ")).to be(true)
+ end
+
+ it "returns true for strings representing a positive number or a valid duration" do
+ expect(described_class.valid?("50")).to be(true)
+ expect(described_class.valid?(" 50 ")).to be(true)
+ expect(described_class.valid?(" +1278 ")).to be(true)
+ expect(described_class.valid?(" -0 ")).to be(true)
+ expect(described_class.valid?(" 1234.0 h ")).to be(true)
+ expect(described_class.valid?("12h.4")).to be(true) # 12h 0.4m
+ expect(described_class.valid?("1 week 2 days 3 hours 5 minutes")).to be(true)
+ end
+
+ it "returns false for strings not representing a positive number nor a valid duration" do
+ expect(described_class.valid?("invalid")).to be(false)
+ expect(described_class.valid?("dsg")).to be(false)
+ expect(described_class.valid?(" +0h ")).to be(false)
+ expect(described_class.valid?("-5")).to be(false)
+ expect(described_class.valid?("-5.6")).to be(false)
+ expect(described_class.valid?("5.")).to be(false)
+ expect(described_class.valid?("5.h")).to be(false)
+ expect(described_class.valid?("-5.75h")).to be(false)
+ expect(described_class.valid?("invalid")).to be(false)
+ expect(described_class.valid?("-")).to be(false)
+ expect(described_class.valid?("+")).to be(false)
+ expect(described_class.valid?("日")).to be(false)
+ expect(described_class.valid?("invalid 123")).to be(false)
+ expect(described_class.valid?("123 invalid")).to be(false)
+ expect(described_class.valid?("- 23")).to be(false)
+ expect(described_class.valid?("123..5")).to be(false)
+ expect(described_class.valid?("1'234.5")).to be(false)
+ expect(described_class.valid?("1,234.5")).to be(false)
+ expect(described_class.valid?("12mm")).to be(false)
+ end
+ end
+
describe ".output" do
it "returns nil when given nil" do
expect(described_class.output(nil)).to be_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 f76e476fab7..631fc108651 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
@@ -701,7 +701,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
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 } }
+ let(:expected_derived_attributes) { { estimated_hours: 0.0 } }
+ let(:expected_kept_attributes) { %w[remaining_hours done_ratio] }
it "keeps the original string value in the _before_type_cast method " \
"so that validation can detect it is invalid" do
@@ -710,6 +711,11 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
expect(work_package.estimated_hours_before_type_cast).to eq("I am a string")
end
+
+ include_examples "update progress values",
+ description: "is an error state (to be detected by contract), " \
+ "and remaining work and % complete are kept",
+ expected_hints: {}
end
context "when work and remaining work are set" do
@@ -772,7 +778,8 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
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 } }
+ let(:expected_derived_attributes) { { estimated_hours: 0.0 } }
+ let(:expected_kept_attributes) { %w[remaining_hours done_ratio] }
it "keeps the original string value in the _before_type_cast method " \
"so that validation can detect it is invalid" do
@@ -781,6 +788,30 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
expect(work_package.estimated_hours_before_type_cast).to eq("I am a string")
end
+
+ include_examples "update progress values",
+ description: "is an error state (to be detected by contract), " \
+ "and remaining work and % complete are kept",
+ expected_hints: {}
+ end
+
+ context "when remaining work is set to a string" do
+ let(:set_attributes) { { remaining_hours: "I am a string" } }
+ let(:expected_derived_attributes) { { remaining_hours: 0.0 } }
+ let(:expected_kept_attributes) { %w[estimated_hours done_ratio] }
+
+ 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.remaining_hours_before_type_cast).to eq("I am a string")
+ end
+
+ include_examples "update progress values",
+ description: "is an error state (to be detected by contract), " \
+ "and work and % complete are kept",
+ expected_hints: {}
end
context "when work and remaining work are set" do
@@ -856,6 +887,25 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBased
expected_hints: {}
end
+ context "when remaining work is set to a string" do
+ let(:set_attributes) { { remaining_hours: "I am a string" } }
+ let(:expected_derived_attributes) { { remaining_hours: 0.0 } }
+ let(:expected_kept_attributes) { %w[estimated_hours done_ratio] }
+
+ 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.remaining_hours_before_type_cast).to eq("I am a string")
+ end
+
+ include_examples "update progress values",
+ description: "is an error state (to be detected by contract), " \
+ "and work and % complete are kept",
+ expected_hints: {}
+ end
+
context "when remaining work is set and work is cleared" do
let(:set_attributes) { { estimated_hours: nil, remaining_hours: 6.7 } }
let(:expected_kept_attributes) { %w[done_ratio] }