From 9c8cfd83ddfbe39871647184ea21bccaf2f4fd8f Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Mon, 30 Nov 2020 17:31:53 +0100 Subject: [PATCH] Fixes #3310 - Zendesk import fails if fields with unsupported chars in its name are present. --- .../common/object_attribute/sanitized_name.rb | 27 ++++++++++++++++--- .../object_attribute/sanitized_name.rb | 2 ++ .../zendesk/ticket_field/sanitized_name.rb | 2 ++ .../object_attribute/sanitized_name_spec.rb | 18 ++++++++++++- test/integration/zendesk_import_test.rb | 6 ++--- 5 files changed, 48 insertions(+), 7 deletions(-) diff --git a/lib/sequencer/unit/import/common/object_attribute/sanitized_name.rb b/lib/sequencer/unit/import/common/object_attribute/sanitized_name.rb index 33adf8802..de0c5829f 100644 --- a/lib/sequencer/unit/import/common/object_attribute/sanitized_name.rb +++ b/lib/sequencer/unit/import/common/object_attribute/sanitized_name.rb @@ -13,6 +13,8 @@ class Sequencer # model_name # model_name # model_name + # model_name_ + # model_name without_double_underscores.gsub(/_id(s?)$/, '_no\1') end @@ -22,16 +24,31 @@ class Sequencer # model_name # model_name # model_name - without_spaces_and_slashes.gsub(/_{2,}/, '_') + # model_name_ + # model_name + only_supported_chars.gsub(/_{2,}/, '_') end - def without_spaces_and_slashes + def only_supported_chars # model_id # model_ids # model___name # model_name + # model__name + # model_name_ # model_name - transliterated.gsub(%r{[\s/]}, '_').underscore + downcased.split('').map { |char| char.match?(/[a-z0-9_]/) ? char : '_' }.join + end + + def downcased + # model id + # model ids + # model / name + # model name + # model::name + # model name? + # model name + transliterated.downcase end def transliterated @@ -39,6 +56,8 @@ class Sequencer # Model IDs # Model / Name # Model Name + # Model::Name + # Model Name? # Model Name ::ActiveSupport::Inflector.transliterate(unsanitized_name, '_'.freeze) end @@ -48,6 +67,8 @@ class Sequencer # Model IDs # Model / Name # Model Name + # Model::Name + # Model Name? # rubocop:disable Style/AsciiComments # Mödel Nâmé # rubocop:enable Style/AsciiComments diff --git a/lib/sequencer/unit/import/zendesk/object_attribute/sanitized_name.rb b/lib/sequencer/unit/import/zendesk/object_attribute/sanitized_name.rb index cfc9f10a9..6d19b831a 100644 --- a/lib/sequencer/unit/import/zendesk/object_attribute/sanitized_name.rb +++ b/lib/sequencer/unit/import/zendesk/object_attribute/sanitized_name.rb @@ -14,6 +14,8 @@ class Sequencer # Model IDs # Model / Name # Model Name + # Model Name? + # Model::Name resource['key'] end end diff --git a/lib/sequencer/unit/import/zendesk/ticket_field/sanitized_name.rb b/lib/sequencer/unit/import/zendesk/ticket_field/sanitized_name.rb index e35ba2ce9..10b5595c4 100644 --- a/lib/sequencer/unit/import/zendesk/ticket_field/sanitized_name.rb +++ b/lib/sequencer/unit/import/zendesk/ticket_field/sanitized_name.rb @@ -17,6 +17,8 @@ class Sequencer # Model IDs # Model / Name # Model Name + # Model Name? + # Model::Name resource.title end end diff --git a/spec/lib/sequencer/unit/import/common/object_attribute/sanitized_name_spec.rb b/spec/lib/sequencer/unit/import/common/object_attribute/sanitized_name_spec.rb index 4fc7cba27..5a1158cb5 100644 --- a/spec/lib/sequencer/unit/import/common/object_attribute/sanitized_name_spec.rb +++ b/spec/lib/sequencer/unit/import/common/object_attribute/sanitized_name_spec.rb @@ -48,7 +48,23 @@ RSpec.describe Sequencer::Unit::Import::Common::ObjectAttribute::SanitizedName, allow(instance).to receive(:unsanitized_name).and_return('Ærøskøbing Ät Mödél') end - expect(provided[:sanitized_name]).to eq('a_eroskobing_at_model') + expect(provided[:sanitized_name]).to eq('aeroskobing_at_model') + end + + it 'replaces questionmark characters' do + provided = process do |instance| + allow(instance).to receive(:unsanitized_name).and_return('model?') + end + + expect(provided[:sanitized_name]).to eq('model_') + end + + it 'replaces colon characters' do + provided = process do |instance| + allow(instance).to receive(:unsanitized_name).and_return('mo::del') + end + + expect(provided[:sanitized_name]).to eq('mo_del') end end end diff --git a/test/integration/zendesk_import_test.rb b/test/integration/zendesk_import_test.rb index 55c1afa54..c2418d758 100644 --- a/test/integration/zendesk_import_test.rb +++ b/test/integration/zendesk_import_test.rb @@ -340,7 +340,7 @@ class ZendeskImportTest < ActiveSupport::TestCase organization_id: 2, test_checkbox: true, custom_integer: 999, - custom_drop_down: 'key2', + custom_dropdown: 'key2', custom_decimal: '1.6', not_existing: nil, }, @@ -361,7 +361,7 @@ class ZendeskImportTest < ActiveSupport::TestCase organization_id: nil, test_checkbox: false, custom_integer: nil, - custom_drop_down: '', + custom_dropdown: '', custom_decimal: nil, not_existing: nil, }, @@ -538,7 +538,7 @@ class ZendeskImportTest < ActiveSupport::TestCase custom_date custom_integer custom_regex - custom_drop_down + custom_dropdown ] assert_equal(copmare_fields, local_fields, 'ticket fields')