annotate .svn/pristine/57/57004ea0d71683df384c4e3126bc90d754acca49.svn-base @ 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
rev   line source
Chris@1295 1 # Redmine - project management software
Chris@1295 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@1295 3 #
Chris@1295 4 # This program is free software; you can redistribute it and/or
Chris@1295 5 # modify it under the terms of the GNU General Public License
Chris@1295 6 # as published by the Free Software Foundation; either version 2
Chris@1295 7 # of the License, or (at your option) any later version.
Chris@1295 8 #
Chris@1295 9 # This program is distributed in the hope that it will be useful,
Chris@1295 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 12 # GNU General Public License for more details.
Chris@1295 13 #
Chris@1295 14 # You should have received a copy of the GNU General Public License
Chris@1295 15 # along with this program; if not, write to the Free Software
Chris@1295 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 17
Chris@1295 18 require File.expand_path('../../test_helper', __FILE__)
Chris@1295 19
Chris@1295 20 class MemberTest < ActiveSupport::TestCase
Chris@1295 21 fixtures :projects, :trackers, :issue_statuses, :issues,
Chris@1295 22 :enumerations, :users, :issue_categories,
Chris@1295 23 :projects_trackers,
Chris@1295 24 :roles,
Chris@1295 25 :member_roles,
Chris@1295 26 :members,
Chris@1295 27 :enabled_modules,
Chris@1295 28 :workflows,
Chris@1295 29 :groups_users,
Chris@1295 30 :watchers,
Chris@1295 31 :journals, :journal_details,
Chris@1295 32 :messages,
Chris@1295 33 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions,
Chris@1295 34 :boards
Chris@1295 35
Chris@1295 36 include Redmine::I18n
Chris@1295 37
Chris@1295 38 def setup
Chris@1295 39 @jsmith = Member.find(1)
Chris@1295 40 end
Chris@1295 41
Chris@1295 42 def test_create
Chris@1295 43 member = Member.new(:project_id => 1, :user_id => 4, :role_ids => [1, 2])
Chris@1295 44 assert member.save
Chris@1295 45 member.reload
Chris@1295 46
Chris@1295 47 assert_equal 2, member.roles.size
Chris@1295 48 assert_equal Role.find(1), member.roles.sort.first
Chris@1295 49 end
Chris@1295 50
Chris@1295 51 def test_update
Chris@1295 52 assert_equal "eCookbook", @jsmith.project.name
Chris@1295 53 assert_equal "Manager", @jsmith.roles.first.name
Chris@1295 54 assert_equal "jsmith", @jsmith.user.login
Chris@1295 55
Chris@1295 56 @jsmith.mail_notification = !@jsmith.mail_notification
Chris@1295 57 assert @jsmith.save
Chris@1295 58 end
Chris@1295 59
Chris@1295 60 def test_update_roles
Chris@1295 61 assert_equal 1, @jsmith.roles.size
Chris@1295 62 @jsmith.role_ids = [1, 2]
Chris@1295 63 assert @jsmith.save
Chris@1295 64 assert_equal 2, @jsmith.reload.roles.size
Chris@1295 65 end
Chris@1295 66
Chris@1295 67 def test_validate
Chris@1295 68 member = Member.new(:project_id => 1, :user_id => 2, :role_ids => [2])
Chris@1295 69 # same use can't have more than one membership for a project
Chris@1295 70 assert !member.save
Chris@1295 71
Chris@1295 72 # must have one role at least
Chris@1295 73 user = User.new(:firstname => "new1", :lastname => "user1", :mail => "test_validate@somenet.foo")
Chris@1295 74 user.login = "test_validate"
Chris@1295 75 user.password, user.password_confirmation = "password", "password"
Chris@1295 76 assert user.save
Chris@1295 77
Chris@1295 78 set_language_if_valid 'fr'
Chris@1295 79 member = Member.new(:project_id => 1, :user_id => user.id, :role_ids => [])
Chris@1295 80 assert !member.save
Chris@1295 81 assert_include I18n.translate('activerecord.errors.messages.empty'), member.errors[:role]
Chris@1295 82 str = "R\xc3\xb4le doit \xc3\xaatre renseign\xc3\xa9(e)"
Chris@1295 83 str.force_encoding('UTF-8') if str.respond_to?(:force_encoding)
Chris@1295 84 assert_equal str, [member.errors.full_messages].flatten.join
Chris@1295 85 end
Chris@1295 86
Chris@1295 87 def test_validate_member_role
Chris@1295 88 user = User.new(:firstname => "new1", :lastname => "user1", :mail => "test_validate@somenet.foo")
Chris@1295 89 user.login = "test_validate_member_role"
Chris@1295 90 user.password, user.password_confirmation = "password", "password"
Chris@1295 91 assert user.save
Chris@1295 92 member = Member.new(:project_id => 1, :user_id => user.id, :role_ids => [5])
Chris@1295 93 assert !member.save
Chris@1295 94 end
Chris@1295 95
Chris@1295 96 def test_destroy
Chris@1295 97 category1 = IssueCategory.find(1)
Chris@1295 98 assert_equal @jsmith.user.id, category1.assigned_to_id
Chris@1295 99 assert_difference 'Member.count', -1 do
Chris@1295 100 assert_difference 'MemberRole.count', -1 do
Chris@1295 101 @jsmith.destroy
Chris@1295 102 end
Chris@1295 103 end
Chris@1295 104 assert_raise(ActiveRecord::RecordNotFound) { Member.find(@jsmith.id) }
Chris@1295 105 category1.reload
Chris@1295 106 assert_nil category1.assigned_to_id
Chris@1295 107 end
Chris@1295 108
Chris@1295 109 def test_sort_without_roles
Chris@1295 110 a = Member.new(:roles => [Role.first])
Chris@1295 111 b = Member.new
Chris@1295 112
Chris@1295 113 assert_equal -1, a <=> b
Chris@1295 114 assert_equal 1, b <=> a
Chris@1295 115 end
Chris@1295 116
Chris@1295 117 def test_sort_without_principal
Chris@1295 118 role = Role.first
Chris@1295 119 a = Member.new(:roles => [role], :principal => User.first)
Chris@1295 120 b = Member.new(:roles => [role])
Chris@1295 121
Chris@1295 122 assert_equal -1, a <=> b
Chris@1295 123 assert_equal 1, b <=> a
Chris@1295 124 end
Chris@1295 125
Chris@1295 126 context "removing permissions" do
Chris@1295 127 setup do
Chris@1295 128 Watcher.delete_all("user_id = 9")
Chris@1295 129 user = User.find(9)
Chris@1295 130 # public
Chris@1295 131 Watcher.create!(:watchable => Issue.find(1), :user => user)
Chris@1295 132 # private
Chris@1295 133 Watcher.create!(:watchable => Issue.find(4), :user => user)
Chris@1295 134 Watcher.create!(:watchable => Message.find(7), :user => user)
Chris@1295 135 Watcher.create!(:watchable => Wiki.find(2), :user => user)
Chris@1295 136 Watcher.create!(:watchable => WikiPage.find(3), :user => user)
Chris@1295 137 end
Chris@1295 138
Chris@1295 139 context "of user" do
Chris@1295 140 setup do
Chris@1295 141 @member = Member.create!(:project => Project.find(2), :principal => User.find(9), :role_ids => [1, 2])
Chris@1295 142 end
Chris@1295 143
Chris@1295 144 context "by deleting membership" do
Chris@1295 145 should "prune watchers" do
Chris@1295 146 assert_difference 'Watcher.count', -4 do
Chris@1295 147 @member.destroy
Chris@1295 148 end
Chris@1295 149 end
Chris@1295 150 end
Chris@1295 151
Chris@1295 152 context "by updating roles" do
Chris@1295 153 should "prune watchers" do
Chris@1295 154 Role.find(2).remove_permission! :view_wiki_pages
Chris@1295 155 member = Member.first(:order => 'id desc')
Chris@1295 156 assert_difference 'Watcher.count', -2 do
Chris@1295 157 member.role_ids = [2]
Chris@1295 158 member.save
Chris@1295 159 end
Chris@1295 160 assert !Message.find(7).watched_by?(@user)
Chris@1295 161 end
Chris@1295 162 end
Chris@1295 163 end
Chris@1295 164
Chris@1295 165 context "of group" do
Chris@1295 166 setup do
Chris@1295 167 group = Group.find(10)
Chris@1295 168 @member = Member.create!(:project => Project.find(2), :principal => group, :role_ids => [1, 2])
Chris@1295 169 group.users << User.find(9)
Chris@1295 170 end
Chris@1295 171
Chris@1295 172 context "by deleting membership" do
Chris@1295 173 should "prune watchers" do
Chris@1295 174 assert_difference 'Watcher.count', -4 do
Chris@1295 175 @member.destroy
Chris@1295 176 end
Chris@1295 177 end
Chris@1295 178 end
Chris@1295 179
Chris@1295 180 context "by updating roles" do
Chris@1295 181 should "prune watchers" do
Chris@1295 182 Role.find(2).remove_permission! :view_wiki_pages
Chris@1295 183 assert_difference 'Watcher.count', -2 do
Chris@1295 184 @member.role_ids = [2]
Chris@1295 185 @member.save
Chris@1295 186 end
Chris@1295 187 end
Chris@1295 188 end
Chris@1295 189 end
Chris@1295 190 end
Chris@1295 191 end