您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

messages_controller_test.rb 9.6KB

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