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