Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

sort_helper_test.rb 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require_relative '../test_helper'
  19. class SortHelperTest < Redmine::HelperTest
  20. include SortHelper
  21. include ERB::Util
  22. def setup
  23. super
  24. @session = nil
  25. @sort_param = nil
  26. end
  27. def test_default_sort_clause_with_array
  28. sort_init 'attr1', 'desc'
  29. sort_update(['attr1', 'attr2'])
  30. assert_equal ['attr1 DESC'], sort_clause
  31. end
  32. def test_default_sort_clause_with_hash
  33. sort_init 'attr1', 'desc'
  34. sort_update({'attr1' => 'table1.attr1', 'attr2' => 'table2.attr2'})
  35. assert_equal ['table1.attr1 DESC'], sort_clause
  36. end
  37. def test_default_sort_clause_with_multiple_columns
  38. sort_init 'attr1', 'desc'
  39. sort_update({'attr1' => ['table1.attr1', 'table1.attr2'], 'attr2' => 'table2.attr2'})
  40. assert_equal ['table1.attr1 DESC', 'table1.attr2 DESC'], sort_clause
  41. end
  42. def test_params_sort
  43. @sort_param = 'attr1,attr2:desc'
  44. sort_init 'attr1', 'desc'
  45. sort_update({'attr1' => 'table1.attr1', 'attr2' => 'table2.attr2'})
  46. assert_equal ['table1.attr1 ASC', 'table2.attr2 DESC'], sort_clause
  47. assert_equal 'attr1,attr2:desc', @session['foo_bar_sort']
  48. end
  49. def test_invalid_params_sort
  50. @sort_param = 'invalid_key'
  51. sort_init 'attr1', 'desc'
  52. sort_update({'attr1' => 'table1.attr1', 'attr2' => 'table2.attr2'})
  53. assert_nil sort_clause
  54. assert_equal 'invalid_key', @session['foo_bar_sort']
  55. end
  56. def test_invalid_order_params_sort
  57. @sort_param = 'attr1:foo:bar,attr2'
  58. sort_init 'attr1', 'desc'
  59. sort_update({'attr1' => 'table1.attr1', 'attr2' => 'table2.attr2'})
  60. assert_equal ['table1.attr1 ASC', 'table2.attr2 ASC'], sort_clause
  61. assert_equal 'attr1,attr2', @session['foo_bar_sort']
  62. end
  63. def test_sort_css_without_params_should_use_default_sort
  64. sort_init 'attr1', 'desc'
  65. sort_update(['attr1', 'attr2'])
  66. assert_equal 'sort-by-attr1 sort-desc', sort_css_classes
  67. end
  68. def test_sort_css_should_use_params
  69. @sort_param = 'attr2,attr1'
  70. sort_init 'attr1', 'desc'
  71. sort_update(['attr1', 'attr2'])
  72. assert_equal 'sort-by-attr2 sort-asc', sort_css_classes
  73. end
  74. def test_sort_css_should_dasherize_sort_name
  75. sort_init 'foo_bar'
  76. sort_update(['foo_bar'])
  77. assert_equal 'sort-by-foo-bar sort-asc', sort_css_classes
  78. end
  79. private
  80. def controller_name; 'foo'; end
  81. def action_name; 'bar'; end
  82. def params; {:sort => @sort_param}; end
  83. def session; @session ||= {}; end
  84. end