Chris@909
|
1 # Redmine - project management software
|
Chris@1494
|
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@909
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@909
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@119
|
18 require File.expand_path('../../test_helper', __FILE__)
|
Chris@0
|
19
|
Chris@0
|
20 class MemberTest < ActiveSupport::TestCase
|
Chris@909
|
21 fixtures :projects, :trackers, :issue_statuses, :issues,
|
Chris@909
|
22 :enumerations, :users, :issue_categories,
|
Chris@909
|
23 :projects_trackers,
|
Chris@909
|
24 :roles,
|
Chris@909
|
25 :member_roles,
|
Chris@909
|
26 :members,
|
Chris@909
|
27 :enabled_modules,
|
Chris@909
|
28 :groups_users,
|
Chris@909
|
29 :watchers,
|
Chris@909
|
30 :journals, :journal_details,
|
Chris@909
|
31 :messages,
|
Chris@909
|
32 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
|
Chris@909
|
33 :boards
|
Chris@0
|
34
|
Chris@1115
|
35 include Redmine::I18n
|
Chris@1115
|
36
|
Chris@0
|
37 def setup
|
Chris@0
|
38 @jsmith = Member.find(1)
|
Chris@0
|
39 end
|
Chris@909
|
40
|
Chris@0
|
41 def test_create
|
Chris@0
|
42 member = Member.new(:project_id => 1, :user_id => 4, :role_ids => [1, 2])
|
Chris@0
|
43 assert member.save
|
Chris@0
|
44 member.reload
|
Chris@909
|
45
|
Chris@0
|
46 assert_equal 2, member.roles.size
|
Chris@0
|
47 assert_equal Role.find(1), member.roles.sort.first
|
Chris@0
|
48 end
|
Chris@0
|
49
|
Chris@909
|
50 def test_update
|
Chris@0
|
51 assert_equal "eCookbook", @jsmith.project.name
|
Chris@0
|
52 assert_equal "Manager", @jsmith.roles.first.name
|
Chris@0
|
53 assert_equal "jsmith", @jsmith.user.login
|
Chris@909
|
54
|
Chris@0
|
55 @jsmith.mail_notification = !@jsmith.mail_notification
|
Chris@0
|
56 assert @jsmith.save
|
Chris@0
|
57 end
|
Chris@0
|
58
|
Chris@0
|
59 def test_update_roles
|
Chris@0
|
60 assert_equal 1, @jsmith.roles.size
|
Chris@0
|
61 @jsmith.role_ids = [1, 2]
|
Chris@0
|
62 assert @jsmith.save
|
Chris@0
|
63 assert_equal 2, @jsmith.reload.roles.size
|
Chris@0
|
64 end
|
Chris@909
|
65
|
Chris@0
|
66 def test_validate
|
Chris@0
|
67 member = Member.new(:project_id => 1, :user_id => 2, :role_ids => [2])
|
Chris@0
|
68 # same use can't have more than one membership for a project
|
Chris@0
|
69 assert !member.save
|
Chris@909
|
70
|
Chris@0
|
71 # must have one role at least
|
Chris@1517
|
72 user = User.new(:firstname => "new1", :lastname => "user1",
|
Chris@1517
|
73 :mail => "test_validate@somenet.foo")
|
Chris@1115
|
74 user.login = "test_validate"
|
Chris@1115
|
75 user.password, user.password_confirmation = "password", "password"
|
Chris@1115
|
76 assert user.save
|
Chris@1115
|
77
|
Chris@1115
|
78 set_language_if_valid 'fr'
|
Chris@1115
|
79 member = Member.new(:project_id => 1, :user_id => user.id, :role_ids => [])
|
Chris@1115
|
80 assert !member.save
|
Chris@1115
|
81 assert_include I18n.translate('activerecord.errors.messages.empty'), member.errors[:role]
|
Chris@1115
|
82 str = "R\xc3\xb4le doit \xc3\xaatre renseign\xc3\xa9(e)"
|
Chris@1115
|
83 str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
|
Chris@1115
|
84 assert_equal str, [member.errors.full_messages].flatten.join
|
Chris@1115
|
85 end
|
Chris@1115
|
86
|
Chris@1115
|
87 def test_validate_member_role
|
Chris@1517
|
88 user = User.new(:firstname => "new1", :lastname => "user1",
|
Chris@1517
|
89 :mail => "test_validate@somenet.foo")
|
Chris@1115
|
90 user.login = "test_validate_member_role"
|
Chris@1115
|
91 user.password, user.password_confirmation = "password", "password"
|
Chris@1115
|
92 assert user.save
|
Chris@1115
|
93 member = Member.new(:project_id => 1, :user_id => user.id, :role_ids => [5])
|
Chris@0
|
94 assert !member.save
|
Chris@0
|
95 end
|
Chris@909
|
96
|
Chris@0
|
97 def test_destroy
|
Chris@1115
|
98 category1 = IssueCategory.find(1)
|
Chris@1115
|
99 assert_equal @jsmith.user.id, category1.assigned_to_id
|
Chris@0
|
100 assert_difference 'Member.count', -1 do
|
Chris@0
|
101 assert_difference 'MemberRole.count', -1 do
|
Chris@0
|
102 @jsmith.destroy
|
Chris@0
|
103 end
|
Chris@0
|
104 end
|
Chris@0
|
105 assert_raise(ActiveRecord::RecordNotFound) { Member.find(@jsmith.id) }
|
Chris@1115
|
106 category1.reload
|
Chris@1115
|
107 assert_nil category1.assigned_to_id
|
Chris@0
|
108 end
|
Chris@909
|
109
|
Chris@1517
|
110 def test_destroy_should_trigger_callbacks_only_once
|
Chris@1517
|
111 Member.class_eval { def destroy_test_callback; end}
|
Chris@1517
|
112 Member.after_destroy :destroy_test_callback
|
Chris@1517
|
113
|
Chris@1517
|
114 m = Member.create!(:user_id => 1, :project_id => 1, :role_ids => [1,3])
|
Chris@1517
|
115
|
Chris@1517
|
116 Member.any_instance.expects(:destroy_test_callback).once
|
Chris@1517
|
117 assert_difference 'Member.count', -1 do
|
Chris@1517
|
118 assert_difference 'MemberRole.count', -2 do
|
Chris@1517
|
119 m.destroy
|
Chris@1517
|
120 end
|
Chris@1517
|
121 end
|
Chris@1517
|
122 assert m.destroyed?
|
Chris@1517
|
123 ensure
|
Chris@1517
|
124 Member._destroy_callbacks.reject! {|c| c.filter==:destroy_test_callback}
|
Chris@1517
|
125 end
|
Chris@1517
|
126
|
Chris@929
|
127 def test_sort_without_roles
|
Chris@929
|
128 a = Member.new(:roles => [Role.first])
|
Chris@929
|
129 b = Member.new
|
Chris@929
|
130
|
Chris@929
|
131 assert_equal -1, a <=> b
|
Chris@929
|
132 assert_equal 1, b <=> a
|
Chris@929
|
133 end
|
Chris@929
|
134
|
Chris@929
|
135 def test_sort_without_principal
|
Chris@929
|
136 role = Role.first
|
Chris@929
|
137 a = Member.new(:roles => [role], :principal => User.first)
|
Chris@929
|
138 b = Member.new(:roles => [role])
|
Chris@929
|
139
|
Chris@929
|
140 assert_equal -1, a <=> b
|
Chris@929
|
141 assert_equal 1, b <=> a
|
Chris@929
|
142 end
|
Chris@0
|
143 end
|