Chris@0: # Redmine - project management software Chris@1115: # Copyright (C) 2006-2012 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 GroupTest < 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: :workflows, Chris@909: :groups_users Chris@909: Chris@909: include Redmine::I18n Chris@0: Chris@0: def test_create Chris@1115: g = Group.new(:name => 'New group') Chris@0: assert g.save Chris@1115: g.reload Chris@1115: assert_equal 'New group', g.name Chris@0: end Chris@909: Chris@909: def test_blank_name_error_message Chris@909: set_language_if_valid 'en' Chris@909: g = Group.new Chris@909: assert !g.save Chris@909: assert_include "Name can't be blank", g.errors.full_messages Chris@909: end Chris@909: Chris@909: def test_blank_name_error_message_fr Chris@909: set_language_if_valid 'fr' Chris@909: str = "Nom doit \xc3\xaatre renseign\xc3\xa9(e)" Chris@909: str.force_encoding('UTF-8') if str.respond_to?(:force_encoding) Chris@909: g = Group.new Chris@909: assert !g.save Chris@909: assert_include str, g.errors.full_messages Chris@909: end Chris@909: Chris@1115: def test_group_roles_should_be_given_to_added_user Chris@0: group = Group.find(11) Chris@0: user = User.find(9) Chris@0: project = Project.first Chris@909: Chris@0: Member.create!(:principal => group, :project => project, :role_ids => [1, 2]) Chris@0: group.users << user Chris@0: assert user.member_of?(project) Chris@0: end Chris@909: Chris@1115: def test_new_roles_should_be_given_to_existing_user Chris@0: group = Group.find(11) Chris@0: user = User.find(9) Chris@0: project = Project.first Chris@909: Chris@0: group.users << user Chris@0: m = Member.create!(:principal => group, :project => project, :role_ids => [1, 2]) Chris@0: assert user.member_of?(project) Chris@0: end Chris@909: Chris@1115: def test_user_roles_should_updated_when_updating_user_ids Chris@1115: group = Group.find(11) Chris@1115: user = User.find(9) Chris@1115: project = Project.first Chris@1115: Chris@1115: Member.create!(:principal => group, :project => project, :role_ids => [1, 2]) Chris@1115: group.user_ids = [user.id] Chris@1115: group.save! Chris@1115: assert User.find(9).member_of?(project) Chris@1115: Chris@1115: group.user_ids = [1] Chris@1115: group.save! Chris@1115: assert !User.find(9).member_of?(project) Chris@1115: end Chris@1115: Chris@1115: def test_user_roles_should_updated_when_updating_group_roles Chris@0: group = Group.find(11) Chris@0: user = User.find(9) Chris@0: project = Project.first Chris@0: group.users << user Chris@0: m = Member.create!(:principal => group, :project => project, :role_ids => [1]) Chris@0: assert_equal [1], user.reload.roles_for_project(project).collect(&:id).sort Chris@909: Chris@0: m.role_ids = [1, 2] Chris@0: assert_equal [1, 2], user.reload.roles_for_project(project).collect(&:id).sort Chris@909: Chris@0: m.role_ids = [2] Chris@0: assert_equal [2], user.reload.roles_for_project(project).collect(&:id).sort Chris@909: Chris@0: m.role_ids = [1] Chris@0: assert_equal [1], user.reload.roles_for_project(project).collect(&:id).sort Chris@0: end Chris@0: Chris@1115: def test_user_memberships_should_be_removed_when_removing_group_membership Chris@0: assert User.find(8).member_of?(Project.find(5)) Chris@0: Member.find_by_project_id_and_user_id(5, 10).destroy Chris@0: assert !User.find(8).member_of?(Project.find(5)) Chris@0: end Chris@0: Chris@1115: def test_user_roles_should_be_removed_when_removing_user_from_group Chris@0: assert User.find(8).member_of?(Project.find(5)) Chris@1115: User.find(8).groups = [] Chris@0: assert !User.find(8).member_of?(Project.find(5)) Chris@0: end Chris@909: Chris@909: def test_destroy_should_unassign_issues Chris@909: group = Group.first Chris@909: Issue.update_all(["assigned_to_id = ?", group.id], 'id = 1') Chris@909: Chris@909: assert group.destroy Chris@909: assert group.destroyed? Chris@909: Chris@909: assert_equal nil, Issue.find(1).assigned_to_id Chris@909: end Chris@0: end