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

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.rb
index 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 WorkPackages
end
def working_week_day?(date)
+ assert_some_working_week_days_exist
working_week_days[date.wday]
end
@@ -131,6 +132,16 @@ module WorkPackages
non_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_days
return @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.rb
index 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',
end
end
end
-
- 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
- end
end
diff --git a/spec/services/work_packages/shared/all_days_spec.rb b/spec/services/work_packages/shared/all_days_spec.rb
index 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 do
end
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 do
include_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 do
include_examples 'start_date', due_date: sunday_2022_07_31, duration: 10, expected: sunday_2022_07_31 - 9.days
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 do
include_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 do
include_examples 'due_date', start_date: sunday_2022_07_31, duration: 10, expected: sunday_2022_07_31 + 9.days
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 do
include_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 do
end
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 do
it 'adds the number of days to the date' do
expect(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 do
end
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 do
it 'returns the given day' do
expect(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 do
end
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 do
it 'returns the same delta as without them' do
expect(subject.delta(previous: Date.new(2022, 12, 1), current: Date.new(2023, 1, 1))).to eq(31)
end
diff --git a/spec/services/work_packages/shared/shared_examples_days.rb b/spec/services/work_packages/shared/shared_examples_days.rb
index 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' do
shared_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_sunday
rspec.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_days
end
RSpec.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.rb
index 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 do
include_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 do
include_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 do
include_examples 'start_date', due_date: saturday_2022_07_30, duration: 6, expected: friday_2022_07_29 - 7.days
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 do
include_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 do
include_examples 'due_date', start_date: saturday_2022_07_30, duration: 6, expected: monday_2022_08_01 + 7.days
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 do
include_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 do
include_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 do
include_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 do
include_examples 'soonest working day', date: monday_2022_08_01, expected: monday_2022_08_01
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 do
include_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
+ end
end
describe '#delta' do
@@ -287,7 +294,7 @@ RSpec.describe WorkPackages::Shared::WorkingDays do
include_examples 'delta', previous: friday_2022_07_29, current: Date.new(2022, 8, 8), expected: 6
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 do
include_examples 'delta', previous: Date.new(2022, 12, 27), current: Date.new(2022, 12, 20), expected: -6
end
end