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

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.rb
index 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 Settings
validate :hours_per_day_are_present
validate :days_per_week_are_present
validate :days_per_month_are_present
+ validate :durations_are_positive_values
validate :days_per_week_and_days_per_month_are_consistent
validate :unique_job
@@ -63,6 +64,21 @@ module Settings
end
end
+ 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_consistent
if days_per_week &&
days_per_month &&
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 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.rb
index 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 do
include_examples "contract is invalid", base: :days_per_week_and_days_per_month_are_inconsistent
end
+
+ 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
+ end
end