diff --git a/app/assets/javascripts/app/controllers/_ui_element/_application_ui_element.coffee b/app/assets/javascripts/app/controllers/_ui_element/_application_ui_element.coffee index 546d3e29b605..674324397546 100644 --- a/app/assets/javascripts/app/controllers/_ui_element/_application_ui_element.coffee +++ b/app/assets/javascripts/app/controllers/_ui_element/_application_ui_element.coffee @@ -119,7 +119,7 @@ class App.UiElement.ApplicationUiElement list.push record # check if current value need to be added - if params[ attribute.name ] + if params[ attribute.name ] && !attribute.rejectNonExistentValues hit = false for value in list if value['id'].toString() is params[ attribute.name ].toString() diff --git a/app/assets/javascripts/app/controllers/_ui_element/select.coffee b/app/assets/javascripts/app/controllers/_ui_element/select.coffee index e8dbdf1b38c5..b72158e59b2f 100644 --- a/app/assets/javascripts/app/controllers/_ui_element/select.coffee +++ b/app/assets/javascripts/app/controllers/_ui_element/select.coffee @@ -1,6 +1,6 @@ # coffeelint: disable=camel_case_classes class App.UiElement.select extends App.UiElement.ApplicationUiElement - @render: (attribute, params) -> + @render: (attribute, params, form = {}) -> # set multiple option if attribute.multiple @@ -8,6 +8,9 @@ class App.UiElement.select extends App.UiElement.ApplicationUiElement else attribute.multiple = '' + if form.rejectNonExistentValues + attribute.rejectNonExistentValues = true + # add deleted historical options if required @addDeletedOptions(attribute, params) @@ -39,6 +42,7 @@ class App.UiElement.select extends App.UiElement.ApplicationUiElement # 2. If attribute.value is not among current and historical options, then add the value itself as an option @addDeletedOptions: (attribute) -> return if !_.isEmpty(attribute.relation) # do not apply for attributes with relation, relations will fill options automatically + return if attribute.rejectNonExistentValues value = attribute.value return if !value return if _.isArray(value) diff --git a/app/assets/javascripts/app/controllers/agent_ticket_create.coffee b/app/assets/javascripts/app/controllers/agent_ticket_create.coffee index f6257e467297..9245d82b1e56 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_create.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_create.coffee @@ -324,11 +324,12 @@ class App.TicketCreate extends App.Controller events: 'change [name=customer_id]': @localUserInfo handlersConfig: handlers - filter: @formMeta.filter - formMeta: @formMeta - params: params - noFieldset: true - taskKey: @taskKey + filter: @formMeta.filter + formMeta: @formMeta + params: params + noFieldset: true + taskKey: @taskKey + rejectNonExistentValues: true ) new App.ControllerForm( el: @$('.ticket-form-bottom') diff --git a/app/assets/javascripts/app/controllers/customer_ticket_create.coffee b/app/assets/javascripts/app/controllers/customer_ticket_create.coffee index fb917fdb9201..fba6fb8ead5b 100644 --- a/app/assets/javascripts/app/controllers/customer_ticket_create.coffee +++ b/app/assets/javascripts/app/controllers/customer_ticket_create.coffee @@ -69,15 +69,16 @@ class Index extends App.ControllerContent handlersConfig: handlers ) new App.ControllerForm( - el: @el.find('.ticket-form-middle') - form_id: @form_id - model: App.Ticket - screen: 'create_middle' - filter: @formMeta.filter - formMeta: @formMeta - params: defaults - noFieldset: true - handlersConfig: handlers + el: @el.find('.ticket-form-middle') + form_id: @form_id + model: App.Ticket + screen: 'create_middle' + filter: @formMeta.filter + formMeta: @formMeta + params: defaults + noFieldset: true + handlersConfig: handlers + rejectNonExistentValues: true ) if !_.isEmpty(App.Ticket.attributesGet('create_bottom', false, true)) new App.ControllerForm( diff --git a/public/assets/tests/form.js b/public/assets/tests/form.js index 43c5c50b0523..11c818f9ac40 100644 --- a/public/assets/tests/form.js +++ b/public/assets/tests/form.js @@ -1339,6 +1339,70 @@ test("object manager form 3", function() { }); +test("check if select value is not existing but is shown", function() { + + $('#forms').append('

