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
[#66093] Fix bug for nested error creation
Tobias Dillmann9bbe70f6d933
diff --git a/app/models/acts_as_customizable/calculated_value.rb b/app/models/acts_as_customizable/calculated_value.rbindex 9881fe29be6..8e94d006635 100644--- a/app/models/acts_as_customizable/calculated_value.rb+++ b/app/models/acts_as_customizable/calculated_value.rb@@ -108,7 +108,7 @@ module ActsAsCustomizable::CalculatedValueif unsuccessfully_calculated_cfs.any?# There are multiple reasons why a calculation could not complete:# 1. The value of a referenced custom field is missing (nil)- cvs_with_errors.concat(create_errors_for_missing_attributes(given_cfs, enabled_calculated_fields))+ cvs_with_errors.concat(create_errors_for_missing_attributes(given_cfs, enabled_calculated_fields, result))# 2. A referenced custom field is disabled (not present in the enabled_ids list)disabled_errors = create_errors_for_disabled_attributes(calculated_fields_without_value, enabled_ids)@@ -133,14 +133,14 @@ module ActsAsCustomizable::CalculatedValueCalculatedValueError.create(project: self, custom_field_id:, error_code:, missing_custom_field_ids:)end- def create_errors_for_missing_attributes(referenced_values, calculated_fields)+ def create_errors_for_missing_attributes(referenced_values, calculated_fields, result)errors_created = []cf_ids_with_missing_values = referenced_values.filter_map { |k, v| to_id(k) if v.nil? }calculated_fields.each do |cv|if handle_missing_value_error(cv, cf_ids_with_missing_values) ||- handle_indirectly_missing_value_error(cv, calculated_fields)+ handle_indirectly_missing_value_error(cv, calculated_fields, result)errors_created << cv.idendend@@ -180,8 +180,11 @@ module ActsAsCustomizable::CalculatedValue# Creates an error if a value that is indirectly required for the calculation is missing.# This is true for example for a formula like `2 + {{cf_12}}` where `cf_12` is a calculated value itself# that cannot be calculated because it references a custom field with no value set.- def handle_indirectly_missing_value_error(calculated_value, calculated_fields)- indirectly_missing = calculated_value.formula_referenced_custom_field_ids.filter do |ref_id|+ def handle_indirectly_missing_value_error(calculated_value, calculated_fields, result)+ cf_ids_with_results = result.filter_map { |cf_id, v| to_id(cf_id) unless v.nil? }+ cf_ids_in_formula_without_result = calculated_value.formula_referenced_custom_field_ids - cf_ids_with_results++ indirectly_missing = cf_ids_in_formula_without_result.filter do |ref_id|calculated_fields.any? { it.id == ref_id }enddiff --git a/spec/models/acts_as_customizable/calculated_value_spec.rb b/spec/models/acts_as_customizable/calculated_value_spec.rbindex 51d5b0ad386..dec1cde630c 100644--- a/spec/models/acts_as_customizable/calculated_value_spec.rb+++ b/spec/models/acts_as_customizable/calculated_value_spec.rb@@ -487,5 +487,89 @@ RSpec.describe ActsAsCustomizable::CalculatedValue, with_flag: { calculated_valuexpect(disabled_cv.first_calculation_error(project:)).to be_blankendend++ describe "nesting multiple custom fields into one formula" do+ let(:a) { create(:integer_project_custom_field) }+ let(:b) { create(:integer_project_custom_field) }+ let(:a_plus_b) do+ create(:calculated_value_project_custom_field, :skip_validations, projects: [project], formula: "#{a} + #{b}")+ end+ let(:a_minus_b) do+ create(:calculated_value_project_custom_field, :skip_validations, projects: [project], formula: "#{a} - #{b}")+ end+ let(:nested_calculation) do+ create(:calculated_value_project_custom_field,+ :skip_validations,+ projects: [project],+ formula: "#{a_plus_b} / #{a_minus_b}")+ end++ let(:enabled_custom_fields) do+ { a => 1, b => 2, a_plus_b => nil, a_minus_b => nil, nested_calculation => nil }+ end++ it "calculates the result correctly and does not produce an error" do+ project.calculate_custom_fields([a_plus_b, a_minus_b, nested_calculation])++ custom_values = project.custom_field_values.to_h { |cv| [cv.custom_field_id, cv.value] }+ expect(custom_values).to eq({+ a_plus_b.id => "3",+ a_minus_b.id => "-1",+ nested_calculation.id => "-3.0"+ })++ expect(a_plus_b.first_calculation_error(project:)).to be_blank+ expect(a_minus_b.first_calculation_error(project:)).to be_blank+ expect(nested_calculation.first_calculation_error(project:)).to be_blank+ end++ describe "with a_minus_b being disabled" do+ let(:a_minus_b) do+ create(:calculated_value_project_custom_field, :skip_validations, projects: [], formula: "#{a} - #{b}")+ end++ let(:enabled_custom_fields) do+ { a => 1, b => 2, a_plus_b => nil, nested_calculation => nil }+ end++ it "produces an error for the disabled field" do+ project.calculate_custom_fields([a_plus_b, a_minus_b, nested_calculation])++ expect(a_plus_b.first_calculation_error(project:)).to be_blank+ expect_calculated_value_error(nested_calculation, project, "ERROR_DISABLED_VALUE", a_minus_b.name)+ end+ end++ describe "with a_plus_b being disabled" do+ let(:a_plus_b) do+ create(:calculated_value_project_custom_field, :skip_validations, projects: [], formula: "#{a} + #{b}")+ end++ let(:enabled_custom_fields) do+ { a => 1, b => 2, a_minus_b => nil, nested_calculation => nil }+ end++ it "produces an error for the disabled field" do+ project.calculate_custom_fields([a_plus_b, a_minus_b, nested_calculation])++ expect(a_minus_b.first_calculation_error(project:)).to be_blank+ expect_calculated_value_error(nested_calculation, project, "ERROR_DISABLED_VALUE", a_plus_b.name)+ end+ end++ describe "resulting in a division by zero error" do+ let(:enabled_custom_fields) do+ { a => 1, b => 1, a_plus_b => nil, a_minus_b => nil, nested_calculation => nil }+ end++ it "produces a mathematical error" do+ project.calculate_custom_fields([a_plus_b, a_minus_b, nested_calculation])++ expect(a_plus_b.first_calculation_error(project:)).to be_blank+ expect(a_minus_b.first_calculation_error(project:)).to be_blank+ expect_calculated_value_error(nested_calculation, project, "ERROR_MATHEMATICAL")+ end+ end+ endendend