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.

principal_test.rb 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # encoding: utf-8
  2. #
  3. # Redmine - project management software
  4. # Copyright (C) 2006-2014 Jean-Philippe Lang
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. require File.expand_path('../../test_helper', __FILE__)
  20. class PrincipalTest < ActiveSupport::TestCase
  21. fixtures :users, :projects, :members, :member_roles
  22. def test_active_scope_should_return_groups_and_active_users
  23. result = Principal.active.all
  24. assert_include Group.first, result
  25. assert_not_nil result.detect {|p| p.is_a?(User)}
  26. assert_nil result.detect {|p| p.is_a?(User) && !p.active?}
  27. assert_nil result.detect {|p| p.is_a?(AnonymousUser)}
  28. end
  29. def test_member_of_scope_should_return_the_union_of_all_members
  30. projects = Project.find_all_by_id(1, 2)
  31. assert_equal projects.map(&:principals).flatten.sort, Principal.member_of(projects).sort
  32. end
  33. def test_member_of_scope_should_be_empty_for_no_projects
  34. assert_equal [], Principal.member_of([]).sort
  35. end
  36. def test_not_member_of_scope_should_return_users_that_have_no_memberships
  37. projects = Project.find_all_by_id(1, 2)
  38. expected = (Principal.all - projects.map(&:memberships).flatten.map(&:principal)).sort
  39. assert_equal expected, Principal.not_member_of(projects).sort
  40. end
  41. def test_not_member_of_scope_should_be_empty_for_no_projects
  42. assert_equal [], Principal.not_member_of([]).sort
  43. end
  44. def test_sorted_scope_should_sort_users_before_groups
  45. scope = Principal.where("type <> ?", 'AnonymousUser')
  46. expected_order = scope.all.sort do |a, b|
  47. if a.is_a?(User) && b.is_a?(Group)
  48. -1
  49. elsif a.is_a?(Group) && b.is_a?(User)
  50. 1
  51. else
  52. a.name.downcase <=> b.name.downcase
  53. end
  54. end
  55. assert_equal expected_order.map(&:name).map(&:downcase), scope.sorted.all.map(&:name).map(&:downcase)
  56. end
  57. test "like scope should search login" do
  58. results = Principal.like('jsmi')
  59. assert results.any?
  60. assert results.all? {|u| u.login.match(/jsmi/i) }
  61. end
  62. test "like scope should search firstname" do
  63. results = Principal.like('john')
  64. assert results.any?
  65. assert results.all? {|u| u.firstname.match(/john/i) }
  66. end
  67. test "like scope should search lastname" do
  68. results = Principal.like('smi')
  69. assert results.any?
  70. assert results.all? {|u| u.lastname.match(/smi/i) }
  71. end
  72. test "like scope should search mail" do
  73. results = Principal.like('somenet')
  74. assert results.any?
  75. assert results.all? {|u| u.mail.match(/somenet/i) }
  76. end
  77. test "like scope should search firstname and lastname" do
  78. results = Principal.like('john smi')
  79. assert_equal 1, results.count
  80. assert_equal User.find(2), results.first
  81. end
  82. test "like scope should search lastname and firstname" do
  83. results = Principal.like('smith joh')
  84. assert_equal 1, results.count
  85. assert_equal User.find(2), results.first
  86. end
  87. def test_like_scope_with_cyrillic_name
  88. user = User.generate!(:firstname => 'Соболев', :lastname => 'Денис')
  89. results = Principal.like('Собо')
  90. assert_equal 1, results.count
  91. assert_equal user, results.first
  92. end
  93. end