Browse Source

Fixed issue #1151 - Problem with cyrillic letters in name of overviews.

Martin Edenhofer 7 years ago
parent
commit
7cb212832e
3 changed files with 237 additions and 9 deletions
  1. 20 7
      app/models/overview.rb
  2. 215 0
      test/unit/overview_test.rb
  3. 2 2
      test/unit/ticket_overview_test.rb

+ 20 - 7
app/models/overview.rb

@@ -28,24 +28,37 @@ class Overview < ApplicationModel
   end
 
   def fill_link_on_create
-    return true if !link.empty?
+    return true if link.present?
     self.link = link_name(name)
     true
   end
 
   def fill_link_on_update
-    return true if link.empty?
     return true if !changes['name']
+    return true if changes['link']
     self.link = link_name(name)
     true
   end
 
   def link_name(name)
-    link = name.downcase
-    link.gsub!(/\s/, '_')
-    link.gsub!(/[^0-9a-z]/i, '_')
-    link.gsub!(/_+/, '_')
-    link
+    local_link = name.downcase
+    local_link = local_link.parameterize('_')
+    local_link.gsub!(/\s/, '_')
+    local_link.gsub!(/_+/, '_')
+    local_link = URI.escape(local_link)
+    if local_link.blank?
+      local_link = id || rand(999)
+    end
+    check = true
+    while check
+      exists = Overview.find_by(link: local_link)
+      if exists && exists.id != id
+        local_link = "#{local_link}_#{rand(999)}"
+      else
+        check = false
+      end
+    end
+    local_link
   end
 
 end

+ 215 - 0
test/unit/overview_test.rb

