Fixed that viewing/editing a wiki page without WikiContent raises an error (#14986).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@12220 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2013-10-13 11:19:47 +00:00
parent 94e7df78ca
commit 5f747faa58
3 changed files with 35 additions and 12 deletions

View File

@ -62,7 +62,12 @@ class WikiController < ApplicationController
# display a page (in editing mode if it doesn't exist) # display a page (in editing mode if it doesn't exist)
def show def show
if @page.new_record? if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
deny_access
return
end
@content = @page.content_for_version(params[:version])
if @content.nil?
if User.current.allowed_to?(:edit_wiki_pages, @project) && editable? && !api_request? if User.current.allowed_to?(:edit_wiki_pages, @project) && editable? && !api_request?
edit edit
render :action => 'edit' render :action => 'edit'
@ -71,11 +76,6 @@ class WikiController < ApplicationController
end end
return return
end end
if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
deny_access
return
end
@content = @page.content_for_version(params[:version])
if User.current.allowed_to?(:export_wiki_pages, @project) if User.current.allowed_to?(:export_wiki_pages, @project)
if params[:format] == 'pdf' if params[:format] == 'pdf'
send_data(wiki_page_to_pdf(@page, @project), :type => 'application/pdf', :filename => "#{@page.title}.pdf") send_data(wiki_page_to_pdf(@page, @project), :type => 'application/pdf', :filename => "#{@page.title}.pdf")
@ -104,19 +104,19 @@ class WikiController < ApplicationController
def edit def edit
return render_403 unless editable? return render_403 unless editable?
if @page.new_record? if @page.new_record?
@page.content = WikiContent.new(:page => @page)
if params[:parent].present? if params[:parent].present?
@page.parent = @page.wiki.find_page(params[:parent].to_s) @page.parent = @page.wiki.find_page(params[:parent].to_s)
end end
end end
@content = @page.content_for_version(params[:version]) @content = @page.content_for_version(params[:version])
@content ||= WikiContent.new(:page => @page)
@content.text = initial_page_content(@page) if @content.text.blank? @content.text = initial_page_content(@page) if @content.text.blank?
# don't keep previous comment # don't keep previous comment
@content.comments = nil @content.comments = nil
# To prevent StaleObjectError exception when reverting to a previous version # To prevent StaleObjectError exception when reverting to a previous version
@content.version = @page.content.version @content.version = @page.content.version if @page.content
@text = @content.text @text = @content.text
if params[:section].present? && Redmine::WikiFormatting.supports_section_edit? if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
@ -130,10 +130,9 @@ class WikiController < ApplicationController
def update def update
return render_403 unless editable? return render_403 unless editable?
was_new_page = @page.new_record? was_new_page = @page.new_record?
@page.content = WikiContent.new(:page => @page) if @page.new_record?
@page.safe_attributes = params[:wiki_page] @page.safe_attributes = params[:wiki_page]
@content = @page.content @content = @page.content || WikiContent.new(:page => @page)
content_params = params[:content] content_params = params[:content]
if content_params.nil? && params[:wiki_page].is_a?(Hash) if content_params.nil? && params[:wiki_page].is_a?(Hash)
content_params = params[:wiki_page].slice(:text, :comments, :version) content_params = params[:wiki_page].slice(:text, :comments, :version)
@ -152,7 +151,7 @@ class WikiController < ApplicationController
end end
@content.author = User.current @content.author = User.current
if @page.save_with_content if @page.save_with_content(@content)
attachments = Attachment.attach_files(@page, params[:attachments]) attachments = Attachment.attach_files(@page, params[:attachments])
render_attachment_warning_if_needed(@page) render_attachment_warning_if_needed(@page)
call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page}) call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})

View File

@ -175,9 +175,10 @@ class WikiPage < ActiveRecord::Base
end end
# Saves the page and its content if text was changed # Saves the page and its content if text was changed
def save_with_content def save_with_content(content)
ret = nil ret = nil
transaction do transaction do
self.content = content
if new_record? if new_record?
# Rails automatically saves associated content # Rails automatically saves associated content
ret = save ret = save

View File

@ -177,6 +177,16 @@ class WikiControllerTest < ActionController::TestCase
assert_response 302 assert_response 302
end end
def test_show_page_without_content_should_display_the_edit_form
@request.session[:user_id] = 2
WikiPage.create!(:title => 'NoContent', :wiki => Project.find(1).wiki)
get :show, :project_id => 1, :id => 'NoContent'
assert_response :success
assert_template 'edit'
assert_select 'textarea[name=?]', 'content[text]'
end
def test_create_page def test_create_page
@request.session[:user_id] = 2 @request.session[:user_id] = 2
assert_difference 'WikiPage.count' do assert_difference 'WikiPage.count' do
@ -412,6 +422,19 @@ class WikiControllerTest < ActionController::TestCase
assert_equal 2, c.version assert_equal 2, c.version
end end
def test_update_page_without_content_should_create_content
@request.session[:user_id] = 2
page = WikiPage.create!(:title => 'NoContent', :wiki => Project.find(1).wiki)
assert_no_difference 'WikiPage.count' do
assert_difference 'WikiContent.count' do
put :update, :project_id => 1, :id => 'NoContent', :content => {:text => 'Some content'}
assert_response 302
end
end
assert_equal 'Some content', page.reload.content.text
end
def test_update_section def test_update_section
@request.session[:user_id] = 2 @request.session[:user_id] = 2
page = WikiPage.find_by_title('Page_with_sections') page = WikiPage.find_by_title('Page_with_sections')