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 8.1KB

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