annotate .svn/pristine/64/6437e5d3cf3ffe39237fc8310437a3540092c950.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents e248c7af89ec
children
rev   line source
Chris@1494 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1494 3 #
Chris@1494 4 # This program is free software; you can redistribute it and/or
Chris@1494 5 # modify it under the terms of the GNU General Public License
Chris@1494 6 # as published by the Free Software Foundation; either version 2
Chris@1494 7 # of the License, or (at your option) any later version.
Chris@1494 8 #
Chris@1494 9 # This program is distributed in the hope that it will be useful,
Chris@1494 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1494 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1494 12 # GNU General Public License for more details.
Chris@1494 13 #
Chris@1494 14 # You should have received a copy of the GNU General Public License
Chris@1494 15 # along with this program; if not, write to the Free Software
Chris@1494 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1494 17
Chris@1494 18 require File.expand_path('../../test_helper', __FILE__)
Chris@1494 19
Chris@1494 20 class WatcherTest < ActiveSupport::TestCase
Chris@1494 21 fixtures :projects, :users, :members, :member_roles, :roles, :enabled_modules,
Chris@1494 22 :issues, :issue_statuses, :enumerations, :trackers, :projects_trackers,
Chris@1494 23 :boards, :messages,
Chris@1494 24 :wikis, :wiki_pages,
Chris@1494 25 :watchers
Chris@1494 26
Chris@1494 27 def setup
Chris@1494 28 @user = User.find(1)
Chris@1494 29 @issue = Issue.find(1)
Chris@1494 30 end
Chris@1494 31
Chris@1494 32 def test_validate
Chris@1494 33 user = User.find(5)
Chris@1494 34 assert !user.active?
Chris@1494 35 watcher = Watcher.new(:user_id => user.id)
Chris@1494 36 assert !watcher.save
Chris@1494 37 end
Chris@1494 38
Chris@1494 39 def test_watch
Chris@1494 40 assert @issue.add_watcher(@user)
Chris@1494 41 @issue.reload
Chris@1494 42 assert @issue.watchers.detect { |w| w.user == @user }
Chris@1494 43 end
Chris@1494 44
Chris@1494 45 def test_cant_watch_twice
Chris@1494 46 assert @issue.add_watcher(@user)
Chris@1494 47 assert !@issue.add_watcher(@user)
Chris@1494 48 end
Chris@1494 49
Chris@1494 50 def test_watched_by
Chris@1494 51 assert @issue.add_watcher(@user)
Chris@1494 52 @issue.reload
Chris@1494 53 assert @issue.watched_by?(@user)
Chris@1494 54 assert Issue.watched_by(@user).include?(@issue)
Chris@1494 55 end
Chris@1494 56
Chris@1494 57 def test_watcher_users
Chris@1494 58 watcher_users = Issue.find(2).watcher_users
Chris@1494 59 assert_kind_of Array, watcher_users
Chris@1494 60 assert_kind_of User, watcher_users.first
Chris@1494 61 end
Chris@1494 62
Chris@1494 63 def test_watcher_users_should_not_validate_user
Chris@1494 64 User.update_all("firstname = ''", "id=1")
Chris@1494 65 @user.reload
Chris@1494 66 assert !@user.valid?
Chris@1494 67
Chris@1494 68 issue = Issue.new(:project => Project.find(1), :tracker_id => 1, :subject => "test", :author => User.find(2))
Chris@1494 69 issue.watcher_users << @user
Chris@1494 70 issue.save!
Chris@1494 71 assert issue.watched_by?(@user)
Chris@1494 72 end
Chris@1494 73
Chris@1494 74 def test_watcher_user_ids
Chris@1494 75 assert_equal [1, 3], Issue.find(2).watcher_user_ids.sort
Chris@1494 76 end
Chris@1494 77
Chris@1494 78 def test_watcher_user_ids=
Chris@1494 79 issue = Issue.new
Chris@1494 80 issue.watcher_user_ids = ['1', '3']
Chris@1494 81 assert issue.watched_by?(User.find(1))
Chris@1494 82 end
Chris@1494 83
Chris@1494 84 def test_watcher_user_ids_should_make_ids_uniq
Chris@1494 85 issue = Issue.new(:project => Project.find(1), :tracker_id => 1, :subject => "test", :author => User.find(2))
Chris@1494 86 issue.watcher_user_ids = ['1', '3', '1']
Chris@1494 87 issue.save!
Chris@1494 88 assert_equal 2, issue.watchers.count
Chris@1494 89 end
Chris@1494 90
Chris@1494 91 def test_addable_watcher_users
Chris@1494 92 addable_watcher_users = @issue.addable_watcher_users
Chris@1494 93 assert_kind_of Array, addable_watcher_users
Chris@1494 94 assert_kind_of User, addable_watcher_users.first
Chris@1494 95 end
Chris@1494 96
Chris@1494 97 def test_addable_watcher_users_should_not_include_user_that_cannot_view_the_object
Chris@1494 98 issue = Issue.new(:project => Project.find(1), :is_private => true)
Chris@1494 99 assert_nil issue.addable_watcher_users.detect {|user| !issue.visible?(user)}
Chris@1494 100 end
Chris@1494 101
Chris@1494 102 def test_any_watched_should_return_false_if_no_object_is_watched
Chris@1494 103 objects = (0..2).map {Issue.generate!}
Chris@1494 104
Chris@1494 105 assert_equal false, Watcher.any_watched?(objects, @user)
Chris@1494 106 end
Chris@1494 107
Chris@1494 108 def test_any_watched_should_return_true_if_one_object_is_watched
Chris@1494 109 objects = (0..2).map {Issue.generate!}
Chris@1494 110 objects.last.add_watcher(@user)
Chris@1494 111
Chris@1494 112 assert_equal true, Watcher.any_watched?(objects, @user)
Chris@1494 113 end
Chris@1494 114
Chris@1494 115 def test_any_watched_should_return_false_with_no_object
Chris@1494 116 assert_equal false, Watcher.any_watched?([], @user)
Chris@1494 117 end
Chris@1494 118
Chris@1494 119 def test_recipients
Chris@1494 120 @issue.watchers.delete_all
Chris@1494 121 @issue.reload
Chris@1494 122
Chris@1494 123 assert @issue.watcher_recipients.empty?
Chris@1494 124 assert @issue.add_watcher(@user)
Chris@1494 125
Chris@1494 126 @user.mail_notification = 'all'
Chris@1494 127 @user.save!
Chris@1494 128 @issue.reload
Chris@1494 129 assert @issue.watcher_recipients.include?(@user.mail)
Chris@1494 130
Chris@1494 131 @user.mail_notification = 'none'
Chris@1494 132 @user.save!
Chris@1494 133 @issue.reload
Chris@1494 134 assert !@issue.watcher_recipients.include?(@user.mail)
Chris@1494 135 end
Chris@1494 136
Chris@1494 137 def test_unwatch
Chris@1494 138 assert @issue.add_watcher(@user)
Chris@1494 139 @issue.reload
Chris@1494 140 assert_equal 1, @issue.remove_watcher(@user)
Chris@1494 141 end
Chris@1494 142
Chris@1494 143 def test_prune
Chris@1494 144 Watcher.delete_all("user_id = 9")
Chris@1494 145 user = User.find(9)
Chris@1494 146
Chris@1494 147 # public
Chris@1494 148 Watcher.create!(:watchable => Issue.find(1), :user => user)
Chris@1494 149 Watcher.create!(:watchable => Issue.find(2), :user => user)
Chris@1494 150 Watcher.create!(:watchable => Message.find(1), :user => user)
Chris@1494 151 Watcher.create!(:watchable => Wiki.find(1), :user => user)
Chris@1494 152 Watcher.create!(:watchable => WikiPage.find(2), :user => user)
Chris@1494 153
Chris@1494 154 # private project (id: 2)
Chris@1494 155 Member.create!(:project => Project.find(2), :principal => user, :role_ids => [1])
Chris@1494 156 Watcher.create!(:watchable => Issue.find(4), :user => user)
Chris@1494 157 Watcher.create!(:watchable => Message.find(7), :user => user)
Chris@1494 158 Watcher.create!(:watchable => Wiki.find(2), :user => user)
Chris@1494 159 Watcher.create!(:watchable => WikiPage.find(3), :user => user)
Chris@1494 160
Chris@1494 161 assert_no_difference 'Watcher.count' do
Chris@1494 162 Watcher.prune(:user => User.find(9))
Chris@1494 163 end
Chris@1494 164
Chris@1494 165 Member.delete_all
Chris@1494 166
Chris@1494 167 assert_difference 'Watcher.count', -4 do
Chris@1494 168 Watcher.prune(:user => User.find(9))
Chris@1494 169 end
Chris@1494 170
Chris@1494 171 assert Issue.find(1).watched_by?(user)
Chris@1494 172 assert !Issue.find(4).watched_by?(user)
Chris@1494 173 end
Chris@1494 174
Chris@1494 175 def test_prune_all
Chris@1494 176 user = User.find(9)
Chris@1494 177 Watcher.new(:watchable => Issue.find(4), :user => User.find(9)).save(:validate => false)
Chris@1494 178
Chris@1494 179 assert Watcher.prune > 0
Chris@1494 180 assert !Issue.find(4).watched_by?(user)
Chris@1494 181 end
Chris@1494 182 end