check if select value is not existing but is shown

') + var el = $('#form17') + var defaults = { + select1: 'NOT EXISTING', + } + new App.ControllerForm({ + el: el, + model: { + configure_attributes: [ + { name: 'select1', display: 'Select1', tag: 'select', null: true, default: 'XY', options: { 'XX': 'AA', 'A': 'XX', 'B': 'B', 'XY': 'b', '': 'äöü' } }, + ], + }, + params: defaults, + }); + + params = App.ControllerForm.params(el) + test_params = { + select1: 'NOT EXISTING', + } + deepEqual(params, test_params) + + equal('AA', el.find('[name=select1] option')[0].text) + equal('äöü', el.find('[name=select1] option')[1].text) + equal('b', el.find('[name=select1] option')[2].text) + equal('B', el.find('[name=select1] option')[3].text) + equal('NOT EXISTING', el.find('[name=select1] option')[4].text) + equal('XX', el.find('[name=select1] option')[5].text) + +}); + +test("check if select value is not existing and is not shown", function() { + + $('#forms').append('

check if select value is not existing and is not shown

') + var el = $('#form18') + var defaults = { + select1: 'NOT EXISTING', + } + new App.ControllerForm({ + el: el, + model: { + configure_attributes: [ + { name: 'select1', display: 'Select1', tag: 'select', null: true, default: 'XY', options: { 'XX': 'AA', 'A': 'XX', 'B': 'B', 'XY': 'b', '': 'äöü' } }, + ], + }, + params: defaults, + rejectNonExistentValues: true, + }); + + params = App.ControllerForm.params(el) + test_params = { + select1: 'XY', + } + deepEqual(params, test_params) + + equal('AA', el.find('[name=select1] option')[0].text) + equal('äöü', el.find('[name=select1] option')[1].text) + equal('b', el.find('[name=select1] option')[2].text) + equal('B', el.find('[name=select1] option')[3].text) + equal('XX', el.find('[name=select1] option')[4].text) + +}); + test("time range form 1", function() { $('#forms').append('

time range form 1

') diff --git a/spec/factories/template.rb b/spec/factories/template.rb new file mode 100644 index 000000000000..59eec72f1768 --- /dev/null +++ b/spec/factories/template.rb @@ -0,0 +1,8 @@ +FactoryBot.define do + factory :template do + name { Faker::Name.unique.name } + options { {} } + updated_by_id { 1 } + created_by_id { 1 } + end +end diff --git a/spec/system/ticket/create_spec.rb b/spec/system/ticket/create_spec.rb new file mode 100644 index 000000000000..3a0490792e12 --- /dev/null +++ b/spec/system/ticket/create_spec.rb @@ -0,0 +1,31 @@ +require 'rails_helper' + +RSpec.describe 'Ticket Create', type: :system do + context 'when applying ticket templates' do + # Regression test for issue #2424 - Unavailable ticket template attributes get applied + scenario 'unavailable attributes do not get applied', authenticated: false do + # create a new agent with permissions for only group "some group1" + user = create :agent_user + user.group_names_access_map = { + 'some group1' => 'full', + } + + # create a template that sets the group to Users and ticket owner to user id 1 + template = create :template, options: { + 'title' => 'Template Title', + 'group_id' => '1', + 'owner_id' => '2', + } + + # apply the ticket template and confirm that the group_id dropdown does not appear + login( + username: user.email, + password: 'test', + ) + visit 'ticket/create' + find('#form-template select[name="id"]').find(:option, template.name).select_option + click '.sidebar-content .js-apply' + expect(page).not_to have_selector 'select[name="group_id"]' + end + end +end