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