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
[#64348] Fix formula reference checking
Tobias Dillmann13f480549052
diff --git a/app/models/custom_field/calculated_value.rb b/app/models/custom_field/calculated_value.rbindex 63042683608..2e6d64fa23d 100644--- a/app/models/custom_field/calculated_value.rb+++ b/app/models/custom_field/calculated_value.rb@@ -77,8 +77,9 @@ module CustomField::CalculatedValue.where.not(id:).visible+ cache = {}visible_cfs.reject do |custom_field|- custom_field.formula_references_id?(id)+ custom_field.formula_references_id?(id, cache)endend@@ -97,19 +98,19 @@ module CustomField::CalculatedValueendend- def formula_references_id?(original_id, visited = Set.new)- return true unless visited.add?(id)+ def formula_references_id?(original_id, cache = {})+ cache.fetch(id) do+ if field_format_calculated_value?+ referenced_custom_fields = formula_referenced_custom_field_ids- if field_format_calculated_value?- referenced_custom_fields = formula_referenced_custom_field_ids+ next true if referenced_custom_fields.include?(original_id) || referenced_custom_fields.include?(id)- return true if referenced_custom_fields.include?(original_id)-- ProjectCustomField.where(id: referenced_custom_fields).any? do |referenced_field|- referenced_field.formula_references_id?(original_id, visited)+ ProjectCustomField.where(id: referenced_custom_fields).any? do |referenced_field|+ referenced_field.formula_references_id?(original_id, cache)+ end+ else+ falseend- else- falseendenddiff --git a/spec/models/custom_field/calculated_value_spec.rb b/spec/models/custom_field/calculated_value_spec.rbindex 10b16905dae..b781fec8514 100644--- a/spec/models/custom_field/calculated_value_spec.rb+++ b/spec/models/custom_field/calculated_value_spec.rb@@ -91,9 +91,9 @@ RSpec.describe CustomField::CalculatedValue, with_flag: { calculated_value_projeit "excludes fields that would create circular references" do# field_a should not be able to reference field_b, field_b should not be able to reference field_c, etc.- expect(field_a.usable_custom_field_references_for_formula).not_to include(field_b)- expect(field_b.usable_custom_field_references_for_formula).not_to include(field_c)- expect(field_c.usable_custom_field_references_for_formula).not_to include(field_a)+ expect(field_a.usable_custom_field_references_for_formula).not_to include(field_b, field_c)+ expect(field_b.usable_custom_field_references_for_formula).not_to include(field_c, field_a)+ expect(field_c.usable_custom_field_references_for_formula).not_to include(field_a, field_b)endit "still includes fields that don't create circular references" do@@ -103,6 +103,25 @@ RSpec.describe CustomField::CalculatedValue, with_flag: { calculated_value_projeendend+ context "when two calculated values reference the same custom field" do+ let!(:constant_cf1) { create(:project_custom_field, :integer, default_value: 1, is_for_all: true) }+ let!(:calculated_cf2) do+ create(:calculated_value_project_custom_field, formula: "{{cf_#{constant_cf1.id}}}", is_for_all: true)+ end+ let!(:calculated_cf3) do+ create(:calculated_value_project_custom_field,+ formula: "{{cf_#{constant_cf1.id}}} + {{cf_#{calculated_cf2.id}}}",+ is_for_all: true)+ end++ it "does not lead to a false positive" do+ subject.formula = "{{cf_#{calculated_cf3.id}}}"+ subject.save(validate: false)++ expect(subject.usable_custom_field_references_for_formula).to include(constant_cf1, calculated_cf2, calculated_cf3)+ end+ end+context "when there are self-referencing fields" dolet!(:self_referencing_field) { create(:calculated_value_project_custom_field, formula: "1 + 1", is_for_all: true) }@@ -288,6 +307,8 @@ RSpec.describe CustomField::CalculatedValue, with_flag: { calculated_value_projeit "returns false when there is no circular reference" doexpect(field_x.formula_references_id?(field_x.id)).to be false+ expect(field_x.formula_references_id?(field_y.id)).to be false+ expect(field_x.formula_references_id?(field_z.id)).to be falseexpect(field_y.formula_references_id?(field_y.id)).to be falseexpect(field_z.formula_references_id?(field_z.id)).to be falseend@@ -307,12 +328,12 @@ RSpec.describe CustomField::CalculatedValue, with_flag: { calculated_value_projeendit "returns true when a node has already been visited" do- visited = Set.new([field1.id])+ visited = { field1.id => true }expect(field1.formula_references_id?(field2.id, visited)).to be trueendit "returns false when checking a new node with empty visited set" do- visited = Set.new+ visited = {}expect(field1.formula_references_id?(field2.id, visited)).to be falseendend