From d51217471073936f8337620cb959982aecf9b3b5 Mon Sep 17 00:00:00 2001 From: Erik Clarizio Date: Thu, 8 Feb 2018 14:11:33 -0800 Subject: [PATCH] Use correct comparison for multi-value default value inclusion rules https://bugzilla.redhat.com/show_bug.cgi?id=1531316 Since the default value for a multi-value drop-down is a string, comparing directly to the list of values in a drop-down did not work. It needs to be converted to an array and then compare the overlap to see if the default value is appropriate. --- app/models/dialog_field_drop_down_list.rb | 14 ++++ ...hestration_template_service_dialog_spec.rb | 4 +- .../dialog_field_drop_down_list_spec.rb | 64 ++++++++++++++----- 3 files changed, 64 insertions(+), 18 deletions(-) diff --git a/app/models/dialog_field_drop_down_list.rb b/app/models/dialog_field_drop_down_list.rb index cd88848fca0..2e02d9154b6 100644 --- a/app/models/dialog_field_drop_down_list.rb +++ b/app/models/dialog_field_drop_down_list.rb @@ -55,4 +55,18 @@ def automate_key_name return super unless force_multi_value MiqAeEngine.create_automation_attribute_array_key(super) end + + private + + def default_value_included?(values_list) + if force_multi_value + return false if default_value.blank? + converted_values_list = values_list.collect { |value_pair| value_pair[0].send(value_modifier) } + converted_default_values = JSON.parse(default_value).collect { |value| value.send(value_modifier) } + overlap = converted_values_list & converted_default_values + !overlap.empty? + else + super(values_list) + end + end end diff --git a/spec/models/dialog/orchestration_template_service_dialog_spec.rb b/spec/models/dialog/orchestration_template_service_dialog_spec.rb index d58197aa556..dd81637c4cc 100644 --- a/spec/models/dialog/orchestration_template_service_dialog_spec.rb +++ b/spec/models/dialog/orchestration_template_service_dialog_spec.rb @@ -25,14 +25,14 @@ context "when allowed values are given" do let(:param_options) do constraint = OrchestrationTemplate::OrchestrationParameterAllowed.new(:allowed_values => %w(val1 val2), :allow_multiple => true) - {:default_value => 'val1', :constraints => [constraint]} + {:default_value => '["val1"]', :constraints => [constraint]} end it "creates a dropdown field with pairs of values" do assert_field(test_field(dialog), DialogFieldDropDownList, :name => "param_user", - :default_value => "val1", + :default_value => "[\"val1\"]", :values => [%w(val1 val1), %w(val2 val2)], :reconfigurable => true, :force_multi_value => true) diff --git a/spec/models/dialog_field_drop_down_list_spec.rb b/spec/models/dialog_field_drop_down_list_spec.rb index 9ac504ae91f..e2775968f14 100644 --- a/spec/models/dialog_field_drop_down_list_spec.rb +++ b/spec/models/dialog_field_drop_down_list_spec.rb @@ -136,33 +136,65 @@ end end - context "#initialize_with_values" do - before(:each) do - @df.values = [%w(3 X), %w(2 Y), %w(1 Z)] - @df.load_values_on_init = true - end + it "#automate_key_name" do + expect(@df.automate_key_name).to eq("dialog_drop_down_list") + end + end + + describe "#initialize_with_values" do + let(:dialog_field) { described_class.new(:options => {:force_multi_value => force_multi_value}) } + + before do + dialog_field.values = [%w(3 X), %w(2 Y), %w(1 Z)] + dialog_field.load_values_on_init = true + end + + context "when force_multi_value is not true" do + let(:force_multi_value) { false } it "uses the nil as the default value" do - @df.default_value = nil - @df.initialize_with_values({}) - expect(@df.value).to eq(nil) + dialog_field.default_value = nil + dialog_field.initialize_with_values({}) + expect(dialog_field.value).to eq(nil) end it "with default value" do - @df.default_value = "1" - @df.initialize_with_values({}) - expect(@df.value).to eq("1") + dialog_field.default_value = "1" + dialog_field.initialize_with_values({}) + expect(dialog_field.value).to eq("1") end it "uses the nil when there is a non-matching default value" do - @df.default_value = "4" - @df.initialize_with_values({}) - expect(@df.value).to eq(nil) + dialog_field.default_value = "4" + dialog_field.initialize_with_values({}) + expect(dialog_field.value).to eq(nil) end end - it "#automate_key_name" do - expect(@df.automate_key_name).to eq("dialog_drop_down_list") + context "when force_multi_value is true" do + let(:force_multi_value) { true } + + context "when the default values are included in the value list" do + before do + dialog_field.default_value = "[\"3\", \"2\"]" + end + + it "uses the default value" do + dialog_field.initialize_with_values({}) + expect(dialog_field.value).to eq("[\"3\", \"2\"]") + end + end + + context "when the default values are not included in the value list" do + before do + dialog_field.default_value = "[\"4\"]" + end + + it "uses nil" do + dialog_field.initialize_with_values({}) + expect(dialog_field.value).to eq(nil) + end + end end end