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

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