Chris@1115: # encoding: utf-8 Chris@1115: # Chris@909: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@909: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@909: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@119: require File.expand_path('../../test_helper', __FILE__) Chris@0: Chris@0: class PrincipalTest < ActiveSupport::TestCase Chris@929: fixtures :users, :projects, :members, :member_roles Chris@929: Chris@1115: def test_active_scope_should_return_groups_and_active_users Chris@1115: result = Principal.active.all Chris@1115: assert_include Group.first, result Chris@1115: assert_not_nil result.detect {|p| p.is_a?(User)} Chris@1115: assert_nil result.detect {|p| p.is_a?(User) && !p.active?} Chris@1115: assert_nil result.detect {|p| p.is_a?(AnonymousUser)} Chris@1115: end Chris@1115: Chris@1115: def test_member_of_scope_should_return_the_union_of_all_members Chris@1517: projects = Project.find([1]) Chris@1517: assert_equal [3, 2], Principal.member_of(projects).sort.map(&:id) Chris@1517: projects = Project.find([1, 2]) Chris@1517: assert_equal [3, 2, 8, 11], Principal.member_of(projects).sort.map(&:id) Chris@1115: end Chris@1115: Chris@1115: def test_member_of_scope_should_be_empty_for_no_projects Chris@1115: assert_equal [], Principal.member_of([]).sort Chris@1115: end Chris@1115: Chris@929: def test_not_member_of_scope_should_return_users_that_have_no_memberships Chris@1517: [[1], [1, 2]].each do |ids| Chris@1517: projects = Project.find(ids) Chris@1517: assert_equal ids.size, projects.count Chris@1517: expected = (Principal.all - projects.map(&:memberships).flatten.map(&:principal)).sort Chris@1517: assert_equal expected, Principal.not_member_of(projects).sort Chris@1517: end Chris@929: end Chris@0: Chris@1115: def test_not_member_of_scope_should_be_empty_for_no_projects Chris@1115: assert_equal [], Principal.not_member_of([]).sort Chris@1115: end Chris@1115: Chris@1464: def test_sorted_scope_should_sort_users_before_groups Chris@1464: scope = Principal.where("type <> ?", 'AnonymousUser') Chris@1464: expected_order = scope.all.sort do |a, b| Chris@1464: if a.is_a?(User) && b.is_a?(Group) Chris@1464: -1 Chris@1464: elsif a.is_a?(Group) && b.is_a?(User) Chris@1464: 1 Chris@1464: else Chris@1464: a.name.downcase <=> b.name.downcase Chris@1464: end Chris@1464: end Chris@1517: assert_equal expected_order.map(&:name).map(&:downcase), Chris@1517: scope.sorted.map(&:name).map(&:downcase) Chris@1464: end Chris@0: Chris@1464: test "like scope should search login" do Chris@1464: results = Principal.like('jsmi') Chris@0: Chris@1464: assert results.any? Chris@1464: assert results.all? {|u| u.login.match(/jsmi/i) } Chris@1464: end Chris@0: Chris@1464: test "like scope should search firstname" do Chris@1464: results = Principal.like('john') Chris@1115: Chris@1464: assert results.any? Chris@1464: assert results.all? {|u| u.firstname.match(/john/i) } Chris@1464: end Chris@909: Chris@1464: test "like scope should search lastname" do Chris@1464: results = Principal.like('smi') Chris@0: Chris@1464: assert results.any? Chris@1464: assert results.all? {|u| u.lastname.match(/smi/i) } Chris@1464: end Chris@0: Chris@1464: test "like scope should search mail" do Chris@1464: results = Principal.like('somenet') Chris@0: Chris@1464: assert results.any? Chris@1464: assert results.all? {|u| u.mail.match(/somenet/i) } Chris@1464: end Chris@0: Chris@1464: test "like scope should search firstname and lastname" do Chris@1464: results = Principal.like('john smi') Chris@0: Chris@1464: assert_equal 1, results.count Chris@1464: assert_equal User.find(2), results.first Chris@1464: end Chris@0: Chris@1464: test "like scope should search lastname and firstname" do Chris@1464: results = Principal.like('smith joh') Chris@0: Chris@1464: assert_equal 1, results.count Chris@1464: assert_equal User.find(2), results.first Chris@0: end Chris@909: Chris@1115: def test_like_scope_with_cyrillic_name Chris@1115: user = User.generate!(:firstname => 'Соболев', :lastname => 'Денис') Chris@1115: results = Principal.like('Собо') Chris@1115: assert_equal 1, results.count Chris@1115: assert_equal user, results.first Chris@1115: end Chris@0: end