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