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
ensure affected_calculated_fields doesn't consider ids which are not in scope
Ivan Kuchin2a593b10bafa
diff --git a/app/models/custom_field/calculated_value.rb b/app/models/custom_field/calculated_value.rbindex 50f9f8c6db8..0aa398c7775 100644--- a/app/models/custom_field/calculated_value.rb+++ b/app/models/custom_field/calculated_value.rb@@ -50,6 +50,9 @@ module CustomField::CalculatedValuedef affected_calculated_fields(changed_cf_ids)return [] if changed_cf_ids.empty?+ # exclude ids that are not in the list+ changed_cf_ids = where(id: changed_cf_ids).pluck(:id)+to_check = field_format_calculated_value# include calculated value fields themselvesdiff --git a/spec/models/custom_field/calculated_value_spec.rb b/spec/models/custom_field/calculated_value_spec.rbindex 3c41b5ff157..ece7ff147f9 100644--- a/spec/models/custom_field/calculated_value_spec.rb+++ b/spec/models/custom_field/calculated_value_spec.rb@@ -33,7 +33,7 @@ require "spec_helper"RSpec.describe CustomField::CalculatedValue, with_flag: { calculated_value_project_attribute: true } dosubject(:custom_field) { create(:calculated_value_project_custom_field, formula: "1 + 1") }- describe ".affected_calculated_fields" do+ describe ".affected_calculated_fields", :aggregate_failures dolet(:scope) { CustomField }def ref(custom_field) = "{{cf_#{custom_field.id}}}"@@ -87,9 +87,9 @@ RSpec.describe CustomField::CalculatedValue, with_flag: { calculated_value_projeendcontext "when scope doesn't include some values" do- let(:scope) { CustomField.where(id: [cf2, cf3]) }+ let(:scope) { CustomField.where.not(id: [cf_c, cf1]) }- it "receives referencing fields when passing referenced constant field ids" do+ it "returns referencing fields when passing referenced constant field ids if both are in scope" doexpect(scope.affected_calculated_fields([cf_a.id])).to contain_exactly(cf2)expect(scope.affected_calculated_fields([cf_b.id])).to contain_exactly(cf2, cf3)expect(scope.affected_calculated_fields([cf_c.id])).to be_empty@@ -99,11 +99,11 @@ RSpec.describe CustomField::CalculatedValue, with_flag: { calculated_value_projeexpect(scope.affected_calculated_fields([cf_a.id, cf_b.id, cf_c.id])).to contain_exactly(cf2, cf3)end- it "receives fields themselves when passing calculated field ids" do+ it "returns fields themselves if they are in scope when passing calculated field ids" doexpect(scope.affected_calculated_fields([cf1.id])).to be_emptyexpect(scope.affected_calculated_fields([cf2.id])).to contain_exactly(cf2)expect(scope.affected_calculated_fields([cf3.id])).to contain_exactly(cf3)- expect(scope.affected_calculated_fields([cf4.id])).to be_empty+ expect(scope.affected_calculated_fields([cf4.id])).to contain_exactly(cf4)endendend@@ -118,26 +118,44 @@ RSpec.describe CustomField::CalculatedValue, with_flag: { calculated_value_projeshared_let(:cf2) { create(:calculated_value_project_custom_field, :skip_validations, formula: "#{ref cf1} * #{ref cf_c}") }shared_let(:cf3) { create(:calculated_value_project_custom_field, :skip_validations, formula: "#{ref cf2} * #{ref cf_d}") }- it "receives referencing fields when passing referenced constant field ids" do+ it "returns referencing fields when passing referenced constant field ids" doexpect(scope.affected_calculated_fields([cf_a.id])).to contain_exactly(cf1, cf2, cf3)expect(scope.affected_calculated_fields([cf_b.id])).to contain_exactly(cf1, cf2, cf3)expect(scope.affected_calculated_fields([cf_c.id])).to contain_exactly(cf2, cf3)expect(scope.affected_calculated_fields([cf_d.id])).to contain_exactly(cf3)end+ it "returns fields themselves and referencing fields when passing calculated field ids" do+ expect(scope.affected_calculated_fields([cf1.id])).to contain_exactly(cf1, cf2, cf3)+ expect(scope.affected_calculated_fields([cf2.id])).to contain_exactly(cf2, cf3)+ expect(scope.affected_calculated_fields([cf3.id])).to contain_exactly(cf3)+ end+context "when scope doesn't include some values" do- let(:scope) { CustomField.where(id: [cf1, cf3]) }+ let(:scope) { CustomField.where.not(id: cf2) }- it "receives referencing fields when passing referenced constant field ids" do+ it "returns referencing fields when passing referenced constant field ids if whole paths are in scope" doexpect(scope.affected_calculated_fields([cf_a.id])).to contain_exactly(cf1)expect(scope.affected_calculated_fields([cf_b.id])).to contain_exactly(cf1)expect(scope.affected_calculated_fields([cf_c.id])).to be_emptyexpect(scope.affected_calculated_fields([cf_d.id])).to contain_exactly(cf3)+ expect(scope.affected_calculated_fields([cf_a.id, cf_b.id])).to contain_exactly(cf1)+ expect(scope.affected_calculated_fields([cf_a.id, cf_c.id])).to contain_exactly(cf1)+ expect(scope.affected_calculated_fields([cf_a.id, cf_d.id])).to contain_exactly(cf1, cf3)+ expect(scope.affected_calculated_fields([cf_b.id, cf_c.id])).to contain_exactly(cf1)+ expect(scope.affected_calculated_fields([cf_b.id, cf_d.id])).to contain_exactly(cf1, cf3)+ expect(scope.affected_calculated_fields([cf_c.id, cf_d.id])).to contain_exactly(cf3)+ end++ it "returns fields themselves if they are in scope when passing calculated field ids" do+ expect(scope.affected_calculated_fields([cf1.id])).to contain_exactly(cf1)+ expect(scope.affected_calculated_fields([cf2.id])).to be_empty+ expect(scope.affected_calculated_fields([cf3.id])).to contain_exactly(cf3)endendend- context "if given formulas with circular references" do+ context "given formulas with circular references" dolet!(:cf_a) { create(:integer_project_custom_field, default_value: 1) }let!(:cf_b) { create(:integer_project_custom_field, default_value: 2) }let!(:cf_c) { create(:integer_project_custom_field, default_value: 3) }