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

include RequestStore clearing into specs

ulferts65032c2961b6
diff --git a/spec/factories/custom_field_factory.rb b/spec/factories/custom_field_factory.rb
index df68f3500d1..c124f9fdd16 100644
--- a/spec/factories/custom_field_factory.rb
+++ b/spec/factories/custom_field_factory.rb
@@ -182,6 +182,21 @@ FactoryBot.define do
type { 'WorkPackageCustomField' }
is_filter { true }
+ transient do
+ projects { [] }
+ end
+
+ after(:create) do |custom_field, evaluator|
+ evaluator.projects.each do |project|
+ project.work_package_custom_fields << custom_field
+ end
+
+ if evaluator.projects.any? || evaluator.types.any?
+ # As the request store keeps track of the available custom fields of work packages
+ RequestStore.clear!
+ end
+ end
+
factory :list_wp_custom_field do
sequence(:name) { |n| "List CF #{n}" }
field_format { 'list' }
diff --git a/spec/features/work_packages/table/edit_work_packages_spec.rb b/spec/features/work_packages/table/edit_work_packages_spec.rb
index 4d8aa7b7c9b..371944c2488 100644
--- a/spec/features/work_packages/table/edit_work_packages_spec.rb
+++ b/spec/features/work_packages/table/edit_work_packages_spec.rb
@@ -116,28 +116,32 @@ RSpec.describe 'Inline editing work packages', js: true do
end
context 'custom field' do
- let(:custom_fields) do
+ let!(:custom_fields) do
fields = [
create(
:work_package_custom_field,
field_format: 'list',
possible_values: %w(foo bar xyz),
- is_required: true,
- is_for_all: false
+ is_required: false,
+ is_for_all: false,
+ types: [type],
+ projects: [project]
),
create(
:work_package_custom_field,
field_format: 'string',
- is_required: true,
- is_for_all: false
+ is_required: false,
+ is_for_all: false,
+ types: [type],
+ projects: [project]
)
]
fields
end
- let(:type) { create(:type_task, custom_fields:) }
+ let(:type) { create(:type_task) }
let(:project) { create(:project, types: [type]) }
- let(:work_package) do
+ let!(:work_package) do
create(:work_package,
subject: 'Foobar',
status: status1,
@@ -146,12 +150,11 @@ RSpec.describe 'Inline editing work packages', js: true do
end
before do
- work_package
workflow
- # Require custom fields for this project
- project.work_package_custom_fields = custom_fields
- project.save!
+ custom_fields.each do |custom_field|
+ custom_field.update_attribute(:is_required, true)
+ end
wp_table.visit!
wp_table.expect_work_package_listed(work_package)
diff --git a/spec/lib/api/v3/work_packages/eager_loading/custom_value_integration_spec.rb b/spec/lib/api/v3/work_packages/eager_loading/custom_value_integration_spec.rb
deleted file mode 100644
index f903e35530a..00000000000
--- a/spec/lib/api/v3/work_packages/eager_loading/custom_value_integration_spec.rb
+++ /dev/null
@@ -1,191 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2023 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 'rspec'
-
-require 'spec_helper'
-require_relative './eager_loading_mock_wrapper'
-
-RSpec.describe API::V3::WorkPackages::EagerLoading::CustomValue do
- let!(:work_package) { create(:work_package) }
- let!(:type) { work_package.type }
- let!(:other_type) { create(:type) }
- let!(:project) { work_package.project }
- let!(:other_project) { create(:project) }
- let!(:user) { create(:user) }
- let!(:version) { create(:version, project:) }
-
- describe 'multiple CFs' do
- let!(:type_project_list_cf) do
- create(:list_wp_custom_field).tap do |cf|
- type.custom_fields << cf
- project.work_package_custom_fields << cf
- end
- end
- let!(:type_project_user_cf) do
- create(:user_wp_custom_field).tap do |cf|
- type.custom_fields << cf
- project.work_package_custom_fields << cf
- end
- end
- let!(:type_project_version_cf) do
- create(:version_wp_custom_field, name: 'blubs').tap do |cf|
- type.custom_fields << cf
- project.work_package_custom_fields << cf
- end
- end
- let!(:for_all_type_cf) do
- create(:list_wp_custom_field, is_for_all: true).tap do |cf|
- type.custom_fields << cf
- end
- end
- let!(:for_all_other_type_cf) do
- create(:list_wp_custom_field, is_for_all: true).tap do |cf|
- other_type.custom_fields << cf
- end
- end
- let!(:type_other_project_cf) do
- create(:list_wp_custom_field).tap do |cf|
- type.custom_fields << cf
- other_project.work_package_custom_fields << cf
- end
- end
- let!(:other_type_project_cf) do
- create(:list_wp_custom_field).tap do |cf|
- other_type.custom_fields << cf
- project.work_package_custom_fields << cf
- end
- end
-
- describe '.apply' do
- it 'preloads the custom fields and values' do
- create(:custom_value,
- custom_field: type_project_list_cf,
- customized: work_package,
- value: type_project_list_cf.custom_options.last.id)
-
- build(:custom_value,
- custom_field: type_project_user_cf,
- customized: work_package,
- value: user.id)
- .save(validate: false)
-
- create(:custom_value,
- custom_field: type_project_version_cf,
- customized: work_package,
- value: version.id)
-
- work_package = WorkPackage.first
- wrapped = EagerLoadingMockWrapper.wrap(described_class, [work_package])
-
- expect(type)
- .not_to receive(:custom_fields)
- expect(project)
- .not_to receive(:all_work_package_custom_fields)
-
- [CustomOption, User, Version].each do |klass|
- expect(klass)
- .not_to receive(:find_by)
- end
-
- wrapped.each do |w|
- expect(w.available_custom_fields)
- .to match_array [type_project_list_cf,
- type_project_version_cf,
- type_project_user_cf,
- for_all_type_cf]
-
- expect(work_package.send(type_project_version_cf.attribute_getter))
- .to eql version
- expect(work_package.send(type_project_list_cf.attribute_getter))
- .to eql type_project_list_cf.custom_options.last.name
- expect(work_package.send(type_project_user_cf.attribute_getter))
- .to eql user
- end
- end
- end
- end
-
- describe '#usages returning an is_for_all custom field within one project (Regression #28435)' do
- let!(:for_all_type_cf) do
- create(:list_wp_custom_field, is_for_all: true).tap do |cf|
- type.custom_fields << cf
- end
- end
- let(:other_project) { create(:project) }
-
- subject { described_class.new [work_package] }
-
- before do
- # Assume that one custom field has an entry in project_custom_fields
- for_all_type_cf.projects << other_project
- end
-
- it 'still allows looking up the global custom field in a different project' do
- # Exhibits the same behavior as in regression, usage returns a hash with project_id set for a global
- # custom field
- expect(for_all_type_cf.is_for_all).to be(true)
- expect(subject.send(:usages))
- .to include("project_id" => other_project.id, "type_id" => type.id, "custom_field_id" => for_all_type_cf.id)
-
- wrapped = EagerLoadingMockWrapper.wrap(described_class, [work_package])
- expect(wrapped.first.available_custom_fields).to include(for_all_type_cf)
- end
- end
-
- describe '#usages returning an is_for_all custom field within multiple projects (Regression #28452)' do
- let!(:for_all_type_cf) do
- create(:list_wp_custom_field, is_for_all: true).tap do |cf|
- type.custom_fields << cf
- end
- end
- let(:other_project) { create(:project) }
- let(:other_project2) { create(:project) }
-
- subject { described_class.new [work_package] }
-
- before do
- # Assume that one custom field has an entry in project_custom_fields
- for_all_type_cf.projects << other_project
- for_all_type_cf.projects << other_project2
- end
-
- it 'does not double add the custom field to the available CFs' do
- # Exhibits the same behavior as in regression, usage returns a hash with project_id set for a global
- # custom field
- expect(for_all_type_cf.is_for_all).to be(true)
- expect(subject.send(:usages))
- .to include("project_id" => other_project.id, "type_id" => type.id, "custom_field_id" => for_all_type_cf.id)
-
- expect(subject.send(:usages))
- .to include("project_id" => other_project2.id, "type_id" => type.id, "custom_field_id" => for_all_type_cf.id)
-
- wrapped = EagerLoadingMockWrapper.wrap(described_class, [work_package])
- expect(wrapped.first.available_custom_fields.length).to eq(1)
- expect(wrapped.first.available_custom_fields.to_a).to eq([for_all_type_cf])
- end
- end
-end
diff --git a/spec/models/query/results_spec.rb b/spec/models/query/results_spec.rb
index 981953f768a..7afe200e166 100644
--- a/spec/models/query/results_spec.rb
+++ b/spec/models/query/results_spec.rb
@@ -180,10 +180,8 @@ RSpec.describe Query::Results do
create(:list_wp_custom_field,
is_for_all: true,
is_filter: true,
- multi_value: true).tap do |cf|
- work_package1.type.custom_fields << cf
- work_package2.type.custom_fields << cf
- end
+ multi_value: true,
+ types: [work_package1.type, work_package2.type])
end
let(:first_value) do
custom_field.custom_options.first
@@ -218,7 +216,11 @@ RSpec.describe Query::Results do
context 'when grouping by int custom field' do
let!(:custom_field) do
- create(:int_wp_custom_field, is_for_all: true, is_filter: true)
+ create(:int_wp_custom_field,
+ is_for_all: true,
+ is_filter: true,
+ projects: [project1],
+ types: [wp_p1[0].type])
end
let(:group_by) { custom_field.column_name }
@@ -226,9 +228,6 @@ RSpec.describe Query::Results do
before do
login_as(user1)
- wp_p1[0].type.custom_fields << custom_field
- project1.work_package_custom_fields << custom_field
-
wp_p1[0].update_attribute(custom_field.attribute_name, 42)
wp_p1[0].save
wp_p1[1].update_attribute(custom_field.attribute_name, 42)
@@ -261,7 +260,11 @@ RSpec.describe Query::Results do
context 'when grouping by bool custom field' do
let!(:custom_field) do
- create(:bool_wp_custom_field, is_for_all: true, is_filter: true)
+ create(:bool_wp_custom_field,
+ is_for_all: true,
+ is_filter: true,
+ projects: [project1],
+ types: [wp_p1[0].type])
end
let(:group_by) { custom_field.column_name }
@@ -269,9 +272,6 @@ RSpec.describe Query::Results do
before do
login_as(user1)
- wp_p1[0].type.custom_fields << custom_field
- project1.work_package_custom_fields << custom_field
-
wp_p1[0].update_attribute(custom_field.attribute_name, true)
wp_p1[0].save
wp_p1[1].update_attribute(custom_field.attribute_name, true)
@@ -285,7 +285,11 @@ RSpec.describe Query::Results do
context 'when grouping by date custom field' do
let!(:custom_field) do
- create(:date_wp_custom_field, is_for_all: true, is_filter: true)
+ create(:date_wp_custom_field,
+ is_for_all: true,
+ is_filter: true,
+ projects: [project1],
+ types: [wp_p1[0].type])
end
let(:group_by) { custom_field.column_name }
@@ -293,9 +297,6 @@ RSpec.describe Query::Results do
before do
login_as(user1)
- wp_p1[0].type.custom_fields << custom_field
- project1.work_package_custom_fields << custom_field
-
wp_p1[0].update_attribute(custom_field.attribute_name, Time.zone.today)
wp_p1[0].save
wp_p1[1].update_attribute(custom_field.attribute_name, Time.zone.today)
@@ -483,7 +484,12 @@ RSpec.describe Query::Results do
column_names: columns)
end
- let(:bool_cf) { create(:bool_wp_custom_field, is_filter: true) }
+ let(:bool_cf) do
+ create(:bool_wp_custom_field,
+ is_filter: true,
+ projects: [work_package1.project],
+ types: [work_package1.type])
+ end
let(:custom_value) do
create(:custom_value,
custom_field: bool_cf,
@@ -492,13 +498,6 @@ RSpec.describe Query::Results do
end
let(:value) { 't' }
let(:filter_value) { 't' }
- let(:activate_cf) do
- work_package1.project.work_package_custom_fields << bool_cf
- work_package1.type.custom_fields << bool_cf
-
- work_package1.reload
- project1.reload
- end
let(:work_package1) { create(:work_package, project: project1) }
let(:work_package2) { create(:work_package, project: project1, id: 2) }
let(:work_package3) { create(:work_package, project: project1, id: 3) }
@@ -511,8 +510,6 @@ RSpec.describe Query::Results do
custom_value
- activate_cf
-
query.add_filter(bool_cf.column_name.to_sym, '=', [filter_value])
end
@@ -577,12 +574,10 @@ RSpec.describe Query::Results do
and the cf not being active for the type' do
let(:custom_value) { nil }
let(:filter_value) { 'f' }
-
- let(:activate_cf) do
- work_package1.type.custom_fields << bool_cf
-
- work_package1.reload
- project1.reload
+ let(:bool_cf) do
+ create(:bool_wp_custom_field,
+ is_filter: true,
+ types: [work_package1.type])
end
it_behaves_like 'is empty'
@@ -597,14 +592,8 @@ RSpec.describe Query::Results do
let(:bool_cf) do
create(:bool_wp_custom_field,
is_filter: true,
- is_for_all: true)
- end
-
- let(:activate_cf) do
- work_package1.project.work_package_custom_fields << bool_cf
-
- work_package1.reload
- project1.reload
+ is_for_all: true,
+ projects: [work_package1.project])
end
it_behaves_like 'is empty'
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 02365d7b67b..45a4df5bd95 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
@@ -48,6 +48,8 @@ RSpec.describe WorkPackage, 'acts_as_customizable' do
def setup_custom_field(cf)
project.work_package_custom_fields << cf
type.custom_fields << cf
+ # Void the custom field caching
+ RequestStore.clear!
end
describe '#custom_field_values=' do
@@ -158,4 +160,77 @@ RSpec.describe WorkPackage, 'acts_as_customizable' do
end
end
end
+
+ describe '.preload_available_custom_fields/#available_custom_fields' do
+ let(:project) { create(:project) }
+ let(:type) { create(:type) }
+ let(:work_package) do
+ build(:work_package,
+ project:,
+ type:)
+ end
+
+ let!(:custom_field_of_project_and_type) do
+ create(:work_package_custom_field,
+ name: 'Custom field of type and project').tap do |cf|
+ project.work_package_custom_fields << cf
+ type.custom_fields << cf
+ end
+ end
+ let!(:custom_field_of_project_not_type) do
+ create(:work_package_custom_field,
+ name: 'Custom field of project not type').tap do |cf|
+ project.work_package_custom_fields << cf
+ end
+ end
+ let!(:custom_field_of_type_not_project) do
+ create(:work_package_custom_field,
+ name: 'Custom field of type not project').tap do |cf|
+ type.custom_fields << cf
+ end
+ end
+ let!(:custom_field_for_all_and_type) do
+ create(:work_package_custom_field,
+ name: 'Custom field for all and type',
+ is_for_all: true).tap do |cf|
+ type.custom_fields << cf
+ end
+ end
+ let!(:custom_field_for_all_not_type) do
+ create(:work_package_custom_field,
+ name: 'Custom field for all not type',
+ is_for_all: true)
+ end
+
+ context 'when preloading the custom fields' do
+ before do
+ described_class.preload_available_custom_fields([work_package])
+ # Bad replacement to check that no database query is run.
+ allow(WorkPackageCustomField)
+ .to receive(:left_joins)
+ .and_call_original
+ end
+
+ it 'returns all custom fields of the project and type' do
+ expect(work_package.available_custom_fields)
+ .to contain_exactly(custom_field_of_project_and_type,
+ custom_field_for_all_and_type)
+ end
+
+ it 'does not call the database' do
+ work_package.available_custom_fields
+
+ expect(WorkPackageCustomField)
+ .not_to have_received(:left_joins)
+ end
+ end
+
+ context 'when not preloading the custom fields' do
+ it 'returns all custom fields of the project and type' do
+ expect(work_package.available_custom_fields)
+ .to contain_exactly(custom_field_of_project_and_type,
+ custom_field_for_all_and_type)
+ end
+ end
+ end
end
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 313d3cff188..e5b42e3c223 100644
--- a/spec/models/work_package/work_package_custom_fields_spec.rb
+++ b/spec/models/work_package/work_package_custom_fields_spec.rb
@@ -400,77 +400,4 @@ RSpec.describe WorkPackage do
end
end
end
-
- describe '.preload_available_custom_fields/#available_custom_fields' do
- let(:project) { create(:project) }
- let(:type) { create(:type) }
- let(:work_package) do
- build(:work_package,
- project:,
- type:)
- end
-
- let!(:custom_field_of_project_and_type) do
- create(:work_package_custom_field,
- name: 'Custom field of type and project').tap do |cf|
- project.work_package_custom_fields << cf
- type.custom_fields << cf
- end
- end
- let!(:custom_field_of_project_not_type) do
- create(:work_package_custom_field,
- name: 'Custom field of project not type').tap do |cf|
- project.work_package_custom_fields << cf
- end
- end
- let!(:custom_field_of_type_not_project) do
- create(:work_package_custom_field,
- name: 'Custom field of type not project').tap do |cf|
- type.custom_fields << cf
- end
- end
- let!(:custom_field_for_all_and_type) do
- create(:work_package_custom_field,
- name: 'Custom field for all and type',
- is_for_all: true).tap do |cf|
- type.custom_fields << cf
- end
- end
- let!(:custom_field_for_all_not_type) do
- create(:work_package_custom_field,
- name: 'Custom field for all not type',
- is_for_all: true)
- end
-
- context 'when preloading the custom fields' do
- before do
- described_class.preload_available_custom_fields([work_package])
- # Bad replacement to check that no database query is run.
- allow(WorkPackageCustomField)
- .to receive(:left_joins)
- .and_call_original
- end
-
- it 'returns all custom fields of the project and type' do
- expect(work_package.available_custom_fields)
- .to contain_exactly(custom_field_of_project_and_type,
- custom_field_for_all_and_type)
- end
-
- it 'does not call the database' do
- work_package.available_custom_fields
-
- expect(WorkPackageCustomField)
- .not_to have_received(:left_joins)
- end
- end
-
- context 'when not preloading the custom fields' do
- it 'returns all custom fields of the project and type' do
- expect(work_package.available_custom_fields)
- .to contain_exactly(custom_field_of_project_and_type,
- custom_field_for_all_and_type)
- end
- end
- end
end
diff --git a/spec/models/work_package/work_package_multi_value_custom_fields_spec.rb b/spec/models/work_package/work_package_multi_value_custom_fields_spec.rb
index 34ee917a899..2a2ce37d17d 100644
--- a/spec/models/work_package/work_package_multi_value_custom_fields_spec.rb
+++ b/spec/models/work_package/work_package_multi_value_custom_fields_spec.rb
@@ -65,7 +65,7 @@ RSpec.describe WorkPackage do
let(:typed_values) { work_package.typed_custom_value_for(custom_field.id) }
it 'returns the properly typed values' do
- expect(values.map { |cv| cv.value }).to eq(custom_values)
+ expect(values.map(&:value)).to eq(custom_values)
expect(typed_values).to eq(%w(ham onions pineapple))
end
@@ -73,7 +73,9 @@ RSpec.describe WorkPackage do
let(:work_package) { create(:work_package, project:, type:) }
it 'returns nil properly' do
- expect(values).to be_nil
+ # I suspect this should rather be
+ # expect(values.map(&:value)).to eq([nil])
+ expect(values.value).to be_nil
expect(typed_values).to be_nil
end
end
@@ -90,7 +92,7 @@ RSpec.describe WorkPackage do
work_package.custom_field_values = { custom_field.id => ids }
work_package.save
end
- .to(change { work_package.lock_version })
+ .to(change(work_package, :lock_version))
end
it 'sets the values' do
diff --git a/spec/services/projects/copy_service_integration_spec.rb b/spec/services/projects/copy_service_integration_spec.rb
index 0c0715dadb3..8abf4854f03 100644
--- a/spec/services/projects/copy_service_integration_spec.rb
+++ b/spec/services/projects/copy_service_integration_spec.rb
@@ -770,7 +770,8 @@ RSpec.describe Projects::CopyService, 'integration', type: :model, with_ee: %i[r
before do
custom_field
- work_package.reload
+ # Void the custom field caching
+ RequestStore.clear!
work_package.send(custom_field.attribute_setter, current_user.id)
work_package.save!(validate: false)
end