You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

watcher_test.rb 6.6KB

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