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

respect visible setting and enriched specs

Jonas Jabari9330fecb36b4
diff --git a/app/contracts/project_custom_field_project_mappings/base_contract.rb b/app/contracts/project_custom_field_project_mappings/base_contract.rb
index fa50115f8bd..2e18e2beb56 100644
--- a/app/contracts/project_custom_field_project_mappings/base_contract.rb
+++ b/app/contracts/project_custom_field_project_mappings/base_contract.rb
@@ -33,6 +33,7 @@ module ProjectCustomFieldProjectMappings
validate :validate_has_select_project_custom_fields_permission
validate :validate_is_not_required
+ validate :validate_is_visbile_to_user
def validate_has_select_project_custom_fields_permission
return if user.allowed_in_project?(:select_project_custom_fields, model.project)
@@ -47,5 +48,13 @@ module ProjectCustomFieldProjectMappings
errors.add :custom_field_id, :invalid
end
+
+ def validate_is_visbile_to_user
+ # "invisible" custom fields can only be seen and edited by admins
+ # using visible scope to check if the custom field is actually visible to the user
+ return if model.project_custom_field.nil? || ProjectCustomField.visible.pluck(:id).include?(model.project_custom_field.id)
+
+ errors.add :custom_field_id, :invalid
+ end
end
end
diff --git a/app/controllers/projects/settings/project_custom_fields_controller.rb b/app/controllers/projects/settings/project_custom_fields_controller.rb
index 728fd88f106..441eb769410 100644
--- a/app/controllers/projects/settings/project_custom_fields_controller.rb
+++ b/app/controllers/projects/settings/project_custom_fields_controller.rb
@@ -90,6 +90,7 @@ class Projects::Settings::ProjectCustomFieldsController < Projects::SettingsCont
def eager_load_project_custom_fields
@project_custom_fields_grouped_by_section = ProjectCustomField
+ .visible
.includes(:project_custom_field_section)
.sort_by { |pcf| pcf.project_custom_field_section.position }
.group_by(&:custom_field_section_id)
diff --git a/app/models/projects/acts_as_customizable_patches.rb b/app/models/projects/acts_as_customizable_patches.rb
index 332487f950d..5780f1a7853 100644
--- a/app/models/projects/acts_as_customizable_patches.rb
+++ b/app/models/projects/acts_as_customizable_patches.rb
@@ -121,6 +121,7 @@ module Projects::ActsAsCustomizablePatches
# in contrast to acts_as_customizable, custom_fields are enabled per project
# thus we need to check the project_custom_field_project_mappings
custom_fields = ProjectCustomField
+ .visible
.includes(:project_custom_field_section)
# available_custom_fields is called from within the acts_as_customizable module
diff --git a/app/services/project_custom_field_project_mappings/bulk_edit_service.rb b/app/services/project_custom_field_project_mappings/bulk_edit_service.rb
index 0795b244a7f..35e2d195483 100644
--- a/app/services/project_custom_field_project_mappings/bulk_edit_service.rb
+++ b/app/services/project_custom_field_project_mappings/bulk_edit_service.rb
@@ -71,7 +71,11 @@ module ProjectCustomFieldProjectMappings
def fetch_custom_field_ids
# only custom fields which are not set to required can be disabled
- @project_custom_field_section.custom_fields.where(is_required: false).pluck(:id)
+ ProjectCustomField
+ .visible(@user)
+ .where(custom_field_section_id: @project_custom_field_section.id)
+ .where(is_required: false)
+ .pluck(:id)
end
def enable_custom_fields(custom_field_ids)
diff --git a/lib/api/v3/utilities/eager_loading/custom_field_accessor.rb b/lib/api/v3/utilities/eager_loading/custom_field_accessor.rb
index cae649d1fe2..8c61d88d8cb 100644
--- a/lib/api/v3/utilities/eager_loading/custom_field_accessor.rb
+++ b/lib/api/v3/utilities/eager_loading/custom_field_accessor.rb
@@ -44,7 +44,7 @@ module API
end
module CustomFieldAccessorPatch
- def available_custom_fields
+ def available_custom_fields(global: false)
@available_custom_fields
end
diff --git a/spec/features/projects/project_custom_fields/overview_page/dialog/render_spec.rb b/spec/features/projects/project_custom_fields/overview_page/dialog/render_spec.rb
index 86c4772d16f..8bf00e23429 100644
--- a/spec/features/projects/project_custom_fields/overview_page/dialog/render_spec.rb
+++ b/spec/features/projects/project_custom_fields/overview_page/dialog/render_spec.rb
@@ -139,4 +139,58 @@ RSpec.describe 'Edit project custom fields on project overview page', :js do
expect(containers[5].text).to include('Boolean field')
end
end
+
+ context 'with visibility of project custom fields' do
+ let!(:section_with_invisible_fields) { create(:project_custom_field_section, name: 'Section with invisible fields') }
+
+ let!(:visible_project_custom_field) do
+ create(:project_custom_field,
+ name: 'Normal field',
+ visible: true,
+ projects: [project],
+ project_custom_field_section: section_with_invisible_fields)
+ end
+
+ let!(:invisible_project_custom_field) do
+ create(:project_custom_field,
+ name: 'Admin only field',
+ visible: false,
+ projects: [project],
+ project_custom_field_section: section_with_invisible_fields)
+ end
+
+ let(:dialog) { Components::Projects::ProjectCustomFields::EditDialog.new(project, section_with_invisible_fields) }
+
+ context 'with admin permissions' do
+ before do
+ login_as admin
+ overview_page.visit_page
+ end
+
+ it 'shows all project custom fields' do
+ overview_page.open_edit_dialog_for_section(section_with_invisible_fields)
+
+ dialog.within_async_content(close_after_yield: true) do
+ expect(page).to have_content('Normal field')
+ expect(page).to have_content('Admin only field')
+ end
+ end
+ end
+
+ context 'with non-admin permissions' do
+ before do
+ login_as member_with_project_edit_permissions
+ overview_page.visit_page
+ end
+
+ it 'shows only visible project custom fields' do
+ overview_page.open_edit_dialog_for_section(section_with_invisible_fields)
+
+ dialog.within_async_content(close_after_yield: true) do
+ expect(page).to have_content('Normal field')
+ expect(page).to have_no_content('Admin only field')
+ end
+ end
+ end
+ end
end
diff --git a/spec/features/projects/project_custom_fields/settings/mapping_spec.rb b/spec/features/projects/project_custom_fields/settings/mapping_spec.rb
index f2d434d71c0..61db89df1e0 100644
--- a/spec/features/projects/project_custom_fields/settings/mapping_spec.rb
+++ b/spec/features/projects/project_custom_fields/settings/mapping_spec.rb
@@ -323,6 +323,105 @@ RSpec.describe 'Projects custom fields mapping via project settings', :js, :with
expect(custom_fields[1].text).to include('Boolean field')
end
end
+
+ context 'with visibility of project custom fields' do
+ let!(:section_with_invisible_fields) { create(:project_custom_field_section, name: 'Section with invisible fields') }
+
+ let!(:visible_project_custom_field) do
+ create(:project_custom_field,
+ name: 'Normal field',
+ visible: true,
+ projects: [project],
+ project_custom_field_section: section_with_invisible_fields)
+ end
+
+ let!(:invisible_project_custom_field) do
+ create(:project_custom_field,
+ name: 'Admin only field',
+ visible: false,
+ projects: [project],
+ project_custom_field_section: section_with_invisible_fields)
+ end
+
+ context 'with admin permissions' do
+ let!(:admin) do
+ create(:admin)
+ end
+
+ before do
+ login_as admin
+ visit project_settings_project_custom_fields_path(project)
+ end
+
+ it 'shows the invisible project custom fields' do
+ within_custom_field_section_container(section_with_invisible_fields) do
+ expect(page).to have_content('Normal field')
+ expect(page).to have_content('Admin only field')
+ end
+ end
+
+ it 'includeds the invisible project custom fields in the bulk actions' do
+ within_custom_field_section_container(section_with_invisible_fields) do
+ page.find("[data-qa-selector='disable-all-project-custom-field-mappings-#{section_with_invisible_fields.id}']").click
+
+ within_custom_field_container(visible_project_custom_field) do
+ expect_unchecked_state
+ end
+ within_custom_field_container(invisible_project_custom_field) do
+ expect_unchecked_state
+ end
+
+ page.find("[data-qa-selector='enable-all-project-custom-field-mappings-#{section_with_invisible_fields.id}']").click
+
+ within_custom_field_container(visible_project_custom_field) do
+ expect_checked_state
+ end
+ within_custom_field_container(invisible_project_custom_field) do
+ expect_checked_state
+ end
+ end
+ end
+ end
+
+ context 'with non-admin permissions' do
+ before do
+ login_as user_with_sufficient_permissions
+ visit project_settings_project_custom_fields_path(project)
+ end
+
+ it 'does not show the invisible project custom fields' do
+ within_custom_field_section_container(section_with_invisible_fields) do
+ expect(page).to have_content('Normal field')
+ expect(page).to have_no_content('Admin only field')
+ end
+ end
+
+ it 'does not include the invisible project custom fields in the bulk actions' do
+ within_custom_field_section_container(section_with_invisible_fields) do
+ page.find("[data-qa-selector='disable-all-project-custom-field-mappings-#{section_with_invisible_fields.id}']").click
+
+ within_custom_field_container(visible_project_custom_field) do
+ expect_unchecked_state
+ end
+
+ # the invisible field is not affected by the bulk action
+ expect(project.project_custom_fields).to include(invisible_project_custom_field)
+
+ # disable manually
+ project.project_custom_field_project_mappings.find_by(custom_field_id: invisible_project_custom_field.id).destroy!
+
+ page.find("[data-qa-selector='enable-all-project-custom-field-mappings-#{section_with_invisible_fields.id}']").click
+
+ within_custom_field_container(visible_project_custom_field) do
+ expect_checked_state
+ end
+
+ # the invisible field is not affected by the bulk action
+ expect(project.project_custom_fields).not_to include(invisible_project_custom_field)
+ end
+ end
+ end
+ end
end
def expect_type(type)
diff --git a/spec/requests/api/v3/projects/create_resource_spec.rb b/spec/requests/api/v3/projects/create_resource_spec.rb
index 3be223c3fc5..0ff82ecfcbd 100644
--- a/spec/requests/api/v3/projects/create_resource_spec.rb
+++ b/spec/requests/api/v3/projects/create_resource_spec.rb
@@ -36,6 +36,9 @@ RSpec.describe 'API v3 Project resource create', content_type: :json do
let(:custom_field) do
create(:text_project_custom_field)
end
+ let(:invisible_custom_field) do
+ create(:text_project_custom_field, visible: false)
+ end
let(:custom_value) do
CustomValue.create(custom_field:,
value: '1234',
@@ -113,7 +116,7 @@ RSpec.describe 'API v3 Project resource create', content_type: :json do
end
end
- context 'with a custom field' do
+ context 'with a visible custom field' do
let(:body) do
{
identifier: 'new_project_identifier',
@@ -136,6 +139,45 @@ RSpec.describe 'API v3 Project resource create', content_type: :json do
end
end
+ context 'with an invisible custom field' do
+ let(:body) do
+ {
+ identifier: 'new_project_identifier',
+ name: 'Project name',
+ invisible_custom_field.attribute_name(:camel_case) => {
+ raw: "CF text"
+ }
+ }.to_json
+ end
+
+ context 'with admin permissions' do
+ current_user { create(:admin) }
+
+ it 'sets the cf value' do
+ expect(last_response.body)
+ .to be_json_eql("CF text".to_json)
+ .at_path("customField#{invisible_custom_field.id}/raw")
+ end
+
+ it 'automatically activates the cf for project if the value was provided' do
+ expect(Project.last.project_custom_fields)
+ .to contain_exactly(invisible_custom_field)
+ end
+ end
+
+ context 'with non-admin permissions' do
+ it 'does not set the cf value' do
+ expect(last_response.body)
+ .not_to have_json_path("customField#{invisible_custom_field.id}/raw")
+ end
+
+ it 'does not activate the cf for project' do
+ expect(Project.last.project_custom_fields)
+ .to be_empty
+ end
+ end
+ end
+
context 'without permission to create projects' do
let(:permissions) { [] }
diff --git a/spec/requests/api/v3/projects/show_resource_spec.rb b/spec/requests/api/v3/projects/show_resource_spec.rb
index a27fd4958c8..7fe2ac10cab 100644
--- a/spec/requests/api/v3/projects/show_resource_spec.rb
+++ b/spec/requests/api/v3/projects/show_resource_spec.rb
@@ -53,6 +53,14 @@ RSpec.describe 'API v3 Project resource show', content_type: :json do
value: '1234',
customized: project)
end
+ let(:invisible_custom_field) do
+ create(:text_project_custom_field, visible: false)
+ end
+ let(:invisible_custom_value) do
+ CustomValue.create(custom_field: invisible_custom_field,
+ value: '1234',
+ customized: project)
+ end
let(:get_path) { api_v3_paths.project project.id }
let!(:parent_project) do
@@ -96,12 +104,33 @@ RSpec.describe 'API v3 Project resource show', content_type: :json do
.at_path('_links/ancestors/0/href')
end
- it 'includes custom fields' do
+ it 'includes only visible custom fields' do
custom_value
+ invisible_custom_value
expect(subject.body)
.to be_json_eql(custom_value.value.to_json)
.at_path("customField#{custom_field.id}/raw")
+
+ expect(subject.body)
+ .not_to have_json_path("customField#{invisible_custom_field.id}/raw")
+ end
+
+ context 'with admin permissions' do
+ current_user { admin }
+
+ it 'includes invisible custom fields' do
+ custom_value
+ invisible_custom_value
+
+ expect(subject.body)
+ .to be_json_eql(custom_value.value.to_json)
+ .at_path("customField#{custom_field.id}/raw")
+
+ expect(subject.body)
+ .to be_json_eql(invisible_custom_value.value.to_json)
+ .at_path("customField#{invisible_custom_field.id}/raw")
+ end
end
it 'includes the project status' do
diff --git a/spec/requests/api/v3/projects/update_resource_spec.rb b/spec/requests/api/v3/projects/update_resource_spec.rb
index e4cf940bc81..fe1a05c6b21 100644
--- a/spec/requests/api/v3/projects/update_resource_spec.rb
+++ b/spec/requests/api/v3/projects/update_resource_spec.rb
@@ -44,6 +44,9 @@ RSpec.describe 'API v3 Project resource update', content_type: :json do
let(:custom_field) do
create(:text_project_custom_field)
end
+ let(:invisible_custom_field) do
+ create(:text_project_custom_field, visible: false)
+ end
let(:custom_value) do
CustomValue.create(custom_field:,
value: '1234',
@@ -89,7 +92,7 @@ RSpec.describe 'API v3 Project resource update', content_type: :json do
.at_path('name')
end
- context 'with a custom field' do
+ context 'with a visible custom field' do
let(:body) do
{
custom_field.attribute_name(:camel_case) => {
@@ -113,6 +116,51 @@ RSpec.describe 'API v3 Project resource update', content_type: :json do
end
end
+ context 'with an invisible custom field' do
+ let(:body) do
+ {
+ invisible_custom_field.attribute_name(:camel_case) => {
+ raw: "CF text"
+ }
+ }
+ end
+
+ context 'with admin permissions' do
+ let(:current_user) { create(:admin) }
+
+ it 'responds with 200 OK' do
+ expect(last_response.status).to eq(200)
+ end
+
+ it 'sets the cf value' do
+ expect(project.reload.send(invisible_custom_field.attribute_getter))
+ .to eql("CF text")
+ end
+
+ it 'automatically activates the cf for project if the value was provided' do
+ expect(project.reload.project_custom_fields)
+ .to contain_exactly(invisible_custom_field)
+ end
+ end
+
+ context 'with non-admin permissions' do
+ it 'responds with 200 OK' do
+ # TBD: trying to set a not accessible custom field is silently ignored
+ expect(last_response.status).to eq(200)
+ end
+
+ it 'does not set the cf value' do
+ expect(project.reload.custom_values)
+ .to be_empty
+ end
+
+ it 'does not activate the cf for project' do
+ expect(project.reload.project_custom_fields)
+ .to be_empty
+ end
+ end
+ end
+
context 'without permission to patch projects' do
let(:permissions) { [] }
diff --git a/spec/services/project_custom_field_project_mappings/bulk_edit_service_spec.rb b/spec/services/project_custom_field_project_mappings/bulk_edit_service_spec.rb
new file mode 100644
index 00000000000..b525697144e
--- /dev/null
+++ b/spec/services/project_custom_field_project_mappings/bulk_edit_service_spec.rb
@@ -0,0 +1,145 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+RSpec.describe ProjectCustomFieldProjectMappings::BulkEditService do
+ let!(:project) { create(:project) }
+ let!(:section_with_invisible_fields) { create(:project_custom_field_section, name: 'Section with invisible fields') }
+
+ let!(:visible_project_custom_field) do
+ create(:project_custom_field,
+ name: 'Visible field',
+ visible: true,
+ project_custom_field_section: section_with_invisible_fields)
+ end
+
+ let!(:visible_required_project_custom_field) do
+ create(:project_custom_field,
+ name: 'Visible required field',
+ visible: true,
+ is_required: true,
+ project_custom_field_section: section_with_invisible_fields)
+ end
+
+ let!(:invisible_project_custom_field) do
+ create(:project_custom_field,
+ name: 'Admin only field',
+ visible: false,
+ project_custom_field_section: section_with_invisible_fields)
+ end
+
+ let(:instance) { described_class.new(user:, project:, project_custom_field_section: section_with_invisible_fields) }
+
+ context 'with admin permissions' do
+ let(:user) { create(:admin) }
+
+ it 'bulk enables/disables all (non-required) fields of the section, including invisible ones' do
+ expect(project.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+
+ expect(instance.call(action: :enable)).to be_success
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field, visible_project_custom_field, invisible_project_custom_field
+ )
+
+ expect(instance.call(action: :disable)).to be_success
+
+ # required fields cannot be disabled, even not by admins
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+ end
+ end
+
+ context 'with non-admin but sufficient permissions' do
+ let(:user) do
+ create(:user,
+ firstname: 'Project',
+ lastname: 'Admin',
+ member_with_permissions: {
+ project => %w[
+ view_work_packages
+ edit_project
+ select_project_custom_fields
+ ]
+ })
+ end
+
+ it 'bulk enables/disables all fields of the section, excluding invisible ones' do
+ expect(project.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+
+ expect(instance.call(action: :enable)).to be_success
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field, visible_project_custom_field
+ )
+
+ project.project_custom_fields << invisible_project_custom_field
+
+ expect(instance.call(action: :disable)).to be_success
+
+ # required fields cannot be disabled, invisible fields are not affected by non-admins
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field, invisible_project_custom_field
+ )
+ end
+ end
+
+ context 'with insufficient permissions' do
+ let(:user) do
+ create(:user,
+ firstname: 'Project',
+ lastname: 'Editor',
+ member_with_permissions: {
+ project => %w[
+ view_work_packages
+ edit_project
+ ]
+ })
+ end
+
+ it 'cannot bulk enable/disable project custom fields' do
+ expect(project.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+
+ expect(instance.call(action: :enable)).to be_failure
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+
+ expect(instance.call(action: :disable)).to be_failure
+ end
+ end
+end
diff --git a/spec/services/project_custom_field_project_mappings/toggle_service_spec.rb b/spec/services/project_custom_field_project_mappings/toggle_service_spec.rb
new file mode 100644
index 00000000000..6b175eb9abb
--- /dev/null
+++ b/spec/services/project_custom_field_project_mappings/toggle_service_spec.rb
@@ -0,0 +1,217 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+RSpec.describe ProjectCustomFieldProjectMappings::ToggleService do
+ let!(:project) { create(:project) }
+ let!(:section_with_invisible_fields) { create(:project_custom_field_section, name: 'Section with invisible fields') }
+
+ let!(:visible_project_custom_field) do
+ create(:project_custom_field,
+ name: 'Visible field',
+ visible: true,
+ project_custom_field_section: section_with_invisible_fields)
+ end
+
+ let!(:visible_required_project_custom_field) do
+ create(:project_custom_field,
+ name: 'Visible required field',
+ visible: true,
+ is_required: true,
+ project_custom_field_section: section_with_invisible_fields)
+ end
+
+ let!(:invisible_project_custom_field) do
+ create(:project_custom_field,
+ name: 'Admin only field',
+ visible: false,
+ project_custom_field_section: section_with_invisible_fields)
+ end
+
+ let(:instance) { described_class.new(user:) }
+
+ context 'with admin permissions' do
+ let(:user) { create(:admin) }
+
+ it 'toggles visible, non-required fields' do
+ expect(project.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+
+ expect(instance.call(project_id: project.id, custom_field_id: visible_project_custom_field.id)).to be_success
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field, visible_project_custom_field
+ )
+
+ expect(instance.call(project_id: project.id, custom_field_id: visible_project_custom_field.id)).to be_success
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+ end
+
+ it 'toggles invisible, non-required fields' do
+ expect(project.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+
+ expect(instance.call(project_id: project.id, custom_field_id: invisible_project_custom_field.id)).to be_success
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field, invisible_project_custom_field
+ )
+
+ expect(instance.call(project_id: project.id, custom_field_id: invisible_project_custom_field.id)).to be_success
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+ end
+
+ it 'does not toggle required fields' do
+ expect(project.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+
+ expect(instance.call(project_id: project.id, custom_field_id: visible_required_project_custom_field.id)).to be_failure
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+ end
+ end
+
+ context 'with non-admin but sufficient permissions' do
+ let(:user) do
+ create(:user,
+ firstname: 'Project',
+ lastname: 'Admin',
+ member_with_permissions: {
+ project => %w[
+ view_work_packages
+ edit_project
+ select_project_custom_fields
+ ]
+ })
+ end
+
+ it 'toggles visible, non-required fields' do
+ expect(project.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+
+ expect(instance.call(project_id: project.id, custom_field_id: visible_project_custom_field.id)).to be_success
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field, visible_project_custom_field
+ )
+
+ expect(instance.call(project_id: project.id, custom_field_id: visible_project_custom_field.id)).to be_success
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+ end
+
+ it 'does not toggle invisible, non-required fields' do
+ expect(project.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+
+ expect(instance.call(project_id: project.id, custom_field_id: invisible_project_custom_field.id)).to be_failure
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+ end
+
+ it 'does not toggle required fields' do
+ expect(project.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+
+ expect(instance.call(project_id: project.id, custom_field_id: visible_required_project_custom_field.id)).to be_failure
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+ end
+ end
+
+ context 'with insufficient permissions' do
+ let(:user) do
+ create(:user,
+ firstname: 'Project',
+ lastname: 'Editor',
+ member_with_permissions: {
+ project => %w[
+ view_work_packages
+ edit_project
+ ]
+ })
+ end
+
+ it 'toggles visible, non-required fields' do
+ expect(project.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+
+ expect(instance.call(project_id: project.id, custom_field_id: visible_project_custom_field.id)).to be_failure
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+ end
+
+ it 'does not toggle invisible, non-required fields' do
+ expect(project.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+
+ expect(instance.call(project_id: project.id, custom_field_id: invisible_project_custom_field.id)).to be_failure
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+ end
+
+ it 'does not toggle required fields' do
+ expect(project.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+
+ expect(instance.call(project_id: project.id, custom_field_id: visible_required_project_custom_field.id)).to be_failure
+
+ expect(project.reload.project_custom_fields).to contain_exactly(
+ visible_required_project_custom_field
+ )
+ end
+ end
+end
diff --git a/spec/support/pages/projects/show.rb b/spec/support/pages/projects/show.rb
index be5c23304d1..5ad63c54282 100644
--- a/spec/support/pages/projects/show.rb
+++ b/spec/support/pages/projects/show.rb
@@ -72,6 +72,7 @@ module Pages
def open_edit_dialog_for_section(section)
within_async_loaded_sidebar do
+ scroll_to_element(page.find("[data-qa-selector='project-custom-field-section-#{section.id}']"))
within_custom_field_section_container(section) do
page.find("[data-qa-selector='project-custom-field-section-edit-button']").click
end