comparison test/unit/principal_test.rb @ 1526:404aa68d4227

Merge from live branch
author Chris Cannam
date Thu, 11 Sep 2014 12:46:20 +0100
parents dffacf8a6908
children
comparison
equal deleted inserted replaced
1493:a5f2bdf3b486 1526:404aa68d4227
1 # encoding: utf-8 1 # encoding: utf-8
2 # 2 #
3 # Redmine - project management software 3 # Redmine - project management software
4 # Copyright (C) 2006-2012 Jean-Philippe Lang 4 # Copyright (C) 2006-2014 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.
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_all_by_id(1, 2) 34 projects = Project.find([1])
35 assert_equal projects.map(&:principals).flatten.sort, Principal.member_of(projects).sort 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 projects = Project.find_all_by_id(1, 2) 45 [[1], [1, 2]].each do |ids|
44 expected = (Principal.all - projects.map(&:memberships).flatten.map(&:principal)).sort 46 projects = Project.find(ids)
45 assert_equal expected, Principal.not_member_of(projects).sort 47 assert_equal ids.size, projects.count
48 expected = (Principal.all - projects.map(&:memberships).flatten.map(&:principal)).sort
49 assert_equal expected, Principal.not_member_of(projects).sort
50 end
46 end 51 end
47 52
48 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
49 assert_equal [], Principal.not_member_of([]).sort 54 assert_equal [], Principal.not_member_of([]).sort
50 end 55 end
51 56
52 context "#like" do 57 def test_sorted_scope_should_sort_users_before_groups
53 setup do 58 scope = Principal.where("type <> ?", 'AnonymousUser')
54 Principal.create!(:login => 'login') 59 expected_order = scope.all.sort do |a, b|
55 Principal.create!(:login => 'login2') 60 if a.is_a?(User) && b.is_a?(Group)
61 -1
62 elsif a.is_a?(Group) && b.is_a?(User)
63 1
64 else
65 a.name.downcase <=> b.name.downcase
66 end
67 end
68 assert_equal expected_order.map(&:name).map(&:downcase),
69 scope.sorted.map(&:name).map(&:downcase)
70 end
56 71
57 Principal.create!(:firstname => 'firstname') 72 test "like scope should search login" do
58 Principal.create!(:firstname => 'firstname2') 73 results = Principal.like('jsmi')
59 74
60 Principal.create!(:lastname => 'lastname') 75 assert results.any?
61 Principal.create!(:lastname => 'lastname2') 76 assert results.all? {|u| u.login.match(/jsmi/i) }
77 end
62 78
63 Principal.create!(:mail => 'mail@example.com') 79 test "like scope should search firstname" do
64 Principal.create!(:mail => 'mail2@example.com') 80 results = Principal.like('john')
65 81
66 @palmer = Principal.create!(:firstname => 'David', :lastname => 'Palmer') 82 assert results.any?
67 end 83 assert results.all? {|u| u.firstname.match(/john/i) }
84 end
68 85
69 should "search login" do 86 test "like scope should search lastname" do
70 results = Principal.like('login') 87 results = Principal.like('smi')
71 88
72 assert_equal 2, results.count 89 assert results.any?
73 assert results.all? {|u| u.login.match(/login/) } 90 assert results.all? {|u| u.lastname.match(/smi/i) }
74 end 91 end
75 92
76 should "search firstname" do 93 test "like scope should search mail" do
77 results = Principal.like('firstname') 94 results = Principal.like('somenet')
78 95
79 assert_equal 2, results.count 96 assert results.any?
80 assert results.all? {|u| u.firstname.match(/firstname/) } 97 assert results.all? {|u| u.mail.match(/somenet/i) }
81 end 98 end
82 99
83 should "search lastname" do 100 test "like scope should search firstname and lastname" do
84 results = Principal.like('lastname') 101 results = Principal.like('john smi')
85 102
86 assert_equal 2, results.count 103 assert_equal 1, results.count
87 assert results.all? {|u| u.lastname.match(/lastname/) } 104 assert_equal User.find(2), results.first
88 end 105 end
89 106
90 should "search mail" do 107 test "like scope should search lastname and firstname" do
91 results = Principal.like('mail') 108 results = Principal.like('smith joh')
92 109
93 assert_equal 2, results.count 110 assert_equal 1, results.count
94 assert results.all? {|u| u.mail.match(/mail/) } 111 assert_equal User.find(2), results.first
95 end
96
97 should "search firstname and lastname" do
98 results = Principal.like('david palm')
99
100 assert_equal 1, results.count
101 assert_equal @palmer, results.first
102 end
103
104 should "search lastname and firstname" do
105 results = Principal.like('palmer davi')
106
107 assert_equal 1, results.count
108 assert_equal @palmer, results.first
109 end
110 end 112 end
111 113
112 def test_like_scope_with_cyrillic_name 114 def test_like_scope_with_cyrillic_name
113 user = User.generate!(:firstname => 'Соболев', :lastname => 'Денис') 115 user = User.generate!(:firstname => 'Соболев', :lastname => 'Денис')
114 results = Principal.like('Собо') 116 results = Principal.like('Собо')