From 72dbd0a291a49f81d02602fc63b39aff1f43c462 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 17 Jul 2013 15:57:12 +0200 Subject: [PATCH] Improved use of text modules. Added unit tests. --- .../controllers/agent_ticket_create.js.coffee | 3 +- .../controllers/text_module_widget.js.coffee | 77 ++++-- .../app/controllers/ticket_zoom.js.coffee | 3 +- app/controllers/sessions/collection_ticket.rb | 3 - app/models/text_module.rb | 7 +- test/browser/text_module_test.rb | 242 ++++++++++++++++-- 6 files changed, 287 insertions(+), 48 deletions(-) diff --git a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee index 8b3467518..e75533e93 100644 --- a/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee +++ b/app/assets/javascripts/app/controllers/agent_ticket_create.js.coffee @@ -83,6 +83,7 @@ class App.TicketCreate extends App.Controller # @clearInterval( @key, 'ticket_zoom' ) @el.remove() @clearInterval( @id, @auto_save_key ) + @textModule.release() autosave: => @auto_save_key = 'create' + @type + @id @@ -210,7 +211,7 @@ class App.TicketCreate extends App.Controller # show text module UI @textModule = new App.TextModuleUI( - el: $('.ticket-create') + el: @el.find('.ticket-create') ) localUserInfo: (params) => diff --git a/app/assets/javascripts/app/controllers/text_module_widget.js.coffee b/app/assets/javascripts/app/controllers/text_module_widget.js.coffee index 0fe929ee4..0b582d232 100644 --- a/app/assets/javascripts/app/controllers/text_module_widget.js.coffee +++ b/app/assets/javascripts/app/controllers/text_module_widget.js.coffee @@ -1,23 +1,6 @@ class App.TextModuleUI extends App.Controller constructor: -> super - ui = @ - values = [] - all = App.Collection.all( type: 'TextModule' ) - for item in all - if item.active is true - contentNew = item.content.replace( /<%=\s{0,2}(.+?)\s{0,2}%>/g, ( all, key ) -> - key = key.replace( /@/g, 'ui.data.' ) - varString = "#{key}" + '' - try - key = eval (varString) - catch error - # console.log( "tag replacement: " + error ) - key = '' - return key - ) - value = { val: contentNew, keywords: item.keywords || item.name } - values.push value customItemTemplate = "
 
