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] Validate % complete numericality when it's a string
Christophe Bliard03721ce32cbc
diff --git a/app/models/work_package.rb b/app/models/work_package.rbindex 0ed89cfe373..8ab973b435e 100644--- a/app/models/work_package.rb+++ b/app/models/work_package.rb@@ -328,6 +328,10 @@ class WorkPackage < ApplicationRecordwrite_attribute :remaining_hours, convert_duration_to_hours(hours)end+ def done_ratio=(value)+ write_attribute :done_ratio, convert_value_to_percentage(value)+ end+def duration_in_hoursduration ? duration * 24 : nilend@@ -561,6 +565,13 @@ class WorkPackage < ApplicationRecordvalueend+ def convert_value_to_percentage(value)+ if value.is_a?(String) && PercentageConverter.valid?(value)+ value = PercentageConverter.parse(value)+ end+ value+ end+### Checks if the time entry defined by the given attributes is blank.# A time entry counts as blank despite a selected activity if that activitydiff --git a/app/models/work_package/validations.rb b/app/models/work_package/validations.rbindex 80c849dc620..28bc8b4c5cd 100644--- a/app/models/work_package/validations.rb+++ b/app/models/work_package/validations.rb@@ -33,7 +33,7 @@ module WorkPackage::Validationsvalidates :subject, :priority, :project, :type, :author, :status, presence: truevalidates :subject, length: { maximum: 255 }- validates :done_ratio, inclusion: { in: 0..100 }, allow_nil: true+ validates :done_ratio, inclusion: { in: 0..100 }, numericality: true, allow_nil: truevalidates :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/percentage_converter.rb b/app/services/percentage_converter.rbnew file mode 100644index 00000000000..75774b001f8--- /dev/null+++ b/app/services/percentage_converter.rb@@ -0,0 +1,57 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# ++++class PercentageConverter+ class ParseError < StandardError; end++ class << self+ VALID_PERCENTAGE = /\A\s*(\+|-)?\d+(\.\d*)?\s*%?\s*\z/++ # Parse a string representing a percentage and return the value as a float.+ def parse(percentage_string)+ return nil if percentage_string.blank?+ raise ParseError, "invalid percentage: #{percentage_string}" unless valid?(percentage_string)++ percentage_string.to_f+ end++ # Returns true for a value which could assigned to a % complete value (done_ratio).+ def valid?(percentage)+ case percentage+ when String+ percentage.blank? || percentage.match?(VALID_PERCENTAGE)+ when Numeric, nil+ true+ else+ false+ end+ end+ end+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 568d8ffc24a..9dafb8c9289 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@@ -46,6 +46,7 @@ class WorkPackages::SetAttributesServicework&.negative? \|| remaining_work&.negative? \|| percent_complete_out_of_range? \+ || percent_complete_unparsable? \|| remaining_work_set_greater_than_work?end@@ -113,6 +114,10 @@ class WorkPackages::SetAttributesServiceremaining_work / remaining_percent_completeend+ def percent_complete_unparsable?+ !PercentageConverter.valid?(work_package.done_ratio_before_type_cast)+ end+def remaining_work_set_greater_than_work?attributes_from_user == %i[remaining_work] && work && remaining_work && remaining_work > workenddiff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rbindex b23d2004dd8..7b9e7f29985 100644--- a/spec/contracts/work_packages/base_contract_spec.rb+++ b/spec/contracts/work_packages/base_contract_spec.rb@@ -474,6 +474,14 @@ RSpec.describe WorkPackages::BaseContract,it_behaves_like "contract is invalid", done_ratio: :inclusionend+ context "when not a number" do+ let(:estimated_hours) { nil }+ let(:remaining_hours) { nil }+ let(:done_ratio) { "abc" }++ it_behaves_like "contract is invalid", done_ratio: :not_a_number+ end+context "when only one being set" dolet(:estimated_hours) { nil }let(:remaining_hours) { nil }diff --git a/spec/models/work_package_spec.rb b/spec/models/work_package_spec.rbindex 06fc8aa916d..68eec331053 100644--- a/spec/models/work_package_spec.rb+++ b/spec/models/work_package_spec.rb@@ -373,6 +373,72 @@ RSpec.describe WorkPackage dodone_ratio: 30)end+ it "allows empty value" do+ work_package.done_ratio = ""+ expect(work_package).to be_valid+ expect(work_package.done_ratio).to be_nil+ end++ it "allows blank values" do+ work_package.done_ratio = " "+ expect(work_package).to be_valid+ expect(work_package.done_ratio).to be_nil+ end++ it "allows nil value" do+ work_package.done_ratio = nil+ expect(work_package).to be_valid+ expect(work_package.done_ratio).to be_nil+ end++ it "allows values between 0 and 100" do+ work_package.done_ratio = 0+ expect(work_package).to be_valid+ work_package.done_ratio = 34+ expect(work_package).to be_valid+ work_package.done_ratio = 99+ expect(work_package).to be_valid++ work_package.done_ratio = "1"+ expect(work_package).to be_valid+ work_package.done_ratio = "100"+ expect(work_package).to be_valid+ end++ it "disallows values outside of the 0-100 range" do+ work_package.done_ratio = -1+ expect(work_package).not_to be_valid++ work_package.done_ratio = "-1%"+ expect(work_package.done_ratio).to eq(-1)+ expect(work_package).not_to be_valid++ work_package.done_ratio = 101.0+ expect(work_package.done_ratio).to eq(101)+ expect(work_package).not_to be_valid+ end++ it "allows floats and truncates them to integer" do+ work_package.done_ratio = 1.7+ expect(work_package).to be_valid+ expect(work_package.done_ratio).to eq(1)++ work_package.done_ratio = "1.7"+ expect(work_package).to be_valid+ expect(work_package.done_ratio).to eq(1)+ end++ it "allows percentage like '50%'" do+ work_package.done_ratio = "50%"+ expect(work_package).to be_valid+ expect(work_package.done_ratio).to eq(50)+ end++ it "disallows string values, that are not valid percentage values" do+ work_package.done_ratio = "abc"+ expect(work_package).not_to be_valid+ end+describe "#value" docontext "for work-based mode",with_settings: { work_package_done_ratio: "field" } dodiff --git a/spec/services/percentage_converter_spec.rb b/spec/services/percentage_converter_spec.rbnew file mode 100644index 00000000000..4bf78be9995--- /dev/null+++ b/spec/services/percentage_converter_spec.rb@@ -0,0 +1,132 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+# ++++require "spec_helper"++RSpec.describe PercentageConverter 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.0 when given '0%'" do+ expect(described_class.parse("0%")).to be(0.0)+ expect(described_class.parse("0 %")).to be(0.0)+ expect(described_class.parse(" 0 % ")).to be(0.0)+ end++ it "returns a float when given a string repsenting an integer" do+ expect(described_class.parse("50")).to be(50.0)+ expect(described_class.parse(" 50 ")).to be(50.0)+ expect(described_class.parse(" -23 ")).to be(-23.0)+ expect(described_class.parse(" +1278 ")).to be(1278.0)+ expect(described_class.parse(" -0 ")).to eq(0.0)+ expect(described_class.parse(" +0% ")).to be(0.0)+ end++ it "returns a float when given a string representing a float" do+ expect(described_class.parse("5.")).to be(5.0)+ expect(described_class.parse("5.%")).to be(5.0)+ expect(described_class.parse("5.7%")).to be(5.7)+ expect(described_class.parse("5.75")).to be(5.75)+ expect(described_class.parse("5.75%")).to be(5.75)+ expect(described_class.parse("5.75 %")).to be(5.75)+ expect(described_class.parse(" -5.75 % ")).to be(-5.75)+ expect(described_class.parse("+5.75 %")).to be(5.75)+ expect(described_class.parse(" 123.321 % ")).to be(123.321)+ expect(described_class.parse("-456.654 %")).to be(-456.654)+ end++ it "does not get tricked with octals representation" do+ expect(described_class.parse("09")).to be(9.0)+ expect(described_class.parse("010")).to be(10.0)+ end++ it "raises a `ParseError` if it's not a valid percentage" do+ expect { described_class.parse("invalid percentage") }.to raise_error(PercentageConverter::ParseError)+ end+ end++ describe ".valid?" do+ it "returns true for integers" do+ expect(described_class.valid?(0)).to be(true)+ expect(described_class.valid?(100)).to be(true)+ expect(described_class.valid?(789)).to be(true)+ expect(described_class.valid?(-456)).to be(true)+ end++ it "returns true for floats" do+ expect(described_class.valid?(0.0)).to be(true)+ expect(described_class.valid?(-0.0)).to be(true)+ expect(described_class.valid?(23.1)).to be(true)+ expect(described_class.valid?(100.2)).to be(true)+ expect(described_class.valid?(789.3)).to be(true)+ expect(described_class.valid?(-456.4)).to be(true)+ 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 number or a valid percentage" do+ expect(described_class.valid?("50")).to be(true)+ expect(described_class.valid?(" 50 ")).to be(true)+ expect(described_class.valid?(" -23 ")).to be(true)+ expect(described_class.valid?(" +1278 ")).to be(true)+ expect(described_class.valid?(" -0 ")).to be(true)+ expect(described_class.valid?(" +0% ")).to be(true)+ expect(described_class.valid?("5.")).to be(true)+ expect(described_class.valid?("5.%")).to be(true)+ expect(described_class.valid?(" -5.75 % ")).to be(true)+ expect(described_class.valid?(" 1234.0 % ")).to be(true)+ end++ it "rreturns false for strings not representing a number" do+ 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.4.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?("12%%")).to be(false)+ expect(described_class.valid?("12%.4")).to be(false)+ end+ end+enddiff --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 40153066f5c..3f6ad4a78cf 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@@ -307,6 +307,23 @@ RSpec.describe WorkPackages::SetAttributesService::DeriveProgressValuesWorkBaseddescription: "is an error state (to be detected by contract), and work and remaining work are kept"end+ context "when % complete is a string not representing a valid percentage" do+ let(:set_attributes) { { done_ratio: "invalid percentage" } }+ let(:expected_derived_attributes) { { done_ratio: 0 } } # coerced to integer by activerecord+ let(:expected_kept_attributes) { %w[estimated_hours remaining_hours] }++ 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.done_ratio_before_type_cast).to eq("invalid percentage")+ end++ 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" dolet(:set_attributes) { { estimated_hours: 20, remaining_hours: 12.0, done_ratio: 40 } }let(:expected_kept_attributes) { %w[estimated_hours remaining_hours done_ratio] }