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.

wiki_page_test.rb 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. class WikiPageTest < ActiveSupport::TestCase
  19. fixtures :projects, :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions
  20. def setup
  21. @wiki = Wiki.find(1)
  22. @page = @wiki.pages.first
  23. end
  24. def test_create
  25. page = WikiPage.new(:wiki => @wiki)
  26. assert !page.save
  27. assert_equal 1, page.errors.count
  28. page.title = "Page"
  29. assert page.save
  30. page.reload
  31. assert !page.protected?
  32. @wiki.reload
  33. assert @wiki.pages.include?(page)
  34. end
  35. def test_sidebar_should_be_protected_by_default
  36. page = @wiki.find_or_new_page('sidebar')
  37. assert page.new_record?
  38. assert page.protected?
  39. end
  40. def test_find_or_new_page
  41. page = @wiki.find_or_new_page("CookBook documentation")
  42. assert_kind_of WikiPage, page
  43. assert !page.new_record?
  44. page = @wiki.find_or_new_page("Non existing page")
  45. assert_kind_of WikiPage, page
  46. assert page.new_record?
  47. end
  48. def test_parent_title
  49. page = WikiPage.find_by_title('Another_page')
  50. assert_nil page.parent_title
  51. page = WikiPage.find_by_title('Page_with_an_inline_image')
  52. assert_equal 'CookBook documentation', page.parent_title
  53. end
  54. def test_assign_parent
  55. page = WikiPage.find_by_title('Another_page')
  56. page.parent_title = 'CookBook documentation'
  57. assert page.save
  58. page.reload
  59. assert_equal WikiPage.find_by_title('CookBook_documentation'), page.parent
  60. end
  61. def test_unassign_parent
  62. page = WikiPage.find_by_title('Page_with_an_inline_image')
  63. page.parent_title = ''
  64. assert page.save
  65. page.reload
  66. assert_nil page.parent
  67. end
  68. def test_parent_validation
  69. page = WikiPage.find_by_title('CookBook_documentation')
  70. # A page that doesn't exist
  71. page.parent_title = 'Unknown title'
  72. assert !page.save
  73. assert_include I18n.translate('activerecord.errors.messages.invalid'),
  74. page.errors[:parent_title]
  75. # A child page
  76. page.parent_title = 'Page_with_an_inline_image'
  77. assert !page.save
  78. assert_include I18n.translate('activerecord.errors.messages.circular_dependency'),
  79. page.errors[:parent_title]
  80. # The page itself
  81. page.parent_title = 'CookBook_documentation'
  82. assert !page.save
  83. assert_include I18n.translate('activerecord.errors.messages.circular_dependency'),
  84. page.errors[:parent_title]
  85. page.parent_title = 'Another_page'
  86. assert page.save
  87. end
  88. def test_destroy
  89. page = WikiPage.find(1)
  90. page.destroy
  91. assert_nil WikiPage.find_by_id(1)
  92. # make sure that page content and its history are deleted
  93. assert WikiContent.find_all_by_page_id(1).empty?
  94. assert WikiContent.versioned_class.find_all_by_page_id(1).empty?
  95. end
  96. def test_destroy_should_not_nullify_children
  97. page = WikiPage.find(2)
  98. child_ids = page.child_ids
  99. assert child_ids.any?
  100. page.destroy
  101. assert_nil WikiPage.find_by_id(2)
  102. children = WikiPage.find_all_by_id(child_ids)
  103. assert_equal child_ids.size, children.size
  104. children.each do |child|
  105. assert_nil child.parent_id
  106. end
  107. end
  108. def test_updated_on_eager_load
  109. page = WikiPage.with_updated_on.first(:order => 'id')
  110. assert page.is_a?(WikiPage)
  111. assert_not_nil page.read_attribute(:updated_on)
  112. assert_equal Time.gm(2007, 3, 6, 23, 10, 51), page.content.updated_on
  113. assert_equal page.content.updated_on, page.updated_on
  114. assert_not_nil page.read_attribute(:version)
  115. end
  116. def test_descendants
  117. page = WikiPage.create!(:wiki => @wiki, :title => 'Parent')
  118. child1 = WikiPage.create!(:wiki => @wiki, :title => 'Child1', :parent => page)
  119. child11 = WikiPage.create!(:wiki => @wiki, :title => 'Child11', :parent => child1)
  120. child111 = WikiPage.create!(:wiki => @wiki, :title => 'Child111', :parent => child11)
  121. child2 = WikiPage.create!(:wiki => @wiki, :title => 'Child2', :parent => page)
  122. assert_equal %w(Child1 Child11 Child111 Child2), page.descendants.map(&:title).sort
  123. assert_equal %w(Child1 Child11 Child111 Child2), page.descendants(nil).map(&:title).sort
  124. assert_equal %w(Child1 Child11 Child2), page.descendants(2).map(&:title).sort
  125. assert_equal %w(Child1 Child2), page.descendants(1).map(&:title).sort
  126. assert_equal %w(Child1 Child11 Child111 Child2 Parent), page.self_and_descendants.map(&:title).sort
  127. assert_equal %w(Child1 Child11 Child111 Child2 Parent), page.self_and_descendants(nil).map(&:title).sort
  128. assert_equal %w(Child1 Child11 Child2 Parent), page.self_and_descendants(2).map(&:title).sort
  129. assert_equal %w(Child1 Child2 Parent), page.self_and_descendants(1).map(&:title).sort
  130. end
  131. def test_diff_for_page_with_deleted_version_should_pick_the_previous_available_version
  132. WikiContent::Version.find_by_page_id_and_version(1, 2).destroy
  133. page = WikiPage.find(1)
  134. diff = page.diff(3)
  135. assert_not_nil diff
  136. assert_equal 3, diff.content_to.version
  137. assert_equal 1, diff.content_from.version
  138. end
  139. end