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
[#36719] Invalid status saved for work packages when created from a view with status filter
Dombi Attilad3120f0f9ee9
diff --git a/app/contracts/work_packages/create_contract.rb b/app/contracts/work_packages/create_contract.rbindex 461c291fe7a..b331aa6a4dc 100644--- a/app/contracts/work_packages/create_contract.rb+++ b/app/contracts/work_packages/create_contract.rb@@ -59,5 +59,17 @@ module WorkPackages# lock version is initialized by AR itselfsuper - ['lock_version']end++ def status_exists?+ super && (!type_exists? || status_exists_in_type?)+ end++ def type_exists?+ model.type_id && model.type && !type_inexistent?+ end++ def status_exists_in_type?+ model.type.statuses.exists?(model.status_id)+ endendenddiff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rbindex 5e56ca961dc..2728acc2653 100644--- a/app/services/work_packages/set_attributes_service.rb+++ b/app/services/work_packages/set_attributes_service.rb@@ -168,9 +168,16 @@ class WorkPackages::SetAttributesService < BaseServices::SetAttributesenddef set_default_status+ remove_status_if_invalidwork_package.status ||= Status.defaultend+ def remove_status_if_invalid+ if work_package.type && !work_package.type.statuses.exists?(model.status_id)+ work_package.status = nil+ end+ end+def set_default_prioritywork_package.priority ||= IssuePriority.active.defaultenddiff --git a/spec/contracts/work_packages/create_contract_spec.rb b/spec/contracts/work_packages/create_contract_spec.rbindex c9f107538c7..6a640668f27 100644--- a/spec/contracts/work_packages/create_contract_spec.rb+++ b/spec/contracts/work_packages/create_contract_spec.rb@@ -78,7 +78,7 @@ RSpec.describe WorkPackages::CreateContract doit 'is not authorized' doexpect(validated_contract.errors.symbols_for(:base))- .to match_array [:error_unauthorized]+ .to contain_exactly(:error_unauthorized)endend@@ -99,7 +99,7 @@ RSpec.describe WorkPackages::CreateContract doit 'is not authorized' doexpect(validated_contract.errors.symbols_for(:base))- .to match_array [:error_unauthorized]+ .to contain_exactly(:error_unauthorized)endend@@ -112,7 +112,7 @@ RSpec.describe WorkPackages::CreateContract doit 'is not authorized' doexpect(validated_contract.errors.symbols_for(:base))- .to match_array [:error_unauthorized]+ .to contain_exactly(:error_unauthorized)endendend@@ -140,7 +140,41 @@ RSpec.describe WorkPackages::CreateContract dowork_package.author = build_stubbed(:user)expect(validated_contract.errors.symbols_for(:author_id))- .to match_array %i[invalid error_readonly]+ .to contain_exactly(:invalid, :error_readonly)+ end+ end+ end++ describe 'status' do+ let(:status) { create(:status) }+ let(:type) { create(:type) }++ before do+ work_package.type = type+ work_package.status = status+ end++ context 'when a type is not present' do+ it 'is valid' do+ work_package.type = nil+ expect(validated_contract.errors.symbols_for(:status))+ .to be_empty+ end+ end++ context 'when a type is present and the status is not part of it' do+ it 'is invalid' do+ expect(validated_contract.errors.symbols_for(:status))+ .to contain_exactly(:does_not_exist)+ end+ end++ context 'when a type is present and the status is part of it' do+ let!(:workflow) { create(:workflow, type:, old_status: status) }++ it 'is valid' do+ expect(validated_contract.errors.symbols_for(:status))+ .to be_emptyendendenddiff --git a/spec/services/work_packages/set_attributes_service_spec.rb b/spec/services/work_packages/set_attributes_service_spec.rbindex adffc3979c3..5c1e933e0e0 100644--- a/spec/services/work_packages/set_attributes_service_spec.rb+++ b/spec/services/work_packages/set_attributes_service_spec.rb@@ -85,11 +85,11 @@ RSpec.describe WorkPackages::SetAttributesService,endit 'sets the value' do- next if !defined?(attributes) || attributes.blank?+ next if !defined?(expected_attributes) || expected_attributes.blank?subject- attributes.each do |attribute, key|+ expected_attributes.each do |attribute, key|expect(work_package.send(attribute)).to eql keyendend@@ -129,18 +129,18 @@ RSpec.describe WorkPackages::SetAttributesService,context 'when updating subject before calling the service' dolet(:call_attributes) { {} }- let(:attributes) { { subject: 'blubs blubs' } }+ let(:expected_attributes) { { subject: 'blubs blubs' } }before do- work_package.attributes = attributes+ work_package.attributes = expected_attributesendit_behaves_like 'service call'endcontext 'when updating subject via attributes' do- let(:call_attributes) { attributes }- let(:attributes) { { subject: 'blubs blubs' } }+ let(:call_attributes) { expected_attributes }+ let(:expected_attributes) { { subject: 'blubs blubs' } }it_behaves_like 'service call'end@@ -158,26 +158,49 @@ RSpec.describe WorkPackages::SetAttributesService,context 'with no value set before for a new work package' dolet(:call_attributes) { {} }- let(:attributes) { {} }+ let(:expected_attributes) { { status: default_status } }let(:work_package) { new_work_package }before dowork_package.status = nilend- it_behaves_like 'service call' do- it 'sets the default status' do- subject+ it_behaves_like 'service call'+ end- expect(work_package.status)- .to eql default_status- end- end+ context 'with an invalid value that is not part of the type.statuses for a new work package' do+ let(:invalid_status) { create(:status) }+ let(:type) { create(:type) }+ let(:call_attributes) { { status: invalid_status, type: } }+ let(:expected_attributes) { { status: default_status, type: } }+ let(:work_package) { new_work_package }++ it_behaves_like 'service call'+ end++ context 'with valid value and without a type present for a new work package' do+ let(:status) { create(:status) }+ let(:call_attributes) { { status:, type: nil } }+ let(:expected_attributes) { { status: } }+ let(:work_package) { new_work_package }++ it_behaves_like 'service call'+ end++ context 'with a valid value that is part of the type.statuses for a new work package' do+ let(:status) { create(:status) }+ let(:type) { create(:type) }+ let!(:workflow) { create(:workflow, type:, old_status: status) }+ let(:call_attributes) { { status:, type: } }+ let(:expected_attributes) { { status:, type: } }+ let(:work_package) { new_work_package }++ it_behaves_like 'service call'endcontext 'with no value set on existing work package' dolet(:call_attributes) { {} }- let(:attributes) { {} }+ let(:expected_attributes) { {} }before dowork_package.status = nil@@ -195,18 +218,18 @@ RSpec.describe WorkPackages::SetAttributesService,context 'when updating status before calling the service' dolet(:call_attributes) { {} }- let(:attributes) { { status: other_status } }+ let(:expected_attributes) { { status: other_status } }before do- work_package.attributes = attributes+ work_package.attributes = expected_attributesendit_behaves_like 'service call'endcontext 'when updating status via attributes' do- let(:call_attributes) { attributes }- let(:attributes) { { status: other_status } }+ let(:call_attributes) { expected_attributes }+ let(:expected_attributes) { { status: other_status } }it_behaves_like 'service call'end@@ -217,7 +240,7 @@ RSpec.describe WorkPackages::SetAttributesService,context 'with no value set before for a new work package' dolet(:call_attributes) { {} }- let(:attributes) { {} }+ let(:expected_attributes) { {} }let(:work_package) { new_work_package }it_behaves_like 'service call' do@@ -239,7 +262,7 @@ RSpec.describe WorkPackages::SetAttributesService,context 'with no value set on existing work package' dolet(:call_attributes) { {} }- let(:attributes) { {} }+ let(:expected_attributes) { {} }before dowork_package.author = nil@@ -257,18 +280,18 @@ RSpec.describe WorkPackages::SetAttributesService,context 'when updating author before calling the service' dolet(:call_attributes) { {} }- let(:attributes) { { author: other_user } }+ let(:expected_attributes) { { author: other_user } }before do- work_package.attributes = attributes+ work_package.attributes = expected_attributesendit_behaves_like 'service call'endcontext 'when updating author via attributes' do- let(:call_attributes) { attributes }- let(:attributes) { { author: other_user } }+ let(:call_attributes) { expected_attributes }+ let(:expected_attributes) { { author: other_user } }it_behaves_like 'service call'end@@ -288,8 +311,8 @@ RSpec.describe WorkPackages::SetAttributesService,endcontext 'with a currently invalid subject' do- let(:call_attributes) { attributes }- let(:attributes) { { subject: 'ABC' } }+ let(:call_attributes) { expected_attributes }+ let(:expected_attributes) { { subject: 'ABC' } }let(:contract_valid) { true }subject { instance.call(call_attributes) }@@ -303,7 +326,7 @@ RSpec.describe WorkPackages::SetAttributesService,context 'for start_date & due_date & duration' docontext 'with a parent' do- let(:attributes) { {} }+ let(:expected_attributes) { {} }let(:work_package) { new_work_package }let(:parent) dobuild_stubbed(:work_package,@@ -587,7 +610,7 @@ RSpec.describe WorkPackages::SetAttributesService,context 'with no value set for a new work package and with default setting active',with_settings: { work_package_startdate_is_adddate: true } dolet(:call_attributes) { {} }- let(:attributes) { {} }+ let(:expected_attributes) { {} }let(:work_package) { new_work_package }it_behaves_like 'service call' do@@ -623,7 +646,7 @@ RSpec.describe WorkPackages::SetAttributesService,context 'with a value set for a new work package and with default setting active',with_settings: { work_package_startdate_is_adddate: true } dolet(:call_attributes) { { start_date: Time.zone.today + 1.day } }- let(:attributes) { {} }+ let(:expected_attributes) { {} }let(:work_package) { new_work_package }it_behaves_like 'service call' do@@ -658,7 +681,7 @@ RSpec.describe WorkPackages::SetAttributesService,context 'with date values set to the same date on a new work package' dolet(:call_attributes) { { start_date: Time.zone.today, due_date: Time.zone.today } }- let(:attributes) { {} }+ let(:expected_attributes) { {} }let(:work_package) { new_work_package }it_behaves_like 'service call' do@@ -687,7 +710,7 @@ RSpec.describe WorkPackages::SetAttributesService,context 'with date values set on a new work package' dolet(:call_attributes) { { start_date: Time.zone.today, due_date: Time.zone.today + 5.days } }- let(:attributes) { {} }+ let(:expected_attributes) { {} }let(:work_package) { new_work_package }it_behaves_like 'service call' do@@ -717,7 +740,7 @@ RSpec.describe WorkPackages::SetAttributesService,context 'with start date changed' dolet(:work_package) { build_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) { {} }+ let(:expected_attributes) { {} }it_behaves_like 'service call' doit 'sets the start date value' do@@ -746,7 +769,7 @@ RSpec.describe WorkPackages::SetAttributesService,context 'with due date changed' dolet(:work_package) { build_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) { {} }+ let(:expected_attributes) { {} }it_behaves_like 'service call' doit 'keeps the start date value' do@@ -778,7 +801,7 @@ RSpec.describe WorkPackages::SetAttributesService,build_stubbed(:work_package, *traits, start_date: Time.zone.today, due_date: Time.zone.today + 5.days)endlet(:call_attributes) { { start_date: nil } }- let(:attributes) { {} }+ let(:expected_attributes) { {} }it_behaves_like 'service call' doit 'sets the start date to nil' do@@ -821,7 +844,7 @@ RSpec.describe WorkPackages::SetAttributesService,build_stubbed(:work_package, *traits, start_date: Time.zone.today, due_date: Time.zone.today + 5.days)endlet(:call_attributes) { { due_date: nil } }- let(:attributes) { {} }+ let(:expected_attributes) { {} }it_behaves_like 'service call' doit 'keeps the start date' do@@ -1258,7 +1281,7 @@ RSpec.describe WorkPackages::SetAttributesService,context 'with no value set before for a new work package' dolet(:call_attributes) { {} }- let(:attributes) { {} }+ let(:expected_attributes) { {} }let(:work_package) { new_work_package }before do@@ -1277,18 +1300,18 @@ RSpec.describe WorkPackages::SetAttributesService,context 'when updating priority before calling the service' dolet(:call_attributes) { {} }- let(:attributes) { { priority: other_priority } }+ let(:expected_attributes) { { priority: other_priority } }before do- work_package.attributes = attributes+ work_package.attributes = expected_attributesendit_behaves_like 'service call'endcontext 'when updating priority via attributes' do- let(:call_attributes) { attributes }- let(:attributes) { { priority: other_priority } }+ let(:call_attributes) { expected_attributes }+ let(:expected_attributes) { { priority: other_priority } }it_behaves_like 'service call'end@@ -1547,7 +1570,7 @@ RSpec.describe WorkPackages::SetAttributesService,endcontext 'when also setting a new type via attributes' do- let(:attributes) { { project: new_project, type: yet_another_type } }+ let(:expected_attributes) { { project: new_project, type: yet_another_type } }it 'sets the desired type' dosubject@@ -1595,10 +1618,10 @@ RSpec.describe WorkPackages::SetAttributesService,context 'when updating project before calling the service' dolet(:call_attributes) { {} }- let(:attributes) { { project: new_project } }+ let(:expected_attributes) { { project: new_project } }before do- work_package.attributes = attributes+ work_package.attributes = expected_attributesendit_behaves_like 'service call' do@@ -1607,8 +1630,8 @@ RSpec.describe WorkPackages::SetAttributesService,endcontext 'when updating project via attributes' do- let(:call_attributes) { attributes }- let(:attributes) { { project: new_project } }+ let(:call_attributes) { expected_attributes }+ let(:expected_attributes) { { project: new_project } }it_behaves_like 'service call' doit_behaves_like 'updating the project'@@ -1650,7 +1673,7 @@ RSpec.describe WorkPackages::SetAttributesService,wpendlet(:call_attributes) { { schedule_manually: false } }- let(:attributes) { {} }+ let(:expected_attributes) { {} }let(:soonest_start) { Time.zone.today + 1.day }context 'when the soonest start date is later than the current start date' do