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
test SetCalculatedCustomFieldValues
Ivan Kuchin17be99435537
diff --git a/spec/factories/custom_field_factory.rb b/spec/factories/custom_field_factory.rbindex eb19a31c60d..e7a8123bb99 100644--- a/spec/factories/custom_field_factory.rb+++ b/spec/factories/custom_field_factory.rb@@ -59,6 +59,10 @@ FactoryBot.define doRequestStore.store.delete_if { |key, _| key.to_s.include?("_custom_fields") }end+ trait :admin_only do+ admin_only { true }+ end+trait :multi_value domulti_value { true }enddiff --git a/spec/services/projects/set_attributes_service_spec.rb b/spec/services/projects/set_attributes_service_spec.rbindex ef48f7d38e8..44ff05f2116 100644--- a/spec/services/projects/set_attributes_service_spec.rb+++ b/spec/services/projects/set_attributes_service_spec.rb@@ -61,10 +61,6 @@ RSpec.describe Projects::SetAttributesService, type: :model doenddescribe "call" do- let(:call_attributes) do- {}- end-before doallow(project).to receive(:valid?)@@ -314,6 +310,250 @@ RSpec.describe Projects::SetAttributesService, type: :model doinclude_examples "setting status attributes"endend++ describe "calculated custom fields", with_flag: { calculated_value_project_attribute: true } do+ shared_let(:project) { create(:project) }++ before do+ # Both User.current and :select_project_custom_fields for ProjectCustomField.visible+ User.current = user+ mock_permissions_for(user) do |mock|+ mock.allow_in_project(:select_project_custom_fields, project:)+ end+ end++ def ref(custom_field) = "{{cf_#{custom_field.id}}}"++ context "when trying to explicitly set values of calculated custom fields" do+ let!(:cf_static) { create(:integer_project_custom_field, projects: [project]) }+ let!(:cf_calculated) do+ create(:calculated_value_project_custom_field,+ projects: [project], formula: "1 + 1")+ end++ let(:call_attributes) do+ {+ custom_field_values: {+ cf_static.id => 3,+ cf_calculated.id => 4+ }+ }+ end++ before do+ create(:custom_value, customized: project, custom_field: cf_static, value: -5)+ create(:custom_value, customized: project, custom_field: cf_calculated, value: -6)+ end++ it "doesn't allow to assign calculated value" do+ expect(subject.result.custom_value_attributes).to eq(cf_static.id => "3", cf_calculated.id => "-6")+ end+ end++ context "when setting value of field referenced in calculated values" do+ let!(:cf_static) { create(:integer_project_custom_field, projects: [project]) }+ let!(:cf_calculated1) do+ create(:calculated_value_project_custom_field, :skip_validations,+ projects: [project],+ formula: "#{ref cf_static} * 7")+ end+ let!(:cf_calculated2) do+ create(:calculated_value_project_custom_field, :skip_validations,+ projects: [project],+ formula: "#{ref cf_calculated1} * 11")+ end++ let(:call_attributes) do+ {+ custom_field_values: {+ cf_static.id => 3+ }+ }+ end++ before do+ create(:custom_value, customized: project, custom_field: cf_static, value: -5)+ create(:custom_value, customized: project, custom_field: cf_calculated1, value: -6)+ create(:custom_value, customized: project, custom_field: cf_calculated2, value: -6)+ end++ it "calculates all values" do+ expect(subject.result.custom_value_attributes).to eq(+ cf_static.id => "3",+ cf_calculated1.id => "21",+ cf_calculated2.id => "231"+ )+ end+ end++ context "when removing value of field referenced in calculated values" do+ let!(:cf_static) { create(:integer_project_custom_field, projects: [project]) }+ let!(:cf_calculated1) do+ create(:calculated_value_project_custom_field, :skip_validations,+ projects: [project],+ formula: "#{ref cf_static} * 7")+ end+ let!(:cf_calculated2) do+ create(:calculated_value_project_custom_field, :skip_validations,+ projects: [project],+ formula: "#{ref cf_calculated1} * 11")+ end++ let(:call_attributes) do+ {+ custom_field_values: {+ cf_static.id => nil+ }+ }+ end++ before do+ create(:custom_value, customized: project, custom_field: cf_static, value: -5)+ create(:custom_value, customized: project, custom_field: cf_calculated1, value: -6)+ create(:custom_value, customized: project, custom_field: cf_calculated2, value: -6)+ end++ it "blanks all values" do+ expect(subject.result.custom_value_attributes).to eq(+ cf_static.id => nil,+ cf_calculated1.id => nil,+ cf_calculated2.id => nil+ )+ end+ end++ context "when setting value of only part of fields referenced in calculated values" do+ let!(:cf_a) { create(:integer_project_custom_field, projects: [project]) }+ let!(:cf_b) { create(:integer_project_custom_field, projects: [project]) }+ let!(:cf_c) { create(:integer_project_custom_field, projects: [project]) }+ let!(:cf_calculated1) do+ create(:calculated_value_project_custom_field, :skip_validations,+ projects: [project], formula: "#{ref cf_a} * 7")+ end+ let!(:cf_calculated2) do+ create(:calculated_value_project_custom_field, :skip_validations,+ projects: [project], formula: "#{ref cf_b} * 11")+ end+ let!(:cf_calculated3) do+ create(:calculated_value_project_custom_field, :skip_validations,+ projects: [project], formula: "#{ref cf_c} * 13")+ end++ let(:call_attributes) do+ {+ custom_field_values: {+ cf_a.id => 3,+ cf_b.id => -5+ }+ }+ end++ before do+ create(:custom_value, customized: project, custom_field: cf_a, value: -5)+ create(:custom_value, customized: project, custom_field: cf_b, value: -5)+ create(:custom_value, customized: project, custom_field: cf_c, value: -5)+ create(:custom_value, customized: project, custom_field: cf_calculated1, value: -6)+ create(:custom_value, customized: project, custom_field: cf_calculated2, value: -6)+ create(:custom_value, customized: project, custom_field: cf_calculated3, value: -6)+ end++ it "calculates only values referenced by changed field" do+ expect(subject.result.custom_value_attributes).to eq(+ cf_a.id => "3",+ cf_b.id => "-5",+ cf_c.id => "-5",+ cf_calculated1.id => "21",+ cf_calculated2.id => "-6",+ cf_calculated3.id => "-6"+ )+ end+ end++ context "when intermediate calculated value field is not enabled" do+ let!(:cf_static) { create(:integer_project_custom_field, projects: [project]) }+ let!(:cf_calculated1) do+ create(:calculated_value_project_custom_field, :skip_validations,+ projects: [project],+ formula: "#{ref cf_static} * 7")+ end+ let!(:cf_calculated2) do+ create(:calculated_value_project_custom_field, :skip_validations,+ formula: "#{ref cf_calculated1} * 11")+ end+ let!(:cf_calculated3) do+ create(:calculated_value_project_custom_field, :skip_validations,+ projects: [project],+ formula: "#{ref cf_calculated2} * 13")+ end++ let(:call_attributes) do+ {+ custom_field_values: {+ cf_static.id => 3+ }+ }+ end++ before do+ create(:custom_value, customized: project, custom_field: cf_static, value: -5)+ create(:custom_value, customized: project, custom_field: cf_calculated1, value: -6)+ create(:custom_value, customized: project, custom_field: cf_calculated2, value: -6)+ create(:custom_value, customized: project, custom_field: cf_calculated3, value: -6)+ end++ it "calculates only accessible values" do+ expect(subject.result.custom_value_attributes).to eq(+ cf_static.id => "3",+ cf_calculated1.id => "21",+ cf_calculated2.id => "-6",+ cf_calculated3.id => "-6"+ )+ end+ end++ context "when intermediate calculated value field is for admin only" do+ let!(:cf_static) { create(:integer_project_custom_field, projects: [project]) }+ let!(:cf_calculated1) do+ create(:calculated_value_project_custom_field, :skip_validations,+ projects: [project],+ formula: "#{ref cf_static} * 7")+ end+ let!(:cf_calculated2) do+ create(:calculated_value_project_custom_field, :skip_validations, :admin_only,+ projects: [project],+ formula: "#{ref cf_calculated1} * 11")+ end+ let!(:cf_calculated3) do+ create(:calculated_value_project_custom_field, :skip_validations,+ projects: [project],+ formula: "#{ref cf_calculated2} * 13")+ end++ let(:call_attributes) do+ {+ custom_field_values: {+ cf_static.id => 3+ }+ }+ end++ before do+ create(:custom_value, customized: project, custom_field: cf_static, value: -5)+ create(:custom_value, customized: project, custom_field: cf_calculated1, value: -6)+ create(:custom_value, customized: project, custom_field: cf_calculated2, value: -6)+ create(:custom_value, customized: project, custom_field: cf_calculated3, value: -6)+ end++ it "calculates only accessible values" do+ expect(subject.result.custom_value_attributes).to eq(+ cf_static.id => "3",+ cf_calculated1.id => "21",+ cf_calculated2.id => "-6",+ cf_calculated3.id => "-6"+ )+ end+ end+ endendendend