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