@@ -0,0 +1,215 @@
+# encoding: utf-8
+require 'test_helper'
+
+class OverviewTest < ActiveSupport::TestCase
+
+  test 'overview link' do
+    UserInfo.current_user_id = 1
+    overview = Overview.create!(
+      name: 'Not Shown Admin 2',
+      condition: {
+        'ticket.state_id' => {
+          operator: 'is',
+          value: [1, 2, 3],
+        },
+      },
+      order: {
+        by: 'created_at',
+        direction: 'DESC',
+      },
+      view: {
+        d: %w(title customer state created_at),
+        s: %w(number title customer state created_at),
+        m: %w(number title customer state created_at),
+        view_mode_default: 's',
+      },
+    )
+    assert_equal(overview.link, 'not_shown_admin_2')
+    overview.destroy!
+
+    overview = Overview.create!(
+      name: 'My assigned Tickets',
+      condition: {
+        'ticket.state_id' => {
+          operator: 'is',
+          value: [1, 2, 3],
+        },
+      },
+      order: {
+        by: 'created_at',
+        direction: 'DESC',
+      },
+      view: {
+        d: %w(title customer state created_at),
+        s: %w(number title customer state created_at),
+        m: %w(number title customer state created_at),
+        view_mode_default: 's',
+      },
+    )
+    assert_equal(overview.link, 'my_assigned_tickets')
+    overview.destroy!
+
+    overview = Overview.create!(
+      name: 'Übersicht',
+      condition: {
+        'ticket.state_id' => {
+          operator: 'is',
+          value: [1, 2, 3],
+        },
+      },
+      order: {
+        by: 'created_at',
+        direction: 'DESC',
+      },
+      view: {
+        d: %w(title customer state created_at),
+        s: %w(number title customer state created_at),
+        m: %w(number title customer state created_at),
+        view_mode_default: 's',
+      },
+    )
+    assert_equal(overview.link, 'ubersicht')
+    overview.destroy!
+
+    overview = Overview.create!(
+      name: "   Übersicht   \n",
+      condition: {
+        'ticket.state_id' => {
+          operator: 'is',
+          value: [1, 2, 3],
+        },
+      },
+      order: {
+        by: 'created_at',
+        direction: 'DESC',
+      },
+      view: {
+        d: %w(title customer state created_at),
+        s: %w(number title customer state created_at),
+        m: %w(number title customer state created_at),
+        view_mode_default: 's',
+      },
+    )
+    assert_equal(overview.link, 'ubersicht')
+    overview.destroy!
+
+    overview1 = Overview.create!(
+      name: 'Meine Übersicht',
+      condition: {
+        'ticket.state_id' => {
+          operator: 'is',
+          value: [1, 2, 3],
+        },
+      },
+      order: {
+        by: 'created_at',
+        direction: 'DESC',
+      },
+      view: {
+        d: %w(title customer state created_at),
+        s: %w(number title customer state created_at),
+        m: %w(number title customer state created_at),
+        view_mode_default: 's',
+      },
+    )
+    assert_equal(overview1.link, 'meine_ubersicht')
+    overview2 = Overview.create!(
+      name: 'Meine Übersicht',
+      condition: {
+        'ticket.state_id' => {
+          operator: 'is',
+          value: [1, 2, 3],
+        },
+      },
+      order: {
+        by: 'created_at',
+        direction: 'DESC',
+      },
+      view: {
+        d: %w(title customer state created_at),
+        s: %w(number title customer state created_at),
+        m: %w(number title customer state created_at),
+        view_mode_default: 's',
+      },
+    )
+    assert(overview2.link.start_with?('meine_ubersicht'))
+    assert_not_equal(overview1.link, overview2.link)
+    overview1.destroy!
+    overview2.destroy!
+
+    overview = Overview.create!(
+      name: 'Д дФ ф',
+      condition: {
+        'ticket.state_id' => {
+          operator: 'is',
+          value: [1, 2, 3],
+        },
+      },
+      order: {
+        by: 'created_at',
+        direction: 'DESC',
+      },
+      view: {
+        d: %w(title customer state created_at),
+        s: %w(number title customer state created_at),
+        m: %w(number title customer state created_at),
+        view_mode_default: 's',
+      },
+    )
+    assert_match(/^\d\d\d$/, overview.link)
+    overview.destroy!
+
+    overview = Overview.create!(
+      name: ' Д дФ ф abc ',
+      condition: {
+        'ticket.state_id' => {
+          operator: 'is',
+          value: [1, 2, 3],
+        },
+      },
+      order: {
+        by: 'created_at',
+        direction: 'DESC',
+      },
+      view: {
+        d: %w(title customer state created_at),
+        s: %w(number title customer state created_at),
+        m: %w(number title customer state created_at),
+        view_mode_default: 's',
+      },
+    )
+    assert_equal(overview.link, 'abc')
+    overview.destroy!
+
+    overview = Overview.create!(
+      name: 'Übersicht',
+      link: 'my_overview',
+      condition: {
+        'ticket.state_id' => {
+          operator: 'is',
+          value: [1, 2, 3],
+        },
+      },
+      order: {
+        by: 'created_at',
+        direction: 'DESC',
+      },
+      view: {
+        d: %w(title customer state created_at),
+        s: %w(number title customer state created_at),
+        m: %w(number title customer state created_at),
+        view_mode_default: 's',
+      },
+    )
+    assert_equal(overview.link, 'my_overview')
+
+    overview.name = 'Übersicht2'
+    overview.link = 'my_overview2'
+    overview.save!
+
+    assert_equal(overview.link, 'my_overview2')
+
+    overview.destroy!
+
+  end
+end

+ 2 - 2
test/unit/ticket_overview_test.rb

@@ -302,7 +302,7 @@ class TicketOverviewTest < ActiveSupport::TestCase
     )
   end
 
-  test 'bbb overiview index' do
+  test 'bbb overview index' do
 
     result = Ticket::Overviews.all(
       current_user: @agent1,
@@ -343,7 +343,7 @@ class TicketOverviewTest < ActiveSupport::TestCase
 
   end
 
-  test 'ccc overiview content' do
+  test 'ccc overview content' do
 
     Ticket.destroy_all