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.

board_test.rb 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # encoding: utf-8
  2. #
  3. # Redmine - project management software
  4. # Copyright (C) 2006-2017 Jean-Philippe Lang
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. require File.expand_path('../../test_helper', __FILE__)
  20. class BoardTest < ActiveSupport::TestCase
  21. fixtures :projects, :boards, :messages, :attachments, :watchers
  22. include Redmine::I18n
  23. def setup
  24. User.current = nil
  25. @project = Project.find(1)
  26. end
  27. def test_create
  28. board = Board.new(:project => @project, :name => 'Test board', :description => 'Test board description')
  29. assert board.save
  30. board.reload
  31. assert_equal 'Test board', board.name
  32. assert_equal 'Test board description', board.description
  33. assert_equal @project, board.project
  34. assert_equal 0, board.topics_count
  35. assert_equal 0, board.messages_count
  36. assert_nil board.last_message
  37. # last position
  38. assert_equal @project.boards.size, board.position
  39. end
  40. def test_parent_should_be_in_same_project
  41. set_language_if_valid 'en'
  42. board = Board.new(:project_id => 3, :name => 'Test', :description => 'Test', :parent_id => 1)
  43. assert !board.save
  44. assert_include "Parent forum is invalid", board.errors.full_messages
  45. end
  46. def test_valid_parents_should_not_include_self_nor_a_descendant
  47. board1 = Board.generate!(:project_id => 3)
  48. board2 = Board.generate!(:project_id => 3, :parent => board1)
  49. board3 = Board.generate!(:project_id => 3, :parent => board2)
  50. board4 = Board.generate!(:project_id => 3)
  51. assert_equal [board4], board1.reload.valid_parents.sort_by(&:id)
  52. assert_equal [board1, board4], board2.reload.valid_parents.sort_by(&:id)
  53. assert_equal [board1, board2, board4], board3.reload.valid_parents.sort_by(&:id)
  54. assert_equal [board1, board2, board3], board4.reload.valid_parents.sort_by(&:id)
  55. end
  56. def test_position_should_be_assigned_with_parent_scope
  57. parent1 = Board.generate!(:project_id => 3)
  58. parent2 = Board.generate!(:project_id => 3)
  59. child1 = Board.generate!(:project_id => 3, :parent => parent1)
  60. child2 = Board.generate!(:project_id => 3, :parent => parent1)
  61. assert_equal 1, parent1.reload.position
  62. assert_equal 1, child1.reload.position
  63. assert_equal 2, child2.reload.position
  64. assert_equal 2, parent2.reload.position
  65. end
  66. def test_board_tree_should_yield_boards_with_level
  67. parent1 = Board.generate!(:project_id => 3)
  68. parent2 = Board.generate!(:project_id => 3)
  69. child1 = Board.generate!(:project_id => 3, :parent => parent1)
  70. child2 = Board.generate!(:project_id => 3, :parent => parent1)
  71. child3 = Board.generate!(:project_id => 3, :parent => child1)
  72. tree = Board.board_tree(Project.find(3).boards)
  73. assert_equal [
  74. [parent1, 0],
  75. [child1, 1],
  76. [child3, 2],
  77. [child2, 1],
  78. [parent2, 0]
  79. ], tree
  80. end
  81. def test_destroy
  82. board = Board.find(1)
  83. assert_difference 'Message.count', -6 do
  84. assert_difference 'Attachment.count', -1 do
  85. assert_difference 'Watcher.count', -1 do
  86. assert board.destroy
  87. end
  88. end
  89. end
  90. assert_equal 0, Message.where(:board_id => 1).count
  91. end
  92. def test_destroy_should_nullify_children
  93. parent = Board.generate!(:project => @project)
  94. child = Board.generate!(:project => @project, :parent => parent)
  95. assert_equal parent, child.parent
  96. assert parent.destroy
  97. child.reload
  98. assert_nil child.parent
  99. assert_nil child.parent_id
  100. end
  101. def test_reset_counters_should_update_attributes
  102. Board.where(:id => 1).update_all(:topics_count => 0, :messages_count => 0, :last_message_id => 0)
  103. Board.reset_counters!(1)
  104. board = Board.find(1)
  105. assert_equal board.topics.count, board.topics_count
  106. assert_equal board.messages.count, board.messages_count
  107. assert_equal board.messages.order("id DESC").first.id, board.last_message_id
  108. end
  109. end