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.

boards_controller_test.rb 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 BoardsControllerTest < Redmine::ControllerTest
  20. fixtures :projects, :users, :members, :member_roles, :roles, :boards, :messages, :enabled_modules
  21. def setup
  22. User.current = nil
  23. end
  24. def test_index
  25. get :index, :params => {
  26. :project_id => 1
  27. }
  28. assert_response :success
  29. assert_select 'table.boards'
  30. end
  31. def test_index_not_found
  32. get :index, :params => {
  33. :project_id => 97
  34. }
  35. assert_response 404
  36. end
  37. def test_index_should_show_messages_if_only_one_board
  38. Project.find(1).boards.to_a.slice(1..-1).each(&:destroy)
  39. get :index, :params => {
  40. :project_id => 1
  41. }
  42. assert_response :success
  43. assert_select 'table.boards', 0
  44. assert_select 'table.messages'
  45. end
  46. def test_show
  47. get :show, :params => {
  48. :project_id => 1,
  49. :id => 1
  50. }
  51. assert_response :success
  52. assert_select 'table.messages tbody' do
  53. assert_select 'tr', Board.find(1).topics.count
  54. end
  55. end
  56. def test_show_should_display_sticky_messages_first
  57. Message.update_all(:sticky => 0)
  58. Message.where({:id => 1}).update_all({:sticky => 1})
  59. get :show, :params => {
  60. :project_id => 1,
  61. :id => 1
  62. }
  63. assert_response :success
  64. assert_select 'table.messages tbody' do
  65. # row is here...
  66. assert_select 'tr.sticky'
  67. # ...and in first position
  68. assert_select 'tr.sticky:first-child'
  69. end
  70. end
  71. def test_show_should_display_message_with_last_reply_first
  72. Message.update_all(:sticky => 0)
  73. # Reply to an old topic
  74. old_topic = Message.where(:board_id => 1, :parent_id => nil).order('created_on ASC').first
  75. reply = Message.new(:board_id => 1, :subject => 'New reply', :content => 'New reply', :author_id => 2)
  76. old_topic.children << reply
  77. get :show, :params => {
  78. :project_id => 1,
  79. :id => 1
  80. }
  81. assert_response :success
  82. assert_select 'table.messages tbody' do
  83. assert_select "tr#message-#{old_topic.id}"
  84. assert_select "tr#message-#{old_topic.id}:first-child"
  85. end
  86. end
  87. def test_show_with_permission_should_display_the_new_message_form
  88. @request.session[:user_id] = 2
  89. get :show, :params => {
  90. :project_id => 1,
  91. :id => 1
  92. }
  93. assert_response :success
  94. assert_select 'form#message-form' do
  95. assert_select 'input[name=?]', 'message[subject]'
  96. end
  97. end
  98. def test_show_atom
  99. get :show, :params => {
  100. :project_id => 1,
  101. :id => 1,
  102. :format => 'atom'
  103. }
  104. assert_response :success
  105. assert_select 'feed > entry > title', :text => 'Help: RE: post 2'
  106. end
  107. def test_show_not_found
  108. get :index, :params => {
  109. :project_id => 1,
  110. :id => 97
  111. }
  112. assert_response 404
  113. end
  114. def test_new
  115. @request.session[:user_id] = 2
  116. get :new, :params => {
  117. :project_id => 1
  118. }
  119. assert_response :success
  120. assert_select 'select[name=?]', 'board[parent_id]' do
  121. assert_select 'option', (Project.find(1).boards.size + 1)
  122. assert_select 'option[value=""]'
  123. assert_select 'option[value="1"]', :text => 'Help'
  124. end
  125. # &nbsp; replaced by nokogiri, not easy to test in DOM assertions
  126. assert_not_include '<option value=""></option>', response.body
  127. assert_include '<option value="">&nbsp;</option>', response.body
  128. end
  129. def test_new_without_project_boards
  130. Project.find(1).boards.delete_all
  131. @request.session[:user_id] = 2
  132. get :new, :params => {
  133. :project_id => 1
  134. }
  135. assert_response :success
  136. assert_select 'select[name=?]', 'board[parent_id]', 0
  137. end
  138. def test_create
  139. @request.session[:user_id] = 2
  140. assert_difference 'Board.count' do
  141. post :create, :params => {
  142. :project_id => 1,
  143. :board => {
  144. :name => 'Testing',
  145. :description => 'Testing board creation'
  146. }
  147. }
  148. end
  149. assert_redirected_to '/projects/ecookbook/settings/boards'
  150. board = Board.order('id DESC').first
  151. assert_equal 'Testing', board.name
  152. assert_equal 'Testing board creation', board.description
  153. end
  154. def test_create_with_parent
  155. @request.session[:user_id] = 2
  156. assert_difference 'Board.count' do
  157. post :create, :params => {
  158. :project_id => 1,
  159. :board => {
  160. :name => 'Testing',
  161. :description => 'Testing',
  162. :parent_id => 2
  163. }
  164. }
  165. end
  166. assert_redirected_to '/projects/ecookbook/settings/boards'
  167. board = Board.order('id DESC').first
  168. assert_equal Board.find(2), board.parent
  169. end
  170. def test_create_with_failure
  171. @request.session[:user_id] = 2
  172. assert_no_difference 'Board.count' do
  173. post :create, :params => {
  174. :project_id => 1,
  175. :board => {
  176. :name => '',
  177. :description => 'Testing board creation'
  178. }
  179. }
  180. end
  181. assert_response :success
  182. assert_select_error /Name cannot be blank/
  183. end
  184. def test_edit
  185. @request.session[:user_id] = 2
  186. get :edit, :params => {
  187. :project_id => 1,
  188. :id => 2
  189. }
  190. assert_response :success
  191. assert_select 'input[name=?][value=?]', 'board[name]', 'Discussion'
  192. end
  193. def test_edit_with_parent
  194. board = Board.generate!(:project_id => 1, :parent_id => 2)
  195. @request.session[:user_id] = 2
  196. get :edit, :params => {
  197. :project_id => 1,
  198. :id => board.id
  199. }
  200. assert_response :success
  201. assert_select 'select[name=?]', 'board[parent_id]' do
  202. assert_select 'option[value="2"][selected=selected]'
  203. end
  204. end
  205. def test_update
  206. @request.session[:user_id] = 2
  207. assert_no_difference 'Board.count' do
  208. put :update, :params => {
  209. :project_id => 1,
  210. :id => 2,
  211. :board => {
  212. :name => 'Testing',
  213. :description => 'Testing board update'
  214. }
  215. }
  216. end
  217. assert_redirected_to '/projects/ecookbook/settings/boards'
  218. assert_equal 'Testing', Board.find(2).name
  219. end
  220. def test_update_position
  221. @request.session[:user_id] = 2
  222. put :update, :params => {
  223. :project_id => 1,
  224. :id => 2,
  225. :board => {
  226. :position => 1
  227. }
  228. }
  229. assert_redirected_to '/projects/ecookbook/settings/boards'
  230. board = Board.find(2)
  231. assert_equal 1, board.position
  232. end
  233. def test_update_with_failure
  234. @request.session[:user_id] = 2
  235. put :update, :params => {
  236. :project_id => 1,
  237. :id => 2,
  238. :board => {
  239. :name => '',
  240. :description => 'Testing board update'
  241. }
  242. }
  243. assert_response :success
  244. assert_select_error /Name cannot be blank/
  245. end
  246. def test_destroy
  247. @request.session[:user_id] = 2
  248. assert_difference 'Board.count', -1 do
  249. delete :destroy, :params => {
  250. :project_id => 1,
  251. :id => 2
  252. }
  253. end
  254. assert_redirected_to '/projects/ecookbook/settings/boards'
  255. assert_nil Board.find_by_id(2)
  256. end
  257. end