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.

message_test.rb 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 MessageTest < ActiveSupport::TestCase
  19. fixtures :projects, :roles, :members, :member_roles, :boards, :messages,
  20. :users, :watchers, :enabled_modules
  21. def setup
  22. User.current = nil
  23. @board = Board.find(1)
  24. @user = User.find(1)
  25. end
  26. def test_create
  27. topics_count = @board.topics_count
  28. messages_count = @board.messages_count
  29. message = Message.new(:board => @board, :subject => 'Test message',
  30. :content => 'Test message content',
  31. :author => @user)
  32. assert message.save
  33. @board.reload
  34. # topics count incremented
  35. assert_equal topics_count + 1, @board[:topics_count]
  36. # messages count incremented
  37. assert_equal messages_count + 1, @board[:messages_count]
  38. assert_equal message, @board.last_message
  39. # author should be watching the message
  40. assert message.watched_by?(@user)
  41. end
  42. def test_reply
  43. topics_count = @board.topics_count
  44. messages_count = @board.messages_count
  45. message = Message.find(1)
  46. replies_count = message.replies_count
  47. reply_author = User.find(2)
  48. reply = Message.new(:board => @board, :subject => 'Test reply',
  49. :content => 'Test reply content',
  50. :parent => message, :author => reply_author)
  51. assert reply.save
  52. @board.reload
  53. # same topics count
  54. assert_equal topics_count, @board[:topics_count]
  55. # messages count incremented
  56. assert_equal messages_count+1, @board[:messages_count]
  57. assert_equal reply, @board.last_message
  58. message.reload
  59. # replies count incremented
  60. assert_equal replies_count+1, message[:replies_count]
  61. assert_equal reply, message.last_reply
  62. # author should be watching the message
  63. assert message.watched_by?(reply_author)
  64. end
  65. def test_cannot_reply_to_locked_topic
  66. topics_count = @board.topics_count
  67. messages_count = @board.messages_count
  68. message = Message.find(1)
  69. replies_count = message.replies_count
  70. assert_equal false, message.locked
  71. message.locked = true
  72. assert message.save
  73. assert_equal true, message.locked
  74. reply_author = User.find(2)
  75. reply = Message.new(:board => @board, :subject => 'Test reply',
  76. :content => 'Test reply content',
  77. :parent => message, :author => reply_author)
  78. reply.save
  79. assert_equal 1, reply.errors.count
  80. end
  81. def test_moving_message_should_update_counters
  82. message = Message.find(1)
  83. assert_no_difference 'Message.count' do
  84. # Previous board
  85. assert_difference 'Board.find(1).topics_count', -1 do
  86. assert_difference 'Board.find(1).messages_count', -(1 + message.replies_count) do
  87. # New board
  88. assert_difference 'Board.find(2).topics_count' do
  89. assert_difference 'Board.find(2).messages_count', (1 + message.replies_count) do
  90. message.update_attributes(:board_id => 2)
  91. end
  92. end
  93. end
  94. end
  95. end
  96. end
  97. def test_destroy_topic
  98. message = Message.find(1)
  99. board = message.board
  100. topics_count, messages_count = board.topics_count, board.messages_count
  101. assert_difference('Watcher.count', -1) do
  102. assert message.destroy
  103. end
  104. board.reload
  105. # Replies deleted
  106. assert Message.where(:parent_id => 1).empty?
  107. # Checks counters
  108. assert_equal topics_count - 1, board.topics_count
  109. assert_equal messages_count - 3, board.messages_count
  110. # Watchers removed
  111. end
  112. def test_destroy_reply
  113. message = Message.find(5)
  114. board = message.board
  115. topics_count, messages_count = board.topics_count, board.messages_count
  116. assert message.destroy
  117. board.reload
  118. # Checks counters
  119. assert_equal topics_count, board.topics_count
  120. assert_equal messages_count - 1, board.messages_count
  121. end
  122. def test_destroying_last_reply_should_update_topic_last_reply_id
  123. topic = Message.find(4)
  124. assert_equal 6, topic.last_reply_id
  125. assert_difference 'Message.count', -1 do
  126. Message.find(6).destroy
  127. end
  128. assert_equal 5, topic.reload.last_reply_id
  129. assert_difference 'Message.count', -1 do
  130. Message.find(5).destroy
  131. end
  132. assert_nil topic.reload.last_reply_id
  133. end
  134. def test_editable_by
  135. message = Message.find(6)
  136. author = message.author
  137. assert message.editable_by?(author)
  138. author.roles_for_project(message.project).first.remove_permission!(:edit_own_messages)
  139. assert !message.reload.editable_by?(author.reload)
  140. end
  141. def test_destroyable_by
  142. message = Message.find(6)
  143. author = message.author
  144. assert message.destroyable_by?(author)
  145. author.roles_for_project(message.project).first.remove_permission!(:delete_own_messages)
  146. assert !message.reload.destroyable_by?(author.reload)
  147. end
  148. def test_set_sticky
  149. message = Message.new
  150. assert_equal 0, message.sticky
  151. message.sticky = nil
  152. assert_equal 0, message.sticky
  153. message.sticky = false
  154. assert_equal 0, message.sticky
  155. message.sticky = true
  156. assert_equal 1, message.sticky
  157. message.sticky = '0'
  158. assert_equal 0, message.sticky
  159. message.sticky = '1'
  160. assert_equal 1, message.sticky
  161. end
  162. end