Skip to content

Commit

Permalink
Fixed #2424 - Unavailable ticket template attributes get applied
Browse files Browse the repository at this point in the history
  • Loading branch information
Billy Zhou authored and znuny-robo committed Jan 21, 2019
1 parent a159eaf commit 74ec891
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 16 deletions.
Expand Up @@ -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()
Expand Down
@@ -1,13 +1,16 @@
# 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
attribute.multiple = 'multiple'
else
attribute.multiple = ''

if form.rejectNonExistentValues
attribute.rejectNonExistentValues = true

# add deleted historical options if required
@addDeletedOptions(attribute, params)

Expand Down Expand Up @@ -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)
Expand Down
Expand Up @@ -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')
Expand Down
Expand Up @@ -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(
Expand Down
64 changes: 64 additions & 0 deletions public/assets/tests/form.js
Expand Up @@ -1339,6 +1339,70 @@ test("object manager form 3", function() {

});

test("check if select value is not existing but is shown", function() {

$('#forms').append('<hr><h1>check if select value is not existing but is shown</h1><form id="form17"></form>')
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('<hr><h1>check if select value is not existing and is not shown</h1><form id="form18"></form>')
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('<hr><h1>time range form 1</h1><form id="form14"></form>')
Expand Down
8 changes: 8 additions & 0 deletions 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
31 changes: 31 additions & 0 deletions 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

0 comments on commit 74ec891

Please sign in to comment.