]> source.dussan.org Git - redmine.git/commitdiff
Fixed that viewing/editing a wiki page without WikiContent raises an error (#14986).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 13 Oct 2013 11:19:47 +0000 (11:19 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sun, 13 Oct 2013 11:19:47 +0000 (11:19 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@12220 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/controllers/wiki_controller.rb
app/models/wiki_page.rb
test/functional/wiki_controller_test.rb

index 17faf2a74e227292d6a80936dab508bd80be2520..8c301151000caccffb515d2f30fb84502f4157dd 100644 (file)
@@ -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})
index c17fa75d753066901d78872b397791f558b249a8..db723ecb5bae0632f43d1abe322f1214d665e846 100644 (file)
@@ -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
index 737a29947107d4fded67bee92bfc47d30754353e..6bf93ea4f19ed4899ff4e72f9dc62398bd19485b 100644 (file)
@@ -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')