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

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