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
[#64532] The following phase's finish date is inconsistent with the start date
Dombi Attilab82abf8442ce
diff --git a/app/models/project/phase.rb b/app/models/project/phase.rbindex 5e7f5bf8ce4..d6d65121288 100644--- a/app/models/project/phase.rb+++ b/app/models/project/phase.rb@@ -61,6 +61,10 @@ class Project::Phase < ApplicationRecordendend+ def any_dates_set?+ start_date? || finish_date?+ end+def date_range_set?start_date? && finish_date?enddiff --git a/app/services/project_phases/activation_service.rb b/app/services/project_phases/activation_service.rbindex 2a892b49900..6d8e9eeddf0 100644--- a/app/services/project_phases/activation_service.rb+++ b/app/services/project_phases/activation_service.rb@@ -85,17 +85,23 @@ module ProjectPhasesif preceding_phasepreceding_phase- elsif phase.date_range_set?+ elsif phase.any_dates_set?phaseendenddef preceding_active_phase(phase)- project.available_phases.reverse.find { it.date_range_set? && it.position < phase.position }+ project.available_phases.reverse.find { it.any_dates_set? && it.position < phase.position }enddef initial_reschedule_date(phase)- phase.active? ? phase.finish_date + 1 : phase.start_date+ if phase.date_range_set?+ phase.active? ? phase.finish_date + 1 : phase.start_date+ else+ from = phase.start_date || phase.finish_date+ from += 1 if phase.active?+ from+ endenddef default_contract_classdiff --git a/app/services/project_phases/reschedule_service.rb b/app/services/project_phases/reschedule_service.rbindex e51f8bc7069..d60aaa8bc6a 100644--- a/app/services/project_phases/reschedule_service.rb+++ b/app/services/project_phases/reschedule_service.rb@@ -52,8 +52,13 @@ module ProjectPhasesif phase.date_range_set?next_start_date = reschedule_phase_and_retrieve_next_start(phase, from)from = next_start_date unless next_start_date.nil?- elsif phase.start_date.present?- reschedule_phase_start_only(phase, from)+ else+ reschedule_phase_start(phase, from)++ if phase.finish_date.present?+ next_start_date = reschedule_phase_finish_and_retrieve_next_start(phase, from)+ from = next_start_date unless next_start_date.nil?+ endendendend@@ -74,9 +79,16 @@ module ProjectPhases[days.first.date, days.last.date] if days.length == durationend- def reschedule_phase_start_only(phase, from)+ def reschedule_phase_start(phase, from)start_date = working_days_from(from, count: 1).first.date- phase.update(start_date: start_date)+ phase.update(start_date:)+ end++ def reschedule_phase_finish_and_retrieve_next_start(phase, from)+ finish_date = [from, phase.finish_date].max+ working_finish_date = working_days_from(finish_date, count: 1).first.date+ phase.update(finish_date: working_finish_date)+ working_finish_date + 1enddef working_days_from(from, count:)diff --git a/app/services/project_phases/update_service.rb b/app/services/project_phases/update_service.rbindex ad9a4bfd9c0..f73cbe6c3eb 100644--- a/app/services/project_phases/update_service.rb+++ b/app/services/project_phases/update_service.rb@@ -33,7 +33,7 @@ module ProjectPhasesdelegate :project, to: :modeldef after_perform(*)- reschedule_following_phases if model.date_range_set?+ reschedule_following_phases if model.any_dates_set?project.touch_and_save_journals@@ -48,7 +48,13 @@ module ProjectPhasesenddef initial_reschedule_date- model.active? ? model.finish_date + 1 : model.start_date+ if model.date_range_set?+ model.active? ? model.finish_date + 1 : model.start_date+ else+ from = model.start_date || model.finish_date+ from += 1 if model.active?+ from+ endendendenddiff --git a/spec/services/project_phases/activation_service_spec.rb b/spec/services/project_phases/activation_service_spec.rbindex a7158361076..c17a351b26d 100644--- a/spec/services/project_phases/activation_service_spec.rb+++ b/spec/services/project_phases/activation_service_spec.rb@@ -173,6 +173,58 @@ RSpec.describe ProjectPhases::ActivationService, type: :model doendend+ context "with a start date only" do+ context "and earlier than the preceding phase finish date" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: date - 3, finish_date: nil) }++ it "reschedules that and following phases" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: true, start_date: date, finish_date: nil)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end++ context "and later than the preceding phase finish date" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: date + 2, finish_date: nil) }++ it "reschedules that and following phases" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: true, start_date: date, finish_date: nil)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end+ end++ context "with a finish date only" do+ context "and earlier than the preceding phase finish date" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: nil, finish_date: date - 3) }++ it "reschedules that and following phases filling the start date and creating 1 day phase" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: true, start_date: date, finish_date: date)+ expect(phase2.reload).to have_attributes(active: true, start_date: date + 1, finish_date: date + 3)+ end+ end++ context "and later than the preceding phase finish date" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: nil, finish_date: date + 3) }++ it "reschedules that and following phases filling the start date and maintining the finish date" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: true, start_date: date, finish_date: date + 3)+ expect(phase2.reload).to have_attributes(active: true, start_date: date + 4, finish_date: date + 6)+ end+ end+ end+context "when already activated" dolet!(:phase1) { create_phase(definition: definitions[1], active: true, start_date: date + 2, finish_date: date + 3) }@@ -192,7 +244,7 @@ RSpec.describe ProjectPhases::ActivationService, type: :model doservice.call(active: true)expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: date - 1)- expect(phase1.reload).to have_attributes(active: true, start_date: nil, finish_date: nil)+ expect(phase1.reload).to have_attributes(active: true, start_date: date, finish_date: nil)expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)endend@@ -303,6 +355,172 @@ RSpec.describe ProjectPhases::ActivationService, type: :model doendend+ context "having preceding phases with a start_date only" do+ let!(:phase0) { create_phase(definition: definitions[0], active: true, start_date: date - 1, finish_date: nil) }+ let!(:phase2) { create_phase(definition: definitions[2], active: true, start_date: date + 7, finish_date: date + 9) }++ let(:service) { described_class.new(user:, project:, definitions: [definitions[1]]) }++ context "with date range" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: date + 2, finish_date: date + 3) }++ it "reschedules that and following phases" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: nil)+ expect(phase1.reload).to have_attributes(active: true, start_date: date, finish_date: date + 1)+ expect(phase2.reload).to have_attributes(active: true, start_date: date + 2, finish_date: date + 4)+ end+ end++ context "with a start date only" do+ context "and earlier than the preceding phase start date" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: date - 3, finish_date: nil) }++ it "reschedules that and following phases" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: nil)+ expect(phase1.reload).to have_attributes(active: true, start_date: date, finish_date: nil)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end++ context "and later than the preceding phase start date" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: date + 2, finish_date: nil) }++ it "reschedules that and following phases" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: nil)+ expect(phase1.reload).to have_attributes(active: true, start_date: date, finish_date: nil)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end+ end++ context "with a finish date only" do+ context "and earlier than the preceding phase start date" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: nil, finish_date: date - 3) }++ it "reschedules that and following phases filling the start date and creating 1 day phase" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: nil)+ expect(phase1.reload).to have_attributes(active: true, start_date: date, finish_date: date)+ expect(phase2.reload).to have_attributes(active: true, start_date: date + 1, finish_date: date + 3)+ end+ end++ context "and later than the preceding phase finish date" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: nil, finish_date: date + 3) }++ it "reschedules that and following phases filling the start date and maintining the finish date" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: nil)+ expect(phase1.reload).to have_attributes(active: true, start_date: date, finish_date: date + 3)+ expect(phase2.reload).to have_attributes(active: true, start_date: date + 4, finish_date: date + 6)+ end+ end+ end++ context "without date range" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: nil, finish_date: nil) }++ it "reschedules following phases using dates of the closest preceding phase with start date" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: nil)+ expect(phase1.reload).to have_attributes(active: true, start_date: date, finish_date: nil)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end+ end++ context "having preceding phases with a finish_date only" do+ let!(:phase0) { create_phase(definition: definitions[0], active: true, start_date: nil, finish_date: date - 1) }+ let!(:phase2) { create_phase(definition: definitions[2], active: true, start_date: date + 7, finish_date: date + 9) }++ let(:service) { described_class.new(user:, project:, definitions: [definitions[1]]) }++ context "with date range" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: date + 2, finish_date: date + 3) }++ it "reschedules that and following phases" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: nil, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: true, start_date: date, finish_date: date + 1)+ expect(phase2.reload).to have_attributes(active: true, start_date: date + 2, finish_date: date + 4)+ end+ end++ context "with a start date only" do+ context "and earlier than the preceding phase finish date" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: date - 3, finish_date: nil) }++ it "reschedules that and following phases" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: nil, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: true, start_date: date, finish_date: nil)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end++ context "and later than the preceding phase finish date" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: date + 2, finish_date: nil) }++ it "reschedules that and following phases" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: nil, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: true, start_date: date, finish_date: nil)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end+ end++ context "with a finish date only" do+ context "and earlier than the preceding phase finish date" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: nil, finish_date: date - 3) }++ it "reschedules that and following phases filling the start date and creating 1 day phase" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: nil, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: true, start_date: date, finish_date: date)+ expect(phase2.reload).to have_attributes(active: true, start_date: date + 1, finish_date: date + 3)+ end+ end++ context "and later than the preceding phase finish date" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: nil, finish_date: date + 3) }++ it "reschedules that and following phases filling the start date and maintining the finish date" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: nil, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: true, start_date: date, finish_date: date + 3)+ expect(phase2.reload).to have_attributes(active: true, start_date: date + 4, finish_date: date + 6)+ end+ end+ end++ context "without date range" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: nil, finish_date: nil) }++ it "reschedules following phases using dates of the closest preceding phase with start date" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: nil, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: true, start_date: date, finish_date: nil)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end+ end+context "having preceding phases without date range" dolet!(:phase0) { create_phase(definition: definitions[0], active: true, start_date: nil, finish_date: nil) }let!(:phase2) { create_phase(definition: definitions[2], active: true, start_date: date + 7, finish_date: date + 9) }@@ -321,6 +539,58 @@ RSpec.describe ProjectPhases::ActivationService, type: :model doendend+ context "with a start date only" do+ context "and earlier than the following phase" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: date - 3, finish_date: nil) }++ it "reschedules that and following phases" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: nil, finish_date: nil)+ expect(phase1.reload).to have_attributes(active: true, start_date: date - 3, finish_date: nil)+ expect(phase2.reload).to have_attributes(active: true, start_date: date - 2, finish_date: date)+ end+ end++ context "and later than the following phase" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: date + 10, finish_date: nil) }++ it "reschedules that and following phases" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: nil, finish_date: nil)+ expect(phase1.reload).to have_attributes(active: true, start_date: date + 10, finish_date: nil)+ expect(phase2.reload).to have_attributes(active: true, start_date: date + 11, finish_date: date + 13)+ end+ end+ end++ context "with a finish date only" do+ context "and earlier than the following phase" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: nil, finish_date: date - 3) }++ it "reschedules that and following phases filling the start date and creating 1 day phase" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: nil, finish_date: nil)+ expect(phase1.reload).to have_attributes(active: true, start_date: nil, finish_date: date - 3)+ expect(phase2.reload).to have_attributes(active: true, start_date: date - 2, finish_date: date)+ end+ end++ context "and later than the following phase" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: nil, finish_date: date + 10) }++ it "reschedules that and following phases filling the start date and maintining the finish date" do+ service.call(active: true)++ expect(phase0.reload).to have_attributes(active: true, start_date: nil, finish_date: nil)+ expect(phase1.reload).to have_attributes(active: true, start_date: nil, finish_date: date + 10)+ expect(phase2.reload).to have_attributes(active: true, start_date: date + 11, finish_date: date + 13)+ end+ end+ end+context "when already activated" dolet!(:phase1) { create_phase(definition: definitions[1], active: true, start_date: date + 2, finish_date: date + 3) }@@ -391,44 +661,204 @@ RSpec.describe ProjectPhases::ActivationService, type: :model doendcontext "when deactivating one phase" do- let!(:phase0) { create_phase(definition: definitions[0], active: true, start_date: date - 1, finish_date: date - 1) }- let!(:phase2) { create_phase(definition: definitions[2], active: true, start_date: date + 7, finish_date: date + 9) }+ context "having preceding phases with date range" do+ let!(:phase0) { create_phase(definition: definitions[0], active: true, start_date: date - 1, finish_date: date - 1) }+ let!(:phase2) { create_phase(definition: definitions[2], active: true, start_date: date + 7, finish_date: date + 9) }- let(:service) { described_class.new(user:, project:, definitions: [definitions[1]]) }+ let(:service) { described_class.new(user:, project:, definitions: [definitions[1]]) }- context "with date range" do- let!(:phase1) { create_phase(definition: definitions[1], active: true, start_date: date + 2, finish_date: date + 3) }+ context "with date range" do+ let!(:phase1) { create_phase(definition: definitions[1], active: true, start_date: date + 2, finish_date: date + 3) }- it "reschedules following phases using dates of the closest preceding phase with date range" do- service.call(active: false)+ it "reschedules following phases using dates of the closest preceding phase with date range" do+ service.call(active: false)- expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: date - 1)- expect(phase1.reload).to have_attributes(active: false, start_date: date + 2, finish_date: date + 3)- expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: false, start_date: date + 2, finish_date: date + 3)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end++ context "when already deactivated" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: date + 2, finish_date: date + 3) }++ it "reschedules following phases using dates of the closest preceding phase with date range" do+ service.call(active: false)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: false, start_date: date + 2, finish_date: date + 3)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end++ context "without date range" do+ let!(:phase1) { create_phase(definition: definitions[1], active: true, start_date: nil, finish_date: nil) }++ it "reschedules following phases using dates of the closest preceding phase with date range" do+ service.call(active: false)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: false, start_date: nil, finish_date: nil)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end++ context "with a start date only" do+ let!(:phase1) { create_phase(definition: definitions[1], active: true, start_date: date - 3, finish_date: nil) }++ it "reschedules following phases using dates of the closest preceding phase with date range" do+ service.call(active: false)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: false, start_date: date - 3, finish_date: nil)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end++ context "with a finish date only" do+ let!(:phase1) { create_phase(definition: definitions[1], active: true, start_date: nil, finish_date: date - 3) }++ it "reschedules that and following phases filling the start date and creating 1 day phase" do+ service.call(active: false)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: false, start_date: nil, finish_date: date - 3)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ endendend- context "when already deactivated" do- let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: date + 2, finish_date: date + 3) }+ context "having preceding phases with a start_date only" do+ let!(:phase0) { create_phase(definition: definitions[0], active: true, start_date: date - 1, finish_date: nil) }+ let!(:phase2) { create_phase(definition: definitions[2], active: true, start_date: date + 7, finish_date: date + 9) }- it "reschedules following phases using dates of the closest preceding phase with date range" do- service.call(active: false)+ let(:service) { described_class.new(user:, project:, definitions: [definitions[1]]) }++ context "with date range" do+ let!(:phase1) { create_phase(definition: definitions[1], active: true, start_date: date + 2, finish_date: date + 3) }- expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: date - 1)- expect(phase1.reload).to have_attributes(active: false, start_date: date + 2, finish_date: date + 3)- expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ it "reschedules following phases using dates of the closest preceding phase with a start date" do+ service.call(active: false)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: nil)+ expect(phase1.reload).to have_attributes(active: false, start_date: date + 2, finish_date: date + 3)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end++ context "when already deactivated" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: date + 2, finish_date: date + 3) }++ it "reschedules following phases using dates of the closest preceding phase with a start date" do+ service.call(active: false)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: nil)+ expect(phase1.reload).to have_attributes(active: false, start_date: date + 2, finish_date: date + 3)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end++ context "without date range" do+ let!(:phase1) { create_phase(definition: definitions[1], active: true, start_date: nil, finish_date: nil) }++ it "reschedules following phases using dates of the closest preceding phase with a start date" do+ service.call(active: false)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: nil)+ expect(phase1.reload).to have_attributes(active: false, start_date: nil, finish_date: nil)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end++ context "with a start date only" do+ let!(:phase1) { create_phase(definition: definitions[1], active: true, start_date: date - 3, finish_date: nil) }++ it "reschedules following phases using dates of the closest preceding phase with a start date" do+ service.call(active: false)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: nil)+ expect(phase1.reload).to have_attributes(active: false, start_date: date - 3, finish_date: nil)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end++ context "with a finish date only" do+ let!(:phase1) { create_phase(definition: definitions[1], active: true, start_date: nil, finish_date: date - 3) }++ it "reschedules following phases using the dates of the closest preceding phase with a start date" do+ service.call(active: false)++ expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: nil)+ expect(phase1.reload).to have_attributes(active: false, start_date: nil, finish_date: date - 3)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ endendend- context "without date range" do- let!(:phase1) { create_phase(definition: definitions[1], active: true, start_date: nil, finish_date: nil) }+ context "having preceding phases with a finish_date only" do+ let!(:phase0) { create_phase(definition: definitions[0], active: true, start_date: nil, finish_date: date - 1) }+ let!(:phase2) { create_phase(definition: definitions[2], active: true, start_date: date + 7, finish_date: date + 9) }++ let(:service) { described_class.new(user:, project:, definitions: [definitions[1]]) }++ context "with date range" do+ let!(:phase1) { create_phase(definition: definitions[1], active: true, start_date: date + 2, finish_date: date + 3) }++ it "reschedules following phases using dates of the closest preceding phase with a start date" do+ service.call(active: false)++ expect(phase0.reload).to have_attributes(active: true, start_date: nil, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: false, start_date: date + 2, finish_date: date + 3)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end++ context "when already deactivated" do+ let!(:phase1) { create_phase(definition: definitions[1], active: false, start_date: date + 2, finish_date: date + 3) }++ it "reschedules following phases using dates of the closest preceding phase with a start date" do+ service.call(active: false)++ expect(phase0.reload).to have_attributes(active: true, start_date: nil, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: false, start_date: date + 2, finish_date: date + 3)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end++ context "without date range" do+ let!(:phase1) { create_phase(definition: definitions[1], active: true, start_date: nil, finish_date: nil) }++ it "reschedules following phases using dates of the closest preceding phase with a start date" do+ service.call(active: false)++ expect(phase0.reload).to have_attributes(active: true, start_date: nil, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: false, start_date: nil, finish_date: nil)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end- it "reschedules following phases using dates of the closest preceding phase with date range" do- service.call(active: false)+ context "with a start date only" do+ let!(:phase1) { create_phase(definition: definitions[1], active: true, start_date: date - 3, finish_date: nil) }- expect(phase0.reload).to have_attributes(active: true, start_date: date - 1, finish_date: date - 1)- expect(phase1.reload).to have_attributes(active: false, start_date: nil, finish_date: nil)- expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ it "reschedules following phases using dates of the closest preceding phase with a start date" do+ service.call(active: false)++ expect(phase0.reload).to have_attributes(active: true, start_date: nil, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: false, start_date: date - 3, finish_date: nil)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ end+ end++ context "with a finish date only" do+ let!(:phase1) { create_phase(definition: definitions[1], active: true, start_date: nil, finish_date: date - 3) }++ it "reschedules following phases using the dates of the closest preceding phase with a start date" do+ service.call(active: false)++ expect(phase0.reload).to have_attributes(active: true, start_date: nil, finish_date: date - 1)+ expect(phase1.reload).to have_attributes(active: false, start_date: nil, finish_date: date - 3)+ expect(phase2.reload).to have_attributes(active: true, start_date: date, finish_date: date + 2)+ endendendenddiff --git a/spec/services/project_phases/reschedule_service_spec.rb b/spec/services/project_phases/reschedule_service_spec.rbindex 888e7ced0f3..4ee4cd23bd1 100644--- a/spec/services/project_phases/reschedule_service_spec.rb+++ b/spec/services/project_phases/reschedule_service_spec.rb@@ -153,15 +153,34 @@ RSpec.describe ProjectPhases::RescheduleService, type: :model do]end- it "reschedules only the ones with a start_date present" do- expect(service.call(phases:, from:)).to be_success-- expect(phases[0]).to have_attributes(start_date: nil, finish_date: date, duration: 5)- expect(phases[1]).to have_attributes(start_date: from, finish_date: nil, duration: 5)- expect(phases[2]).to have_attributes(start_date: from, finish_date: nil, duration: 5)- expect(phases[3]).to have_attributes(start_date: nil, finish_date: nil, duration: 5)- expect(phases[4]).to have_attributes(start_date: from, finish_date: from + 16, duration: 13)- expect(phases[5]).to have_attributes(start_date: from + 19, finish_date: nil, duration: 2)+ subject do+ service_response = service.call(phases:, from:)+ phases.each { |p| p.duration = p.calculate_duration }+ service_response+ end++ it "reschedules only the ones with a start_date or finish_date present" do+ expect(subject).to be_success+ expect(phases[0]).to have_attributes(start_date: from, finish_date: from, duration: 1)+ expect(phases[1]).to have_attributes(start_date: from + 1, finish_date: nil, duration: nil)+ expect(phases[2]).to have_attributes(start_date: from + 1, finish_date: nil, duration: nil)+ expect(phases[3]).to have_attributes(start_date: from + 1, finish_date: nil, duration: nil)+ expect(phases[4]).to have_attributes(start_date: from + 1, finish_date: from + 19, duration: 13)+ expect(phases[5]).to have_attributes(start_date: from + 20, finish_date: nil, duration: nil)+ end++ context "when the from date is earlier than the phases dates" do+ let(:from) { Date.new(2025, 3, 31) }++ it "reschedules only the ones with a start_date or finish_date present" do+ expect(subject).to be_success+ expect(phases[0]).to have_attributes(start_date: from, finish_date: date, duration: 2)+ expect(phases[1]).to have_attributes(start_date: date + 1, finish_date: nil, duration: nil)+ expect(phases[2]).to have_attributes(start_date: date + 1, finish_date: nil, duration: nil)+ expect(phases[3]).to have_attributes(start_date: date + 1, finish_date: nil, duration: nil)+ expect(phases[4]).to have_attributes(start_date: date + 1, finish_date: date + 17, duration: 13)+ expect(phases[5]).to have_attributes(start_date: date + 20, finish_date: nil, duration: nil)+ endendenddiff --git a/spec/services/project_phases/update_service_spec.rb b/spec/services/project_phases/update_service_spec.rbindex bc2df80e644..6c93a1653d1 100644--- a/spec/services/project_phases/update_service_spec.rb+++ b/spec/services/project_phases/update_service_spec.rb@@ -106,6 +106,17 @@ RSpec.describe ProjectPhases::UpdateService, type: :model doallow(Project).to receive(:allowed_to).with(user, :view_project_phases).and_return(Project.all)end+ context "for no dates range" do+ let!(:phase) { create(:project_phase, project:, start_date: date, finish_date: date) }+ let!(:following) { create(:project_phase, project:, start_date: date - 10, finish_date: date - 10, duration: 3) }++ it "doesn't get rescheduled" do+ expect do+ expect(service.call(start_date: nil, finish_date: nil)).to be_success+ end.not_to change(following, :attributes)+ end+ end+context "for invalid date range" dolet!(:phase) { create(:project_phase, project:, start_date: date, finish_date: date) }let!(:following) { create(:project_phase, project:, start_date: date - 10, finish_date: date - 10, duration: 3) }@@ -141,6 +152,26 @@ RSpec.describe ProjectPhases::UpdateService, type: :model doexpect(following2).to have_attributes(start_date: date + 7, finish_date: date + 8, duration: 2)expect(following3).to have_attributes(start_date: date + 9, finish_date: date + 9, duration: 1)end++ context "when providing a start date only" do+ it "reschedules all of them relying on duration" do+ expect(service.call(start_date: date, finish_date: nil)).to be_success++ expect(following1).to have_attributes(start_date: date + 1, finish_date: date + 5, duration: 3)+ expect(following2).to have_attributes(start_date: date + 6, finish_date: date + 7, duration: 2)+ expect(following3).to have_attributes(start_date: date + 8, finish_date: date + 8, duration: 1)+ end+ end++ context "for a finish date only" do+ it "reschedules all of them relying on duration" do+ expect(service.call(start_date: nil, finish_date: date + 1)).to be_success++ expect(following1).to have_attributes(start_date: date + 2, finish_date: date + 6, duration: 3)+ expect(following2).to have_attributes(start_date: date + 7, finish_date: date + 8, duration: 2)+ expect(following3).to have_attributes(start_date: date + 9, finish_date: date + 9, duration: 1)+ end+ endendcontext "for following phase without duration" do@@ -158,10 +189,9 @@ RSpec.describe ProjectPhases::UpdateService, type: :model dolet!(:phase) { create(:project_phase, project:, start_date: date, finish_date: date) }let!(:following) { create(:project_phase, project:, start_date: nil, finish_date: nil, duration: 3) }- it "doesn't get rescheduled" do- expect do- expect(service.call(start_date: date, finish_date: date + 1)).to be_success- end.not_to change(following, :attributes)+ it "reschedules setting the start date to follow the previous phase" do+ expect(service.call(start_date: date, finish_date: date + 1)).to be_success+ expect(following).to have_attributes(start_date: date + 2, finish_date: nil, duration: 3)endend