@@ -1,117 +1,119 | |||||
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 |
|
34 | projects = Project.find([1]) | |
35 |
assert_equal |
|
35 | assert_equal [3, 2], Principal.member_of(projects).sort.map(&:id) | |
|
36 | projects = Project.find([1, 2]) | |||
|
37 | assert_equal [3, 2, 8, 11], Principal.member_of(projects).sort.map(&:id) | |||
36 | end |
|
38 | end | |
37 |
|
39 | |||
38 | def test_member_of_scope_should_be_empty_for_no_projects |
|
40 | def test_member_of_scope_should_be_empty_for_no_projects | |
39 | assert_equal [], Principal.member_of([]).sort |
|
41 | assert_equal [], Principal.member_of([]).sort | |
40 | end |
|
42 | end | |
41 |
|
43 | |||
42 | 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 | |
43 | [[1], [1, 2]].each do |ids| |
|
45 | [[1], [1, 2]].each do |ids| | |
44 | projects = Project.find(ids) |
|
46 | projects = Project.find(ids) | |
45 | assert_equal ids.size, projects.count |
|
47 | assert_equal ids.size, projects.count | |
46 | expected = (Principal.all - projects.map(&:memberships).flatten.map(&:principal)).sort |
|
48 | expected = (Principal.all - projects.map(&:memberships).flatten.map(&:principal)).sort | |
47 | assert_equal expected, Principal.not_member_of(projects).sort |
|
49 | assert_equal expected, Principal.not_member_of(projects).sort | |
48 | end |
|
50 | end | |
49 | end |
|
51 | end | |
50 |
|
52 | |||
51 | 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 | |
52 | assert_equal [], Principal.not_member_of([]).sort |
|
54 | assert_equal [], Principal.not_member_of([]).sort | |
53 | end |
|
55 | end | |
54 |
|
56 | |||
55 | def test_sorted_scope_should_sort_users_before_groups |
|
57 | def test_sorted_scope_should_sort_users_before_groups | |
56 | scope = Principal.where("type <> ?", 'AnonymousUser') |
|
58 | scope = Principal.where("type <> ?", 'AnonymousUser') | |
57 | expected_order = scope.all.sort do |a, b| |
|
59 | expected_order = scope.all.sort do |a, b| | |
58 | if a.is_a?(User) && b.is_a?(Group) |
|
60 | if a.is_a?(User) && b.is_a?(Group) | |
59 | -1 |
|
61 | -1 | |
60 | elsif a.is_a?(Group) && b.is_a?(User) |
|
62 | elsif a.is_a?(Group) && b.is_a?(User) | |
61 | 1 |
|
63 | 1 | |
62 | else |
|
64 | else | |
63 | a.name.downcase <=> b.name.downcase |
|
65 | a.name.downcase <=> b.name.downcase | |
64 | end |
|
66 | end | |
65 | end |
|
67 | end | |
66 | assert_equal expected_order.map(&:name).map(&:downcase), scope.sorted.all.map(&:name).map(&:downcase) |
|
68 | assert_equal expected_order.map(&:name).map(&:downcase), scope.sorted.all.map(&:name).map(&:downcase) | |
67 | end |
|
69 | end | |
68 |
|
70 | |||
69 | test "like scope should search login" do |
|
71 | test "like scope should search login" do | |
70 | results = Principal.like('jsmi') |
|
72 | results = Principal.like('jsmi') | |
71 |
|
73 | |||
72 | assert results.any? |
|
74 | assert results.any? | |
73 | assert results.all? {|u| u.login.match(/jsmi/i) } |
|
75 | assert results.all? {|u| u.login.match(/jsmi/i) } | |
74 | end |
|
76 | end | |
75 |
|
77 | |||
76 | test "like scope should search firstname" do |
|
78 | test "like scope should search firstname" do | |
77 | results = Principal.like('john') |
|
79 | results = Principal.like('john') | |
78 |
|
80 | |||
79 | assert results.any? |
|
81 | assert results.any? | |
80 | assert results.all? {|u| u.firstname.match(/john/i) } |
|
82 | assert results.all? {|u| u.firstname.match(/john/i) } | |
81 | end |
|
83 | end | |
82 |
|
84 | |||
83 | test "like scope should search lastname" do |
|
85 | test "like scope should search lastname" do | |
84 | results = Principal.like('smi') |
|
86 | results = Principal.like('smi') | |
85 |
|
87 | |||
86 | assert results.any? |
|
88 | assert results.any? | |
87 | assert results.all? {|u| u.lastname.match(/smi/i) } |
|
89 | assert results.all? {|u| u.lastname.match(/smi/i) } | |
88 | end |
|
90 | end | |
89 |
|
91 | |||
90 | test "like scope should search mail" do |
|
92 | test "like scope should search mail" do | |
91 | results = Principal.like('somenet') |
|
93 | results = Principal.like('somenet') | |
92 |
|
94 | |||
93 | assert results.any? |
|
95 | assert results.any? | |
94 | assert results.all? {|u| u.mail.match(/somenet/i) } |
|
96 | assert results.all? {|u| u.mail.match(/somenet/i) } | |
95 | end |
|
97 | end | |
96 |
|
98 | |||
97 | test "like scope should search firstname and lastname" do |
|
99 | test "like scope should search firstname and lastname" do | |
98 | results = Principal.like('john smi') |
|
100 | results = Principal.like('john smi') | |
99 |
|
101 | |||
100 | assert_equal 1, results.count |
|
102 | assert_equal 1, results.count | |
101 | assert_equal User.find(2), results.first |
|
103 | assert_equal User.find(2), results.first | |
102 | end |
|
104 | end | |
103 |
|
105 | |||
104 | test "like scope should search lastname and firstname" do |
|
106 | test "like scope should search lastname and firstname" do | |
105 | results = Principal.like('smith joh') |
|
107 | results = Principal.like('smith joh') | |
106 |
|
108 | |||
107 | assert_equal 1, results.count |
|
109 | assert_equal 1, results.count | |
108 | assert_equal User.find(2), results.first |
|
110 | assert_equal User.find(2), results.first | |
109 | end |
|
111 | end | |
110 |
|
112 | |||
111 | def test_like_scope_with_cyrillic_name |
|
113 | def test_like_scope_with_cyrillic_name | |
112 | user = User.generate!(:firstname => 'Соболев', :lastname => 'Денис') |
|
114 | user = User.generate!(:firstname => 'Соболев', :lastname => 'Денис') | |
113 | results = Principal.like('Собо') |
|
115 | results = Principal.like('Собо') | |
114 | assert_equal 1, results.count |
|
116 | assert_equal 1, results.count | |
115 | assert_equal user, results.first |
|
117 | assert_equal user, results.first | |
116 | end |
|
118 | end | |
117 | end |
|
119 | end |
General Comments 0
You need to be logged in to leave comments.
Login now