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
update duration in service
ulfertsd307e96bbfd5
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rbindex 471d5116ce3..77afb91e0b6 100644--- a/app/services/work_packages/set_attributes_service.rb+++ b/app/services/work_packages/set_attributes_service.rb@@ -44,6 +44,7 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributesmodel.change_by_system doupdate_dates+ update_durationreassign_invalid_status_if_type_changedset_templated_descriptionend@@ -140,13 +141,6 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributesinitialize_unset_custom_valuesend- def unify_dates- return unless work_package_now_milestone?-- unified_date = work_package.due_date || work_package.start_date- work_package.start_date = work_package.due_date = unified_date- end-def custom_field_context_changed?work_package.type_id_changed? || work_package.project_id_changed?end@@ -167,7 +161,7 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributesenddef update_dates- unify_dates+ unify_dates if work_package_now_milestone?min_start = new_start_date@@ -177,9 +171,25 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributeswork_package.start_date = min_startend+ def unify_dates+ unified_date = work_package.due_date || work_package.start_date+ work_package.start_date = work_package.due_date = unified_date+ end++ def update_duration+ return unless date_changed_but_not_duration?++ work_package.duration = if work_package.start_date && work_package.due_date+ work_package.due_date - work_package.start_date + 1+ else+ 1+ end+ end+def set_version_to_nilif work_package.version &&- !work_package.project&.shared_versions.include?(work_package.version)+ work_package.project &&+ work_package.project.shared_versions.exclude?(work_package.version)work_package.version = nilendend@@ -291,4 +301,8 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes(due && !start) || ((due && start) && (due > start))end++ def date_changed_but_not_duration?+ (work_package.start_date_changed? || work_package.due_date_changed?) && !work_package.duration_changed?+ endenddiff --git a/spec/factories/work_package_factory.rb b/spec/factories/work_package_factory.rbindex 59f7e0d2032..ebeaca799e6 100644--- a/spec/factories/work_package_factory.rb+++ b/spec/factories/work_package_factory.rb@@ -27,61 +27,65 @@#++FactoryBot.define do- duration_calculation = -> do- if start_date && due_date- due_date - start_date + 1- else- 1- end- end-- factory :work_package do+ factory :abstract_work_package_factory, class: 'WorkPackage' dotransient docustom_values { nil }+ abstract_factory { raise "You can't instantiate this abstract factory." }endpriority- project factory: :project_with_types- status factory: :status+ statussequence(:subject) { |n| "WorkPackage No. #{n}" }description { |i| "Description for '#{i.subject}'" }author factory: :user- created_at { Time.zone.now }- updated_at { Time.zone.now }- duration &duration_calculation+ duration do+ if start_date && due_date+ due_date - start_date + 1+ else+ 1+ end+ end++ after(:build) do |_, evaluator|+ evaluator.abstract_factory+ end++ after(:stub) do |_, evaluator|+ evaluator.abstract_factory+ end++ factory :work_package do+ transient do+ abstract_factory { false }+ end- callback(:after_build) do |work_package, evaluator|- work_package.type = work_package.project.types.first unless work_package.type+ project factory: :project_with_types- custom_values = evaluator.custom_values || {}+ callback(:after_build) do |work_package, evaluator|+ work_package.type = work_package.project.types.first unless work_package.type- if custom_values.is_a? Hash- custom_values.each_pair do |custom_field_id, value|- work_package.custom_values.build custom_field_id: custom_field_id, value: value+ custom_values = evaluator.custom_values || {}++ if custom_values.is_a? Hash+ custom_values.each_pair do |custom_field_id, value|+ work_package.custom_values.build custom_field_id: custom_field_id, value: value+ end+ else+ custom_values.each { |cv| work_package.custom_values << cv }end- else- custom_values.each { |cv| work_package.custom_values << cv }endend- end- factory :stubbed_work_package, class: 'WorkPackage' do- transient do- custom_values { nil }- end+ factory :stubbed_work_package, parent: :abstract_work_package_factory do+ transient do+ abstract_factory { false }+ end- priority- project { build_stubbed(:project_with_types) }- status- sequence(:subject) { |n| "WorkPackage No. #{n}" }- description { |i| "Description for '#{i.subject}'" }- author factory: :user- created_at { Time.zone.now }- updated_at { Time.zone.now }- duration &duration_calculation+ project { build_stubbed(:project_with_types) }- callback(:after_stub) do |wp, arguments|- wp.type = wp.project.types.first unless wp.type_id || arguments.instance_variable_get(:@overrides).has_key?(:type)+ callback(:after_stub) do |wp, arguments|+ wp.type = wp.project.types.first unless wp.type_id || arguments.instance_variable_get(:@overrides).has_key?(:type)+ endendendenddiff --git a/spec/features/projects/template_spec.rb b/spec/features/projects/template_spec.rbindex 5bbc1b427ef..e3de2c3e882 100644--- a/spec/features/projects/template_spec.rb+++ b/spec/features/projects/template_spec.rb@@ -146,7 +146,7 @@ describe 'Project templates', type: :feature, js: true dowp_source = template.work_packages.first.attributes.except(*%w[id author_id project_id updated_at created_at])wp_target = project.work_packages.first.attributes.except(*%w[id author_id project_id updated_at created_at])- expect(wp_source).to eq(wp_target)+ expect(wp_target).to eq(wp_source)wiki_source = template.wiki.pages.firstwiki_target = project.wiki.pages.firstdiff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rbindex 5019c1651eb..9dbad9b1dcd 100644--- a/spec/services/work_packages/set_attributes_service_spec.rb+++ b/spec/services/work_packages/set_attributes_service_spec.rb@@ -69,57 +69,122 @@ describe WorkPackages::SetAttributesService, type: :model docontract_class: mock_contract)end- describe 'call' do- shared_examples_for 'service call' do- subject { instance.call(call_attributes) }+ shared_examples_for 'service call' do+ subject { instance.call(call_attributes) }- it 'is successful' do- expect(subject.success?).to be_truthy- end+ it 'is successful' do+ expect(subject).to be_success+ end- it 'sets the value' do- subject+ it 'sets the value' do+ subject- attributes.each do |attribute, key|- expect(work_package.send(attribute)).to eql key- end+ attributes.each do |attribute, key|+ expect(work_package.send(attribute)).to eql keyend+ end- it 'does not persist the work_package' do- expect(work_package)- .not_to receive(:save)+ it 'does not persist the work_package' do+ expect(work_package)+ .not_to receive(:save)++ subject+ end++ it 'has no errors' do+ expect(subject.errors).to be_empty+ end++ context 'when the contract does not validate' do+ let(:contract_valid) { false }++ it 'is unsuccessful' do+ expect(subject).not_to be_success+ end+ it 'does not persist the changes' dosubject++ expect(work_package).not_to receive(:save)end- it 'has no errors' do- expect(subject.errors).to be_empty+ it "exposes the contract's errors" do+ subject++ expect(subject.errors).to eql mock_contract_instance.errorsend+ end+ end- context 'when the contract does not validate' do- let(:contract_valid) { false }+ context 'when updating subject before calling the service' do+ let(:call_attributes) { {} }+ let(:attributes) { { subject: 'blubs blubs' } }- it 'is unsuccessful' do- expect(subject.success?).to be_falsey- end+ before do+ work_package.attributes = attributes+ end++ it_behaves_like 'service call'+ end++ context 'when updating subject via attributes' do+ let(:call_attributes) { attributes }+ let(:attributes) { { subject: 'blubs blubs' } }++ it_behaves_like 'service call'+ end- it 'does not persist the changes' do+ context 'for status' do+ let(:default_status) { build_stubbed(:default_status) }+ let(:other_status) { build_stubbed(:status) }+ let(:new_statuses) { [other_status, default_status] }++ before do+ allow(Status)+ .to receive(:default)+ .and_return(default_status)+ end++ context 'with no value set before for a new work package' do+ let(:call_attributes) { {} }+ let(:attributes) { {} }+ let(:work_package) { new_work_package }++ before do+ work_package.status = nil+ end++ it_behaves_like 'service call' do+ it 'sets the default status' dosubject- expect(work_package).to_not receive(:save)+ expect(work_package.status)+ .to eql default_statusend+ end+ end++ context 'with no value set on existing work package' do+ let(:call_attributes) { {} }+ let(:attributes) { {} }++ before do+ work_package.status = nil+ end- it "exposes the contract's errors" do+ it_behaves_like 'service call' do+ it 'stays nil' dosubject- expect(subject.errors).to eql mock_contract_instance.errors+ expect(work_package.status)+ .to be_nilendendend- context 'update subject before calling the service' do+ context 'when updating status before calling the service' dolet(:call_attributes) { {} }- let(:attributes) { { subject: 'blubs blubs' } }+ let(:attributes) { { status: other_status } }before dowork_package.attributes = attributes@@ -128,839 +193,1036 @@ describe WorkPackages::SetAttributesService, type: :model doit_behaves_like 'service call'end- context 'updating subject via attributes' do+ context 'when updating status via attributes' dolet(:call_attributes) { attributes }- let(:attributes) { { subject: 'blubs blubs' } }+ let(:attributes) { { status: other_status } }it_behaves_like 'service call'end+ end- context 'status' do- let(:default_status) { build_stubbed(:default_status) }- let(:other_status) { build_stubbed(:status) }- let(:new_statuses) { [other_status, default_status] }+ context 'for author' do+ let(:other_user) { build_stubbed(:user) }- before do- allow(Status)- .to receive(:default)- .and_return(default_status)- end+ context 'with no value set before for a new work package' do+ let(:call_attributes) { {} }+ let(:attributes) { {} }+ let(:work_package) { new_work_package }- context 'no value set before for a new work package' do- let(:call_attributes) { {} }- let(:attributes) { {} }- let(:work_package) { new_work_package }+ it_behaves_like 'service call' do+ it "sets the service's author" do+ subject- before do- work_package.status = nil+ expect(work_package.author)+ .to eql userend- it_behaves_like 'service call' do- it 'sets the default status' do- subject+ it 'notes the author to be system changed' do+ subject- expect(work_package.status)- .to eql default_status- end+ expect(work_package.changed_by_system['author_id'])+ .to eql [0, user.id]endend+ end- context 'no value set on existing work package' do- let(:call_attributes) { {} }- let(:attributes) { {} }+ context 'with no value set on existing work package' do+ let(:call_attributes) { {} }+ let(:attributes) { {} }- before do- work_package.status = nil- end+ before do+ work_package.author = nil+ end- it_behaves_like 'service call' do- it 'stays nil' do- subject+ it_behaves_like 'service call' do+ it 'stays nil' do+ subject- expect(work_package.status)- .to be_nil- end+ expect(work_package.author)+ .to be_nilendend+ end- context 'update status before calling the service' do- let(:call_attributes) { {} }- let(:attributes) { { status: other_status } }-- before do- work_package.attributes = attributes- end+ context 'when updating author before calling the service' do+ let(:call_attributes) { {} }+ let(:attributes) { { author: other_user } }- it_behaves_like 'service call'+ before do+ work_package.attributes = attributesend- context 'updating status via attributes' do- let(:call_attributes) { attributes }- let(:attributes) { { status: other_status } }+ it_behaves_like 'service call'+ end++ context 'updating author via attributes' do+ let(:call_attributes) { attributes }+ let(:attributes) { { author: other_user } }- it_behaves_like 'service call'+ it_behaves_like 'service call'+ end+ end++ context 'with the actual contract' do+ let(:invalid_wp) do+ wp = create(:work_package)+ wp.start_date = Time.zone.today + 5.days+ wp.due_date = Time.zone.today+ wp.save!(validate: false)++ wp+ end+ let(:user) { build_stubbed(:admin) }+ let(:instance) do+ described_class.new(user: user,+ model: invalid_wp,+ contract_class: contract_class)+ end++ context 'with a current invalid start date' do+ let(:call_attributes) { attributes }+ let(:attributes) { { start_date: Time.zone.today - 5.days } }+ let(:contract_valid) { true }++ subject { instance.call(call_attributes) }++ it 'is successful' do+ expect(subject).to be_success+ expect(subject.errors).to be_emptyendend+ end- context 'author' do- let(:other_user) { build_stubbed(:user) }+ context 'for start_date & due_date & duration' do+ context 'with a parent' do+ let(:attributes) { {} }+ let(:work_package) { new_work_package }+ let(:parent) do+ build_stubbed(:stubbed_work_package,+ start_date: parent_start_date,+ due_date: parent_due_date)+ end+ let(:parent_start_date) { Time.zone.today - 5.days }+ let(:parent_due_date) { Time.zone.today + 10.days }- context 'no value set before for a new work package' do- let(:call_attributes) { {} }- let(:attributes) { {} }- let(:work_package) { new_work_package }+ context 'with the parent having dates and not providing own dates' do+ let(:call_attributes) { { parent: parent } }it_behaves_like 'service call' do- it "sets the service's author" do+ it "sets the start_date to the parent`s start_date" dosubject- expect(work_package.author)- .to eql user+ expect(work_package.start_date)+ .to eql parent_start_dateend- it 'notes the author to be system changed' do+ it "sets the due_date to the parent`s due_date" dosubject- expect(work_package.changed_by_system['author_id'])- .to eql [0, user.id]+ expect(work_package.due_date)+ .to eql parent_due_dateendendend- context 'no value set on existing work package' do- let(:call_attributes) { {} }- let(:attributes) { {} }-- before do- work_package.author = nil- end+ context 'with the parent having start date (no due) and not providing own dates' do+ let(:call_attributes) { { parent: parent } }+ let(:parent_due_date) { nil }it_behaves_like 'service call' do- it 'stays nil' do+ it "sets the start_date to the parent`s start_date" do+ subject++ expect(work_package.start_date)+ .to eql parent_start_date+ end++ it "sets the due_date to nil" dosubject- expect(work_package.author)+ expect(work_package.due_date).to be_nilendendend- context 'update author before calling the service' do- let(:call_attributes) { {} }- let(:attributes) { { author: other_user } }+ context 'with the parent having due date (no start) and not providing own dates' do+ let(:call_attributes) { { parent: parent } }+ let(:parent_start_date) { nil }- before do- work_package.attributes = attributes- end+ it_behaves_like 'service call' do+ it "sets the start_date to nil" do+ subject- it_behaves_like 'service call'- end+ expect(work_package.start_date)+ .to be_nil+ end- context 'updating author via attributes' do- let(:call_attributes) { attributes }- let(:attributes) { { author: other_user } }+ it "sets the due_date to the parent`s due_date" do+ subject- it_behaves_like 'service call'+ expect(work_package.due_date)+ .to eql parent_due_date+ end+ endend- end- context 'with the actual contract' do- let(:invalid_wp) do- wp = create(:work_package)- wp.start_date = Date.today + 5.days- wp.due_date = Date.today- wp.save!(validate: false)+ context 'with the parent having dates but providing own dates' do+ let(:call_attributes) { { parent: parent, start_date: Time.zone.today, due_date: Time.zone.today + 1.day } }- wp- end- let(:user) { build_stubbed(:admin) }- let(:instance) do- described_class.new(user: user,- model: invalid_wp,- contract_class: contract_class)- end+ it_behaves_like 'service call' do+ it "sets the start_date to the provided date" do+ subject- context 'with a current invalid start date' do- let(:call_attributes) { attributes }- let(:attributes) { { start_date: Date.today - 5.days } }- let(:contract_valid) { true }- subject { instance.call(call_attributes) }+ expect(work_package.start_date)+ .to eql Time.zone.today+ end- it 'is successful' do- expect(subject.success?).to be_truthy- expect(subject.errors).to be_empty- end- end- end+ it "sets the due_date to the provided date" do+ subject- context 'start_date & due_date' do- let(:parent) do- build_stubbed(:stubbed_work_package,- start_date: parent_start_date,- due_date: parent_due_date)+ expect(work_package.due_date)+ .to eql Time.zone.today + 1.day+ end+ endend- let(:parent_start_date) { Date.today - 5.days }- let(:parent_due_date) { Date.today + 10.days }-- context 'with a parent' do- let(:attributes) { {} }- let(:work_package) { new_work_package }- context 'with the parent having dates and not providing own dates' do- let(:call_attributes) { { parent: parent } }+ context 'with the parent having dates but providing own start_date' do+ let(:call_attributes) { { parent: parent, start_date: Time.zone.today } }- it_behaves_like 'service call' do- it "sets the start_date to the parent`s start_date" do- subject+ it_behaves_like 'service call' do+ it "sets the start_date to the provided date" do+ subject- expect(work_package.start_date)- .to eql parent_start_date- end+ expect(work_package.start_date)+ .to eql Time.zone.today+ end- it "sets the due_date to the parent`s due_date" do- subject+ it "sets the due_date to the parent's due_date" do+ subject- expect(work_package.due_date)- .to eql parent_due_date- end+ expect(work_package.due_date)+ .to eql parent_due_dateendend+ end- context 'with the parent having start date (no due) and not providing own dates' do- let(:call_attributes) { { parent: parent } }- let(:parent_due_date) { nil }+ context 'with the parent having dates but providing own due_date' do+ let(:call_attributes) { { parent: parent, due_date: Time.zone.today + 4.days } }- it_behaves_like 'service call' do- it "sets the start_date to the parent`s start_date" do- subject+ it_behaves_like 'service call' do+ it "sets the start_date to the parent's start date" do+ subject- expect(work_package.start_date)- .to eql parent_start_date- end+ expect(work_package.start_date)+ .to eql parent_start_date+ end- it "sets the due_date to nil" do- subject+ it "sets the due_date to the provided date" do+ subject- expect(work_package.due_date)- .to be_nil- end+ expect(work_package.due_date)+ .to eql Time.zone.today + 4.daysendend+ end- context 'with the parent having due date (no start) and not providing own dates' do- let(:call_attributes) { { parent: parent } }- let(:parent_start_date) { nil }+ context 'with the parent having dates but providing own empty start_date' do+ let(:call_attributes) { { parent: parent, start_date: nil } }- it_behaves_like 'service call' do- it "sets the start_date to nil" do- subject+ it_behaves_like 'service call' do+ it "sets the start_date to nil" do+ subject- expect(work_package.start_date)- .to be_nil- end+ expect(work_package.start_date)+ .to be_nil+ end- it "sets the due_date to the parent`s due_date" do- subject+ it "sets the due_date to the parent's due_date" do+ subject- expect(work_package.due_date)- .to eql parent_due_date- end+ expect(work_package.due_date)+ .to eql parent_due_dateendend+ end- context 'with the parent having dates but providing own dates' do- let(:call_attributes) { { parent: parent, start_date: Date.today, due_date: Date.today + 1.day } }+ context 'with the parent having dates but providing own empty due_date' do+ let(:call_attributes) { { parent: parent, due_date: nil } }- it_behaves_like 'service call' do- it "sets the start_date to the provided date" do- subject+ it_behaves_like 'service call' do+ it "sets the start_date to the parent's start date" do+ subject- expect(work_package.start_date)- .to eql Date.today- end+ expect(work_package.start_date)+ .to eql parent_start_date+ end- it "sets the due_date to the provided date" do- subject+ it "sets the due_date to nil" do+ subject- expect(work_package.due_date)- .to eql Date.today + 1.day- end+ expect(work_package.due_date)+ .to be_nilendend+ end- context 'with the parent having dates but providing own start_date' do- let(:call_attributes) { { parent: parent, start_date: Date.today } }+ context 'with the parent having dates but providing a start date that is before parent`s due date`' do+ let(:call_attributes) { { parent: parent, start_date: parent_due_date - 4.days } }- it_behaves_like 'service call' do- it "sets the start_date to the provided date" do- subject+ it_behaves_like 'service call' do+ it "sets the start_date to the provided date" do+ subject- expect(work_package.start_date)- .to eql Date.today- end+ expect(work_package.start_date)+ .to eql parent_due_date - 4.days+ end- it "sets the due_date to the parent's due_date" do- subject+ it "sets the due_date to the parent's due_date" do+ subject- expect(work_package.due_date)- .to eql parent_due_date- end+ expect(work_package.due_date)+ .to eql parent_due_dateendend+ end- context 'with the parent having dates but providing own due_date' do- let(:call_attributes) { { parent: parent, due_date: Date.today + 4.days } }+ context 'with the parent having dates but providing a start date that is after the parent`s due date`' do+ let(:call_attributes) { { parent: parent, start_date: parent_due_date + 1.day } }- it_behaves_like 'service call' do- it "sets the start_date to the parent's start date" do- subject+ it_behaves_like 'service call' do+ it "sets the start_date to the provided date" do+ subject- expect(work_package.start_date)- .to eql parent_start_date- end+ expect(work_package.start_date)+ .to eql parent_due_date + 1.day+ end- it "sets the due_date to the provided date" do- subject+ it "leaves the due date empty" do+ subject- expect(work_package.due_date)- .to eql Date.today + 4.days- end+ expect(work_package.due_date)+ .to be_nilendend+ end- context 'with the parent having dates but providing own empty start_date' do- let(:call_attributes) { { parent: parent, start_date: nil } }+ context 'with the parent having dates but providing a due date that is before the parent`s start date`' do+ let(:call_attributes) { { parent: parent, due_date: parent_start_date - 3.days } }- it_behaves_like 'service call' do- it "sets the start_date to nil" do- subject+ it_behaves_like 'service call' do+ it "leaves the start date empty" do+ subject- expect(work_package.start_date)- .to be_nil- end+ expect(work_package.start_date)+ .to be_nil+ end- it "sets the due_date to the parent's due_date" do- subject+ it "set the due date to the provided date" do+ subject- expect(work_package.due_date)- .to eql parent_due_date- end+ expect(work_package.due_date)+ .to eql parent_start_date - 3.daysendend+ end+ end- context 'with the parent having dates but providing own empty due_date' do- let(:call_attributes) { { parent: parent, due_date: nil } }+ context 'with no value set for a new work package and with default setting active',+ with_settings: { work_package_startdate_is_adddate: true } do+ let(:call_attributes) { {} }+ let(:attributes) { {} }+ let(:work_package) { new_work_package }- it_behaves_like 'service call' do- it "sets the start_date to the parent's start date" do- subject+ it_behaves_like 'service call' do+ it "sets the start date to today" do+ subject- expect(work_package.start_date)- .to eql parent_start_date- end+ expect(work_package.start_date)+ .to eql Time.zone.today+ end- it "sets the due_date to nil" do- subject+ it "sets the duration to 1" do+ subject- expect(work_package.due_date)- .to be_nil- end- end+ expect(work_package.duration)+ .to eq 1end+ end+ end- context 'with the parent having dates but providing a start date that is before parent`s due date`' do- let(:call_attributes) { { parent: parent, start_date: parent_due_date - 4.days } }+ context 'with a value set for a new work package and with default setting active',+ with_settings: { work_package_startdate_is_adddate: true } do+ let(:call_attributes) { { start_date: Time.zone.today + 1.day } }+ let(:attributes) { {} }+ let(:work_package) { new_work_package }- it_behaves_like 'service call' do- it "sets the start_date to the provided date" do- subject+ it_behaves_like 'service call' do+ it 'stays that value' do+ subject- expect(work_package.start_date)- .to eql parent_due_date - 4.days- end+ expect(work_package.start_date)+ .to eq(Time.zone.today + 1.day)+ end- it "sets the due_date to the parent's due_date" do- subject+ it "sets the duration to 1" do+ subject- expect(work_package.due_date)- .to eql parent_due_date- end- end+ expect(work_package.duration)+ .to eq 1end+ end+ end- context 'with the parent having dates but providing a start date that is after the parent`s due date`' do- let(:call_attributes) { { parent: parent, start_date: parent_due_date + 1.day } }+ context 'with date values set to the same date on a new work package' do+ let(:call_attributes) { { start_date: Time.zone.today, due_date: Time.zone.today } }+ let(:attributes) { {} }+ let(:work_package) { new_work_package }- it_behaves_like 'service call' do- it "sets the start_date to the provided date" do- subject+ it_behaves_like 'service call' do+ it 'sets the start date value' do+ subject- expect(work_package.start_date)- .to eql parent_due_date + 1.day- end+ expect(work_package.start_date)+ .to eq(Time.zone.today)+ end- it "leaves the due date empty" do- subject+ it 'sets the due date value' do+ subject- expect(work_package.due_date)- .to be_nil- end- end+ expect(work_package.due_date)+ .to eq(Time.zone.today)+ end++ it "sets the duration to 1" do+ subject++ expect(work_package.duration)+ .to eq 1end+ end+ end- context 'with the parent having dates but providing a due date that is before the parent`s start date`' do- let(:call_attributes) { { parent: parent, due_date: parent_start_date - 3.day } }+ context 'with date values set on a new work package' do+ let(:call_attributes) { { start_date: Time.zone.today, due_date: Time.zone.today + 5.days } }+ let(:attributes) { {} }+ let(:work_package) { new_work_package }- it_behaves_like 'service call' do- it "leaves the start date empty" do- subject+ it_behaves_like 'service call' do+ it 'sets the start date value' do+ subject- expect(work_package.start_date)- .to be_nil- end+ expect(work_package.start_date)+ .to eq(Time.zone.today)+ end- it "set the due date to the provided date" do- subject+ it 'sets the due date value' do+ subject- expect(work_package.due_date)- .to eql parent_start_date - 3.day- end- end+ expect(work_package.due_date)+ .to eq(Time.zone.today + 5.days)+ end++ it "sets the duration to 6" do+ subject++ expect(work_package.duration)+ .to eq 6endend+ end- context 'with default setting', with_settings: { work_package_startdate_is_adddate: true } do- context 'no value set before for a new work package' do- let(:call_attributes) { {} }- let(:attributes) { {} }- let(:work_package) { new_work_package }+ context 'with start date changed' do+ let(:work_package) { build_stubbed(:stubbed_work_package, start_date: Time.zone.today, due_date: Time.zone.today + 5.days) }+ let(:call_attributes) { { start_date: Time.zone.today + 1.day } }+ let(:attributes) { {} }- it_behaves_like 'service call' do- it "sets the default priority" do- subject+ it_behaves_like 'service call' do+ it 'sets the start date value' do+ subject- expect(work_package.start_date)- .to eql Date.today- end- end+ expect(work_package.start_date)+ .to eq(Time.zone.today + 1.day)end- context 'value set on new work package' do- let(:call_attributes) { { start_date: Date.today + 1.day } }- let(:attributes) { {} }- let(:work_package) { new_work_package }+ it 'keeps the due date value' do+ subject- it_behaves_like 'service call' do- it 'stays that value' do- subject+ expect(work_package.due_date)+ .to eq(Time.zone.today + 5.days)+ end- expect(work_package.start_date)- .to eq(Date.today + 1.day)- end- end+ it "updates the duration" do+ subject++ expect(work_package.duration)+ .to eq 5endendend- context 'priority' do- let(:default_priority) { build_stubbed(:priority) }- let(:other_priority) { build_stubbed(:priority) }+ context 'with due date changed' do+ let(:work_package) { build_stubbed(:stubbed_work_package, start_date: Time.zone.today, due_date: Time.zone.today + 5.days) }+ let(:call_attributes) { { due_date: Time.zone.today + 1.day } }+ let(:attributes) { {} }++ it_behaves_like 'service call' do+ it 'keeps the start date value' do+ subject- before do- allow(IssuePriority)- .to receive_message_chain(:active, :default)- .and_return(default_priority)- end+ expect(work_package.start_date)+ .to eq(Time.zone.today)+ end- context 'no value set before for a new work package' do- let(:call_attributes) { {} }- let(:attributes) { {} }- let(:work_package) { new_work_package }+ it 'sets the due date value' do+ subject- before do- work_package.priority = nil+ expect(work_package.due_date)+ .to eq(Time.zone.today + 1.day)end- it_behaves_like 'service call' do- it "sets the default priority" do- subject+ it "updates the duration" do+ subject- expect(work_package.priority)- .to eql default_priority- end+ expect(work_package.duration)+ .to eq 2endend+ end- context 'update priority before calling the service' do- let(:call_attributes) { {} }- let(:attributes) { { priority: other_priority } }+ context 'with start date nilled' do+ let(:work_package) { build_stubbed(:stubbed_work_package, start_date: Time.zone.today, due_date: Time.zone.today + 5.days) }+ let(:call_attributes) { { start_date: nil } }+ let(:attributes) { {} }- before do- work_package.attributes = attributes+ it_behaves_like 'service call' do+ it 'sets the start date to nil' do+ subject++ expect(work_package.start_date)+ .to be_nilend- it_behaves_like 'service call'- end+ it 'keeps the due date value' do+ subject- context 'updating priority via attributes' do- let(:call_attributes) { attributes }- let(:attributes) { { priority: other_priority } }+ expect(work_package.due_date)+ .to eq(Time.zone.today + 5.days)+ end- it_behaves_like 'service call'+ it "sets the duration to 1" do+ subject++ expect(work_package.duration)+ .to eq 1+ endendend- context 'when switching the type' do- let(:target_type) { build_stubbed(:type) }+ context 'with due date nilled' do+ let(:work_package) { build_stubbed(:stubbed_work_package, start_date: Time.zone.today, due_date: Time.zone.today + 5.days) }+ let(:call_attributes) { { due_date: nil } }+ let(:attributes) { {} }- context 'with a type that is no milestone' do- before do- allow(target_type)- .to receive(:is_milestone?)- .and_return(false)+ it_behaves_like 'service call' do+ it 'keeps the start date' do+ subject++ expect(work_package.start_date)+ .to eq(Time.zone.today)end- it 'sets the start date to the due date' do- work_package.due_date = Date.today+ it 'nils the due date' do+ subject- instance.call(type: target_type)+ expect(work_package.due_date)+ .to be_nil+ end++ it "sets the duration to 1" do+ subject- expect(work_package.start_date).to be_nil+ expect(work_package.duration)+ .to eq 1endend+ end- context 'with a type that is a milestone' do- before do- allow(target_type)- .to receive(:is_milestone?)- .and_return(true)+ context 'with duration explicitly set' do+ let(:work_package) { build_stubbed(:stubbed_work_package, start_date: Time.zone.today, due_date: Time.zone.today + 5.days) }+ let(:call_attributes) { { due_date: Time.zone.today + 2.days, duration: 8 } }+ let(:attributes) { {} }++ it_behaves_like 'service call' do+ it 'keeps the start date' do+ subject++ expect(work_package.start_date)+ .to eq(Time.zone.today)end- it 'sets the start date to the due date' do- date = Date.today- work_package.due_date = date+ it 'sets the due date' do+ subject- instance.call(type: target_type)+ expect(work_package.due_date)+ .to eq(Time.zone.today + 2.days)+ end++ it "sets the faulty duration (for error reporting)" do+ subject- expect(work_package.start_date).to eql date+ expect(work_package.duration)+ .to eq 8end+ end+ end+ end++ context 'for priority' do+ let(:default_priority) { build_stubbed(:priority) }+ let(:other_priority) { build_stubbed(:priority) }++ before do+ allow(IssuePriority)+ .to receive_message_chain(:active, :default)+ .and_return(default_priority)+ end- it 'set the due date to the start date if the due date is nil' do- date = Date.today- work_package.start_date = date+ context 'with no value set before for a new work package' do+ let(:call_attributes) { {} }+ let(:attributes) { {} }+ let(:work_package) { new_work_package }- instance.call(type: target_type)+ before do+ work_package.priority = nil+ end- expect(work_package.due_date).to eql date+ it_behaves_like 'service call' do+ it "sets the default priority" do+ subject++ expect(work_package.priority)+ .to eql default_priorityendendend- context 'when switching the project' do- let(:new_project) { build_stubbed(:project) }- let(:version) { build_stubbed(:version) }- let(:category) { build_stubbed(:category) }- let(:new_category) { build_stubbed(:category, name: category.name) }- let(:new_statuses) { [work_package.status] }- let(:new_versions) { [] }- let(:type) { work_package.type }- let(:new_types) { [type] }- let(:default_type) { build_stubbed(:type_standard) }- let(:other_type) { build_stubbed(:type) }- let(:yet_another_type) { build_stubbed(:type) }-+ context 'when updating priority before calling the service' dolet(:call_attributes) { {} }- let(:new_project_categories) do- categories_stub = double('categories')- allow(new_project)- .to receive(:categories)- .and_return(categories_stub)+ let(:attributes) { { priority: other_priority } }- categories_stub+ before do+ work_package.attributes = attributesend+ it_behaves_like 'service call'+ end++ context 'when updating priority via attributes' do+ let(:call_attributes) { attributes }+ let(:attributes) { { priority: other_priority } }++ it_behaves_like 'service call'+ end+ end++ context 'when switching the type' do+ let(:target_type) { build_stubbed(:type) }+ let(:work_package) do+ build_stubbed(:stubbed_work_package, start_date: Time.zone.today - 6.days, due_date: Time.zone.today)+ end++ context 'with a type that is no milestone' dobefore do- allow(new_project)- .to receive(:shared_versions)- .and_return(new_versions)- allow(new_project_categories)- .to receive(:find_by)- .with(name: category.name)- .and_return nil- allow(new_project)- .to receive(:types)- .and_return(new_types)- allow(new_types)- .to receive(:order)- .with(:position)- .and_return(new_types)- end-- shared_examples_for 'updating the project' do- context 'version' do- before do- work_package.version = version- end+ allow(target_type)+ .to receive(:is_milestone?)+ .and_return(false)+ end- context 'not shared in new project' do- it 'sets to nil' do- subject+ it 'keeps the start date' do+ instance.call(type: target_type)- expect(work_package.version)- .to be_nil- end- end+ expect(work_package.start_date)+ .to eql Time.zone.today - 6.days+ end++ it 'keeps the due date' do+ instance.call(type: target_type)- context 'shared in the new project' do- let(:new_versions) { [version] }+ expect(work_package.due_date)+ .to eql Time.zone.today+ end++ it 'keeps duration' do+ instance.call(type: target_type)++ expect(work_package.duration).to be 7+ end+ end++ context 'with a type that is a milestone and with both dates set' do+ before do+ allow(target_type)+ .to receive(:is_milestone?)+ .and_return(true)+ end++ it 'sets the start date to the due date' do+ instance.call(type: target_type)++ expect(work_package.start_date).to eql Time.zone.today+ end++ it 'keeps the due date' do+ instance.call(type: target_type)++ expect(work_package.due_date).to eql Time.zone.today+ end++ it 'sets the duration to 1 (to be changed to 0 later on)' do+ instance.call(type: target_type)++ expect(work_package.duration).to eq 1+ end+ end++ context 'with a type that is a milestone and with only the start date set' do+ let(:work_package) do+ build_stubbed(:stubbed_work_package, start_date: Time.zone.today - 6.days)+ end++ before do+ allow(target_type)+ .to receive(:is_milestone?)+ .and_return(true)+ end++ it 'keeps the start date' do+ instance.call(type: target_type)++ expect(work_package.start_date).to eql Time.zone.today - 6.days+ end- it 'keeps the version' do- subject+ it 'set the due date to the start date' do+ instance.call(type: target_type)- expect(work_package.version)- .to eql version- end+ expect(work_package.due_date).to eql Time.zone.today - 6.days+ end++ it 'keeps the duration at 1 (to be changed to 0 later on)' do+ instance.call(type: target_type)++ expect(work_package.duration).to eq 1+ end+ end+ end++ # rubocop:disable RSpec/MultipleMemoizedHelpers+ context 'when switching the project' do+ let(:new_project) { build_stubbed(:project) }+ let(:version) { build_stubbed(:version) }+ let(:category) { build_stubbed(:category) }+ let(:new_category) { build_stubbed(:category, name: category.name) }+ let(:new_statuses) { [work_package.status] }+ let(:new_versions) { [] }+ let(:type) { work_package.type }+ let(:new_types) { [type] }+ let(:default_type) { build_stubbed(:type_standard) }+ let(:other_type) { build_stubbed(:type) }+ let(:yet_another_type) { build_stubbed(:type) }++ let(:call_attributes) { {} }+ let(:new_project_categories) do+ categories_stub = double('categories')+ allow(new_project)+ .to receive(:categories)+ .and_return(categories_stub)++ categories_stub+ end++ before do+ allow(new_project)+ .to receive(:shared_versions)+ .and_return(new_versions)+ allow(new_project_categories)+ .to receive(:find_by)+ .with(name: category.name)+ .and_return nil+ allow(new_project)+ .to receive(:types)+ .and_return(new_types)+ allow(new_types)+ .to receive(:order)+ .with(:position)+ .and_return(new_types)+ end++ shared_examples_for 'updating the project' do+ context 'for version' do+ before do+ work_package.version = version+ end++ context 'when not shared in new project' do+ it 'sets to nil' do+ subject++ expect(work_package.version)+ .to be_nilendend- context 'category' do- before do- work_package.category = category+ context 'when shared in the new project' do+ let(:new_versions) { [version] }++ it 'keeps the version' do+ subject++ expect(work_package.version)+ .to eql versionend+ end+ end++ context 'for category' do+ before do+ work_package.category = category+ end- context 'no category of same name in new project' do- it 'sets to nil' do- subject+ context 'when no category of same name in new project' do+ it 'sets to nil' do+ subject- expect(work_package.category)- .to be_nil- end+ expect(work_package.category)+ .to be_nilend+ end- context 'category of same name in new project' do- before do- allow(new_project_categories)- .to receive(:find_by)- .with(name: category.name)- .and_return new_category- end+ context 'when category of same name in new project' do+ before do+ allow(new_project_categories)+ .to receive(:find_by)+ .with(name: category.name)+ .and_return new_category+ end- it 'uses the equally named category' do- subject+ it 'uses the equally named category' do+ subject- expect(work_package.category)- .to eql new_category- end+ expect(work_package.category)+ .to eql new_category+ end- it 'adds change to system changes' do- subject+ it 'adds change to system changes' do+ subject- expect(work_package.changed_by_system['category_id'])- .to eql [nil, new_category.id]- end+ expect(work_package.changed_by_system['category_id'])+ .to eql [nil, new_category.id]endend+ end- context 'type' do- context 'current type exists in new project' do- it 'leaves the type' do- subject+ context 'for type' do+ context 'when current type exists in new project' do+ it 'leaves the type' do+ subject- expect(work_package.type)- .to eql type- end+ expect(work_package.type)+ .to eql typeend+ end- context 'a default type exists in new project' do- let(:new_types) { [other_type, default_type] }+ context 'when a default type exists in new project' do+ let(:new_types) { [other_type, default_type] }- it 'uses the first type (by position)' do- subject+ it 'uses the first type (by position)' do+ subject- expect(work_package.type)- .to eql other_type- end+ expect(work_package.type)+ .to eql other_type+ end- it 'adds change to system changes' do- subject+ it 'adds change to system changes' do+ subject- expect(work_package.changed_by_system['type_id'])- .to eql [initial_type.id, other_type.id]- end+ expect(work_package.changed_by_system['type_id'])+ .to eql [initial_type.id, other_type.id]end+ end- context 'no default type exists in new project' do- let(:new_types) { [other_type, yet_another_type] }+ context 'when no default type exists in new project' do+ let(:new_types) { [other_type, yet_another_type] }- it 'uses the first type (by position)' do- subject+ it 'uses the first type (by position)' do+ subject- expect(work_package.type)- .to eql other_type- end+ expect(work_package.type)+ .to eql other_type+ end- it 'adds change to system changes' do- subject+ it 'adds change to system changes' do+ subject- expect(work_package.changed_by_system['type_id'])- .to eql [initial_type.id, other_type.id]- end+ expect(work_package.changed_by_system['type_id'])+ .to eql [initial_type.id, other_type.id]end+ end- context 'when also setting a new type via attributes' do- let(:attributes) { { project: new_project, type: yet_another_type } }+ context 'when also setting a new type via attributes' do+ let(:attributes) { { project: new_project, type: yet_another_type } }- it 'sets the desired type' do- subject+ it 'sets the desired type' do+ subject- expect(work_package.type)- .to eql yet_another_type- end+ expect(work_package.type)+ .to eql yet_another_type+ end- it 'does not set the change to system changes' do- subject+ it 'does not set the change to system changes' do+ subject- expect(work_package.changed_by_system)- .not_to include('type_id')- end+ expect(work_package.changed_by_system)+ .not_to include('type_id')endendend+ end- context 'update project before calling the service' do- let(:call_attributes) { {} }- let(:attributes) { { project: new_project } }+ context 'when updating project before calling the service' do+ let(:call_attributes) { {} }+ let(:attributes) { { project: new_project } }- before do- work_package.attributes = attributes- end+ before do+ work_package.attributes = attributes+ end- it_behaves_like 'service call' do- it_behaves_like 'updating the project'- end+ it_behaves_like 'service call' do+ it_behaves_like 'updating the project'end+ end- context 'updating project via attributes' do- let(:call_attributes) { attributes }- let(:attributes) { { project: new_project } }+ context 'when updating project via attributes' do+ let(:call_attributes) { attributes }+ let(:attributes) { { project: new_project } }- it_behaves_like 'service call' do- it_behaves_like 'updating the project'- end+ it_behaves_like 'service call' do+ it_behaves_like 'updating the project'endend+ end+ # rubocop:enable RSpec/MultipleMemoizedHelpers- context 'custom fields' do- subject { instance.call(call_attributes) }+ context 'for custom fields' do+ subject { instance.call(call_attributes) }- context 'non existing fields' do- let(:call_attributes) { { custom_field_891: '1' } }+ context 'non existing fields' do+ let(:call_attributes) { { custom_field_891: '1' } }- before do- subject- end+ before do+ subject+ end- it 'is successful' do- expect(subject).to be_success- end+ it 'is successful' do+ expect(subject).to be_successendend+ end- context 'when switching back to automatic scheduling' do- let(:work_package) do- wp = build_stubbed(:work_package,- project: project,- schedule_manually: true,- start_date: Date.today,- due_date: Date.today + 5.days)- wp.type = build_stubbed(:type)- wp.send(:clear_changes_information)-- allow(wp)- .to receive(:soonest_start)- .and_return(soonest_start)-- wp- end- let(:call_attributes) { { schedule_manually: false } }- let(:attributes) { {} }- let(:soonest_start) { Date.today + 1.day }+ context 'when switching back to automatic scheduling' do+ let(:work_package) do+ wp = build_stubbed(:work_package,+ project: project,+ schedule_manually: true,+ start_date: Time.zone.today,+ due_date: Time.zone.today + 5.days)+ wp.type = build_stubbed(:type)+ wp.send(:clear_changes_information)++ allow(wp)+ .to receive(:soonest_start)+ .and_return(soonest_start)++ wp+ end+ let(:call_attributes) { { schedule_manually: false } }+ let(:attributes) { {} }+ let(:soonest_start) { Time.zone.today + 1.day }- context 'when the soonest start date is later than the current start date' do- let(:soonest_start) { Date.today + 3.days }+ context 'when the soonest start date is later than the current start date' do+ let(:soonest_start) { Time.zone.today + 3.days }- it_behaves_like 'service call' do- it 'sets the start date to the soonest possible start date' do- subject+ it_behaves_like 'service call' do+ it 'sets the start date to the soonest possible start date' do+ subject- expect(work_package.start_date).to eql(Date.today + 3.days)- expect(work_package.due_date).to eql(Date.today + 8.days)- end+ expect(work_package.start_date).to eql(Time.zone.today + 3.days)+ expect(work_package.due_date).to eql(Time.zone.today + 8.days)endend+ end- context 'when the soonest start date is before the current start date' do- let(:soonest_start) { Date.today - 3.days }+ context 'when the soonest start date is before the current start date' do+ let(:soonest_start) { Time.zone.today - 3.days }- it_behaves_like 'service call' do- it 'sets the start date to the soonest possible start date' do- subject+ it_behaves_like 'service call' do+ it 'sets the start date to the soonest possible start date' do+ subject- expect(work_package.start_date).to eql(soonest_start)- expect(work_package.due_date).to eql(Date.today + 2.days)- end+ expect(work_package.start_date).to eql(soonest_start)+ expect(work_package.due_date).to eql(Time.zone.today + 2.days)endend+ end- context 'when the soonest start date is nil' do- let(:soonest_start) { nil }+ context 'when the soonest start date is nil' do+ let(:soonest_start) { nil }- it_behaves_like 'service call' do- it 'sets the start date to the soonest possible start date' do- subject+ it_behaves_like 'service call' do+ it 'sets the start date to the soonest possible start date' do+ subject- expect(work_package.start_date).to eql(Date.today)- expect(work_package.due_date).to eql(Date.today + 5.days)- end+ expect(work_package.start_date).to eql(Time.zone.today)+ expect(work_package.due_date).to eql(Time.zone.today + 5.days)endend+ end- context 'when the work package also has a child' do- let(:child) do- build_stubbed(:stubbed_work_package,- start_date: child_start_date,- due_date: child_due_date)- end- let(:child_start_date) { Date.today + 2.days }- let(:child_due_date) { Date.today + 10.days }+ context 'when the work package also has a child' do+ let(:child) do+ build_stubbed(:stubbed_work_package,+ start_date: child_start_date,+ due_date: child_due_date)+ end+ let(:child_start_date) { Time.zone.today + 2.days }+ let(:child_due_date) { Time.zone.today + 10.days }- before do- allow(work_package)- .to receive(:children)- .and_return([child])- end+ before do+ allow(work_package)+ .to receive(:children)+ .and_return([child])+ end- context 'when the child`s start date is after soonest_start' do- it_behaves_like 'service call' do- it 'sets the dates to the child dates' do- subject+ context 'when the child`s start date is after soonest_start' do+ it_behaves_like 'service call' do+ it 'sets the dates to the child dates' do+ subject- expect(work_package.start_date).to eql(Date.today + 2.days)- expect(work_package.due_date).to eql(Date.today + 10.days)- end+ expect(work_package.start_date).to eql(Time.zone.today + 2.days)+ expect(work_package.due_date).to eql(Time.zone.today + 10.days)endend+ end- context 'when the child`s start date is before soonest_start' do- let(:soonest_start) { Date.today + 3.days }+ context 'when the child`s start date is before soonest_start' do+ let(:soonest_start) { Time.zone.today + 3.days }- it_behaves_like 'service call' do- it 'sets the dates to soonest date and to the duration of the child' do- subject+ it_behaves_like 'service call' do+ it 'sets the dates to soonest date and to the duration of the child' do+ subject- expect(work_package.start_date).to eql(Date.today + 3.days)- expect(work_package.due_date).to eql(Date.today + 11.days)- end+ expect(work_package.start_date).to eql(Time.zone.today + 3.days)+ expect(work_package.due_date).to eql(Time.zone.today + 11.days)endendend