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

[#66471] Enable calculation of admin only calculated values on change of non admin only field values by non admins fix

Ivan Kuchin2230539bc0ba
diff --git a/app/services/projects/concerns/set_calculated_custom_field_values.rb b/app/services/projects/concerns/set_calculated_custom_field_values.rb
index 28a246a46d2..b886d9cd9ee 100644
--- a/app/services/projects/concerns/set_calculated_custom_field_values.rb
+++ b/app/services/projects/concerns/set_calculated_custom_field_values.rb
@@ -63,7 +63,9 @@ module Projects::Concerns
# admin_only check.
affected_cfs = model.available_custom_fields.unscope(where: :admin_only).affected_calculated_fields(changed_cf_ids)
- model.calculate_custom_fields(affected_cfs)
+ model.change_by_system do
+ model.calculate_custom_fields(affected_cfs)
+ end
end
end
end
diff --git a/spec/features/projects/project_custom_fields/overview_page/dialog/update_spec.rb b/spec/features/projects/project_custom_fields/overview_page/dialog/update_spec.rb
index 63b33297968..bd3bdbf7981 100644
--- a/spec/features/projects/project_custom_fields/overview_page/dialog/update_spec.rb
+++ b/spec/features/projects/project_custom_fields/overview_page/dialog/update_spec.rb
@@ -759,7 +759,7 @@ RSpec.describe "Edit project custom fields on project overview page", :js do
let(:field) { FormFields::Primerized::InputField.new(custom_field) }
before do
- all_fields.without(string_project_custom_field).each { |cf| cf.update(admin_only: true) }
+ all_fields.without(string_project_custom_field).each { it.update_attribute!(:admin_only, true) }
end
it "does not clears them after a project admin updates" do
@@ -803,5 +803,48 @@ RSpec.describe "Edit project custom fields on project overview page", :js do
expect(project.project_custom_fields.reload).to eq(expected_custom_fields)
end
end
+
+ describe "with hidden calculated value CFs" do
+ let(:section) { section_for_input_fields }
+ let(:dialog) { Components::Projects::ProjectCustomFields::EditDialog.new(project, section) }
+ let(:custom_field) { integer_project_custom_field }
+ let(:field) { FormFields::Primerized::InputField.new(custom_field) }
+
+ before do
+ [
+ float_project_custom_field,
+ calculated_from_int_project_custom_field,
+ calculated_from_int_and_float_project_custom_field
+ ].each { it.update_attribute!(:admin_only, true) }
+ end
+
+ it "updates them when visible referencing field changes" do
+ # Checking that calculated value fields can be calculated even if they
+ # are hidden from the user changing referenced field values
+ # https://community.openproject.org/wp/66471
+
+ overview_page.visit_page
+
+ overview_page.open_edit_dialog_for_section(section)
+
+ field.fill_in(with: 567)
+
+ dialog.submit
+ dialog.expect_closed
+
+ overview_page.within_custom_field_container(custom_field) do
+ expect(page).to have_content "567"
+ end
+ overview_page.expect_no_custom_field(float_project_custom_field)
+ overview_page.expect_no_custom_field(calculated_from_int_project_custom_field)
+ overview_page.expect_no_custom_field(calculated_from_int_and_float_project_custom_field)
+
+ expect(project.reload.custom_value_attributes(all: true)).to include(
+ float_project_custom_field.id => "123.456",
+ calculated_from_int_project_custom_field.id => "1134",
+ calculated_from_int_and_float_project_custom_field.id => "69999.552"
+ )
+ end
+ end
end
end
diff --git a/spec/support/pages/projects/show.rb b/spec/support/pages/projects/show.rb
index 74d43b7b8c5..d25e8b75001 100644
--- a/spec/support/pages/projects/show.rb
+++ b/spec/support/pages/projects/show.rb
@@ -76,6 +76,10 @@ module Pages
within("[data-test-selector='project-custom-field-#{custom_field.id}']", &)
end
+ def expect_no_custom_field(custom_field)
+ expect(page).to have_no_css("[data-test-selector='project-custom-field-#{custom_field.id}']")
+ end
+
def open_edit_dialog_for_section(section)
within_project_attributes_sidebar do
scroll_to_element(page.find("[data-test-selector='project-custom-field-section-#{section.id}']"))