Chris@1517: # Redmine - project management software Chris@1517: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1517: # Chris@1517: # This program is free software; you can redistribute it and/or Chris@1517: # modify it under the terms of the GNU General Public License Chris@1517: # as published by the Free Software Foundation; either version 2 Chris@1517: # of the License, or (at your option) any later version. Chris@1517: # Chris@1517: # This program is distributed in the hope that it will be useful, Chris@1517: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1517: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1517: # GNU General Public License for more details. Chris@1517: # Chris@1517: # You should have received a copy of the GNU General Public License Chris@1517: # along with this program; if not, write to the Free Software Chris@1517: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1517: Chris@1517: require File.expand_path('../../test_helper', __FILE__) Chris@1517: Chris@1517: class GroupTest < ActiveSupport::TestCase Chris@1517: fixtures :projects, :trackers, :issue_statuses, :issues, Chris@1517: :enumerations, :users, Chris@1517: :projects_trackers, Chris@1517: :roles, Chris@1517: :member_roles, Chris@1517: :members, Chris@1517: :groups_users Chris@1517: Chris@1517: include Redmine::I18n Chris@1517: Chris@1517: def test_create Chris@1517: g = Group.new(:name => 'New group') Chris@1517: assert g.save Chris@1517: g.reload Chris@1517: assert_equal 'New group', g.name Chris@1517: end Chris@1517: Chris@1517: def test_name_should_accept_255_characters Chris@1517: name = 'a' * 255 Chris@1517: g = Group.new(:name => name) Chris@1517: assert g.save Chris@1517: g.reload Chris@1517: assert_equal name, g.name Chris@1517: end Chris@1517: Chris@1517: def test_blank_name_error_message Chris@1517: set_language_if_valid 'en' Chris@1517: g = Group.new Chris@1517: assert !g.save Chris@1517: assert_include "Name can't be blank", g.errors.full_messages Chris@1517: end Chris@1517: Chris@1517: def test_blank_name_error_message_fr Chris@1517: set_language_if_valid 'fr' Chris@1517: str = "Nom doit \xc3\xaatre renseign\xc3\xa9(e)" Chris@1517: str.force_encoding('UTF-8') if str.respond_to?(:force_encoding) Chris@1517: g = Group.new Chris@1517: assert !g.save Chris@1517: assert_include str, g.errors.full_messages Chris@1517: end Chris@1517: Chris@1517: def test_group_roles_should_be_given_to_added_user Chris@1517: group = Group.find(11) Chris@1517: user = User.find(9) Chris@1517: project = Project.first Chris@1517: Chris@1517: Member.create!(:principal => group, :project => project, :role_ids => [1, 2]) Chris@1517: group.users << user Chris@1517: assert user.member_of?(project) Chris@1517: end Chris@1517: Chris@1517: def test_new_roles_should_be_given_to_existing_user Chris@1517: group = Group.find(11) Chris@1517: user = User.find(9) Chris@1517: project = Project.first Chris@1517: Chris@1517: group.users << user Chris@1517: m = Member.create!(:principal => group, :project => project, :role_ids => [1, 2]) Chris@1517: assert user.member_of?(project) Chris@1517: end Chris@1517: Chris@1517: def test_user_roles_should_updated_when_updating_user_ids Chris@1517: group = Group.find(11) Chris@1517: user = User.find(9) Chris@1517: project = Project.first Chris@1517: Chris@1517: Member.create!(:principal => group, :project => project, :role_ids => [1, 2]) Chris@1517: group.user_ids = [user.id] Chris@1517: group.save! Chris@1517: assert User.find(9).member_of?(project) Chris@1517: Chris@1517: group.user_ids = [1] Chris@1517: group.save! Chris@1517: assert !User.find(9).member_of?(project) Chris@1517: end Chris@1517: Chris@1517: def test_user_roles_should_updated_when_updating_group_roles Chris@1517: group = Group.find(11) Chris@1517: user = User.find(9) Chris@1517: project = Project.first Chris@1517: group.users << user Chris@1517: m = Member.create!(:principal => group, :project => project, :role_ids => [1]) Chris@1517: assert_equal [1], user.reload.roles_for_project(project).collect(&:id).sort Chris@1517: Chris@1517: m.role_ids = [1, 2] Chris@1517: assert_equal [1, 2], user.reload.roles_for_project(project).collect(&:id).sort Chris@1517: Chris@1517: m.role_ids = [2] Chris@1517: assert_equal [2], user.reload.roles_for_project(project).collect(&:id).sort Chris@1517: Chris@1517: m.role_ids = [1] Chris@1517: assert_equal [1], user.reload.roles_for_project(project).collect(&:id).sort Chris@1517: end Chris@1517: Chris@1517: def test_user_memberships_should_be_removed_when_removing_group_membership Chris@1517: assert User.find(8).member_of?(Project.find(5)) Chris@1517: Member.find_by_project_id_and_user_id(5, 10).destroy Chris@1517: assert !User.find(8).member_of?(Project.find(5)) Chris@1517: end Chris@1517: Chris@1517: def test_user_roles_should_be_removed_when_removing_user_from_group Chris@1517: assert User.find(8).member_of?(Project.find(5)) Chris@1517: User.find(8).groups = [] Chris@1517: assert !User.find(8).member_of?(Project.find(5)) Chris@1517: end Chris@1517: Chris@1517: def test_destroy_should_unassign_issues Chris@1517: group = Group.first Chris@1517: Issue.where(:id => 1).update_all(["assigned_to_id = ?", group.id]) Chris@1517: Chris@1517: assert group.destroy Chris@1517: assert group.destroyed? Chris@1517: Chris@1517: assert_equal nil, Issue.find(1).assigned_to_id Chris@1517: end Chris@1517: end