@@ -1,359 +1,363 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require 'diff' |
|
19 | 19 | |
|
20 | 20 | # The WikiController follows the Rails REST controller pattern but with |
|
21 | 21 | # a few differences |
|
22 | 22 | # |
|
23 | 23 | # * index - shows a list of WikiPages grouped by page or date |
|
24 | 24 | # * new - not used |
|
25 | 25 | # * create - not used |
|
26 | 26 | # * show - will also show the form for creating a new wiki page |
|
27 | 27 | # * edit - used to edit an existing or new page |
|
28 | 28 | # * update - used to save a wiki page update to the database, including new pages |
|
29 | 29 | # * destroy - normal |
|
30 | 30 | # |
|
31 | 31 | # Other member and collection methods are also used |
|
32 | 32 | # |
|
33 | 33 | # TODO: still being worked on |
|
34 | 34 | class WikiController < ApplicationController |
|
35 | 35 | default_search_scope :wiki_pages |
|
36 | 36 | before_filter :find_wiki, :authorize |
|
37 | 37 | before_filter :find_existing_or_new_page, :only => [:show, :edit, :update] |
|
38 | 38 | before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy, :destroy_version] |
|
39 | accept_api_auth :index, :show, :update | |
|
39 | accept_api_auth :index, :show, :update, :destroy | |
|
40 | 40 | |
|
41 | 41 | helper :attachments |
|
42 | 42 | include AttachmentsHelper |
|
43 | 43 | helper :watchers |
|
44 | 44 | include Redmine::Export::PDF |
|
45 | 45 | |
|
46 | 46 | # List of pages, sorted alphabetically and by parent (hierarchy) |
|
47 | 47 | def index |
|
48 | 48 | load_pages_for_index |
|
49 | 49 | |
|
50 | 50 | respond_to do |format| |
|
51 | 51 | format.html { |
|
52 | 52 | @pages_by_parent_id = @pages.group_by(&:parent_id) |
|
53 | 53 | } |
|
54 | 54 | format.api |
|
55 | 55 | end |
|
56 | 56 | end |
|
57 | 57 | |
|
58 | 58 | # List of page, by last update |
|
59 | 59 | def date_index |
|
60 | 60 | load_pages_for_index |
|
61 | 61 | @pages_by_date = @pages.group_by {|p| p.updated_on.to_date} |
|
62 | 62 | end |
|
63 | 63 | |
|
64 | 64 | # display a page (in editing mode if it doesn't exist) |
|
65 | 65 | def show |
|
66 | 66 | if @page.new_record? |
|
67 | 67 | if User.current.allowed_to?(:edit_wiki_pages, @project) && editable? && !api_request? |
|
68 | 68 | edit |
|
69 | 69 | render :action => 'edit' |
|
70 | 70 | else |
|
71 | 71 | render_404 |
|
72 | 72 | end |
|
73 | 73 | return |
|
74 | 74 | end |
|
75 | 75 | if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project) |
|
76 | 76 | deny_access |
|
77 | 77 | return |
|
78 | 78 | end |
|
79 | 79 | @content = @page.content_for_version(params[:version]) |
|
80 | 80 | if User.current.allowed_to?(:export_wiki_pages, @project) |
|
81 | 81 | if params[:format] == 'pdf' |
|
82 | 82 | send_data(wiki_page_to_pdf(@page, @project), :type => 'application/pdf', :filename => "#{@page.title}.pdf") |
|
83 | 83 | return |
|
84 | 84 | elsif params[:format] == 'html' |
|
85 | 85 | export = render_to_string :action => 'export', :layout => false |
|
86 | 86 | send_data(export, :type => 'text/html', :filename => "#{@page.title}.html") |
|
87 | 87 | return |
|
88 | 88 | elsif params[:format] == 'txt' |
|
89 | 89 | send_data(@content.text, :type => 'text/plain', :filename => "#{@page.title}.txt") |
|
90 | 90 | return |
|
91 | 91 | end |
|
92 | 92 | end |
|
93 | 93 | @editable = editable? |
|
94 | 94 | @sections_editable = @editable && User.current.allowed_to?(:edit_wiki_pages, @page.project) && |
|
95 | 95 | @content.current_version? && |
|
96 | 96 | Redmine::WikiFormatting.supports_section_edit? |
|
97 | 97 | |
|
98 | 98 | respond_to do |format| |
|
99 | 99 | format.html |
|
100 | 100 | format.api |
|
101 | 101 | end |
|
102 | 102 | end |
|
103 | 103 | |
|
104 | 104 | # edit an existing page or a new one |
|
105 | 105 | def edit |
|
106 | 106 | return render_403 unless editable? |
|
107 | 107 | if @page.new_record? |
|
108 | 108 | @page.content = WikiContent.new(:page => @page) |
|
109 | 109 | if params[:parent].present? |
|
110 | 110 | @page.parent = @page.wiki.find_page(params[:parent].to_s) |
|
111 | 111 | end |
|
112 | 112 | end |
|
113 | 113 | |
|
114 | 114 | @content = @page.content_for_version(params[:version]) |
|
115 | 115 | @content.text = initial_page_content(@page) if @content.text.blank? |
|
116 | 116 | # don't keep previous comment |
|
117 | 117 | @content.comments = nil |
|
118 | 118 | |
|
119 | 119 | # To prevent StaleObjectError exception when reverting to a previous version |
|
120 | 120 | @content.version = @page.content.version |
|
121 | 121 | |
|
122 | 122 | @text = @content.text |
|
123 | 123 | if params[:section].present? && Redmine::WikiFormatting.supports_section_edit? |
|
124 | 124 | @section = params[:section].to_i |
|
125 | 125 | @text, @section_hash = Redmine::WikiFormatting.formatter.new(@text).get_section(@section) |
|
126 | 126 | render_404 if @text.blank? |
|
127 | 127 | end |
|
128 | 128 | end |
|
129 | 129 | |
|
130 | 130 | # Creates a new page or updates an existing one |
|
131 | 131 | def update |
|
132 | 132 | return render_403 unless editable? |
|
133 | 133 | was_new_page = @page.new_record? |
|
134 | 134 | @page.content = WikiContent.new(:page => @page) if @page.new_record? |
|
135 | 135 | @page.safe_attributes = params[:wiki_page] |
|
136 | 136 | |
|
137 | 137 | @content = @page.content |
|
138 | 138 | content_params = params[:content] |
|
139 | 139 | if content_params.nil? && params[:wiki_page].is_a?(Hash) |
|
140 | 140 | content_params = params[:wiki_page].slice(:text, :comments, :version) |
|
141 | 141 | end |
|
142 | 142 | content_params ||= {} |
|
143 | 143 | |
|
144 | 144 | if !@page.new_record? && content_params.present? && @content.text == content_params[:text] |
|
145 | 145 | attachments = Attachment.attach_files(@page, params[:attachments]) |
|
146 | 146 | render_attachment_warning_if_needed(@page) |
|
147 | 147 | # don't save content if text wasn't changed |
|
148 | 148 | @page.save |
|
149 | 149 | redirect_to :action => 'show', :project_id => @project, :id => @page.title |
|
150 | 150 | return |
|
151 | 151 | end |
|
152 | 152 | |
|
153 | 153 | @content.comments = content_params[:comments] |
|
154 | 154 | @text = content_params[:text] |
|
155 | 155 | if params[:section].present? && Redmine::WikiFormatting.supports_section_edit? |
|
156 | 156 | @section = params[:section].to_i |
|
157 | 157 | @section_hash = params[:section_hash] |
|
158 | 158 | @content.text = Redmine::WikiFormatting.formatter.new(@content.text).update_section(params[:section].to_i, @text, @section_hash) |
|
159 | 159 | else |
|
160 | 160 | @content.version = content_params[:version] if content_params[:version] |
|
161 | 161 | @content.text = @text |
|
162 | 162 | end |
|
163 | 163 | @content.author = User.current |
|
164 | 164 | @page.content = @content |
|
165 | 165 | if @page.save |
|
166 | 166 | attachments = Attachment.attach_files(@page, params[:attachments]) |
|
167 | 167 | render_attachment_warning_if_needed(@page) |
|
168 | 168 | call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page}) |
|
169 | 169 | |
|
170 | 170 | respond_to do |format| |
|
171 | 171 | format.html { redirect_to :action => 'show', :project_id => @project, :id => @page.title } |
|
172 | 172 | format.api { |
|
173 | 173 | if was_new_page |
|
174 | 174 | render :action => 'show', :status => :created, :location => url_for(:controller => 'wiki', :action => 'show', :project_id => @project, :id => @page.title) |
|
175 | 175 | else |
|
176 | 176 | render_api_ok |
|
177 | 177 | end |
|
178 | 178 | } |
|
179 | 179 | end |
|
180 | 180 | else |
|
181 | 181 | respond_to do |format| |
|
182 | 182 | format.html { render :action => 'edit' } |
|
183 | 183 | format.api { render_validation_errors(@content) } |
|
184 | 184 | end |
|
185 | 185 | end |
|
186 | 186 | |
|
187 | 187 | rescue ActiveRecord::StaleObjectError, Redmine::WikiFormatting::StaleSectionError |
|
188 | 188 | # Optimistic locking exception |
|
189 | 189 | respond_to do |format| |
|
190 | 190 | format.html { |
|
191 | 191 | flash.now[:error] = l(:notice_locking_conflict) |
|
192 | 192 | render :action => 'edit' |
|
193 | 193 | } |
|
194 | 194 | format.api { render_api_head :conflict } |
|
195 | 195 | end |
|
196 | 196 | rescue ActiveRecord::RecordNotSaved |
|
197 | 197 | respond_to do |format| |
|
198 | 198 | format.html { render :action => 'edit' } |
|
199 | 199 | format.api { render_validation_errors(@content) } |
|
200 | 200 | end |
|
201 | 201 | end |
|
202 | 202 | |
|
203 | 203 | # rename a page |
|
204 | 204 | def rename |
|
205 | 205 | return render_403 unless editable? |
|
206 | 206 | @page.redirect_existing_links = true |
|
207 | 207 | # used to display the *original* title if some AR validation errors occur |
|
208 | 208 | @original_title = @page.pretty_title |
|
209 | 209 | if request.post? && @page.update_attributes(params[:wiki_page]) |
|
210 | 210 | flash[:notice] = l(:notice_successful_update) |
|
211 | 211 | redirect_to :action => 'show', :project_id => @project, :id => @page.title |
|
212 | 212 | end |
|
213 | 213 | end |
|
214 | 214 | |
|
215 | 215 | def protect |
|
216 | 216 | @page.update_attribute :protected, params[:protected] |
|
217 | 217 | redirect_to :action => 'show', :project_id => @project, :id => @page.title |
|
218 | 218 | end |
|
219 | 219 | |
|
220 | 220 | # show page history |
|
221 | 221 | def history |
|
222 | 222 | @version_count = @page.content.versions.count |
|
223 | 223 | @version_pages = Paginator.new self, @version_count, per_page_option, params['p'] |
|
224 | 224 | # don't load text |
|
225 | 225 | @versions = @page.content.versions.find :all, |
|
226 | 226 | :select => "id, author_id, comments, updated_on, version", |
|
227 | 227 | :order => 'version DESC', |
|
228 | 228 | :limit => @version_pages.items_per_page + 1, |
|
229 | 229 | :offset => @version_pages.current.offset |
|
230 | 230 | |
|
231 | 231 | render :layout => false if request.xhr? |
|
232 | 232 | end |
|
233 | 233 | |
|
234 | 234 | def diff |
|
235 | 235 | @diff = @page.diff(params[:version], params[:version_from]) |
|
236 | 236 | render_404 unless @diff |
|
237 | 237 | end |
|
238 | 238 | |
|
239 | 239 | def annotate |
|
240 | 240 | @annotate = @page.annotate(params[:version]) |
|
241 | 241 | render_404 unless @annotate |
|
242 | 242 | end |
|
243 | 243 | |
|
244 | 244 | # Removes a wiki page and its history |
|
245 | 245 | # Children can be either set as root pages, removed or reassigned to another parent page |
|
246 | 246 | def destroy |
|
247 | 247 | return render_403 unless editable? |
|
248 | 248 | |
|
249 | 249 | @descendants_count = @page.descendants.size |
|
250 | 250 | if @descendants_count > 0 |
|
251 | 251 | case params[:todo] |
|
252 | 252 | when 'nullify' |
|
253 | 253 | # Nothing to do |
|
254 | 254 | when 'destroy' |
|
255 | 255 | # Removes all its descendants |
|
256 | 256 | @page.descendants.each(&:destroy) |
|
257 | 257 | when 'reassign' |
|
258 | 258 | # Reassign children to another parent page |
|
259 | 259 | reassign_to = @wiki.pages.find_by_id(params[:reassign_to_id].to_i) |
|
260 | 260 | return unless reassign_to |
|
261 | 261 | @page.children.each do |child| |
|
262 | 262 | child.update_attribute(:parent, reassign_to) |
|
263 | 263 | end |
|
264 | 264 | else |
|
265 | 265 | @reassignable_to = @wiki.pages - @page.self_and_descendants |
|
266 | return | |
|
266 | # display the destroy form if it's a user request | |
|
267 | return unless api_request? | |
|
267 | 268 | end |
|
268 | 269 | end |
|
269 | 270 | @page.destroy |
|
270 | redirect_to :action => 'index', :project_id => @project | |
|
271 | respond_to do |format| | |
|
272 | format.html { redirect_to :action => 'index', :project_id => @project } | |
|
273 | format.api { render_api_ok } | |
|
274 | end | |
|
271 | 275 | end |
|
272 | 276 | |
|
273 | 277 | def destroy_version |
|
274 | 278 | return render_403 unless editable? |
|
275 | 279 | |
|
276 | 280 | @content = @page.content_for_version(params[:version]) |
|
277 | 281 | @content.destroy |
|
278 | 282 | redirect_to_referer_or :action => 'history', :id => @page.title, :project_id => @project |
|
279 | 283 | end |
|
280 | 284 | |
|
281 | 285 | # Export wiki to a single pdf or html file |
|
282 | 286 | def export |
|
283 | 287 | @pages = @wiki.pages.all(:order => 'title', :include => [:content, :attachments], :limit => 75) |
|
284 | 288 | respond_to do |format| |
|
285 | 289 | format.html { |
|
286 | 290 | export = render_to_string :action => 'export_multiple', :layout => false |
|
287 | 291 | send_data(export, :type => 'text/html', :filename => "wiki.html") |
|
288 | 292 | } |
|
289 | 293 | format.pdf { |
|
290 | 294 | send_data(wiki_pages_to_pdf(@pages, @project), :type => 'application/pdf', :filename => "#{@project.identifier}.pdf") |
|
291 | 295 | } |
|
292 | 296 | end |
|
293 | 297 | end |
|
294 | 298 | |
|
295 | 299 | def preview |
|
296 | 300 | page = @wiki.find_page(params[:id]) |
|
297 | 301 | # page is nil when previewing a new page |
|
298 | 302 | return render_403 unless page.nil? || editable?(page) |
|
299 | 303 | if page |
|
300 | 304 | @attachements = page.attachments |
|
301 | 305 | @previewed = page.content |
|
302 | 306 | end |
|
303 | 307 | @text = params[:content][:text] |
|
304 | 308 | render :partial => 'common/preview' |
|
305 | 309 | end |
|
306 | 310 | |
|
307 | 311 | def add_attachment |
|
308 | 312 | return render_403 unless editable? |
|
309 | 313 | attachments = Attachment.attach_files(@page, params[:attachments]) |
|
310 | 314 | render_attachment_warning_if_needed(@page) |
|
311 | 315 | redirect_to :action => 'show', :id => @page.title, :project_id => @project |
|
312 | 316 | end |
|
313 | 317 | |
|
314 | 318 | private |
|
315 | 319 | |
|
316 | 320 | def find_wiki |
|
317 | 321 | @project = Project.find(params[:project_id]) |
|
318 | 322 | @wiki = @project.wiki |
|
319 | 323 | render_404 unless @wiki |
|
320 | 324 | rescue ActiveRecord::RecordNotFound |
|
321 | 325 | render_404 |
|
322 | 326 | end |
|
323 | 327 | |
|
324 | 328 | # Finds the requested page or a new page if it doesn't exist |
|
325 | 329 | def find_existing_or_new_page |
|
326 | 330 | @page = @wiki.find_or_new_page(params[:id]) |
|
327 | 331 | if @wiki.page_found_with_redirect? |
|
328 | 332 | redirect_to params.update(:id => @page.title) |
|
329 | 333 | end |
|
330 | 334 | end |
|
331 | 335 | |
|
332 | 336 | # Finds the requested page and returns a 404 error if it doesn't exist |
|
333 | 337 | def find_existing_page |
|
334 | 338 | @page = @wiki.find_page(params[:id]) |
|
335 | 339 | if @page.nil? |
|
336 | 340 | render_404 |
|
337 | 341 | return |
|
338 | 342 | end |
|
339 | 343 | if @wiki.page_found_with_redirect? |
|
340 | 344 | redirect_to params.update(:id => @page.title) |
|
341 | 345 | end |
|
342 | 346 | end |
|
343 | 347 | |
|
344 | 348 | # Returns true if the current user is allowed to edit the page, otherwise false |
|
345 | 349 | def editable?(page = @page) |
|
346 | 350 | page.editable_by?(User.current) |
|
347 | 351 | end |
|
348 | 352 | |
|
349 | 353 | # Returns the default content of a new wiki page |
|
350 | 354 | def initial_page_content(page) |
|
351 | 355 | helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting) |
|
352 | 356 | extend helper unless self.instance_of?(helper) |
|
353 | 357 | helper.instance_method(:initial_page_content).bind(self).call(page) |
|
354 | 358 | end |
|
355 | 359 | |
|
356 | 360 | def load_pages_for_index |
|
357 | 361 | @pages = @wiki.pages.with_updated_on.order("#{WikiPage.table_name}.title").includes(:wiki => :project).includes(:parent).all |
|
358 | 362 | end |
|
359 | 363 | end |
@@ -1,184 +1,193 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class ApiTest::WikiPagesTest < ActionController::IntegrationTest |
|
21 | 21 | fixtures :projects, :users, :roles, :members, :member_roles, |
|
22 | 22 | :enabled_modules, :wikis, :wiki_pages, :wiki_contents, |
|
23 | 23 | :wiki_content_versions, :attachments |
|
24 | 24 | |
|
25 | 25 | def setup |
|
26 | 26 | Setting.rest_api_enabled = '1' |
|
27 | 27 | end |
|
28 | 28 | |
|
29 | 29 | test "GET /projects/:project_id/wiki/index.xml should return wiki pages" do |
|
30 | 30 | get '/projects/ecookbook/wiki/index.xml' |
|
31 | 31 | assert_response 200 |
|
32 | 32 | assert_equal 'application/xml', response.content_type |
|
33 | 33 | assert_select 'wiki_pages[type=array]' do |
|
34 | 34 | assert_select 'wiki_page', :count => Wiki.find(1).pages.count |
|
35 | 35 | assert_select 'wiki_page' do |
|
36 | 36 | assert_select 'title', :text => 'CookBook_documentation' |
|
37 | 37 | assert_select 'version', :text => '3' |
|
38 | 38 | assert_select 'created_on' |
|
39 | 39 | assert_select 'updated_on' |
|
40 | 40 | end |
|
41 | 41 | assert_select 'wiki_page' do |
|
42 | 42 | assert_select 'title', :text => 'Page_with_an_inline_image' |
|
43 | 43 | assert_select 'parent[title=?]', 'CookBook_documentation' |
|
44 | 44 | end |
|
45 | 45 | end |
|
46 | 46 | end |
|
47 | 47 | |
|
48 | 48 | test "GET /projects/:project_id/wiki/:title.xml should return wiki page" do |
|
49 | 49 | get '/projects/ecookbook/wiki/CookBook_documentation.xml' |
|
50 | 50 | assert_response 200 |
|
51 | 51 | assert_equal 'application/xml', response.content_type |
|
52 | 52 | assert_select 'wiki_page' do |
|
53 | 53 | assert_select 'title', :text => 'CookBook_documentation' |
|
54 | 54 | assert_select 'version', :text => '3' |
|
55 | 55 | assert_select 'text' |
|
56 | 56 | assert_select 'author' |
|
57 | 57 | assert_select 'comments' |
|
58 | 58 | assert_select 'created_on' |
|
59 | 59 | assert_select 'updated_on' |
|
60 | 60 | end |
|
61 | 61 | end |
|
62 | 62 | |
|
63 | 63 | test "GET /projects/:project_id/wiki/:title.xml?include=attachments should include attachments" do |
|
64 | 64 | get '/projects/ecookbook/wiki/Page_with_an_inline_image.xml?include=attachments' |
|
65 | 65 | assert_response 200 |
|
66 | 66 | assert_equal 'application/xml', response.content_type |
|
67 | 67 | assert_select 'wiki_page' do |
|
68 | 68 | assert_select 'title', :text => 'Page_with_an_inline_image' |
|
69 | 69 | assert_select 'attachments[type=array]' do |
|
70 | 70 | assert_select 'attachment' do |
|
71 | 71 | assert_select 'id', :text => '3' |
|
72 | 72 | assert_select 'filename', :text => 'logo.gif' |
|
73 | 73 | end |
|
74 | 74 | end |
|
75 | 75 | end |
|
76 | 76 | end |
|
77 | 77 | |
|
78 | 78 | test "GET /projects/:project_id/wiki/:title.xml with unknown title and edit permission should respond with 404" do |
|
79 | 79 | get '/projects/ecookbook/wiki/Invalid_Page.xml', {}, credentials('jsmith') |
|
80 | 80 | assert_response 404 |
|
81 | 81 | assert_equal 'application/xml', response.content_type |
|
82 | 82 | end |
|
83 | 83 | |
|
84 | 84 | test "GET /projects/:project_id/wiki/:title/:version.xml should return wiki page version" do |
|
85 | 85 | get '/projects/ecookbook/wiki/CookBook_documentation/2.xml' |
|
86 | 86 | assert_response 200 |
|
87 | 87 | assert_equal 'application/xml', response.content_type |
|
88 | 88 | assert_select 'wiki_page' do |
|
89 | 89 | assert_select 'title', :text => 'CookBook_documentation' |
|
90 | 90 | assert_select 'version', :text => '2' |
|
91 | 91 | assert_select 'text' |
|
92 | 92 | assert_select 'author' |
|
93 | 93 | assert_select 'created_on' |
|
94 | 94 | assert_select 'updated_on' |
|
95 | 95 | end |
|
96 | 96 | end |
|
97 | 97 | |
|
98 | 98 | test "GET /projects/:project_id/wiki/:title/:version.xml without permission should be denied" do |
|
99 | 99 | Role.anonymous.remove_permission! :view_wiki_edits |
|
100 | 100 | |
|
101 | 101 | get '/projects/ecookbook/wiki/CookBook_documentation/2.xml' |
|
102 | 102 | assert_response 401 |
|
103 | 103 | assert_equal 'application/xml', response.content_type |
|
104 | 104 | end |
|
105 | 105 | |
|
106 | 106 | test "PUT /projects/:project_id/wiki/:title.xml should update wiki page" do |
|
107 | 107 | assert_no_difference 'WikiPage.count' do |
|
108 | 108 | assert_difference 'WikiContent::Version.count' do |
|
109 | 109 | put '/projects/ecookbook/wiki/CookBook_documentation.xml', |
|
110 | 110 | {:wiki_page => {:text => 'New content from API', :comments => 'API update'}}, |
|
111 | 111 | credentials('jsmith') |
|
112 | 112 | assert_response 200 |
|
113 | 113 | end |
|
114 | 114 | end |
|
115 | 115 | |
|
116 | 116 | page = WikiPage.find(1) |
|
117 | 117 | assert_equal 'New content from API', page.content.text |
|
118 | 118 | assert_equal 4, page.content.version |
|
119 | 119 | assert_equal 'API update', page.content.comments |
|
120 | 120 | assert_equal 'jsmith', page.content.author.login |
|
121 | 121 | end |
|
122 | 122 | |
|
123 | 123 | test "PUT /projects/:project_id/wiki/:title.xml with current versino should update wiki page" do |
|
124 | 124 | assert_no_difference 'WikiPage.count' do |
|
125 | 125 | assert_difference 'WikiContent::Version.count' do |
|
126 | 126 | put '/projects/ecookbook/wiki/CookBook_documentation.xml', |
|
127 | 127 | {:wiki_page => {:text => 'New content from API', :comments => 'API update', :version => '3'}}, |
|
128 | 128 | credentials('jsmith') |
|
129 | 129 | assert_response 200 |
|
130 | 130 | end |
|
131 | 131 | end |
|
132 | 132 | |
|
133 | 133 | page = WikiPage.find(1) |
|
134 | 134 | assert_equal 'New content from API', page.content.text |
|
135 | 135 | assert_equal 4, page.content.version |
|
136 | 136 | assert_equal 'API update', page.content.comments |
|
137 | 137 | assert_equal 'jsmith', page.content.author.login |
|
138 | 138 | end |
|
139 | 139 | |
|
140 | 140 | test "PUT /projects/:project_id/wiki/:title.xml with stale version should respond with 409" do |
|
141 | 141 | assert_no_difference 'WikiPage.count' do |
|
142 | 142 | assert_no_difference 'WikiContent::Version.count' do |
|
143 | 143 | put '/projects/ecookbook/wiki/CookBook_documentation.xml', |
|
144 | 144 | {:wiki_page => {:text => 'New content from API', :comments => 'API update', :version => '2'}}, |
|
145 | 145 | credentials('jsmith') |
|
146 | 146 | assert_response 409 |
|
147 | 147 | end |
|
148 | 148 | end |
|
149 | 149 | end |
|
150 | 150 | |
|
151 | 151 | test "PUT /projects/:project_id/wiki/:title.xml should create the page if it does not exist" do |
|
152 | 152 | assert_difference 'WikiPage.count' do |
|
153 | 153 | assert_difference 'WikiContent::Version.count' do |
|
154 | 154 | put '/projects/ecookbook/wiki/New_page_from_API.xml', |
|
155 | 155 | {:wiki_page => {:text => 'New content from API', :comments => 'API create'}}, |
|
156 | 156 | credentials('jsmith') |
|
157 | 157 | assert_response 201 |
|
158 | 158 | end |
|
159 | 159 | end |
|
160 | 160 | |
|
161 | 161 | page = WikiPage.order('id DESC').first |
|
162 | 162 | assert_equal 'New_page_from_API', page.title |
|
163 | 163 | assert_equal 'New content from API', page.content.text |
|
164 | 164 | assert_equal 1, page.content.version |
|
165 | 165 | assert_equal 'API create', page.content.comments |
|
166 | 166 | assert_equal 'jsmith', page.content.author.login |
|
167 | 167 | assert_nil page.parent |
|
168 | 168 | end |
|
169 | 169 | |
|
170 | 170 | test "PUT /projects/:project_id/wiki/:title.xml with parent" do |
|
171 | 171 | assert_difference 'WikiPage.count' do |
|
172 | 172 | assert_difference 'WikiContent::Version.count' do |
|
173 | 173 | put '/projects/ecookbook/wiki/New_subpage_from_API.xml', |
|
174 | 174 | {:wiki_page => {:parent_title => 'CookBook_documentation', :text => 'New content from API', :comments => 'API create'}}, |
|
175 | 175 | credentials('jsmith') |
|
176 | 176 | assert_response 201 |
|
177 | 177 | end |
|
178 | 178 | end |
|
179 | 179 | |
|
180 | 180 | page = WikiPage.order('id DESC').first |
|
181 | 181 | assert_equal 'New_subpage_from_API', page.title |
|
182 | 182 | assert_equal WikiPage.find(1), page.parent |
|
183 | 183 | end |
|
184 | ||
|
185 | test "DELETE /projects/:project_id/wiki/:title.xml should destroy the page" do | |
|
186 | assert_difference 'WikiPage.count', -1 do | |
|
187 | delete '/projects/ecookbook/wiki/CookBook_documentation.xml', {}, credentials('jsmith') | |
|
188 | assert_response 200 | |
|
189 | end | |
|
190 | ||
|
191 | assert_nil WikiPage.find_by_id(1) | |
|
192 | end | |
|
184 | 193 | end |
@@ -1,172 +1,182 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class RoutingWikiTest < ActionController::IntegrationTest |
|
21 | 21 | def test_wiki_matching |
|
22 | 22 | assert_routing( |
|
23 | 23 | { :method => 'get', :path => "/projects/567/wiki" }, |
|
24 | 24 | { :controller => 'wiki', :action => 'show', :project_id => '567' } |
|
25 | 25 | ) |
|
26 | 26 | assert_routing( |
|
27 | 27 | { :method => 'get', :path => "/projects/567/wiki/lalala" }, |
|
28 | 28 | { :controller => 'wiki', :action => 'show', :project_id => '567', |
|
29 | 29 | :id => 'lalala' } |
|
30 | 30 | ) |
|
31 | 31 | assert_routing( |
|
32 | 32 | { :method => 'get', :path => "/projects/567/wiki/lalala.pdf" }, |
|
33 | 33 | { :controller => 'wiki', :action => 'show', :project_id => '567', |
|
34 | 34 | :id => 'lalala', :format => 'pdf' } |
|
35 | 35 | ) |
|
36 | 36 | assert_routing( |
|
37 | 37 | { :method => 'get', :path => "/projects/1/wiki/CookBook_documentation/diff" }, |
|
38 | 38 | { :controller => 'wiki', :action => 'diff', :project_id => '1', |
|
39 | 39 | :id => 'CookBook_documentation' } |
|
40 | 40 | ) |
|
41 | 41 | assert_routing( |
|
42 | 42 | { :method => 'get', :path => "/projects/1/wiki/CookBook_documentation/2" }, |
|
43 | 43 | { :controller => 'wiki', :action => 'show', :project_id => '1', |
|
44 | 44 | :id => 'CookBook_documentation', :version => '2' } |
|
45 | 45 | ) |
|
46 | 46 | assert_routing( |
|
47 | 47 | { :method => 'get', :path => "/projects/1/wiki/CookBook_documentation/2/diff" }, |
|
48 | 48 | { :controller => 'wiki', :action => 'diff', :project_id => '1', |
|
49 | 49 | :id => 'CookBook_documentation', :version => '2' } |
|
50 | 50 | ) |
|
51 | 51 | assert_routing( |
|
52 | 52 | { :method => 'get', :path => "/projects/1/wiki/CookBook_documentation/2/annotate" }, |
|
53 | 53 | { :controller => 'wiki', :action => 'annotate', :project_id => '1', |
|
54 | 54 | :id => 'CookBook_documentation', :version => '2' } |
|
55 | 55 | ) |
|
56 | 56 | end |
|
57 | 57 | |
|
58 | 58 | def test_wiki_misc |
|
59 | 59 | assert_routing( |
|
60 | 60 | { :method => 'get', :path => "/projects/567/wiki/date_index" }, |
|
61 | 61 | { :controller => 'wiki', :action => 'date_index', :project_id => '567' } |
|
62 | 62 | ) |
|
63 | 63 | assert_routing( |
|
64 | 64 | { :method => 'get', :path => "/projects/567/wiki/export" }, |
|
65 | 65 | { :controller => 'wiki', :action => 'export', :project_id => '567' } |
|
66 | 66 | ) |
|
67 | 67 | assert_routing( |
|
68 | 68 | { :method => 'get', :path => "/projects/567/wiki/export.pdf" }, |
|
69 | 69 | { :controller => 'wiki', :action => 'export', :project_id => '567', :format => 'pdf' } |
|
70 | 70 | ) |
|
71 | 71 | assert_routing( |
|
72 | 72 | { :method => 'get', :path => "/projects/567/wiki/index" }, |
|
73 | 73 | { :controller => 'wiki', :action => 'index', :project_id => '567' } |
|
74 | 74 | ) |
|
75 | 75 | end |
|
76 | 76 | |
|
77 | 77 | def test_wiki_resources |
|
78 | 78 | assert_routing( |
|
79 | 79 | { :method => 'get', :path => "/projects/567/wiki/my_page/edit" }, |
|
80 | 80 | { :controller => 'wiki', :action => 'edit', :project_id => '567', |
|
81 | 81 | :id => 'my_page' } |
|
82 | 82 | ) |
|
83 | 83 | assert_routing( |
|
84 | 84 | { :method => 'get', :path => "/projects/1/wiki/CookBook_documentation/history" }, |
|
85 | 85 | { :controller => 'wiki', :action => 'history', :project_id => '1', |
|
86 | 86 | :id => 'CookBook_documentation' } |
|
87 | 87 | ) |
|
88 | 88 | assert_routing( |
|
89 | 89 | { :method => 'get', :path => "/projects/22/wiki/ladida/rename" }, |
|
90 | 90 | { :controller => 'wiki', :action => 'rename', :project_id => '22', |
|
91 | 91 | :id => 'ladida' } |
|
92 | 92 | ) |
|
93 | 93 | ["post", "put"].each do |method| |
|
94 | 94 | assert_routing( |
|
95 | 95 | { :method => method, :path => "/projects/567/wiki/CookBook_documentation/preview" }, |
|
96 | 96 | { :controller => 'wiki', :action => 'preview', :project_id => '567', |
|
97 | 97 | :id => 'CookBook_documentation' } |
|
98 | 98 | ) |
|
99 | 99 | end |
|
100 | 100 | assert_routing( |
|
101 | 101 | { :method => 'post', :path => "/projects/22/wiki/ladida/rename" }, |
|
102 | 102 | { :controller => 'wiki', :action => 'rename', :project_id => '22', |
|
103 | 103 | :id => 'ladida' } |
|
104 | 104 | ) |
|
105 | 105 | assert_routing( |
|
106 | 106 | { :method => 'post', :path => "/projects/22/wiki/ladida/protect" }, |
|
107 | 107 | { :controller => 'wiki', :action => 'protect', :project_id => '22', |
|
108 | 108 | :id => 'ladida' } |
|
109 | 109 | ) |
|
110 | 110 | assert_routing( |
|
111 | 111 | { :method => 'post', :path => "/projects/22/wiki/ladida/add_attachment" }, |
|
112 | 112 | { :controller => 'wiki', :action => 'add_attachment', :project_id => '22', |
|
113 | 113 | :id => 'ladida' } |
|
114 | 114 | ) |
|
115 | 115 | assert_routing( |
|
116 | 116 | { :method => 'put', :path => "/projects/567/wiki/my_page" }, |
|
117 | 117 | { :controller => 'wiki', :action => 'update', :project_id => '567', |
|
118 | 118 | :id => 'my_page' } |
|
119 | 119 | ) |
|
120 | 120 | assert_routing( |
|
121 | 121 | { :method => 'delete', :path => "/projects/22/wiki/ladida" }, |
|
122 | 122 | { :controller => 'wiki', :action => 'destroy', :project_id => '22', |
|
123 | 123 | :id => 'ladida' } |
|
124 | 124 | ) |
|
125 | 125 | assert_routing( |
|
126 | 126 | { :method => 'delete', :path => "/projects/22/wiki/ladida/3" }, |
|
127 | 127 | { :controller => 'wiki', :action => 'destroy_version', :project_id => '22', |
|
128 | 128 | :id => 'ladida', :version => '3' } |
|
129 | 129 | ) |
|
130 | 130 | end |
|
131 | 131 | |
|
132 | 132 | def test_api |
|
133 | 133 | assert_routing( |
|
134 | 134 | { :method => 'get', :path => "/projects/567/wiki/my_page.xml" }, |
|
135 | 135 | { :controller => 'wiki', :action => 'show', :project_id => '567', |
|
136 | 136 | :id => 'my_page', :format => 'xml' } |
|
137 | 137 | ) |
|
138 | 138 | assert_routing( |
|
139 | 139 | { :method => 'get', :path => "/projects/567/wiki/my_page.json" }, |
|
140 | 140 | { :controller => 'wiki', :action => 'show', :project_id => '567', |
|
141 | 141 | :id => 'my_page', :format => 'json' } |
|
142 | 142 | ) |
|
143 | 143 | assert_routing( |
|
144 | 144 | { :method => 'get', :path => "/projects/1/wiki/CookBook_documentation/2.xml" }, |
|
145 | 145 | { :controller => 'wiki', :action => 'show', :project_id => '1', |
|
146 | 146 | :id => 'CookBook_documentation', :version => '2', :format => 'xml' } |
|
147 | 147 | ) |
|
148 | 148 | assert_routing( |
|
149 | 149 | { :method => 'get', :path => "/projects/1/wiki/CookBook_documentation/2.json" }, |
|
150 | 150 | { :controller => 'wiki', :action => 'show', :project_id => '1', |
|
151 | 151 | :id => 'CookBook_documentation', :version => '2', :format => 'json' } |
|
152 | 152 | ) |
|
153 | 153 | assert_routing( |
|
154 | 154 | { :method => 'get', :path => "/projects/567/wiki/index.xml" }, |
|
155 | 155 | { :controller => 'wiki', :action => 'index', :project_id => '567', :format => 'xml' } |
|
156 | 156 | ) |
|
157 | 157 | assert_routing( |
|
158 | 158 | { :method => 'get', :path => "/projects/567/wiki/index.json" }, |
|
159 | 159 | { :controller => 'wiki', :action => 'index', :project_id => '567', :format => 'json' } |
|
160 | 160 | ) |
|
161 | 161 | assert_routing( |
|
162 | 162 | { :method => 'put', :path => "/projects/567/wiki/my_page.xml" }, |
|
163 | 163 | { :controller => 'wiki', :action => 'update', :project_id => '567', |
|
164 | 164 | :id => 'my_page', :format => 'xml' } |
|
165 | 165 | ) |
|
166 | 166 | assert_routing( |
|
167 | 167 | { :method => 'put', :path => "/projects/567/wiki/my_page.json" }, |
|
168 | 168 | { :controller => 'wiki', :action => 'update', :project_id => '567', |
|
169 | 169 | :id => 'my_page', :format => 'json' } |
|
170 | 170 | ) |
|
171 | assert_routing( | |
|
172 | { :method => 'delete', :path => "/projects/567/wiki/my_page.xml" }, | |
|
173 | { :controller => 'wiki', :action => 'destroy', :project_id => '567', | |
|
174 | :id => 'my_page', :format => 'xml' } | |
|
175 | ) | |
|
176 | assert_routing( | |
|
177 | { :method => 'delete', :path => "/projects/567/wiki/my_page.json" }, | |
|
178 | { :controller => 'wiki', :action => 'destroy', :project_id => '567', | |
|
179 | :id => 'my_page', :format => 'json' } | |
|
180 | ) | |
|
171 | 181 | end |
|
172 | 182 | end |
General Comments 0
You need to be logged in to leave comments.
Login now