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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2014 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, :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
  52. assert_kind_of User, watcher_users.first
  53. end
  54. def test_watcher_users_should_not_validate_user
  55. User.update_all("firstname = ''", "id=1")
  56. @user.reload
  57. assert !@user.valid?
  58. issue = Issue.new(:project => Project.find(1), :tracker_id => 1, :subject => "test", :author => User.find(2))
  59. issue.watcher_users << @user
  60. issue.save!
  61. assert issue.watched_by?(@user)
  62. end
  63. def test_watcher_user_ids
  64. assert_equal [1, 3], Issue.find(2).watcher_user_ids.sort
  65. end
  66. def test_watcher_user_ids=
  67. issue = Issue.new
  68. issue.watcher_user_ids = ['1', '3']
  69. assert issue.watched_by?(User.find(1))
  70. end
  71. def test_watcher_user_ids_should_make_ids_uniq
  72. issue = Issue.new(:project => Project.find(1), :tracker_id => 1, :subject => "test", :author => User.find(2))
  73. issue.watcher_user_ids = ['1', '3', '1']
  74. issue.save!
  75. assert_equal 2, issue.watchers.count
  76. end
  77. def test_addable_watcher_users
  78. addable_watcher_users = @issue.addable_watcher_users
  79. assert_kind_of Array, addable_watcher_users
  80. assert_kind_of User, addable_watcher_users.first
  81. end
  82. def test_addable_watcher_users_should_not_include_user_that_cannot_view_the_object
  83. issue = Issue.new(:project => Project.find(1), :is_private => true)
  84. assert_nil issue.addable_watcher_users.detect {|user| !issue.visible?(user)}
  85. end
  86. def test_any_watched_should_return_false_if_no_object_is_watched
  87. objects = (0..2).map {Issue.generate!}
  88. assert_equal false, Watcher.any_watched?(objects, @user)
  89. end
  90. def test_any_watched_should_return_true_if_one_object_is_watched
  91. objects = (0..2).map {Issue.generate!}
  92. objects.last.add_watcher(@user)
  93. assert_equal true, Watcher.any_watched?(objects, @user)
  94. end
  95. def test_any_watched_should_return_false_with_no_object
  96. assert_equal false, Watcher.any_watched?([], @user)
  97. end
  98. def test_recipients
  99. @issue.watchers.delete_all
  100. @issue.reload
  101. assert @issue.watcher_recipients.empty?
  102. assert @issue.add_watcher(@user)
  103. @user.mail_notification = 'all'
  104. @user.save!
  105. @issue.reload
  106. assert @issue.watcher_recipients.include?(@user.mail)
  107. @user.mail_notification = 'none'
  108. @user.save!
  109. @issue.reload
  110. assert !@issue.watcher_recipients.include?(@user.mail)
  111. end
  112. def test_unwatch
  113. assert @issue.add_watcher(@user)
  114. @issue.reload
  115. assert_equal 1, @issue.remove_watcher(@user)
  116. end
  117. def test_prune
  118. Watcher.delete_all("user_id = 9")
  119. user = User.find(9)
  120. # public
  121. Watcher.create!(:watchable => Issue.find(1), :user => user)
  122. Watcher.create!(:watchable => Issue.find(2), :user => user)
  123. Watcher.create!(:watchable => Message.find(1), :user => user)
  124. Watcher.create!(:watchable => Wiki.find(1), :user => user)
  125. Watcher.create!(:watchable => WikiPage.find(2), :user => user)
  126. # private project (id: 2)
  127. Member.create!(:project => Project.find(2), :principal => user, :role_ids => [1])
  128. Watcher.create!(:watchable => Issue.find(4), :user => user)
  129. Watcher.create!(:watchable => Message.find(7), :user => user)
  130. Watcher.create!(:watchable => Wiki.find(2), :user => user)
  131. Watcher.create!(:watchable => WikiPage.find(3), :user => user)
  132. assert_no_difference 'Watcher.count' do
  133. Watcher.prune(:user => User.find(9))
  134. end
  135. Member.delete_all
  136. assert_difference 'Watcher.count', -4 do
  137. Watcher.prune(:user => User.find(9))
  138. end
  139. assert Issue.find(1).watched_by?(user)
  140. assert !Issue.find(4).watched_by?(user)
  141. end
  142. def test_prune_all
  143. user = User.find(9)
  144. Watcher.new(:watchable => Issue.find(4), :user => User.find(9)).save(:validate => false)
  145. assert Watcher.prune > 0
  146. assert !Issue.find(4).watched_by?(user)
  147. end
  148. end