" elementFactory = (element, e) -> @@ -26,7 +9,65 @@ class App.TextModuleUI extends App.Controller .find('small') .text("(" + e.keywords + ")").end() element.append(template) - @el.find('textarea').sew({values: values, token: '::', elementFactory: elementFactory }) + + @el.find('textarea').sew( + values: @reload() + token: '::' + elementFactory: elementFactory + ) + + App.TextModule.bind( + 'refresh change' + => + @reload() + ) + + # subscribe and reload data / fetch new data if triggered + @bindLevel = 'TextModule-' + Math.floor( Math.random() * 99999 ) + App.Event.bind( + 'TextModule:updated TextModule:created TextModule:destroy' + => + App.TextModule.fetch() + @bindLevel + ) + + # fetch init collection + App.TextModule.fetch() + + release: => + App.Event.unbindLevel(@bindLevel) + + reload: (data = false) => + if data + @lastData = data + all = App.TextModule.all() + values = [{val: '-', keywords: '-'}] + ui = @lastData || @ + for item in all + if item.active is true + contentNew = item.content.replace( /<%=\s{0,2}(.+?)\s{0,2}%>/g, ( all, key ) -> + key = key.replace( /@/g, 'ui.data.' ) + varString = "#{key}" + '' + try + key = eval (varString) + catch error + console.log( "tag replacement: " + error ) + key = '' + return key + ) + value = { val: contentNew, keywords: item.keywords || item.name } + values.push value + + if values.length isnt 1 + values.shift() + + # set new data + if @el.find('textarea')[0] + if $(@el.find('textarea')[0]).data() + if $(@el.find('textarea')[0]).data().plugin_sew + $(@el.find('textarea')[0]).data().plugin_sew.options.values = values + + return values class App.TextModuleUIOld extends App.Controller events: diff --git a/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee b/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee index a73f035f0..4837ce04f 100644 --- a/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee +++ b/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee @@ -58,6 +58,7 @@ class App.TicketZoom extends App.Controller return true release: => + @textModule.release() App.Event.unbindLevel 'ticket-zoom-' + @ticket_id @clearInterval( @key, 'ticket_zoom' ) @el.remove() @@ -184,7 +185,7 @@ class App.TicketZoom extends App.Controller # show text module UI if !@isRole('Customer') - new App.TextModuleUI( + @textModule = new App.TextModuleUI( el: @el data: ticket: @ticket diff --git a/app/controllers/sessions/collection_ticket.rb b/app/controllers/sessions/collection_ticket.rb index 40461cb20..a05f24a4c 100644 --- a/app/controllers/sessions/collection_ticket.rb +++ b/app/controllers/sessions/collection_ticket.rb @@ -38,9 +38,6 @@ module ExtraCollection # all templates collections['Template'] = Template.all - - # all text modules - collections['TextModule'] = TextModule.all end end diff --git a/app/models/text_module.rb b/app/models/text_module.rb index be494b70e..77e55f0ac 100644 --- a/app/models/text_module.rb +++ b/app/models/text_module.rb @@ -1,6 +1,9 @@ # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ class TextModule < ApplicationModel - validates :name, :presence => true - validates :content, :presence => true + validates :name, :presence => true + validates :content, :presence => true + after_create :notify_clients_after_create + after_update :notify_clients_after_update + after_destroy :notify_clients_after_destroy end diff --git a/test/browser/text_module_test.rb b/test/browser/text_module_test.rb index 2ab7e7ac6..5866789fe 100644 --- a/test/browser/text_module_test.rb +++ b/test/browser/text_module_test.rb @@ -2,16 +2,18 @@ require 'browser_test_helper' class TextModuleTest < TestCase - def test_user + def test_I random = 'text_module_test_' + rand(999999).to_s random2 = 'text_module_test_' + rand(999999).to_s - user_email = random + '@example.com' # user tests = [ { :name => 'add #1', :action => [ + { + :execute => 'close_all_tasks', + }, { :execute => 'click', :css => 'a[href="#admin"]', @@ -20,18 +22,10 @@ class TextModuleTest < TestCase :execute => 'click', :css => 'a[href="#text_modules"]', }, - { - :execute => 'wait', - :value => 1, - }, { :execute => 'click', :css => 'a[data-type="new"]', }, - { - :execute => 'wait', - :value => 2, - }, { :execute => 'set', :css => 'input[name=name]', @@ -74,18 +68,10 @@ class TextModuleTest < TestCase :execute => 'click', :css => 'a[href="#text_modules"]', }, - { - :execute => 'wait', - :value => 1, - }, { :execute => 'click', :css => 'a[data-type="new"]', }, - { - :execute => 'wait', - :value => 1, - }, { :execute => 'set', :css => 'input[name=name]', @@ -148,9 +134,8 @@ class TextModuleTest < TestCase :match_result => true, }, { - :execute => 'sendkey', - :css => '.active textarea[name=body]', - :value => :enter, + :execute => 'click', + :css => '.-sew-list-item.selected', }, { :execute => 'wait', @@ -159,13 +144,224 @@ class TextModuleTest < TestCase { :execute => 'match', :css => '.active textarea[name=body]', - :value => random, + :value => 'some content' + random, :match_result => true, }, ], - }, ] browser_signle_test_with_login(tests, { :username => 'master@example.com' }) end + def test_II + random = 'text_II_module_test_' + rand(999999).to_s + + # user + tests = [ + { + :name => 'start', + :instance1 => browser_instance, + :instance2 => browser_instance, + :instance1_username => 'master@example.com', + :instance1_password => 'test', + :instance2_username => 'agent1@example.com', + :instance2_password => 'test', + :action => [ + # create ticket + { + :where => :instance2, + :execute => 'close_all_tasks', + }, + { + :where => :instance2, + :execute => 'click', + :css => 'a[href="#new"]', + }, + { + :where => :instance2, + :execute => 'click', + :css => 'a[href="#ticket_create/call_inbound"]', + }, + { + :where => :instance2, + :execute => 'click', + :css => 'a[href="#new"]', + }, + { + :where => :instance2, + :execute => 'set', + :css => '.active input[name=subject]', + :value => 'A', + }, + { + :where => :instance2, + :execute => 'click', + :css => 'a[href="#ticket_create/call_outbound"]', + }, + { + :where => :instance2, + :execute => 'set', + :css => '.active input[name=subject]', + :value => 'B', + }, + + # create new text module + { + :where => :instance1, + :execute => 'click', + :css => 'a[href="#admin"]', + }, + { + :where => :instance1, + :execute => 'click', + :css => 'a[href="#text_modules"]', + }, + { + :where => :instance1, + :execute => 'click', + :css => 'a[data-type="new"]', + }, + { + :where => :instance1, + :execute => 'set', + :css => 'input[name=name]', + :value => 'some name' + random, + }, + { + :where => :instance1, + :execute => 'set', + :css => 'input[name="keywords"]', + :value => random, + }, + { + :where => :instance1, + :execute => 'set', + :css => 'textarea[name="content"]', + :value => 'some content <%= @ticket.customer.lastname %>' + random, + }, + { + :where => :instance1, + :execute => 'click', + :css => '.modal button.submit', + }, + { + :execute => 'wait', + :value => 3, + }, + { + :where => :instance1, + :execute => 'match', + :css => 'body', + :value => random, + :match_result => true, + }, + + + ], + }, + { + :name => 'check if text module exists in instance2, for ready to use', + :action => [ + { + :execute => 'wait', + :value => 3, + }, + { + :where => :instance2, + :execute => 'set', + :css => '.active textarea[name=body]', + :value => '::' + random, + }, + { + :where => :instance2, + :execute => 'wait', + :value => 1, + }, + { + :where => :instance2, + :execute => 'match', + :css => 'body', + :value => random, + :match_result => true, + }, + { + :where => :instance2, + :execute => 'click', + :css => '.-sew-list-item.selected', + }, + { + :where => :instance2, + :execute => 'wait', + :value => 1, + }, + { + :where => :instance2, + :execute => 'match', + :css => '.active textarea[name=body]', + :value => 'some content ' + random, + :match_result => true, + }, + { + :execute => 'wait', + :value => 3, + }, + { + :where => :instance2, + :execute => 'set', + :css => '.active .ticket_create input[name="customer_id_autocompletion"]', + :value => 'nicole', + }, + { + :execute => 'wait', + :value => 4, + }, + { + :where => :instance2, + :execute => 'sendkey', + :css => '.active .ticket_create input[name="customer_id_autocompletion"]', + :value => :arrow_down, + }, + { + :where => :instance2, + :execute => 'sendkey', + :css => '.active .ticket_create input[name="customer_id_autocompletion"]', + :value => :tab, + }, + { + :where => :instance2, + :execute => 'wait', + :value => 1, + }, + { + :where => :instance2, + :execute => 'set', + :css => '.active textarea[name=body]', + :value => '::' + random, + }, + { + :where => :instance2, + :execute => 'wait', + :value => 1, + }, + { + :where => :instance2, + :execute => 'click', + :css => '.-sew-list-item.selected', + }, + { + :where => :instance2, + :execute => 'wait', + :value => 1, + }, + { + :where => :instance2, + :execute => 'match', + :css => '.active textarea[name=body]', + :value => 'some content Braun' + random, + :match_result => true, + }, + ], + }, + ] + browser_double_test(tests) + end end