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