Fix #2001 (Use non-bang gsub in NotificationFactory:Template)

This commit is contained in:
Ryan Lue 2018-05-15 18:18:14 +08:00
parent ca454dd461
commit c3ad7e307b
2 changed files with 34 additions and 24 deletions

View file

@ -18,42 +18,33 @@ examples how to use
def to_s def to_s
strip_html strip_html
@template
end end
def strip_html def strip_html
# some browsers start adding HTML tags # some browsers start adding HTML tags
# fixes https://github.com/zammad/zammad/issues/385 # fixes https://github.com/zammad/zammad/issues/385
@template.gsub!(/\#\{\s*t\((.+?)\)\s*\}/m) do @template.gsub(/\#\{\s*t\((.+?)\)\s*\}/m) do
content = $1 if $1 =~ /^'(.+?)'$/
if content =~ /^'(.+?)'$/ %(<%= t "#{strip_content($1)}", #{@escape} %>)
"<%= t \"#{strip_content($1)}\", #{@escape} %>"
else else
"<%= t d\"#{strip_variable(content)}\", #{@escape} %>" %(<%= t d"#{strip_variable(strip_content($1))}", #{@escape} %>)
end end
end end.gsub(/\#\{\s*config\.(.+?)\s*\}/m) do
@template.gsub!(/\#\{\s*config\.(.+?)\s*\}/m) do %(<%= c "#{strip_variable($1)}", #{@escape} %>)
"<%= c \"#{strip_variable($1)}\", #{@escape} %>" end.gsub(/\#\{(.*?)\}/m) do
end %(<%= d "#{strip_variable($1)}", #{@escape} %>)
@template.gsub!(/\#\{(.*?)\}/m) do
"<%= d \"#{strip_variable($1)}\", #{@escape} %>"
end end
end end
def strip_content(string) def strip_content(string)
return string if !string string&.gsub(/\t|\r|\n/, '')
string.gsub!(/\t|\r|\n/, '') &.gsub(/"/, '\"')
string.gsub!(/"/, '\"')
string
end end
def strip_variable(string) def strip_variable(string)
return string if !string string&.gsub(/\t|\r|\n|"|'|§|;/, '')
string.gsub!(/\t|\r|\n|"|'|§|;/, '') &.gsub(/\s*/, '')
string.gsub!(/\s*/, '') &.gsub(/<.+?>/, '')
string.gsub!(/<.+?>/, '')
string
end end
end end

View file

@ -175,7 +175,7 @@ RSpec.describe Ticket do
describe '#perform_changes' do describe '#perform_changes' do
it 'performes a ticket state change on a ticket' do it 'performs a ticket state change on a ticket' do
source_ticket = create(:ticket) source_ticket = create(:ticket)
changes = { changes = {
@ -188,7 +188,7 @@ RSpec.describe Ticket do
expect(source_ticket.state.name).to eq('closed') expect(source_ticket.state.name).to eq('closed')
end end
it 'performes a ticket deletion on a ticket' do it 'performs a ticket deletion on a ticket' do
source_ticket = create(:ticket) source_ticket = create(:ticket)
changes = { changes = {
@ -201,6 +201,25 @@ RSpec.describe Ticket do
expect(ticket_with_source_ids).to match_array([]) expect(ticket_with_source_ids).to match_array([])
end end
# Regression test for https://github.com/zammad/zammad/issues/2001
it 'does not modify its arguments' do
trigger = Trigger.new(
perform: {
# rubocop:disable Lint/InterpolationCheck
'notification.email' => {
body: 'Hello #{ticket.customer.firstname} #{ticket.customer.lastname},',
recipient: %w[article_last_sender ticket_owner ticket_customer ticket_agents],
subject: 'Autoclose (#{ticket.title})'
}
# rubocop:enable Lint/InterpolationCheck
}
)
expect { Ticket.first.perform_changes(trigger.perform, 'trigger', {}, 1) }
.to not_change { trigger.perform['notification.email'][:body] }
.and not_change { trigger.perform['notification.email'][:subject] }
end
end end
describe '#selectors' do describe '#selectors' do