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