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