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.2KB

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