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.

messages_controller_test.rb 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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 File.expand_path('../../test_helper', __FILE__)
  19. class MessagesControllerTest < Redmine::ControllerTest
  20. fixtures :projects, :users, :email_addresses, :user_preferences, :members, :member_roles, :roles, :boards, :messages, :enabled_modules
  21. def setup
  22. User.current = nil
  23. end
  24. def test_show
  25. get :show, :params => {
  26. :board_id => 1,
  27. :id => 1
  28. }
  29. assert_response :success
  30. assert_select 'h2', :text => 'First post'
  31. end
  32. def test_show_should_contain_reply_field_tags_for_quoting
  33. @request.session[:user_id] = 2
  34. get :show, :params => {
  35. :board_id => 1,
  36. :id => 1
  37. }
  38. assert_response :success
  39. # tags required by MessagesController#quote
  40. assert_select 'input#message_subject'
  41. assert_select 'textarea#message_content'
  42. assert_select 'div#reply'
  43. end
  44. def test_show_with_pagination
  45. message = Message.find(1)
  46. assert_difference 'Message.count', 30 do
  47. 30.times do
  48. message.children << Message.new(:subject => 'Reply',
  49. :content => 'Reply body',
  50. :author_id => 2,
  51. :board_id => 1)
  52. end
  53. end
  54. reply_ids = message.children.map(&:id).sort
  55. get :show, :params => {
  56. :board_id => 1,
  57. :id => 1,
  58. :r => reply_ids.last
  59. }
  60. assert_response :success
  61. assert_select 'a[href=?]', "/boards/1/topics/1?r=#{reply_ids.last}#message-#{reply_ids.last}"
  62. assert_select 'a[href=?]', "/boards/1/topics/1?r=#{reply_ids.first}#message-#{reply_ids.first}", 0
  63. end
  64. def test_show_with_reply_permission
  65. @request.session[:user_id] = 2
  66. get :show, :params => {
  67. :board_id => 1,
  68. :id => 1
  69. }
  70. assert_response :success
  71. assert_select 'div#reply textarea#message_content'
  72. end
  73. def test_show_message_not_found
  74. get :show, :params => {
  75. :board_id => 1,
  76. :id => 99999
  77. }
  78. assert_response 404
  79. end
  80. def test_show_message_from_invalid_board_should_respond_with_404
  81. get :show, :params => {
  82. :board_id => 999,
  83. :id => 1
  84. }
  85. assert_response 404
  86. end
  87. def test_get_new
  88. @request.session[:user_id] = 2
  89. get :new, :params => {
  90. :board_id => 1
  91. }
  92. assert_response :success
  93. assert_select 'input[name=?]', 'message[subject]'
  94. end
  95. def test_get_new_with_invalid_board
  96. @request.session[:user_id] = 2
  97. get :new, :params => {
  98. :board_id => 99
  99. }
  100. assert_response 404
  101. end
  102. def test_post_new
  103. @request.session[:user_id] = 2
  104. ActionMailer::Base.deliveries.clear
  105. with_settings :notified_events => %w(message_posted) do
  106. post :new, :params => {
  107. :board_id => 1,
  108. :message => {
  109. :subject => 'Test created message',
  110. :content => 'Message body'
  111. }
  112. }
  113. end
  114. assert_equal I18n.t(:notice_successful_create), flash[:notice]
  115. message = Message.find_by_subject('Test created message')
  116. assert_not_nil message
  117. assert_redirected_to "/boards/1/topics/#{message.to_param}"
  118. assert_equal 'Message body', message.content
  119. assert_equal 2, message.author_id
  120. assert_equal 1, message.board_id
  121. mails = ActionMailer::Base.deliveries
  122. assert_not_empty mails
  123. mails.each do |mail|
  124. assert_equal "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] Test created message", mail.subject
  125. assert_mail_body_match 'Message body', mail
  126. end
  127. # author
  128. assert_equal ['jsmith@somenet.foo'], mails[0].bcc
  129. # project member
  130. assert_equal ['dlopper@somenet.foo'], mails[1].bcc
  131. end
  132. def test_get_edit
  133. @request.session[:user_id] = 2
  134. get :edit, :params => {
  135. :board_id => 1,
  136. :id => 1
  137. }
  138. assert_response :success
  139. assert_select 'input[name=?][value=?]', 'message[subject]', 'First post'
  140. end
  141. def test_post_edit
  142. @request.session[:user_id] = 2
  143. post :edit, :params => {
  144. :board_id => 1,
  145. :id => 1,
  146. :message => {
  147. :subject => 'New subject',
  148. :content => 'New body'
  149. }
  150. }
  151. assert_redirected_to '/boards/1/topics/1'
  152. assert_equal I18n.t(:notice_successful_update), flash[:notice]
  153. message = Message.find(1)
  154. assert_equal 'New subject', message.subject
  155. assert_equal 'New body', message.content
  156. end
  157. def test_post_edit_sticky_and_locked
  158. @request.session[:user_id] = 2
  159. post :edit, :params => {
  160. :board_id => 1,
  161. :id => 1,
  162. :message => {
  163. :subject => 'New subject',
  164. :content => 'New body',
  165. :locked => '1',
  166. :sticky => '1'
  167. }
  168. }
  169. assert_redirected_to '/boards/1/topics/1'
  170. assert_equal I18n.t(:notice_successful_update), flash[:notice]
  171. message = Message.find(1)
  172. assert_equal true, message.sticky?
  173. assert_equal true, message.locked?
  174. end
  175. def test_post_edit_should_allow_to_change_board
  176. @request.session[:user_id] = 2
  177. post :edit, :params => {
  178. :board_id => 1,
  179. :id => 1,
  180. :message => {
  181. :subject => 'New subject',
  182. :content => 'New body',
  183. :board_id => 2
  184. }
  185. }
  186. assert_redirected_to '/boards/2/topics/1'
  187. message = Message.find(1)
  188. assert_equal Board.find(2), message.board
  189. end
  190. def test_reply
  191. @request.session[:user_id] = 2
  192. post :reply, :params => {
  193. :board_id => 1,
  194. :id => 1,
  195. :reply => {
  196. :content => 'This is a test reply',
  197. :subject => 'Test reply'
  198. }
  199. }
  200. reply = Message.order('id DESC').first
  201. assert_redirected_to "/boards/1/topics/1?r=#{reply.id}"
  202. assert_equal I18n.t(:notice_successful_update), flash[:notice]
  203. assert Message.find_by_subject('Test reply')
  204. end
  205. def test_destroy_topic
  206. @request.session[:user_id] = 2
  207. assert_difference 'Message.count', -3 do
  208. post :destroy, :params => {
  209. :board_id => 1,
  210. :id => 1
  211. }
  212. end
  213. assert_redirected_to '/projects/ecookbook/boards/1'
  214. assert_equal I18n.t(:notice_successful_delete), flash[:notice]
  215. assert_nil Message.find_by_id(1)
  216. end
  217. def test_destroy_reply
  218. @request.session[:user_id] = 2
  219. assert_difference 'Message.count', -1 do
  220. post :destroy, :params => {
  221. :board_id => 1,
  222. :id => 2
  223. }
  224. end
  225. assert_redirected_to '/boards/1/topics/1?r=2'
  226. assert_equal I18n.t(:notice_successful_delete), flash[:notice]
  227. assert_nil Message.find_by_id(2)
  228. end
  229. def test_quote_if_message_is_root
  230. @request.session[:user_id] = 2
  231. get :quote, :params => {
  232. :board_id => 1,
  233. :id => 1
  234. },
  235. :xhr => true
  236. assert_response :success
  237. assert_equal 'text/javascript', response.content_type
  238. assert_include 'RE: First post', response.body
  239. assert_include "Redmine Admin wrote:", response.body
  240. assert_include '> This is the very first post\n> in the forum', response.body
  241. end
  242. def test_quote_if_message_is_not_root
  243. @request.session[:user_id] = 2
  244. get :quote, :params => {
  245. :board_id => 1,
  246. :id => 3
  247. },
  248. :xhr => true
  249. assert_response :success
  250. assert_equal 'text/javascript', response.content_type
  251. assert_include 'RE: First post', response.body
  252. assert_include 'John Smith wrote in message#3:', response.body
  253. assert_include '> An other reply', response.body
  254. end
  255. def test_preview_new
  256. @request.session[:user_id] = 2
  257. post :preview, :params => {
  258. :board_id => 1,
  259. :message => {
  260. :subject => ""
  261. },
  262. :text => "Previewed text"
  263. }
  264. assert_response :success
  265. assert_include 'Previewed text', response.body
  266. end
  267. def test_preview_edit
  268. @request.session[:user_id] = 2
  269. post :preview, :params => {
  270. :id => 4,
  271. :board_id => 1,
  272. :message => {
  273. :subject => "",
  274. },
  275. :text => "Previewed text"
  276. }
  277. assert_response :success
  278. assert_include 'Previewed text', response.body
  279. end
  280. end