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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 WikiPageTest < ActiveSupport::TestCase
  20. fixtures :projects, :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions
  21. def setup
  22. User.current = nil
  23. @wiki = Wiki.find(1)
  24. @page = @wiki.pages.first
  25. end
  26. def test_create
  27. page = WikiPage.new(:wiki => @wiki)
  28. assert !page.save
  29. assert_equal 1, page.errors.count
  30. page.title = "Page"
  31. assert page.save
  32. page.reload
  33. assert !page.protected?
  34. @wiki.reload
  35. assert @wiki.pages.include?(page)
  36. end
  37. def test_sidebar_should_be_protected_by_default
  38. page = @wiki.find_or_new_page('sidebar')
  39. assert page.new_record?
  40. assert page.protected?
  41. end
  42. def test_find_or_new_page
  43. page = @wiki.find_or_new_page("CookBook documentation")
  44. assert_kind_of WikiPage, page
  45. assert !page.new_record?
  46. page = @wiki.find_or_new_page("Non existing page")
  47. assert_kind_of WikiPage, page
  48. assert page.new_record?
  49. end
  50. def test_parent_title
  51. page = WikiPage.find_by_title('Another_page')
  52. assert_nil page.parent_title
  53. page = WikiPage.find_by_title('Page_with_an_inline_image')
  54. assert_equal 'CookBook documentation', page.parent_title
  55. end
  56. def test_assign_parent
  57. page = WikiPage.find_by_title('Another_page')
  58. page.parent_title = 'CookBook documentation'
  59. assert page.save
  60. page.reload
  61. assert_equal WikiPage.find_by_title('CookBook_documentation'), page.parent
  62. end
  63. def test_unassign_parent
  64. page = WikiPage.find_by_title('Page_with_an_inline_image')
  65. page.parent_title = ''
  66. assert page.save
  67. page.reload
  68. assert_nil page.parent
  69. end
  70. def test_parent_validation
  71. page = WikiPage.find_by_title('CookBook_documentation')
  72. # A page that doesn't exist
  73. page.parent_title = 'Unknown title'
  74. assert !page.save
  75. assert_include I18n.translate('activerecord.errors.messages.invalid'),
  76. page.errors[:parent_title]
  77. # A child page
  78. page.parent_title = 'Page_with_an_inline_image'
  79. assert !page.save
  80. assert_include I18n.translate('activerecord.errors.messages.circular_dependency'),
  81. page.errors[:parent_title]
  82. # The page itself
  83. page.parent_title = 'CookBook_documentation'
  84. assert !page.save
  85. assert_include I18n.translate('activerecord.errors.messages.circular_dependency'),
  86. page.errors[:parent_title]
  87. page.parent_title = 'Another_page'
  88. assert page.save
  89. end
  90. def test_move_child_should_clear_parent
  91. parent = WikiPage.create!(:wiki_id => 1, :title => 'Parent')
  92. child = WikiPage.create!(:wiki_id => 1, :title => 'Child', :parent => parent)
  93. child.wiki_id = 2
  94. child.save!
  95. assert_nil child.reload.parent_id
  96. end
  97. def test_move_parent_should_move_child_page
  98. parent = WikiPage.create!(:wiki_id => 1, :title => 'Parent')
  99. child = WikiPage.create!(:wiki_id => 1, :title => 'Child', :parent => parent)
  100. parent.reload
  101. parent.wiki_id = 2
  102. parent.save!
  103. assert_equal 2, child.reload.wiki_id
  104. assert_equal parent, child.parent
  105. end
  106. def test_move_parent_with_child_with_duplicate_name_should_not_move_child
  107. parent = WikiPage.create!(:wiki_id => 1, :title => 'Parent')
  108. child = WikiPage.create!(:wiki_id => 1, :title => 'Child', :parent_id => parent.id)
  109. parent.reload
  110. # page with the same name as the child in the target wiki
  111. WikiPage.create!(:wiki_id => 2, :title => 'Child')
  112. parent.wiki_id = 2
  113. parent.save!
  114. parent.reload
  115. assert_equal 2, parent.wiki_id
  116. child.reload
  117. assert_equal 1, child.wiki_id
  118. assert_nil child.parent_id
  119. end
  120. def test_destroy_should_delete_content_and_its_versions
  121. set_tmp_attachments_directory
  122. page = WikiPage.find(1)
  123. assert_difference 'WikiPage.count', -1 do
  124. assert_difference 'WikiContent.count', -1 do
  125. assert_difference 'WikiContentVersion.count', -3 do
  126. page.destroy
  127. end
  128. end
  129. end
  130. assert_nil WikiPage.find_by_id(1)
  131. assert_equal 0, WikiContent.where(:page_id => 1).count
  132. assert_equal 0, WikiContentVersion.where(:page_id => 1).count
  133. end
  134. def test_destroy_should_not_nullify_children
  135. page = WikiPage.find(2)
  136. child_ids = page.child_ids
  137. assert child_ids.any?
  138. page.destroy
  139. assert_nil WikiPage.find_by_id(2)
  140. children = WikiPage.where(:id => child_ids)
  141. assert_equal child_ids.size, children.count
  142. children.each do |child|
  143. assert_nil child.parent_id
  144. end
  145. end
  146. def test_with_updated_on_scope_should_preload_updated_on_and_version
  147. page = WikiPage.with_updated_on.where(:id => 1).first
  148. # make the assertions fail if attributes are not preloaded
  149. WikiContent.update_all(:updated_on => '2001-01-01 10:00:00', :version => 1)
  150. assert_equal Time.gm(2007, 3, 6, 23, 10, 51), page.updated_on
  151. assert_equal 3, page.version
  152. end
  153. def test_descendants
  154. page = WikiPage.create!(:wiki => @wiki, :title => 'Parent')
  155. child1 = WikiPage.create!(:wiki => @wiki, :title => 'Child1', :parent => page)
  156. child11 = WikiPage.create!(:wiki => @wiki, :title => 'Child11', :parent => child1)
  157. child111 = WikiPage.create!(:wiki => @wiki, :title => 'Child111', :parent => child11)
  158. child2 = WikiPage.create!(:wiki => @wiki, :title => 'Child2', :parent => page)
  159. assert_equal %w(Child1 Child11 Child111 Child2), page.descendants.map(&:title).sort
  160. assert_equal %w(Child1 Child11 Child111 Child2), page.descendants(nil).map(&:title).sort
  161. assert_equal %w(Child1 Child11 Child2), page.descendants(2).map(&:title).sort
  162. assert_equal %w(Child1 Child2), page.descendants(1).map(&:title).sort
  163. assert_equal %w(Child1 Child11 Child111 Child2 Parent), page.self_and_descendants.map(&:title).sort
  164. assert_equal %w(Child1 Child11 Child111 Child2 Parent), page.self_and_descendants(nil).map(&:title).sort
  165. assert_equal %w(Child1 Child11 Child2 Parent), page.self_and_descendants(2).map(&:title).sort
  166. assert_equal %w(Child1 Child2 Parent), page.self_and_descendants(1).map(&:title).sort
  167. end
  168. def test_diff_for_page_with_deleted_version_should_pick_the_previous_available_version
  169. WikiContentVersion.find_by_page_id_and_version(1, 2).destroy
  170. page = WikiPage.find(1)
  171. diff = page.diff(3)
  172. assert_not_nil diff
  173. assert_equal 3, diff.content_to.version
  174. assert_equal 1, diff.content_from.version
  175. end
  176. end