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

Move and fix specs related to life cycle attributes and update service.

Dombi Attila59e12b606df8
diff --git a/app/services/project_life_cycle_steps/set_attributes_service.rb b/app/services/project_life_cycle_steps/set_attributes_service.rb
index f3ab2cb56ba..f6292c95858 100644
--- a/app/services/project_life_cycle_steps/set_attributes_service.rb
+++ b/app/services/project_life_cycle_steps/set_attributes_service.rb
@@ -37,7 +37,7 @@ module ProjectLifeCycleSteps
private
def ensure_default_attributes(_params)
- model.start_date = model.default_start_date if model.default_start_date.present?
+ model.start_date ||= model.default_start_date if model.default_start_date.present?
end
def set_calculated_duration
diff --git a/spec/models/project/phase_spec.rb b/spec/models/project/phase_spec.rb
index 324e6813c30..c380dfe1d4e 100644
--- a/spec/models/project/phase_spec.rb
+++ b/spec/models/project/phase_spec.rb
@@ -93,36 +93,17 @@ RSpec.describe Project::Phase do
let(:date) { Time.zone.today }
- describe "#set_calculated_duration" do
- it "sets duration to the number of working days in complete date range" do
- subject.duration = 0
- subject.start_date = date
- subject.finish_date = date + 27
+ it "returns number of working days in complete date range" do
+ subject.start_date = date
+ subject.finish_date = date + 27
- expect { subject.set_calculated_duration }.to change(subject, :duration).from(0).to(20)
- end
-
- it "sets duration to nil if date range is incomplete" do
- subject.duration = 0
- subject.start_date = nil
-
- expect { subject.set_calculated_duration }.to change(subject, :duration).from(0).to(nil)
- end
+ expect(subject.calculate_duration).to eq(20)
end
- describe "#calculate_duration" do
- it "returns number of working days in complete date range" do
- subject.start_date = date
- subject.finish_date = date + 27
-
- expect(subject.calculate_duration).to eq(20)
- end
-
- it "returns nil if date range is incomplete" do
- subject.start_date = nil
+ it "returns nil if date range is incomplete" do
+ subject.start_date = nil
- expect(subject.calculate_duration).to be_nil
- end
+ expect(subject.calculate_duration).to be_nil
end
end
end
diff --git a/spec/services/project_life_cycle_steps/set_attributes_service_spec.rb b/spec/services/project_life_cycle_steps/set_attributes_service_spec.rb
new file mode 100644
index 00000000000..09b464004f6
--- /dev/null
+++ b/spec/services/project_life_cycle_steps/set_attributes_service_spec.rb
@@ -0,0 +1,141 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+
+RSpec.describe ProjectLifeCycleSteps::SetAttributesService, type: :model do
+ let(:user) { build_stubbed(:user) }
+ let(:contract_class) do
+ contract = class_double(ProjectLifeCycleSteps::UpdateContract)
+
+ allow(contract)
+ .to receive(:new)
+ .with(phase, user, options: {})
+ .and_return(contract_instance)
+
+ contract
+ end
+ let(:contract_instance) do
+ instance_double(Projects::UpdateContract, validate: contract_valid, errors: contract_errors)
+ end
+ let(:contract_valid) { true }
+ let(:contract_errors) do
+ instance_double(ActiveModel::Errors)
+ end
+ let(:project_valid) { true }
+ let(:instance) do
+ described_class.new(user:,
+ model: phase,
+ contract_class:)
+ end
+ let(:call_attributes) { {} }
+
+ let(:date) { Time.zone.today }
+ let(:project) { build_stubbed(:project) }
+ let(:phase) { build_stubbed(:project_phase, project:, duration: 0) }
+
+ describe "call" do
+ let(:call_attributes) do
+ {}
+ end
+
+ before do
+ allow(project)
+ .to receive(:valid?)
+ .and_return(project_valid)
+
+ allow(contract_instance)
+ .to receive(:validate)
+ .and_return(contract_valid)
+ end
+
+ subject { instance.call(call_attributes) }
+
+ describe "setting calculated duration" do
+ shared_let(:week_days) { week_with_saturday_and_sunday_as_weekend }
+
+ context "when the date range is complete" do
+ let(:call_attributes) { { start_date: date, finish_date: date + 27 } }
+
+ it "sets duration to the number of working days" do
+ expect { subject }.to change(phase, :duration).from(0).to(20)
+ end
+ end
+
+ context "when the date range is incomplete" do
+ let(:call_attributes) { { start_date: nil } }
+
+ it "sets duration to nil" do
+ expect { subject }.to change(phase, :duration).from(0).to(nil)
+ end
+ end
+ end
+
+ describe "setting default start date" do
+ let(:phase) do
+ build_stubbed(:project_phase, project:, start_date: previous_date)
+ end
+
+ context "when there is a default_start_date on the phase" do
+ before do
+ allow(phase).to receive(:default_start_date).and_return(date)
+ end
+
+ context "and the previous_date is not set" do
+ let(:previous_date) { nil }
+
+ it "sets the model's start_date to the default_start_date" do
+ expect { subject }.to change(phase, :start_date).from(nil).to(date)
+ end
+ end
+
+ context "and the previous_date is set" do
+ let(:previous_date) { Time.zone.yesterday }
+
+ it "does not sets the model's start_date to the default_start_date" do
+ expect { subject }.not_to change(phase, :start_date)
+ end
+ end
+ end
+
+ context "when there is no default_start_date on the phase" do
+ let(:previous_date) { date }
+
+ before do
+ allow(phase).to receive(:default_start_date).and_return(nil)
+ end
+
+ it "does not set the model's start_date" do
+ expect { subject }.not_to change(phase, :start_date).from(date)
+ end
+ end
+ end
+ end
+end
diff --git a/spec/services/project_life_cycle_steps/update_service_spec.rb b/spec/services/project_life_cycle_steps/update_service_spec.rb
index 04dbfb7ceca..8495679970d 100644
--- a/spec/services/project_life_cycle_steps/update_service_spec.rb
+++ b/spec/services/project_life_cycle_steps/update_service_spec.rb
@@ -63,7 +63,7 @@ RSpec.describe ProjectLifeCycleSteps::UpdateService, type: :model do
end
describe "updating duration through SetAttributesService" do
- shared_let(:phase) { create(:project_phase, duration: 0) }
+ shared_let(:phase) { create(:project_phase, duration: nil) }
let(:user) { build_stubbed(:user) }
let(:project) { phase.project }
@@ -74,20 +74,16 @@ RSpec.describe ProjectLifeCycleSteps::UpdateService, type: :model do
mock_permissions_for(user) do |mock|
mock.allow_in_project(:edit_project_phases, project:)
end
-
- allow(phase).to receive(:set_calculated_duration)
end
it "sets duration for valid model" do
expect(service.call(start_date: date - 1, finish_date: date + 1)).to be_success
-
- expect(phase).to have_received(:set_calculated_duration)
+ expect(phase.saved_change_to_duration).to eq([nil, 3]) # Changes are saved
end
it "sets duration for invalid model" do
expect(service.call(start_date: date + 1, finish_date: date - 1)).to be_failure
-
- expect(phase).to have_received(:set_calculated_duration)
+ expect(phase.duration_change).to eq([nil, 0]) # Changes are not saved
end
end