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