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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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_move_child_should_clear_parent
  89. parent = WikiPage.create!(:wiki_id => 1, :title => 'Parent')
  90. child = WikiPage.create!(:wiki_id => 1, :title => 'Child', :parent => parent)
  91. child.wiki_id = 2
  92. child.save!
  93. assert_nil child.reload.parent_id
  94. end
  95. def test_move_parent_should_move_child_page
  96. parent = WikiPage.create!(:wiki_id => 1, :title => 'Parent')
  97. child = WikiPage.create!(:wiki_id => 1, :title => 'Child', :parent => parent)
  98. parent.reload
  99. parent.wiki_id = 2
  100. parent.save!
  101. assert_equal 2, child.reload.wiki_id
  102. assert_equal parent, child.parent
  103. end
  104. def test_move_parent_with_child_with_duplicate_name_should_not_move_child
  105. parent = WikiPage.create!(:wiki_id => 1, :title => 'Parent')
  106. child = WikiPage.create!(:wiki_id => 1, :title => 'Child', :parent_id => parent.id)
  107. parent.reload
  108. # page with the same name as the child in the target wiki
  109. WikiPage.create!(:wiki_id => 2, :title => 'Child')
  110. parent.wiki_id = 2
  111. parent.save!
  112. parent.reload
  113. assert_equal 2, parent.wiki_id
  114. child.reload
  115. assert_equal 1, child.wiki_id
  116. assert_nil child.parent_id
  117. end
  118. def test_destroy_should_delete_content_and_its_versions
  119. page = WikiPage.find(1)
  120. assert_difference 'WikiPage.count', -1 do
  121. assert_difference 'WikiContent.count', -1 do
  122. assert_difference 'WikiContentVersion.count', -3 do
  123. page.destroy
  124. end
  125. end
  126. end
  127. assert_nil WikiPage.find_by_id(1)
  128. assert_equal 0, WikiContent.where(:page_id => 1).count
  129. assert_equal 0, WikiContentVersion.where(:page_id => 1).count
  130. end
  131. def test_destroy_should_not_nullify_children
  132. page = WikiPage.find(2)
  133. child_ids = page.child_ids
  134. assert child_ids.any?
  135. page.destroy
  136. assert_nil WikiPage.find_by_id(2)
  137. children = WikiPage.where(:id => child_ids)
  138. assert_equal child_ids.size, children.count
  139. children.each do |child|
  140. assert_nil child.parent_id
  141. end
  142. end
  143. def test_with_updated_on_scope_should_preload_updated_on_and_version
  144. page = WikiPage.with_updated_on.where(:id => 1).first
  145. # make the assertions fail if attributes are not preloaded
  146. WikiContent.update_all(:updated_on => '2001-01-01 10:00:00', :version => 1)
  147. assert_equal Time.gm(2007, 3, 6, 23, 10, 51), page.updated_on
  148. assert_equal 3, page.version
  149. end
  150. def test_descendants
  151. page = WikiPage.create!(:wiki => @wiki, :title => 'Parent')
  152. child1 = WikiPage.create!(:wiki => @wiki, :title => 'Child1', :parent => page)
  153. child11 = WikiPage.create!(:wiki => @wiki, :title => 'Child11', :parent => child1)
  154. child111 = WikiPage.create!(:wiki => @wiki, :title => 'Child111', :parent => child11)
  155. child2 = WikiPage.create!(:wiki => @wiki, :title => 'Child2', :parent => page)
  156. assert_equal %w(Child1 Child11 Child111 Child2), page.descendants.map(&:title).sort
  157. assert_equal %w(Child1 Child11 Child111 Child2), page.descendants(nil).map(&:title).sort
  158. assert_equal %w(Child1 Child11 Child2), page.descendants(2).map(&:title).sort
  159. assert_equal %w(Child1 Child2), page.descendants(1).map(&:title).sort
  160. assert_equal %w(Child1 Child11 Child111 Child2 Parent), page.self_and_descendants.map(&:title).sort
  161. assert_equal %w(Child1 Child11 Child111 Child2 Parent), page.self_and_descendants(nil).map(&:title).sort
  162. assert_equal %w(Child1 Child11 Child2 Parent), page.self_and_descendants(2).map(&:title).sort
  163. assert_equal %w(Child1 Child2 Parent), page.self_and_descendants(1).map(&:title).sort
  164. end
  165. def test_diff_for_page_with_deleted_version_should_pick_the_previous_available_version
  166. WikiContent::Version.find_by_page_id_and_version(1, 2).destroy
  167. page = WikiPage.find(1)
  168. diff = page.diff(3)
  169. assert_not_nil diff
  170. assert_equal 3, diff.content_to.version
  171. assert_equal 1, diff.content_from.version
  172. end
  173. end