annotate .svn/pristine/3e/3e8fae3ebee67b280950bc292ca802d88a6d99d2.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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