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
Ensure durations are positive numbers
Aaron Contreras15d4145f9e8f
diff --git a/app/contracts/settings/working_days_and_hours_params_contract.rb b/app/contracts/settings/working_days_and_hours_params_contract.rbindex 2a73109218a..4a347f4c988 100644--- a/app/contracts/settings/working_days_and_hours_params_contract.rb+++ b/app/contracts/settings/working_days_and_hours_params_contract.rb@@ -34,6 +34,7 @@ module Settingsvalidate :hours_per_day_are_presentvalidate :days_per_week_are_presentvalidate :days_per_month_are_present+ validate :durations_are_positive_valuesvalidate :days_per_week_and_days_per_month_are_consistentvalidate :unique_job@@ -63,6 +64,21 @@ module Settingsendend+ def durations_are_positive_values+ if hours_per_day &&+ days_per_week &&+ days_per_month &&+ any_duration_is_negative_or_zero?+ errors.add :base, :durations_are_not_positive_numbers+ end+ end++ def any_duration_is_negative_or_zero?+ !hours_per_day.to_i.positive? ||+ !days_per_week.to_i.positive? ||+ !days_per_month.to_i.positive?+ end+def days_per_week_and_days_per_month_are_consistentif days_per_week &&days_per_month &&diff --git a/config/locales/en.yml b/config/locales/en.ymlindex 2c9ad9191e6..48e989a9ffb 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -1020,6 +1020,7 @@ Project attributes and sections are defined in the <a href=%{admin_settings_url}hours_per_day_are_missing: "The number of hours per day must be defined."days_per_week_are_missing: "The number of days per week must be defined."days_per_month_are_missing: "The number of days per month must be defined."+ durations_are_not_positive_numbers: "The durations must be positive numbers."days_per_week_and_days_per_month_are_inconsistent: "The number of days per week and the number of days per month must be consistent."time_entry:attributes:diff --git a/spec/contracts/settings/working_days_and_hours_params_contract_spec.rb b/spec/contracts/settings/working_days_and_hours_params_contract_spec.rbindex faf60b034ab..e2ed5cdbf7a 100644--- a/spec/contracts/settings/working_days_and_hours_params_contract_spec.rb+++ b/spec/contracts/settings/working_days_and_hours_params_contract_spec.rb@@ -69,4 +69,32 @@ RSpec.describe Settings::WorkingDaysAndHoursParamsContract doinclude_examples "contract is invalid", base: :days_per_week_and_days_per_month_are_inconsistentend++ describe "0 durations" do+ context "when hours_per_day is 0" do+ let(:params) { { working_days: [1], hours_per_day: 0, days_per_week: 5, days_per_month: 20 } }++ include_examples "contract is invalid", base: :durations_are_not_positive_numbers+ end++ # These two are correlated. Making only one of them 0 will also+ # add the "incosistent" error tested for above.+ context "when days_per_week or days_per_month is 0" do+ let(:params) { { working_days: [1], hours_per_day: 8, days_per_week: 0, days_per_month: 0 } }++ include_examples "contract is invalid", base: :durations_are_not_positive_numbers+ end++ context "when all durations are 0" do+ let(:params) { { working_days: [1], hours_per_day: 0, days_per_week: 0, days_per_month: 0 } }++ include_examples "contract is invalid", base: :durations_are_not_positive_numbers+ end+ end++ describe "Negative durations" do+ let(:params) { { working_days: [1], hours_per_day: -2, days_per_week: -5, days_per_month: -20 } }++ include_examples "contract is invalid", base: :durations_are_not_positive_numbers+ endend