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
Raise error if no working days defined at all
Christophe Bliardf5ad11f893a6
diff --git a/app/services/work_packages/shared/working_days.rb b/app/services/work_packages/shared/working_days.rbindex 031f5e60550..c15815236b0 100644--- a/app/services/work_packages/shared/working_days.rb+++ b/app/services/work_packages/shared/working_days.rb@@ -124,6 +124,7 @@ module WorkPackagesenddef working_week_day?(date)+ assert_some_working_week_days_existworking_week_days[date.wday]end@@ -131,6 +132,16 @@ module WorkPackagesnon_working_dates.exclude?(date)end+ def assert_some_working_week_days_exist+ return if @working_week_days_exist++ if working_week_days.all? { |working| working == false }+ raise 'cannot have all week days as non-working days'+ end++ @working_week_days_exist = true+ end+def working_week_daysreturn @working_week_days if defined?(@working_week_days)diff --git a/spec/features/work_packages/details/workdays_spec.rb b/spec/features/work_packages/details/workdays_spec.rbindex a86dff41ea7..8bda951ac15 100644--- a/spec/features/work_packages/details/workdays_spec.rb+++ b/spec/features/work_packages/details/workdays_spec.rb@@ -74,19 +74,4 @@ describe 'Work packages datepicker workdays',endendend-- context 'with all days marked as weekend' do- let(:week_days) do- days = create(:week_days)-- WeekDay.update_all(working: false)-- days- end-- it 'shows them as disabled' do- expect(page).to have_selector('.dayContainer', count: 2)- expect(page).to have_selector('.dayContainer:first-of-type .flatpickr-day.flatpickr-non-working-day', count: 31)- end- endenddiff --git a/spec/services/work_packages/shared/all_days_spec.rb b/spec/services/work_packages/shared/all_days_spec.rbindex ca774a15a97..2d5eb0ded77 100644--- a/spec/services/work_packages/shared/all_days_spec.rb+++ b/spec/services/work_packages/shared/all_days_spec.rb@@ -49,7 +49,7 @@ RSpec.describe WorkPackages::Shared::AllDays doendend- context 'with non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 do+ context 'with some non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 doinclude_examples 'it returns duration', 365, Date.new(2022, 1, 1), Date.new(2022, 12, 31)include_examples 'it returns duration', 365 * 2, Date.new(2022, 1, 1), Date.new(2023, 12, 31)end@@ -106,7 +106,7 @@ RSpec.describe WorkPackages::Shared::AllDays doinclude_examples 'start_date', due_date: sunday_2022_07_31, duration: 10, expected: sunday_2022_07_31 - 9.daysend- context 'with non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 do+ context 'with some non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 doinclude_examples 'start_date', due_date: Date.new(2022, 12, 31), duration: 365, expected: Date.new(2022, 1, 1)include_examples 'start_date', due_date: Date.new(2023, 12, 31), duration: 365 * 2, expected: Date.new(2022, 1, 1)end@@ -139,7 +139,7 @@ RSpec.describe WorkPackages::Shared::AllDays doinclude_examples 'due_date', start_date: sunday_2022_07_31, duration: 10, expected: sunday_2022_07_31 + 9.daysend- context 'with non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 do+ context 'with some non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 doinclude_examples 'due_date', start_date: Date.new(2022, 1, 1), duration: 365, expected: Date.new(2022, 12, 31)include_examples 'due_date', start_date: Date.new(2022, 1, 1), duration: 365 * 2, expected: Date.new(2023, 12, 31)end@@ -167,7 +167,7 @@ RSpec.describe WorkPackages::Shared::AllDays doendend- context 'with non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 do+ context 'with some non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 doit 'adds the number of days to the date' doexpect(subject.add_days(Date.new(2022, 1, 1), 365)).to eq(Date.new(2023, 1, 1))expect(subject.add_days(Date.new(2022, 1, 1), -365)).to eq(Date.new(2021, 1, 1))@@ -190,7 +190,7 @@ RSpec.describe WorkPackages::Shared::AllDays doendend- context 'with non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 do+ context 'with some non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 doit 'returns the given day' doexpect(subject.soonest_working_day(Date.new(2022, 12, 25))).to eq(Date.new(2022, 12, 25))end@@ -209,7 +209,7 @@ RSpec.describe WorkPackages::Shared::AllDays doendend- context 'with non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 do+ context 'with some non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 doit 'returns the same delta as without them' doexpect(subject.delta(previous: Date.new(2022, 12, 1), current: Date.new(2023, 1, 1))).to eq(31)enddiff --git a/spec/services/work_packages/shared/shared_examples_days.rb b/spec/services/work_packages/shared/shared_examples_days.rbindex 6aaeef97763..401f7432e3f 100644--- a/spec/services/work_packages/shared/shared_examples_days.rb+++ b/spec/services/work_packages/shared/shared_examples_days.rb@@ -37,9 +37,18 @@ RSpec.shared_context 'with non working days Christmas 2022 and new year 2023' doshared_let(:new_year_day) { create(:non_working_day, date: Date.new(2023, 1, 1)) }end+RSpec.shared_context 'with no working days' do+ include_context 'with weekend days Saturday and Sunday'++ before do+ WeekDay.update_all(working: false)+ end+end+RSpec.configure do |rspec|rspec.include_context 'with weekend days Saturday and Sunday', :weekend_saturday_sundayrspec.include_context 'with non working days Christmas 2022 and new year 2023', :christmas_2022_new_year_2023+ rspec.include_context 'with no working days', :no_working_daysendRSpec.shared_examples 'it returns duration' do |expected_duration, start_date, due_date|diff --git a/spec/services/work_packages/shared/working_days_spec.rb b/spec/services/work_packages/shared/working_days_spec.rbindex aacad55a6ab..bb8e74ae1b9 100644--- a/spec/services/work_packages/shared/working_days_spec.rb+++ b/spec/services/work_packages/shared/working_days_spec.rb@@ -72,7 +72,7 @@ RSpec.describe WorkPackages::Shared::WorkingDays doinclude_examples 'it returns duration', 5, wednesday_2022_08_03, Date.new(2022, 8, 9)end- context 'with non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 do+ context 'with some non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 doinclude_examples 'it returns duration', 0, Date.new(2022, 12, 25), Date.new(2022, 12, 25)include_examples 'it returns duration', 1, Date.new(2022, 12, 24), Date.new(2022, 12, 25)include_examples 'it returns duration', 8, Date.new(2022, 12, 24), Date.new(2023, 1, 2)@@ -141,7 +141,7 @@ RSpec.describe WorkPackages::Shared::WorkingDays doinclude_examples 'start_date', due_date: saturday_2022_07_30, duration: 6, expected: friday_2022_07_29 - 7.daysend- context 'with non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 do+ context 'with some non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 doinclude_examples 'start_date', due_date: Date.new(2022, 12, 26), duration: 2, expected: Date.new(2022, 12, 24)include_examples 'start_date', due_date: Date.new(2023, 1, 2), duration: 8, expected: Date.new(2022, 12, 24)end@@ -185,7 +185,7 @@ RSpec.describe WorkPackages::Shared::WorkingDays doinclude_examples 'due_date', start_date: saturday_2022_07_30, duration: 6, expected: monday_2022_08_01 + 7.daysend- context 'with non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 do+ context 'with some non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 doinclude_examples 'due_date', start_date: Date.new(2022, 12, 24), duration: 2, expected: Date.new(2022, 12, 26)include_examples 'due_date', start_date: Date.new(2022, 12, 24), duration: 8, expected: Date.new(2023, 1, 2)end@@ -236,7 +236,7 @@ RSpec.describe WorkPackages::Shared::WorkingDays doinclude_examples 'add_days returns date', date: Date.new(2022, 12, 31), count: -365, expected: Date.new(2021, 8, 9)end- context 'with non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 do+ context 'with some non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 doinclude_examples 'add_days returns date', date: Date.new(2022, 12, 24), count: 1, expected: Date.new(2022, 12, 26)include_examples 'add_days returns date', date: Date.new(2022, 12, 24), count: 7, expected: Date.new(2023, 1, 2)@@ -261,11 +261,18 @@ RSpec.describe WorkPackages::Shared::WorkingDays doinclude_examples 'soonest working day', date: monday_2022_08_01, expected: monday_2022_08_01end- context 'with non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 do+ context 'with some non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 doinclude_examples 'soonest working day', date: Date.new(2022, 12, 25), expected: Date.new(2022, 12, 26)include_examples 'soonest working day', date: Date.new(2022, 12, 31), expected: Date.new(2022, 12, 31)include_examples 'soonest working day', date: Date.new(2023, 1, 1), expected: Date.new(2023, 1, 2)end++ context 'with no working days', :no_working_days do+ it 'prevents looping infinitely by raising a runtime error' do+ expect { subject.soonest_working_day(sunday_2022_07_31) }+ .to raise_error(RuntimeError, 'cannot have all week days as non-working days')+ end+ endenddescribe '#delta' do@@ -287,7 +294,7 @@ RSpec.describe WorkPackages::Shared::WorkingDays doinclude_examples 'delta', previous: friday_2022_07_29, current: Date.new(2022, 8, 8), expected: 6end- context 'with non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 do+ context 'with some non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 doinclude_examples 'delta', previous: Date.new(2022, 12, 27), current: Date.new(2022, 12, 20), expected: -6endend