From 5f747faa58c0756827bc13276954136451f6bbc2 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sun, 13 Oct 2013 11:19:47 +0000 Subject: [PATCH] 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 --- app/controllers/wiki_controller.rb | 21 ++++++++++----------- app/models/wiki_page.rb | 3 ++- test/functional/wiki_controller_test.rb | 23 +++++++++++++++++++++++ 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 17faf2a74..8c3011510 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -62,7 +62,12 @@ class WikiController < ApplicationController # display a page (in editing mode if it doesn't exist) 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? edit render :action => 'edit' @@ -71,11 +76,6 @@ class WikiController < ApplicationController end return 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 params[:format] == '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 return render_403 unless editable? if @page.new_record? - @page.content = WikiContent.new(:page => @page) if params[:parent].present? @page.parent = @page.wiki.find_page(params[:parent].to_s) end end @content = @page.content_for_version(params[:version]) + @content ||= WikiContent.new(:page => @page) @content.text = initial_page_content(@page) if @content.text.blank? # don't keep previous comment @content.comments = nil # 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 if params[:section].present? && Redmine::WikiFormatting.supports_section_edit? @@ -130,10 +130,9 @@ class WikiController < ApplicationController def update return render_403 unless editable? was_new_page = @page.new_record? - @page.content = WikiContent.new(:page => @page) if @page.new_record? @page.safe_attributes = params[:wiki_page] - @content = @page.content + @content = @page.content || WikiContent.new(:page => @page) content_params = params[:content] if content_params.nil? && params[:wiki_page].is_a?(Hash) content_params = params[:wiki_page].slice(:text, :comments, :version) @@ -152,7 +151,7 @@ class WikiController < ApplicationController end @content.author = User.current - if @page.save_with_content + if @page.save_with_content(@content) attachments = Attachment.attach_files(@page, params[:attachments]) render_attachment_warning_if_needed(@page) call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page}) diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index c17fa75d7..db723ecb5 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -175,9 +175,10 @@ class WikiPage < ActiveRecord::Base end # Saves the page and its content if text was changed - def save_with_content + def save_with_content(content) ret = nil transaction do + self.content = content if new_record? # Rails automatically saves associated content ret = save diff --git a/test/functional/wiki_controller_test.rb b/test/functional/wiki_controller_test.rb index 737a29947..6bf93ea4f 100644 --- a/test/functional/wiki_controller_test.rb +++ b/test/functional/wiki_controller_test.rb @@ -177,6 +177,16 @@ class WikiControllerTest < ActionController::TestCase assert_response 302 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 @request.session[:user_id] = 2 assert_difference 'WikiPage.count' do @@ -412,6 +422,19 @@ class WikiControllerTest < ActionController::TestCase assert_equal 2, c.version 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 @request.session[:user_id] = 2 page = WikiPage.find_by_title('Page_with_sections') -- 2.39.5