comparison test/unit/principal_test.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children e248c7af89ec
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
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-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.
47 47
48 def test_not_member_of_scope_should_be_empty_for_no_projects 48 def test_not_member_of_scope_should_be_empty_for_no_projects
49 assert_equal [], Principal.not_member_of([]).sort 49 assert_equal [], Principal.not_member_of([]).sort
50 end 50 end
51 51
52 context "#like" do 52 def test_sorted_scope_should_sort_users_before_groups
53 setup do 53 scope = Principal.where("type <> ?", 'AnonymousUser')
54 Principal.create!(:login => 'login') 54 expected_order = scope.all.sort do |a, b|
55 Principal.create!(:login => 'login2') 55 if a.is_a?(User) && b.is_a?(Group)
56 -1
57 elsif a.is_a?(Group) && b.is_a?(User)
58 1
59 else
60 a.name.downcase <=> b.name.downcase
61 end
62 end
63 assert_equal expected_order.map(&:name).map(&:downcase), scope.sorted.all.map(&:name).map(&:downcase)
64 end
56 65
57 Principal.create!(:firstname => 'firstname') 66 test "like scope should search login" do
58 Principal.create!(:firstname => 'firstname2') 67 results = Principal.like('jsmi')
59 68
60 Principal.create!(:lastname => 'lastname') 69 assert results.any?
61 Principal.create!(:lastname => 'lastname2') 70 assert results.all? {|u| u.login.match(/jsmi/i) }
71 end
62 72
63 Principal.create!(:mail => 'mail@example.com') 73 test "like scope should search firstname" do
64 Principal.create!(:mail => 'mail2@example.com') 74 results = Principal.like('john')
65 75
66 @palmer = Principal.create!(:firstname => 'David', :lastname => 'Palmer') 76 assert results.any?
67 end 77 assert results.all? {|u| u.firstname.match(/john/i) }
78 end
68 79
69 should "search login" do 80 test "like scope should search lastname" do
70 results = Principal.like('login') 81 results = Principal.like('smi')
71 82
72 assert_equal 2, results.count 83 assert results.any?
73 assert results.all? {|u| u.login.match(/login/) } 84 assert results.all? {|u| u.lastname.match(/smi/i) }
74 end 85 end
75 86
76 should "search firstname" do 87 test "like scope should search mail" do
77 results = Principal.like('firstname') 88 results = Principal.like('somenet')
78 89
79 assert_equal 2, results.count 90 assert results.any?
80 assert results.all? {|u| u.firstname.match(/firstname/) } 91 assert results.all? {|u| u.mail.match(/somenet/i) }
81 end 92 end
82 93
83 should "search lastname" do 94 test "like scope should search firstname and lastname" do
84 results = Principal.like('lastname') 95 results = Principal.like('john smi')
85 96
86 assert_equal 2, results.count 97 assert_equal 1, results.count
87 assert results.all? {|u| u.lastname.match(/lastname/) } 98 assert_equal User.find(2), results.first
88 end 99 end
89 100
90 should "search mail" do 101 test "like scope should search lastname and firstname" do
91 results = Principal.like('mail') 102 results = Principal.like('smith joh')
92 103
93 assert_equal 2, results.count 104 assert_equal 1, results.count
94 assert results.all? {|u| u.mail.match(/mail/) } 105 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 106 end
111 107
112 def test_like_scope_with_cyrillic_name 108 def test_like_scope_with_cyrillic_name
113 user = User.generate!(:firstname => 'Соболев', :lastname => 'Денис') 109 user = User.generate!(:firstname => 'Соболев', :lastname => 'Денис')
114 results = Principal.like('Собо') 110 results = Principal.like('Собо')