annotate test/unit/wiki_page_test.rb @ 1628:9c5f8e24dadc live tip

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