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

[#60122] Required custom fields prevent a WorkPackage from being added as a child

Dombi Attila9412dc226d17
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rb
index 869a428f392..55b20e962f4 100644
--- a/app/contracts/work_packages/base_contract.rb
+++ b/app/contracts/work_packages/base_contract.rb
@@ -222,6 +222,8 @@ module WorkPackages
end
alias_method :assignable_responsibles, :assignable_assignees
+ def valid?(context = :saving_custom_fields) = super
+
private
attr_reader :can
diff --git a/app/contracts/work_packages/update_parent_contract.rb b/app/contracts/work_packages/update_parent_contract.rb
new file mode 100644
index 00000000000..10c2d6823c7
--- /dev/null
+++ b/app/contracts/work_packages/update_parent_contract.rb
@@ -0,0 +1,37 @@
+# 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.
+#++
+
+module WorkPackages
+ class UpdateParentContract < UpdateContract
+ # Replacing the :saving_custom_fields so that no no required custom fields
+ # are validated, when updating the parent relation for a work package.
+ def valid?(context = :update) = super
+ end
+end
diff --git a/app/controllers/work_package_children_relations_controller.rb b/app/controllers/work_package_children_relations_controller.rb
index 603ca0abbc7..ddaf07cd88a 100644
--- a/app/controllers/work_package_children_relations_controller.rb
+++ b/app/controllers/work_package_children_relations_controller.rb
@@ -79,8 +79,11 @@ class WorkPackageChildrenRelationsController < ApplicationController
def set_relation(child:, parent:)
if allowed_to_set_parent?(child)
- WorkPackages::UpdateService.new(user: current_user, model: child)
- .call(parent:)
+ WorkPackages::UpdateService.new(
+ user: current_user,
+ model: child,
+ contract_class: WorkPackages::UpdateParentContract
+ ).call(parent:)
else
child.errors.add(:id, :cannot_add_child_because_of_lack_of_permission)
ServiceResult.failure(result: child)
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index bcea8d0a959..29440e3d9f8 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -147,7 +147,7 @@ class WorkPackage < ApplicationRecord
# thus the associated agenda items will be available at the time the callback method is performed.
around_destroy :save_agenda_item_journals, prepend: true, if: -> { meeting_agenda_items.any? }
- acts_as_customizable
+ acts_as_customizable validate_on: :saving_custom_fields
acts_as_searchable columns: ["subject",
"#{table_name}.description",
diff --git a/spec/controllers/work_package_children_relations_controller_spec.rb b/spec/controllers/work_package_children_relations_controller_spec.rb
index e7f691ec82d..28f52d6d3ee 100644
--- a/spec/controllers/work_package_children_relations_controller_spec.rb
+++ b/spec/controllers/work_package_children_relations_controller_spec.rb
@@ -40,7 +40,7 @@ RSpec.describe WorkPackageChildrenRelationsController do
current_user { user }
- describe "GET /work_packages/:work_package_id/children/new" do
+ describe "GET /work_packages/:work_package_id/children_relations/new" do
before do
allow(WorkPackageRelationsTab::AddWorkPackageChildDialogComponent)
.to receive(:new)
@@ -57,7 +57,7 @@ RSpec.describe WorkPackageChildrenRelationsController do
end
end
- describe "POST /work_packages/:work_package_id/children" do
+ describe "POST /work_packages/:work_package_id/children_relations" do
shared_let(:future_child_work_package) { create(:work_package, project:) }
it "creates a child relationship" do
@@ -76,6 +76,23 @@ RSpec.describe WorkPackageChildrenRelationsController do
expect(response).to have_http_status(:unprocessable_entity)
expect(future_child_work_package.reload.parent).to be_nil
end
+
+ context "when the child is invalid due to a required custom field" do
+ shared_let(:custom_field) do
+ create(:integer_wp_custom_field, is_required: true, is_for_all: true, default_value: nil) do |cf|
+ project.types.first.custom_fields << cf
+ project.work_package_custom_fields << cf
+ end
+ end
+
+ it "the creation call still succeeds" do
+ post("create", params: { work_package_id: work_package.id,
+ work_package: { id: future_child_work_package.id } },
+ as: :turbo_stream)
+ expect(response).to have_http_status(:ok)
+ expect(future_child_work_package.reload.parent).to eq(work_package)
+ end
+ end
end
describe "DELETE /work_packages/:work_package_id/children/:id" do
@@ -116,5 +133,21 @@ RSpec.describe WorkPackageChildrenRelationsController do
expect(WorkPackages::SetScheduleService).to have_received(:new)
.with(a_hash_including(work_package: [child_work_package, work_package]))
end
+
+ context "when the child is invalid due to a required custom field" do
+ shared_let(:custom_field) do
+ create(:integer_wp_custom_field, is_required: true, is_for_all: true, default_value: nil) do |cf|
+ project.types.first.custom_fields << cf
+ project.work_package_custom_fields << cf
+ end
+ end
+
+ it "the creation call still succeeds" do
+ send_delete_request
+
+ expect(response).to have_http_status(:ok)
+ expect(child_work_package.reload.parent).to be_nil
+ end
+ end
end
end
diff --git a/spec/features/activities/work_package/activities_spec.rb b/spec/features/activities/work_package/activities_spec.rb
index d4219ed1250..73ad5d2fd03 100644
--- a/spec/features/activities/work_package/activities_spec.rb
+++ b/spec/features/activities/work_package/activities_spec.rb
@@ -1515,6 +1515,28 @@ RSpec.describe "Work package activity", :js, :with_cuprite do
end
end
end
+
+ context "when the work package is invalid due to a required custom field" do
+ let!(:custom_field) do
+ create(:integer_wp_custom_field, is_required: true, is_for_all: true, default_value: nil) do |cf|
+ project.types.first.custom_fields << cf
+ project.work_package_custom_fields << cf
+ end
+ end
+
+ it "the creation call still succeeds" do
+ activity_tab.add_comment(text: "First comment by admin")
+
+ comment = work_package.journals.reload.last
+
+ activity_tab.within_journal_entry(comment) do
+ page.find_test_selector("op-wp-journal-#{comment.id}-action-menu").click
+
+ expect(page).to have_test_selector("op-wp-journal-#{comment.id}-edit")
+ expect(page).to have_test_selector("op-wp-journal-#{comment.id}-quote")
+ end
+ end
+ end
end
context "when editing a comment" do
diff --git a/spec/models/work_package/work_package_acts_as_customizable_spec.rb b/spec/models/work_package/work_package_acts_as_customizable_spec.rb
index a0581f2d05f..6b65861c71b 100644
--- a/spec/models/work_package/work_package_acts_as_customizable_spec.rb
+++ b/spec/models/work_package/work_package_acts_as_customizable_spec.rb
@@ -165,7 +165,7 @@ RSpec.describe WorkPackage, "acts_as_customizable" do
# set that custom field with a value, should be fine
work_package.custom_field_values = { cf1.id => "test" }
- work_package.save!
+ work_package.save!(context: :saving_custom_fields)
work_package.reload
# now give the work_package another required custom field, but don't assign a value
@@ -173,7 +173,7 @@ RSpec.describe WorkPackage, "acts_as_customizable" do
work_package.custom_field_values # #custom_field_values needs to be touched
# that should not be valid
- expect(work_package).not_to be_valid
+ expect(work_package).not_to be_valid(:saving_custom_fields)
# assert that there is only one error
expect(work_package.errors.size).to eq 1
diff --git a/spec/models/work_package/work_package_custom_fields_spec.rb b/spec/models/work_package/work_package_custom_fields_spec.rb
index 2906dbd412c..5e7a1d3a31a 100644
--- a/spec/models/work_package/work_package_custom_fields_spec.rb
+++ b/spec/models/work_package/work_package_custom_fields_spec.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -61,7 +63,7 @@ RSpec.describe WorkPackage do
val = custom_field.custom_options.find { |co| co.value == value }.try(:id)
work_package.custom_field_values = { custom_field.id => val || value }
- work_package.save if save
+ work_package.save(context: :saving_custom_fields) if save
end
end
@@ -82,12 +84,12 @@ RSpec.describe WorkPackage do
context "creating a cf value" do
it "updates the updated_at attribute" do
expect { change_custom_field_value(work_package, custom_field.possible_values.first) }
- .to change { work_package.updated_at }
+ .to change(work_package, :updated_at)
end
it "updates the lock_version attribute" do
expect { change_custom_field_value(work_package, custom_field.possible_values.first) }
- .to change { work_package.lock_version }.by(1)
+ .to change(work_package, :lock_version).by(1)
end
end
@@ -98,12 +100,12 @@ RSpec.describe WorkPackage do
it "updates the updated_at attribute" do
expect { change_custom_field_value(work_package, nil) }
- .to change { work_package.updated_at }
+ .to change(work_package, :updated_at)
end
it "updates the lock_version attribute" do
expect { change_custom_field_value(work_package, nil) }
- .to change { work_package.lock_version }.by(1)
+ .to change(work_package, :lock_version).by(1)
end
end
@@ -114,12 +116,12 @@ RSpec.describe WorkPackage do
it "updates the updated_at attribute" do
expect { change_custom_field_value(work_package, custom_field.possible_values.last) }
- .to change { work_package.updated_at }
+ .to change(work_package, :updated_at)
end
it "updates the lock_version attribute" do
expect { change_custom_field_value(work_package, custom_field.possible_values.last) }
- .to change { work_package.lock_version }.by(1)
+ .to change(work_package, :lock_version).by(1)
end
end
@@ -132,7 +134,7 @@ RSpec.describe WorkPackage do
work_package.subject = "new subject"
expect { change_custom_field_value(work_package, custom_field.possible_values.last) }
- .to change { work_package.lock_version }.by(1)
+ .to change(work_package, :lock_version).by(1)
end
end
end
@@ -152,7 +154,7 @@ RSpec.describe WorkPackage do
end
describe "error message" do
- before { work_package.save }
+ before { work_package.save(context: :saving_custom_fields) }
subject { work_package.errors[custom_field_key] }
@@ -163,7 +165,7 @@ RSpec.describe WorkPackage do
describe "symbols_for" do
before do
- work_package.save
+ work_package.save(context: :saving_custom_fields)
end
it "stores the symbol" do
@@ -173,7 +175,7 @@ RSpec.describe WorkPackage do
end
describe "work package attribute update" do
- subject { work_package.save }
+ subject { work_package.save(context: :saving_custom_fields) }
it { is_expected.to be_falsey }
end
@@ -272,7 +274,7 @@ RSpec.describe WorkPackage do
work_package.type = type_feature
end
- subject { WorkPackage.find(work_package.id).typed_custom_value_for(custom_field) }
+ subject { described_class.find(work_package.id).typed_custom_value_for(custom_field) }
it { is_expected.to eq("PostgreSQL") }
end
@@ -309,7 +311,7 @@ RSpec.describe WorkPackage do
end
subject do
- wp = WorkPackage.new.tap do |i|
+ wp = described_class.new.tap do |i|
i.attributes = { project: }
end
wp.attributes = attribute_hash
@@ -336,7 +338,7 @@ RSpec.describe WorkPackage do
describe "value" do
let(:relevant_journal) do
- work_package.journals.find { |j| j.customizable_journals.size > 0 }
+ work_package.journals.find { |j| !j.customizable_journals.empty? }
end
subject { relevant_journal.customizable_journals.first.value }
@@ -392,8 +394,8 @@ RSpec.describe WorkPackage do
work_package.custom_field_values.first.value = "12345"
# don't want to see I18n::MissingInterpolationArgument specifically
- expect { work_package.valid? }.not_to raise_error
- expect(work_package.valid?).to be_falsey
+ expect { work_package.valid?(:saving_custom_fields) }.not_to raise_error
+ expect(work_package).not_to be_valid(:saving_custom_fields)
expect(work_package.errors.full_messages.first)
.to eq "PIN is too long (maximum is 4 characters)."
diff --git a/spec/services/relations/delete_service_spec.rb b/spec/services/relations/delete_service_spec.rb
index c699794c2a2..87a18a8c4cb 100644
--- a/spec/services/relations/delete_service_spec.rb
+++ b/spec/services/relations/delete_service_spec.rb
@@ -96,13 +96,13 @@ RSpec.describe Relations::DeleteService do
it "still updates correctly" do
# ensure the work package is invalid as intended
expect(work_package.schedule_manually).to be false
- expect(work_package).not_to be_valid
+ expect(work_package).not_to be_valid(:saving_custom_fields)
expect(delete_service_result).to be_success
# work package has been changed to manual scheduling though still invalid
expect(work_package.schedule_manually).to be true
- expect(work_package).not_to be_valid
+ expect(work_package).not_to be_valid(:saving_custom_fields)
end
end
end
@@ -139,13 +139,13 @@ RSpec.describe Relations::DeleteService do
it "still updates correctly" do
# ensure the work package is invalid as intended
expect(work_package.start_date).to eq(_table.friday)
- expect(work_package).not_to be_valid
+ expect(work_package).not_to be_valid(:saving_custom_fields)
expect(delete_service_result).to be_success
# work package has been rescheduled though still invalid
expect(work_package.start_date).to eq(_table.wednesday)
- expect(work_package).not_to be_valid
+ expect(work_package).not_to be_valid(:saving_custom_fields)
end
end
end
diff --git a/spec/services/work_packages/update_service_integration_spec.rb b/spec/services/work_packages/update_service_integration_spec.rb
index 27408311ec1..3f980d30ce3 100644
--- a/spec/services/work_packages/update_service_integration_spec.rb
+++ b/spec/services/work_packages/update_service_integration_spec.rb
@@ -1539,13 +1539,13 @@ RSpec.describe WorkPackages::UpdateService, "integration", type: :model do
end
it "removes the parent successfully and reschedules the parent" do
- expect(parent.valid?).to be(false)
+ expect(parent).not_to be_valid(:saving_custom_fields)
expect(subject).to be_success
expect(work_package.reload.parent).to be_nil
parent.reload
- expect(parent.valid?).to be(false)
+ expect(parent).not_to be_valid(:saving_custom_fields)
expect(parent.start_date)
.to eql(sibling.start_date)
expect(parent.due_date)