annotate .svn/pristine/4d/4d14758a6e50c23ba7fb276c8b7f166cdd68e14a.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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