@@ -1,165 +1,165 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 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, |
|
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 |
|
59 | assert_kind_of Array, watcher_users | |
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.update_all("firstname = ''", "id=1") |
|
64 | User.update_all("firstname = ''", "id=1") | |
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_recipients |
|
102 | def test_recipients | |
103 | @issue.watchers.delete_all |
|
103 | @issue.watchers.delete_all | |
104 | @issue.reload |
|
104 | @issue.reload | |
105 |
|
105 | |||
106 | assert @issue.watcher_recipients.empty? |
|
106 | assert @issue.watcher_recipients.empty? | |
107 | assert @issue.add_watcher(@user) |
|
107 | assert @issue.add_watcher(@user) | |
108 |
|
108 | |||
109 | @user.mail_notification = 'all' |
|
109 | @user.mail_notification = 'all' | |
110 | @user.save! |
|
110 | @user.save! | |
111 | @issue.reload |
|
111 | @issue.reload | |
112 | assert @issue.watcher_recipients.include?(@user.mail) |
|
112 | assert @issue.watcher_recipients.include?(@user.mail) | |
113 |
|
113 | |||
114 | @user.mail_notification = 'none' |
|
114 | @user.mail_notification = 'none' | |
115 | @user.save! |
|
115 | @user.save! | |
116 | @issue.reload |
|
116 | @issue.reload | |
117 | assert !@issue.watcher_recipients.include?(@user.mail) |
|
117 | assert !@issue.watcher_recipients.include?(@user.mail) | |
118 | end |
|
118 | end | |
119 |
|
119 | |||
120 | def test_unwatch |
|
120 | def test_unwatch | |
121 | assert @issue.add_watcher(@user) |
|
121 | assert @issue.add_watcher(@user) | |
122 | @issue.reload |
|
122 | @issue.reload | |
123 | assert_equal 1, @issue.remove_watcher(@user) |
|
123 | assert_equal 1, @issue.remove_watcher(@user) | |
124 | end |
|
124 | end | |
125 |
|
125 | |||
126 | def test_prune |
|
126 | def test_prune | |
127 | Watcher.delete_all("user_id = 9") |
|
127 | Watcher.delete_all("user_id = 9") | |
128 | user = User.find(9) |
|
128 | user = User.find(9) | |
129 |
|
129 | |||
130 | # public |
|
130 | # public | |
131 | Watcher.create!(:watchable => Issue.find(1), :user => user) |
|
131 | Watcher.create!(:watchable => Issue.find(1), :user => user) | |
132 | Watcher.create!(:watchable => Issue.find(2), :user => user) |
|
132 | Watcher.create!(:watchable => Issue.find(2), :user => user) | |
133 | Watcher.create!(:watchable => Message.find(1), :user => user) |
|
133 | Watcher.create!(:watchable => Message.find(1), :user => user) | |
134 | Watcher.create!(:watchable => Wiki.find(1), :user => user) |
|
134 | Watcher.create!(:watchable => Wiki.find(1), :user => user) | |
135 | Watcher.create!(:watchable => WikiPage.find(2), :user => user) |
|
135 | Watcher.create!(:watchable => WikiPage.find(2), :user => user) | |
136 |
|
136 | |||
137 | # private project (id: 2) |
|
137 | # private project (id: 2) | |
138 | Member.create!(:project => Project.find(2), :principal => user, :role_ids => [1]) |
|
138 | Member.create!(:project => Project.find(2), :principal => user, :role_ids => [1]) | |
139 | Watcher.create!(:watchable => Issue.find(4), :user => user) |
|
139 | Watcher.create!(:watchable => Issue.find(4), :user => user) | |
140 | Watcher.create!(:watchable => Message.find(7), :user => user) |
|
140 | Watcher.create!(:watchable => Message.find(7), :user => user) | |
141 | Watcher.create!(:watchable => Wiki.find(2), :user => user) |
|
141 | Watcher.create!(:watchable => Wiki.find(2), :user => user) | |
142 | Watcher.create!(:watchable => WikiPage.find(3), :user => user) |
|
142 | Watcher.create!(:watchable => WikiPage.find(3), :user => user) | |
143 |
|
143 | |||
144 | assert_no_difference 'Watcher.count' do |
|
144 | assert_no_difference 'Watcher.count' do | |
145 | Watcher.prune(:user => User.find(9)) |
|
145 | Watcher.prune(:user => User.find(9)) | |
146 | end |
|
146 | end | |
147 |
|
147 | |||
148 | Member.delete_all |
|
148 | Member.delete_all | |
149 |
|
149 | |||
150 | assert_difference 'Watcher.count', -4 do |
|
150 | assert_difference 'Watcher.count', -4 do | |
151 | Watcher.prune(:user => User.find(9)) |
|
151 | Watcher.prune(:user => User.find(9)) | |
152 | end |
|
152 | end | |
153 |
|
153 | |||
154 | assert Issue.find(1).watched_by?(user) |
|
154 | assert Issue.find(1).watched_by?(user) | |
155 | assert !Issue.find(4).watched_by?(user) |
|
155 | assert !Issue.find(4).watched_by?(user) | |
156 | end |
|
156 | end | |
157 |
|
157 | |||
158 | def test_prune_all |
|
158 | def test_prune_all | |
159 | user = User.find(9) |
|
159 | user = User.find(9) | |
160 | Watcher.new(:watchable => Issue.find(4), :user => User.find(9)).save(:validate => false) |
|
160 | Watcher.new(:watchable => Issue.find(4), :user => User.find(9)).save(:validate => false) | |
161 |
|
161 | |||
162 | assert Watcher.prune > 0 |
|
162 | assert Watcher.prune > 0 | |
163 | assert !Issue.find(4).watched_by?(user) |
|
163 | assert !Issue.find(4).watched_by?(user) | |
164 | end |
|
164 | end | |
165 | end |
|
165 | end |
General Comments 0
You need to be logged in to leave comments.
Login now