Chris@909: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@909: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@909: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@119: require File.expand_path('../../test_helper', __FILE__) Chris@0: Chris@0: class MemberTest < ActiveSupport::TestCase Chris@909: fixtures :projects, :trackers, :issue_statuses, :issues, Chris@909: :enumerations, :users, :issue_categories, Chris@909: :projects_trackers, Chris@909: :roles, Chris@909: :member_roles, Chris@909: :members, Chris@909: :enabled_modules, Chris@909: :groups_users, Chris@909: :watchers, Chris@909: :journals, :journal_details, Chris@909: :messages, Chris@909: :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions, Chris@909: :boards Chris@0: Chris@1115: include Redmine::I18n Chris@1115: Chris@0: def setup Chris@0: @jsmith = Member.find(1) Chris@0: end Chris@909: Chris@0: def test_create Chris@0: member = Member.new(:project_id => 1, :user_id => 4, :role_ids => [1, 2]) Chris@0: assert member.save Chris@0: member.reload Chris@909: Chris@0: assert_equal 2, member.roles.size Chris@0: assert_equal Role.find(1), member.roles.sort.first Chris@0: end Chris@0: Chris@909: def test_update Chris@0: assert_equal "eCookbook", @jsmith.project.name Chris@0: assert_equal "Manager", @jsmith.roles.first.name Chris@0: assert_equal "jsmith", @jsmith.user.login Chris@909: Chris@0: @jsmith.mail_notification = !@jsmith.mail_notification Chris@0: assert @jsmith.save Chris@0: end Chris@0: Chris@0: def test_update_roles Chris@0: assert_equal 1, @jsmith.roles.size Chris@0: @jsmith.role_ids = [1, 2] Chris@0: assert @jsmith.save Chris@0: assert_equal 2, @jsmith.reload.roles.size Chris@0: end Chris@909: Chris@0: def test_validate Chris@0: member = Member.new(:project_id => 1, :user_id => 2, :role_ids => [2]) Chris@0: # same use can't have more than one membership for a project Chris@0: assert !member.save Chris@909: Chris@0: # must have one role at least Chris@1115: user = User.new(:firstname => "new1", :lastname => "user1", :mail => "test_validate@somenet.foo") Chris@1115: user.login = "test_validate" Chris@1115: user.password, user.password_confirmation = "password", "password" Chris@1115: assert user.save Chris@1115: Chris@1115: set_language_if_valid 'fr' Chris@1115: member = Member.new(:project_id => 1, :user_id => user.id, :role_ids => []) Chris@1115: assert !member.save Chris@1115: assert_include I18n.translate('activerecord.errors.messages.empty'), member.errors[:role] Chris@1115: str = "R\xc3\xb4le doit \xc3\xaatre renseign\xc3\xa9(e)" Chris@1115: str.force_encoding('UTF-8') if str.respond_to?(:force_encoding) Chris@1115: assert_equal str, [member.errors.full_messages].flatten.join Chris@1115: end Chris@1115: Chris@1115: def test_validate_member_role Chris@1115: user = User.new(:firstname => "new1", :lastname => "user1", :mail => "test_validate@somenet.foo") Chris@1115: user.login = "test_validate_member_role" Chris@1115: user.password, user.password_confirmation = "password", "password" Chris@1115: assert user.save Chris@1115: member = Member.new(:project_id => 1, :user_id => user.id, :role_ids => [5]) Chris@0: assert !member.save Chris@0: end Chris@909: Chris@0: def test_destroy Chris@1115: category1 = IssueCategory.find(1) Chris@1115: assert_equal @jsmith.user.id, category1.assigned_to_id Chris@0: assert_difference 'Member.count', -1 do Chris@0: assert_difference 'MemberRole.count', -1 do Chris@0: @jsmith.destroy Chris@0: end Chris@0: end Chris@0: assert_raise(ActiveRecord::RecordNotFound) { Member.find(@jsmith.id) } Chris@1115: category1.reload Chris@1115: assert_nil category1.assigned_to_id Chris@0: end Chris@909: Chris@929: def test_sort_without_roles Chris@929: a = Member.new(:roles => [Role.first]) Chris@929: b = Member.new Chris@929: Chris@929: assert_equal -1, a <=> b Chris@929: assert_equal 1, b <=> a Chris@929: end Chris@929: Chris@929: def test_sort_without_principal Chris@929: role = Role.first Chris@929: a = Member.new(:roles => [role], :principal => User.first) Chris@929: b = Member.new(:roles => [role]) Chris@929: Chris@929: assert_equal -1, a <=> b Chris@929: assert_equal 1, b <=> a Chris@929: end Chris@0: end