##// END OF EJS Templates
Rails4: replace deprecated Relation#first with finder options at WikiControllerTest...
Toshi MARUYAMA -
r12360:378a3e684fae
parent child
Show More
@@ -1,959 +1,959
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class WikiControllerTest < ActionController::TestCase
20 class WikiControllerTest < ActionController::TestCase
21 fixtures :projects, :users, :roles, :members, :member_roles,
21 fixtures :projects, :users, :roles, :members, :member_roles,
22 :enabled_modules, :wikis, :wiki_pages, :wiki_contents,
22 :enabled_modules, :wikis, :wiki_pages, :wiki_contents,
23 :wiki_content_versions, :attachments
23 :wiki_content_versions, :attachments
24
24
25 def setup
25 def setup
26 User.current = nil
26 User.current = nil
27 end
27 end
28
28
29 def test_show_start_page
29 def test_show_start_page
30 get :show, :project_id => 'ecookbook'
30 get :show, :project_id => 'ecookbook'
31 assert_response :success
31 assert_response :success
32 assert_template 'show'
32 assert_template 'show'
33 assert_tag :tag => 'h1', :content => /CookBook documentation/
33 assert_tag :tag => 'h1', :content => /CookBook documentation/
34
34
35 # child_pages macro
35 # child_pages macro
36 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
36 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
37 :child => { :tag => 'li',
37 :child => { :tag => 'li',
38 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
38 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
39 :content => 'Page with an inline image' } }
39 :content => 'Page with an inline image' } }
40 end
40 end
41
41
42 def test_export_link
42 def test_export_link
43 Role.anonymous.add_permission! :export_wiki_pages
43 Role.anonymous.add_permission! :export_wiki_pages
44 get :show, :project_id => 'ecookbook'
44 get :show, :project_id => 'ecookbook'
45 assert_response :success
45 assert_response :success
46 assert_tag 'a', :attributes => {:href => '/projects/ecookbook/wiki/CookBook_documentation.txt'}
46 assert_tag 'a', :attributes => {:href => '/projects/ecookbook/wiki/CookBook_documentation.txt'}
47 end
47 end
48
48
49 def test_show_page_with_name
49 def test_show_page_with_name
50 get :show, :project_id => 1, :id => 'Another_page'
50 get :show, :project_id => 1, :id => 'Another_page'
51 assert_response :success
51 assert_response :success
52 assert_template 'show'
52 assert_template 'show'
53 assert_tag :tag => 'h1', :content => /Another page/
53 assert_tag :tag => 'h1', :content => /Another page/
54 # Included page with an inline image
54 # Included page with an inline image
55 assert_tag :tag => 'p', :content => /This is an inline image/
55 assert_tag :tag => 'p', :content => /This is an inline image/
56 assert_tag :tag => 'img', :attributes => { :src => '/attachments/download/3/logo.gif',
56 assert_tag :tag => 'img', :attributes => { :src => '/attachments/download/3/logo.gif',
57 :alt => 'This is a logo' }
57 :alt => 'This is a logo' }
58 end
58 end
59
59
60 def test_show_old_version
60 def test_show_old_version
61 get :show, :project_id => 'ecookbook', :id => 'CookBook_documentation', :version => '2'
61 get :show, :project_id => 'ecookbook', :id => 'CookBook_documentation', :version => '2'
62 assert_response :success
62 assert_response :success
63 assert_template 'show'
63 assert_template 'show'
64
64
65 assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/1', :text => /Previous/
65 assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/1', :text => /Previous/
66 assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2/diff', :text => /diff/
66 assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2/diff', :text => /diff/
67 assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/3', :text => /Next/
67 assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/3', :text => /Next/
68 assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation', :text => /Current version/
68 assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation', :text => /Current version/
69 end
69 end
70
70
71 def test_show_old_version_with_attachments
71 def test_show_old_version_with_attachments
72 page = WikiPage.find(4)
72 page = WikiPage.find(4)
73 assert page.attachments.any?
73 assert page.attachments.any?
74 content = page.content
74 content = page.content
75 content.text = "update"
75 content.text = "update"
76 content.save!
76 content.save!
77
77
78 get :show, :project_id => 'ecookbook', :id => page.title, :version => '1'
78 get :show, :project_id => 'ecookbook', :id => page.title, :version => '1'
79 assert_kind_of WikiContent::Version, assigns(:content)
79 assert_kind_of WikiContent::Version, assigns(:content)
80 assert_response :success
80 assert_response :success
81 assert_template 'show'
81 assert_template 'show'
82 end
82 end
83
83
84 def test_show_old_version_without_permission_should_be_denied
84 def test_show_old_version_without_permission_should_be_denied
85 Role.anonymous.remove_permission! :view_wiki_edits
85 Role.anonymous.remove_permission! :view_wiki_edits
86
86
87 get :show, :project_id => 'ecookbook', :id => 'CookBook_documentation', :version => '2'
87 get :show, :project_id => 'ecookbook', :id => 'CookBook_documentation', :version => '2'
88 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fprojects%2Fecookbook%2Fwiki%2FCookBook_documentation%2F2'
88 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fprojects%2Fecookbook%2Fwiki%2FCookBook_documentation%2F2'
89 end
89 end
90
90
91 def test_show_first_version
91 def test_show_first_version
92 get :show, :project_id => 'ecookbook', :id => 'CookBook_documentation', :version => '1'
92 get :show, :project_id => 'ecookbook', :id => 'CookBook_documentation', :version => '1'
93 assert_response :success
93 assert_response :success
94 assert_template 'show'
94 assert_template 'show'
95
95
96 assert_select 'a', :text => /Previous/, :count => 0
96 assert_select 'a', :text => /Previous/, :count => 0
97 assert_select 'a', :text => /diff/, :count => 0
97 assert_select 'a', :text => /diff/, :count => 0
98 assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2', :text => /Next/
98 assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2', :text => /Next/
99 assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation', :text => /Current version/
99 assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation', :text => /Current version/
100 end
100 end
101
101
102 def test_show_redirected_page
102 def test_show_redirected_page
103 WikiRedirect.create!(:wiki_id => 1, :title => 'Old_title', :redirects_to => 'Another_page')
103 WikiRedirect.create!(:wiki_id => 1, :title => 'Old_title', :redirects_to => 'Another_page')
104
104
105 get :show, :project_id => 'ecookbook', :id => 'Old_title'
105 get :show, :project_id => 'ecookbook', :id => 'Old_title'
106 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
106 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
107 end
107 end
108
108
109 def test_show_with_sidebar
109 def test_show_with_sidebar
110 page = Project.find(1).wiki.pages.new(:title => 'Sidebar')
110 page = Project.find(1).wiki.pages.new(:title => 'Sidebar')
111 page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar')
111 page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar')
112 page.save!
112 page.save!
113
113
114 get :show, :project_id => 1, :id => 'Another_page'
114 get :show, :project_id => 1, :id => 'Another_page'
115 assert_response :success
115 assert_response :success
116 assert_tag :tag => 'div', :attributes => {:id => 'sidebar'},
116 assert_tag :tag => 'div', :attributes => {:id => 'sidebar'},
117 :content => /Side bar content for test_show_with_sidebar/
117 :content => /Side bar content for test_show_with_sidebar/
118 end
118 end
119
119
120 def test_show_should_display_section_edit_links
120 def test_show_should_display_section_edit_links
121 @request.session[:user_id] = 2
121 @request.session[:user_id] = 2
122 get :show, :project_id => 1, :id => 'Page with sections'
122 get :show, :project_id => 1, :id => 'Page with sections'
123 assert_no_tag 'a', :attributes => {
123 assert_no_tag 'a', :attributes => {
124 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=1'
124 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=1'
125 }
125 }
126 assert_tag 'a', :attributes => {
126 assert_tag 'a', :attributes => {
127 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
127 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
128 }
128 }
129 assert_tag 'a', :attributes => {
129 assert_tag 'a', :attributes => {
130 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=3'
130 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=3'
131 }
131 }
132 end
132 end
133
133
134 def test_show_current_version_should_display_section_edit_links
134 def test_show_current_version_should_display_section_edit_links
135 @request.session[:user_id] = 2
135 @request.session[:user_id] = 2
136 get :show, :project_id => 1, :id => 'Page with sections', :version => 3
136 get :show, :project_id => 1, :id => 'Page with sections', :version => 3
137
137
138 assert_tag 'a', :attributes => {
138 assert_tag 'a', :attributes => {
139 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
139 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
140 }
140 }
141 end
141 end
142
142
143 def test_show_old_version_should_not_display_section_edit_links
143 def test_show_old_version_should_not_display_section_edit_links
144 @request.session[:user_id] = 2
144 @request.session[:user_id] = 2
145 get :show, :project_id => 1, :id => 'Page with sections', :version => 2
145 get :show, :project_id => 1, :id => 'Page with sections', :version => 2
146
146
147 assert_no_tag 'a', :attributes => {
147 assert_no_tag 'a', :attributes => {
148 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
148 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
149 }
149 }
150 end
150 end
151
151
152 def test_show_unexistent_page_without_edit_right
152 def test_show_unexistent_page_without_edit_right
153 get :show, :project_id => 1, :id => 'Unexistent page'
153 get :show, :project_id => 1, :id => 'Unexistent page'
154 assert_response 404
154 assert_response 404
155 end
155 end
156
156
157 def test_show_unexistent_page_with_edit_right
157 def test_show_unexistent_page_with_edit_right
158 @request.session[:user_id] = 2
158 @request.session[:user_id] = 2
159 get :show, :project_id => 1, :id => 'Unexistent page'
159 get :show, :project_id => 1, :id => 'Unexistent page'
160 assert_response :success
160 assert_response :success
161 assert_template 'edit'
161 assert_template 'edit'
162 end
162 end
163
163
164 def test_show_unexistent_page_with_parent_should_preselect_parent
164 def test_show_unexistent_page_with_parent_should_preselect_parent
165 @request.session[:user_id] = 2
165 @request.session[:user_id] = 2
166 get :show, :project_id => 1, :id => 'Unexistent page', :parent => 'Another_page'
166 get :show, :project_id => 1, :id => 'Unexistent page', :parent => 'Another_page'
167 assert_response :success
167 assert_response :success
168 assert_template 'edit'
168 assert_template 'edit'
169 assert_tag 'select', :attributes => {:name => 'wiki_page[parent_id]'},
169 assert_tag 'select', :attributes => {:name => 'wiki_page[parent_id]'},
170 :child => {:tag => 'option', :attributes => {:value => '2', :selected => 'selected'}}
170 :child => {:tag => 'option', :attributes => {:value => '2', :selected => 'selected'}}
171 end
171 end
172
172
173 def test_show_should_not_show_history_without_permission
173 def test_show_should_not_show_history_without_permission
174 Role.anonymous.remove_permission! :view_wiki_edits
174 Role.anonymous.remove_permission! :view_wiki_edits
175 get :show, :project_id => 1, :id => 'Page with sections', :version => 2
175 get :show, :project_id => 1, :id => 'Page with sections', :version => 2
176
176
177 assert_response 302
177 assert_response 302
178 end
178 end
179
179
180 def test_show_page_without_content_should_display_the_edit_form
180 def test_show_page_without_content_should_display_the_edit_form
181 @request.session[:user_id] = 2
181 @request.session[:user_id] = 2
182 WikiPage.create!(:title => 'NoContent', :wiki => Project.find(1).wiki)
182 WikiPage.create!(:title => 'NoContent', :wiki => Project.find(1).wiki)
183
183
184 get :show, :project_id => 1, :id => 'NoContent'
184 get :show, :project_id => 1, :id => 'NoContent'
185 assert_response :success
185 assert_response :success
186 assert_template 'edit'
186 assert_template 'edit'
187 assert_select 'textarea[name=?]', 'content[text]'
187 assert_select 'textarea[name=?]', 'content[text]'
188 end
188 end
189
189
190 def test_create_page
190 def test_create_page
191 @request.session[:user_id] = 2
191 @request.session[:user_id] = 2
192 assert_difference 'WikiPage.count' do
192 assert_difference 'WikiPage.count' do
193 assert_difference 'WikiContent.count' do
193 assert_difference 'WikiContent.count' do
194 put :update, :project_id => 1,
194 put :update, :project_id => 1,
195 :id => 'New page',
195 :id => 'New page',
196 :content => {:comments => 'Created the page',
196 :content => {:comments => 'Created the page',
197 :text => "h1. New page\n\nThis is a new page",
197 :text => "h1. New page\n\nThis is a new page",
198 :version => 0}
198 :version => 0}
199 end
199 end
200 end
200 end
201 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'New_page'
201 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'New_page'
202 page = Project.find(1).wiki.find_page('New page')
202 page = Project.find(1).wiki.find_page('New page')
203 assert !page.new_record?
203 assert !page.new_record?
204 assert_not_nil page.content
204 assert_not_nil page.content
205 assert_nil page.parent
205 assert_nil page.parent
206 assert_equal 'Created the page', page.content.comments
206 assert_equal 'Created the page', page.content.comments
207 end
207 end
208
208
209 def test_create_page_with_attachments
209 def test_create_page_with_attachments
210 @request.session[:user_id] = 2
210 @request.session[:user_id] = 2
211 assert_difference 'WikiPage.count' do
211 assert_difference 'WikiPage.count' do
212 assert_difference 'Attachment.count' do
212 assert_difference 'Attachment.count' do
213 put :update, :project_id => 1,
213 put :update, :project_id => 1,
214 :id => 'New page',
214 :id => 'New page',
215 :content => {:comments => 'Created the page',
215 :content => {:comments => 'Created the page',
216 :text => "h1. New page\n\nThis is a new page",
216 :text => "h1. New page\n\nThis is a new page",
217 :version => 0},
217 :version => 0},
218 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
218 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
219 end
219 end
220 end
220 end
221 page = Project.find(1).wiki.find_page('New page')
221 page = Project.find(1).wiki.find_page('New page')
222 assert_equal 1, page.attachments.count
222 assert_equal 1, page.attachments.count
223 assert_equal 'testfile.txt', page.attachments.first.filename
223 assert_equal 'testfile.txt', page.attachments.first.filename
224 end
224 end
225
225
226 def test_create_page_with_parent
226 def test_create_page_with_parent
227 @request.session[:user_id] = 2
227 @request.session[:user_id] = 2
228 assert_difference 'WikiPage.count' do
228 assert_difference 'WikiPage.count' do
229 put :update, :project_id => 1, :id => 'New page',
229 put :update, :project_id => 1, :id => 'New page',
230 :content => {:text => "h1. New page\n\nThis is a new page", :version => 0},
230 :content => {:text => "h1. New page\n\nThis is a new page", :version => 0},
231 :wiki_page => {:parent_id => 2}
231 :wiki_page => {:parent_id => 2}
232 end
232 end
233 page = Project.find(1).wiki.find_page('New page')
233 page = Project.find(1).wiki.find_page('New page')
234 assert_equal WikiPage.find(2), page.parent
234 assert_equal WikiPage.find(2), page.parent
235 end
235 end
236
236
237 def test_edit_page
237 def test_edit_page
238 @request.session[:user_id] = 2
238 @request.session[:user_id] = 2
239 get :edit, :project_id => 'ecookbook', :id => 'Another_page'
239 get :edit, :project_id => 'ecookbook', :id => 'Another_page'
240
240
241 assert_response :success
241 assert_response :success
242 assert_template 'edit'
242 assert_template 'edit'
243
243
244 assert_tag 'textarea',
244 assert_tag 'textarea',
245 :attributes => { :name => 'content[text]' },
245 :attributes => { :name => 'content[text]' },
246 :content => "\n"+WikiPage.find_by_title('Another_page').content.text
246 :content => "\n"+WikiPage.find_by_title('Another_page').content.text
247 end
247 end
248
248
249 def test_edit_section
249 def test_edit_section
250 @request.session[:user_id] = 2
250 @request.session[:user_id] = 2
251 get :edit, :project_id => 'ecookbook', :id => 'Page_with_sections', :section => 2
251 get :edit, :project_id => 'ecookbook', :id => 'Page_with_sections', :section => 2
252
252
253 assert_response :success
253 assert_response :success
254 assert_template 'edit'
254 assert_template 'edit'
255
255
256 page = WikiPage.find_by_title('Page_with_sections')
256 page = WikiPage.find_by_title('Page_with_sections')
257 section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
257 section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
258
258
259 assert_tag 'textarea',
259 assert_tag 'textarea',
260 :attributes => { :name => 'content[text]' },
260 :attributes => { :name => 'content[text]' },
261 :content => "\n"+section
261 :content => "\n"+section
262 assert_tag 'input',
262 assert_tag 'input',
263 :attributes => { :name => 'section', :type => 'hidden', :value => '2' }
263 :attributes => { :name => 'section', :type => 'hidden', :value => '2' }
264 assert_tag 'input',
264 assert_tag 'input',
265 :attributes => { :name => 'section_hash', :type => 'hidden', :value => hash }
265 :attributes => { :name => 'section_hash', :type => 'hidden', :value => hash }
266 end
266 end
267
267
268 def test_edit_invalid_section_should_respond_with_404
268 def test_edit_invalid_section_should_respond_with_404
269 @request.session[:user_id] = 2
269 @request.session[:user_id] = 2
270 get :edit, :project_id => 'ecookbook', :id => 'Page_with_sections', :section => 10
270 get :edit, :project_id => 'ecookbook', :id => 'Page_with_sections', :section => 10
271
271
272 assert_response 404
272 assert_response 404
273 end
273 end
274
274
275 def test_update_page
275 def test_update_page
276 @request.session[:user_id] = 2
276 @request.session[:user_id] = 2
277 assert_no_difference 'WikiPage.count' do
277 assert_no_difference 'WikiPage.count' do
278 assert_no_difference 'WikiContent.count' do
278 assert_no_difference 'WikiContent.count' do
279 assert_difference 'WikiContent::Version.count' do
279 assert_difference 'WikiContent::Version.count' do
280 put :update, :project_id => 1,
280 put :update, :project_id => 1,
281 :id => 'Another_page',
281 :id => 'Another_page',
282 :content => {
282 :content => {
283 :comments => "my comments",
283 :comments => "my comments",
284 :text => "edited",
284 :text => "edited",
285 :version => 1
285 :version => 1
286 }
286 }
287 end
287 end
288 end
288 end
289 end
289 end
290 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
290 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
291
291
292 page = Wiki.find(1).pages.find_by_title('Another_page')
292 page = Wiki.find(1).pages.find_by_title('Another_page')
293 assert_equal "edited", page.content.text
293 assert_equal "edited", page.content.text
294 assert_equal 2, page.content.version
294 assert_equal 2, page.content.version
295 assert_equal "my comments", page.content.comments
295 assert_equal "my comments", page.content.comments
296 end
296 end
297
297
298 def test_update_page_with_parent
298 def test_update_page_with_parent
299 @request.session[:user_id] = 2
299 @request.session[:user_id] = 2
300 assert_no_difference 'WikiPage.count' do
300 assert_no_difference 'WikiPage.count' do
301 assert_no_difference 'WikiContent.count' do
301 assert_no_difference 'WikiContent.count' do
302 assert_difference 'WikiContent::Version.count' do
302 assert_difference 'WikiContent::Version.count' do
303 put :update, :project_id => 1,
303 put :update, :project_id => 1,
304 :id => 'Another_page',
304 :id => 'Another_page',
305 :content => {
305 :content => {
306 :comments => "my comments",
306 :comments => "my comments",
307 :text => "edited",
307 :text => "edited",
308 :version => 1
308 :version => 1
309 },
309 },
310 :wiki_page => {:parent_id => '1'}
310 :wiki_page => {:parent_id => '1'}
311 end
311 end
312 end
312 end
313 end
313 end
314 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
314 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
315
315
316 page = Wiki.find(1).pages.find_by_title('Another_page')
316 page = Wiki.find(1).pages.find_by_title('Another_page')
317 assert_equal "edited", page.content.text
317 assert_equal "edited", page.content.text
318 assert_equal 2, page.content.version
318 assert_equal 2, page.content.version
319 assert_equal "my comments", page.content.comments
319 assert_equal "my comments", page.content.comments
320 assert_equal WikiPage.find(1), page.parent
320 assert_equal WikiPage.find(1), page.parent
321 end
321 end
322
322
323 def test_update_page_with_failure
323 def test_update_page_with_failure
324 @request.session[:user_id] = 2
324 @request.session[:user_id] = 2
325 assert_no_difference 'WikiPage.count' do
325 assert_no_difference 'WikiPage.count' do
326 assert_no_difference 'WikiContent.count' do
326 assert_no_difference 'WikiContent.count' do
327 assert_no_difference 'WikiContent::Version.count' do
327 assert_no_difference 'WikiContent::Version.count' do
328 put :update, :project_id => 1,
328 put :update, :project_id => 1,
329 :id => 'Another_page',
329 :id => 'Another_page',
330 :content => {
330 :content => {
331 :comments => 'a' * 300, # failure here, comment is too long
331 :comments => 'a' * 300, # failure here, comment is too long
332 :text => 'edited',
332 :text => 'edited',
333 :version => 1
333 :version => 1
334 }
334 }
335 end
335 end
336 end
336 end
337 end
337 end
338 assert_response :success
338 assert_response :success
339 assert_template 'edit'
339 assert_template 'edit'
340
340
341 assert_error_tag :descendant => {:content => /Comment is too long/}
341 assert_error_tag :descendant => {:content => /Comment is too long/}
342 assert_tag :tag => 'textarea', :attributes => {:id => 'content_text'}, :content => "\nedited"
342 assert_tag :tag => 'textarea', :attributes => {:id => 'content_text'}, :content => "\nedited"
343 assert_tag :tag => 'input', :attributes => {:id => 'content_version', :value => '1'}
343 assert_tag :tag => 'input', :attributes => {:id => 'content_version', :value => '1'}
344 end
344 end
345
345
346 def test_update_page_with_parent_change_only_should_not_create_content_version
346 def test_update_page_with_parent_change_only_should_not_create_content_version
347 @request.session[:user_id] = 2
347 @request.session[:user_id] = 2
348 assert_no_difference 'WikiPage.count' do
348 assert_no_difference 'WikiPage.count' do
349 assert_no_difference 'WikiContent.count' do
349 assert_no_difference 'WikiContent.count' do
350 assert_no_difference 'WikiContent::Version.count' do
350 assert_no_difference 'WikiContent::Version.count' do
351 put :update, :project_id => 1,
351 put :update, :project_id => 1,
352 :id => 'Another_page',
352 :id => 'Another_page',
353 :content => {
353 :content => {
354 :comments => '',
354 :comments => '',
355 :text => Wiki.find(1).find_page('Another_page').content.text,
355 :text => Wiki.find(1).find_page('Another_page').content.text,
356 :version => 1
356 :version => 1
357 },
357 },
358 :wiki_page => {:parent_id => '1'}
358 :wiki_page => {:parent_id => '1'}
359 end
359 end
360 end
360 end
361 end
361 end
362 page = Wiki.find(1).pages.find_by_title('Another_page')
362 page = Wiki.find(1).pages.find_by_title('Another_page')
363 assert_equal 1, page.content.version
363 assert_equal 1, page.content.version
364 assert_equal WikiPage.find(1), page.parent
364 assert_equal WikiPage.find(1), page.parent
365 end
365 end
366
366
367 def test_update_page_with_attachments_only_should_not_create_content_version
367 def test_update_page_with_attachments_only_should_not_create_content_version
368 @request.session[:user_id] = 2
368 @request.session[:user_id] = 2
369 assert_no_difference 'WikiPage.count' do
369 assert_no_difference 'WikiPage.count' do
370 assert_no_difference 'WikiContent.count' do
370 assert_no_difference 'WikiContent.count' do
371 assert_no_difference 'WikiContent::Version.count' do
371 assert_no_difference 'WikiContent::Version.count' do
372 assert_difference 'Attachment.count' do
372 assert_difference 'Attachment.count' do
373 put :update, :project_id => 1,
373 put :update, :project_id => 1,
374 :id => 'Another_page',
374 :id => 'Another_page',
375 :content => {
375 :content => {
376 :comments => '',
376 :comments => '',
377 :text => Wiki.find(1).find_page('Another_page').content.text,
377 :text => Wiki.find(1).find_page('Another_page').content.text,
378 :version => 1
378 :version => 1
379 },
379 },
380 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
380 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
381 end
381 end
382 end
382 end
383 end
383 end
384 end
384 end
385 page = Wiki.find(1).pages.find_by_title('Another_page')
385 page = Wiki.find(1).pages.find_by_title('Another_page')
386 assert_equal 1, page.content.version
386 assert_equal 1, page.content.version
387 end
387 end
388
388
389 def test_update_stale_page_should_not_raise_an_error
389 def test_update_stale_page_should_not_raise_an_error
390 @request.session[:user_id] = 2
390 @request.session[:user_id] = 2
391 c = Wiki.find(1).find_page('Another_page').content
391 c = Wiki.find(1).find_page('Another_page').content
392 c.text = 'Previous text'
392 c.text = 'Previous text'
393 c.save!
393 c.save!
394 assert_equal 2, c.version
394 assert_equal 2, c.version
395
395
396 assert_no_difference 'WikiPage.count' do
396 assert_no_difference 'WikiPage.count' do
397 assert_no_difference 'WikiContent.count' do
397 assert_no_difference 'WikiContent.count' do
398 assert_no_difference 'WikiContent::Version.count' do
398 assert_no_difference 'WikiContent::Version.count' do
399 put :update, :project_id => 1,
399 put :update, :project_id => 1,
400 :id => 'Another_page',
400 :id => 'Another_page',
401 :content => {
401 :content => {
402 :comments => 'My comments',
402 :comments => 'My comments',
403 :text => 'Text should not be lost',
403 :text => 'Text should not be lost',
404 :version => 1
404 :version => 1
405 }
405 }
406 end
406 end
407 end
407 end
408 end
408 end
409 assert_response :success
409 assert_response :success
410 assert_template 'edit'
410 assert_template 'edit'
411 assert_tag :div,
411 assert_tag :div,
412 :attributes => { :class => /error/ },
412 :attributes => { :class => /error/ },
413 :content => /Data has been updated by another user/
413 :content => /Data has been updated by another user/
414 assert_tag 'textarea',
414 assert_tag 'textarea',
415 :attributes => { :name => 'content[text]' },
415 :attributes => { :name => 'content[text]' },
416 :content => /Text should not be lost/
416 :content => /Text should not be lost/
417 assert_tag 'input',
417 assert_tag 'input',
418 :attributes => { :name => 'content[comments]', :value => 'My comments' }
418 :attributes => { :name => 'content[comments]', :value => 'My comments' }
419
419
420 c.reload
420 c.reload
421 assert_equal 'Previous text', c.text
421 assert_equal 'Previous text', c.text
422 assert_equal 2, c.version
422 assert_equal 2, c.version
423 end
423 end
424
424
425 def test_update_page_without_content_should_create_content
425 def test_update_page_without_content_should_create_content
426 @request.session[:user_id] = 2
426 @request.session[:user_id] = 2
427 page = WikiPage.create!(:title => 'NoContent', :wiki => Project.find(1).wiki)
427 page = WikiPage.create!(:title => 'NoContent', :wiki => Project.find(1).wiki)
428
428
429 assert_no_difference 'WikiPage.count' do
429 assert_no_difference 'WikiPage.count' do
430 assert_difference 'WikiContent.count' do
430 assert_difference 'WikiContent.count' do
431 put :update, :project_id => 1, :id => 'NoContent', :content => {:text => 'Some content'}
431 put :update, :project_id => 1, :id => 'NoContent', :content => {:text => 'Some content'}
432 assert_response 302
432 assert_response 302
433 end
433 end
434 end
434 end
435 assert_equal 'Some content', page.reload.content.text
435 assert_equal 'Some content', page.reload.content.text
436 end
436 end
437
437
438 def test_update_section
438 def test_update_section
439 @request.session[:user_id] = 2
439 @request.session[:user_id] = 2
440 page = WikiPage.find_by_title('Page_with_sections')
440 page = WikiPage.find_by_title('Page_with_sections')
441 section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
441 section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
442 text = page.content.text
442 text = page.content.text
443
443
444 assert_no_difference 'WikiPage.count' do
444 assert_no_difference 'WikiPage.count' do
445 assert_no_difference 'WikiContent.count' do
445 assert_no_difference 'WikiContent.count' do
446 assert_difference 'WikiContent::Version.count' do
446 assert_difference 'WikiContent::Version.count' do
447 put :update, :project_id => 1, :id => 'Page_with_sections',
447 put :update, :project_id => 1, :id => 'Page_with_sections',
448 :content => {
448 :content => {
449 :text => "New section content",
449 :text => "New section content",
450 :version => 3
450 :version => 3
451 },
451 },
452 :section => 2,
452 :section => 2,
453 :section_hash => hash
453 :section_hash => hash
454 end
454 end
455 end
455 end
456 end
456 end
457 assert_redirected_to '/projects/ecookbook/wiki/Page_with_sections#section-2'
457 assert_redirected_to '/projects/ecookbook/wiki/Page_with_sections#section-2'
458 assert_equal Redmine::WikiFormatting::Textile::Formatter.new(text).update_section(2, "New section content"), page.reload.content.text
458 assert_equal Redmine::WikiFormatting::Textile::Formatter.new(text).update_section(2, "New section content"), page.reload.content.text
459 end
459 end
460
460
461 def test_update_section_should_allow_stale_page_update
461 def test_update_section_should_allow_stale_page_update
462 @request.session[:user_id] = 2
462 @request.session[:user_id] = 2
463 page = WikiPage.find_by_title('Page_with_sections')
463 page = WikiPage.find_by_title('Page_with_sections')
464 section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
464 section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
465 text = page.content.text
465 text = page.content.text
466
466
467 assert_no_difference 'WikiPage.count' do
467 assert_no_difference 'WikiPage.count' do
468 assert_no_difference 'WikiContent.count' do
468 assert_no_difference 'WikiContent.count' do
469 assert_difference 'WikiContent::Version.count' do
469 assert_difference 'WikiContent::Version.count' do
470 put :update, :project_id => 1, :id => 'Page_with_sections',
470 put :update, :project_id => 1, :id => 'Page_with_sections',
471 :content => {
471 :content => {
472 :text => "New section content",
472 :text => "New section content",
473 :version => 2 # Current version is 3
473 :version => 2 # Current version is 3
474 },
474 },
475 :section => 2,
475 :section => 2,
476 :section_hash => hash
476 :section_hash => hash
477 end
477 end
478 end
478 end
479 end
479 end
480 assert_redirected_to '/projects/ecookbook/wiki/Page_with_sections#section-2'
480 assert_redirected_to '/projects/ecookbook/wiki/Page_with_sections#section-2'
481 page.reload
481 page.reload
482 assert_equal Redmine::WikiFormatting::Textile::Formatter.new(text).update_section(2, "New section content"), page.content.text
482 assert_equal Redmine::WikiFormatting::Textile::Formatter.new(text).update_section(2, "New section content"), page.content.text
483 assert_equal 4, page.content.version
483 assert_equal 4, page.content.version
484 end
484 end
485
485
486 def test_update_section_should_not_allow_stale_section_update
486 def test_update_section_should_not_allow_stale_section_update
487 @request.session[:user_id] = 2
487 @request.session[:user_id] = 2
488
488
489 assert_no_difference 'WikiPage.count' do
489 assert_no_difference 'WikiPage.count' do
490 assert_no_difference 'WikiContent.count' do
490 assert_no_difference 'WikiContent.count' do
491 assert_no_difference 'WikiContent::Version.count' do
491 assert_no_difference 'WikiContent::Version.count' do
492 put :update, :project_id => 1, :id => 'Page_with_sections',
492 put :update, :project_id => 1, :id => 'Page_with_sections',
493 :content => {
493 :content => {
494 :comments => 'My comments',
494 :comments => 'My comments',
495 :text => "Text should not be lost",
495 :text => "Text should not be lost",
496 :version => 3
496 :version => 3
497 },
497 },
498 :section => 2,
498 :section => 2,
499 :section_hash => Digest::MD5.hexdigest("wrong hash")
499 :section_hash => Digest::MD5.hexdigest("wrong hash")
500 end
500 end
501 end
501 end
502 end
502 end
503 assert_response :success
503 assert_response :success
504 assert_template 'edit'
504 assert_template 'edit'
505 assert_tag :div,
505 assert_tag :div,
506 :attributes => { :class => /error/ },
506 :attributes => { :class => /error/ },
507 :content => /Data has been updated by another user/
507 :content => /Data has been updated by another user/
508 assert_tag 'textarea',
508 assert_tag 'textarea',
509 :attributes => { :name => 'content[text]' },
509 :attributes => { :name => 'content[text]' },
510 :content => /Text should not be lost/
510 :content => /Text should not be lost/
511 assert_tag 'input',
511 assert_tag 'input',
512 :attributes => { :name => 'content[comments]', :value => 'My comments' }
512 :attributes => { :name => 'content[comments]', :value => 'My comments' }
513 end
513 end
514
514
515 def test_preview
515 def test_preview
516 @request.session[:user_id] = 2
516 @request.session[:user_id] = 2
517 xhr :post, :preview, :project_id => 1, :id => 'CookBook_documentation',
517 xhr :post, :preview, :project_id => 1, :id => 'CookBook_documentation',
518 :content => { :comments => '',
518 :content => { :comments => '',
519 :text => 'this is a *previewed text*',
519 :text => 'this is a *previewed text*',
520 :version => 3 }
520 :version => 3 }
521 assert_response :success
521 assert_response :success
522 assert_template 'common/_preview'
522 assert_template 'common/_preview'
523 assert_tag :tag => 'strong', :content => /previewed text/
523 assert_tag :tag => 'strong', :content => /previewed text/
524 end
524 end
525
525
526 def test_preview_new_page
526 def test_preview_new_page
527 @request.session[:user_id] = 2
527 @request.session[:user_id] = 2
528 xhr :post, :preview, :project_id => 1, :id => 'New page',
528 xhr :post, :preview, :project_id => 1, :id => 'New page',
529 :content => { :text => 'h1. New page',
529 :content => { :text => 'h1. New page',
530 :comments => '',
530 :comments => '',
531 :version => 0 }
531 :version => 0 }
532 assert_response :success
532 assert_response :success
533 assert_template 'common/_preview'
533 assert_template 'common/_preview'
534 assert_tag :tag => 'h1', :content => /New page/
534 assert_tag :tag => 'h1', :content => /New page/
535 end
535 end
536
536
537 def test_history
537 def test_history
538 @request.session[:user_id] = 2
538 @request.session[:user_id] = 2
539 get :history, :project_id => 'ecookbook', :id => 'CookBook_documentation'
539 get :history, :project_id => 'ecookbook', :id => 'CookBook_documentation'
540 assert_response :success
540 assert_response :success
541 assert_template 'history'
541 assert_template 'history'
542 assert_not_nil assigns(:versions)
542 assert_not_nil assigns(:versions)
543 assert_equal 3, assigns(:versions).size
543 assert_equal 3, assigns(:versions).size
544
544
545 assert_select "input[type=submit][name=commit]"
545 assert_select "input[type=submit][name=commit]"
546 assert_select 'td' do
546 assert_select 'td' do
547 assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2', :text => '2'
547 assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2', :text => '2'
548 assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2/annotate', :text => 'Annotate'
548 assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2/annotate', :text => 'Annotate'
549 assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2', :text => 'Delete'
549 assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2', :text => 'Delete'
550 end
550 end
551 end
551 end
552
552
553 def test_history_with_one_version
553 def test_history_with_one_version
554 @request.session[:user_id] = 2
554 @request.session[:user_id] = 2
555 get :history, :project_id => 'ecookbook', :id => 'Another_page'
555 get :history, :project_id => 'ecookbook', :id => 'Another_page'
556 assert_response :success
556 assert_response :success
557 assert_template 'history'
557 assert_template 'history'
558 assert_not_nil assigns(:versions)
558 assert_not_nil assigns(:versions)
559 assert_equal 1, assigns(:versions).size
559 assert_equal 1, assigns(:versions).size
560 assert_select "input[type=submit][name=commit]", false
560 assert_select "input[type=submit][name=commit]", false
561 assert_select 'td' do
561 assert_select 'td' do
562 assert_select 'a[href=?]', '/projects/ecookbook/wiki/Another_page/1', :text => '1'
562 assert_select 'a[href=?]', '/projects/ecookbook/wiki/Another_page/1', :text => '1'
563 assert_select 'a[href=?]', '/projects/ecookbook/wiki/Another_page/1/annotate', :text => 'Annotate'
563 assert_select 'a[href=?]', '/projects/ecookbook/wiki/Another_page/1/annotate', :text => 'Annotate'
564 assert_select 'a[href=?]', '/projects/ecookbook/wiki/Another_page/1', :text => 'Delete', :count => 0
564 assert_select 'a[href=?]', '/projects/ecookbook/wiki/Another_page/1', :text => 'Delete', :count => 0
565 end
565 end
566 end
566 end
567
567
568 def test_diff
568 def test_diff
569 content = WikiPage.find(1).content
569 content = WikiPage.find(1).content
570 assert_difference 'WikiContent::Version.count', 2 do
570 assert_difference 'WikiContent::Version.count', 2 do
571 content.text = "Line removed\nThis is a sample text for testing diffs"
571 content.text = "Line removed\nThis is a sample text for testing diffs"
572 content.save!
572 content.save!
573 content.text = "This is a sample text for testing diffs\nLine added"
573 content.text = "This is a sample text for testing diffs\nLine added"
574 content.save!
574 content.save!
575 end
575 end
576
576
577 get :diff, :project_id => 1, :id => 'CookBook_documentation', :version => content.version, :version_from => (content.version - 1)
577 get :diff, :project_id => 1, :id => 'CookBook_documentation', :version => content.version, :version_from => (content.version - 1)
578 assert_response :success
578 assert_response :success
579 assert_template 'diff'
579 assert_template 'diff'
580 assert_select 'span.diff_out', :text => 'Line removed'
580 assert_select 'span.diff_out', :text => 'Line removed'
581 assert_select 'span.diff_in', :text => 'Line added'
581 assert_select 'span.diff_in', :text => 'Line added'
582 end
582 end
583
583
584 def test_diff_with_invalid_version_should_respond_with_404
584 def test_diff_with_invalid_version_should_respond_with_404
585 get :diff, :project_id => 1, :id => 'CookBook_documentation', :version => '99'
585 get :diff, :project_id => 1, :id => 'CookBook_documentation', :version => '99'
586 assert_response 404
586 assert_response 404
587 end
587 end
588
588
589 def test_diff_with_invalid_version_from_should_respond_with_404
589 def test_diff_with_invalid_version_from_should_respond_with_404
590 get :diff, :project_id => 1, :id => 'CookBook_documentation', :version => '99', :version_from => '98'
590 get :diff, :project_id => 1, :id => 'CookBook_documentation', :version => '99', :version_from => '98'
591 assert_response 404
591 assert_response 404
592 end
592 end
593
593
594 def test_annotate
594 def test_annotate
595 get :annotate, :project_id => 1, :id => 'CookBook_documentation', :version => 2
595 get :annotate, :project_id => 1, :id => 'CookBook_documentation', :version => 2
596 assert_response :success
596 assert_response :success
597 assert_template 'annotate'
597 assert_template 'annotate'
598
598
599 # Line 1
599 # Line 1
600 assert_tag :tag => 'tr', :child => {
600 assert_tag :tag => 'tr', :child => {
601 :tag => 'th', :attributes => {:class => 'line-num'}, :content => '1', :sibling => {
601 :tag => 'th', :attributes => {:class => 'line-num'}, :content => '1', :sibling => {
602 :tag => 'td', :attributes => {:class => 'author'}, :content => /John Smith/, :sibling => {
602 :tag => 'td', :attributes => {:class => 'author'}, :content => /John Smith/, :sibling => {
603 :tag => 'td', :content => /h1\. CookBook documentation/
603 :tag => 'td', :content => /h1\. CookBook documentation/
604 }
604 }
605 }
605 }
606 }
606 }
607
607
608 # Line 5
608 # Line 5
609 assert_tag :tag => 'tr', :child => {
609 assert_tag :tag => 'tr', :child => {
610 :tag => 'th', :attributes => {:class => 'line-num'}, :content => '5', :sibling => {
610 :tag => 'th', :attributes => {:class => 'line-num'}, :content => '5', :sibling => {
611 :tag => 'td', :attributes => {:class => 'author'}, :content => /Redmine Admin/, :sibling => {
611 :tag => 'td', :attributes => {:class => 'author'}, :content => /Redmine Admin/, :sibling => {
612 :tag => 'td', :content => /Some updated \[\[documentation\]\] here/
612 :tag => 'td', :content => /Some updated \[\[documentation\]\] here/
613 }
613 }
614 }
614 }
615 }
615 }
616 end
616 end
617
617
618 def test_annotate_with_invalid_version_should_respond_with_404
618 def test_annotate_with_invalid_version_should_respond_with_404
619 get :annotate, :project_id => 1, :id => 'CookBook_documentation', :version => '99'
619 get :annotate, :project_id => 1, :id => 'CookBook_documentation', :version => '99'
620 assert_response 404
620 assert_response 404
621 end
621 end
622
622
623 def test_get_rename
623 def test_get_rename
624 @request.session[:user_id] = 2
624 @request.session[:user_id] = 2
625 get :rename, :project_id => 1, :id => 'Another_page'
625 get :rename, :project_id => 1, :id => 'Another_page'
626 assert_response :success
626 assert_response :success
627 assert_template 'rename'
627 assert_template 'rename'
628 assert_tag 'option',
628 assert_tag 'option',
629 :attributes => {:value => ''},
629 :attributes => {:value => ''},
630 :content => '',
630 :content => '',
631 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
631 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
632 assert_no_tag 'option',
632 assert_no_tag 'option',
633 :attributes => {:selected => 'selected'},
633 :attributes => {:selected => 'selected'},
634 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
634 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
635 end
635 end
636
636
637 def test_get_rename_child_page
637 def test_get_rename_child_page
638 @request.session[:user_id] = 2
638 @request.session[:user_id] = 2
639 get :rename, :project_id => 1, :id => 'Child_1'
639 get :rename, :project_id => 1, :id => 'Child_1'
640 assert_response :success
640 assert_response :success
641 assert_template 'rename'
641 assert_template 'rename'
642 assert_tag 'option',
642 assert_tag 'option',
643 :attributes => {:value => ''},
643 :attributes => {:value => ''},
644 :content => '',
644 :content => '',
645 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
645 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
646 assert_tag 'option',
646 assert_tag 'option',
647 :attributes => {:value => '2', :selected => 'selected'},
647 :attributes => {:value => '2', :selected => 'selected'},
648 :content => /Another page/,
648 :content => /Another page/,
649 :parent => {
649 :parent => {
650 :tag => 'select',
650 :tag => 'select',
651 :attributes => {:name => 'wiki_page[parent_id]'}
651 :attributes => {:name => 'wiki_page[parent_id]'}
652 }
652 }
653 end
653 end
654
654
655 def test_rename_with_redirect
655 def test_rename_with_redirect
656 @request.session[:user_id] = 2
656 @request.session[:user_id] = 2
657 post :rename, :project_id => 1, :id => 'Another_page',
657 post :rename, :project_id => 1, :id => 'Another_page',
658 :wiki_page => { :title => 'Another renamed page',
658 :wiki_page => { :title => 'Another renamed page',
659 :redirect_existing_links => 1 }
659 :redirect_existing_links => 1 }
660 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
660 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
661 wiki = Project.find(1).wiki
661 wiki = Project.find(1).wiki
662 # Check redirects
662 # Check redirects
663 assert_not_nil wiki.find_page('Another page')
663 assert_not_nil wiki.find_page('Another page')
664 assert_nil wiki.find_page('Another page', :with_redirect => false)
664 assert_nil wiki.find_page('Another page', :with_redirect => false)
665 end
665 end
666
666
667 def test_rename_without_redirect
667 def test_rename_without_redirect
668 @request.session[:user_id] = 2
668 @request.session[:user_id] = 2
669 post :rename, :project_id => 1, :id => 'Another_page',
669 post :rename, :project_id => 1, :id => 'Another_page',
670 :wiki_page => { :title => 'Another renamed page',
670 :wiki_page => { :title => 'Another renamed page',
671 :redirect_existing_links => "0" }
671 :redirect_existing_links => "0" }
672 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
672 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
673 wiki = Project.find(1).wiki
673 wiki = Project.find(1).wiki
674 # Check that there's no redirects
674 # Check that there's no redirects
675 assert_nil wiki.find_page('Another page')
675 assert_nil wiki.find_page('Another page')
676 end
676 end
677
677
678 def test_rename_with_parent_assignment
678 def test_rename_with_parent_assignment
679 @request.session[:user_id] = 2
679 @request.session[:user_id] = 2
680 post :rename, :project_id => 1, :id => 'Another_page',
680 post :rename, :project_id => 1, :id => 'Another_page',
681 :wiki_page => { :title => 'Another page', :redirect_existing_links => "0", :parent_id => '4' }
681 :wiki_page => { :title => 'Another page', :redirect_existing_links => "0", :parent_id => '4' }
682 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
682 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
683 assert_equal WikiPage.find(4), WikiPage.find_by_title('Another_page').parent
683 assert_equal WikiPage.find(4), WikiPage.find_by_title('Another_page').parent
684 end
684 end
685
685
686 def test_rename_with_parent_unassignment
686 def test_rename_with_parent_unassignment
687 @request.session[:user_id] = 2
687 @request.session[:user_id] = 2
688 post :rename, :project_id => 1, :id => 'Child_1',
688 post :rename, :project_id => 1, :id => 'Child_1',
689 :wiki_page => { :title => 'Child 1', :redirect_existing_links => "0", :parent_id => '' }
689 :wiki_page => { :title => 'Child 1', :redirect_existing_links => "0", :parent_id => '' }
690 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Child_1'
690 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Child_1'
691 assert_nil WikiPage.find_by_title('Child_1').parent
691 assert_nil WikiPage.find_by_title('Child_1').parent
692 end
692 end
693
693
694 def test_destroy_a_page_without_children_should_not_ask_confirmation
694 def test_destroy_a_page_without_children_should_not_ask_confirmation
695 @request.session[:user_id] = 2
695 @request.session[:user_id] = 2
696 delete :destroy, :project_id => 1, :id => 'Child_2'
696 delete :destroy, :project_id => 1, :id => 'Child_2'
697 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
697 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
698 end
698 end
699
699
700 def test_destroy_parent_should_ask_confirmation
700 def test_destroy_parent_should_ask_confirmation
701 @request.session[:user_id] = 2
701 @request.session[:user_id] = 2
702 assert_no_difference('WikiPage.count') do
702 assert_no_difference('WikiPage.count') do
703 delete :destroy, :project_id => 1, :id => 'Another_page'
703 delete :destroy, :project_id => 1, :id => 'Another_page'
704 end
704 end
705 assert_response :success
705 assert_response :success
706 assert_template 'destroy'
706 assert_template 'destroy'
707 assert_select 'form' do
707 assert_select 'form' do
708 assert_select 'input[name=todo][value=nullify]'
708 assert_select 'input[name=todo][value=nullify]'
709 assert_select 'input[name=todo][value=destroy]'
709 assert_select 'input[name=todo][value=destroy]'
710 assert_select 'input[name=todo][value=reassign]'
710 assert_select 'input[name=todo][value=reassign]'
711 end
711 end
712 end
712 end
713
713
714 def test_destroy_parent_with_nullify_should_delete_parent_only
714 def test_destroy_parent_with_nullify_should_delete_parent_only
715 @request.session[:user_id] = 2
715 @request.session[:user_id] = 2
716 assert_difference('WikiPage.count', -1) do
716 assert_difference('WikiPage.count', -1) do
717 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'nullify'
717 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'nullify'
718 end
718 end
719 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
719 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
720 assert_nil WikiPage.find_by_id(2)
720 assert_nil WikiPage.find_by_id(2)
721 end
721 end
722
722
723 def test_destroy_parent_with_cascade_should_delete_descendants
723 def test_destroy_parent_with_cascade_should_delete_descendants
724 @request.session[:user_id] = 2
724 @request.session[:user_id] = 2
725 assert_difference('WikiPage.count', -4) do
725 assert_difference('WikiPage.count', -4) do
726 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'destroy'
726 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'destroy'
727 end
727 end
728 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
728 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
729 assert_nil WikiPage.find_by_id(2)
729 assert_nil WikiPage.find_by_id(2)
730 assert_nil WikiPage.find_by_id(5)
730 assert_nil WikiPage.find_by_id(5)
731 end
731 end
732
732
733 def test_destroy_parent_with_reassign
733 def test_destroy_parent_with_reassign
734 @request.session[:user_id] = 2
734 @request.session[:user_id] = 2
735 assert_difference('WikiPage.count', -1) do
735 assert_difference('WikiPage.count', -1) do
736 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'reassign', :reassign_to_id => 1
736 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'reassign', :reassign_to_id => 1
737 end
737 end
738 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
738 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
739 assert_nil WikiPage.find_by_id(2)
739 assert_nil WikiPage.find_by_id(2)
740 assert_equal WikiPage.find(1), WikiPage.find_by_id(5).parent
740 assert_equal WikiPage.find(1), WikiPage.find_by_id(5).parent
741 end
741 end
742
742
743 def test_destroy_version
743 def test_destroy_version
744 @request.session[:user_id] = 2
744 @request.session[:user_id] = 2
745 assert_difference 'WikiContent::Version.count', -1 do
745 assert_difference 'WikiContent::Version.count', -1 do
746 assert_no_difference 'WikiContent.count' do
746 assert_no_difference 'WikiContent.count' do
747 assert_no_difference 'WikiPage.count' do
747 assert_no_difference 'WikiPage.count' do
748 delete :destroy_version, :project_id => 'ecookbook', :id => 'CookBook_documentation', :version => 2
748 delete :destroy_version, :project_id => 'ecookbook', :id => 'CookBook_documentation', :version => 2
749 assert_redirected_to '/projects/ecookbook/wiki/CookBook_documentation/history'
749 assert_redirected_to '/projects/ecookbook/wiki/CookBook_documentation/history'
750 end
750 end
751 end
751 end
752 end
752 end
753 end
753 end
754
754
755 def test_index
755 def test_index
756 get :index, :project_id => 'ecookbook'
756 get :index, :project_id => 'ecookbook'
757 assert_response :success
757 assert_response :success
758 assert_template 'index'
758 assert_template 'index'
759 pages = assigns(:pages)
759 pages = assigns(:pages)
760 assert_not_nil pages
760 assert_not_nil pages
761 assert_equal Project.find(1).wiki.pages.size, pages.size
761 assert_equal Project.find(1).wiki.pages.size, pages.size
762 assert_equal pages.first.content.updated_on, pages.first.updated_on
762 assert_equal pages.first.content.updated_on, pages.first.updated_on
763
763
764 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
764 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
765 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/CookBook_documentation' },
765 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/CookBook_documentation' },
766 :content => 'CookBook documentation' },
766 :content => 'CookBook documentation' },
767 :child => { :tag => 'ul',
767 :child => { :tag => 'ul',
768 :child => { :tag => 'li',
768 :child => { :tag => 'li',
769 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
769 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
770 :content => 'Page with an inline image' } } } },
770 :content => 'Page with an inline image' } } } },
771 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Another_page' },
771 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Another_page' },
772 :content => 'Another page' } }
772 :content => 'Another page' } }
773 end
773 end
774
774
775 def test_index_should_include_atom_link
775 def test_index_should_include_atom_link
776 get :index, :project_id => 'ecookbook'
776 get :index, :project_id => 'ecookbook'
777 assert_tag 'a', :attributes => { :href => '/projects/ecookbook/activity.atom?show_wiki_edits=1'}
777 assert_tag 'a', :attributes => { :href => '/projects/ecookbook/activity.atom?show_wiki_edits=1'}
778 end
778 end
779
779
780 def test_export_to_html
780 def test_export_to_html
781 @request.session[:user_id] = 2
781 @request.session[:user_id] = 2
782 get :export, :project_id => 'ecookbook'
782 get :export, :project_id => 'ecookbook'
783
783
784 assert_response :success
784 assert_response :success
785 assert_not_nil assigns(:pages)
785 assert_not_nil assigns(:pages)
786 assert assigns(:pages).any?
786 assert assigns(:pages).any?
787 assert_equal "text/html", @response.content_type
787 assert_equal "text/html", @response.content_type
788
788
789 assert_select "a[name=?]", "CookBook_documentation"
789 assert_select "a[name=?]", "CookBook_documentation"
790 assert_select "a[name=?]", "Another_page"
790 assert_select "a[name=?]", "Another_page"
791 assert_select "a[name=?]", "Page_with_an_inline_image"
791 assert_select "a[name=?]", "Page_with_an_inline_image"
792 end
792 end
793
793
794 def test_export_to_pdf
794 def test_export_to_pdf
795 @request.session[:user_id] = 2
795 @request.session[:user_id] = 2
796 get :export, :project_id => 'ecookbook', :format => 'pdf'
796 get :export, :project_id => 'ecookbook', :format => 'pdf'
797
797
798 assert_response :success
798 assert_response :success
799 assert_not_nil assigns(:pages)
799 assert_not_nil assigns(:pages)
800 assert assigns(:pages).any?
800 assert assigns(:pages).any?
801 assert_equal 'application/pdf', @response.content_type
801 assert_equal 'application/pdf', @response.content_type
802 assert_equal 'attachment; filename="ecookbook.pdf"', @response.headers['Content-Disposition']
802 assert_equal 'attachment; filename="ecookbook.pdf"', @response.headers['Content-Disposition']
803 assert @response.body.starts_with?('%PDF')
803 assert @response.body.starts_with?('%PDF')
804 end
804 end
805
805
806 def test_export_without_permission_should_be_denied
806 def test_export_without_permission_should_be_denied
807 @request.session[:user_id] = 2
807 @request.session[:user_id] = 2
808 Role.find_by_name('Manager').remove_permission! :export_wiki_pages
808 Role.find_by_name('Manager').remove_permission! :export_wiki_pages
809 get :export, :project_id => 'ecookbook'
809 get :export, :project_id => 'ecookbook'
810
810
811 assert_response 403
811 assert_response 403
812 end
812 end
813
813
814 def test_date_index
814 def test_date_index
815 get :date_index, :project_id => 'ecookbook'
815 get :date_index, :project_id => 'ecookbook'
816
816
817 assert_response :success
817 assert_response :success
818 assert_template 'date_index'
818 assert_template 'date_index'
819 assert_not_nil assigns(:pages)
819 assert_not_nil assigns(:pages)
820 assert_not_nil assigns(:pages_by_date)
820 assert_not_nil assigns(:pages_by_date)
821
821
822 assert_tag 'a', :attributes => { :href => '/projects/ecookbook/activity.atom?show_wiki_edits=1'}
822 assert_tag 'a', :attributes => { :href => '/projects/ecookbook/activity.atom?show_wiki_edits=1'}
823 end
823 end
824
824
825 def test_not_found
825 def test_not_found
826 get :show, :project_id => 999
826 get :show, :project_id => 999
827 assert_response 404
827 assert_response 404
828 end
828 end
829
829
830 def test_protect_page
830 def test_protect_page
831 page = WikiPage.find_by_wiki_id_and_title(1, 'Another_page')
831 page = WikiPage.find_by_wiki_id_and_title(1, 'Another_page')
832 assert !page.protected?
832 assert !page.protected?
833 @request.session[:user_id] = 2
833 @request.session[:user_id] = 2
834 post :protect, :project_id => 1, :id => page.title, :protected => '1'
834 post :protect, :project_id => 1, :id => page.title, :protected => '1'
835 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
835 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
836 assert page.reload.protected?
836 assert page.reload.protected?
837 end
837 end
838
838
839 def test_unprotect_page
839 def test_unprotect_page
840 page = WikiPage.find_by_wiki_id_and_title(1, 'CookBook_documentation')
840 page = WikiPage.find_by_wiki_id_and_title(1, 'CookBook_documentation')
841 assert page.protected?
841 assert page.protected?
842 @request.session[:user_id] = 2
842 @request.session[:user_id] = 2
843 post :protect, :project_id => 1, :id => page.title, :protected => '0'
843 post :protect, :project_id => 1, :id => page.title, :protected => '0'
844 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'CookBook_documentation'
844 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'CookBook_documentation'
845 assert !page.reload.protected?
845 assert !page.reload.protected?
846 end
846 end
847
847
848 def test_show_page_with_edit_link
848 def test_show_page_with_edit_link
849 @request.session[:user_id] = 2
849 @request.session[:user_id] = 2
850 get :show, :project_id => 1
850 get :show, :project_id => 1
851 assert_response :success
851 assert_response :success
852 assert_template 'show'
852 assert_template 'show'
853 assert_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
853 assert_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
854 end
854 end
855
855
856 def test_show_page_without_edit_link
856 def test_show_page_without_edit_link
857 @request.session[:user_id] = 4
857 @request.session[:user_id] = 4
858 get :show, :project_id => 1
858 get :show, :project_id => 1
859 assert_response :success
859 assert_response :success
860 assert_template 'show'
860 assert_template 'show'
861 assert_no_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
861 assert_no_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
862 end
862 end
863
863
864 def test_show_pdf
864 def test_show_pdf
865 @request.session[:user_id] = 2
865 @request.session[:user_id] = 2
866 get :show, :project_id => 1, :format => 'pdf'
866 get :show, :project_id => 1, :format => 'pdf'
867 assert_response :success
867 assert_response :success
868 assert_not_nil assigns(:page)
868 assert_not_nil assigns(:page)
869 assert_equal 'application/pdf', @response.content_type
869 assert_equal 'application/pdf', @response.content_type
870 assert_equal 'attachment; filename="CookBook_documentation.pdf"',
870 assert_equal 'attachment; filename="CookBook_documentation.pdf"',
871 @response.headers['Content-Disposition']
871 @response.headers['Content-Disposition']
872 end
872 end
873
873
874 def test_show_html
874 def test_show_html
875 @request.session[:user_id] = 2
875 @request.session[:user_id] = 2
876 get :show, :project_id => 1, :format => 'html'
876 get :show, :project_id => 1, :format => 'html'
877 assert_response :success
877 assert_response :success
878 assert_not_nil assigns(:page)
878 assert_not_nil assigns(:page)
879 assert_equal 'text/html', @response.content_type
879 assert_equal 'text/html', @response.content_type
880 assert_equal 'attachment; filename="CookBook_documentation.html"',
880 assert_equal 'attachment; filename="CookBook_documentation.html"',
881 @response.headers['Content-Disposition']
881 @response.headers['Content-Disposition']
882 assert_tag 'h1', :content => 'CookBook documentation'
882 assert_tag 'h1', :content => 'CookBook documentation'
883 end
883 end
884
884
885 def test_show_versioned_html
885 def test_show_versioned_html
886 @request.session[:user_id] = 2
886 @request.session[:user_id] = 2
887 get :show, :project_id => 1, :format => 'html', :version => 2
887 get :show, :project_id => 1, :format => 'html', :version => 2
888 assert_response :success
888 assert_response :success
889 assert_not_nil assigns(:content)
889 assert_not_nil assigns(:content)
890 assert_equal 2, assigns(:content).version
890 assert_equal 2, assigns(:content).version
891 assert_equal 'text/html', @response.content_type
891 assert_equal 'text/html', @response.content_type
892 assert_equal 'attachment; filename="CookBook_documentation.html"',
892 assert_equal 'attachment; filename="CookBook_documentation.html"',
893 @response.headers['Content-Disposition']
893 @response.headers['Content-Disposition']
894 assert_tag 'h1', :content => 'CookBook documentation'
894 assert_tag 'h1', :content => 'CookBook documentation'
895 end
895 end
896
896
897 def test_show_txt
897 def test_show_txt
898 @request.session[:user_id] = 2
898 @request.session[:user_id] = 2
899 get :show, :project_id => 1, :format => 'txt'
899 get :show, :project_id => 1, :format => 'txt'
900 assert_response :success
900 assert_response :success
901 assert_not_nil assigns(:page)
901 assert_not_nil assigns(:page)
902 assert_equal 'text/plain', @response.content_type
902 assert_equal 'text/plain', @response.content_type
903 assert_equal 'attachment; filename="CookBook_documentation.txt"',
903 assert_equal 'attachment; filename="CookBook_documentation.txt"',
904 @response.headers['Content-Disposition']
904 @response.headers['Content-Disposition']
905 assert_include 'h1. CookBook documentation', @response.body
905 assert_include 'h1. CookBook documentation', @response.body
906 end
906 end
907
907
908 def test_show_versioned_txt
908 def test_show_versioned_txt
909 @request.session[:user_id] = 2
909 @request.session[:user_id] = 2
910 get :show, :project_id => 1, :format => 'txt', :version => 2
910 get :show, :project_id => 1, :format => 'txt', :version => 2
911 assert_response :success
911 assert_response :success
912 assert_not_nil assigns(:content)
912 assert_not_nil assigns(:content)
913 assert_equal 2, assigns(:content).version
913 assert_equal 2, assigns(:content).version
914 assert_equal 'text/plain', @response.content_type
914 assert_equal 'text/plain', @response.content_type
915 assert_equal 'attachment; filename="CookBook_documentation.txt"',
915 assert_equal 'attachment; filename="CookBook_documentation.txt"',
916 @response.headers['Content-Disposition']
916 @response.headers['Content-Disposition']
917 assert_include 'h1. CookBook documentation', @response.body
917 assert_include 'h1. CookBook documentation', @response.body
918 end
918 end
919
919
920 def test_edit_unprotected_page
920 def test_edit_unprotected_page
921 # Non members can edit unprotected wiki pages
921 # Non members can edit unprotected wiki pages
922 @request.session[:user_id] = 4
922 @request.session[:user_id] = 4
923 get :edit, :project_id => 1, :id => 'Another_page'
923 get :edit, :project_id => 1, :id => 'Another_page'
924 assert_response :success
924 assert_response :success
925 assert_template 'edit'
925 assert_template 'edit'
926 end
926 end
927
927
928 def test_edit_protected_page_by_nonmember
928 def test_edit_protected_page_by_nonmember
929 # Non members can't edit protected wiki pages
929 # Non members can't edit protected wiki pages
930 @request.session[:user_id] = 4
930 @request.session[:user_id] = 4
931 get :edit, :project_id => 1, :id => 'CookBook_documentation'
931 get :edit, :project_id => 1, :id => 'CookBook_documentation'
932 assert_response 403
932 assert_response 403
933 end
933 end
934
934
935 def test_edit_protected_page_by_member
935 def test_edit_protected_page_by_member
936 @request.session[:user_id] = 2
936 @request.session[:user_id] = 2
937 get :edit, :project_id => 1, :id => 'CookBook_documentation'
937 get :edit, :project_id => 1, :id => 'CookBook_documentation'
938 assert_response :success
938 assert_response :success
939 assert_template 'edit'
939 assert_template 'edit'
940 end
940 end
941
941
942 def test_history_of_non_existing_page_should_return_404
942 def test_history_of_non_existing_page_should_return_404
943 get :history, :project_id => 1, :id => 'Unknown_page'
943 get :history, :project_id => 1, :id => 'Unknown_page'
944 assert_response 404
944 assert_response 404
945 end
945 end
946
946
947 def test_add_attachment
947 def test_add_attachment
948 @request.session[:user_id] = 2
948 @request.session[:user_id] = 2
949 assert_difference 'Attachment.count' do
949 assert_difference 'Attachment.count' do
950 post :add_attachment, :project_id => 1, :id => 'CookBook_documentation',
950 post :add_attachment, :project_id => 1, :id => 'CookBook_documentation',
951 :attachments => {
951 :attachments => {
952 '1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'),
952 '1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'),
953 'description' => 'test file'}
953 'description' => 'test file'}
954 }
954 }
955 end
955 end
956 attachment = Attachment.first(:order => 'id DESC')
956 attachment = Attachment.order('id DESC').first
957 assert_equal Wiki.find(1).find_page('CookBook_documentation'), attachment.container
957 assert_equal Wiki.find(1).find_page('CookBook_documentation'), attachment.container
958 end
958 end
959 end
959 end
General Comments 0
You need to be logged in to leave comments. Login now