You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sort_helper_test.rb 2.6KB

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