annotate test/unit/watcher_test.rb @ 853:969bb872d4bf feature_142

Close obsolete branch feature_142
author Chris Cannam
date Thu, 14 Jul 2011 14:26:44 +0100
parents cbce1fd3b1b7
children cbb26bc654de
rev   line source
Chris@0 1 # Redmine - project management software
Chris@0 2 # Copyright (C) 2006-2009 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@0 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@0 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 WatcherTest < ActiveSupport::TestCase
Chris@0 21 fixtures :projects, :users, :members, :member_roles, :roles, :enabled_modules,
Chris@0 22 :issues,
Chris@0 23 :boards, :messages,
Chris@0 24 :wikis, :wiki_pages,
Chris@0 25 :watchers
Chris@0 26
Chris@0 27 def setup
Chris@0 28 @user = User.find(1)
Chris@0 29 @issue = Issue.find(1)
Chris@0 30 end
Chris@0 31
Chris@0 32 def test_watch
Chris@0 33 assert @issue.add_watcher(@user)
Chris@0 34 @issue.reload
Chris@0 35 assert @issue.watchers.detect { |w| w.user == @user }
Chris@0 36 end
Chris@0 37
Chris@0 38 def test_cant_watch_twice
Chris@0 39 assert @issue.add_watcher(@user)
Chris@0 40 assert !@issue.add_watcher(@user)
Chris@0 41 end
Chris@0 42
Chris@0 43 def test_watched_by
Chris@0 44 assert @issue.add_watcher(@user)
Chris@0 45 @issue.reload
Chris@0 46 assert @issue.watched_by?(@user)
Chris@0 47 assert Issue.watched_by(@user).include?(@issue)
Chris@0 48 end
Chris@0 49
Chris@441 50 def test_watcher_users
Chris@441 51 watcher_users = Issue.find(2).watcher_users
Chris@441 52 assert_kind_of Array, watcher_users
Chris@441 53 assert_kind_of User, watcher_users.first
Chris@441 54 end
Chris@441 55
Chris@441 56 def test_watcher_users_should_not_validate_user
Chris@441 57 User.update_all("firstname = ''", "id=1")
Chris@441 58 @user.reload
Chris@441 59 assert !@user.valid?
Chris@441 60
Chris@441 61 issue = Issue.new(:project => Project.find(1), :tracker_id => 1, :subject => "test", :author => User.find(2))
Chris@441 62 issue.watcher_users << @user
Chris@441 63 issue.save!
Chris@441 64 assert issue.watched_by?(@user)
Chris@441 65 end
Chris@441 66
Chris@0 67 def test_watcher_user_ids
Chris@441 68 assert_equal [1, 3], Issue.find(2).watcher_user_ids.sort
Chris@441 69 end
Chris@441 70
Chris@441 71 def test_watcher_user_ids=
Chris@0 72 issue = Issue.new
Chris@0 73 issue.watcher_user_ids = ['1', '3']
Chris@0 74 assert issue.watched_by?(User.find(1))
Chris@0 75 end
Chris@0 76
Chris@441 77 def test_addable_watcher_users
Chris@441 78 addable_watcher_users = @issue.addable_watcher_users
Chris@441 79 assert_kind_of Array, addable_watcher_users
Chris@441 80 assert_kind_of User, addable_watcher_users.first
Chris@441 81 end
Chris@441 82
Chris@441 83 def test_addable_watcher_users_should_not_include_user_that_cannot_view_the_object
Chris@441 84 issue = Issue.new(:project => Project.find(1), :is_private => true)
Chris@441 85 assert_nil issue.addable_watcher_users.detect {|user| !issue.visible?(user)}
Chris@441 86 end
Chris@441 87
Chris@0 88 def test_recipients
Chris@0 89 @issue.watchers.delete_all
Chris@0 90 @issue.reload
Chris@0 91
Chris@0 92 assert @issue.watcher_recipients.empty?
Chris@0 93 assert @issue.add_watcher(@user)
Chris@0 94
Chris@441 95 @user.mail_notification = 'all'
Chris@441 96 @user.save!
Chris@0 97 @issue.reload
Chris@0 98 assert @issue.watcher_recipients.include?(@user.mail)
Chris@0 99
Chris@441 100 @user.mail_notification = 'none'
Chris@441 101 @user.save!
Chris@0 102 @issue.reload
Chris@441 103 assert !@issue.watcher_recipients.include?(@user.mail)
Chris@0 104 end
Chris@0 105
Chris@0 106 def test_unwatch
Chris@0 107 assert @issue.add_watcher(@user)
Chris@0 108 @issue.reload
Chris@0 109 assert_equal 1, @issue.remove_watcher(@user)
Chris@0 110 end
Chris@0 111
Chris@0 112 def test_prune
Chris@0 113 Watcher.delete_all("user_id = 9")
Chris@0 114 user = User.find(9)
Chris@0 115
Chris@0 116 # public
Chris@0 117 Watcher.create!(:watchable => Issue.find(1), :user => user)
Chris@0 118 Watcher.create!(:watchable => Issue.find(2), :user => user)
Chris@0 119 Watcher.create!(:watchable => Message.find(1), :user => user)
Chris@0 120 Watcher.create!(:watchable => Wiki.find(1), :user => user)
Chris@0 121 Watcher.create!(:watchable => WikiPage.find(2), :user => user)
Chris@0 122
Chris@0 123 # private project (id: 2)
Chris@0 124 Member.create!(:project => Project.find(2), :principal => user, :role_ids => [1])
Chris@0 125 Watcher.create!(:watchable => Issue.find(4), :user => user)
Chris@0 126 Watcher.create!(:watchable => Message.find(7), :user => user)
Chris@0 127 Watcher.create!(:watchable => Wiki.find(2), :user => user)
Chris@0 128 Watcher.create!(:watchable => WikiPage.find(3), :user => user)
Chris@0 129
Chris@0 130 assert_no_difference 'Watcher.count' do
Chris@0 131 Watcher.prune(:user => User.find(9))
Chris@0 132 end
Chris@0 133
Chris@0 134 Member.delete_all
Chris@0 135
Chris@0 136 assert_difference 'Watcher.count', -4 do
Chris@0 137 Watcher.prune(:user => User.find(9))
Chris@0 138 end
Chris@0 139
Chris@0 140 assert Issue.find(1).watched_by?(user)
Chris@0 141 assert !Issue.find(4).watched_by?(user)
Chris@0 142 end
Chris@0 143 end