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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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 BoardTest < ActiveSupport::TestCase
  20. fixtures :projects, :boards, :messages, :attachments, :watchers
  21. include Redmine::I18n
  22. def setup
  23. User.current = nil
  24. @project = Project.find(1)
  25. end
  26. def test_create
  27. board = Board.new(:project => @project, :name => 'Test board', :description => 'Test board description')
  28. assert board.save
  29. board.reload
  30. assert_equal 'Test board', board.name
  31. assert_equal 'Test board description', board.description
  32. assert_equal @project, board.project
  33. assert_equal 0, board.topics_count
  34. assert_equal 0, board.messages_count
  35. assert_nil board.last_message
  36. # last position
  37. assert_equal @project.boards.size, board.position
  38. end
  39. def test_parent_should_be_in_same_project
  40. set_language_if_valid 'en'
  41. board = Board.new(:project_id => 3, :name => 'Test', :description => 'Test', :parent_id => 1)
  42. assert !board.save
  43. assert_include "Parent forum is invalid", board.errors.full_messages
  44. end
  45. def test_valid_parents_should_not_include_self_nor_a_descendant
  46. board1 = Board.generate!(:project_id => 3)
  47. board2 = Board.generate!(:project_id => 3, :parent => board1)
  48. board3 = Board.generate!(:project_id => 3, :parent => board2)
  49. board4 = Board.generate!(:project_id => 3)
  50. assert_equal [board4], board1.reload.valid_parents.sort_by(&:id)
  51. assert_equal [board1, board4], board2.reload.valid_parents.sort_by(&:id)
  52. assert_equal [board1, board2, board4], board3.reload.valid_parents.sort_by(&:id)
  53. assert_equal [board1, board2, board3], board4.reload.valid_parents.sort_by(&:id)
  54. end
  55. def test_position_should_be_assigned_with_parent_scope
  56. parent1 = Board.generate!(:project_id => 3)
  57. parent2 = Board.generate!(:project_id => 3)
  58. child1 = Board.generate!(:project_id => 3, :parent => parent1)
  59. child2 = Board.generate!(:project_id => 3, :parent => parent1)
  60. assert_equal 1, parent1.reload.position
  61. assert_equal 1, child1.reload.position
  62. assert_equal 2, child2.reload.position
  63. assert_equal 2, parent2.reload.position
  64. end
  65. def test_board_tree_should_yield_boards_with_level
  66. parent1 = Board.generate!(:project_id => 3)
  67. parent2 = Board.generate!(:project_id => 3)
  68. child1 = Board.generate!(:project_id => 3, :parent => parent1)
  69. child2 = Board.generate!(:project_id => 3, :parent => parent1)
  70. child3 = Board.generate!(:project_id => 3, :parent => child1)
  71. tree = Board.board_tree(Project.find(3).boards)
  72. assert_equal [
  73. [parent1, 0],
  74. [child1, 1],
  75. [child3, 2],
  76. [child2, 1],
  77. [parent2, 0]
  78. ], tree
  79. end
  80. def test_destroy
  81. set_tmp_attachments_directory
  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